Removed unnecessary for loop
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / texture-cache.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <stdint.h>
23
24 // INTERNAL INCLUDES
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>
38
39 namespace Dali
40 {
41 class NativeImage;
42
43
44 namespace Integration
45 {
46 class Bitmap;
47 typedef IntrusivePtr<Bitmap>   BitmapPtr;
48 }
49
50
51 namespace Internal
52 {
53 class Context;
54 class Texture;
55 class BitmapTexture;
56 class FrameBufferTexture;
57 class TextureObserver;
58
59 namespace SceneGraph
60 {
61 class RenderQueue;
62 class PostProcessResourceDispatcher;
63
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;
68
69 /**
70  * Caches textures. Owned by Render Thread
71  */
72 class TextureCache : public TextureCacheDispatcher
73 {
74 public:
75  /**
76    * Constructor
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
80    */
81   TextureCache( RenderQueue& renderQueue,
82                 PostProcessResourceDispatcher& postProcessDispatcher,
83                 Context& context );
84
85   /**
86    * Destructor
87    */
88   ~TextureCache();
89
90   /**
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
96    */
97   void CreateTexture( ResourceId        id,
98                       unsigned int      width,
99                       unsigned int      height,
100                       Pixel::Format     pixelFormat,
101                       bool              clearPixels );
102
103   /**
104    * Add a bitmap to the texture cache
105    * @param[in] id Resource Id of the bitmap
106    * @param[in] bitmap The bitmap
107    */
108   void AddBitmap( ResourceId id, Integration::BitmapPtr bitmap );
109
110   /**
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
114    */
115   void AddNativeImage( ResourceId id, NativeImagePtr nativeImage );
116
117   /**
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    */
124   void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
125
126   /**
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
130    */
131   void AddFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
132
133   /**
134    * Update the texture with a newly loaded bitmap
135    * @param[in] id Resource Id of the bitmap
136    * @param[in] bitmap The bitmap
137    */
138   void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
139
140   /**
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
147    */
148   void UpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
149
150  /**
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
154    */
155   void AddBitmapUploadArray( ResourceId id, const BitmapUploadArray& uploadArray );
156
157   /**
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
161    */
162   void UpdateTextureArea( ResourceId id, const RectArea& area );
163
164   /**
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
170    */
171   void ClearAreas( ResourceId id,
172                    const BitmapClearArray& area,
173                    std::size_t blockSize,
174                    uint32_t color );
175
176   /**
177    * Discard texture associated with resource ID
178    * @param[in] id Resource Id of the texture
179    */
180   void DiscardTexture( ResourceId id );
181
182   /**
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
186    * object.
187    *
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
192    */
193   void BindTexture( Texture* texture, ResourceId id, GLenum target, TextureUnit textureunit );
194
195   /**
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.
201    */
202   Texture* GetTexture( ResourceId id );
203
204   /**
205    * Get the bitmaptexture associated with the resource ID
206    * @param[in] id Resource Id of the texture
207    * @return The texture or NULL
208    */
209   BitmapTexture* GetBitmapTexture(ResourceId id);
210
211   /**
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
216    */
217   FrameBufferTexture* GetFramebuffer(ResourceId id);
218
219   /**
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
223    */
224   void AddObserver( ResourceId id, TextureObserver* observer );
225
226   /**
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
230    */
231   void RemoveObserver( ResourceId id, TextureObserver* observer );
232
233   /**
234    * Reset all textures.
235    * This method is called when context is or has been deleted.
236    */
237   void GlContextDestroyed();
238
239   /**
240    * Set whether textures should retain or discard their bitmaps after upload to GL
241    * @param[in] policy Whether to retain or discard bitmaps
242    */
243   void SetDiscardBitmapsPolicy( ResourcePolicy::Discardable policy );
244
245   /**
246    * Get the discard policy.
247    * @return The discard policy.
248    */
249   ResourcePolicy::Discardable GetDiscardBitmapsPolicy();
250
251 protected: // Implements TextureCacheDispatcher
252
253   /**
254    * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
255    */
256   virtual void DispatchCreateTexture( ResourceId        id,
257                                       unsigned int      width,
258                                       unsigned int      height,
259                                       Pixel::Format     pixelFormat,
260                                       bool              clearPixels );
261
262   /**
263    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
264    */
265   virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
266
267   /**
268    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
269    */
270   virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImagePtr nativeImage );
271
272   /**
273    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
274    */
275   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
276
277   /**
278    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
279    */
280   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
281
282   /**
283    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
284    */
285   virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
286
287   /**
288    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
289    */
290   virtual void DispatchUpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
291
292   /**
293    * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
294    */
295   virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
296
297   /**
298    * @copydoc TextureCacheDispatcher::DispatchUploadBitmapArrayToTexture()
299    */
300   virtual void DispatchUploadBitmapArrayToTexture( ResourceId id, const BitmapUploadArray& uploadArray );
301
302   /**
303    * @copydoc TextureCacheDispatcher::DispatchClearAreas()
304    */
305   virtual void DispatchClearAreas( ResourceId id, const BitmapClearArray& area, std::size_t blockSize, uint32_t color );
306
307   /**
308    * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
309    */
310   virtual void DispatchDiscardTexture( ResourceId id );
311
312 private:
313
314   PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
315   Context&         mContext;
316   TextureContainer mTextures;
317   TextureContainer mFramebufferTextures;
318
319   typedef std::vector< TextureObserver* > TextureObservers;
320   typedef TextureObservers::iterator      TextureObserversIter;
321
322   typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
323   typedef TextureResourceObservers::iterator       TextureResourceObserversIter;
324
325   TextureResourceObservers mObservers;
326   ResourcePolicy::Discardable mDiscardBitmapsPolicy;
327 };
328
329
330
331 } // SceneGraph
332 } // Internal
333 } // Dali
334
335 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__