Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-core.git] / dali / internal / render / gl-resources / 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/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 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Context& context)
33 : Texture( context,
34            width, height,
35            width, height )
36 {
37   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
38
39   mFrameBufferName  = 0;
40   mRenderBufferName = 0;
41 }
42
43 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Pixel::Format pixelFormat, Context& context)
44 : Texture( context,
45            width, height,
46            width, height ),
47   mPixelFormat( pixelFormat )
48 {
49   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
50
51   mFrameBufferName  = 0;
52   mRenderBufferName = 0;
53 }
54
55 FrameBufferTexture::~FrameBufferTexture()
56 {
57   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
58   // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
59   // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
60 }
61
62 bool FrameBufferTexture::IsFullyOpaque() const
63 {
64   return true;
65 }
66
67 bool FrameBufferTexture::HasAlphaChannel() const
68 {
69   return false;
70 }
71
72 bool FrameBufferTexture::Init()
73 {
74   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
75   return true;
76 }
77
78 bool FrameBufferTexture::Prepare()
79 {
80   // bind texture
81   Bind( GL_TEXTURE_2D, TEXTURE_UNIT_FRAMEBUFFER );
82
83   if( 0 != mId )
84   {
85     // bind frame buffer
86     mContext.BindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
87     // bind render buffer
88     mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
89     // attach texture to the color attachment point
90     mContext.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mId, 0);
91     // attach render buffer to the depth buffer attachment point
92     mContext.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mRenderBufferName);
93
94     int status = mContext.CheckFramebufferStatus(GL_FRAMEBUFFER);
95     if ( GL_FRAMEBUFFER_COMPLETE != status )
96     {
97       DALI_LOG_ERROR( "status (0x%x), glError (0x%x)\n", status, mContext.GetError() );
98       DALI_ASSERT_ALWAYS( false && "Frame buffer is not complete!" );
99     }
100
101     return true;
102   }
103
104   // Texture could not be bound
105   return false;
106 }
107
108 bool FrameBufferTexture::CreateGlTexture()
109 {
110   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
111
112   mContext.GenTextures(1, &mId);
113   mContext.ActiveTexture( TEXTURE_UNIT_UPLOAD );  // bind in unused unit so rebind works the first time
114   mContext.Bind2dTexture(mId);
115
116   // set texture parameters
117   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
118   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
119   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
120
121   // Assign memory for texture in GL memory space
122   GLenum pixelFormat = GL_RGBA;
123   GLenum pixelDataType = GL_UNSIGNED_BYTE;
124   Integration::ConvertToGlFormat(mPixelFormat, pixelDataType, pixelFormat);
125
126   mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat,mWidth, mHeight, 0, pixelFormat, pixelDataType, NULL);
127
128   // generate frame and render buffer names
129   mContext.GenFramebuffers(1, &mFrameBufferName);
130   mContext.GenRenderbuffers(1, &mRenderBufferName);
131
132   // Bind render buffer and create 16 depth buffer
133   mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
134   mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
135
136   return mId != 0;
137 }
138
139 void FrameBufferTexture::GlCleanup()
140 {
141   Texture::GlCleanup();
142
143   if (mFrameBufferName != 0)
144   {
145     mContext.DeleteFramebuffers(1, &mFrameBufferName );
146     mFrameBufferName = 0;
147   }
148
149   if (mRenderBufferName != 0)
150   {
151     mContext.DeleteRenderbuffers(1, &mRenderBufferName );
152     mRenderBufferName = 0;
153   }
154 }
155
156
157
158 } //namespace Internal
159
160 } //namespace Dali