Revert "License conversion from Flora to Apache 2.0"
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/native-texture.h>
19
20 // EXTERNAL INCLUDES
21 #include <math.h>
22 #include <memory.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/math/rect.h>
26 #include <dali/public-api/math/math-utils.h>
27 #include <dali/integration-api/debug.h>
28 #include <dali/internal/render/common/vertex.h>
29 #include <dali/internal/render/gl-resources/context.h>
30 #include <dali/internal/render/gl-resources/texture.h>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 NativeTexture::NativeTexture(NativeImage* nativeImg, Context& context)
39 : Texture(context,
40           nativeImg->GetWidth(),
41           nativeImg->GetHeight(),
42           nativeImg->GetWidth(),
43           nativeImg->GetHeight(),
44           nativeImg->GetPixelFormat()),
45           mNativeImage(nativeImg)
46 {
47   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeTexture created 0x%x\n", &nativeImg );
48 }
49
50 NativeTexture::~NativeTexture()
51 {
52   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeTexture destroyed\n");
53   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
54   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
55 }
56
57 bool NativeTexture::Bind(GLenum target, GLenum textureunit )
58 {
59   bool created = false;
60
61   if( mId==0 )
62   {
63     CreateGlTexture();
64     created = true;
65   }
66
67   // Bind the texture id
68   mContext.ActiveTexture( textureunit );
69   mContext.Bind2dTexture( mId );
70
71   mNativeImage->PrepareTexture();
72
73   return created;
74 }
75
76 bool NativeTexture::IsFullyOpaque() const
77 {
78   // TODO - Should test actual texture...
79   return !HasAlphaChannel();
80 }
81
82 bool NativeTexture::HasAlphaChannel() const
83 {
84   return Pixel::HasAlpha( mNativeImage->GetPixelFormat() );
85 }
86
87 Pixel::Format NativeTexture::GetPixelFormat() const
88 {
89   return mNativeImage->GetPixelFormat();
90 }
91
92 bool NativeTexture::CreateGlTexture()
93 {
94   if( mNativeImage->GlExtensionCreate() )
95   {
96     mContext.GenTextures( 1, &mId );
97     mContext.ActiveTexture( GL_TEXTURE7 );  // bind in unused unit so rebind works the first time
98     mContext.Bind2dTexture( mId );
99
100     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
101
102     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
103     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
104     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
105     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
106
107     // platform specific implementation decides on what GL extension to use
108     mNativeImage->TargetTexture();
109   }
110   else
111   {
112     DALI_LOG_ERROR( "Error creating native image!" );
113   }
114
115   return mId != 0;
116 }
117
118 void NativeTexture::GlCleanup()
119 {
120   Texture::GlCleanup();
121
122   DALI_ASSERT_DEBUG(mNativeImage);
123
124   mNativeImage->GlExtensionDestroy();
125
126   mNativeImage.Reset();
127 }
128
129 bool NativeTexture::Init()
130 {
131   return true;
132 }
133
134 } //namespace Internal
135
136 } //namespace Dali