Removed legacy resource tracking / logging
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / native-frame-buffer-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-frame-buffer-texture.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/gl-resources/context.h>
22 #include <dali/integration-api/debug.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 NativeFrameBufferTexture::NativeFrameBufferTexture( NativeImagePtr nativeImage, Context& context)
31   : FrameBufferTexture(nativeImage->GetWidth(),
32                        nativeImage->GetHeight(),
33                        nativeImage->GetPixelFormat(),
34                        context),
35     mNativeImage(nativeImage)
36 {
37   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture created 0x%x\n", &nativeImage );
38 }
39
40 NativeFrameBufferTexture::~NativeFrameBufferTexture()
41 {
42   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture destroyed\n");
43   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
44   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
45 }
46
47 bool NativeFrameBufferTexture::IsFullyOpaque() const
48 {
49   // TODO - Should test actual texture...
50   return !HasAlphaChannel();
51 }
52
53 bool NativeFrameBufferTexture::HasAlphaChannel() const
54 {
55   return Pixel::HasAlpha(mNativeImage->GetPixelFormat());
56 }
57
58 bool NativeFrameBufferTexture::Init()
59 {
60   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
61
62   bool status( true ); // assume success
63   if( mFrameBufferName == 0 )
64   {
65     status = CreateGlTexture();
66   }
67
68   return status;
69 }
70
71 bool NativeFrameBufferTexture::CreateGlTexture()
72 {
73   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
74
75   if( mNativeImage->GlExtensionCreate() )
76   {
77     mContext.GenTextures(1, &mId);
78     mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
79     mContext.Bind2dTexture(mId);
80
81     mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
82
83     mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
84     mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
85     mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
86     mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
87
88     // platform specific implementation decides on what GL extension to use
89     mNativeImage->TargetTexture();
90
91     if (!mFrameBufferName)
92     {
93       // generate frame and render buffer names
94       mContext.GenFramebuffers(1, &mFrameBufferName);
95       mContext.GenRenderbuffers(1, &mRenderBufferName);
96
97       // Bind render buffer and create 16 depth buffer
98       mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
99       mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
100     }
101   }
102   else
103   {
104     DALI_LOG_ERROR( "Error creating native image!" );
105   }
106
107   return mId != 0;
108 }
109
110 void NativeFrameBufferTexture::GlCleanup()
111 {
112   Texture::GlCleanup();
113
114   if (mFrameBufferName != 0)
115   {
116     mContext.DeleteFramebuffers(1, &mFrameBufferName );
117     mFrameBufferName = 0;
118   }
119
120   if (mRenderBufferName != 0)
121   {
122     mContext.DeleteRenderbuffers(1, &mRenderBufferName );
123     mRenderBufferName = 0;
124   }
125
126   mNativeImage->GlExtensionDestroy();
127
128   mNativeImage.Reset();
129 }
130
131
132 } //namespace Internal
133
134 } //namespace Dali