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);
116 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
118 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
121 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
123 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
126 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
130 GetTextureCoordinates(uv, pixelArea);
132 float uScale = fabsf(uv.u2 - uv.u0);
133 float vScale = fabsf(uv.v2 - uv.v0);
135 for (unsigned int i = 0; i < numVerts; ++i)
137 verts[0] = uv.u0 + verts[0] * uScale;
138 verts[1] = uv.v0 + verts[1] * vScale;
143 unsigned int Texture::GetWidth() const
148 unsigned int Texture::GetHeight() const
153 Pixel::Format Texture::GetPixelFormat() const
159 * When an OpenGL context is created and made active we don't
160 * do anything, because we use lazy binding.
161 * This means when a texture is required that's when it's loaded
164 void Texture::GlContextCreated()
169 * From Context::Observer, called just before the OpenGL context is destroyed.
171 void Texture::GlContextToBeDestroyed()
176 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
178 if( pixelArea == NULL )
180 GetDefaultTextureCoordinates(uv);
184 // pre-calulate the normalized values
186 const float uScale = 1.0f / float(mWidth);
187 const float vScale = 1.0f / float(mHeight);
188 const float x = uScale * float(pixelArea->x);
189 const float y = vScale * float(pixelArea->y);
190 const float width = uScale * float(pixelArea->width);
191 const float height = vScale * float(pixelArea->height);
204 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
206 if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
208 // set the uv's to display 0,0 to 1,1
213 // the texture co-ordinates go from 0 to 1. But the image is smaller than the
214 // texture, so we need to adjust the uv values.
215 float uScale = float(mImageWidth) / float(mWidth);
216 float vScale = float(mImageHeight) / float(mHeight);
228 } // namespace Internal