Merge "Distinguish NativeImage from Image & Clean PixelFormat from ImageAttribute...
[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(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   // 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 created = false;
55
56   if( mId==0 )
57   {
58     CreateGlTexture();
59     created = true;
60   }
61
62   // Bind the texture id
63   mContext.ActiveTexture( textureunit );
64   mContext.Bind2dTexture( mId );
65
66   mNativeImage->PrepareTexture();
67
68   return created;
69 }
70
71 bool NativeTexture::IsFullyOpaque() const
72 {
73   return !HasAlphaChannel();
74 }
75
76 bool NativeTexture::HasAlphaChannel() const
77 {
78   return mNativeImage->RequiresBlending();
79 }
80
81 bool NativeTexture::CreateGlTexture()
82 {
83   if( mNativeImage->GlExtensionCreate() )
84   {
85     mContext.GenTextures( 1, &mId );
86     mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );  // bind in unused unit so rebind works the first time
87     mContext.Bind2dTexture( mId );
88
89     mContext.PixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // We always use tightly packed data
90
91     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
92     mContext.TexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
93
94     // platform specific implementation decides on what GL extension to use
95     mNativeImage->TargetTexture();
96   }
97   else
98   {
99     DALI_LOG_ERROR( "Error creating native image!" );
100   }
101
102   return mId != 0;
103 }
104
105 void NativeTexture::GlCleanup()
106 {
107   Texture::GlCleanup();
108
109   DALI_ASSERT_DEBUG(mNativeImage);
110
111   mNativeImage->GlExtensionDestroy();
112
113   mNativeImage.Reset();
114 }
115
116 bool NativeTexture::Init()
117 {
118   return true;
119 }
120
121 } //namespace Internal
122
123 } //namespace Dali