OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/shape_detection/detection_utils_mac.h" | 5 #include "services/shape_detection/detection_utils_mac.h" |
6 | 6 |
7 #include "base/mac/scoped_cftyperef.h" | 7 #include "base/mac/scoped_cftyperef.h" |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
10 #include "mojo/public/cpp/system/platform_handle.h" | 10 #include "mojo/public/cpp/system/platform_handle.h" |
11 #include "services/shape_detection/barcode_detection_impl.h" | 11 #include "services/shape_detection/barcode_detection_impl.h" |
12 #include "skia/ext/skia_utils_mac.h" | |
13 #include "third_party/skia/include/utils/mac/SkCGUtils.h" | |
14 | 12 |
15 namespace shape_detection { | 13 namespace shape_detection { |
16 | 14 |
17 base::scoped_nsobject<CIImage> CreateCIImageFromSkBitmap( | 15 // These formats are available but not public until Mac 10.11. |
18 const SkBitmap& bitmap) { | 16 #if !defined(MAC_OS_X_VERSION_10_11) || \ |
| 17 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_11 |
| 18 const int kCIFormatRGBA8 = 24; |
| 19 #else |
| 20 //static_assert(kCIFormatRGBA8 == 24, "RGBA8 format enum index."); |
| 21 #endif |
| 22 |
| 23 base::scoped_nsobject<CIImage> CreateCIImageFromSharedMemory( |
| 24 mojo::ScopedSharedBufferHandle frame_data, |
| 25 uint32_t width, |
| 26 uint32_t height) { |
19 base::CheckedNumeric<uint32_t> num_pixels = | 27 base::CheckedNumeric<uint32_t> num_pixels = |
20 base::CheckedNumeric<uint32_t>(bitmap.width()) * bitmap.height(); | 28 base::CheckedNumeric<uint32_t>(width) * height; |
21 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; | 29 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; |
22 if (!num_bytes.IsValid()) { | 30 if (!num_bytes.IsValid()) { |
23 DLOG(ERROR) << "Data overflow"; | 31 DLOG(ERROR) << "Data overflow"; |
24 return base::scoped_nsobject<CIImage>(); | 32 return base::scoped_nsobject<CIImage>(); |
25 } | 33 } |
26 | 34 |
27 // First convert SkBitmap to CGImageRef. | 35 base::SharedMemoryHandle memory_handle; |
28 base::ScopedCFTypeRef<CGImageRef> cg_image( | 36 size_t memory_size = 0; |
29 SkCreateCGImageRefWithColorspace(bitmap, NULL)); | 37 bool read_only_flag = false; |
30 if (!cg_image) { | 38 const MojoResult result = mojo::UnwrapSharedMemoryHandle( |
31 DLOG(ERROR) << "Failed to create CGImageRef"; | 39 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag); |
| 40 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle"; |
| 41 if (!memory_size || memory_size != num_bytes.ValueOrDie()) { |
| 42 DLOG(ERROR) << "Invalid image size"; |
32 return base::scoped_nsobject<CIImage>(); | 43 return base::scoped_nsobject<CIImage>(); |
33 } | 44 } |
34 | 45 |
35 base::scoped_nsobject<CIImage> ci_image( | 46 auto shared_memory = |
36 [[CIImage alloc] initWithCGImage:cg_image]); | 47 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); |
| 48 if (!shared_memory->Map(memory_size)) { |
| 49 DLOG(ERROR) << "Failed to map bytes from shared memory"; |
| 50 return base::scoped_nsobject<CIImage>(); |
| 51 } |
| 52 |
| 53 NSData* byte_data = [NSData dataWithBytesNoCopy:shared_memory->memory() |
| 54 length:num_bytes.ValueOrDie() |
| 55 freeWhenDone:NO]; |
| 56 |
| 57 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace( |
| 58 CGColorSpaceCreateWithName(kCGColorSpaceSRGB)); |
| 59 |
| 60 // CIImage will return nil if RGBA8 is not supported in a certain version. |
| 61 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc] |
| 62 initWithBitmapData:byte_data |
| 63 bytesPerRow:width * 4 |
| 64 size:CGSizeMake(width, height) |
| 65 format:kCIFormatRGBA8 |
| 66 colorSpace:colorspace]); |
37 if (!ci_image) { | 67 if (!ci_image) { |
38 DLOG(ERROR) << "Failed to create CIImage"; | 68 DLOG(ERROR) << "Failed to create CIImage"; |
39 return base::scoped_nsobject<CIImage>(); | 69 return base::scoped_nsobject<CIImage>(); |
40 } | 70 } |
41 return ci_image; | 71 return ci_image; |
42 } | 72 } |
43 | 73 |
44 } // namespace shape_detection | 74 } // namespace shape_detection |
OLD | NEW |