Implemented face culling for SceneGraph::Material
[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/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>
38
39 namespace Dali
40 {
41 class NativeImageInterface;
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, NativeImageInterfacePtr 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    * @param[in] bufferFormat Renderbuffer format of the framebuffer
124    */
125   void AddFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat, RenderBuffer::Format bufferFormat );
126
127   /**
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
131    */
132   void AddFrameBuffer( ResourceId id, NativeImageInterfacePtr nativeImage );
133
134   /**
135    * Create GL texture eagerly right now instead of waiting for first use.
136    * @param[in] id The resource id corresponding to the texture.
137    */
138   void CreateGlTexture( ResourceId id );
139
140   /**
141    * Update the texture with a newly loaded bitmap
142    * @param[in] id Resource Id of the bitmap
143    * @param[in] bitmap The bitmap
144    */
145   void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap );
146
147   /**
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
153    */
154   void UpdateTexture( ResourceId id, Integration::BitmapPtr bitmap, std::size_t xOffset, std::size_t yOffset );
155
156   /**
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
163    */
164   void UpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
165
166   /**
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
172    */
173   void UpdateTexture( ResourceId id, PixelDataPtr pixelData, std::size_t xOffset, std::size_t yOffset );
174
175   /**
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
179    */
180   void UpdateTextureArea( ResourceId id, const RectArea& area );
181
182   /**
183    * Discard texture associated with resource ID
184    * @param[in] id Resource Id of the texture
185    */
186   void DiscardTexture( ResourceId id );
187
188   /**
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
192    * object.
193    *
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
198    */
199   void BindTexture( Texture* texture, ResourceId id, GLenum target, TextureUnit textureunit );
200
201   /**
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.
207    */
208   Texture* GetTexture( ResourceId id );
209
210   /**
211    * Get the bitmaptexture associated with the resource ID
212    * @param[in] id Resource Id of the texture
213    * @return The texture or NULL
214    */
215   BitmapTexture* GetBitmapTexture(ResourceId id);
216
217   /**
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
222    */
223   FrameBufferTexture* GetFramebuffer(ResourceId id);
224
225   /**
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
229    */
230   void AddObserver( ResourceId id, TextureObserver* observer );
231
232   /**
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
236    */
237   void RemoveObserver( ResourceId id, TextureObserver* observer );
238
239   /**
240    * Reset all textures.
241    * This method is called when context is or has been deleted.
242    */
243   void GlContextDestroyed();
244
245   /**
246    * Set whether textures should retain or discard their bitmaps after upload to GL
247    * @param[in] policy Whether to retain or discard bitmaps
248    */
249   void SetDiscardBitmapsPolicy( ResourcePolicy::Discardable policy );
250
251   /**
252    * Get the discard policy.
253    * @return The discard policy.
254    */
255   ResourcePolicy::Discardable GetDiscardBitmapsPolicy();
256
257 protected: // Implements TextureCacheDispatcher
258
259   /**
260    * @copydoc TextureCacheDispatcher::DispatchCreateTexture()
261    */
262   virtual void DispatchCreateTexture( ResourceId        id,
263                                       unsigned int      width,
264                                       unsigned int      height,
265                                       Pixel::Format     pixelFormat,
266                                       bool              clearPixels );
267
268   /**
269    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForBitmap()
270    */
271   virtual void DispatchCreateTextureForBitmap( ResourceId id, Integration::Bitmap* bitmap );
272
273   /**
274    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForNativeImage()
275    */
276   virtual void DispatchCreateTextureForNativeImage( ResourceId id, NativeImageInterfacePtr nativeImage );
277
278   /**
279    * @copydoc TextureCacheDispatcher::DispatchCreateGlTexture()
280    */
281   virtual void DispatchCreateGlTexture( ResourceId id );
282
283   /**
284    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
285    */
286   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, unsigned int width, unsigned int height, Pixel::Format pixelFormat, RenderBuffer::Format bufferFormat );
287
288   /**
289    * @copydoc TextureCacheDispatcher::DispatchCreateTextureForFramebuffer()
290    */
291   virtual void DispatchCreateTextureForFrameBuffer( ResourceId id, NativeImageInterfacePtr nativeImage );
292
293   /**
294    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
295    */
296   virtual void DispatchUpdateTexture( ResourceId id, Integration::Bitmap* bitmap );
297
298   /**
299    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
300    */
301   virtual void DispatchUpdateTexture( ResourceId id, Integration::BitmapPtr bitmap, std::size_t xOffset, std::size_t yOffset ) ;
302
303   /**
304    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
305    */
306   virtual void DispatchUpdateTexture( ResourceId destId, ResourceId srcId, std::size_t xOffset, std::size_t yOffset );
307
308   /**
309    * @copydoc TextureCacheDispatcher::DispatchUpdateTexture()
310    */
311   virtual void DispatchUpdateTexture( ResourceId destId, PixelDataPtr srcId, std::size_t xOffset, std::size_t yOffset );
312
313   /**
314    * @copydoc TextureCacheDispatcher::DispatchUpdateTextureArea()
315    */
316   virtual void DispatchUpdateTextureArea( ResourceId id, const RectArea& area );
317
318   /**
319    * @copydoc TextureCacheDispatcher::DispatchDiscardTexture()
320    */
321   virtual void DispatchDiscardTexture( ResourceId id );
322
323 private:
324
325   PostProcessResourceDispatcher& mPostProcessResourceDispatcher;
326   Context&         mContext;
327   TextureContainer mTextures;
328   TextureContainer mFramebufferTextures;
329
330   typedef std::vector< TextureObserver* > TextureObservers;
331   typedef TextureObservers::iterator      TextureObserversIter;
332
333   typedef std::map< ResourceId, TextureObservers > TextureResourceObservers;
334   typedef TextureResourceObservers::iterator       TextureResourceObserversIter;
335
336   TextureResourceObservers mObservers;
337   ResourcePolicy::Discardable mDiscardBitmapsPolicy;
338 };
339
340
341
342 } // SceneGraph
343 } // Internal
344 } // Dali
345
346 #endif //__DALI_INTERNAL_SCENE_GRAPH_TEXTURE_CACHE_H__