proper enums for texture units to improve readability and allow renderer type specifi...
[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( NativeImagePtr nativeImage, Context& context)
33   : FrameBufferTexture(nativeImage->GetWidth(),
34                        nativeImage->GetHeight(),
35                        nativeImage->GetPixelFormat(),
36                        context),
37     mNativeImage(nativeImage)
38 {
39   DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture created 0x%x\n", &nativeImage );
40 }
41
42 NativeFrameBufferTexture::~NativeFrameBufferTexture()
43 {
44   DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture destroyed\n");
45   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
46   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
47 }
48
49 bool NativeFrameBufferTexture::IsFullyOpaque() const
50 {
51   // TODO - Should test actual texture...
52   return !HasAlphaChannel();
53 }
54
55 bool NativeFrameBufferTexture::HasAlphaChannel() const
56 {
57   return Pixel::HasAlpha(mNativeImage->GetPixelFormat());
58 }
59
60 bool NativeFrameBufferTexture::Init()
61 {
62   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
63
64   bool status( true ); // assume success
65   if( mFrameBufferName == 0 )
66   {
67     status = CreateGlTexture();
68   }
69
70   return status;
71 }
72
73 bool NativeFrameBufferTexture::CreateGlTexture()
74 {
75   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
76
77   if( mNativeImage->GlExtensionCreate() )
78   {
79     mContext.GenTextures(1, &mId);
80     mContext.ActiveTexture( TextureUnitAsGLenum( TEXTURE_UNIT_UPLOAD ) );  // bind in unused unit so rebind works the first time
81     mContext.Bind2dTexture(mId);
82
83     mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
84
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     // generate frame and render buffer names
92     mContext.GenFramebuffers(1, &mFrameBufferName);
93     mContext.GenRenderbuffers(1, &mRenderBufferName);
94
95     // Bind render buffer and create 16 depth buffer
96     mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
97     mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
98   }
99   else
100   {
101     DALI_LOG_ERROR( "Error creating native image!" );
102   }
103
104   return mId != 0;
105 }
106
107 void NativeFrameBufferTexture::GlCleanup()
108 {
109   Texture::GlCleanup();
110
111   if (mFrameBufferName != 0)
112   {
113     mContext.DeleteFramebuffers(1, &mFrameBufferName );
114     mFrameBufferName = 0;
115   }
116
117   if (mRenderBufferName != 0)
118   {
119     mContext.DeleteRenderbuffers(1, &mRenderBufferName );
120     mRenderBufferName = 0;
121   }
122
123   mNativeImage->GlExtensionDestroy();
124
125   mNativeImage.Reset();
126 }
127
128
129 } //namespace Internal
130
131 } //namespace Dali