#include "pdf/paint_manager.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <cmath>
#include <utility>
#include "base/auto_reset.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/notreached.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "pdf/paint_ready_rect.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSamplingOptions.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/blit.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/geometry/vector2d_f.h"
namespace chrome_pdf {
PaintManager::PaintManager(Client* client) : client_(client) {
DCHECK(client_);
}
PaintManager::~PaintManager() = default;
gfx::Size PaintManager::GetNewContextSize(const gfx::Size& current_context_size,
const gfx::Size& plugin_size) {
constexpr int kBufferSize = 50;
gfx::Size result = current_context_size;
gfx::Size min_size(
std::max(current_context_size.width() - 2 * kBufferSize, 0),
std::max(current_context_size.height() - 2 * kBufferSize, 0));
if (plugin_size.width() > current_context_size.width() ||
plugin_size.height() > current_context_size.height() ||
plugin_size.width() < min_size.width() ||
plugin_size.height() < min_size.height()) {
result = gfx::Size(plugin_size.width() + kBufferSize,
plugin_size.height() + kBufferSize);
}
return result;
}
void PaintManager::SetSize(const gfx::Size& new_size, float device_scale) {
if (GetEffectiveSize() == new_size &&
GetEffectiveDeviceScale() == device_scale) {
return;
}
has_pending_resize_ = true;
pending_size_ = new_size;
pending_device_scale_ = device_scale;
view_size_changed_waiting_for_paint_ = true;
Invalidate();
}
void PaintManager::SetTransform(float scale,
const gfx::Point& origin,
const gfx::Vector2d& translate,
bool schedule_flush) {
if (!surface_)
return;
if (scale <= 0.0f) {
NOTREACHED();
} else {
gfx::Vector2dF translate_with_origin = origin.OffsetFromOrigin();
translate_with_origin.Scale(1.0f - scale);
translate_with_origin.Subtract(translate);
client_->UpdateLayerTransform(scale, translate_with_origin);
}
if (!schedule_flush)
return;
if (flush_pending_) {
flush_requested_ = true;
return;
}
Flush();
}
void PaintManager::ClearTransform() {
SetTransform(1.f, gfx::Point(), gfx::Vector2d(), false);
}
void PaintManager::Invalidate() {
if (!surface_ && !has_pending_resize_)
return;
EnsureCallbackPending();
aggregator_.InvalidateRect(gfx::Rect(GetEffectiveSize()));
}
void PaintManager::InvalidateRect(const gfx::Rect& rect) {
DCHECK(!in_paint_);
if (!surface_ && !has_pending_resize_)
return;
gfx::Rect clipped_rect =
gfx::IntersectRects(rect, gfx::Rect(GetEffectiveSize()));
if (clipped_rect.IsEmpty())
return;
EnsureCallbackPending();
aggregator_.InvalidateRect(clipped_rect);
}
void PaintManager::ScrollRect(const gfx::Rect& clip_rect,
const gfx::Vector2d& amount) {
DCHECK(!in_paint_);
if (!surface_ && !has_pending_resize_)
return;
EnsureCallbackPending();
aggregator_.ScrollRect(clip_rect, amount);
}
gfx::Size PaintManager::GetEffectiveSize() const {
return has_pending_resize_ ? pending_size_ : plugin_size_;
}
float PaintManager::GetEffectiveDeviceScale() const {
return has_pending_resize_ ? pending_device_scale_ : device_scale_;
}
void PaintManager::EnsureCallbackPending() {
if (flush_pending_)
return;
if (manual_callback_pending_)
return;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&PaintManager::OnManualCallbackComplete,
weak_factory_.GetWeakPtr()));
manual_callback_pending_ = true;
}
void PaintManager::DoPaint() {
base::AutoReset<bool> auto_reset_in_paint(&in_paint_, true);
std::vector<PaintReadyRect> ready_rects;
std::vector<gfx::Rect> pending_rects;
DCHECK(aggregator_.HasPendingUpdate());
DCHECK(surface_ || has_pending_resize_);
if (has_pending_resize_) {
plugin_size_ = pending_size_;
gfx::Size old_size = surface_
? gfx::Size(surface_->width(), surface_->height())
: gfx::Size();
gfx::Size new_size = GetNewContextSize(old_size, pending_size_);
if (old_size != new_size || !surface_) {
surface_ =
SkSurface::MakeRasterN32Premul(new_size.width(), new_size.height());
DCHECK(surface_);
client_->InvalidatePluginContainer();
device_scale_ = 1.0f;
manual_callback_pending_ = false;
flush_pending_ = false;
weak_factory_.InvalidateWeakPtrs();
}
if (pending_device_scale_ != device_scale_)
client_->UpdateScale(1.0f / pending_device_scale_);
device_scale_ = pending_device_scale_;
has_pending_resize_ = false;
pending_size_ = gfx::Size();
}
PaintAggregator::PaintUpdate update = aggregator_.GetPendingUpdate();
client_->OnPaint(update.paint_rects, ready_rects, pending_rects);
if (ready_rects.empty() && pending_rects.empty())
return;
std::vector<PaintReadyRect> ready_now;
if (pending_rects.empty()) {
aggregator_.SetIntermediateResults(ready_rects, pending_rects);
ready_now = aggregator_.GetReadyRects();
aggregator_.ClearPendingUpdate();
if (update.has_scroll &&
std::abs(update.scroll_delta.x()) < surface_->width() &&
std::abs(update.scroll_delta.y()) < surface_->height()) {
gfx::ScrollCanvas(surface_->getCanvas(), update.scroll_rect,
update.scroll_delta);
}
view_size_changed_waiting_for_paint_ = false;
} else {
std::vector<PaintReadyRect> ready_later;
for (const auto& ready_rect : ready_rects) {
if (ready_rect.flush_now() &&
(!view_size_changed_waiting_for_paint_ || first_paint_)) {
ready_now.push_back(ready_rect);
} else {
ready_later.push_back(ready_rect);
}
}
aggregator_.SetIntermediateResults(ready_later, pending_rects);
if (ready_now.empty()) {
EnsureCallbackPending();
return;
}
}
for (const auto& ready_rect : ready_now) {
SkRect skia_rect = gfx::RectToSkRect(ready_rect.rect());
surface_->getCanvas()->drawImageRect(
&ready_rect.image(), skia_rect, skia_rect, SkSamplingOptions(), nullptr,
SkCanvas::kStrict_SrcRectConstraint);
}
Flush();
first_paint_ = false;
}
void PaintManager::Flush() {
flush_requested_ = false;
sk_sp<SkImage> snapshot = surface_->makeImageSnapshot();
surface_->getCanvas()->drawImage(snapshot.get(), 0, 0,
SkSamplingOptions(), nullptr);
client_->UpdateSnapshot(std::move(snapshot));
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&PaintManager::OnFlushComplete,
weak_factory_.GetWeakPtr()));
flush_pending_ = true;
}
void PaintManager::OnFlushComplete() {
DCHECK(flush_pending_);
flush_pending_ = false;
if (aggregator_.HasPendingUpdate())
DoPaint();
if (flush_requested_) {
Flush();
}
}
void PaintManager::OnManualCallbackComplete() {
DCHECK(manual_callback_pending_);
manual_callback_pending_ = false;
if (aggregator_.HasPendingUpdate())
DoPaint();
}
}