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, Pixel::Format pixelFormat, Context& context)
38 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
41 mRenderBufferName = 0;
44 FrameBufferTexture::~FrameBufferTexture()
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)
51 bool FrameBufferTexture::IsFullyOpaque() const
56 bool FrameBufferTexture::HasAlphaChannel() const
61 bool FrameBufferTexture::Init()
63 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
67 bool FrameBufferTexture::Prepare()
70 Bind(GL_TEXTURE_2D, GL_TEXTURE0);
75 mContext.BindFramebuffer(GL_FRAMEBUFFER, mFrameBufferName);
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);
83 int status = mContext.CheckFramebufferStatus(GL_FRAMEBUFFER);
84 if ( GL_FRAMEBUFFER_COMPLETE != status )
86 DALI_LOG_ERROR( "status (0x%x), glError (0x%x)\n", status, mContext.GetError() );
87 DALI_ASSERT_ALWAYS( false && "Frame buffer is not complete!" );
93 // Texture could not be bound
97 bool FrameBufferTexture::CreateGlTexture()
99 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
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);
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);
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 // generate frame and render buffer names
118 mContext.GenFramebuffers(1, &mFrameBufferName);
119 mContext.GenRenderbuffers(1, &mRenderBufferName);
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);
128 void FrameBufferTexture::GlCleanup()
130 Texture::GlCleanup();
132 if (mFrameBufferName != 0)
134 mContext.DeleteFramebuffers(1, &mFrameBufferName );
135 mFrameBufferName = 0;
138 if (mRenderBufferName != 0)
140 mContext.DeleteRenderbuffers(1, &mRenderBufferName );
141 mRenderBufferName = 0;
147 } //namespace Internal