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