Chromium Code Reviews| 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 "bindings/core/v8/ScriptPromiseResolver.h" | |
| 8 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/DOMRect.h" | |
| 9 #include "core/dom/Document.h" | |
| 10 #include "core/fetch/ImageResource.h" | |
| 11 #include "core/frame/LocalFrame.h" | |
| 12 #include "core/html/HTMLImageElement.h" | |
| 13 #include "platform/graphics/Image.h" | |
| 14 #include "public/platform/InterfaceProvider.h" | |
| 15 #include "third_party/skia/include/core/SkImage.h" | |
| 16 #include "third_party/skia/include/core/SkImageInfo.h" | |
| 9 | 17 |
| 10 namespace blink { | 18 namespace blink { |
| 11 | 19 |
| 12 FaceDetector* FaceDetector::create() { | 20 namespace { |
| 13 return new FaceDetector(); | 21 |
| 22 mojo::ScopedSharedBufferHandle getSharedBufferHandle( | |
| 23 const HTMLImageElement* img) { | |
| 24 ImageResource* const imageResource = img->cachedImage(); | |
| 25 if (!imageResource) { | |
| 26 DLOG(ERROR) << "Failed to convert HTMLImageElement to ImageSource."; | |
| 27 return mojo::ScopedSharedBufferHandle(); | |
| 28 } | |
| 29 | |
| 30 Image* const blinkImage = imageResource->getImage(); | |
| 31 if (!blinkImage) { | |
| 32 DLOG(ERROR) << "Failed to convert ImageSource to blink::Image."; | |
| 33 return mojo::ScopedSharedBufferHandle(); | |
| 34 } | |
| 35 | |
| 36 const sk_sp<SkImage> image = blinkImage->imageForCurrentFrame(); | |
| 37 DCHECK_EQ(img->naturalWidth(), image->width()); | |
| 38 DCHECK_EQ(img->naturalHeight(), image->height()); | |
| 39 | |
| 40 if (!image) { | |
| 41 DLOG(ERROR) << "Failed to convert blink::Image to sk_sp<SkImage>."; | |
| 42 return mojo::ScopedSharedBufferHandle(); | |
| 43 } | |
| 44 | |
| 45 const SkImageInfo skiaInfo = | |
| 46 SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType()); | |
| 47 | |
| 48 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()); | |
| 49 mojo::ScopedSharedBufferHandle sharedBufferHandle = | |
| 50 mojo::SharedBufferHandle::Create(allocationSize); | |
| 51 const mojo::ScopedSharedBufferMapping mappedBuffer = | |
| 52 sharedBufferHandle->Map(allocationSize); | |
| 53 DCHECK(mappedBuffer); | |
| 54 | |
| 55 const SkPixmap pixmap(skiaInfo, mappedBuffer.get(), skiaInfo.minRowBytes()); | |
| 56 if (!image->readPixels(pixmap, 0, 0)) { | |
| 57 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; | |
| 58 return mojo::ScopedSharedBufferHandle(); | |
| 59 } | |
| 60 | |
| 61 return sharedBufferHandle; | |
| 62 } | |
| 63 | |
| 64 } // anonymous namespace | |
| 65 | |
| 66 FaceDetector* FaceDetector::create(const Document& document) { | |
| 67 return new FaceDetector(*document.frame()); | |
| 68 } | |
| 69 | |
| 70 FaceDetector::FaceDetector(LocalFrame& frame) { | |
| 71 DCHECK(!m_service.is_bound()); | |
| 72 DCHECK(frame.interfaceProvider()); | |
| 73 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service)); | |
| 14 } | 74 } |
| 15 | 75 |
| 16 ScriptPromise FaceDetector::detect(ScriptState* scriptState, | 76 ScriptPromise FaceDetector::detect(ScriptState* scriptState, |
| 17 const HTMLImageElement* image) { | 77 const HTMLImageElement* img) { |
| 18 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 78 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 19 ScriptPromise promise = resolver->promise(); | 79 ScriptPromise promise = resolver->promise(); |
| 20 resolver->reject(DOMException::create(NotSupportedError, "Not implemented")); | 80 |
| 81 if (!m_service) { | |
| 82 resolver->reject(DOMException::create( | |
| 83 NotFoundError, "Face detection service unavailable.")); | |
| 84 return promise; | |
| 85 } | |
| 86 | |
| 87 if (!img) { | |
| 88 resolver->reject(DOMException::create( | |
| 89 SyntaxError, "The provided HTMLImageElement is empty.")); | |
| 90 return promise; | |
| 91 } | |
| 92 | |
| 93 // TODO(xianglu): Add security check when the spec is ready. | |
| 94 // https://crbug.com/646083 | |
| 95 mojo::ScopedSharedBufferHandle sharedBufferHandle = | |
| 96 getSharedBufferHandle(img); | |
| 97 if (!sharedBufferHandle->is_valid()) { | |
| 98 resolver->reject( | |
| 99 DOMException::create(SyntaxError, "Failed to get sharedBufferHandle.")); | |
| 100 return promise; | |
| 101 } | |
| 102 | |
| 103 m_serviceRequests.add(resolver); | |
| 104 DCHECK(m_service.is_bound()); | |
| 105 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(), | |
| 106 img->naturalHeight(), | |
| 107 convertToBaseCallback(WTF::bind( | |
| 108 &FaceDetector::onDetectFace, wrapPersistent(this), | |
| 109 wrapPersistent(resolver)))); | |
| 110 sharedBufferHandle.reset(); | |
| 21 return promise; | 111 return promise; |
| 22 } | 112 } |
| 23 | 113 |
| 114 void FaceDetector::onDetectFace( | |
| 115 ScriptPromiseResolver* resolver, | |
| 116 mojom::blink::FaceDetectionResultPtr faceDetectionResult) { | |
| 117 if (!m_serviceRequests.contains(resolver)) | |
| 118 return; | |
| 119 | |
| 120 HeapVector<Member<DOMRect>> detectedFaces; | |
| 121 for (const auto& boundingBox : faceDetectionResult->boundingBoxes) { | |
| 122 DCHECK_LE(boundingBox->x + boundingBox->width, 1.0); | |
|
esprehn
2016/10/03 23:59:37
what's the reason for asserting that the x + width
xianglu
2016/10/04 00:58:26
Removed.
| |
| 123 DCHECK_LE(boundingBox->y + boundingBox->height, 1.0); | |
|
esprehn
2016/10/03 23:59:37
ditto
| |
| 124 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, | |
| 125 boundingBox->width, | |
| 126 boundingBox->height)); | |
| 127 } | |
| 128 | |
| 129 resolver->resolve(detectedFaces); | |
| 130 m_serviceRequests.remove(resolver); | |
| 131 } | |
| 132 | |
| 133 DEFINE_TRACE(FaceDetector) { | |
| 134 visitor->trace(m_serviceRequests); | |
| 135 } | |
| 136 | |
| 24 } // namespace blink | 137 } // namespace blink |
| OLD | NEW |