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