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