Add conversion from pp::ImageData to SkBitmap
Adds a new function, SkBitmapFromPPImageData(), that converts from pp::ImageData to SkBitmap. SkBitmap can then be used to replace any instances of pp::ImageData. Note that the underlying pixel memory is shared, so these conversions are relatively cheap. As an example, converts chrome_pdf::draw_utils::DrawShadow() to accept an SkBitmap& instead of a pp::ImageData*. Bug: 1099020 Change-Id: Ib5ed84fe45f2a4ac882ba6b367a02398ba256c5f Cq-Do-Not-Cancel-Tryjobs: true Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2270813 Reviewed-by: Dirk Pranke <dpranke@google.com> Reviewed-by: Mike Klein <mtklein@google.com> Reviewed-by: Daniel Hosseinian <dhoss@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: K. Moon <kmoon@chromium.org> Cr-Commit-Position: refs/heads/master@{#787942}
This commit is contained in:
@ -57,6 +57,8 @@ if (enable_pdf) {
|
|||||||
"pdf_ppapi.cc",
|
"pdf_ppapi.cc",
|
||||||
"pdf_transform.cc",
|
"pdf_transform.cc",
|
||||||
"pdf_transform.h",
|
"pdf_transform.h",
|
||||||
|
"ppapi_migration/bitmap.cc",
|
||||||
|
"ppapi_migration/bitmap.h",
|
||||||
"ppapi_migration/callback.cc",
|
"ppapi_migration/callback.cc",
|
||||||
"ppapi_migration/callback.h",
|
"ppapi_migration/callback.h",
|
||||||
"ppapi_migration/geometry_conversions.cc",
|
"ppapi_migration/geometry_conversions.cc",
|
||||||
@ -91,6 +93,7 @@ if (enable_pdf) {
|
|||||||
"//net",
|
"//net",
|
||||||
"//ppapi/cpp:objects",
|
"//ppapi/cpp:objects",
|
||||||
"//ppapi/cpp/private:internal_module",
|
"//ppapi/cpp/private:internal_module",
|
||||||
|
"//skia",
|
||||||
"//ui/base",
|
"//ui/base",
|
||||||
"//ui/gfx/range",
|
"//ui/gfx/range",
|
||||||
]
|
]
|
||||||
|
1
pdf/DEPS
1
pdf/DEPS
@ -2,6 +2,7 @@ include_rules = [
|
|||||||
"+net",
|
"+net",
|
||||||
"+ppapi",
|
"+ppapi",
|
||||||
"+printing/units.h",
|
"+printing/units.h",
|
||||||
|
"+third_party/skia/include/core",
|
||||||
"+ui/base/window_open_disposition.h",
|
"+ui/base/window_open_disposition.h",
|
||||||
"+ui/events/keycodes/keyboard_codes.h",
|
"+ui/events/keycodes/keyboard_codes.h",
|
||||||
"+ui/gfx/geometry",
|
"+ui/gfx/geometry",
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include "base/check_op.h"
|
#include "base/check_op.h"
|
||||||
#include "ppapi/cpp/image_data.h"
|
|
||||||
#include "ppapi/cpp/rect.h"
|
#include "ppapi/cpp/rect.h"
|
||||||
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
|
|
||||||
namespace chrome_pdf {
|
namespace chrome_pdf {
|
||||||
namespace draw_utils {
|
namespace draw_utils {
|
||||||
@ -110,7 +110,7 @@ ShadowMatrix::~ShadowMatrix() = default;
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void PaintShadow(pp::ImageData* image,
|
void PaintShadow(SkBitmap& image,
|
||||||
const pp::Rect& clip_rc,
|
const pp::Rect& clip_rc,
|
||||||
const pp::Rect& shadow_rc,
|
const pp::Rect& shadow_rc,
|
||||||
const ShadowMatrix& matrix) {
|
const ShadowMatrix& matrix) {
|
||||||
@ -125,7 +125,7 @@ void PaintShadow(pp::ImageData* image,
|
|||||||
depth - shadow_rc.right() + x);
|
depth - shadow_rc.right() + x);
|
||||||
int32_t matrix_y = std::max(depth + shadow_rc.y() - y - 1,
|
int32_t matrix_y = std::max(depth + shadow_rc.y() - y - 1,
|
||||||
depth - shadow_rc.bottom() + y);
|
depth - shadow_rc.bottom() + y);
|
||||||
uint32_t* pixel = image->GetAddr32(pp::Point(x, y));
|
uint32_t* pixel = image.getAddr32(x, y);
|
||||||
|
|
||||||
if (matrix_x < 0)
|
if (matrix_x < 0)
|
||||||
matrix_x = 0;
|
matrix_x = 0;
|
||||||
@ -144,7 +144,7 @@ void PaintShadow(pp::ImageData* image,
|
|||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void DrawShadow(pp::ImageData* image,
|
void DrawShadow(SkBitmap& image,
|
||||||
const pp::Rect& shadow_rc,
|
const pp::Rect& shadow_rc,
|
||||||
const pp::Rect& object_rc,
|
const pp::Rect& object_rc,
|
||||||
const pp::Rect& clip_rc,
|
const pp::Rect& clip_rc,
|
||||||
|
@ -9,8 +9,9 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
class SkBitmap;
|
||||||
|
|
||||||
namespace pp {
|
namespace pp {
|
||||||
class ImageData;
|
|
||||||
class Rect;
|
class Rect;
|
||||||
} // namespace pp
|
} // namespace pp
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ class ShadowMatrix {
|
|||||||
// shadow_rc - rectangle occupied by shadow
|
// shadow_rc - rectangle occupied by shadow
|
||||||
// object_rc - rectangle that drops the shadow
|
// object_rc - rectangle that drops the shadow
|
||||||
// clip_rc - clipping region
|
// clip_rc - clipping region
|
||||||
void DrawShadow(pp::ImageData* image,
|
void DrawShadow(SkBitmap& image,
|
||||||
const pp::Rect& shadow_rc,
|
const pp::Rect& shadow_rc,
|
||||||
const pp::Rect& object_rc,
|
const pp::Rect& object_rc,
|
||||||
const pp::Rect& clip_rc,
|
const pp::Rect& clip_rc,
|
||||||
|
@ -41,6 +41,7 @@
|
|||||||
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
|
#include "pdf/pdfium/pdfium_mem_buffer_file_write.h"
|
||||||
#include "pdf/pdfium/pdfium_permissions.h"
|
#include "pdf/pdfium/pdfium_permissions.h"
|
||||||
#include "pdf/pdfium/pdfium_unsupported_features.h"
|
#include "pdf/pdfium/pdfium_unsupported_features.h"
|
||||||
|
#include "pdf/ppapi_migration/bitmap.h"
|
||||||
#include "pdf/ppapi_migration/input_event_conversions.h"
|
#include "pdf/ppapi_migration/input_event_conversions.h"
|
||||||
#include "pdf/url_loader_wrapper_impl.h"
|
#include "pdf/url_loader_wrapper_impl.h"
|
||||||
#include "ppapi/c/ppb_input_event.h"
|
#include "ppapi/c/ppb_input_event.h"
|
||||||
@ -56,6 +57,7 @@
|
|||||||
#include "third_party/pdfium/public/fpdf_fwlevent.h"
|
#include "third_party/pdfium/public/fpdf_fwlevent.h"
|
||||||
#include "third_party/pdfium/public/fpdf_ppo.h"
|
#include "third_party/pdfium/public/fpdf_ppo.h"
|
||||||
#include "third_party/pdfium/public/fpdf_searchex.h"
|
#include "third_party/pdfium/public/fpdf_searchex.h"
|
||||||
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
#include "ui/events/keycodes/keyboard_codes.h"
|
#include "ui/events/keycodes/keyboard_codes.h"
|
||||||
#include "ui/gfx/geometry/rect.h"
|
#include "ui/gfx/geometry/rect.h"
|
||||||
#include "v8/include/v8.h"
|
#include "v8/include/v8.h"
|
||||||
@ -3501,8 +3503,12 @@ void PDFiumEngine::DrawPageShadow(const pp::Rect& page_rc,
|
|||||||
depth, factor, client_->GetBackgroundColor());
|
depth, factor, client_->GetBackgroundColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(crbug.com/1099020): Converting to SkBitmap here may seem silly, but
|
||||||
|
// this is part of a migration from pp::ImageData to SkBitmap in general.
|
||||||
DCHECK(!image_data->is_null());
|
DCHECK(!image_data->is_null());
|
||||||
DrawShadow(image_data, shadow_rect, page_rect, clip_rect, *page_shadow_);
|
SkBitmap bitmap =
|
||||||
|
SkBitmapFromPPImageData(std::make_unique<pp::ImageData>(*image_data));
|
||||||
|
DrawShadow(bitmap, shadow_rect, page_rect, clip_rect, *page_shadow_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PDFiumEngine::GetRegion(const pp::Point& location,
|
void PDFiumEngine::GetRegion(const pp::Point& location,
|
||||||
|
51
pdf/ppapi_migration/bitmap.cc
Normal file
51
pdf/ppapi_migration/bitmap.cc
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// 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/bitmap.h"
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "base/check_op.h"
|
||||||
|
#include "ppapi/cpp/image_data.h"
|
||||||
|
#include "third_party/skia/include/core/SkBitmap.h"
|
||||||
|
#include "third_party/skia/include/core/SkImageInfo.h"
|
||||||
|
|
||||||
|
namespace chrome_pdf {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// Releases pp::ImageData associated with the SkPixelRef. The pp::ImageData acts
|
||||||
|
// like a shared pointer to memory provided by Pepper, and must be retained for
|
||||||
|
// the life of the SkPixelRef.
|
||||||
|
void ReleaseImageData(void* addr, void* context) {
|
||||||
|
pp::ImageData* image_data = static_cast<pp::ImageData*>(context);
|
||||||
|
DCHECK_EQ(addr, image_data->data());
|
||||||
|
delete image_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
SkBitmap SkBitmapFromPPImageData(std::unique_ptr<pp::ImageData> image_data) {
|
||||||
|
if (image_data->is_null()) {
|
||||||
|
return SkBitmap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note that we unconditionally use BGRA_PREMUL with PDFium.
|
||||||
|
DCHECK_EQ(image_data->format(), PP_IMAGEDATAFORMAT_BGRA_PREMUL);
|
||||||
|
SkImageInfo info =
|
||||||
|
SkImageInfo::Make(image_data->size().width(), image_data->size().height(),
|
||||||
|
kBGRA_8888_SkColorType, kPremul_SkAlphaType);
|
||||||
|
void* data = image_data->data();
|
||||||
|
int32_t stride = image_data->stride();
|
||||||
|
|
||||||
|
SkBitmap bitmap;
|
||||||
|
bool success = bitmap.installPixels(info, data, stride, ReleaseImageData,
|
||||||
|
image_data.release());
|
||||||
|
DCHECK(success);
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace chrome_pdf
|
30
pdf/ppapi_migration/bitmap.h
Normal file
30
pdf/ppapi_migration/bitmap.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// 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.
|
||||||
|
|
||||||
|
#ifndef PDF_PPAPI_MIGRATION_BITMAP_H_
|
||||||
|
#define PDF_PPAPI_MIGRATION_BITMAP_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class SkBitmap;
|
||||||
|
|
||||||
|
namespace pp {
|
||||||
|
class ImageData;
|
||||||
|
} // namespace pp
|
||||||
|
|
||||||
|
namespace chrome_pdf {
|
||||||
|
|
||||||
|
// Creates an SkBitmap from a pp::ImageData. The SkBitmap takes ownership of the
|
||||||
|
// pp::ImageData, and shares ownership of the underlying pixel memory. (Note
|
||||||
|
// that it's easy to make a shallow copy of a pp::ImageData.)
|
||||||
|
//
|
||||||
|
// In case of an error, returns an empty SkBitmap.
|
||||||
|
//
|
||||||
|
// TODO(kmoon): Skia is trying to get rid of SkBitmap in favor of immutable
|
||||||
|
// types like SkImage, so we should migrate once PDFium is ready for Skia.
|
||||||
|
SkBitmap SkBitmapFromPPImageData(std::unique_ptr<pp::ImageData> image_data);
|
||||||
|
|
||||||
|
} // namespace chrome_pdf
|
||||||
|
|
||||||
|
#endif // PDF_PPAPI_MIGRATION_BITMAP_H_
|
Reference in New Issue
Block a user