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/native-texture.h>
26 #include <dali/public-api/math/rect.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/integration-api/debug.h>
29 #include <dali/internal/render/common/vertex.h>
30 #include <dali/internal/render/gl-resources/context.h>
31 #include <dali/internal/render/gl-resources/texture.h>
39 NativeTexture::NativeTexture(NativeImage* nativeImg, Context& context)
41 nativeImg->GetWidth(),
42 nativeImg->GetHeight(),
43 nativeImg->GetWidth(),
44 nativeImg->GetHeight(),
45 nativeImg->GetPixelFormat()),
46 mNativeImage(nativeImg)
48 DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeTexture created 0x%x\n", &nativeImg );
51 NativeTexture::~NativeTexture()
53 DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeTexture destroyed\n");
54 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
55 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
58 bool NativeTexture::Bind(GLenum target, GLenum textureunit )
68 // Bind the texture id
69 mContext.ActiveTexture( textureunit );
70 mContext.Bind2dTexture( mId );
72 mNativeImage->PrepareTexture();
77 bool NativeTexture::IsFullyOpaque() const
79 // TODO - Should test actual texture...
80 return !HasAlphaChannel();
83 bool NativeTexture::HasAlphaChannel() const
85 return Pixel::HasAlpha( mNativeImage->GetPixelFormat() );
88 Pixel::Format NativeTexture::GetPixelFormat() const
90 return mNativeImage->GetPixelFormat();
93 bool NativeTexture::CreateGlTexture()
95 if( mNativeImage->GlExtensionCreate() )
97 mContext.GenTextures( 1, &mId );
98 mContext.ActiveTexture( GL_TEXTURE7 ); // bind in unused unit so rebind works the first time
99 mContext.Bind2dTexture( mId );
101 mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
103 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
104 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
105 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
106 mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
108 // platform specific implementation decides on what GL extension to use
109 mNativeImage->TargetTexture();
113 DALI_LOG_ERROR( "Error creating native image!" );
119 void NativeTexture::GlCleanup()
121 Texture::GlCleanup();
123 DALI_ASSERT_DEBUG(mNativeImage);
125 mNativeImage->GlExtensionDestroy();
127 mNativeImage.Reset();
130 bool NativeTexture::Init()
135 } //namespace Internal