[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / frame-buffer-impl.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
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( NULL ),
57   mWidth( width ),
58   mHeight( height ),
59   mAttachments( attachments ),
60   mIsSurfaceBacked( false )
61 {
62 }
63
64 void FrameBuffer::Initialize( Integration::RenderSurface* renderSurface )
65 {
66   mIsSurfaceBacked = ( renderSurface != nullptr );
67
68   // If render surface backed, create a different scene object
69   // Make Render::FrameBuffer as a base class, and implement Render::TextureFrameBuffer & Render::WindowFrameBuffer
70   if ( mIsSurfaceBacked )
71   {
72     mRenderObject = new Render::SurfaceFrameBuffer( renderSurface );
73   }
74   else
75   {
76     mRenderObject = new Render::TextureFrameBuffer( mWidth, mHeight, mAttachments );
77   }
78
79   OwnerPointer< Render::FrameBuffer > transferOwnership( mRenderObject );
80   AddFrameBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
81 }
82
83 void FrameBuffer::AttachColorTexture( TexturePtr texture, uint32_t mipmapLevel, uint32_t layer )
84 {
85   if ( mIsSurfaceBacked )
86   {
87     DALI_LOG_ERROR( "Attempted to attach color texture to a render surface backed FrameBuffer \n" );
88   }
89   else
90   {
91     if( ( texture->GetWidth() / ( 1u << mipmapLevel ) == mWidth ) &&
92         ( texture->GetHeight() / ( 1u << mipmapLevel ) == mHeight ) )
93     {
94       mColor = texture;
95       AttachColorTextureToFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderObject(), mipmapLevel, layer );
96     }
97     else
98     {
99       DALI_LOG_ERROR( "Failed to attach color texture to FrameBuffer: Size mismatch \n" );
100     }
101   }
102 }
103
104 Texture* FrameBuffer::GetColorTexture()
105 {
106   return mIsSurfaceBacked ? nullptr : mColor.Get();
107 }
108
109 void FrameBuffer::SetSize( uint32_t width, uint32_t height )
110 {
111   mWidth = width;
112   mHeight = height;
113
114   if( mRenderObject->IsSurfaceBacked() )
115   {
116     SetFrameBufferSizeMessage( mEventThreadServices.GetUpdateManager(), static_cast<Render::SurfaceFrameBuffer*>( mRenderObject ), width, height );
117   }
118 }
119
120 void FrameBuffer::SetBackgroundColor( const Vector4& color )
121 {
122   if( mRenderObject->IsSurfaceBacked() )
123   {
124     SetFrameBufferBackgroundColorMessage( mEventThreadServices.GetUpdateManager(), static_cast<Render::SurfaceFrameBuffer*>( mRenderObject ), color );
125   }
126 }
127
128 void FrameBuffer::MarkSurfaceAsInvalid()
129 {
130   if ( mIsSurfaceBacked )
131   {
132     Render::SurfaceFrameBuffer* renderObject = static_cast<Render::SurfaceFrameBuffer*>( mRenderObject );
133     renderObject->MarkSurfaceAsInvalid();
134   }
135 }
136
137 void FrameBuffer::SetPartialUpdateEnabled( bool value )
138 {
139   if( mRenderObject->IsSurfaceBacked() )
140   {
141     SetFrameBufferPartialUpdateMessage( mEventThreadServices.GetUpdateManager(), static_cast<Render::SurfaceFrameBuffer*>( mRenderObject ), value );
142   }
143 }
144
145
146 FrameBuffer::~FrameBuffer()
147 {
148   if( EventThreadServices::IsCoreRunning() && mRenderObject )
149   {
150     RemoveFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
151   }
152 }
153
154
155 } // namespace Internal
156 } // namespace Dali