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