2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include <dali/internal/render/gl-resources/texture.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/render/common/vertex.h>
27 #include <dali/internal/render/gl-resources/context.h>
35 using Dali::Internal::Vertex2D;
36 using Dali::Internal::Vertex3D;
38 using namespace Dali::Pixel;
40 Texture::Texture(Context& context,
43 unsigned int imageWidth,
44 unsigned int imageHeight,
45 Pixel::Format pixelFormat)
50 mImageWidth(imageWidth),
51 mImageHeight(imageHeight),
52 mPixelFormat(pixelFormat),
59 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
60 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
63 void Texture::SetTextureId(GLuint id)
68 void Texture::Update(Integration::Bitmap* bitmap)
70 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
73 void Texture::UpdateArea( const RectArea& area )
75 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
78 bool Texture::UpdateOnCreate()
83 bool Texture::Bind(GLenum target, GLenum textureunit )
85 // This is the only supported type at the moment
86 DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
91 if( CreateGlTexture() )
97 // Bind the texture id
98 mContext.ActiveTexture(textureunit);
99 mContext.Bind2dTexture(mId);
104 void Texture::GlContextDestroyed()
110 void Texture::GlCleanup()
112 // delete the gl texture
115 mContext.DeleteTextures(1,&mId);
120 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
122 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
125 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
127 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
130 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
134 GetTextureCoordinates(uv, pixelArea);
136 float uScale = fabsf(uv.u2 - uv.u0);
137 float vScale = fabsf(uv.v2 - uv.v0);
139 for (unsigned int i = 0; i < numVerts; ++i)
141 verts[0] = uv.u0 + verts[0] * uScale;
142 verts[1] = uv.v0 + verts[1] * vScale;
147 unsigned int Texture::GetWidth() const
152 unsigned int Texture::GetHeight() const
157 Pixel::Format Texture::GetPixelFormat() const
162 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
164 if( pixelArea == NULL )
166 GetDefaultTextureCoordinates(uv);
170 // pre-calulate the normalized values
172 const float uScale = 1.0f / float(mWidth);
173 const float vScale = 1.0f / float(mHeight);
174 const float x = uScale * float(pixelArea->x);
175 const float y = vScale * float(pixelArea->y);
176 const float width = uScale * float(pixelArea->width);
177 const float height = vScale * float(pixelArea->height);
190 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
192 if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
194 // set the uv's to display 0,0 to 1,1
199 // the texture co-ordinates go from 0 to 1. But the image is smaller than the
200 // texture, so we need to adjust the uv values.
201 float uScale = float(mImageWidth) / float(mWidth);
202 float vScale = float(mImageHeight) / float(mHeight);
214 } // namespace Internal