2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/render/gl-resources/texture.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/render/common/vertex.h>
28 #include <dali/internal/render/gl-resources/context.h>
29 #include <dali/internal/common/image-sampler.h>
40 // These match the GL specification
41 const GLint SYSTEM_MINIFY_DEFAULT = GL_NEAREST_MIPMAP_LINEAR;
42 const GLint SYSTEM_MAGNIFY_DEFAULT = GL_LINEAR;
44 // These are the Dali defaults
45 const GLint DALI_MINIFY_DEFAULT = GL_LINEAR;
46 const GLint DALI_MAGNIFY_DEFAULT = GL_LINEAR;
51 * @brief Convert a FilterMode to it's corresponding GL type.
53 * @param[in] filterMode The FilterMode type.
54 * @param[in] defaultfilterMode The filter mode to use if filterMode is DEFAULT.
55 * @param[in] defaultSystemFilterMode The filter mode to use if filterMode is NONE.
56 * @return Return the equivalent GL type.
58 GLint FilterModeToGL( FilterMode::Type filterMode, GLint defaultfilterMode, GLint defaultSystemFilterMode )
62 case FilterMode::NEAREST:
66 case FilterMode::LINEAR:
70 case FilterMode::NONE:
72 return defaultSystemFilterMode;
74 case FilterMode::DEFAULT:
76 return defaultfilterMode;
83 using Dali::Internal::Vertex2D;
84 using Dali::Internal::Vertex3D;
86 using namespace Dali::Pixel;
88 Texture::Texture(Context& context,
91 unsigned int imageWidth,
92 unsigned int imageHeight,
93 Pixel::Format pixelFormat)
96 mSamplerBitfield( 0 ),
99 mImageWidth(imageWidth),
100 mImageHeight(imageHeight),
101 mPixelFormat(pixelFormat)
107 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
108 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
111 void Texture::SetTextureId(GLuint id)
116 void Texture::Update(Integration::Bitmap* bitmap)
118 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
121 void Texture::UpdateArea( const RectArea& area )
123 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
126 bool Texture::UpdateOnCreate()
131 bool Texture::Bind(GLenum target, TextureUnit textureunit )
133 // This is the only supported type at the moment
134 DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
135 bool created = false;
139 if( CreateGlTexture() )
145 // Bind the texture id
146 mContext.BindTextureForUnit(textureunit, mId );
151 void Texture::GlContextDestroyed()
155 // reset sampler state as well
156 mSamplerBitfield = 0;
159 void Texture::GlCleanup()
161 // delete the gl texture
164 mContext.DeleteTextures(1,&mId);
169 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
171 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
174 void Texture::MapUV(unsigned int numVerts,Vertex3D *verts, const PixelArea* pixelArea)
176 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex3D)/sizeof(float), pixelArea);
179 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
183 GetTextureCoordinates(uv, pixelArea);
185 float uScale = fabsf(uv.u2 - uv.u0);
186 float vScale = fabsf(uv.v2 - uv.v0);
188 for (unsigned int i = 0; i < numVerts; ++i)
190 verts[0] = uv.u0 + verts[0] * uScale;
191 verts[1] = uv.v0 + verts[1] * vScale;
196 unsigned int Texture::GetWidth() const
201 unsigned int Texture::GetHeight() const
206 Pixel::Format Texture::GetPixelFormat() const
211 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
213 if( pixelArea == NULL )
215 GetDefaultTextureCoordinates(uv);
219 // pre-calulate the normalized values
221 const float uScale = 1.0f / float(mWidth);
222 const float vScale = 1.0f / float(mHeight);
223 const float x = uScale * float(pixelArea->x);
224 const float y = vScale * float(pixelArea->y);
225 const float width = uScale * float(pixelArea->width);
226 const float height = vScale * float(pixelArea->height);
239 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
241 if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
243 // set the uv's to display 0,0 to 1,1
248 // the texture co-ordinates go from 0 to 1. But the image is smaller than the
249 // texture, so we need to adjust the uv values.
250 float uScale = float(mImageWidth) / float(mWidth);
251 float vScale = float(mImageHeight) / float(mHeight);
263 void Texture::ApplyTextureParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
265 GLint newFilterModeGL = FilterModeToGL( newFilterMode, daliDefault, systemDefault );
266 GLint currentFilterModeGL = FilterModeToGL( currentFilterMode, daliDefault, systemDefault );
268 if( newFilterModeGL != currentFilterModeGL )
270 mContext.ActiveTexture( unit );
271 mContext.TexParameteri( GL_TEXTURE_2D, filterType, newFilterModeGL );
275 void Texture::ApplySampler( TextureUnit unit, unsigned int samplerBitfield )
277 if( mSamplerBitfield != samplerBitfield && mId != 0 )
279 ApplyTextureParameter( unit,
280 GL_TEXTURE_MIN_FILTER,
281 ImageSampler::GetMinifyFilterMode( mSamplerBitfield ),
282 ImageSampler::GetMinifyFilterMode( samplerBitfield ),
284 SYSTEM_MINIFY_DEFAULT );
286 ApplyTextureParameter( unit,
287 GL_TEXTURE_MAG_FILTER,
288 ImageSampler::GetMagnifyFilterMode( mSamplerBitfield ),
289 ImageSampler::GetMagnifyFilterMode( samplerBitfield ),
290 DALI_MAGNIFY_DEFAULT,
291 SYSTEM_MAGNIFY_DEFAULT );
293 mSamplerBitfield = samplerBitfield;
297 } // namespace Internal