Merge "Change PixelData to use the handle/body pattern" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / gl-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/integration-api/bitmap.h>
29 #include <dali/integration-api/gl-abstraction.h>
30 #include <dali/internal/event/images/pixel-data-impl.h>
31 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
32 #include <dali/internal/render/gl-resources/texture-units.h>
33
34
35 namespace Dali
36 {
37
38 class NativeImage;
39
40 namespace Internal
41 {
42
43 class Context;
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    * Update part of the texture with a pixel buffer
92    * @param[in] srcPixelData The pixel data to copy from
93    * @param [in] xOffset Specifies an offset in the x direction within the texture
94    * @param [in] yOffset Specifies an offset in the y direction within the texture
95    */
96   virtual void Update( PixelData* srcPixelData, std::size_t xOffset, std::size_t yOffset ) {}
97
98   /**
99    * @return Return true if the texture should be updated on GL texture creation.
100    */
101   virtual bool UpdateOnCreate();
102
103   /**
104    * Binds the texture for use.
105    * If there is no GL texture yet, it tries to create one.
106    *
107    * @param target (e.g. GL_TEXTURE_2D)
108    * @param textureunit to bind to
109    *
110    * @return true if the bind succeeded, or false if either the create or bind failed
111    */
112   virtual bool Bind(GLenum target, TextureUnit textureunit);
113
114   /**
115    * Returns GL texture ID
116    * @return texture id
117    */
118   unsigned int GetTextureId()
119   {
120     return mId;
121   }
122
123   /**
124    * Return the width of image in pixels.
125    * @return width
126    */
127   virtual unsigned int GetWidth() const;
128
129   /**
130    * Return the height of image in pixels.
131    * @return height
132    */
133   virtual unsigned int GetHeight() const;
134
135   /**
136    * Query whether the texture data has an alpha channel.
137    * @return True if the texture data has an alpha channel.
138    */
139   virtual bool HasAlphaChannel() const = 0;
140
141   /**
142    * Query whether the texture is completely opaque
143    * @return True if all pixels of the texture data are opaque
144    */
145   virtual bool IsFullyOpaque() const = 0;
146
147   /**
148    * Sets the texture id.
149    * @param id OpenGL texture id
150    */
151   void SetTextureId(GLuint id);
152
153   /**
154    * @brief Apply the given sampler to the texture.
155    *
156    * @param[in] texture unit to use
157    * @param[in] samplerBitfield A bitfield with packed sampler options.
158    */
159   void ApplySampler( TextureUnit unit, unsigned int samplerBitfield );
160
161 protected:
162
163   /**
164    * Constructor.
165    * @param[in] context The GL context
166    * @param[in] width       The buffer width
167    * @param[in] height      The buffer height
168    * @param[in] imageWidth  The image width
169    * @param[in] imageHeight The image height
170    */
171   Texture( Context&      context,
172            unsigned int  width,
173            unsigned int  height,
174            unsigned int  imageWidth,
175            unsigned int  imageHeight );
176   /**
177    * Constructor.
178    * @param[in] context The GL context
179    * @param[in] width       Both the buffer width and the image width (they are equal)
180    * @param[in] height      Both the buffer height and the image height.
181    */
182   Texture( Context&      context,
183            unsigned int  width,
184            unsigned int  height );
185 public:
186   /**
187    * Initialize texture for rendering.
188    * @return true on success
189    */
190   virtual bool CreateGlTexture() = 0;
191
192   /**
193    * Destructor.
194    * Delete the GL texture associated with it.
195    */
196   virtual ~Texture();
197
198 public: // From GlResourceOwner
199
200   /**
201    * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
202    */
203   virtual void GlContextDestroyed();
204
205   /**
206    * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
207    */
208   virtual void GlCleanup();
209
210 private:
211
212   // Undefined
213   Texture(const Texture&);
214
215   // Undefined
216   Texture& operator=(const Texture& rhs);
217
218   /**
219    * @brief Apply the given texture filtering parameters.
220    *
221    * @param[in] texture unit to use
222    * @param[in] filterType Minification or magnification.
223    * @param[in] currentFilterMode The current filter mode.
224    * @param[in] newFilterMode The new filter mode.
225    * @param[in] daliDefault The default dali filter mode for the given filterType.
226    * @param[in] systemDefault The default system filter mode for the given filterType.
227    */
228   void ApplyFilterModeParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault );
229
230   /**
231    * @brief Apply the given texture wrap mode.
232    *
233    * @param[in] texture unit to use
234    * @param[in] wrapType UWrap or VWrap
235    * @param[in] currentWrapMode The current wrap mode.
236    * @param[in] newWrapMode The new wrap mode.
237    */
238   void ApplyWrapModeParameter( TextureUnit unit, GLint wrapType, WrapMode::Type currentWrapMode, WrapMode::Type newWrapMode );
239
240 protected:
241
242   Context&      mContext;      ///< The GL Context
243
244   GLuint        mId;           ///< Texture id
245
246   unsigned int  mSamplerBitfield;    ///< The packed bitfield of the current sampler
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
255 } // namespace Internal
256
257 } // namespace Dali
258
259 #endif // __DALI_INTERNAL_TEXTURE_H__