1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
25 #include <dali/public-api/common/map-wrapper.h>
26 #include <dali/public-api/common/intrusive-ptr.h>
27 #include <dali/public-api/images/native-image.h>
28 #include <dali/public-api/math/rect.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/integration-api/platform-abstraction.h>
31 #include <dali/integration-api/resource-policies.h>
32 #include <dali/integration-api/gl-abstraction.h>
33 #include <dali/internal/common/owner-pointer.h>
34 #include <dali/internal/update/common/scene-graph-buffers.h>
35 #include <dali/internal/render/common/texture-cache-dispatcher.h>
36 #include <dali/internal/render/gl-resources/texture-declarations.h>
37 #include <dali/internal/render/gl-resources/texture-units.h>
47 typedef IntrusivePtr<Bitmap> BitmapPtr;
56 class FrameBufferTexture;
57 class TextureObserver;
62 class PostProcessResourceDispatcher;
64 typedef std::map<ResourceId, TexturePointer > TextureContainer;
65 typedef std::pair<ResourceId, TexturePointer > TexturePair;
66 typedef TextureContainer::iterator TextureIter;
67 typedef TextureContainer::const_iterator TextureConstIter;
70 * Caches textures. Owned by Render Thread
72 class TextureCache : public TextureCacheDispatcher
77 * @param[in] renderQueue Queue to use for dispatching messages to this object
78 * @param[in] postProcessDispatcher Dispatcher for resource post processing requests
79 * @param[in] context GL Context
81 TextureCache( RenderQueue& renderQueue,
82 PostProcessResourceDispatcher& postProcessDispatcher,
91 * Creates a new empty texture object with the given dimensions.
92 * @param[in] width The width (pixels)
93 * @param[in] height The height (pixels)
94 * @param[in] pixelFormat The pixel format
95 * @param[in] clearPixels True if the pixel data should be cleared first
97 void CreateTexture( ResourceId id,
100 Pixel::Format pixelFormat,
104 * Add a bitmap to the texture cache
105 * @param[in] id Resource Id of the bitmap
106 * @param[in] bitmap The bitmap
108 void AddBitmap( ResourceId id, Integration::BitmapPtr bitmap );
111 * Add a native image to the texture cache
112 * @param[in] id Resource Id of the native image
113 * @param[in] nativeImage The native image
115 void AddNativeImage( ResourceId id, NativeImagePtr nativeImage );
118 * Create a framebuffer texture and add it to the texture cache
119 * @param[in] id Resource Id of the native image
120 * @param[in] width Width of the framebuffer
121 * @param[in] height Height of the framebuffer
122 * @param[in] pixelFormat Pixel format of the framebuffer
124 void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
127 * Create a framebuffer texture and add it to the texture cache
128 * @param[in] id Resource Id of the native image
129 * @param[in] nativeImage The NativeImage
131 void AddFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
134 * Update the texture with a newly loaded bitmap
135 * @param[in] id Resource Id of the bitmap
136 * @param[in] bitmap The bitmap
138 void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
141 * Update the part of a texture with a newly loaded bitmap
142 * May be called from Update thread
143 * @param[in] destId The ID of the texture to update
144 * @param[in] srcId The resource ID of the source bitmap
145 * @param [in] xOffset Specifies an offset in the x direction within the texture
146 * @param [in] yOffset Specifies an offset in the y direction within the texture
148 void UpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
151 * Add an array of bitmaps to an existing texture used as an Atlas
152 * @param[in] id Resource id of the texture
153 * @param[in] uploadArray array of upload bitmap structures
155 void AddBitmapUploadArray( ResourceId id, const BitmapUploadArray& uploadArray );
158 * Update the area of the texture from the associated bitmap
159 * @param[in] id Resource Id of the bitmap
160 * @param[in] area The area of the bitmap that has changed
162 void UpdateTextureArea( ResourceId id, const RectArea& area );
165 * Clear multiple areas of the texture
166 * @param[in] id Resource id of the texture
167 * @param[in] area Areas of the texture to clear
168 * @param[in] blockSize Size of block to clear
169 * @param[in] color Color to clear
171 void ClearAreas( ResourceId id,
172 const BitmapClearArray& area,
173 std::size_t blockSize,
177 * Discard texture associated with resource ID
178 * @param[in] id Resource Id of the texture
180 void DiscardTexture( ResourceId id );
183 * Bind a texture. On the first call, the texture will copy it's
184 * pixel data to an OpenGL texture. If it's a BitmapTexture, then
185 * it will also trigger SignalUpdated to be sent on the event thread
188 * @param[in] texture pointer to the texture
189 * @param[in] id Resource id of texture
190 * @param[in] target (e.g. GL_TEXTURE_2D)
191 * @param[in] textureunit to use
193 void BindTexture( Texture* texture, ResourceId id, GLenum target, TextureUnit textureunit );
196 * Get the texture associated with the resource ID
197 * May be a texture or a framebuffer
198 * @param[in] id Resource Id of the texture
199 * @return NULL if the GL resource hasn't yet been created,
200 * or a valid pointer if it has.
202 Texture* GetTexture( ResourceId id );
205 * Get the bitmaptexture associated with the resource ID
206 * @param[in] id Resource Id of the texture
207 * @return The texture or NULL
209 BitmapTexture* GetBitmapTexture(ResourceId id);
212 * Get the framebuffer texture associated with the resource ID
213 * Used for writing to the framebuffer
214 * @param[in] id Resource Id of the framebuffer
215 * @return the framebuffer
217 FrameBufferTexture* GetFramebuffer(ResourceId id);
220 * Add a texture observer. Should be called in render thread
221 * @param[in] id The resource id to watch
222 * @param[in] observer The observer to add
224 void AddObserver( ResourceId id, TextureObserver* observer );
227 * Remove a texture observer. Should be called in render thread
228 * @param[in] id The resource id to stop watching
229 * @param[in] observer The observer to remove
231 void RemoveObserver( ResourceId id, TextureObserver* observer );
234 * Reset all textures.
235 * This method is called when context is or has been deleted.
237 void GlContextDestroyed();
240 * Set whether textures should retain or discard their bitmaps after upload to GL
241 * @param[in] policy Whether to retain or discard bitmaps
243 void SetDiscardBitmapsPolicy( ResourcePolicy::Discardable policy );
246 * Get the discard policy.
247 * @return The discard policy.
249 ResourcePolicy::Discardable GetDiscardBitmapsPolicy();
251 protected: // Implements TextureCacheDispatcher
254 * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
256 virtual void DispatchCreateTexture( ResourceId id,
259 Pixel::Format pixelFormat,
263 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
265 virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
268 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
270 virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImagePtr nativeImage );
273 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
275 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
278 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
280 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
283 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
285 virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
288 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
290 virtual void DispatchUpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
293 * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
295 virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
298 * @copydoc TextureCacheDispatcher::DispatchUploadBitmapArrayToTexture()
300 virtual void DispatchUploadBitmapArrayToTexture( ResourceId id, const BitmapUploadArray& uploadArray );
303 * @copydoc TextureCacheDispatcher::DispatchClearAreas()
305 virtual void DispatchClearAreas( ResourceId id, const BitmapClearArray& area, std::size_t blockSize, uint32_t color );
308 * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
310 virtual void DispatchDiscardTexture( ResourceId id );
314 PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
316 TextureContainer mTextures;
317 TextureContainer mFramebufferTextures;
319 typedef std::vector< TextureObserver* > TextureObservers;
320 typedef TextureObservers::iterator TextureObserversIter;
322 typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
323 typedef TextureResourceObservers::iterator TextureResourceObserversIter;
325 TextureResourceObservers mObservers;
326 ResourcePolicy::Discardable mDiscardBitmapsPolicy;
335 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__