43a5db14a35ca84611b650386d8386e8342e0888
[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
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 NativeTexture::NativeTexture(NativeImageInterface* nativeImg, Context& context)
34 : Texture(context,
35           nativeImg->GetWidth(),
36           nativeImg->GetHeight(),
37           nativeImg->GetWidth(),
38           nativeImg->GetHeight()),
39           mNativeImage(nativeImg)
40 {
41   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeTexture created 0x%x\n", &nativeImg );
42 }
43
44 NativeTexture::~NativeTexture()
45 {
46   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeTexture destroyed\n");
47
48   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
49   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
50 }
51
52 bool NativeTexture::Bind( GLenum target, TextureUnit textureunit )
53 {
54   bool result = true;
55
56   if( mId == 0 )
57   {
58     result = CreateGlTexture();
59   }
60
61   if( result )
62   {
63     // Bind the texture id
64     mContext.ActiveTexture( textureunit );
65     mContext.Bind2dTexture(mId);
66
67     mNativeImage->PrepareTexture();
68   }
69
70   return result;
71 }
72
73 bool NativeTexture::IsFullyOpaque() const
74 {
75   return !HasAlphaChannel();
76 }
77
78 bool NativeTexture::HasAlphaChannel() const
79 {
80   return mNativeImage->RequiresBlending();
81 }
82
83 bool NativeTexture::CreateGlTexture()
84 {
85   if( mId != 0 )
86   {
87     DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "GL texture creation duplicate for GL id: %d\n", &mId );
88     return true;
89   }
90
91   if( mNativeImage->GlExtensionCreate() )
92   {
93     mContext.GenTextures( 1, &mId );
94     mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );  // bind in unused unit so rebind works the first time
95     mContext.Bind2dTexture( mId );
96
97     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
98
99     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
100     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
101
102     // platform specific implementation decides on what GL extension to use
103     mNativeImage->TargetTexture();
104   }
105   else
106   {
107     DALI_LOG_ERROR( "Error creating native image!" );
108   }
109
110   return mId != 0;
111 }
112
113 void NativeTexture::GlCleanup()
114 {
115   Texture::GlCleanup();
116
117   DALI_ASSERT_DEBUG( mNativeImage );
118
119   mNativeImage->GlExtensionDestroy();
120   mNativeImage.Reset();
121 }
122
123 bool NativeTexture::Init()
124 {
125   return true;
126 }
127
128 } //namespace Internal
129
130 } //namespace Dali