1 #ifndef __DALI_INTERNAL_TEXTURE_H__
2 #define __DALI_INTERNAL_TEXTURE_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
21 #include <dali/public-api/object/ref-object.h>
22 #include <dali/integration-api/bitmap.h>
23 #include <dali/internal/render/gl-resources/context-observer.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>
47 class Texture: public RefObject,
48 public ContextObserver,
49 public GlResourceOwner
54 * Used to define the area of the texture to display
56 typedef Rect<int> PixelArea;
59 * Used to define a region of a bitmap.
61 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
64 * Initialization method for Texture.
65 * Might or might not be needed in specific implementations.
66 * @return true if successful, false otherwise
68 virtual bool Init() = 0;
71 * Update the texture with the bitmap.
73 virtual void Update(Integration::Bitmap* bitmap);
76 * Update the texture from the modified bitmap.
77 * @param area to update
79 virtual void UpdateArea( const RectArea& area );
82 * @return Return true if the texture should be updated on GL texture creation.
84 virtual bool UpdateOnCreate();
87 * Binds the texture for use.
88 * Only when Bind() is first called, does a texture create an
90 * @param target (e.g. GL_TEXTURE_2D)
91 * @param textureunit to bind to
92 * @return True if the opengl texture was created
94 virtual bool Bind(GLenum target, GLenum textureunit);
97 * Returns GL texture ID
100 unsigned int GetTextureId()
106 * Return the width of image in pixels.
109 virtual unsigned int GetWidth() const;
112 * Return the height of image in pixels.
115 virtual unsigned int GetHeight() const;
118 * Query whether the texture data has an alpha channel.
119 * @return True if the texture data has an alpha channel.
121 virtual bool HasAlphaChannel() const = 0;
124 * Query whether the texture is completely opaque
125 * @return True if all pixels of the texture data are opaque
127 virtual bool IsFullyOpaque() const = 0;
130 * Get the pixel format of the image data.
131 * @return the pixel format of the image.
133 virtual Pixel::Format GetPixelFormat() const;
136 * Sets the texture id.
137 * @param id OpenGL texture id
139 void SetTextureId(GLuint id);
142 * When creating a texture mapped object, the developer can
143 * can assume the texture u,v coordinates have a range of 0 to 1.
144 * They then just call MapUV which will adjust uv values depending on
145 * whether a pixel area is being used or not.
146 *@param[in] numVerts number of vertices
147 *@param[out] verts pointer to an array of vertex objects
148 *@param[in] pixelArea the area of the texture to display, null = use default image area
150 void MapUV(unsigned int numVerts, Dali::Internal::Vertex3D* verts, const PixelArea* pixelArea = NULL);
153 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
155 void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
158 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
159 * @param[in] stride The number of floats on each row of the vertex object table
161 void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
164 * Gets the texture coordinates for the texture.
165 * The texture maybe in an atlas or may only be part of a texture (that's been padded to be a power of 2).
166 * 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?
167 * If the texture is an atlas it maybe rotated, to encode that information we need to use all 4 u,v points.
168 * @param[out] uv coordinates.
169 * @param[in] pixelArea the area of the texture to display, null = use default image area
171 void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
173 public: // From Context::Observer
176 * From Context::Observer, called when the OpenGL context is active.
178 virtual void GlContextCreated();
181 * From Context::Observer, called just before the OpenGL context is destroyed.
183 virtual void GlContextToBeDestroyed();
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 * @param[in] pixelFormat The pixel format
196 Texture( Context& context,
199 unsigned int imageWidth,
200 unsigned int imageHeight,
201 Pixel::Format pixelFormat );
204 * @param[in] context The GL context
205 * @param[in] width Both the buffer width and the image width (they are equal)
206 * @param[in] height Both the buffer height and the image height.
207 * @param[in] pixelFormat The pixel format
209 Texture( Context& context,
212 Pixel::Format pixelFormat );
215 * Initialize texture for rendering.
216 * @return true on success
218 virtual bool CreateGlTexture() = 0;
222 * Delete the GL texture associated with it.
226 public: // From GlResourceOwner
229 * Delete the GL texture.
230 * @pre This method is should only be called from the render-thread.
232 virtual void GlCleanup();
237 Texture(const Texture&);
240 Texture& operator=(const Texture& rhs);
243 * Helper function for GetTextureCoordinates.
244 * Gets the texture co-ordinates without using a pixel area.
245 * It is possible the image is smaller than the texture
246 * so the texture co-ordinates have to be adjusted.
247 * @param uv texture co-ordinates
249 void GetDefaultTextureCoordinates(UvRect& uv) const;
253 Context& mContext; ///< The GL Context
255 GLuint mId; ///< Texture id
257 unsigned int mWidth; ///< texture width, may be scaled power of 2 (if not in an atlas)
258 unsigned int mHeight; ///< texture width, may be scaled power of 2 (if not in an atlas)
260 unsigned int mImageWidth; ///< width of the original image (may be smaller than texture width)
261 unsigned int mImageHeight; ///< height of the original image (may be smaller than texture height)
263 Pixel::Format mPixelFormat; ///< Pixel format of the contained image data.
264 bool mDiscarded; ///< True if texture was added to the DiscardQueue
268 } // namespace Internal
272 #endif // __DALI_INTERNAL_TEXTURE_H__