[Tizen] Make possible to capture on the old driver devices.
[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
25 namespace Dali
26 {
27 namespace Internal
28 {
29
30 FrameBufferPtr FrameBuffer::New( uint32_t width, uint32_t height, Mask attachments  )
31 {
32   FrameBufferPtr frameBuffer( new FrameBuffer( width, height, attachments ) );
33   frameBuffer->Initialize();
34   return frameBuffer;
35 }
36
37 Render::FrameBuffer* FrameBuffer::GetRenderObject() const
38 {
39   return mRenderObject;
40 }
41
42 FrameBuffer::FrameBuffer( uint32_t width, uint32_t height, Mask attachments )
43 : mEventThreadServices( EventThreadServices::Get() ),
44   mRenderObject( nullptr ),
45   mColor{ nullptr },
46   mDepth( nullptr ),
47   mStencil( nullptr ),
48   mWidth( width ),
49   mHeight( height ),
50   mAttachments( attachments ),
51   mColorAttachmentCount( 0 )
52 {
53 }
54
55 void FrameBuffer::Initialize()
56 {
57    mRenderObject = new Render::FrameBuffer( mWidth, mHeight, mAttachments );
58
59   OwnerPointer< Render::FrameBuffer > transferOwnership( mRenderObject );
60   AddFrameBuffer( mEventThreadServices.GetUpdateManager(), transferOwnership );
61 }
62
63 void FrameBuffer::AttachColorTexture( TexturePtr texture, uint32_t mipmapLevel, uint32_t layer )
64 {
65   if( ( texture->GetWidth() / ( 1u << mipmapLevel ) != mWidth ) ||
66       ( texture->GetHeight() / ( 1u << mipmapLevel ) != mHeight ) )
67   {
68     DALI_LOG_ERROR( "Failed to attach color texture to FrameBuffer: Size mismatch \n" );
69   }
70   else if ( mColorAttachmentCount >= Dali::DevelFrameBuffer::MAX_COLOR_ATTACHMENTS )
71   {
72     DALI_LOG_ERROR( "Failed to attach color texture to FrameBuffer: Exceeded maximum supported color attachments.\n" );
73   }
74   else
75   {
76     mColor[mColorAttachmentCount] = texture;
77     ++mColorAttachmentCount;
78
79     AttachColorTextureToFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderObject(), mipmapLevel, layer );
80   }
81 }
82
83 void FrameBuffer::AttachDepthTexture( TexturePtr texture, uint32_t mipmapLevel )
84 {
85   if( ( texture->GetWidth() / ( 1u << mipmapLevel ) != mWidth ) ||
86       ( texture->GetHeight() / ( 1u << mipmapLevel ) != mHeight ) )
87   {
88     DALI_LOG_ERROR( "Failed to attach depth texture to FrameBuffer: Size mismatch \n" );
89   }
90   else
91   {
92     mDepth = texture;
93     AttachDepthTextureToFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderObject(), mipmapLevel );
94   }
95 }
96
97 void FrameBuffer::AttachDepthStencilTexture( TexturePtr texture, unsigned int mipmapLevel )
98 {
99   if( ( texture->GetWidth() / ( 1u << mipmapLevel ) != mWidth ) ||
100       ( texture->GetHeight() / ( 1u << mipmapLevel ) != mHeight ) )
101   {
102     DALI_LOG_ERROR( "Failed to attach depth/stencil texture to FrameBuffer: Size mismatch \n" );
103   }
104   else
105   {
106     mStencil = texture;
107     AttachDepthStencilTextureToFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject, texture->GetRenderObject(), mipmapLevel );
108   }
109 }
110
111 Texture* FrameBuffer::GetColorTexture(uint8_t index) const
112 {
113   return ( index >= mColorAttachmentCount ) ? nullptr : mColor[index].Get();
114 }
115
116 Texture* FrameBuffer::GetDepthTexture() const
117 {
118   return ( mDepth ) ? mDepth.Get() : nullptr;
119 }
120
121 Texture* FrameBuffer::GetDepthStencilTexture() const
122 {
123   return ( mStencil ) ? mStencil.Get() : nullptr;
124 }
125
126 uint8_t* FrameBuffer::GetRenderedBuffer()
127 {
128   return mRenderObject->GetRenderedBuffer();
129 }
130
131 void FrameBuffer::CaptureRenderedResult()
132 {
133   CaptureRenderingResult(mEventThreadServices.GetUpdateManager(), *mRenderObject);
134 }
135
136 void FrameBuffer::SetSize( uint32_t width, uint32_t height )
137 {
138   mWidth = width;
139   mHeight = height;
140 }
141
142 FrameBuffer::~FrameBuffer()
143 {
144   if( EventThreadServices::IsCoreRunning() && mRenderObject )
145   {
146     RemoveFrameBuffer( mEventThreadServices.GetUpdateManager(), *mRenderObject );
147   }
148 }
149
150
151 } // namespace Internal
152 } // namespace Dali