2 * Copyright (c) 2020 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/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>
30 namespace Dali::Internal::Adaptor
33 using Dali::Integration::PixelBuffer;
35 NativeImageSourceCocoa* NativeImageSourceCocoa::New(
38 Dali::NativeImageSource::ColorDepth depth,
42 return new NativeImageSourceCocoa( width, height, depth, nativeImageSource );
45 NativeImageSourceCocoa::NativeImageSourceCocoa(
48 Dali::NativeImageSource::ColorDepth depth,
51 : mImage(MakeRef<CGImageRef>(nullptr))
53 DALI_ASSERT_ALWAYS( Adaptor::IsAvailable() );
54 DALI_ASSERT_ALWAYS( nativeImageSource.Empty() );
56 CFStringRef colorSpaceName;
57 CGImageAlphaInfo alphaInfo;
58 std::size_t bitsPerPixel;
62 case Dali::NativeImageSource::COLOR_DEPTH_8:
63 colorSpaceName = kCGColorSpaceGenericGray;
64 alphaInfo = kCGImageAlphaNone;
67 case Dali::NativeImageSource::COLOR_DEPTH_16:
68 colorSpaceName = kCGColorSpaceSRGB;
69 alphaInfo = kCGImageAlphaNone;
72 case Dali::NativeImageSource::COLOR_DEPTH_24:
73 colorSpaceName = kCGColorSpaceSRGB;
74 alphaInfo = kCGImageAlphaNone;
77 case Dali::NativeImageSource::COLOR_DEPTH_32:
79 colorSpaceName = kCGColorSpaceSRGB;
80 alphaInfo = kCGImageAlphaLast;
85 // round to next 16 bytes boundary
86 std::size_t bytesPerRow = width & ~0xf;
87 bytesPerRow = bytesPerRow ? bytesPerRow + 16 : width;
89 auto dataProvider = MakeRef(CGDataProviderCreateWithData(nullptr, nullptr, 0, nullptr));
90 auto colorSpace = MakeRef(CGColorSpaceCreateWithName(colorSpaceName));
91 mImage = MakeRef(CGImageCreate(
102 kCGRenderingIntentDefault
107 colorSpace.release();
108 dataProvider.release();
111 DALI_ASSERT_ALWAYS(mImage.get());
114 NativeImageSourceCocoa::~NativeImageSourceCocoa()
118 Any NativeImageSourceCocoa::GetNativeImageSource() const
123 bool NativeImageSourceCocoa::GetPixels(
124 std::vector<uint8_t>& pixbuf,
125 unsigned& width, unsigned& height,
126 Pixel::Format& pixelFormat
129 width = CGImageGetWidth(mImage.get());
130 height = CGImageGetHeight(mImage.get());
134 void NativeImageSourceCocoa::SetSource( Any source )
138 bool NativeImageSourceCocoa::IsColorDepthSupported( Dali::NativeImageSource::ColorDepth colorDepth )
143 bool NativeImageSourceCocoa::CreateResource()
148 void NativeImageSourceCocoa::DestroyResource()
152 unsigned int NativeImageSourceCocoa::TargetTexture()
157 void NativeImageSourceCocoa::PrepareTexture()
161 const char* NativeImageSourceCocoa::GetCustomFragmentPrefix() const
166 const char* NativeImageSourceCocoa::GetCustomSamplerTypename() const
171 int NativeImageSourceCocoa::GetTextureTarget() const
173 return GL_TEXTURE_2D;
176 Any NativeImageSourceCocoa::GetNativeImageHandle() const
178 return Any(mImage.get());
181 unsigned int NativeImageSourceCocoa::GetWidth() const
183 return CGImageGetWidth(mImage.get());
186 unsigned int NativeImageSourceCocoa::GetHeight() const
188 return CGImageGetHeight(mImage.get());
191 bool NativeImageSourceCocoa::RequiresBlending() const
193 const auto alphaInfo = CGImageGetAlphaInfo(mImage.get());
195 alphaInfo != kCGImageAlphaNone
196 && alphaInfo != kCGImageAlphaNoneSkipFirst
197 && alphaInfo != kCGImageAlphaNoneSkipLast;
200 bool NativeImageSourceCocoa::SourceChanged() const
205 uint8_t* NativeImageSourceCocoa::AcquireBuffer( uint16_t& width, uint16_t& height, uint16_t& stride )
211 bool NativeImageSourceCocoa::ReleaseBuffer()
216 } // namespace Dali::Internal::Adaptor