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/devel-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>
41 class NativeImageInterface;
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, NativeImageInterfacePtr 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
123 * @param[in] bufferFormat Renderbuffer format of the framebuffer
125 void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat, RenderBuffer::Format bufferFormat );
128 * Create a framebuffer texture and add it to the texture cache
129 * @param[in] id Resource Id of the native image
130 * @param[in] nativeImage The NativeImage
132 void AddFrameBuffer( ResourceId id, NativeImageInterfacePtr nativeImage );
135 * Create GL texture eagerly right now instead of waiting for first use.
136 * @param[in] id The resource id corresponding to the texture.
138 void CreateGlTexture( ResourceId id );
141 * Update the texture with a newly loaded bitmap
142 * @param[in] id Resource Id of the bitmap
143 * @param[in] bitmap The bitmap
145 void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
148 * Update the texture with a newly loaded bitmap
149 * @param[in] id Resource Id of the bitmap
150 * @param[in] bitmap The bitmap
151 * @param[in] xOffset Specifies an offset in the x direction within the texture
152 * @param[in] yOffset Specifies an offset in the y direction within the texture
154 void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap, std::size_t xOffset, std::size_t yOffset );
157 * Update the part of a texture with a newly loaded bitmap
158 * May be called from Update thread
159 * @param[in] destId The ID of the texture to update
160 * @param[in] srcId The resource ID of the source bitmap
161 * @param[in] xOffset Specifies an offset in the x direction within the texture
162 * @param[in] yOffset Specifies an offset in the y direction within the texture
164 void UpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
167 * Update the texture with a pixel buffer
168 * @param[in] id Resource Id of the bitmap
169 * @param[in] pixelData The pixel data
170 * @param[in] xOffset Specifies an offset in the x direction within the texture
171 * @param[in] yOffset Specifies an offset in the y direction within the texture
173 void UpdateTexture( ResourceId id, PixelDataPtr pixelData, std::size_t xOffset, std::size_t yOffset );
176 * Update the area of the texture from the associated bitmap
177 * @param[in] id Resource Id of the bitmap
178 * @param[in] area The area of the bitmap that has changed
180 void UpdateTextureArea( ResourceId id, const RectArea& area );
183 * Discard texture associated with resource ID
184 * @param[in] id Resource Id of the texture
186 void DiscardTexture( ResourceId id );
189 * Bind a texture. On the first call, the texture will copy it's
190 * pixel data to an OpenGL texture. If it's a BitmapTexture, then
191 * it will also trigger SignalUpdated to be sent on the event thread
194 * @param[in] texture pointer to the texture
195 * @param[in] id Resource id of texture
196 * @param[in] target (e.g. GL_TEXTURE_2D)
197 * @param[in] textureunit to use
199 void BindTexture( Texture* texture, ResourceId id, GLenum target, TextureUnit textureunit );
202 * Get the texture associated with the resource ID
203 * May be a texture or a framebuffer
204 * @param[in] id Resource Id of the texture
205 * @return NULL if the GL resource hasn't yet been created,
206 * or a valid pointer if it has.
208 Texture* GetTexture( ResourceId id );
211 * Get the bitmaptexture associated with the resource ID
212 * @param[in] id Resource Id of the texture
213 * @return The texture or NULL
215 BitmapTexture* GetBitmapTexture(ResourceId id);
218 * Get the framebuffer texture associated with the resource ID
219 * Used for writing to the framebuffer
220 * @param[in] id Resource Id of the framebuffer
221 * @return the framebuffer
223 FrameBufferTexture* GetFramebuffer(ResourceId id);
226 * Add a texture observer. Should be called in render thread
227 * @param[in] id The resource id to watch
228 * @param[in] observer The observer to add
230 void AddObserver( ResourceId id, TextureObserver* observer );
233 * Remove a texture observer. Should be called in render thread
234 * @param[in] id The resource id to stop watching
235 * @param[in] observer The observer to remove
237 void RemoveObserver( ResourceId id, TextureObserver* observer );
240 * Reset all textures.
241 * This method is called when context is or has been deleted.
243 void GlContextDestroyed();
246 * Set whether textures should retain or discard their bitmaps after upload to GL
247 * @param[in] policy Whether to retain or discard bitmaps
249 void SetDiscardBitmapsPolicy( ResourcePolicy::Discardable policy );
252 * Get the discard policy.
253 * @return The discard policy.
255 ResourcePolicy::Discardable GetDiscardBitmapsPolicy();
257 protected: // Implements TextureCacheDispatcher
260 * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
262 virtual void DispatchCreateTexture( ResourceId id,
265 Pixel::Format pixelFormat,
269 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
271 virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
274 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
276 virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImageInterfacePtr nativeImage );
279 * @copydoc TextureCacheDispatcher::DispatchCreateGlTexture()
281 virtual void DispatchCreateGlTexture( ResourceId id );
284 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
286 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat, RenderBuffer::Format bufferFormat );
289 * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
291 virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImageInterfacePtr nativeImage );
294 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
296 virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
299 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
301 virtual void DispatchUpdateTexture( ResourceId id, Integration::BitmapPtr bitmap, std::size_t xOffset, std::size_t yOffset ) ;
304 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
306 virtual void DispatchUpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
309 * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
311 virtual void DispatchUpdateTexture( ResourceId destId, PixelDataPtr srcId, std::size_t xOffset, std::size_t yOffset );
314 * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
316 virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
319 * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
321 virtual void DispatchDiscardTexture( ResourceId id );
325 PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
327 TextureContainer mTextures;
328 TextureContainer mFramebufferTextures;
330 typedef std::vector< TextureObserver* > TextureObservers;
331 typedef TextureObservers::iterator TextureObserversIter;
333 typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
334 typedef TextureResourceObservers::iterator TextureResourceObserversIter;
336 TextureResourceObservers mObservers;
337 ResourcePolicy::Discardable mDiscardBitmapsPolicy;
346 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__