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),
55 mContext.AddObserver(*this);
60 mContext.RemoveObserver(*this);
61 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
62 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
65 void Texture::SetTextureId(GLuint id)
70 void Texture::Update(Integration::Bitmap* bitmap)
72 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
75 void Texture::UpdateArea( const RectArea& area )
77 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
80 bool Texture::UpdateOnCreate()
85 bool Texture::Bind(GLenum target, GLenum textureunit )
87 // This is the only supported type at the moment
88 DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
93 if( CreateGlTexture() )
99 // Bind the texture id
100 mContext.ActiveTexture(textureunit);
101 mContext.Bind2dTexture(mId);
106 void Texture::GlCleanup()
108 // otherwise, delete the gl texture
111 mContext.DeleteTextures(1,&mId);
112 DALI_LOG_RESOURCE("[DELTEXTURE] Freed image data memory for Texture %u - (%dx%d)\n",
113 mId, mWidth, mHeight);
118 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
120 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
123 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
125 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
128 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
132 GetTextureCoordinates(uv, pixelArea);
134 float uScale = fabsf(uv.u2 - uv.u0);
135 float vScale = fabsf(uv.v2 - uv.v0);
137 for (unsigned int i = 0; i < numVerts; ++i)
139 verts[0] = uv.u0 + verts[0] * uScale;
140 verts[1] = uv.v0 + verts[1] * vScale;
145 unsigned int Texture::GetWidth() const
150 unsigned int Texture::GetHeight() const
155 Pixel::Format Texture::GetPixelFormat() const
161 * When an OpenGL context is created and made active we don't
162 * do anything, because we use lazy binding.
163 * This means when a texture is required that's when it's loaded
166 void Texture::GlContextCreated()
171 * From Context::Observer, called just before the OpenGL context is destroyed.
173 void Texture::GlContextToBeDestroyed()
178 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
180 if( pixelArea == NULL )
182 GetDefaultTextureCoordinates(uv);
186 // pre-calulate the normalized values
188 const float uScale = 1.0f / float(mWidth);
189 const float vScale = 1.0f / float(mHeight);
190 const float x = uScale * float(pixelArea->x);
191 const float y = vScale * float(pixelArea->y);
192 const float width = uScale * float(pixelArea->width);
193 const float height = vScale * float(pixelArea->height);
206 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
208 if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
210 // set the uv's to display 0,0 to 1,1
215 // the texture co-ordinates go from 0 to 1. But the image is smaller than the
216 // texture, so we need to adjust the uv values.
217 float uScale = float(mImageWidth) / float(mWidth);
218 float vScale = float(mImageHeight) / float(mHeight);
230 } // namespace Internal