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