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