Add an easier to use New in FrameBuffer for the 99% of the cases where we use offscreens
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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/renderers/render-frame-buffer.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/renderers/render-texture.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Render
28 {
29
30 FrameBuffer::FrameBuffer( uint32_t width, uint32_t height, Mask attachments )
31 :mId( 0u ),
32  mDepthBuffer( attachments & Dali::FrameBuffer::Attachment::DEPTH ),
33  mStencilBuffer( attachments & Dali::FrameBuffer::Attachment::STENCIL ),
34  mWidth( width ),
35  mHeight( height )
36 {
37 }
38
39 FrameBuffer::~FrameBuffer()
40 {}
41
42 void FrameBuffer::Destroy( Context& context )
43 {
44   if( mId )
45   {
46     context.DeleteFramebuffers( 1, &mId );
47   }
48 }
49
50 void FrameBuffer::GlContextDestroyed()
51 {
52   mId = 0u;
53 }
54
55 void FrameBuffer::Initialize(Context& context)
56 {
57   context.GenFramebuffers( 1, &mId );
58   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
59
60   if( mDepthBuffer )
61   {
62     // Create a depth render target.
63     context.GenRenderbuffers( 1, &mDepthBuffer );
64     context.BindRenderbuffer( GL_RENDERBUFFER, mDepthBuffer );
65     context.RenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight );
66     context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer );
67   }
68
69   if( mStencilBuffer )
70   {
71     // Create a stencil render target.
72     context.GenRenderbuffers( 1, &mStencilBuffer );
73     context.BindRenderbuffer( GL_RENDERBUFFER, mStencilBuffer );
74     context.RenderbufferStorage( GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight );
75     context.FramebufferRenderbuffer( GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
76   }
77
78   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
79 }
80
81 void FrameBuffer::AttachColorTexture( Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer )
82 {
83   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
84
85   // Create a color attachment.
86   if( texture->GetType() == TextureType::TEXTURE_2D )
87   {
88     if( !texture->IsNativeImage() )
89     {
90       context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
91     }
92     else
93     {
94       // If it's a native image we need to use GL_TEXTURE_EXTERNAL_OES as the texture target parameter
95       context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_EXTERNAL_OES, texture->GetId(), mipmapLevel );
96     }
97   }
98   else
99   {
100     context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, texture->GetId(), mipmapLevel );
101   }
102
103   context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
104 }
105
106 void FrameBuffer::Bind( Context& context )
107 {
108   context.BindFramebuffer( GL_FRAMEBUFFER, mId );
109 }
110
111 uint32_t FrameBuffer::GetWidth() const
112 {
113   return mWidth;
114 }
115
116 uint32_t FrameBuffer::GetHeight() const
117 {
118   return mHeight;
119 }
120
121
122 } //Render
123
124 } //Internal
125
126 } //Dali