| 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" |
| 11 #include "core/frame/ImageBitmap.h" | 11 #include "core/frame/ImageBitmap.h" |
| 12 #include "core/frame/LocalDOMWindow.h" | 12 #include "core/frame/LocalDOMWindow.h" |
| 13 #include "core/frame/LocalFrame.h" | 13 #include "core/frame/LocalFrame.h" |
| 14 #include "core/html/HTMLImageElement.h" | 14 #include "core/html/HTMLImageElement.h" |
| 15 #include "core/html/HTMLVideoElement.h" | 15 #include "core/html/HTMLVideoElement.h" |
| 16 #include "core/html/canvas/CanvasImageSource.h" | 16 #include "core/html/canvas/CanvasImageSource.h" |
| 17 #include "modules/shapedetection/FaceDetectorImpl.h" |
| 18 #include "platform/SharedBuffer.h" |
| 17 #include "platform/graphics/Image.h" | 19 #include "platform/graphics/Image.h" |
| 18 #include "public/platform/InterfaceProvider.h" | 20 #include "public/platform/InterfaceProvider.h" |
| 19 #include "third_party/skia/include/core/SkImage.h" | 21 #include "third_party/skia/include/core/SkImage.h" |
| 20 #include "third_party/skia/include/core/SkImageInfo.h" | 22 #include "third_party/skia/include/core/SkImageInfo.h" |
| 21 #include "wtf/CheckedNumeric.h" | 23 #include "wtf/CheckedNumeric.h" |
| 22 | 24 |
| 23 namespace blink { | 25 namespace blink { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 ScriptPromise promise = resolver->promise(); | 62 ScriptPromise promise = resolver->promise(); |
| 61 | 63 |
| 62 if (!imageSourceInternal) { | 64 if (!imageSourceInternal) { |
| 63 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138 | 65 // TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138 |
| 64 NOTIMPLEMENTED() << "Unsupported CanvasImageSource"; | 66 NOTIMPLEMENTED() << "Unsupported CanvasImageSource"; |
| 65 resolver->reject( | 67 resolver->reject( |
| 66 DOMException::create(NotFoundError, "Unsupported source.")); | 68 DOMException::create(NotFoundError, "Unsupported source.")); |
| 67 return promise; | 69 return promise; |
| 68 } | 70 } |
| 69 | 71 |
| 70 if (imageSourceInternal->wouldTaintOrigin( | 72 // if (imageSourceInternal->wouldTaintOrigin( |
| 71 scriptState->getExecutionContext()->getSecurityOrigin())) { | 73 // scriptState->getExecutionContext()->getSecurityOrigin())) { |
| 72 resolver->reject( | 74 // resolver->reject( |
| 73 DOMException::create(SecurityError, "Source would taint origin.")); | 75 // DOMException::create(SecurityError, "Source would taint origin.")); |
| 74 return promise; | 76 // return promise; |
| 75 } | 77 // } |
| 76 | 78 |
| 77 if (imageSource.isHTMLImageElement()) { | 79 if (imageSource.isHTMLImageElement()) { |
| 78 return detectFacesOnImageElement( | 80 return detectFacesOnImageElement( |
| 79 resolver, static_cast<HTMLImageElement*>(imageSourceInternal)); | 81 resolver, static_cast<HTMLImageElement*>(imageSourceInternal)); |
| 80 } | 82 } |
| 81 if (imageSourceInternal->isImageBitmap()) { | 83 if (imageSourceInternal->isImageBitmap()) { |
| 82 return detectFacesOnImageBitmap( | 84 return detectFacesOnImageBitmap( |
| 83 resolver, static_cast<ImageBitmap*>(imageSourceInternal)); | 85 resolver, static_cast<ImageBitmap*>(imageSourceInternal)); |
| 84 } | 86 } |
| 85 if (imageSourceInternal->isVideoElement()) { | 87 if (imageSourceInternal->isVideoElement()) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 108 } | 110 } |
| 109 | 111 |
| 110 Image* const blinkImage = imageResource->getImage(); | 112 Image* const blinkImage = imageResource->getImage(); |
| 111 if (!blinkImage) { | 113 if (!blinkImage) { |
| 112 resolver->reject(DOMException::create( | 114 resolver->reject(DOMException::create( |
| 113 InvalidStateError, "Failed to get image from resource.")); | 115 InvalidStateError, "Failed to get image from resource.")); |
| 114 return promise; | 116 return promise; |
| 115 } | 117 } |
| 116 | 118 |
| 117 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame(); | 119 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame(); |
| 118 DCHECK_EQ(img->naturalWidth(), image->width()); | |
| 119 DCHECK_EQ(img->naturalHeight(), image->height()); | |
| 120 | 120 |
| 121 if (!image) { | 121 if (!image) { |
| 122 resolver->reject(DOMException::create( | 122 resolver->reject(DOMException::create( |
| 123 InvalidStateError, "Failed to get image from current frame.")); | 123 InvalidStateError, "Failed to get image from current frame.")); |
| 124 return promise; | 124 return promise; |
| 125 } | 125 } |
| 126 | 126 |
| 127 const SkImageInfo skiaInfo = | 127 const SkImageInfo skiaInfo = |
| 128 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); | 128 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); |
| 129 | 129 |
| 130 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); | 130 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); |
| 131 | |
| 132 mojo::ScopedSharedBufferHandle sharedBufferHandle = | 131 mojo::ScopedSharedBufferHandle sharedBufferHandle = |
| 133 mojo::SharedBufferHandle::Create(allocationSize); | 132 mojo::SharedBufferHandle::Create(allocationSize); |
| 134 if (!sharedBufferHandle.is_valid()) { | 133 if (!sharedBufferHandle.is_valid()) { |
| 135 DLOG(ERROR) << "Requested allocation : " << allocationSize | 134 DLOG(ERROR) << "Requested allocation : " << allocationSize |
| 136 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB "; | 135 << "B, larger than |mojo::edk::kMaxSharedBufferSize| == 16MB "; |
| 137 // TODO(xianglu): For now we reject the promise if the image is too large. | 136 // TODO(xianglu): For now we reject the promise if the image is too large. |
| 138 // But consider resizing the image to remove restriction on the user side. | 137 // But consider resizing the image to remove restriction on the user side. |
| 139 // Also, add LayoutTests for this case later. | 138 // Also, add LayoutTests for this case later. |
| 140 resolver->reject( | 139 resolver->reject( |
| 141 DOMException::create(InvalidStateError, "Image exceeds size limit.")); | 140 DOMException::create(InvalidStateError, "Image exceeds size limit.")); |
| 142 return promise; | 141 return promise; |
| 143 } | 142 } |
| 144 | 143 |
| 145 const mojo::ScopedSharedBufferMapping mappedBuffer = | 144 const mojo::ScopedSharedBufferMapping mappedBuffer = |
| 146 sharedBufferHandle->Map(allocationSize); | 145 sharedBufferHandle->Map(allocationSize); |
| 147 | 146 |
| 148 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); | 147 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); |
| 149 if (!image->readPixels(pixmap, 0, 0)) { | 148 if (!image->readPixels(pixmap, 0, 0)) { |
| 150 resolver->reject(DOMException::create( | 149 resolver->reject(DOMException::create( |
| 151 InvalidStateError, | 150 InvalidStateError, |
| 152 "Failed to read pixels: Unable to decompress or unsupported format.")); | 151 "Failed to read pixels: Unable to decompress or unsupported format.")); |
| 153 return promise; | 152 return promise; |
| 154 } | 153 } |
| 154 DLOG(ERROR) << "pixmap.getSafeSize():" << pixmap.getSafeSize(); |
| 155 | 155 |
| 156 if (!m_service) { | 156 FaceDetectorImpl* detector = FaceDetectorImpl::Create(); |
| 157 resolver->reject(DOMException::create( | 157 HeapVector<Member<DOMRect>> detectedFaces = detector->detectFace( |
| 158 NotSupportedError, "Face detection service unavailable.")); | 158 pixmap.writable_addr(), img->naturalWidth(), img->naturalHeight()); |
| 159 return promise; | 159 resolver->resolve(detectedFaces); |
| 160 } | |
| 161 | |
| 162 m_serviceRequests.add(resolver); | |
| 163 DCHECK(m_service.is_bound()); | |
| 164 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), | |
| 165 img->naturalHeight(), | |
| 166 convertToBaseCallback(WTF::bind( | |
| 167 &FaceDetector::onDetectFace, wrapPersistent(this), | |
| 168 wrapPersistent(resolver)))); | |
| 169 return promise; | 160 return promise; |
| 170 } | 161 } |
| 171 | 162 |
| 172 ScriptPromise FaceDetector::detectFacesOnImageBitmap( | 163 ScriptPromise FaceDetector::detectFacesOnImageBitmap( |
| 173 ScriptPromiseResolver* resolver, | 164 ScriptPromiseResolver* resolver, |
| 174 ImageBitmap* imageBitmap) { | 165 ImageBitmap* imageBitmap) { |
| 175 ScriptPromise promise = resolver->promise(); | 166 ScriptPromise promise = resolver->promise(); |
| 176 if (!imageBitmap->originClean()) { | 167 if (!imageBitmap->originClean()) { |
| 177 resolver->reject( | 168 resolver->reject( |
| 178 DOMException::create(SecurityError, "ImageBitmap is not origin clean")); | 169 DOMException::create(SecurityError, "ImageBitmap is not origin clean")); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 | 300 |
| 310 resolver->resolve(detectedFaces); | 301 resolver->resolve(detectedFaces); |
| 311 m_serviceRequests.remove(resolver); | 302 m_serviceRequests.remove(resolver); |
| 312 } | 303 } |
| 313 | 304 |
| 314 DEFINE_TRACE(FaceDetector) { | 305 DEFINE_TRACE(FaceDetector) { |
| 315 visitor->trace(m_serviceRequests); | 306 visitor->trace(m_serviceRequests); |
| 316 } | 307 } |
| 317 | 308 |
| 318 } // namespace blink | 309 } // namespace blink |
| OLD | NEW |