Commit 0202bfd3 authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

Simplify use of CrossThreadPersistent in ImageBitmap::RasterizeImageOnBackgroundThread.

This captures all of the dispatching (main) thread objects in a reply callback,
reducing the risk of the background thread accessing it in an unsafe way.

As it happens this is fine today, mostly because the main thread heap cannot be
torn down, but it's actually shorter this way, too.)

Change-Id: I33021cf19d82f704c61a8f08bc89d5f7595c2aec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1799278
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Commit-Queue: Kentaro Hara <haraken@chromium.org>
Auto-Submit: Jeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#695926}
parent cc3303b3
......@@ -911,9 +911,9 @@ ImageBitmap* ImageBitmap::Create(const void* pixel_data,
void ImageBitmap::ResolvePromiseOnOriginalThread(
ScriptPromiseResolver* resolver,
sk_sp<SkImage> skia_image,
bool origin_clean,
std::unique_ptr<ParsedOptions> parsed_options) {
std::unique_ptr<ParsedOptions> parsed_options,
sk_sp<SkImage> skia_image) {
if (!skia_image) {
resolver->Reject(
ScriptValue(resolver->GetScriptState(),
......@@ -945,11 +945,10 @@ void ImageBitmap::ResolvePromiseOnOriginalThread(
}
void ImageBitmap::RasterizeImageOnBackgroundThread(
ScriptPromiseResolver* resolver,
sk_sp<PaintRecord> paint_record,
const IntRect& dst_rect,
bool origin_clean,
std::unique_ptr<ParsedOptions> parsed_options) {
scoped_refptr<base::SequencedTaskRunner> task_runner,
WTF::CrossThreadOnceFunction<void(sk_sp<SkImage>)> callback) {
DCHECK(!IsMainThread());
SkImageInfo info =
SkImageInfo::MakeN32Premul(dst_rect.Width(), dst_rect.Height());
......@@ -959,14 +958,9 @@ void ImageBitmap::RasterizeImageOnBackgroundThread(
paint_record->Playback(surface->getCanvas());
skia_image = surface->makeImageSnapshot();
}
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
Thread::MainThread()->GetTaskRunner();
PostCrossThreadTask(
*task_runner, FROM_HERE,
CrossThreadBindOnce(&ResolvePromiseOnOriginalThread,
WrapCrossThreadPersistent(resolver),
std::move(skia_image), origin_clean,
WTF::Passed(std::move(parsed_options))));
CrossThreadBindOnce(std::move(callback), std::move(skia_image)));
}
ScriptPromise ImageBitmap::CreateAsync(ImageElementBase* image,
......@@ -1017,11 +1011,13 @@ ScriptPromise ImageBitmap::CreateAsync(ImageElementBase* image,
std::make_unique<ParsedOptions>(parsed_options);
worker_pool::PostTask(
FROM_HERE,
CrossThreadBindOnce(&RasterizeImageOnBackgroundThread,
WrapCrossThreadPersistent(resolver),
std::move(paint_record), draw_dst_rect,
!image->WouldTaintOrigin(),
WTF::Passed(std::move(passed_parsed_options))));
CrossThreadBindOnce(
&RasterizeImageOnBackgroundThread, std::move(paint_record),
draw_dst_rect, Thread::MainThread()->GetTaskRunner(),
CrossThreadBindOnce(&ResolvePromiseOnOriginalThread,
WrapCrossThreadPersistent(resolver),
!image->WouldTaintOrigin(),
WTF::Passed(std::move(passed_parsed_options)))));
return promise;
}
......
......@@ -7,6 +7,7 @@
#include <memory>
#include "base/memory/scoped_refptr.h"
#include "base/sequenced_task_runner.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_image_source.h"
#include "third_party/blink/renderer/core/html/canvas/image_element_base.h"
......@@ -17,6 +18,7 @@
#include "third_party/blink/renderer/platform/graphics/image.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/skia/include/core/SkRefCnt.h"
namespace blink {
......@@ -172,14 +174,14 @@ class CORE_EXPORT ImageBitmap final : public ScriptWrappable,
private:
void UpdateImageBitmapMemoryUsage();
static void ResolvePromiseOnOriginalThread(ScriptPromiseResolver*,
sk_sp<SkImage>,
bool origin_clean,
std::unique_ptr<ParsedOptions>);
static void RasterizeImageOnBackgroundThread(ScriptPromiseResolver*,
sk_sp<PaintRecord>,
const IntRect&,
bool origin_clean,
std::unique_ptr<ParsedOptions>);
std::unique_ptr<ParsedOptions>,
sk_sp<SkImage>);
static void RasterizeImageOnBackgroundThread(
sk_sp<PaintRecord>,
const IntRect&,
scoped_refptr<base::SequencedTaskRunner>,
WTF::CrossThreadOnceFunction<void(sk_sp<SkImage>)> callback);
scoped_refptr<StaticBitmapImage> image_;
bool is_neutered_ = false;
int32_t memory_usage_ = 0;
......
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