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/native-frame-buffer-texture.h>
22 #include <dali/internal/render/gl-resources/context.h>
23 #include <dali/integration-api/debug.h>
31 NativeFrameBufferTexture::NativeFrameBufferTexture( NativeImagePtr nativeImage, Context& context)
32 : FrameBufferTexture(nativeImage->GetWidth(),
33 nativeImage->GetHeight(),
34 nativeImage->GetPixelFormat(),
36 mNativeImage(nativeImage)
38 DALI_LOG_INFO( Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture created 0x%x\n", &nativeImage );
41 NativeFrameBufferTexture::~NativeFrameBufferTexture()
43 DALI_LOG_INFO (Debug::Filter::gImage, Debug::General, "NativeFrameBufferTexture destroyed\n");
44 // GlCleanup() should already have been called by TextureCache ensuring the resource is destroyed
45 // on the render thread. (And avoiding a potentially problematic virtual call in the destructor)
48 bool NativeFrameBufferTexture::IsFullyOpaque() const
50 // TODO - Should test actual texture...
51 return !HasAlphaChannel();
54 bool NativeFrameBufferTexture::HasAlphaChannel() const
56 return Pixel::HasAlpha(mNativeImage->GetPixelFormat());
59 bool NativeFrameBufferTexture::Init()
61 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
63 bool status( true ); // assume success
64 if( mFrameBufferName == 0 )
66 status = CreateGlTexture();
72 bool NativeFrameBufferTexture::CreateGlTexture()
74 DALI_LOG_TRACE_METHOD(Debug::Filter::gImage);
76 if( mNativeImage->GlExtensionCreate() )
78 mContext.GenTextures(1, &mId);
79 mContext.ActiveTexture(GL_TEXTURE7); // bind in unused unit so rebind works the first time
80 mContext.Bind2dTexture(mId);
82 mContext.PixelStorei(GL_UNPACK_ALIGNMENT, 1); // We always use tightly packed data
84 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
85 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
86 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
87 mContext.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
89 // platform specific implementation decides on what GL extension to use
90 mNativeImage->TargetTexture();
92 if (!mFrameBufferName)
94 // generate frame and render buffer names
95 mContext.GenFramebuffers(1, &mFrameBufferName);
96 mContext.GenRenderbuffers(1, &mRenderBufferName);
98 // Bind render buffer and create 16 depth buffer
99 mContext.BindRenderbuffer(GL_RENDERBUFFER, mRenderBufferName);
100 mContext.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
105 DALI_LOG_ERROR( "Error creating native image!" );
111 void NativeFrameBufferTexture::GlCleanup()
113 Texture::GlCleanup();
115 if (mFrameBufferName != 0)
117 mContext.DeleteFramebuffers(1, &mFrameBufferName );
118 mFrameBufferName = 0;
121 if (mRenderBufferName != 0)
123 mContext.DeleteRenderbuffers(1, &mRenderBufferName );
124 mRenderBufferName = 0;
127 mNativeImage->GlExtensionDestroy();
129 mNativeImage.Reset();
133 } //namespace Internal