1 #ifndef __DALI_INTERNAL_TEXTURE_H__
2 #define __DALI_INTERNAL_TEXTURE_H__
5 * Copyright (c) 2015 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/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/devel-api/images/pixel-data.h>
29 #include <dali/integration-api/bitmap.h>
30 #include <dali/internal/render/common/uv-rect.h>
31 #include <dali/integration-api/gl-abstraction.h>
32 #include <dali/internal/render/gl-resources/gl-resource-owner.h>
33 #include <dali/internal/render/gl-resources/texture-units.h>
50 class Texture: public RefObject,
51 public GlResourceOwner
56 * Used to define the area of the texture to display
58 typedef Rect<int> PixelArea;
61 * Used to define a region of a bitmap.
63 typedef Rect<unsigned int> RectArea; ///< rectangular area (x,y,w,h)
66 * Initialization method for Texture.
67 * Might or might not be needed in specific implementations.
68 * @return true if successful, false otherwise
70 virtual bool Init() = 0;
73 * Update the texture with the bitmap.
75 virtual void Update(Integration::Bitmap* bitmap);
78 * Update the texture from the modified bitmap.
79 * @param area to update
81 virtual void UpdateArea( const RectArea& area );
84 * Update part of the texture with a different bitmap
85 * @param[in] srcBitmap The bitmap to copy from
86 * @param [in] xOffset Specifies an offset in the x direction within the texture
87 * @param [in] yOffset Specifies an offset in the y direction within the texture
89 virtual void Update( Integration::Bitmap* srcBitmap, std::size_t xOffset, std::size_t yOffset ) {}
92 * Update part of the texture with a pixel buffer
93 * @param[in] srcPixelData The pixel data to copy from
94 * @param [in] xOffset Specifies an offset in the x direction within the texture
95 * @param [in] yOffset Specifies an offset in the y direction within the texture
97 virtual void Update( PixelData* srcPixelData, std::size_t xOffset, std::size_t yOffset ) {}
100 * @return Return true if the texture should be updated on GL texture creation.
102 virtual bool UpdateOnCreate();
105 * Binds the texture for use.
106 * If there is no GL texture yet, it tries to create one.
108 * @param target (e.g. GL_TEXTURE_2D)
109 * @param textureunit to bind to
110 * @return True if the opengl texture was created, false if there was already a texture
111 * or no texture could be created yet ( e.g. no bitmap data after context loss )
113 virtual bool Bind(GLenum target, TextureUnit textureunit);
116 * Returns GL texture ID
119 unsigned int GetTextureId()
125 * Return the width of image in pixels.
128 virtual unsigned int GetWidth() const;
131 * Return the height of image in pixels.
134 virtual unsigned int GetHeight() const;
137 * Query whether the texture data has an alpha channel.
138 * @return True if the texture data has an alpha channel.
140 virtual bool HasAlphaChannel() const = 0;
143 * Query whether the texture is completely opaque
144 * @return True if all pixels of the texture data are opaque
146 virtual bool IsFullyOpaque() const = 0;
149 * Sets the texture id.
150 * @param id OpenGL texture id
152 void SetTextureId(GLuint id);
155 * When creating a texture mapped object, the developer can
156 * assume the texture u,v coordinates have a range of 0 to 1.
157 * They then just call MapUV which will adjust uv values depending on
158 * whether a pixel area is being used or not.
159 *@param[in] numVerts number of vertices
160 *@param[out] verts pointer to an array of vertex objects
161 *@param[in] pixelArea the area of the texture to display, null = use default image area
163 void MapUV(unsigned int numVerts, Dali::Internal::Vertex2D* verts, const PixelArea* pixelArea = NULL);
166 * @copydoc MapUV(unsigned int,Dali::Internal::Vertex2D*, const PixelArea* pixelArea)
167 * @param[in] stride The number of floats on each row of the vertex object table
169 void MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea = NULL);
172 * Gets the texture coordinates for the texture.
173 * The texture maybe in an atlas or may only be part of a texture (that's been padded to be a power of 2).
174 * 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?
175 * If the texture is an atlas it maybe rotated, to encode that information we need to use all 4 u,v points.
176 * @param[out] uv coordinates.
177 * @param[in] pixelArea the area of the texture to display, null = use default image area
179 void GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea = NULL);
182 * @brief Apply the given sampler to the texture.
184 * @param[in] texture unit to use
185 * @param[in] samplerBitfield A bitfield with packed sampler options.
187 void ApplySampler( TextureUnit unit, unsigned int samplerBitfield );
193 * @param[in] context The GL context
194 * @param[in] width The buffer width
195 * @param[in] height The buffer height
196 * @param[in] imageWidth The image width
197 * @param[in] imageHeight The image height
199 Texture( Context& context,
202 unsigned int imageWidth,
203 unsigned int imageHeight );
206 * @param[in] context The GL context
207 * @param[in] width Both the buffer width and the image width (they are equal)
208 * @param[in] height Both the buffer height and the image height.
210 Texture( Context& context,
212 unsigned int height );
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 * @copydoc Dali::Internal::GlResourceOwner::GlContextDestroyed()
231 virtual void GlContextDestroyed();
234 * @copydoc Dali::Internal::GlResourceOwner::GlCleanup()
236 virtual void GlCleanup();
241 Texture(const Texture&);
244 Texture& operator=(const Texture& rhs);
247 * Helper function for GetTextureCoordinates.
248 * Gets the texture co-ordinates without using a pixel area.
249 * It is possible the image is smaller than the texture
250 * so the texture co-ordinates have to be adjusted.
251 * @param uv texture co-ordinates
253 void GetDefaultTextureCoordinates(UvRect& uv) const;
256 * @brief Apply the given texture filtering parameters.
258 * @param[in] texture unit to use
259 * @param[in] filterType Minification or magnification.
260 * @param[in] currentFilterMode The current filter mode.
261 * @param[in] newFilterMode The new filter mode.
262 * @param[in] daliDefault The default dali filter mode for the given filterType.
263 * @param[in] systemDefault The default system filter mode for the given filterType.
265 void ApplyFilterModeParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault );
268 * @brief Apply the given texture wrap mode.
270 * @param[in] texture unit to use
271 * @param[in] wrapType UWrap or VWrap
272 * @param[in] currentWrapMode The current wrap mode.
273 * @param[in] newWrapMode The new wrap mode.
275 void ApplyWrapModeParameter( TextureUnit unit, GLint wrapType, WrapMode::Type currentWrapMode, WrapMode::Type newWrapMode );
279 Context& mContext; ///< The GL Context
281 GLuint mId; ///< Texture id
283 unsigned int mSamplerBitfield; ///< The packed bitfield of the current sampler
285 unsigned int mWidth; ///< texture width, may be scaled power of 2 (if not in an atlas)
286 unsigned int mHeight; ///< texture width, may be scaled power of 2 (if not in an atlas)
288 unsigned int mImageWidth; ///< width of the original image (may be smaller than texture width)
289 unsigned int mImageHeight; ///< height of the original image (may be smaller than texture height)
292 } // namespace Internal
296 #endif // __DALI_INTERNAL_TEXTURE_H__