2 * Copyright (c) 2015 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;
43 const GLint SYSTEM_WRAP_DEFAULT = GL_CLAMP_TO_EDGE;
45 // These are the Dali defaults
46 const GLint DALI_MINIFY_DEFAULT = GL_LINEAR;
47 const GLint DALI_MAGNIFY_DEFAULT = GL_LINEAR;
52 * @brief Convert a FilterMode to it's corresponding GL type.
54 * @param[in] filterMode The FilterMode type.
55 * @param[in] defaultfilterMode The filter mode to use if filterMode is DEFAULT.
56 * @param[in] defaultSystemFilterMode The filter mode to use if filterMode is NONE.
57 * @return Return the equivalent GL type.
59 GLint FilterModeToGL( FilterMode::Type filterMode, GLint defaultfilterMode, GLint defaultSystemFilterMode )
63 case FilterMode::NEAREST:
67 case FilterMode::LINEAR:
71 case FilterMode::NONE:
73 return defaultSystemFilterMode;
75 case FilterMode::DEFAULT:
77 return defaultfilterMode;
84 GLint WrapModeToGL( WrapMode::Type wrapMode, GLint defaultWrapMode )
88 case WrapMode::DEFAULT:
90 return defaultWrapMode;
92 case WrapMode::CLAMP_TO_EDGE:
94 return GL_CLAMP_TO_EDGE;
96 case WrapMode::REPEAT:
100 case WrapMode::MIRRORED_REPEAT:
102 return GL_MIRRORED_REPEAT;
106 return defaultWrapMode;
109 using Dali::Internal::Vertex2D;
111 using namespace Dali::Pixel;
113 Texture::Texture(Context& context,
116 unsigned int imageWidth,
117 unsigned int imageHeight)
120 mSamplerBitfield( 0 ),
123 mImageWidth(imageWidth),
124 mImageHeight(imageHeight)
128 Texture::Texture(Context& context,
133 mSamplerBitfield( 0 ),
143 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
144 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
147 void Texture::SetTextureId(GLuint id)
152 void Texture::Update(Integration::Bitmap* bitmap)
154 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
157 void Texture::UpdateArea( const RectArea& area )
159 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
162 bool Texture::UpdateOnCreate()
167 bool Texture::Bind(GLenum target, TextureUnit textureunit )
169 // This is the only supported type at the moment
170 DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
171 bool created = false;
175 if( CreateGlTexture() )
181 // Bind the texture id
182 mContext.BindTextureForUnit(textureunit, mId );
187 void Texture::GlContextDestroyed()
191 // reset sampler state as well
192 mSamplerBitfield = 0;
195 void Texture::GlCleanup()
197 // delete the gl texture
200 mContext.DeleteTextures(1,&mId);
205 void Texture::MapUV(unsigned int numVerts,Vertex2D *verts, const PixelArea* pixelArea)
207 MapUV(numVerts, (float*)(&verts->mU), sizeof(Vertex2D)/sizeof(float), pixelArea);
210 void Texture::MapUV(unsigned int numVerts, float* verts, unsigned int stride, const PixelArea* pixelArea)
214 GetTextureCoordinates(uv, pixelArea);
216 float uScale = fabsf(uv.u2 - uv.u0);
217 float vScale = fabsf(uv.v2 - uv.v0);
219 for (unsigned int i = 0; i < numVerts; ++i)
221 verts[0] = uv.u0 + verts[0] * uScale;
222 verts[1] = uv.v0 + verts[1] * vScale;
227 unsigned int Texture::GetWidth() const
232 unsigned int Texture::GetHeight() const
237 void Texture::GetTextureCoordinates(UvRect& uv, const PixelArea* pixelArea)
239 if( pixelArea == NULL )
241 GetDefaultTextureCoordinates(uv);
245 // pre-calulate the normalized values
247 const float uScale = 1.0f / float(mWidth);
248 const float vScale = 1.0f / float(mHeight);
249 const float x = uScale * float(pixelArea->x);
250 const float y = vScale * float(pixelArea->y);
251 const float width = uScale * float(pixelArea->width);
252 const float height = vScale * float(pixelArea->height);
265 void Texture::GetDefaultTextureCoordinates(UvRect& uv) const
267 if ((mWidth == mImageWidth) && (mHeight == mImageHeight))
269 // set the uv's to display 0,0 to 1,1
274 // the texture co-ordinates go from 0 to 1. But the image is smaller than the
275 // texture, so we need to adjust the uv values.
276 float uScale = float(mImageWidth) / float(mWidth);
277 float vScale = float(mImageHeight) / float(mHeight);
289 void Texture::ApplyFilterModeParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
291 GLint newFilterModeGL = FilterModeToGL( newFilterMode, daliDefault, systemDefault );
292 GLint currentFilterModeGL = FilterModeToGL( currentFilterMode, daliDefault, systemDefault );
294 if( newFilterModeGL != currentFilterModeGL )
296 mContext.ActiveTexture( unit );
297 mContext.TexParameteri( GL_TEXTURE_2D, filterType, newFilterModeGL );
301 void Texture::ApplyWrapModeParameter( TextureUnit unit, GLint wrapType, WrapMode::Type currentWrapMode, WrapMode::Type newWrapMode )
303 GLint newWrapModeGL = WrapModeToGL( newWrapMode, SYSTEM_WRAP_DEFAULT );
304 GLint currentWrapModeGL = WrapModeToGL( currentWrapMode, SYSTEM_WRAP_DEFAULT );
306 if( newWrapModeGL != currentWrapModeGL )
308 mContext.ActiveTexture( unit );
309 mContext.TexParameteri( GL_TEXTURE_2D, wrapType, newWrapModeGL );
313 void Texture::ApplySampler( TextureUnit unit, unsigned int samplerBitfield )
315 if( mSamplerBitfield != samplerBitfield && mId != 0 )
317 ApplyFilterModeParameter( unit,
318 GL_TEXTURE_MIN_FILTER,
319 ImageSampler::GetMinifyFilterMode( mSamplerBitfield ),
320 ImageSampler::GetMinifyFilterMode( samplerBitfield ),
322 SYSTEM_MINIFY_DEFAULT );
324 ApplyFilterModeParameter( unit,
325 GL_TEXTURE_MAG_FILTER,
326 ImageSampler::GetMagnifyFilterMode( mSamplerBitfield ),
327 ImageSampler::GetMagnifyFilterMode( samplerBitfield ),
328 DALI_MAGNIFY_DEFAULT,
329 SYSTEM_MAGNIFY_DEFAULT );
331 ApplyWrapModeParameter( unit,
333 ImageSampler::GetUWrapMode( mSamplerBitfield ),
334 ImageSampler::GetUWrapMode( samplerBitfield ));
336 ApplyWrapModeParameter( unit,
338 ImageSampler::GetVWrapMode( mSamplerBitfield ),
339 ImageSampler::GetVWrapMode( samplerBitfield ));
343 mSamplerBitfield = samplerBitfield;
347 } // namespace Internal