Merge "Refactored NativeImageInterfaceExtension into NativeImageInterface" into devel...
[platform/core/uifw/dali-core.git] / dali / public-api / rendering / frame-buffer.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/public-api/rendering/frame-buffer.h>
20
21 // EXTERNAL INCLUDES
22 #include <type_traits>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h> // DALI_LOG_WARNING_NOFN
26 #include <dali/internal/event/rendering/frame-buffer-impl.h> // Dali::Internal::FrameBuffer
27 #include <dali/internal/event/rendering/texture-impl.h> // Dali::Internal::Texture
28
29 namespace Dali
30 {
31
32 namespace
33 {
34 /// Bool operator for FrameBuffer::Attachment::Mask.
35 /// in cpp as only used in this file
36 bool operator&( FrameBuffer::Attachment::Mask lhs, FrameBuffer::Attachment::Mask rhs )
37 {
38   using UnderlyingType = typename std::underlying_type< FrameBuffer::Attachment::Mask >::type;
39   return static_cast<bool>( static_cast<UnderlyingType>( lhs ) & static_cast<UnderlyingType>( rhs ) );
40 }
41
42 } // unnamed namespace
43
44 FrameBuffer FrameBuffer::New( uint32_t width, uint32_t height )
45 {
46   return New( width, height, FrameBuffer::Attachment::COLOR );
47 }
48
49 FrameBuffer FrameBuffer::New( uint32_t width, uint32_t height, Attachment::Mask attachments )
50 {
51   Internal::FrameBufferPtr frameBuffer = Internal::FrameBuffer::New( width, height, attachments );
52   if( attachments & FrameBuffer::Attachment::COLOR )
53   {
54     Internal::TexturePtr texture = Internal::Texture::New( Dali::TextureType::TEXTURE_2D, Pixel::RGB888, width, height );
55     frameBuffer->AttachColorTexture( texture, 0u, 0u );
56   }
57   return FrameBuffer( frameBuffer.Get() );
58 }
59
60 FrameBuffer::FrameBuffer()
61 {
62 }
63
64 FrameBuffer::~FrameBuffer()
65 {
66 }
67
68 FrameBuffer::FrameBuffer( const FrameBuffer& handle )
69 : BaseHandle( handle )
70 {
71 }
72
73 FrameBuffer FrameBuffer::DownCast( BaseHandle handle )
74 {
75   return FrameBuffer( dynamic_cast<Dali::Internal::FrameBuffer*>(handle.GetObjectPtr()));
76 }
77
78 FrameBuffer& FrameBuffer::operator=( const FrameBuffer& handle )
79 {
80   BaseHandle::operator=( handle );
81   return *this;
82 }
83
84 FrameBuffer::FrameBuffer( Internal::FrameBuffer* pointer )
85 : BaseHandle( pointer )
86 {
87 }
88
89 FrameBuffer::FrameBuffer( FrameBuffer&& rhs ) =  default;
90
91 FrameBuffer& FrameBuffer::operator=( FrameBuffer&& rhs ) =  default;
92
93 void FrameBuffer::AttachColorTexture( Texture& texture )
94 {
95   AttachColorTexture( texture, 0u, 0u );
96 }
97
98 void FrameBuffer::AttachColorTexture( Texture& texture, uint32_t mipmapLevel, uint32_t layer )
99 {
100   if( texture )
101   {
102     Internal::TexturePtr texturePtr( &GetImplementation( texture ) );
103     GetImplementation(*this).AttachColorTexture( texturePtr, mipmapLevel, layer );
104   }
105 }
106
107 Texture FrameBuffer::GetColorTexture()
108 {
109   Internal::Texture* texturePtr = GetImplementation(*this).GetColorTexture(0);
110   return Dali::Texture( texturePtr );
111 }
112
113 } //namespace Dali