1703fd7db03e010fe4c60cc43107a5d43d0dae8e
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / frame-buffer-impl.cpp
1 /*
2  * Copyright (c) 2020 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
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/frame-buffer-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/update/manager/update-manager.h>
23 #include <dali/internal/render/renderers/render-frame-buffer.h>
24 #include <dali/internal/render/renderers/render-texture-frame-buffer.h>
25 #include <dali/internal/render/renderers/render-surface-frame-buffer.h>
26 #include <dali/integration-api/render-surface.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32
33 FrameBufferPtr FrameBuffer::New( uint32_t width, uint32_t height, Mask attachments  )
34 {
35   FrameBufferPtr frameBuffer( new FrameBuffer( width, height, attachments ) );
36   frameBuffer->Initialize();
37   return frameBuffer;
38 }
39
40 FrameBufferPtr FrameBuffer::New( Dali::Integration::RenderSurface& renderSurface, Mask attachments )
41 {
42   Dali::PositionSize positionSize = renderSurface.GetPositionSize();
43   FrameBufferPtr frameBuffer( new FrameBuffer( positionSize.width, positionSize.height, attachments ) );
44   frameBuffer->Initialize( &renderSurface );
45   return frameBuffer;
46 }
47
48 Render::FrameBuffer* FrameBuffer::GetRenderObject() const
49 {
50   return mRenderObject;
51 }
52
53 FrameBuffer::FrameBuffer( uint32_t width, uint32_t height, Mask attachments )
54 : mEventThreadServices( EventThreadServices::Get() ),
55   mRenderObject( NULL ),
56   mColor{ nullptr },
57   mWidth( width ),
58   mHeight( height ),
59   mAttachments( attachments ),
60   mColorAttachmentCount( 0 ),
61   mIsSurfaceBacked( false )
62 {
63 }
64
65 void FrameBuffer::Initialize( Integration::RenderSurface* renderSurface )
66 {
67   mIsSurfaceBacked = ( renderSurface != nullptr );
68
69   // If render surface backed, create a different scene object
70   // Make Render::FrameBuffer as a base class, and implement Render::TextureFrameBuffer & Render::WindowFrameBuffer
71   if ( mIsSurfaceBacked )
72   {
73     mRenderObject = new Render::SurfaceFrameBuffer( renderSurface );
74   }
75   else
76   {
77     mRenderObject = new Render::TextureFrameBuffer( mWidth, mHeight, mAttachments );
78   }
79
80   OwnerPointer< Render::FrameBuffer > transferOwnership( mRenderObject );
81   AddFrameBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
82 }
83
84 void FrameBuffer::AttachColorTexture( TexturePtr texture, uint32_t mipmapLevel, uint32_t layer )
85 {
86   if ( mIsSurfaceBacked )
87   {
88     DALI_LOG_ERROR( "Attempted to attach color texture to a render surface backed FrameBuffer \n" );
89   }
90   else
91   {
92     if( ( texture->GetWidth() / ( 1u << mipmapLevel ) != mWidth ) ||
93         ( texture->GetHeight() / ( 1u << mipmapLevel ) != mHeight ) )
94     {
95       DALI_LOG_ERROR( "Failed to attach color texture to FrameBuffer: Size mismatch \n" );
96     }
97     else if ( mColorAttachmentCount >= Dali::DevelFrameBuffer::MAX_COLOR_ATTACHMENTS )
98     {
99       DALI_LOG_ERROR( "Failed to attach color texture to FrameBuffer: Exceeded maximum supported color attachments.\n" );
100     }
101     else
102     {
103       mColor[mColorAttachmentCount] = texture;
104       ++mColorAttachmentCount;
105
106       AttachColorTextureToFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderObject(), mipmapLevel, layer );
107     }
108   }
109 }
110
111 Texture* FrameBuffer::GetColorTexture(uint8_t index) const
112 {
113   return ( mIsSurfaceBacked || index >= mColorAttachmentCount ) ? nullptr : mColor[index].Get();
114 }
115
116 void FrameBuffer::SetSize( uint32_t width, uint32_t height )
117 {
118   mWidth = width;
119   mHeight = height;
120
121   if( mRenderObject->IsSurfaceBacked() )
122   {
123     SetFrameBufferSizeMessage( mEventThreadServices.GetUpdateManager(), static_cast<Render::SurfaceFrameBuffer*>( mRenderObject ), width, height );
124   }
125 }
126
127 void FrameBuffer::MarkSurfaceAsInvalid()
128 {
129   if ( mIsSurfaceBacked )
130   {
131     Render::SurfaceFrameBuffer* renderObject = static_cast<Render::SurfaceFrameBuffer*>( mRenderObject );
132     renderObject->MarkSurfaceAsInvalid();
133   }
134 }
135
136 FrameBuffer::~FrameBuffer()
137 {
138   if( EventThreadServices::IsCoreRunning() && mRenderObject )
139   {
140     RemoveFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
141   }
142 }
143
144
145 } // namespace Internal
146 } // namespace Dali