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)),
49 mResourceDestructionCallback()
51 DALI_ASSERT_ALWAYS(Adaptor::IsAvailable());
52 DALI_ASSERT_ALWAYS(nativeImageSource.Empty());
54 CFStringRef colorSpaceName;
55 CGImageAlphaInfo alphaInfo;
56 std::size_t bitsPerPixel;
60 case Dali::NativeImageSource::COLOR_DEPTH_8:
61 colorSpaceName = kCGColorSpaceGenericGray;
62 alphaInfo = kCGImageAlphaNone;
65 case Dali::NativeImageSource::COLOR_DEPTH_16:
66 colorSpaceName = kCGColorSpaceSRGB;
67 alphaInfo = kCGImageAlphaNone;
70 case Dali::NativeImageSource::COLOR_DEPTH_24:
71 colorSpaceName = kCGColorSpaceSRGB;
72 alphaInfo = kCGImageAlphaNone;
75 case Dali::NativeImageSource::COLOR_DEPTH_32:
77 colorSpaceName = kCGColorSpaceSRGB;
78 alphaInfo = kCGImageAlphaLast;
83 // round to next 16 bytes boundary
84 std::size_t bytesPerRow = width & ~0xf;
85 bytesPerRow = bytesPerRow ? bytesPerRow + 16 : width;
87 auto dataProvider = MakeRef(CGDataProviderCreateWithData(nullptr, nullptr, 0, nullptr));
88 auto colorSpace = MakeRef(CGColorSpaceCreateWithName(colorSpaceName));
89 mImage = MakeRef(CGImageCreate(
100 kCGRenderingIntentDefault));
104 colorSpace.release();
105 dataProvider.release();
108 DALI_ASSERT_ALWAYS(mImage.get());
111 NativeImageSourceCocoa::~NativeImageSourceCocoa()
115 Any NativeImageSourceCocoa::GetNativeImageSource() const
120 bool NativeImageSourceCocoa::GetPixels(
121 std::vector<uint8_t>& pixbuf,
124 Pixel::Format& pixelFormat) const
126 width = CGImageGetWidth(mImage.get());
127 height = CGImageGetHeight(mImage.get());
131 void NativeImageSourceCocoa::SetSource(Any source)
135 bool NativeImageSourceCocoa::IsColorDepthSupported(Dali::NativeImageSource::ColorDepth colorDepth)
140 bool NativeImageSourceCocoa::CreateResource()
145 void NativeImageSourceCocoa::DestroyResource()
149 unsigned int NativeImageSourceCocoa::TargetTexture()
154 void NativeImageSourceCocoa::PrepareTexture()
158 bool NativeImageSourceCocoa::ApplyNativeFragmentShader(std::string& shader)
163 const char* NativeImageSourceCocoa::GetCustomSamplerTypename() const
168 int NativeImageSourceCocoa::GetTextureTarget() const
170 return GL_TEXTURE_2D;
173 Any NativeImageSourceCocoa::GetNativeImageHandle() const
175 return Any(mImage.get());
178 unsigned int NativeImageSourceCocoa::GetWidth() const
180 return CGImageGetWidth(mImage.get());
183 unsigned int NativeImageSourceCocoa::GetHeight() const
185 return CGImageGetHeight(mImage.get());
188 bool NativeImageSourceCocoa::RequiresBlending() const
190 const auto alphaInfo = CGImageGetAlphaInfo(mImage.get());
191 return alphaInfo != kCGImageAlphaNone && alphaInfo != kCGImageAlphaNoneSkipFirst && alphaInfo != kCGImageAlphaNoneSkipLast;
194 bool NativeImageSourceCocoa::SourceChanged() const
199 uint8_t* NativeImageSourceCocoa::AcquireBuffer(uint16_t& width, uint16_t& height, uint16_t& stride)
204 bool NativeImageSourceCocoa::ReleaseBuffer()
209 void NativeImageSourceCocoa::SetResourceDestructionCallback(EventThreadCallback* callback)
211 mResourceDestructionCallback = std::unique_ptr<EventThreadCallback>(callback);
214 } // namespace Dali::Internal::Adaptor