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