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