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