Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: services/shape_detection/detection_utils_mac.mm

Issue 2629433003: ShapeDetection: use mojom::Bitmap for mojo interface. (Closed)
Patch Set: ShapeDetection: use mojom::Bitmap for mojo interface. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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> CreateCIImageFromData(
16 #if !defined(MAC_OS_X_VERSION_10_11) || \ 18 const SkBitmap& bitmap_data) {
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_data.width())
21 * bitmap_data.height();
29 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4; 22 base::CheckedNumeric<uint32_t> num_bytes = num_pixels * 4;
30 if (!num_bytes.IsValid()) { 23 if (!num_bytes.IsValid()) {
31 DLOG(ERROR) << "Data overflow"; 24 DLOG(ERROR) << "Data overflow";
32 return base::scoped_nsobject<CIImage>(); 25 return base::scoped_nsobject<CIImage>();
33 } 26 }
34 27
35 base::SharedMemoryHandle memory_handle; 28 base::ScopedCFTypeRef<CGColorSpaceRef> colorspace(
36 size_t memory_size = 0; 29 CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
37 bool read_only_flag = false; 30
38 const MojoResult result = mojo::UnwrapSharedMemoryHandle( 31 // First convert SkBitmap to CGImageRef.
39 std::move(frame_data), &memory_handle, &memory_size, &read_only_flag); 32 base::ScopedCFTypeRef<CGImageRef> cg_image(
40 DCHECK_EQ(MOJO_RESULT_OK, result) << "Failed to unwrap SharedBufferHandle"; 33 SkCreateCGImageRefWithColorspace(bitmap_data, colorspace));
41 if (!memory_size || memory_size != num_bytes.ValueOrDie()) { 34 if (!cg_image) {
42 DLOG(ERROR) << "Invalid image size"; 35 DLOG(ERROR) << "Failed to create CGImageRef";
43 return base::scoped_nsobject<CIImage>(); 36 return base::scoped_nsobject<CIImage>();
44 } 37 }
45 38
46 auto shared_memory =
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. 39 // CIImage will return nil if RGBA8 is not supported in a certain version.
61 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc] 40 base::scoped_nsobject<CIImage> ci_image([[CIImage alloc]
62 initWithBitmapData:byte_data 41 initWithCGImage:cg_image]);
63 bytesPerRow:width * 4
64 size:CGSizeMake(width, height)
65 format:kCIFormatRGBA8
66 colorSpace:colorspace]);
67 if (!ci_image) { 42 if (!ci_image) {
68 DLOG(ERROR) << "Failed to create CIImage"; 43 DLOG(ERROR) << "Failed to create CIImage";
69 return base::scoped_nsobject<CIImage>(); 44 return base::scoped_nsobject<CIImage>();
70 } 45 }
71 return ci_image; 46 return ci_image;
72 } 47 }
73 48
74 } // namespace shape_detection 49 } // namespace shape_detection
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698