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/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>
46 class Texture: public RefObject,
47 public GlResourceOwner
52 * Used to define the area of the texture to display
54 typedef Rect<int> PixelArea;
57 * Used to define a region of a bitmap.
59 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
62 * Initialization method for Texture.
63 * Might or might not be needed in specific implementations.
64 * @return true if successful, false otherwise
66 virtual bool Init() = 0;
69 * Update the texture with the bitmap.
71 virtual void Update(Integration::Bitmap* bitmap);
74 * Update the texture from the modified bitmap.
75 * @param area to update
77 virtual void UpdateArea( const RectArea& area );
80 * @return Return true if the texture should be updated on GL texture creation.
82 virtual bool UpdateOnCreate();
85 * Binds the texture for use.
86 * Only when Bind() is first called, does a texture create an
88 * @param target (e.g. GL_TEXTURE_2D)
89 * @param textureunit to bind to
90 * @return True if the opengl texture was created
92 virtual bool Bind(GLenum target, GLenum textureunit);
95 * Returns GL texture ID
98 unsigned int GetTextureId()
104 * Return the width of image in pixels.
107 virtual unsigned int GetWidth() const;
110 * Return the height of image in pixels.
113 virtual unsigned int GetHeight() const;
116 * Query whether the texture data has an alpha channel.
117 * @return True if the texture data has an alpha channel.
119 virtual bool HasAlphaChannel() const = 0;
122 * Query whether the texture is completely opaque
123 * @return True if all pixels of the texture data are opaque
125 virtual bool IsFullyOpaque() const = 0;
128 * Get the pixel format of the image data.
129 * @return the pixel format of the image.
131 virtual Pixel::Format GetPixelFormat() const;
134 * Sets the texture id.
135 * @param id OpenGL texture id
137 void SetTextureId(GLuint id);
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
148 void MapUV(unsigned int numVerts, Dali::Internal::Vertex3D* verts, const PixelArea* pixelArea = NULL);
151 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
153 void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
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
159 void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
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
169 void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
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
182 Texture( Context& context,
185 unsigned int imageWidth,
186 unsigned int imageHeight,
187 Pixel::Format pixelFormat );
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
195 Texture( Context& context,
198 Pixel::Format pixelFormat );
201 * Initialize texture for rendering.
202 * @return true on success
204 virtual bool CreateGlTexture() = 0;
209 * Delete the GL texture associated with it.
213 public: // From GlResourceOwner
216 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
218 virtual void GlContextDestroyed();
221 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
223 virtual void GlCleanup();
228 Texture(const Texture&);
231 Texture& operator=(const Texture& rhs);
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
240 void GetDefaultTextureCoordinates(UvRect& uv) const;
244 Context& mContext; ///< The GL Context
246 GLuint mId; ///< Texture id
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)
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)
254 Pixel::Format mPixelFormat; ///< Pixel format of the contained image data.
255 bool mDiscarded; ///< True if texture was added to the DiscardQueue
259 } // namespace Internal
263 #endif // __DALI_INTERNAL_TEXTURE_H__