Merge "VectorImageRenderer: Remove TizenVectorImageRenderer dependency" into devel...
[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   mResourceDestructionCallback()
50 {
51   DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
52   DALI_ASSERT_ALWAYS(nativeImageSource.Empty());
53
54   CFStringRef      colorSpaceName;
55   CGImageAlphaInfo alphaInfo;
56   std::size_t      bitsPerPixel;
57
58   switch(depth)
59   {
60     case Dali::NativeImageSource::COLOR_DEPTH_8:
61       colorSpaceName = kCGColorSpaceGenericGray;
62       alphaInfo      = kCGImageAlphaNone;
63       bitsPerPixel   = 8;
64       break;
65     case Dali::NativeImageSource::COLOR_DEPTH_16:
66       colorSpaceName = kCGColorSpaceSRGB;
67       alphaInfo      = kCGImageAlphaNone;
68       bitsPerPixel   = 16;
69       break;
70     case Dali::NativeImageSource::COLOR_DEPTH_24:
71       colorSpaceName = kCGColorSpaceSRGB;
72       alphaInfo      = kCGImageAlphaNone;
73       bitsPerPixel   = 24;
74       break;
75     case Dali::NativeImageSource::COLOR_DEPTH_32:
76     default:
77       colorSpaceName = kCGColorSpaceSRGB;
78       alphaInfo      = kCGImageAlphaLast;
79       bitsPerPixel   = 32;
80       break;
81   }
82
83   // round to next 16 bytes boundary
84   std::size_t bytesPerRow = width & ~0xf;
85   bytesPerRow             = bytesPerRow ? bytesPerRow + 16 : width;
86
87   auto dataProvider = MakeRef(CGDataProviderCreateWithData(nullptr, nullptr, 0, nullptr));
88   auto colorSpace   = MakeRef(CGColorSpaceCreateWithName(colorSpaceName));
89   mImage            = MakeRef(CGImageCreate(
90     width,
91     height,
92     8,
93     bitsPerPixel,
94     bytesPerRow,
95     colorSpace.get(),
96     alphaInfo,
97     dataProvider.get(),
98     nullptr,
99     true,
100     kCGRenderingIntentDefault));
101
102   if(mImage)
103   {
104     colorSpace.release();
105     dataProvider.release();
106   }
107
108   DALI_ASSERT_ALWAYS(mImage.get());
109 }
110
111 NativeImageSourceCocoa::~NativeImageSourceCocoa()
112 {
113 }
114
115 Any NativeImageSourceCocoa::GetNativeImageSource() const
116 {
117   return Any();
118 }
119
120 bool NativeImageSourceCocoa::GetPixels(
121   std::vector<uint8_t>& pixbuf,
122   unsigned&             width,
123   unsigned&             height,
124   Pixel::Format&        pixelFormat) const
125 {
126   width  = CGImageGetWidth(mImage.get());
127   height = CGImageGetHeight(mImage.get());
128   return true;
129 }
130
131 void NativeImageSourceCocoa::SetSource(Any source)
132 {
133 }
134
135 bool NativeImageSourceCocoa::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
136 {
137   return true;
138 }
139
140 bool NativeImageSourceCocoa::CreateResource()
141 {
142   return false;
143 }
144
145 void NativeImageSourceCocoa::DestroyResource()
146 {
147 }
148
149 unsigned int NativeImageSourceCocoa::TargetTexture()
150 {
151   return 0;
152 }
153
154 void NativeImageSourceCocoa::PrepareTexture()
155 {
156 }
157
158 bool NativeImageSourceCocoa::ApplyNativeFragmentShader(std::string& shader)
159 {
160   return false;
161 }
162
163 const char* NativeImageSourceCocoa::GetCustomSamplerTypename() const
164 {
165   return nullptr;
166 }
167
168 int NativeImageSourceCocoa::GetTextureTarget() const
169 {
170   return GL_TEXTURE_2D;
171 }
172
173 Any NativeImageSourceCocoa::GetNativeImageHandle() const
174 {
175   return Any(mImage.get());
176 }
177
178 unsigned int NativeImageSourceCocoa::GetWidth() const
179 {
180   return CGImageGetWidth(mImage.get());
181 }
182
183 unsigned int NativeImageSourceCocoa::GetHeight() const
184 {
185   return CGImageGetHeight(mImage.get());
186 }
187
188 bool NativeImageSourceCocoa::RequiresBlending() const
189 {
190   const auto alphaInfo = CGImageGetAlphaInfo(mImage.get());
191   return alphaInfo != kCGImageAlphaNone && alphaInfo != kCGImageAlphaNoneSkipFirst && alphaInfo != kCGImageAlphaNoneSkipLast;
192 }
193
194 bool NativeImageSourceCocoa::SourceChanged() const
195 {
196   return false;
197 }
198
199 uint8_t* NativeImageSourceCocoa::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
200 {
201   return nullptr;
202 }
203
204 bool NativeImageSourceCocoa::ReleaseBuffer()
205 {
206   return false;
207 }
208
209 void NativeImageSourceCocoa::SetResourceDestructionCallback(EventThreadCallback* callback)
210 {
211   mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
212 }
213
214 } // namespace Dali::Internal::Adaptor