2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/imaging/macos/native-image-source-impl-mac.h>
22 #include <dali/integration-api/debug.h>
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>
30 namespace Dali::Internal::Adaptor
32 using Dali::Integration::PixelBuffer;
34 NativeImageSourceCocoa* NativeImageSourceCocoa::New(
37 Dali::NativeImageSource::ColorDepth depth,
38 Any nativeImageSource)
40 return new NativeImageSourceCocoa(width, height, depth, nativeImageSource);
43 NativeImageSourceCocoa::NativeImageSourceCocoa(
46 Dali::NativeImageSource::ColorDepth depth,
47 Any nativeImageSource)
48 : mImage(MakeRef<CGImageRef>(nullptr))
50 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
51 DALI_ASSERT_ALWAYS(nativeImageSource.Empty());
53 CFStringRef colorSpaceName;
54 CGImageAlphaInfo alphaInfo;
55 std::size_t bitsPerPixel;
59 case Dali::NativeImageSource::COLOR_DEPTH_8:
60 colorSpaceName = kCGColorSpaceGenericGray;
61 alphaInfo = kCGImageAlphaNone;
64 case Dali::NativeImageSource::COLOR_DEPTH_16:
65 colorSpaceName = kCGColorSpaceSRGB;
66 alphaInfo = kCGImageAlphaNone;
69 case Dali::NativeImageSource::COLOR_DEPTH_24:
70 colorSpaceName = kCGColorSpaceSRGB;
71 alphaInfo = kCGImageAlphaNone;
74 case Dali::NativeImageSource::COLOR_DEPTH_32:
76 colorSpaceName = kCGColorSpaceSRGB;
77 alphaInfo = kCGImageAlphaLast;
82 // round to next 16 bytes boundary
83 std::size_t bytesPerRow = width & ~0xf;
84 bytesPerRow = bytesPerRow ? bytesPerRow + 16 : width;
86 auto dataProvider = MakeRef(CGDataProviderCreateWithData(nullptr, nullptr, 0, nullptr));
87 auto colorSpace = MakeRef(CGColorSpaceCreateWithName(colorSpaceName));
88 mImage = MakeRef(CGImageCreate(
99 kCGRenderingIntentDefault));
103 colorSpace.release();
104 dataProvider.release();
107 DALI_ASSERT_ALWAYS(mImage.get());
110 NativeImageSourceCocoa::~NativeImageSourceCocoa()
114 Any NativeImageSourceCocoa::GetNativeImageSource() const
119 bool NativeImageSourceCocoa::GetPixels(
120 std::vector<uint8_t>& pixbuf,
123 Pixel::Format& pixelFormat) const
125 width = CGImageGetWidth(mImage.get());
126 height = CGImageGetHeight(mImage.get());
130 void NativeImageSourceCocoa::SetSource(Any source)
134 bool NativeImageSourceCocoa::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
139 bool NativeImageSourceCocoa::CreateResource()
144 void NativeImageSourceCocoa::DestroyResource()
148 unsigned int NativeImageSourceCocoa::TargetTexture()
153 void NativeImageSourceCocoa::PrepareTexture()
157 bool NativeImageSourceCocoa::ApplyNativeFragmentShader(std::string& shader)
162 const char* NativeImageSourceCocoa::GetCustomSamplerTypename() const
167 int NativeImageSourceCocoa::GetTextureTarget() const
169 return GL_TEXTURE_2D;
172 Any NativeImageSourceCocoa::GetNativeImageHandle() const
174 return Any(mImage.get());
177 unsigned int NativeImageSourceCocoa::GetWidth() const
179 return CGImageGetWidth(mImage.get());
182 unsigned int NativeImageSourceCocoa::GetHeight() const
184 return CGImageGetHeight(mImage.get());
187 bool NativeImageSourceCocoa::RequiresBlending() const
189 const auto alphaInfo = CGImageGetAlphaInfo(mImage.get());
190 return alphaInfo != kCGImageAlphaNone && alphaInfo != kCGImageAlphaNoneSkipFirst && alphaInfo != kCGImageAlphaNoneSkipLast;
193 bool NativeImageSourceCocoa::SourceChanged() const
198 uint8_t* NativeImageSourceCocoa::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
203 bool NativeImageSourceCocoa::ReleaseBuffer()
208 } // namespace Dali::Internal::Adaptor