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/internal/render/gl-resources/texture-units.h>
28 #include <dali/public-api/images/image.h>
29 #include <dali/public-api/images/pixel.h>
30 #include <dali/public-api/images/native-image.h>
31 #include <dali/public-api/math/rect.h>
32 #include <dali/public-api/actors/sampling.h>
49 class Texture: public RefObject,
50 public GlResourceOwner
55 * Used to define the area of the texture to display
57 typedef Rect<int> PixelArea;
60 * Used to define a region of a bitmap.
62 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
65 * Initialization method for Texture.
66 * Might or might not be needed in specific implementations.
67 * @return true if successful, false otherwise
69 virtual bool Init() = 0;
72 * Update the texture with the bitmap.
74 virtual void Update(Integration::Bitmap* bitmap);
77 * Update the texture from the modified bitmap.
78 * @param area to update
80 virtual void UpdateArea( const RectArea& area );
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
88 virtual void Update( Integration::Bitmap* srcBitmap, std::size_t xOffset, std::size_t yOffset ) {}
91 * @return Return true if the texture should be updated on GL texture creation.
93 virtual bool UpdateOnCreate();
96 * Binds the texture for use.
97 * If there is no GL texture yet, it tries to create one.
99 * @param target (e.g. GL_TEXTURE_2D)
100 * @param textureunit to bind to
101 * @return True if the opengl texture was created, false if there was already a texture
102 * or no texture could be created yet ( e.g. no bitmap data after context loss )
104 virtual bool Bind(GLenum target, TextureUnit textureunit);
107 * Returns GL texture ID
110 unsigned int GetTextureId()
116 * Return the width of image in pixels.
119 virtual unsigned int GetWidth() const;
122 * Return the height of image in pixels.
125 virtual unsigned int GetHeight() const;
128 * Query whether the texture data has an alpha channel.
129 * @return True if the texture data has an alpha channel.
131 virtual bool HasAlphaChannel() const = 0;
134 * Query whether the texture is completely opaque
135 * @return True if all pixels of the texture data are opaque
137 virtual bool IsFullyOpaque() const = 0;
140 * Sets the texture id.
141 * @param id OpenGL texture id
143 void SetTextureId(GLuint id);
146 * When creating a texture mapped object, the developer can
147 * can assume the texture u,v coordinates have a range of 0 to 1.
148 * They then just call MapUV which will adjust uv values depending on
149 * whether a pixel area is being used or not.
150 *@param[in] numVerts number of vertices
151 *@param[out] verts pointer to an array of vertex objects
152 *@param[in] pixelArea the area of the texture to display, null = use default image area
154 void MapUV(unsigned int numVerts, Dali::Internal::Vertex3D* verts, const PixelArea* pixelArea = NULL);
157 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
159 void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
162 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex3D*, const PixelArea* pixelArea)
163 * @param[in] stride The number of floats on each row of the vertex object table
165 void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
168 * Gets the texture coordinates for the texture.
169 * The texture maybe in an atlas or may only be part of a texture (that's been padded to be a power of 2).
170 * 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?
171 * If the texture is an atlas it maybe rotated, to encode that information we need to use all 4 u,v points.
172 * @param[out] uv coordinates.
173 * @param[in] pixelArea the area of the texture to display, null = use default image area
175 void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
178 * @brief Apply the given sampler to the texture.
180 * @param[in] texture unit to use
181 * @param[in] samplerBitfield A bitfield with packed sampler options.
183 void ApplySampler( TextureUnit unit, unsigned int samplerBitfield );
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
195 Texture( Context& context,
198 unsigned int imageWidth,
199 unsigned int imageHeight );
202 * @param[in] context The GL context
203 * @param[in] width Both the buffer width and the image width (they are equal)
204 * @param[in] height Both the buffer height and the image height.
206 Texture( Context& context,
208 unsigned int height );
211 * Initialize texture for rendering.
212 * @return true on success
214 virtual bool CreateGlTexture() = 0;
219 * Delete the GL texture associated with it.
223 public: // From GlResourceOwner
226 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
228 virtual void GlContextDestroyed();
231 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
233 virtual void GlCleanup();
238 Texture(const Texture&);
241 Texture& operator=(const Texture& rhs);
244 * Helper function for GetTextureCoordinates.
245 * Gets the texture co-ordinates without using a pixel area.
246 * It is possible the image is smaller than the texture
247 * so the texture co-ordinates have to be adjusted.
248 * @param uv texture co-ordinates
250 void GetDefaultTextureCoordinates(UvRect& uv) const;
253 * @brief Apply the given texture parameters.
255 * @param[in] texture unit to use
256 * @param[in] filterType Minification or magnification.
257 * @param[in] currentFilterMode The current filter mode.
258 * @param[in] newFilterMode The new filter mode.
259 * @param[in] daliDefault The default dali filter mode for the given filterType.
260 * @param[in] systemDefault The default system filter mode for the given filterType.
262 void ApplyTextureParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault );
266 Context& mContext; ///< The GL Context
268 GLuint mId; ///< Texture id
270 unsigned int mSamplerBitfield; ///< The packed bitfield of the current sampler
272 unsigned int mWidth; ///< texture width, may be scaled power of 2 (if not in an atlas)
273 unsigned int mHeight; ///< texture width, may be scaled power of 2 (if not in an atlas)
275 unsigned int mImageWidth; ///< width of the original image (may be smaller than texture width)
276 unsigned int mImageHeight; ///< height of the original image (may be smaller than texture height)
279 } // namespace Internal
283 #endif // __DALI_INTERNAL_TEXTURE_H__