Removed legacy resource tracking / logging
[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 Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/render/gl-resources/frame-buffer-texture.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/gl-resources/context.h>
22 #include <dali/integration-api/debug.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Pixel::Format pixelFormat, Context& context)
31 : Texture( context,
32            width, height,
33            width, height,
34            pixelFormat )
35 {
36   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
37
38   mFrameBufferName  = 0;
39   mRenderBufferName = 0;
40 }
41
42 FrameBufferTexture::~FrameBufferTexture()
43 {
44   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
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 FrameBufferTexture::IsFullyOpaque() const
50 {
51   return true;
52 }
53
54 bool FrameBufferTexture::HasAlphaChannel() const
55 {
56   return false;
57 }
58
59 bool FrameBufferTexture::Init()
60 {
61   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
62   return true;
63 }
64
65 bool FrameBufferTexture::Prepare()
66 {
67   // bind texture
68   Bind(GL_TEXTURE_2D, GL_TEXTURE0);
69
70   if( 0 != mId )
71   {
72     // bind frame buffer
73     mContext.BindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
74     // bind render buffer
75     mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
76     // attach texture to the color attachment point
77     mContext.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mId, 0);
78     // attach render buffer to the depth buffer attachment point
79     mContext.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mRenderBufferName);
80
81     int status = mContext.CheckFramebufferStatus(GL_FRAMEBUFFER);
82     if ( GL_FRAMEBUFFER_COMPLETE != status )
83     {
84       DALI_LOG_ERROR( "status (0x%x), glError (0x%x)\n", status, mContext.GetError() );
85       DALI_ASSERT_ALWAYS( false && "Frame buffer is not complete!" );
86     }
87
88     return true;
89   }
90
91   // Texture could not be bound
92   return false;
93 }
94
95 bool FrameBufferTexture::CreateGlTexture()
96 {
97   DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
98
99   mContext.GenTextures(1, &mId);
100   mContext.ActiveTexture(GL_TEXTURE7);  // bind in unused unit so rebind works the first time
101   mContext.Bind2dTexture(mId);
102
103   // set texture parameters
104   mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1);
105   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
106   mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
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   if (!mFrameBufferName)
118   {
119     // generate frame and render buffer names
120     mContext.GenFramebuffers(1, &mFrameBufferName);
121     mContext.GenRenderbuffers(1, &mRenderBufferName);
122
123     // Bind render buffer and create 16 depth buffer
124     mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
125     mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
126   }
127
128   return mId != 0;
129 }
130
131 void FrameBufferTexture::GlCleanup()
132 {
133   Texture::GlCleanup();
134
135   if (mFrameBufferName != 0)
136   {
137     mContext.DeleteFramebuffers(1, &mFrameBufferName );
138     mFrameBufferName = 0;
139   }
140
141   if (mRenderBufferName != 0)
142   {
143     mContext.DeleteRenderbuffers(1, &mRenderBufferName );
144     mRenderBufferName = 0;
145   }
146 }
147
148
149
150 } //namespace Internal
151
152 } //namespace Dali