| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/shapedetection/FaceDetector.h" | 5 #include "modules/shapedetection/FaceDetector.h" |
| 6 | 6 |
| 7 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/DOMRect.h" | 8 #include "core/dom/DOMRect.h" |
| 9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
| 10 #include "core/fetch/ImageResource.h" | 10 #include "core/fetch/ImageResource.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 if (!image) { | 41 if (!image) { |
| 42 DLOG(ERROR) << "Failed to convert blink::Image to sk_sp<SkImage>."; | 42 DLOG(ERROR) << "Failed to convert blink::Image to sk_sp<SkImage>."; |
| 43 return mojo::ScopedSharedBufferHandle(); | 43 return mojo::ScopedSharedBufferHandle(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 const SkImageInfo skiaInfo = | 46 const SkImageInfo skiaInfo = |
| 47 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); | 47 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); |
| 48 | 48 |
| 49 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); | 49 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); |
| 50 |
| 50 mojo::ScopedSharedBufferHandle sharedBufferHandle = | 51 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
| 51 mojo::SharedBufferHandle::Create(allocationSize); | 52 mojo::SharedBufferHandle::Create(allocationSize); |
| 53 if (!sharedBufferHandle.is_valid()) { |
| 54 // TODO(xianglu): Do something when the image is too large. |
| 55 DLOG(ERROR) << "Failed to create a sharedBufferHandle. allocationSize = " |
| 56 << allocationSize << "bytes. limit = 16777216"; |
| 57 return mojo::ScopedSharedBufferHandle(); |
| 58 } |
| 59 |
| 52 const mojo::ScopedSharedBufferMapping mappedBuffer = | 60 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 53 sharedBufferHandle->Map(allocationSize); | 61 sharedBufferHandle->Map(allocationSize); |
| 54 DCHECK(mappedBuffer); | |
| 55 | 62 |
| 56 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); | 63 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); |
| 57 if (!image->readPixels(pixmap, 0, 0)) { | 64 if (!image->readPixels(pixmap, 0, 0)) { |
| 58 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; | 65 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; |
| 59 return mojo::ScopedSharedBufferHandle(); | 66 return mojo::ScopedSharedBufferHandle(); |
| 60 } | 67 } |
| 61 | 68 |
| 62 return sharedBufferHandle; | 69 return sharedBufferHandle; |
| 63 } | 70 } |
| 64 | 71 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 89 resolver->reject(DOMException::create( | 96 resolver->reject(DOMException::create( |
| 90 SyntaxError, "The provided HTMLImageElement is empty.")); | 97 SyntaxError, "The provided HTMLImageElement is empty.")); |
| 91 return promise; | 98 return promise; |
| 92 } | 99 } |
| 93 | 100 |
| 94 // TODO(xianglu): Add security check when the spec is ready. | 101 // TODO(xianglu): Add security check when the spec is ready. |
| 95 // https://crbug.com/646083 | 102 // https://crbug.com/646083 |
| 96 mojo::ScopedSharedBufferHandle sharedBufferHandle = | 103 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
| 97 getSharedBufferHandle(img); | 104 getSharedBufferHandle(img); |
| 98 if (!sharedBufferHandle->is_valid()) { | 105 if (!sharedBufferHandle->is_valid()) { |
| 99 resolver->reject( | 106 resolver->reject(DOMException::create( |
| 100 DOMException::create(SyntaxError, "Failed to get sharedBufferHandle.")); | 107 SyntaxError, "Request for sharedBufferHandle failed.")); |
| 101 return promise; | 108 return promise; |
| 102 } | 109 } |
| 103 | 110 |
| 104 m_serviceRequests.add(resolver); | 111 m_serviceRequests.add(resolver); |
| 105 DCHECK(m_service.is_bound()); | 112 DCHECK(m_service.is_bound()); |
| 106 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), | 113 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), |
| 107 img->naturalHeight(), | 114 img->naturalHeight(), |
| 108 convertToBaseCallback(WTF::bind( | 115 convertToBaseCallback(WTF::bind( |
| 109 &FaceDetector::onDetectFace, wrapPersistent(this), | 116 &FaceDetector::onDetectFace, wrapPersistent(this), |
| 110 wrapPersistent(resolver)))); | 117 wrapPersistent(resolver)))); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 127 | 134 |
| 128 resolver->resolve(detectedFaces); | 135 resolver->resolve(detectedFaces); |
| 129 m_serviceRequests.remove(resolver); | 136 m_serviceRequests.remove(resolver); |
| 130 } | 137 } |
| 131 | 138 |
| 132 DEFINE_TRACE(FaceDetector) { | 139 DEFINE_TRACE(FaceDetector) { |
| 133 visitor->trace(m_serviceRequests); | 140 visitor->trace(m_serviceRequests); |
| 134 } | 141 } |
| 135 | 142 |
| 136 } // namespace blink | 143 } // namespace blink |
| OLD | NEW |