Removed legacy resource tracking / logging
[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 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
10 //
11 //     http://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <stdint.h>
22
23 // INTERNAL INCLUDES
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>
34
35 namespace Dali
36 {
37 class NativeImage;
38
39
40 namespace Integration
41 {
42 class Bitmap;
43 typedef IntrusivePtr<Bitmap>   BitmapPtr;
44 }
45
46
47 namespace Internal
48 {
49 class Context;
50 class Texture;
51 class FrameBufferTexture;
52 class TextureObserver;
53
54 namespace SceneGraph
55 {
56 class RenderQueue;
57 class PostProcessResourceDispatcher;
58
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;
63
64 /**
65  * Caches textures. Owned by Render Thread
66  */
67 class TextureCache : public TextureCacheDispatcher
68 {
69 public:
70   /**
71    * Constructor
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
75    */
76   TextureCache( RenderQueue& renderQueue,
77                 PostProcessResourceDispatcher& postProcessDispatcher,
78                 Context& context );
79
80   /**
81    * Destructor
82    */
83   ~TextureCache();
84
85   /**
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
91    */
92   void CreateTexture( ResourceId        id,
93                       unsigned int      width,
94                       unsigned int      height,
95                       Pixel::Format     pixelFormat,
96                       bool              clearPixels );
97
98   /**
99    * Add a bitmap to the texture cache
100    * @param[in] id Resource Id of the bitmap
101    * @param[in] bitmap The bitmap
102    */
103   void AddBitmap( ResourceId id, Integration::BitmapPtr bitmap );
104
105   /**
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
109    */
110   void AddNativeImage( ResourceId id, NativeImagePtr nativeImage );
111
112   /**
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
118    */
119   void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
120
121   /**
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
125    */
126   void AddFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
127
128   /**
129    * Update the texture with a newly loaded bitmap
130    * @param[in] id Resource Id of the bitmap
131    * @param[in] bitmap The bitmap
132    */
133   void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
134
135  /**
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
139    */
140   void AddBitmapUploadArray( ResourceId id, const BitmapUploadArray& uploadArray );
141
142   /**
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
146    */
147   void UpdateTextureArea( ResourceId id, const RectArea& area );
148
149   /**
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
155    */
156   void ClearAreas( ResourceId id,
157                    const BitmapClearArray& area,
158                    std::size_t blockSize,
159                    uint32_t color );
160
161   /**
162    * Discard texture associated with resource ID
163    * @param[in] id Resource Id of the texture
164    */
165   void DiscardTexture( ResourceId id );
166
167   /**
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
171    * object.
172    *
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 )
177    */
178   void BindTexture( Texture* texture, ResourceId id, GLenum target, GLenum textureunit );
179
180   /**
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.
186    */
187   Texture* GetTexture( ResourceId id );
188
189   /**
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
194    */
195   FrameBufferTexture* GetFramebuffer(ResourceId id);
196
197   /**
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
201    */
202   void AddObserver( ResourceId id, TextureObserver* observer );
203
204   /**
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
208    */
209   void RemoveObserver( ResourceId id, TextureObserver* observer );
210
211 protected: // Implements TextureCacheDispatcher
212
213   /**
214    * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
215    */
216   virtual void DispatchCreateTexture( ResourceId        id,
217                                       unsigned int      width,
218                                       unsigned int      height,
219                                       Pixel::Format     pixelFormat,
220                                       bool              clearPixels );
221
222   /**
223    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
224    */
225   virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
226
227   /**
228    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
229    */
230   virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImagePtr nativeImage );
231
232   /**
233    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
234    */
235   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat );
236
237   /**
238    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
239    */
240   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImagePtr nativeImage );
241
242   /**
243    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
244    */
245   virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
246
247   /**
248    * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
249    */
250   virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
251
252   /**
253    * @copydoc TextureCacheDispatcher::DispatchUploadBitmapArrayToTexture()
254    */
255   virtual void DispatchUploadBitmapArrayToTexture( ResourceId id, const BitmapUploadArray& uploadArray );
256
257   /**
258    * @copydoc TextureCacheDispatcher::DispatchClearAreas()
259    */
260   virtual void DispatchClearAreas( ResourceId id, const BitmapClearArray& area, std::size_t blockSize, uint32_t color );
261
262   /**
263    * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
264    */
265   virtual void DispatchDiscardTexture( ResourceId id );
266
267 private:
268
269   PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
270   Context&         mContext;
271   TextureContainer mTextures;
272   TextureContainer mFramebufferTextures;
273
274   typedef std::vector< TextureObserver* > TextureObservers;
275   typedef TextureObservers::iterator      TextureObserversIter;
276
277   typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
278   typedef TextureResourceObservers::iterator       TextureResourceObserversIter;
279
280   TextureResourceObservers mObservers;
281 };
282
283
284
285 } // SceneGraph
286 } // Internal
287 } // Dali
288
289 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__