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/gl-abstraction.h>
31 #include <dali/internal/common/owner-pointer.h>
32 #include <dali/internal/update/common/scene-graph-buffers.h>
33 #include <dali/internal/render/common/texture-cache-dispatcher.h>
34 #include <dali/internal/render/gl-resources/texture-declarations.h>
44 typedef IntrusivePtr<Bitmap> BitmapPtr;
52 class FrameBufferTexture;
53 class TextureObserver;
58 class PostProcessResourceDispatcher;
60 typedef std::map<ResourceId, TexturePointer > TextureContainer;
61 typedef std::pair<ResourceId, TexturePointer > TexturePair;
62 typedef TextureContainer::iterator TextureIter;
63 typedef TextureContainer::const_iterator TextureConstIter;
66 * Caches textures. Owned by Render Thread
68 class TextureCache : public TextureCacheDispatcher
73 * @param[in] renderQueue Queue to use for dispatching messages to this object
74 * @param[in] postProcessDispatcher Dispatcher for resource post processing requests
75 * @param[in] context GL Context
77 TextureCache( RenderQueue& renderQueue,
78 PostProcessResourceDispatcher& postProcessDispatcher,
87 * Creates a new empty texture object with the given dimensions.
88 * @param[in] width The width (pixels)
89 * @param[in] height The height (pixels)
90 * @param[in] pixelFormat The pixel format
91 * @param[in] clearPixels True if the pixel data should be cleared first
93 void CreateTexture( ResourceId id,
96 Pixel::Format pixelFormat,
100 * Add a bitmap to the texture cache
101 * @param[in] id Resource Id of the bitmap
102 * @param[in] bitmap The bitmap
104 void AddBitmap( ResourceId id, Integration::BitmapPtr bitmap );
107 * Add a native image to the texture cache
108 * @param[in] id Resource Id of the native image
109 * @param[in] nativeImage The native image
111 void AddNativeImage( ResourceId id, NativeImagePtr nativeImage );
114 * Create a framebuffer texture and add it to the texture cache
115 * @param[in] id Resource Id of the native image
116 * @param[in] width Width of the framebuffer
117 * @param[in] height Height of the framebuffer
118 * @param[in] pixelFormat Pixel format of the framebuffer
120 void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
123 * Create a framebuffer texture and add it to the texture cache
124 * @param[in] id Resource Id of the native image
125 * @param[in] nativeImage The NativeImage
127 void AddFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
130 * Update the texture with a newly loaded bitmap
131 * @param[in] id Resource Id of the bitmap
132 * @param[in] bitmap The bitmap
134 void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
137 * Add an array of bitmaps to an existing texture used as an Atlas
138 * @param[in] id Resource id of the texture
139 * @param[in] uploadArray array of upload bitmap structures
141 void AddBitmapUploadArray( ResourceId id, const BitmapUploadArray& uploadArray );
144 * Update the area of the texture from the associated bitmap
145 * @param[in] id Resource Id of the bitmap
146 * @param[in] area The area of the bitmap that has changed
148 void UpdateTextureArea( ResourceId id, const RectArea& area );
151 * Clear multiple areas of the texture
152 * @param[in] id Resource id of the texture
153 * @param[in] area Areas of the texture to clear
154 * @param[in] blockSize Size of block to clear
155 * @param[in] color Color to clear
157 void ClearAreas( ResourceId id,
158 const BitmapClearArray& area,
159 std::size_t blockSize,
163 * Discard texture associated with resource ID
164 * @param[in] id Resource Id of the texture
166 void DiscardTexture( ResourceId id );
169 * Bind a texture. On the first call, the texture will copy it's
170 * pixel data to an OpenGL texture. If it's a BitmapTexture, then
171 * it will also trigger SignalUpdated to be sent on the event thread
174 * @param[in] texture pointer to the texture
175 * @param[in] id Resource id of texture
176 * @param[in] target (e.g. GL_TEXTURE_2D)
177 * @param[in] textureunit ( e.g.: GL_TEXTURE0 )
179 void BindTexture( Texture* texture, ResourceId id, GLenum target, GLenum textureunit );
182 * Get the texture associated with the resource ID
183 * May be a texture or a framebuffer
184 * @param[in] id Resource Id of the texture
185 * @return NULL if the GL resource hasn't yet been created,
186 * or a valid pointer if it has.
188 Texture* GetTexture( ResourceId id );
191 * Get the framebuffer texture associated with the resource ID
192 * Used for writing to the framebuffer
193 * @param[in] id Resource Id of the framebuffer
194 * @return the framebuffer
196 FrameBufferTexture* GetFramebuffer(ResourceId id);
199 * Add a texture observer. Should be called in render thread
200 * @param[in] id The resource id to watch
201 * @param[in] observer The observer to add
203 void AddObserver( ResourceId id, TextureObserver* observer );
206 * Remove a texture observer. Should be called in render thread
207 * @param[in] id The resource id to stop watching
208 * @param[in] observer The observer to remove
210 void RemoveObserver( ResourceId id, TextureObserver* observer );
213 * Reset all textures.
214 * This method is called when context is or has been deleted.
216 void GlContextDestroyed();
218 protected: // Implements TextureCacheDispatcher
221 * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
223 virtual void DispatchCreateTexture( ResourceId id,
226 Pixel::Format pixelFormat,
230 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
232 virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
235 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
237 virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImagePtr nativeImage );
240 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
242 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
245 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
247 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
250 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
252 virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
255 * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
257 virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
260 * @copydoc TextureCacheDispatcher::DispatchUploadBitmapArrayToTexture()
262 virtual void DispatchUploadBitmapArrayToTexture( ResourceId id, const BitmapUploadArray& uploadArray );
265 * @copydoc TextureCacheDispatcher::DispatchClearAreas()
267 virtual void DispatchClearAreas( ResourceId id, const BitmapClearArray& area, std::size_t blockSize, uint32_t color );
270 * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
272 virtual void DispatchDiscardTexture( ResourceId id );
276 PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
278 TextureContainer mTextures;
279 TextureContainer mFramebufferTextures;
281 typedef std::vector< TextureObserver* > TextureObservers;
282 typedef TextureObservers::iterator TextureObserversIter;
284 typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
285 typedef TextureResourceObservers::iterator TextureResourceObserversIter;
287 TextureResourceObservers mObservers;
296 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__