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 { |
21 | |
22 mojo::ScopedSharedBufferHandle getSharedBufferHandle(const HTMLImageElement* img ) | |
13 { | 23 { |
14 return new FaceDetector(); | 24 ImageResource* const imgResource = img->cachedImage(); |
25 if (!imgResource) { | |
26 DLOG(ERROR) << "Failed to convert HTMLImageElement to ImageSource."; | |
27 return mojo::ScopedSharedBufferHandle(); | |
28 } | |
29 | |
30 Image* const blinkImage = imgResource->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> skiaImg = blinkImage->imageForCurrentFrame(); | |
37 DCHECK_EQ(img->naturalWidth(), skiaImg->width()); | |
38 DCHECK_EQ(img->naturalHeight(), skiaImg->height()); | |
39 | |
40 if (!skiaImg) { | |
41 DLOG(ERROR) << "Failed to convert blink::Image to sk_sp<SkImage>."; | |
42 return mojo::ScopedSharedBufferHandle(); | |
43 } | |
44 | |
45 const SkImageInfo skiaInfo = SkImageInfo::MakeN32(skiaImg->width(), skiaImg- >height(), skiaImg->alphaType()); | |
46 | |
47 const uint32_t allocationSize = skiaInfo.getSafeSize(skiaInfo.minRowBytes()) ; | |
48 mojo::ScopedSharedBufferHandle sharedBufferHandle = mojo::SharedBufferHandle ::Create(allocationSize); | |
49 const mojo::ScopedSharedBufferMapping mappingPtr = sharedBufferHandle->Map(a llocationSize); | |
mcasas
2016/10/03 21:02:46
Naming: don't use abbreviations like Img for
img,
xianglu
2016/10/03 22:00:06
Done.
| |
50 DCHECK(mappingPtr); | |
51 | |
52 const SkPixmap pixmap(skiaInfo, mappingPtr.get(), skiaInfo.minRowBytes()); | |
53 if (!skiaImg->readPixels(pixmap, 0, 0)) { | |
54 DLOG(ERROR) << "Failed to read pixels from sk_sp<SkImage>."; | |
55 return mojo::ScopedSharedBufferHandle(); | |
56 } | |
57 | |
58 return sharedBufferHandle; | |
15 } | 59 } |
16 | 60 |
17 ScriptPromise FaceDetector::detect(ScriptState* scriptState, const HTMLImageElem ent* image) | 61 } // anonymous namespace |
62 | |
63 FaceDetector* FaceDetector::create(const Document& document) | |
64 { | |
65 return new FaceDetector(*document.frame()); | |
66 } | |
67 | |
68 FaceDetector::FaceDetector(LocalFrame& frame) | |
69 { | |
70 DCHECK(!m_service.is_bound()); | |
71 DCHECK(frame.interfaceProvider()); | |
72 frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_service)); | |
73 } | |
74 | |
75 ScriptPromise FaceDetector::detect(ScriptState* scriptState, const HTMLImageElem ent* img) | |
18 { | 76 { |
19 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | 77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; |
20 ScriptPromise promise = resolver->promise(); | 78 ScriptPromise promise = resolver->promise(); |
21 resolver->reject(DOMException::create(NotSupportedError, "Not implemented")) ; | 79 |
80 if (!m_service) { | |
81 resolver->reject(DOMException::create(NotFoundError, "Face detection ser vice unavailable.")); | |
82 return promise; | |
83 } | |
84 | |
85 if (!img) { | |
86 resolver->reject(DOMException::create(SyntaxError, "The provided HTMLIma geElement is empty.")); | |
87 return promise; | |
88 } | |
89 | |
90 // TODO(xianglu): Add security check when the spec is ready. https://crbug.c om/646083 | |
91 mojo::ScopedSharedBufferHandle sharedBufferHandle = getSharedBufferHandle(im g); | |
92 if (!sharedBufferHandle->is_valid()) { | |
93 resolver->reject(DOMException::create(SyntaxError, "Failed to get shared BufferHandle.")); | |
94 return promise; | |
95 } | |
96 | |
97 m_serviceRequests.add(resolver); | |
98 DCHECK(m_service.is_bound()); | |
99 m_service->DetectFace( | |
100 std::move(sharedBufferHandle), | |
101 img->naturalWidth(), | |
102 img->naturalHeight(), | |
103 convertToBaseCallback(WTF::bind(&FaceDetector::onDetectFace, wrapPersist ent(this), wrapPersistent(resolver))) | |
104 ); | |
105 sharedBufferHandle.reset(); | |
22 return promise; | 106 return promise; |
23 } | 107 } |
24 | 108 |
109 | |
110 void FaceDetector::onDetectFace(ScriptPromiseResolver* resolver, mojom::blink::F aceDetectionResultPtr faceDetectionResult) | |
111 { | |
112 if (!m_serviceRequests.contains(resolver)) | |
113 return; | |
114 | |
115 HeapVector<Member<DOMRect>> detectedFaces; | |
116 for (const auto& boundingBox : faceDetectionResult->boundingBoxes) { | |
117 detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y, | |
118 boundingBox->width, boundingBox->height)); | |
mcasas
2016/10/03 21:02:46
These two lines need clang-formatting.
Also, afte
xianglu
2016/10/03 22:00:06
Done.
| |
119 } | |
120 | |
121 resolver->resolve(detectedFaces); | |
122 m_serviceRequests.remove(resolver); | |
123 } | |
124 | |
125 DEFINE_TRACE(FaceDetector) | |
126 { | |
127 visitor->trace(m_serviceRequests); | |
128 } | |
129 | |
25 } // namespace blink | 130 } // namespace blink |
OLD | NEW |