Commit 6ccb0369 authored by junweifu's avatar junweifu Committed by Commit Bot

ShapeDetection: Move the implementation of AsyncOperation to the .cc file

It's better to put the class definition in a header file and the implementation
in a C++ source file. Then, the source file is made part of the project, meaning
it is compiled separately.

BUG=791371

Cq-Include-Trybots: luci.chromium.try:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win10_chromium_x64_rel_ng
Change-Id: Ic58d0c714b405ced8974aca22c3b20f7b479682f
Reviewed-on: https://chromium-review.googlesource.com/982751Reviewed-by: default avatarMiguel Casas <mcasas@chromium.org>
Commit-Queue: Junwei Fu <junwei.fu@intel.com>
Cr-Commit-Position: refs/heads/master@{#547078}
parent c68101a5
......@@ -4,12 +4,80 @@
#include "services/shape_detection/detection_utils_win.h"
#include <windows.foundation.collections.h>
#include <windows.media.faceanalysis.h>
#include <windows.media.ocr.h>
#include <wrl/implements.h>
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/numerics/checked_math.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/win/winrt_storage_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace shape_detection {
using ABI::Windows::Foundation::Collections::IVector;
using ABI::Windows::Foundation::IAsyncOperationCompletedHandler;
using ABI::Windows::Media::FaceAnalysis::DetectedFace;
using ABI::Windows::Media::Ocr::OcrResult;
using ABI::Windows::Media::FaceAnalysis::FaceDetector;
template <typename RuntimeType>
HRESULT AsyncOperation<RuntimeType>::BeginAsyncOperation(
typename AsyncOperation<RuntimeType>::Callback callback,
typename AsyncOperation<RuntimeType>::IAsyncOperationPtr async_op_ptr) {
auto instance =
new AsyncOperation<RuntimeType>(std::move(callback), async_op_ptr);
scoped_refptr<base::SequencedTaskRunner> task_runner =
base::SequencedTaskRunnerHandle::Get();
typedef WRL::Implements<WRL::RuntimeClassFlags<WRL::ClassicCom>,
IAsyncOperationCompletedHandler<RuntimeType*>,
WRL::FtmBase>
AsyncCallback;
auto async_callback = WRL::Callback<AsyncCallback>(
[instance, task_runner](IAsyncOperation<RuntimeType*>* async_op,
AsyncStatus status) {
// A reference to |async_op| is kept in |async_op_ptr_|, safe to pass
// outside. This is happening on an OS thread.
task_runner->PostTask(
FROM_HERE, base::BindOnce(&AsyncOperation::AsyncCallbackInternal,
base::Owned(instance),
base::Unretained(async_op), status));
return S_OK;
});
return async_op_ptr->put_Completed(async_callback.Get());
}
template HRESULT AsyncOperation<IVector<DetectedFace*>>::BeginAsyncOperation(
AsyncOperation<IVector<DetectedFace*>>::Callback callback,
AsyncOperation<IVector<DetectedFace*>>::IAsyncOperationPtr async_op_ptr);
template HRESULT AsyncOperation<FaceDetector>::BeginAsyncOperation(
AsyncOperation<FaceDetector>::Callback callback,
AsyncOperation<FaceDetector>::IAsyncOperationPtr async_op_ptr);
template HRESULT AsyncOperation<OcrResult>::BeginAsyncOperation(
AsyncOperation<OcrResult>::Callback callback,
AsyncOperation<OcrResult>::IAsyncOperationPtr async_op_ptr);
template <typename RuntimeType>
void AsyncOperation<RuntimeType>::AsyncCallbackInternal(
IAsyncOperation<RuntimeType*>* async_op,
AsyncStatus status) {
DCHECK_EQ(async_op, async_op_ptr_.Get());
std::move(callback_).Run((async_op && status == AsyncStatus::Completed)
? std::move(async_op_ptr_)
: nullptr);
}
WRL::ComPtr<ISoftwareBitmap> CreateWinBitmapFromSkBitmap(
const SkBitmap& bitmap,
ISoftwareBitmapStatics* bitmap_factory) {
......
......@@ -8,14 +8,9 @@
#include <windows.storage.streams.h>
#include <wrl/client.h>
#include <wrl/event.h>
#include <wrl/implements.h>
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
class SkBitmap;
......@@ -24,7 +19,6 @@ namespace shape_detection {
namespace WRL = Microsoft::WRL;
using ABI::Windows::Foundation::IAsyncOperation;
using ABI::Windows::Foundation::IAsyncOperationCompletedHandler;
using ABI::Windows::Graphics::Imaging::ISoftwareBitmapStatics;
using ABI::Windows::Graphics::Imaging::ISoftwareBitmap;
using ABI::Windows::Graphics::Imaging::BitmapPixelFormat;
......@@ -46,32 +40,7 @@ class AsyncOperation {
// Creates an AsyncOperation instance which sets |callback| to be called when
// the asynchronous action completes.
static HRESULT BeginAsyncOperation(Callback callback,
IAsyncOperationPtr async_op_ptr) {
auto instance =
new AsyncOperation<RuntimeType>(std::move(callback), async_op_ptr);
scoped_refptr<base::SequencedTaskRunner> task_runner =
base::SequencedTaskRunnerHandle::Get();
typedef WRL::Implements<WRL::RuntimeClassFlags<WRL::ClassicCom>,
IAsyncOperationCompletedHandler<RuntimeType*>,
WRL::FtmBase>
AsyncCallback;
auto async_callback = WRL::Callback<AsyncCallback>(
[instance, task_runner](IAsyncOperation<RuntimeType*>* async_op,
AsyncStatus status) {
// A reference to |async_op| is kept in |async_op_ptr_|, safe to pass
// outside. This is happening on an OS thread.
task_runner->PostTask(
FROM_HERE, base::BindOnce(&AsyncOperation::AsyncCallbackInternal,
base::Owned(instance),
base::Unretained(async_op), status));
return S_OK;
});
return async_op_ptr->put_Completed(async_callback.Get());
}
IAsyncOperationPtr async_op_ptr);
private:
AsyncOperation(Callback callback, IAsyncOperationPtr async_op_ptr)
......@@ -79,13 +48,7 @@ class AsyncOperation {
callback_(std::move(callback)) {}
void AsyncCallbackInternal(IAsyncOperation<RuntimeType*>* async_op,
AsyncStatus status) {
DCHECK_EQ(async_op, async_op_ptr_.Get());
std::move(callback_).Run((async_op && status == AsyncStatus::Completed)
? std::move(async_op_ptr_)
: nullptr);
}
AsyncStatus status);
IAsyncOperationPtr async_op_ptr_;
Callback callback_;
......
......@@ -4,8 +4,6 @@
#include "services/shape_detection/face_detection_impl_win.h"
#include <windows.media.faceanalysis.h>
#include "base/bind.h"
#include "base/logging.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment