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