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

Side by Side Diff: third_party/WebKit/Source/modules/shapedetection/FaceDetector.cpp

Issue 2369693002: Shapedetection module: Blink side implementation (Closed)
Patch Set: Code reformatting Created 4 years, 2 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 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.";
mcasas 2016/10/03 22:18:09 nit: just for cases like this, we can also conside
xianglu 2016/10/04 00:58:26 Yeah. But in that way the message will be too long
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. https://crbug.com /646083
mcasas 2016/10/03 22:18:09 nit: reflow this comment line to 80 chars wide plz
xianglu 2016/10/04 00:58:26 Done.
94 mojo::ScopedSharedBufferHandle sharedBufferHandle =
95 getSharedBufferHandle(img);
96 if (!sharedBufferHandle->is_valid()) {
97 resolver->reject(
98 DOMException::create(SyntaxError, "Failed to get sharedBufferHandle."));
99 return promise;
100 }
101
102 m_serviceRequests.add(resolver);
103 DCHECK(m_service.is_bound());
104 m_service->DetectFace(std::move(sharedBufferHandle), img->naturalWidth(),
105 img->naturalHeight(),
106 convertToBaseCallback(WTF::bind(
107 &FaceDetector::onDetectFace, wrapPersistent(this),
108 wrapPersistent(resolver))));
109 sharedBufferHandle.reset();
21 return promise; 110 return promise;
22 } 111 }
23 112
113 void FaceDetector::onDetectFace(
114 ScriptPromiseResolver* resolver,
115 mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
116 if (!m_serviceRequests.contains(resolver))
117 return;
118
119 HeapVector<Member<DOMRect>> detectedFaces;
120 for (const auto& boundingBox : faceDetectionResult->boundingBoxes) {
121 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
mcasas 2016/10/03 22:18:09 nit: consider adding here a contract on the boundi
xianglu 2016/10/04 00:58:26 Done.
122 boundingBox->width,
123 boundingBox->height));
124 }
125
126 resolver->resolve(detectedFaces);
127 m_serviceRequests.remove(resolver);
128 }
129
130 DEFINE_TRACE(FaceDetector) {
131 visitor->trace(m_serviceRequests);
132 }
133
24 } // namespace blink 134 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698