0

Simplify PaintManager in pdf/

Various small changes to simplify the copy of PaintManager in pdf/:

1. Unify PaintAggregator::ReadyRect and PaintManager::ReadyRect.
2. Drop |ReadyRect::offset| field, which is always (0, 0).
3. Drop |is_always_opaque| option, which is always true.
4. Move PaintAggregator and PaintManager into chrome_pdf::.

R=dhoss@chromium.org

Bug: 1099020
Change-Id: Ic68d9c32ff24baac5d5a63185da688b11556b93c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2300989
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Auto-Submit: K. Moon <kmoon@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789149}
This commit is contained in:
K. Moon
2020-07-16 19:30:49 +00:00
committed by Commit Bot
parent aa627725d1
commit ad77a18ea3
9 changed files with 99 additions and 85 deletions

@ -52,6 +52,8 @@ if (enable_pdf) {
"paint_aggregator.h",
"paint_manager.cc",
"paint_manager.h",
"paint_ready_rect.cc",
"paint_ready_rect.h",
"pdf.cc",
"pdf_engine.h",
"pdf_init.cc",

@ -446,7 +446,7 @@ OutOfProcessInstance::OutOfProcessInstance(PP_Instance instance)
: pp::Instance(instance),
pp::Find_Private(this),
pp::Printing_Dev(this),
paint_manager_(this, this, true) {
paint_manager_(this, this) {
callback_factory_.Initialize(this);
pp::Module::Get()->AddPluginInterface(kPPPPdfInterface, &ppp_private);
AddPerInstanceObject(kPPPPdfInterface, this);
@ -949,7 +949,7 @@ void OutOfProcessInstance::StopFind() {
}
void OutOfProcessInstance::OnPaint(const std::vector<pp::Rect>& paint_rects,
std::vector<PaintManager::ReadyRect>* ready,
std::vector<PaintReadyRect>* ready,
std::vector<pp::Rect>* pending) {
base::AutoReset<bool> auto_reset_in_paint(&in_paint_, true);
if (image_data_.is_null()) {
@ -960,7 +960,7 @@ void OutOfProcessInstance::OnPaint(const std::vector<pp::Rect>& paint_rects,
first_paint_ = false;
pp::Rect rect = pp::Rect(pp::Point(), image_data_.size());
FillRect(rect, background_color_);
ready->push_back(PaintManager::ReadyRect(rect, image_data_, true));
ready->push_back(PaintReadyRect(rect, image_data_, /*flush_now=*/true));
}
if (!received_viewport_message_ || !needs_reraster_)
@ -984,8 +984,7 @@ void OutOfProcessInstance::OnPaint(const std::vector<pp::Rect>& paint_rects,
engine_->Paint(pdf_rect, skia_image_data_, pdf_ready, pdf_pending);
for (auto& ready_rect : pdf_ready) {
ready_rect.Offset(available_area_.point());
ready->push_back(
PaintManager::ReadyRect(ready_rect, image_data_, false));
ready->push_back(PaintReadyRect(ready_rect, image_data_));
}
for (auto& pending_rect : pdf_pending) {
pending_rect.Offset(available_area_.point());
@ -1000,7 +999,7 @@ void OutOfProcessInstance::OnPaint(const std::vector<pp::Rect>& paint_rects,
if (rect.y() < first_page_ypos) {
pp::Rect region = rect.Intersect(pp::Rect(
pp::Point(), pp::Size(plugin_size_.width(), first_page_ypos)));
ready->push_back(PaintManager::ReadyRect(region, image_data_, false));
ready->push_back(PaintReadyRect(region, image_data_));
FillRect(region, background_color_);
}
@ -1008,8 +1007,7 @@ void OutOfProcessInstance::OnPaint(const std::vector<pp::Rect>& paint_rects,
pp::Rect intersection = background_part.location.Intersect(rect);
if (!intersection.IsEmpty()) {
FillRect(intersection, background_part.color);
ready->push_back(
PaintManager::ReadyRect(intersection, image_data_, false));
ready->push_back(PaintReadyRect(intersection, image_data_));
}
}
}

@ -35,6 +35,8 @@ class TextInput_Dev;
namespace chrome_pdf {
struct PaintReadyRect;
class OutOfProcessInstance : public pp::Instance,
public pp::Find_Private,
public pp::Printing_Dev,
@ -59,7 +61,7 @@ class OutOfProcessInstance : public pp::Instance,
// pp::PaintManager::Client implementation.
void OnPaint(const std::vector<pp::Rect>& paint_rects,
std::vector<PaintManager::ReadyRect>* ready,
std::vector<PaintReadyRect>* ready,
std::vector<pp::Rect>* pending) override;
// pp::Printing_Dev implementation.

@ -11,6 +11,8 @@
#include "base/check.h"
namespace chrome_pdf {
namespace {
bool IsNegative(int32_t num) {
@ -116,14 +118,14 @@ PaintAggregator::PaintUpdate PaintAggregator::GetPendingUpdate() {
}
void PaintAggregator::SetIntermediateResults(
const std::vector<ReadyRect>& ready,
const std::vector<PaintReadyRect>& ready,
const std::vector<pp::Rect>& pending) {
update_.ready_rects.insert(update_.ready_rects.end(), ready.begin(),
ready.end());
update_.paint_rects = pending;
}
std::vector<PaintAggregator::ReadyRect> PaintAggregator::GetReadyRects() const {
std::vector<PaintReadyRect> PaintAggregator::GetReadyRects() const {
return update_.ready_rects;
}
@ -285,3 +287,5 @@ void PaintAggregator::InvalidateRectInternal(const pp::Rect& rect_old,
InvalidateRectInternal(ScrollPaintRect(rect, update_.scroll_delta), false);
}
}
} // namespace chrome_pdf

@ -7,9 +7,11 @@
#include <vector>
#include "ppapi/cpp/image_data.h"
#include "pdf/paint_ready_rect.h"
#include "ppapi/cpp/rect.h"
namespace chrome_pdf {
// This class is responsible for aggregating multiple invalidation and scroll
// commands to produce a scroll and repaint sequence. You can use this manually
// to track your updates, but most applications will use the PaintManager to
@ -19,15 +21,6 @@
// See http://code.google.com/p/ppapi/wiki/2DPaintingModel
class PaintAggregator {
public:
// Stores information about a rectangle that has finished painting. The
// PaintManager will paint it only when everything else on the screen is also
// ready.
struct ReadyRect {
pp::Point offset;
pp::Rect rect;
pp::ImageData image_data;
};
struct PaintUpdate {
PaintUpdate();
PaintUpdate(const PaintUpdate& that);
@ -69,11 +62,11 @@ class PaintAggregator {
// Sets the result of a call to the plugin to paint. This includes rects that
// are finished painting (ready), and ones that are still in-progress
// (pending).
void SetIntermediateResults(const std::vector<ReadyRect>& ready,
void SetIntermediateResults(const std::vector<PaintReadyRect>& ready,
const std::vector<pp::Rect>& pending);
// Returns the rectangles that are ready to be painted.
std::vector<ReadyRect> GetReadyRects() const;
std::vector<PaintReadyRect> GetReadyRects() const;
// The given rect should be repainted.
void InvalidateRect(const pp::Rect& rect);
@ -109,7 +102,7 @@ class PaintAggregator {
std::vector<pp::Rect> paint_rects;
// Rectangles that are finished painting.
std::vector<ReadyRect> ready_rects;
std::vector<PaintReadyRect> ready_rects;
// Whether we have added the scroll damage rect to paint_rects yet or not.
bool synthesized_scroll_damage_rect_;
@ -127,4 +120,6 @@ class PaintAggregator {
InternalPaintUpdate update_;
};
} // namespace chrome_pdf
#endif // PDF_PAINT_AGGREGATOR_H_

@ -11,25 +11,15 @@
#include "base/auto_reset.h"
#include "base/check_op.h"
#include "pdf/paint_ready_rect.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
PaintManager::ReadyRect::ReadyRect() = default;
namespace chrome_pdf {
PaintManager::ReadyRect::ReadyRect(const pp::Rect& r,
const pp::ImageData& i,
bool f)
: rect(r), image_data(i), flush_now(f) {}
PaintManager::ReadyRect::ReadyRect(const ReadyRect& that) = default;
PaintManager::PaintManager(pp::Instance* instance,
Client* client,
bool is_always_opaque)
: instance_(instance),
client_(client),
is_always_opaque_(is_always_opaque) {
PaintManager::PaintManager(pp::Instance* instance, Client* client)
: instance_(instance), client_(client) {
DCHECK(instance_);
DCHECK(client_);
@ -177,7 +167,7 @@ void PaintManager::EnsureCallbackPending() {
void PaintManager::DoPaint() {
base::AutoReset<bool> auto_reset_in_paint(&in_paint_, true);
std::vector<ReadyRect> ready_rects;
std::vector<PaintReadyRect> ready_rects;
std::vector<pp::Rect> pending_rects;
DCHECK(aggregator_.HasPendingUpdate());
@ -194,7 +184,8 @@ void PaintManager::DoPaint() {
// we only resize by a small amount.
pp::Size new_size = GetNewContextSize(graphics_.size(), pending_size_);
if (graphics_.size() != new_size) {
graphics_ = pp::Graphics2D(instance_, new_size, is_always_opaque_);
graphics_ =
pp::Graphics2D(instance_, new_size, /*is_always_opaque=*/true);
graphics_need_to_be_bound_ = true;
// Since we're binding a new one, all of the callbacks have been canceled.
@ -219,9 +210,9 @@ void PaintManager::DoPaint() {
if (ready_rects.empty() && pending_rects.empty())
return; // Nothing was painted, don't schedule a flush.
std::vector<PaintAggregator::ReadyRect> ready_now;
std::vector<PaintReadyRect> ready_now;
if (pending_rects.empty()) {
std::vector<PaintAggregator::ReadyRect> temp_ready;
std::vector<PaintReadyRect> temp_ready;
temp_ready.insert(temp_ready.end(), ready_rects.begin(), ready_rects.end());
aggregator_.SetIntermediateResults(temp_ready, pending_rects);
ready_now = aggregator_.GetReadyRects();
@ -233,7 +224,7 @@ void PaintManager::DoPaint() {
view_size_changed_waiting_for_paint_ = false;
} else {
std::vector<PaintAggregator::ReadyRect> ready_later;
std::vector<PaintReadyRect> ready_later;
for (const auto& ready_rect : ready_rects) {
// Don't flush any part (i.e. scrollbars) if we're resizing the browser,
// as that'll lead to flashes. Until we flush, the browser will use the
@ -258,7 +249,7 @@ void PaintManager::DoPaint() {
}
for (const auto& ready_rect : ready_now) {
graphics_.PaintImageData(ready_rect.image_data, ready_rect.offset,
graphics_.PaintImageData(ready_rect.image_data, pp::Point(),
ready_rect.rect);
}
@ -322,3 +313,5 @@ void PaintManager::OnManualCallbackComplete(int32_t) {
if (aggregator_.HasPendingUpdate())
DoPaint();
}
} // namespace chrome_pdf

@ -14,12 +14,13 @@
#include "ppapi/utility/completion_callback_factory.h"
namespace pp {
class Graphics2D;
class Instance;
class Point;
class Rect;
} // namespace pp
namespace chrome_pdf {
// Custom PaintManager for the PDF plugin. This is branched from the Pepper
// version. The difference is that this supports progressive rendering of dirty
// rects, where multiple calls to the rendering engine are needed. It also
@ -29,28 +30,6 @@ class Rect;
// The client's OnPaint
class PaintManager {
public:
// Like PaintAggregator's version, but allows the plugin to tell us whether
// it should be flushed to the screen immediately or when the rest of the
// plugin viewport is ready.
struct ReadyRect {
ReadyRect();
ReadyRect(const pp::Rect& r, const pp::ImageData& i, bool f);
ReadyRect(const ReadyRect& that);
operator PaintAggregator::ReadyRect() const {
PaintAggregator::ReadyRect rv;
rv.offset = offset;
rv.rect = rect;
rv.image_data = image_data;
return rv;
}
pp::Point offset;
pp::Rect rect;
pp::ImageData image_data;
bool flush_now;
};
class Client {
public:
// Paints the given invalid area of the plugin to the given graphics
@ -72,7 +51,7 @@ class PaintManager {
//
// Calling Invalidate/Scroll is not allowed while inside an OnPaint
virtual void OnPaint(const std::vector<pp::Rect>& paint_rects,
std::vector<ReadyRect>* ready,
std::vector<PaintReadyRect>* ready,
std::vector<pp::Rect>* pending) = 0;
protected:
@ -88,23 +67,9 @@ class PaintManager {
// The Client is a non-owning pointer and must remain valid (normally the
// object implementing the Client interface will own the paint manager).
//
// The is_always_opaque flag will be passed to the device contexts that this
// class creates. Set this to true if your plugin always draws an opaque
// image to the device. This is used as a hint to the browser that it does
// not need to do alpha blending, which speeds up painting. If you generate
// non-opqaue pixels or aren't sure, set this to false for more general
// blending.
//
// If you set is_always_opaque, your alpha channel should always be set to
// 0xFF or there may be painting artifacts. Being opaque will allow the
// browser to do a memcpy rather than a blend to paint the plugin, and this
// means your alpha values will get set on the page backing store. If these
// values are incorrect, it could mess up future blending. If you aren't
// sure, it is always correct to specify that it it not opaque.
//
// You will need to call SetSize before this class will do anything. Normally
// you do this from the ViewChanged method of your plugin instance.
PaintManager(pp::Instance* instance, Client* client, bool is_always_opaque);
PaintManager(pp::Instance* instance, Client* client);
PaintManager(const PaintManager&) = delete;
PaintManager& operator=(const PaintManager&) = delete;
~PaintManager();
@ -180,8 +145,6 @@ class PaintManager {
// Non-owning pointer. See the constructor.
Client* const client_;
const bool is_always_opaque_;
pp::CompletionCallbackFactory<PaintManager> callback_factory_;
// This graphics device will be is_null() if no graphics has been manually
@ -215,4 +178,6 @@ class PaintManager {
bool view_size_changed_waiting_for_paint_ = false;
};
} // namespace chrome_pdf
#endif // PDF_PAINT_MANAGER_H_

22
pdf/paint_ready_rect.cc Normal file

@ -0,0 +1,22 @@
// 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/paint_ready_rect.h"
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/rect.h"
namespace chrome_pdf {
PaintReadyRect::PaintReadyRect(const pp::Rect& rect,
const pp::ImageData& image_data,
bool flush_now)
: rect(rect), image_data(image_data), flush_now(flush_now) {}
PaintReadyRect::PaintReadyRect(const PaintReadyRect& other) = default;
PaintReadyRect& PaintReadyRect::operator=(const PaintReadyRect& other) =
default;
} // namespace chrome_pdf

33
pdf/paint_ready_rect.h Normal file

@ -0,0 +1,33 @@
// 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_PAINT_READY_RECT_H_
#define PDF_PAINT_READY_RECT_H_
#include "ppapi/cpp/image_data.h"
#include "ppapi/cpp/rect.h"
namespace chrome_pdf {
// Stores information about a rectangle that has finished painting. The
// `PaintManager` will paint it only when everything else on the screen is also
// ready.
struct PaintReadyRect {
PaintReadyRect(const pp::Rect& rect,
const pp::ImageData& image_data,
bool flush_now = false);
PaintReadyRect(const PaintReadyRect& other);
PaintReadyRect& operator=(const PaintReadyRect& other);
pp::Rect rect;
pp::ImageData image_data;
// Whether to flush to screen immediately; otherwise, when the rest of the
// plugin viewport is ready.
bool flush_now;
};
} // namespace chrome_pdf
#endif // PDF_PAINT_READY_RECT_H_