2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 // http://floralicense.org/license/
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.
18 #include <dali/internal/render/gl-resources/frame-buffer-texture.h>
21 #include <dali/internal/render/gl-resources/context.h>
22 #include <dali/integration-api/debug.h>
30 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Pixel::Format pixelFormat, Context& context)
36 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
39 mRenderBufferName = 0;
42 FrameBufferTexture::~FrameBufferTexture()
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)
49 bool FrameBufferTexture::IsFullyOpaque() const
54 bool FrameBufferTexture::HasAlphaChannel() const
59 bool FrameBufferTexture::Init()
61 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
65 bool FrameBufferTexture::Prepare()
68 Bind(GL_TEXTURE_2D, GL_TEXTURE0);
73 mContext.BindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
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);
81 int status = mContext.CheckFramebufferStatus(GL_FRAMEBUFFER);
82 if ( GL_FRAMEBUFFER_COMPLETE != status )
84 DALI_LOG_ERROR( "status (0x%x), glError (0x%x)\n", status, mContext.GetError() );
85 DALI_ASSERT_ALWAYS( false && "Frame buffer is not complete!" );
91 // Texture could not be bound
95 bool FrameBufferTexture::CreateGlTexture()
97 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
99 mContext.GenTextures(1, &mId);
100 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
101 mContext.Bind2dTexture(mId);
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);
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);
115 mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat,mWidth, mHeight, 0, pixelFormat, pixelDataType, NULL);
117 if (!mFrameBufferName)
119 // generate frame and render buffer names
120 mContext.GenFramebuffers(1, &mFrameBufferName);
121 mContext.GenRenderbuffers(1, &mRenderBufferName);
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);
131 void FrameBufferTexture::GlCleanup()
133 Texture::GlCleanup();
135 if (mFrameBufferName != 0)
137 mContext.DeleteFramebuffers(1, &mFrameBufferName );
138 mFrameBufferName = 0;
141 if (mRenderBufferName != 0)
143 mContext.DeleteRenderbuffers(1, &mRenderBufferName );
144 mRenderBufferName = 0;
150 } //namespace Internal