Merge "Klockwork: Remove unreachable code" into tizen
[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/common/vertex.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/gl-resources/texture.h>
26 #include <dali/internal/render/gl-resources/texture-units.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 NativeTexture::NativeTexture(NativeImage* nativeImg, Context& context)
35 : Texture(context,
36           nativeImg->GetWidth(),
37           nativeImg->GetHeight(),
38           nativeImg->GetWidth(),
39           nativeImg->GetHeight(),
40           nativeImg->GetPixelFormat()),
41           mNativeImage(nativeImg)
42 {
43   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeTexture created 0x%x\n", &nativeImg );
44 }
45
46 NativeTexture::~NativeTexture()
47 {
48   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeTexture destroyed\n");
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, GLenum textureunit )
54 {
55   bool created = false;
56
57   if( mId==0 )
58   {
59     CreateGlTexture();
60     created = true;
61   }
62
63   // Bind the texture id
64   mContext.ActiveTexture( textureunit );
65   mContext.Bind2dTexture( mId );
66
67   mNativeImage->PrepareTexture();
68
69   return created;
70 }
71
72 bool NativeTexture::IsFullyOpaque() const
73 {
74   // TODO - Should test actual texture...
75   return !HasAlphaChannel();
76 }
77
78 bool NativeTexture::HasAlphaChannel() const
79 {
80   return Pixel::HasAlpha( mNativeImage->GetPixelFormat() );
81 }
82
83 Pixel::Format NativeTexture::GetPixelFormat() const
84 {
85   return mNativeImage->GetPixelFormat();
86 }
87
88 bool NativeTexture::CreateGlTexture()
89 {
90   if( mNativeImage->GlExtensionCreate() )
91   {
92     mContext.GenTextures( 1, &mId );
93     mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) );  // bind in unused unit so rebind works the first time
94     mContext.Bind2dTexture( mId );
95
96     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
97
98     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
99     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
100
101     // platform specific implementation decides on what GL extension to use
102     mNativeImage->TargetTexture();
103   }
104   else
105   {
106     DALI_LOG_ERROR( "Error creating native image!" );
107   }
108
109   return mId != 0;
110 }
111
112 void NativeTexture::GlCleanup()
113 {
114   Texture::GlCleanup();
115
116   DALI_ASSERT_DEBUG(mNativeImage);
117
118   mNativeImage->GlExtensionDestroy();
119
120   mNativeImage.Reset();
121 }
122
123 bool NativeTexture::Init()
124 {
125   return true;
126 }
127
128 } //namespace Internal
129
130 } //namespace Dali