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

Unified Diff: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp

Issue 2557513003: ShapeDetection: Eliminate DetectorType enum in ShapeDetector.cpp (Closed)
Patch Set: Remove state info from parent class, add doDetect() pure virtual method. Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
diff --git a/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp b/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
index abfa2e3ddf937b308d08632890be87dd3294b2fc..a51226ee2e5724b5f3f8d76d8993f917b48b6409 100644
--- a/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
+++ b/third_party/WebKit/Source/modules/shapedetection/ShapeDetector.cpp
@@ -9,14 +9,11 @@
#include "core/dom/Document.h"
#include "core/fetch/ImageResource.h"
#include "core/frame/ImageBitmap.h"
-#include "core/frame/LocalDOMWindow.h"
#include "core/frame/LocalFrame.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/HTMLVideoElement.h"
#include "core/html/canvas/CanvasImageSource.h"
-#include "modules/shapedetection/DetectedBarcode.h"
#include "platform/graphics/Image.h"
-#include "public/platform/InterfaceProvider.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkImageInfo.h"
#include "wtf/CheckedNumeric.h"
@@ -44,34 +41,21 @@ static CanvasImageSource* toImageSourceInternal(
} // anonymous namespace
ShapeDetector::ShapeDetector(LocalFrame& frame) {
- DCHECK(!m_faceService.is_bound());
- DCHECK(!m_barcodeService.is_bound());
DCHECK(frame.interfaceProvider());
- frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_faceService));
- frame.interfaceProvider()->getInterface(mojo::GetProxy(&m_barcodeService));
- m_faceService.set_connection_error_handler(convertToBaseCallback(WTF::bind(
- &ShapeDetector::onFaceServiceConnectionError, wrapWeakPersistent(this))));
- m_barcodeService.set_connection_error_handler(convertToBaseCallback(
- WTF::bind(&ShapeDetector::onBarcodeServiceConnectionError,
- wrapWeakPersistent(this))));
}
-ShapeDetector::ShapeDetector(LocalFrame& frame,
- const FaceDetectorOptions& options)
- : ShapeDetector(frame) {
- m_faceDetectorOptions = mojom::blink::FaceDetectorOptions::New();
- m_faceDetectorOptions->max_detected_faces = options.maxDetectedFaces();
- m_faceDetectorOptions->fast_mode = options.fastMode();
+ScriptPromise ShapeDetector::detect(ScriptState* scriptState,
+ const CanvasImageSourceUnion& imageSource) {
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
+ return detectShapes(scriptState, resolver, imageSource);
}
ScriptPromise ShapeDetector::detectShapes(
ScriptState* scriptState,
- DetectorType detectorType,
+ ScriptPromiseResolver* resolver,
const CanvasImageSourceUnion& imageSource) {
- CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource);
-
- ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
+ CanvasImageSource* imageSourceInternal = toImageSourceInternal(imageSource);
if (!imageSourceInternal) {
// TODO(mcasas): Implement more CanvasImageSources, https://crbug.com/659138
@@ -90,17 +74,15 @@ ScriptPromise ShapeDetector::detectShapes(
if (imageSource.isHTMLImageElement()) {
return detectShapesOnImageElement(
- detectorType, resolver,
- static_cast<HTMLImageElement*>(imageSourceInternal));
+ resolver, static_cast<HTMLImageElement*>(imageSourceInternal));
Reilly Grant (use Gerrit) 2016/12/06 20:40:13 It seems to me that since this set of if statement
xianglu 2016/12/06 22:31:56 Removed duplicated logic. I think the only trade-o
}
if (imageSourceInternal->isImageBitmap()) {
return detectShapesOnImageBitmap(
- detectorType, resolver, static_cast<ImageBitmap*>(imageSourceInternal));
+ resolver, static_cast<ImageBitmap*>(imageSourceInternal));
}
if (imageSourceInternal->isVideoElement()) {
return detectShapesOnVideoElement(
- detectorType, resolver,
- static_cast<HTMLVideoElement*>(imageSourceInternal));
+ resolver, static_cast<HTMLVideoElement*>(imageSourceInternal));
}
NOTREACHED();
@@ -108,7 +90,6 @@ ScriptPromise ShapeDetector::detectShapes(
}
ScriptPromise ShapeDetector::detectShapesOnImageElement(
- DetectorType detectorType,
ScriptPromiseResolver* resolver,
const HTMLImageElement* img) {
ScriptPromise promise = resolver->promise();
@@ -170,40 +151,11 @@ ScriptPromise ShapeDetector::detectShapesOnImageElement(
return promise;
}
- if (detectorType == DetectorType::Face) {
- if (!m_faceService) {
- resolver->reject(DOMException::create(
- NotSupportedError, "Face detection service unavailable."));
- return promise;
- }
- m_faceServiceRequests.add(resolver);
- m_faceService->Detect(std::move(sharedBufferHandle), img->naturalWidth(),
- img->naturalHeight(), m_faceDetectorOptions.Clone(),
- convertToBaseCallback(WTF::bind(
- &ShapeDetector::onDetectFaces,
- wrapPersistent(this), wrapPersistent(resolver))));
- } else if (detectorType == DetectorType::Barcode) {
- if (!m_barcodeService) {
- resolver->reject(DOMException::create(
- NotSupportedError, "Barcode detection service unavailable."));
- return promise;
- }
- m_barcodeServiceRequests.add(resolver);
- m_barcodeService->Detect(
- std::move(sharedBufferHandle), img->naturalWidth(),
- img->naturalHeight(),
- convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
- wrapPersistent(this),
- wrapPersistent(resolver))));
- } else {
- NOTREACHED() << "Unsupported detector type";
- }
-
- return promise;
+ return doDetect(resolver, std::move(sharedBufferHandle), img->naturalWidth(),
+ img->naturalHeight());
}
ScriptPromise ShapeDetector::detectShapesOnImageBitmap(
- DetectorType detectorType,
ScriptPromiseResolver* resolver,
ImageBitmap* imageBitmap) {
ScriptPromise promise = resolver->promise();
@@ -237,17 +189,28 @@ ScriptPromise ShapeDetector::detectShapesOnImageBitmap(
allocationSize = imageBitmap->size().area() * 4 /* bytes per pixel */;
}
- return detectShapesOnData(detectorType, resolver, pixelDataPtr,
- allocationSize.ValueOrDefault(0),
- imageBitmap->width(), imageBitmap->height());
+ int size = allocationSize.ValueOrDefault(0);
+ mojo::ScopedSharedBufferHandle sharedBufferHandle =
+ mojo::SharedBufferHandle::Create(size);
+ if (!sharedBufferHandle->is_valid()) {
+ resolver->reject(
+ DOMException::create(InvalidStateError, "Internal allocation error"));
+ return promise;
+ }
+
+ const mojo::ScopedSharedBufferMapping mappedBuffer =
+ sharedBufferHandle->Map(size);
+ DCHECK(mappedBuffer.get());
+ memcpy(mappedBuffer.get(), pixelDataPtr, size);
+
+ return doDetect(resolver, std::move(sharedBufferHandle), imageBitmap->width(),
+ imageBitmap->height());
}
ScriptPromise ShapeDetector::detectShapesOnVideoElement(
- DetectorType detectorType,
ScriptPromiseResolver* resolver,
const HTMLVideoElement* video) {
ScriptPromise promise = resolver->promise();
-
// TODO(mcasas): Check if |video| is actually playing a MediaStream by using
// HTMLMediaElement::isMediaStreamURL(video->currentSrc().getString()); if
// there is a local WebCam associated, there might be sophisticated ways to
@@ -285,21 +248,7 @@ ScriptPromise ShapeDetector::detectShapesOnVideoElement(
return promise;
}
- return detectShapesOnData(detectorType, resolver, pixelDataPtr,
- allocationSize.ValueOrDefault(0), image->width(),
- image->height());
-}
-
-ScriptPromise ShapeDetector::detectShapesOnData(DetectorType detectorType,
- ScriptPromiseResolver* resolver,
- uint8_t* data,
- int size,
- int width,
- int height) {
- DCHECK(data);
- DCHECK(size);
- ScriptPromise promise = resolver->promise();
-
+ int size = allocationSize.ValueOrDefault(0);
mojo::ScopedSharedBufferHandle sharedBufferHandle =
mojo::SharedBufferHandle::Create(size);
if (!sharedBufferHandle->is_valid()) {
@@ -311,95 +260,10 @@ ScriptPromise ShapeDetector::detectShapesOnData(DetectorType detectorType,
const mojo::ScopedSharedBufferMapping mappedBuffer =
sharedBufferHandle->Map(size);
DCHECK(mappedBuffer.get());
+ memcpy(mappedBuffer.get(), pixelDataPtr, size);
- memcpy(mappedBuffer.get(), data, size);
-
- if (detectorType == DetectorType::Face) {
- if (!m_faceService) {
- resolver->reject(DOMException::create(
- NotSupportedError, "Face detection service unavailable."));
- return promise;
- }
- m_faceServiceRequests.add(resolver);
- m_faceService->Detect(std::move(sharedBufferHandle), width, height,
- m_faceDetectorOptions.Clone(),
- convertToBaseCallback(WTF::bind(
- &ShapeDetector::onDetectFaces,
- wrapPersistent(this), wrapPersistent(resolver))));
- } else if (detectorType == DetectorType::Barcode) {
- if (!m_barcodeService) {
- resolver->reject(DOMException::create(
- NotSupportedError, "Barcode detection service unavailable."));
- return promise;
- }
- m_barcodeServiceRequests.add(resolver);
- m_barcodeService->Detect(
- std::move(sharedBufferHandle), width, height,
- convertToBaseCallback(WTF::bind(&ShapeDetector::onDetectBarcodes,
- wrapPersistent(this),
- wrapPersistent(resolver))));
- } else {
- NOTREACHED() << "Unsupported detector type";
- }
- sharedBufferHandle.reset();
- return promise;
-}
-
-void ShapeDetector::onDetectFaces(
- ScriptPromiseResolver* resolver,
- mojom::blink::FaceDetectionResultPtr faceDetectionResult) {
- DCHECK(m_faceServiceRequests.contains(resolver));
- m_faceServiceRequests.remove(resolver);
-
- HeapVector<Member<DOMRect>> detectedFaces;
- for (const auto& boundingBox : faceDetectionResult->bounding_boxes) {
- detectedFaces.append(DOMRect::create(boundingBox->x, boundingBox->y,
- boundingBox->width,
- boundingBox->height));
- }
-
- resolver->resolve(detectedFaces);
-}
-
-void ShapeDetector::onDetectBarcodes(
- ScriptPromiseResolver* resolver,
- Vector<mojom::blink::BarcodeDetectionResultPtr> barcodeDetectionResults) {
- DCHECK(m_barcodeServiceRequests.contains(resolver));
- m_barcodeServiceRequests.remove(resolver);
-
- HeapVector<Member<DetectedBarcode>> detectedBarcodes;
- for (const auto& barcode : barcodeDetectionResults) {
- detectedBarcodes.append(DetectedBarcode::create(
- barcode->raw_value,
- DOMRect::create(barcode->bounding_box->x, barcode->bounding_box->y,
- barcode->bounding_box->width,
- barcode->bounding_box->height)));
- }
-
- resolver->resolve(detectedBarcodes);
-}
-
-void ShapeDetector::onFaceServiceConnectionError() {
- for (const auto& request : m_faceServiceRequests) {
- request->reject(DOMException::create(NotSupportedError,
- "Face Detection not implemented."));
- }
- m_faceServiceRequests.clear();
- m_faceService.reset();
-}
-
-void ShapeDetector::onBarcodeServiceConnectionError() {
- for (const auto& request : m_barcodeServiceRequests) {
- request->reject(DOMException::create(NotSupportedError,
- "Barcode Detection not implemented."));
- }
- m_barcodeServiceRequests.clear();
- m_barcodeService.reset();
-}
-
-DEFINE_TRACE(ShapeDetector) {
- visitor->trace(m_faceServiceRequests);
- visitor->trace(m_barcodeServiceRequests);
+ return doDetect(resolver, std::move(sharedBufferHandle), image->width(),
+ image->height());
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698