Merge "Distinguish NativeImage from Image & Clean PixelFormat from ImageAttribute...
[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/internal/render/gl-resources/texture-units.h>
24 #include <dali/integration-api/debug.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 NativeFrameBufferTexture::NativeFrameBufferTexture( NativeImageInterfacePtr nativeImage, Context& context)
33   : FrameBufferTexture(nativeImage->GetWidth(),
34                        nativeImage->GetHeight(),
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   return !HasAlphaChannel();
51 }
52
53 bool NativeFrameBufferTexture::HasAlphaChannel() const
54 {
55   return mNativeImage->RequiresBlending();
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( TEXTURE_UNIT_UPLOAD );  // 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_WRAP_S, GL_CLAMP_TO_EDGE);
84     mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
85
86     // platform specific implementation decides on what GL extension to use
87     mNativeImage->TargetTexture();
88
89     // generate frame and render buffer names
90     mContext.GenFramebuffers(1, &mFrameBufferName);
91     mContext.GenRenderbuffers(1, &mRenderBufferName);
92
93     // Bind render buffer and create 16 depth buffer
94     mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
95     mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
96   }
97   else
98   {
99     DALI_LOG_ERROR( "Error creating native image!" );
100   }
101
102   return mId != 0;
103 }
104
105 void NativeFrameBufferTexture::GlCleanup()
106 {
107   Texture::GlCleanup();
108
109   if (mFrameBufferName != 0)
110   {
111     mContext.DeleteFramebuffers(1, &mFrameBufferName );
112     mFrameBufferName = 0;
113   }
114
115   if (mRenderBufferName != 0)
116   {
117     mContext.DeleteRenderbuffers(1, &mRenderBufferName );
118     mRenderBufferName = 0;
119   }
120
121   mNativeImage->GlExtensionDestroy();
122
123   mNativeImage.Reset();
124 }
125
126 } //namespace Internal
127
128 } //namespace Dali