[3.0] Modify texture bind for OES_EGL_image_external
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / native-texture.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/gl-resources/native-texture.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/render/gl-resources/context.h>
24 #include <dali/internal/render/gl-resources/texture-units.h>
25 #include <dali/internal/render/gl-resources/gl-texture.h>
26 #include <dali/devel-api/images/native-image-interface-extension.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 NativeTexture::NativeTexture(NativeImageInterface* nativeImg, Context& context)
35 : Texture(context,
36           nativeImg->GetWidth(),
37           nativeImg->GetHeight(),
38           nativeImg->GetWidth(),
39           nativeImg->GetHeight()),
40           mNativeImage(nativeImg)
41 {
42   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeTexture created 0x%x\n", &nativeImg );
43 }
44
45 NativeTexture::~NativeTexture()
46 {
47   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeTexture destroyed\n");
48
49   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
50   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
51 }
52
53 bool NativeTexture::Bind( GLenum target, TextureUnit textureunit )
54 {
55   bool result = true;
56
57   if( mId == 0 )
58   {
59     result = CreateGlTexture();
60   }
61
62   if( result )
63   {
64     // Bind the texture id
65     mContext.ActiveTexture( textureunit );
66
67     int textureTarget = GL_TEXTURE_2D;
68     NativeImageInterface::Extension* extension = mNativeImage->GetExtension();
69     if( extension )
70     {
71       textureTarget = extension->GetEglImageTextureTarget();
72     }
73
74     mContext.BindTexture( textureTarget, mId );
75     mNativeImage->PrepareTexture();
76   }
77
78   return result;
79 }
80
81 bool NativeTexture::IsFullyOpaque() const
82 {
83   return !HasAlphaChannel();
84 }
85
86 bool NativeTexture::HasAlphaChannel() const
87 {
88   return mNativeImage->RequiresBlending();
89 }
90
91 bool NativeTexture::CreateGlTexture()
92 {
93   if( mId != 0 )
94   {
95     DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "GL texture creation duplicate for GL id: %d\n", &mId );
96     return true;
97   }
98
99   if( mNativeImage->GlExtensionCreate() )
100   {
101     mContext.GenTextures( 1, &mId );
102     mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );  // bind in unused unit so rebind works the first time
103
104     int textureTarget = GL_TEXTURE_2D;
105     NativeImageInterface::Extension* extension = mNativeImage->GetExtension();
106     if( extension )
107     {
108       textureTarget = extension->GetEglImageTextureTarget();
109     }
110
111     mContext.BindTexture( textureTarget, mId );
112
113     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
114
115     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
116     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
117
118     // platform specific implementation decides on what GL extension to use
119     mNativeImage->TargetTexture();
120   }
121   else
122   {
123     DALI_LOG_ERROR( "Error creating native image!\n" );
124   }
125
126   return mId != 0;
127 }
128
129 void NativeTexture::GlCleanup()
130 {
131   Texture::GlCleanup();
132
133   DALI_ASSERT_DEBUG( mNativeImage );
134
135   mNativeImage->GlExtensionDestroy();
136   mNativeImage.Reset();
137 }
138
139 bool NativeTexture::Init()
140 {
141   return true;
142 }
143
144 } //namespace Internal
145
146 } //namespace Dali