| 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" |
| 12 | 14 |
| 13 namespace shape_detection { | 15 namespace shape_detection { |
| 14 | 16 |
| 15 // These formats are available but not public until Mac 10.11. | 17 base::scoped_nsobject<CIImage> CreateCIImageFromSkBitmap( |
| 16 #if !defined(MAC_OS_X_VERSION_10_11) || \ | 18 const SkBitmap& bitmap) { |
| 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) { | |
| 27 base::CheckedNumeric<uint32_t> num_pixels = | 19 base::CheckedNumeric<uint32_t> num_pixels = |
| 28 base::CheckedNumeric<uint32_t>(width) * height; | 20 base::CheckedNumeric<uint32_t>(bitmap.width()) * bitmap.height(); |
| 29 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; | 21 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; |
| 30 if (!num_bytes.IsValid()) { | 22 if (!num_bytes.IsValid()) { |
| 31 DLOG(ERROR) << "Data overflow"; | 23 DLOG(ERROR) << "Data overflow"; |
| 32 return base::scoped_nsobject<CIImage>(); | 24 return base::scoped_nsobject<CIImage>(); |
| 33 } | 25 } |
| 34 | 26 |
| 35 base::SharedMemoryHandle memory_handle; | 27 // First convert SkBitmap to CGImageRef. |
| 36 size_t memory_size = 0; | 28 base::ScopedCFTypeRef<CGImageRef> cg_image( |
| 37 bool read_only_flag = false; | 29 SkCreateCGImageRefWithColorspace(bitmap, NULL)); |
| 38 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | 30 if (!cg_image) { |
| 39 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag); | 31 DLOG(ERROR) << "Failed to create CGImageRef"; |
| 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"; | |
| 43 return base::scoped_nsobject<CIImage>(); | 32 return base::scoped_nsobject<CIImage>(); |
| 44 } | 33 } |
| 45 | 34 |
| 46 auto shared_memory = | 35 base::scoped_nsobject<CIImage> ci_image( |
| 47 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); | 36 [[CIImage alloc] initWithCGImage:cg_image]); |
| 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]); | |
| 67 if (!ci_image) { | 37 if (!ci_image) { |
| 68 DLOG(ERROR) << "Failed to create CIImage"; | 38 DLOG(ERROR) << "Failed to create CIImage"; |
| 69 return base::scoped_nsobject<CIImage>(); | 39 return base::scoped_nsobject<CIImage>(); |
| 70 } | 40 } |
| 71 return ci_image; | 41 return ci_image; |
| 72 } | 42 } |
| 73 | 43 |
| 74 } // namespace shape_detection | 44 } // namespace shape_detection |
| OLD | NEW |