Commit 2c27fdf4 authored by Hui Yingst's avatar Hui Yingst Committed by Chromium LUCI CQ

Create a non-Pepper graphic interface for the PDF plugin.

Adds a new Pepper-free graphic interface SkiaGraphics, which is backed
by an SkSurface, and accepts SkBitmap as input.

SkiaGraphics interface will be used for the non-Pepper PDF plugin.

Bug: 1099020
Change-Id: Ib8b6f9173fd70102a86358b0dfe640a0e516a7de
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2559537
Commit-Queue: Hui Yingst <nigi@chromium.org>
Reviewed-by: default avatardanakj <danakj@chromium.org>
Reviewed-by: default avatarK. Moon <kmoon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837228}
parent 27284e57
...@@ -329,6 +329,7 @@ if (enable_pdf) { ...@@ -329,6 +329,7 @@ if (enable_pdf) {
"pdfium/pdfium_test_base.cc", "pdfium/pdfium_test_base.cc",
"pdfium/pdfium_test_base.h", "pdfium/pdfium_test_base.h",
"ppapi_migration/geometry_conversions_unittest.cc", "ppapi_migration/geometry_conversions_unittest.cc",
"ppapi_migration/graphics_unittest.cc",
"ppapi_migration/url_loader_unittest.cc", "ppapi_migration/url_loader_unittest.cc",
"range_set_unittest.cc", "range_set_unittest.cc",
"test/run_all_unittests.cc", "test/run_all_unittests.cc",
......
...@@ -10,6 +10,13 @@ include_rules = [ ...@@ -10,6 +10,13 @@ include_rules = [
"+ui/events/keycodes/keyboard_codes.h", "+ui/events/keycodes/keyboard_codes.h",
"+ui/gfx/geometry", "+ui/gfx/geometry",
"+ui/gfx/range/range.h", "+ui/gfx/range/range.h",
"+ui/gfx/skia_util.h",
"+ui/gfx/test/gfx_util.h", "+ui/gfx/test/gfx_util.h",
"+v8/include/v8.h" "+v8/include/v8.h"
] ]
specific_include_rules = {
".*_unittest.*\.cc": [
"+cc/test",
],
}
include_rules = [ include_rules = [
"+cc/test/pixel_comparator.h",
"+cc/test/pixel_test_utils.h",
"+gin/array_buffer.h", "+gin/array_buffer.h",
"+gin/public", "+gin/public",
"+gin/v8_initializer.h", "+gin/v8_initializer.h",
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/check_op.h" #include "base/check_op.h"
#include "base/memory/ptr_util.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "pdf/ppapi_migration/callback.h" #include "pdf/ppapi_migration/callback.h"
#include "pdf/ppapi_migration/geometry_conversions.h" #include "pdf/ppapi_migration/geometry_conversions.h"
#include "pdf/ppapi_migration/image.h" #include "pdf/ppapi_migration/image.h"
...@@ -18,8 +20,11 @@ ...@@ -18,8 +20,11 @@
#include "ppapi/cpp/instance_handle.h" #include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/point.h" #include "ppapi/cpp/point.h"
#include "ppapi/cpp/rect.h" #include "ppapi/cpp/rect.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h" #include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/skia_util.h"
namespace chrome_pdf { namespace chrome_pdf {
...@@ -73,4 +78,50 @@ void PepperGraphics::SetLayerTransform(float scale, ...@@ -73,4 +78,50 @@ void PepperGraphics::SetLayerTransform(float scale,
DCHECK(result); DCHECK(result);
} }
// static
std::unique_ptr<SkiaGraphics> SkiaGraphics::Create(const gfx::Size& size) {
auto graphics = base::WrapUnique(new SkiaGraphics(size));
if (!graphics->skia_graphics_)
return nullptr;
return graphics;
}
SkiaGraphics::SkiaGraphics(const gfx::Size& size)
: Graphics(size),
skia_graphics_(
SkSurface::MakeRasterN32Premul(size.width(), size.height())) {}
SkiaGraphics::~SkiaGraphics() = default;
bool SkiaGraphics::Flush(ResultCallback callback) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), 0));
return true;
}
void SkiaGraphics::PaintImage(const Image& image, const gfx::Rect& src_rect) {
SkRect skia_rect = RectToSkRect(src_rect);
skia_graphics_->getCanvas()->drawBitmapRect(image.skia_image(), skia_rect,
skia_rect, nullptr);
}
void SkiaGraphics::Scroll(const gfx::Rect& clip, const gfx::Vector2d& amount) {
NOTIMPLEMENTED_LOG_ONCE();
}
void SkiaGraphics::SetScale(float scale) {
NOTIMPLEMENTED_LOG_ONCE();
}
void SkiaGraphics::SetLayerTransform(float scale,
const gfx::Point& origin,
const gfx::Vector2d& translate) {
NOTIMPLEMENTED_LOG_ONCE();
}
sk_sp<SkImage> SkiaGraphics::CreateSnapshot() {
return skia_graphics_->makeImageSnapshot();
}
} // namespace chrome_pdf } // namespace chrome_pdf
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "pdf/ppapi_migration/callback.h" #include "pdf/ppapi_migration/callback.h"
#include "ppapi/cpp/graphics_2d.h" #include "ppapi/cpp/graphics_2d.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
namespace gfx { namespace gfx {
...@@ -80,6 +81,31 @@ class PepperGraphics final : public Graphics { ...@@ -80,6 +81,31 @@ class PepperGraphics final : public Graphics {
pp::Graphics2D pepper_graphics_; pp::Graphics2D pepper_graphics_;
}; };
// A Skia graphics device.
class SkiaGraphics final : public Graphics {
public:
static std::unique_ptr<SkiaGraphics> Create(const gfx::Size& size);
~SkiaGraphics() override;
bool Flush(ResultCallback callback) override;
void PaintImage(const Image& image, const gfx::Rect& src_rect) override;
void Scroll(const gfx::Rect& clip, const gfx::Vector2d& amount) override;
void SetScale(float scale) override;
void SetLayerTransform(float scale,
const gfx::Point& origin,
const gfx::Vector2d& translate) override;
sk_sp<SkImage> CreateSnapshot();
private:
explicit SkiaGraphics(const gfx::Size& size);
sk_sp<SkSurface> skia_graphics_;
};
} // namespace chrome_pdf } // namespace chrome_pdf
#endif // PDF_PPAPI_MIGRATION_GRAPHICS_H_ #endif // PDF_PPAPI_MIGRATION_GRAPHICS_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "pdf/ppapi_migration/graphics.h"
#include <utility>
#include "cc/test/pixel_comparator.h"
#include "cc/test/pixel_test_utils.h"
#include "pdf/ppapi_migration/image.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/skia_util.h"
namespace chrome_pdf {
namespace {
SkBitmap CreateN32PremulSkBitmap(const SkISize& size) {
SkBitmap bitmap;
bitmap.allocPixels(SkImageInfo::MakeN32Premul(size));
return bitmap;
}
Image CreateSourceImage(const SkISize& src_size) {
SkBitmap bitmap = CreateN32PremulSkBitmap(src_size);
bitmap.eraseColor(SK_ColorRED);
return Image(bitmap);
}
SkBitmap GenerateExpectedBitmap(const SkISize& graphics_size,
const SkIRect& rect) {
SkBitmap bitmap = CreateN32PremulSkBitmap(graphics_size);
bitmap.erase(SK_ColorRED, rect);
return bitmap;
}
void TestPaintImageResult(const SkISize& graphics_size,
const SkISize& src_size,
const gfx::Rect& paint_rect,
const SkIRect& overlapped_rect) {
auto graphics = SkiaGraphics::Create(gfx::SkISizeToSize(graphics_size));
ASSERT_TRUE(graphics);
// Create snapshots as SkImage and SkBitmap after painting.
graphics->PaintImage(CreateSourceImage(src_size), paint_rect);
sk_sp<SkImage> snapshot = graphics->CreateSnapshot();
SkBitmap snapshot_bitmap;
ASSERT_TRUE(snapshot->asLegacyBitmap(&snapshot_bitmap));
// Verify snapshot dimensions.
EXPECT_EQ(snapshot->dimensions(), graphics_size)
<< snapshot->width() << " x " << snapshot->height()
<< " != " << graphics_size.width() << " x " << graphics_size.height();
// Verify the snapshot matches the expected result.
const SkBitmap expected_bitmap =
GenerateExpectedBitmap(graphics_size, overlapped_rect);
EXPECT_TRUE(
cc::MatchesBitmap(snapshot_bitmap, expected_bitmap,
cc::ExactPixelComparator(/*discard_alpha=*/false)))
<< "SkBitmap comparison failed for graphics size of "
<< graphics_size.width() << " x " << graphics_size.height();
}
TEST(SkiaGraphicsTest, PaintImage) {
struct PaintImageParams {
// Size of the graphics to be painted on.
SkISize graphics_size;
// Size of the source image.
SkISize src_size;
// Painting area.
gfx::Rect paint_rect;
// Common area of the graphics, the source image and the painting area.
SkIRect overlapped_rect;
};
static constexpr PaintImageParams kPaintImageTestParams[] = {
// Paint area is within the graphics and the source image.
{{20, 20}, {15, 15}, gfx::Rect(0, 0, 10, 10), {0, 0, 10, 10}},
// Paint area is not completely within the graphics, or the source
// image.
{{50, 30}, {30, 50}, gfx::Rect(10, 10, 30, 30), {10, 10, 30, 30}},
// Paint area is outside the graphics.
{{10, 10}, {30, 30}, gfx::Rect(10, 10, 10, 10), {0, 0, 0, 0}},
// Paint area is outside the source image.
{{15, 15}, {5, 5}, gfx::Rect(10, 10, 5, 5), {0, 0, 0, 0}},
};
for (const auto& params : kPaintImageTestParams)
TestPaintImageResult(params.graphics_size, params.src_size,
params.paint_rect, params.overlapped_rect);
}
} // namespace
} // namespace chrome_pdf
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