2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/render/gl-resources/frame-buffer-texture.h>
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>
32 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Context& context)
37 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
40 mRenderBufferName = 0;
43 FrameBufferTexture::FrameBufferTexture(unsigned int width, unsigned int height, Pixel::Format pixelFormat, Context& context)
47 mPixelFormat( pixelFormat )
49 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
52 mRenderBufferName = 0;
55 FrameBufferTexture::~FrameBufferTexture()
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)
62 bool FrameBufferTexture::IsFullyOpaque() const
67 bool FrameBufferTexture::HasAlphaChannel() const
72 bool FrameBufferTexture::Init()
74 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
78 bool FrameBufferTexture::Prepare()
81 Bind( GL_TEXTURE_2D, TEXTURE_UNIT_FRAMEBUFFER );
86 mContext.BindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
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);
94 int status = mContext.CheckFramebufferStatus(GL_FRAMEBUFFER);
95 if ( GL_FRAMEBUFFER_COMPLETE != status )
97 DALI_LOG_ERROR( "status (0x%x), glError (0x%x)\n", status, mContext.GetError() );
98 DALI_ASSERT_ALWAYS( false && "Frame buffer is not complete!" );
104 // Texture could not be bound
108 bool FrameBufferTexture::CreateGlTexture()
110 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
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);
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);
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);
126 mContext.TexImage2D(GL_TEXTURE_2D, 0, pixelFormat,mWidth, mHeight, 0, pixelFormat, pixelDataType, NULL);
128 // generate frame and render buffer names
129 mContext.GenFramebuffers(1, &mFrameBufferName);
130 mContext.GenRenderbuffers(1, &mRenderBufferName);
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);
139 void FrameBufferTexture::GlCleanup()
141 Texture::GlCleanup();
143 if (mFrameBufferName != 0)
145 mContext.DeleteFramebuffers(1, &mFrameBufferName );
146 mFrameBufferName = 0;
149 if (mRenderBufferName != 0)
151 mContext.DeleteRenderbuffers(1, &mRenderBufferName );
152 mRenderBufferName = 0;
158 } //namespace Internal