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 Flora License, Version 1.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://floralicense.org/license/
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.
24 #include <dali/public-api/common/map-wrapper.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/images/native-image.h>
27 #include <dali/public-api/math/rect.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/internal/common/owner-pointer.h>
31 #include <dali/internal/update/common/scene-graph-buffers.h>
32 #include <dali/internal/render/common/texture-cache-dispatcher.h>
33 #include <dali/internal/render/gl-resources/texture-declarations.h>
43 typedef IntrusivePtr<Bitmap> BitmapPtr;
51 class FrameBufferTexture;
52 class TextureObserver;
57 class PostProcessResourceDispatcher;
59 typedef std::map<ResourceId, TexturePointer > TextureContainer;
60 typedef std::pair<ResourceId, TexturePointer > TexturePair;
61 typedef TextureContainer::iterator TextureIter;
62 typedef TextureContainer::const_iterator TextureConstIter;
65 * Caches textures. Owned by Render Thread
67 class TextureCache : public TextureCacheDispatcher
72 * @param[in] renderQueue Queue to use for dispatching messages to this object
73 * @param[in] postProcessDispatcher Dispatcher for resource post processing requests
74 * @param[in] context GL Context
76 TextureCache( RenderQueue& renderQueue,
77 PostProcessResourceDispatcher& postProcessDispatcher,
86 * Creates a new empty texture object with the given dimensions.
87 * @param[in] width The width (pixels)
88 * @param[in] height The height (pixels)
89 * @param[in] pixelFormat The pixel format
90 * @param[in] clearPixels True if the pixel data should be cleared first
92 void CreateTexture( ResourceId id,
95 Pixel::Format pixelFormat,
99 * Add a bitmap to the texture cache
100 * @param[in] id Resource Id of the bitmap
101 * @param[in] bitmap The bitmap
103 void AddBitmap( ResourceId id, Integration::BitmapPtr bitmap );
106 * Add a native image to the texture cache
107 * @param[in] id Resource Id of the native image
108 * @param[in] nativeImage The native image
110 void AddNativeImage( ResourceId id, NativeImagePtr nativeImage );
113 * Create a framebuffer texture and add it to the texture cache
114 * @param[in] id Resource Id of the native image
115 * @param[in] width Width of the framebuffer
116 * @param[in] height Height of the framebuffer
117 * @param[in] pixelFormat Pixel format of the framebuffer
119 void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
122 * Create a framebuffer texture and add it to the texture cache
123 * @param[in] id Resource Id of the native image
124 * @param[in] nativeImage The NativeImage
126 void AddFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
129 * Update the texture with a newly loaded bitmap
130 * @param[in] id Resource Id of the bitmap
131 * @param[in] bitmap The bitmap
133 void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
136 * Add an array of bitmaps to an existing texture used as an Atlas
137 * @param[in] id Resource id of the texture
138 * @param[in] uploadArray array of upload bitmap structures
140 void AddBitmapUploadArray( ResourceId id, const BitmapUploadArray& uploadArray );
143 * Update the area of the texture from the associated bitmap
144 * @param[in] id Resource Id of the bitmap
145 * @param[in] area The area of the bitmap that has changed
147 void UpdateTextureArea( ResourceId id, const RectArea& area );
150 * Clear multiple areas of the texture
151 * @param[in] id Resource id of the texture
152 * @param[in] area Areas of the texture to clear
153 * @param[in] blockSize Size of block to clear
154 * @param[in] color Color to clear
156 void ClearAreas( ResourceId id,
157 const BitmapClearArray& area,
158 std::size_t blockSize,
162 * Discard texture associated with resource ID
163 * @param[in] id Resource Id of the texture
165 void DiscardTexture( ResourceId id );
168 * Bind a texture. On the first call, the texture will copy it's
169 * pixel data to an OpenGL texture. If it's a BitmapTexture, then
170 * it will also trigger SignalUpdated to be sent on the event thread
173 * @param[in] texture pointer to the texture
174 * @param[in] id Resource id of texture
175 * @param[in] target (e.g. GL_TEXTURE_2D)
176 * @param[in] textureunit ( e.g.: GL_TEXTURE0 )
178 void BindTexture( Texture* texture, ResourceId id, GLenum target, GLenum textureunit );
181 * Get the texture associated with the resource ID
182 * May be a texture or a framebuffer
183 * @param[in] id Resource Id of the texture
184 * @return NULL if the GL resource hasn't yet been created,
185 * or a valid pointer if it has.
187 Texture* GetTexture( ResourceId id );
190 * Get the framebuffer texture associated with the resource ID
191 * Used for writing to the framebuffer
192 * @param[in] id Resource Id of the framebuffer
193 * @return the framebuffer
195 FrameBufferTexture* GetFramebuffer(ResourceId id);
198 * Add a texture observer. Should be called in render thread
199 * @param[in] id The resource id to watch
200 * @param[in] observer The observer to add
202 void AddObserver( ResourceId id, TextureObserver* observer );
205 * Remove a texture observer. Should be called in render thread
206 * @param[in] id The resource id to stop watching
207 * @param[in] observer The observer to remove
209 void RemoveObserver( ResourceId id, TextureObserver* observer );
211 protected: // Implements TextureCacheDispatcher
214 * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
216 virtual void DispatchCreateTexture( ResourceId id,
219 Pixel::Format pixelFormat,
223 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
225 virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
228 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
230 virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImagePtr nativeImage );
233 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
235 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
238 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
240 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
243 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
245 virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
248 * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
250 virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
253 * @copydoc TextureCacheDispatcher::DispatchUploadBitmapArrayToTexture()
255 virtual void DispatchUploadBitmapArrayToTexture( ResourceId id, const BitmapUploadArray& uploadArray );
258 * @copydoc TextureCacheDispatcher::DispatchClearAreas()
260 virtual void DispatchClearAreas( ResourceId id, const BitmapClearArray& area, std::size_t blockSize, uint32_t color );
263 * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
265 virtual void DispatchDiscardTexture( ResourceId id );
269 PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
271 TextureContainer mTextures;
272 TextureContainer mFramebufferTextures;
274 typedef std::vector< TextureObserver* > TextureObservers;
275 typedef TextureObservers::iterator TextureObserversIter;
277 typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
278 typedef TextureResourceObservers::iterator TextureResourceObserversIter;
280 TextureResourceObservers mObservers;
289 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__