1 #ifndef __DALI_INTERNAL_TEXTURE_H__
2 #define __DALI_INTERNAL_TEXTURE_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/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 GlResourceOwner
53 * Used to define the area of the texture to display
55 typedef Rect<int> PixelArea;
58 * Used to define a region of a bitmap.
60 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
63 * Initialization method for Texture.
64 * Might or might not be needed in specific implementations.
65 * @return true if successful, false otherwise
67 virtual bool Init() = 0;
70 * Update the texture with the bitmap.
72 virtual void Update(Integration::Bitmap* bitmap);
75 * Update the texture from the modified bitmap.
76 * @param area to update
78 virtual void UpdateArea( const RectArea& area );
81 * @return Return true if the texture should be updated on GL texture creation.
83 virtual bool UpdateOnCreate();
86 * Binds the texture for use.
87 * Only when Bind() is first called, does a texture create an
89 * @param target (e.g. GL_TEXTURE_2D)
90 * @param textureunit to bind to
91 * @return True if the opengl texture was created
93 virtual bool Bind(GLenum target, GLenum textureunit);
96 * Returns GL texture ID
99 unsigned int GetTextureId()
105 * Return the width of image in pixels.
108 virtual unsigned int GetWidth() const;
111 * Return the height of image in pixels.
114 virtual unsigned int GetHeight() const;
117 * Query whether the texture data has an alpha channel.
118 * @return True if the texture data has an alpha channel.
120 virtual bool HasAlphaChannel() const = 0;
123 * Query whether the texture is completely opaque
124 * @return True if all pixels of the texture data are opaque
126 virtual bool IsFullyOpaque() const = 0;
129 * Get the pixel format of the image data.
130 * @return the pixel format of the image.
132 virtual Pixel::Format GetPixelFormat() const;
135 * Sets the texture id.
136 * @param id OpenGL texture id
138 void SetTextureId(GLuint id);
141 * When creating a texture mapped object, the developer can
142 * can assume the texture u,v coordinates have a range of 0 to 1.
143 * They then just call MapUV which will adjust uv values depending on
144 * whether a pixel area is being used or not.
145 *@param[in] numVerts number of vertices
146 *@param[out] verts pointer to an array of vertex objects
147 *@param[in] pixelArea the area of the texture to display, null = use default image area
149 void MapUV(unsigned int numVerts, Dali::Internal::Vertex3D* verts, const PixelArea* pixelArea = NULL);
152 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
154 void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
157 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
158 * @param[in] stride The number of floats on each row of the vertex object table
160 void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
163 * Gets the texture coordinates for the texture.
164 * The texture maybe in an atlas or may only be part of a texture (that's been padded to be a power of 2).
165 * 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?
166 * If the texture is an atlas it maybe rotated, to encode that information we need to use all 4 u,v points.
167 * @param[out] uv coordinates.
168 * @param[in] pixelArea the area of the texture to display, null = use default image area
170 void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
176 * @param[in] context The GL context
177 * @param[in] width The buffer width
178 * @param[in] height The buffer height
179 * @param[in] imageWidth The image width
180 * @param[in] imageHeight The image height
181 * @param[in] pixelFormat The pixel format
183 Texture( Context& context,
186 unsigned int imageWidth,
187 unsigned int imageHeight,
188 Pixel::Format pixelFormat );
191 * @param[in] context The GL context
192 * @param[in] width Both the buffer width and the image width (they are equal)
193 * @param[in] height Both the buffer height and the image height.
194 * @param[in] pixelFormat The pixel format
196 Texture( Context& context,
199 Pixel::Format pixelFormat );
202 * Initialize texture for rendering.
203 * @return true on success
205 virtual bool CreateGlTexture() = 0;
210 * Delete the GL texture associated with it.
214 public: // From GlResourceOwner
217 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
219 virtual void GlContextDestroyed();
222 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
224 virtual void GlCleanup();
229 Texture(const Texture&);
232 Texture& operator=(const Texture& rhs);
235 * Helper function for GetTextureCoordinates.
236 * Gets the texture co-ordinates without using a pixel area.
237 * It is possible the image is smaller than the texture
238 * so the texture co-ordinates have to be adjusted.
239 * @param uv texture co-ordinates
241 void GetDefaultTextureCoordinates(UvRect& uv) const;
245 Context& mContext; ///< The GL Context
247 GLuint mId; ///< Texture id
249 unsigned int mWidth; ///< texture width, may be scaled power of 2 (if not in an atlas)
250 unsigned int mHeight; ///< texture width, may be scaled power of 2 (if not in an atlas)
252 unsigned int mImageWidth; ///< width of the original image (may be smaller than texture width)
253 unsigned int mImageHeight; ///< height of the original image (may be smaller than texture height)
255 Pixel::Format mPixelFormat; ///< Pixel format of the contained image data.
256 bool mDiscarded; ///< True if texture was added to the DiscardQueue
260 } // namespace Internal
264 #endif // __DALI_INTERNAL_TEXTURE_H__