Merge "Support scroll in web engine." into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / macos / native-image-source-impl-mac.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/imaging/macos/native-image-source-impl-mac.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/graphics/common/egl-image-extensions.h>
26 #include <dali/internal/graphics/gles/egl-graphics.h>
27 #include <dali/internal/adaptor/common/adaptor-impl.h>
28 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
29
30 namespace Dali::Internal::Adaptor
31 {
32
33 using Dali::Integration::PixelBuffer;
34
35 NativeImageSourceCocoa* NativeImageSourceCocoa::New(
36   unsigned int width,
37   unsigned int height,
38   Dali::NativeImageSource::ColorDepth depth,
39   Any nativeImageSource
40 )
41 {
42   return new NativeImageSourceCocoa( width, height, depth, nativeImageSource );
43 }
44
45 NativeImageSourceCocoa::NativeImageSourceCocoa(
46   unsigned int width,
47   unsigned int height,
48   Dali::NativeImageSource::ColorDepth depth,
49   Any nativeImageSource
50 )
51 : mImage(MakeRef<CGImageRef>(nullptr))
52 {
53   DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
54   DALI_ASSERT_ALWAYS( nativeImageSource.Empty() );
55
56   CFStringRef colorSpaceName;
57   CGImageAlphaInfo alphaInfo;
58   std::size_t bitsPerPixel;
59
60   switch (depth)
61   {
62     case Dali::NativeImageSource::COLOR_DEPTH_8:
63       colorSpaceName = kCGColorSpaceGenericGray;
64       alphaInfo = kCGImageAlphaNone;
65       bitsPerPixel = 8;
66       break;
67     case Dali::NativeImageSource::COLOR_DEPTH_16:
68       colorSpaceName = kCGColorSpaceSRGB;
69       alphaInfo = kCGImageAlphaNone;
70       bitsPerPixel = 16;
71       break;
72     case Dali::NativeImageSource::COLOR_DEPTH_24:
73       colorSpaceName = kCGColorSpaceSRGB;
74       alphaInfo = kCGImageAlphaNone;
75       bitsPerPixel = 24;
76       break;
77     case Dali::NativeImageSource::COLOR_DEPTH_32:
78     default:
79       colorSpaceName = kCGColorSpaceSRGB;
80       alphaInfo = kCGImageAlphaLast;
81       bitsPerPixel = 32;
82       break;
83   }
84
85   // round to next 16 bytes boundary
86   std::size_t bytesPerRow = width & ~0xf;
87   bytesPerRow = bytesPerRow ? bytesPerRow + 16 : width;
88
89   auto dataProvider = MakeRef(CGDataProviderCreateWithData(nullptr, nullptr, 0, nullptr));
90   auto colorSpace = MakeRef(CGColorSpaceCreateWithName(colorSpaceName));
91   mImage = MakeRef(CGImageCreate(
92     width,
93     height,
94     8,
95     bitsPerPixel,
96     bytesPerRow,
97     colorSpace.get(),
98     alphaInfo,
99     dataProvider.get(),
100     nullptr,
101     true,
102     kCGRenderingIntentDefault
103   ));
104
105   if (mImage)
106   {
107     colorSpace.release();
108     dataProvider.release();
109   }
110
111   DALI_ASSERT_ALWAYS(mImage.get());
112 }
113
114 NativeImageSourceCocoa::~NativeImageSourceCocoa()
115 {
116 }
117
118 Any NativeImageSourceCocoa::GetNativeImageSource() const
119 {
120   return Any();
121 }
122
123 bool NativeImageSourceCocoa::GetPixels(
124   std::vector<uint8_t>& pixbuf,
125   unsigned& width, unsigned& height,
126   Pixel::Format& pixelFormat
127 ) const
128 {
129   width  = CGImageGetWidth(mImage.get());
130   height = CGImageGetHeight(mImage.get());
131   return true;
132 }
133
134 void NativeImageSourceCocoa::SetSource( Any source )
135 {
136 }
137
138 bool NativeImageSourceCocoa::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
139 {
140   return true;
141 }
142
143 bool NativeImageSourceCocoa::CreateResource()
144 {
145   return false;
146 }
147
148 void NativeImageSourceCocoa::DestroyResource()
149 {
150 }
151
152 unsigned int NativeImageSourceCocoa::TargetTexture()
153 {
154   return 0;
155 }
156
157 void NativeImageSourceCocoa::PrepareTexture()
158 {
159 }
160
161 const char* NativeImageSourceCocoa::GetCustomFragmentPrefix() const
162 {
163   return nullptr;
164 }
165
166 const char* NativeImageSourceCocoa::GetCustomSamplerTypename() const
167 {
168   return nullptr;
169 }
170
171 int NativeImageSourceCocoa::GetTextureTarget() const
172 {
173   return GL_TEXTURE_2D;
174 }
175
176 Any NativeImageSourceCocoa::GetNativeImageHandle() const
177 {
178   return Any(mImage.get());
179 }
180
181 unsigned int NativeImageSourceCocoa::GetWidth() const
182 {
183   return CGImageGetWidth(mImage.get());
184 }
185
186 unsigned int NativeImageSourceCocoa::GetHeight() const
187 {
188   return CGImageGetHeight(mImage.get());
189 }
190
191 bool NativeImageSourceCocoa::RequiresBlending() const
192 {
193   const auto alphaInfo = CGImageGetAlphaInfo(mImage.get());
194   return
195     alphaInfo != kCGImageAlphaNone
196     && alphaInfo != kCGImageAlphaNoneSkipFirst
197     && alphaInfo != kCGImageAlphaNoneSkipLast;
198 }
199
200 bool NativeImageSourceCocoa::SourceChanged() const
201 {
202   return false;
203 }
204
205 uint8_t* NativeImageSourceCocoa::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
206 {
207   return nullptr;
208 }
209
210
211 bool NativeImageSourceCocoa::ReleaseBuffer()
212 {
213   return false;
214 }
215
216 } // namespace Dali::Internal::Adaptor