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/gl-resources/context.h>
28 #include <dali/internal/common/image-sampler.h>
39 // These match the GL specification
40 const GLint SYSTEM_MINIFY_DEFAULT = GL_NEAREST_MIPMAP_LINEAR;
41 const GLint SYSTEM_MAGNIFY_DEFAULT = GL_LINEAR;
42 const GLint SYSTEM_WRAP_DEFAULT = GL_CLAMP_TO_EDGE;
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 GLint WrapModeToGL( WrapMode::Type wrapMode, GLint defaultWrapMode )
87 case WrapMode::DEFAULT:
89 return defaultWrapMode;
91 case WrapMode::CLAMP_TO_EDGE:
93 return GL_CLAMP_TO_EDGE;
95 case WrapMode::REPEAT:
99 case WrapMode::MIRRORED_REPEAT:
101 return GL_MIRRORED_REPEAT;
105 return defaultWrapMode;
108 using Dali::Internal::Vertex2D;
110 using namespace Dali::Pixel;
112 Texture::Texture(Context& context,
115 unsigned int imageWidth,
116 unsigned int imageHeight)
119 mSamplerBitfield( 0 ),
122 mImageWidth(imageWidth),
123 mImageHeight(imageHeight)
127 Texture::Texture(Context& context,
132 mSamplerBitfield( 0 ),
142 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
143 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
146 void Texture::SetTextureId(GLuint id)
151 void Texture::Update(Integration::Bitmap* bitmap)
153 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
156 void Texture::UpdateArea( const RectArea& area )
158 DALI_ASSERT_DEBUG( "Updating incorrect texture type" == NULL );
161 bool Texture::UpdateOnCreate()
166 bool Texture::Bind(GLenum target, TextureUnit textureunit )
168 // This is the only supported type at the moment
169 DALI_ASSERT_DEBUG( target == GL_TEXTURE_2D );
170 bool created = false;
174 if( CreateGlTexture() )
180 // Bind the texture id
181 mContext.BindTextureForUnit(textureunit, mId );
186 void Texture::GlContextDestroyed()
190 // reset sampler state as well
191 mSamplerBitfield = 0;
194 void Texture::GlCleanup()
196 // delete the gl texture
199 mContext.DeleteTextures(1,&mId);
204 unsigned int Texture::GetWidth() const
209 unsigned int Texture::GetHeight() const
214 void Texture::ApplyFilterModeParameter( TextureUnit unit, GLint filterType, FilterMode::Type currentFilterMode, FilterMode::Type newFilterMode, GLint daliDefault, GLint systemDefault )
216 GLint newFilterModeGL = FilterModeToGL( newFilterMode, daliDefault, systemDefault );
217 GLint currentFilterModeGL = FilterModeToGL( currentFilterMode, daliDefault, systemDefault );
219 if( newFilterModeGL != currentFilterModeGL )
221 mContext.ActiveTexture( unit );
222 mContext.TexParameteri( GL_TEXTURE_2D, filterType, newFilterModeGL );
226 void Texture::ApplyWrapModeParameter( TextureUnit unit, GLint wrapType, WrapMode::Type currentWrapMode, WrapMode::Type newWrapMode )
228 GLint newWrapModeGL = WrapModeToGL( newWrapMode, SYSTEM_WRAP_DEFAULT );
229 GLint currentWrapModeGL = WrapModeToGL( currentWrapMode, SYSTEM_WRAP_DEFAULT );
231 if( newWrapModeGL != currentWrapModeGL )
233 mContext.ActiveTexture( unit );
234 mContext.TexParameteri( GL_TEXTURE_2D, wrapType, newWrapModeGL );
238 void Texture::ApplySampler( TextureUnit unit, unsigned int samplerBitfield )
240 if( mSamplerBitfield != samplerBitfield && mId != 0 )
242 ApplyFilterModeParameter( unit,
243 GL_TEXTURE_MIN_FILTER,
244 ImageSampler::GetMinifyFilterMode( mSamplerBitfield ),
245 ImageSampler::GetMinifyFilterMode( samplerBitfield ),
247 SYSTEM_MINIFY_DEFAULT );
249 ApplyFilterModeParameter( unit,
250 GL_TEXTURE_MAG_FILTER,
251 ImageSampler::GetMagnifyFilterMode( mSamplerBitfield ),
252 ImageSampler::GetMagnifyFilterMode( samplerBitfield ),
253 DALI_MAGNIFY_DEFAULT,
254 SYSTEM_MAGNIFY_DEFAULT );
256 ApplyWrapModeParameter( unit,
258 ImageSampler::GetUWrapMode( mSamplerBitfield ),
259 ImageSampler::GetUWrapMode( samplerBitfield ));
261 ApplyWrapModeParameter( unit,
263 ImageSampler::GetVWrapMode( mSamplerBitfield ),
264 ImageSampler::GetVWrapMode( samplerBitfield ));
268 mSamplerBitfield = samplerBitfield;
272 } // namespace Internal