Removed legacy resource tracking / logging
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / texture.h
1 #ifndef __DALI_INTERNAL_TEXTURE_H__
2 #define __DALI_INTERNAL_TEXTURE_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 // INTERNAL INCLUDES
21 #include <dali/public-api/object/ref-object.h>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/internal/render/gl-resources/context-observer.h>
24 #include <dali/internal/render/common/uv-rect.h>
25 #include <dali/integration-api/gl-abstraction.h>
26 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
27 #include <dali/public-api/images/image.h>
28 #include <dali/public-api/images/pixel.h>
29 #include <dali/public-api/images/native-image.h>
30 #include <dali/public-api/math/rect.h>
31
32 namespace Dali
33 {
34
35 class NativeImage;
36
37 namespace Internal
38 {
39
40 class Context;
41 struct Vertex3D;
42 struct Vertex2D;
43
44 /**
45  * Texture class.
46  */
47 class Texture: public RefObject,
48                public ContextObserver,
49                public GlResourceOwner
50 {
51 public:
52
53   /**
54    * Used to define the area of the texture to display
55    */
56   typedef Rect<int> PixelArea;
57
58   /**
59    * Used to define a region of a bitmap.
60    */
61   typedef Rect<unsigned int>    RectArea;     ///< rectangular area (x,y,w,h)
62
63   /**
64    * Initialization method for Texture.
65    * Might or might not be needed in specific implementations.
66    * @return true if successful, false otherwise
67    */
68   virtual bool Init() = 0;
69
70   /**
71    * Update the texture with the bitmap.
72    */
73   virtual void Update(Integration::Bitmap* bitmap);
74
75   /**
76    * Update the texture from the modified bitmap.
77    * @param area to update
78    */
79   virtual void UpdateArea( const RectArea& area );
80
81   /**
82    * @return Return true if the texture should be updated on GL texture creation.
83    */
84   virtual bool UpdateOnCreate();
85
86   /**
87    * Binds the texture for use.
88    * Only when Bind() is first called, does a texture create an
89    * an opengl texture.
90    * @param target (e.g. GL_TEXTURE_2D)
91    * @param textureunit to bind to
92    * @return True if the opengl texture was created
93    */
94   virtual bool Bind(GLenum target, GLenum textureunit);
95
96   /**
97    * Returns GL texture ID
98    * @return texture id
99    */
100   unsigned int GetTextureId()
101   {
102     return mId;
103   }
104
105   /**
106    * Return the width of image in pixels.
107    * @return width
108    */
109   virtual unsigned int GetWidth() const;
110
111   /**
112    * Return the height of image in pixels.
113    * @return height
114    */
115   virtual unsigned int GetHeight() const;
116
117   /**
118    * Query whether the texture data has an alpha channel.
119    * @return True if the texture data has an alpha channel.
120    */
121   virtual bool HasAlphaChannel() const = 0;
122
123   /**
124    * Query whether the texture is completely opaque
125    * @return True if all pixels of the texture data are opaque
126    */
127   virtual bool IsFullyOpaque() const = 0;
128
129   /**
130    * Get the pixel format of the image data.
131    * @return the pixel format of the image.
132    */
133   virtual Pixel::Format GetPixelFormat() const;
134
135   /**
136    * Sets the texture id.
137    * @param id OpenGL texture id
138    */
139   void SetTextureId(GLuint id);
140
141   /**
142    * When creating a texture mapped object, the developer can
143    * can assume the texture u,v coordinates have a range of 0 to 1.
144    * They then just call MapUV which will adjust uv values depending on
145    * whether a pixel area is being used or not.
146    *@param[in] numVerts number of vertices
147    *@param[out] verts pointer to an array of vertex objects
148    *@param[in] pixelArea the area of the texture to display, null = use default image area
149    */
150   void MapUV(unsigned int numVerts, Dali::Internal::Vertex3D* verts,  const PixelArea* pixelArea = NULL);
151
152   /**
153    * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
154    */
155   void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
156
157   /**
158    * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
159    * @param[in] stride The number of floats on each row of the vertex object table
160    */
161   void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
162
163   /**
164    * Gets the texture coordinates for the texture.
165    * The texture maybe in an atlas or may only be part of a texture (that's been padded to be a power of 2).
166    * Why do we specify u,v coordinates for all 4 points of a texture, instead of just having bottom left u,v and top right u,v?
167    * If the texture is an atlas it maybe rotated, to encode that information we need to use all 4 u,v points.
168    * @param[out] uv coordinates.
169    * @param[in] pixelArea the area of the texture to display, null = use default image area
170    */
171   void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
172
173 public: // From Context::Observer
174
175   /**
176    * From Context::Observer, called when the OpenGL context is active.
177    */
178   virtual void GlContextCreated();
179
180   /**
181    * From Context::Observer, called just before the OpenGL context is destroyed.
182    */
183   virtual void GlContextToBeDestroyed();
184
185 protected:
186
187   /**
188    * Constructor.
189    * @param[in] context The GL context
190    * @param[in] width       The buffer width
191    * @param[in] height      The buffer height
192    * @param[in] imageWidth  The image width
193    * @param[in] imageHeight The image height
194    * @param[in] pixelFormat The pixel format
195    */
196   Texture( Context&      context,
197            unsigned int  width,
198            unsigned int  height,
199            unsigned int  imageWidth,
200            unsigned int  imageHeight,
201            Pixel::Format pixelFormat );
202   /**
203    * Constructor.
204    * @param[in] context The GL context
205    * @param[in] width       Both the buffer width and the image width (they are equal)
206    * @param[in] height      Both the buffer height and the image height.
207    * @param[in] pixelFormat The pixel format
208    */
209   Texture( Context&      context,
210            unsigned int  width,
211            unsigned int  height,
212            Pixel::Format pixelFormat );
213
214   /**
215    * Initialize texture for rendering.
216    * @return true on success
217    */
218   virtual bool CreateGlTexture() = 0;
219 public:
220   /**
221    * Destructor.
222    * Delete the GL texture associated with it.
223    */
224   virtual ~Texture();
225
226 public: // From GlResourceOwner
227
228   /**
229    * Delete the GL texture.
230    * @pre This method is should only be called from the render-thread.
231    */
232   virtual void GlCleanup();
233
234 private:
235
236   // Undefined
237   Texture(const Texture&);
238
239   // Undefined
240   Texture& operator=(const Texture& rhs);
241
242   /**
243    * Helper function for GetTextureCoordinates.
244    * Gets the texture co-ordinates without using a pixel area.
245    * It is possible the image is smaller than the texture
246    * so the texture co-ordinates have to be adjusted.
247    * @param uv texture co-ordinates
248    */
249   void GetDefaultTextureCoordinates(UvRect& uv) const;
250
251 protected:
252
253   Context&      mContext;      ///< The GL Context
254
255   GLuint        mId;           ///< Texture id
256
257   unsigned int  mWidth;        ///< texture width, may be scaled power of 2 (if not in an atlas)
258   unsigned int  mHeight;       ///< texture width, may be scaled power of 2 (if not in an atlas)
259
260   unsigned int  mImageWidth;   ///< width of the original image (may be smaller than texture width)
261   unsigned int  mImageHeight;  ///< height of the original image (may be smaller than texture height)
262
263   Pixel::Format mPixelFormat;  ///< Pixel format of the contained image data.
264   bool          mDiscarded;    ///< True if texture was added to the DiscardQueue
265
266 };
267
268 } // namespace Internal
269
270 } // namespace Dali
271
272 #endif // __DALI_INTERNAL_TEXTURE_H__