1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "components/paint_preview/common/paint_preview_tracker.h"
#include <stdint.h>
#include <string>
#include <utility>
#include "base/check.h"
#include "base/containers/contains.h"
#include "components/paint_preview/common/glyph_usage.h"
#include "components/paint_preview/common/mojom/paint_preview_recorder.mojom.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkMatrix.h"
#include "third_party/skia/include/core/SkTextBlob.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "ui/gfx/geometry/rect.h"
#include "url/gurl.h"
namespace paint_preview {
namespace {
constexpr int kMaxGlyphsForDenseGlyphUsage = 10000;
// Heuristically choose between a dense and sparse glyph usage map.
// TODO(crbug.com/40101107): Gather data to make this heuristic better.
bool ShouldUseDenseGlyphUsage(SkTypeface* typeface) {
// DenseGlyphUsage is a bitset; it is efficient if lots of glyphs are used.
// SparseGlyphUsage is a set; it is efficient if few glyphs are used.
// Generally, smaller fonts have a higher percentage of used glyphs so set a
// maximum threshold for number of glyphs before using SparseGlyphUsage.
return typeface->countGlyphs() < kMaxGlyphsForDenseGlyphUsage;
}
} // namespace
PaintPreviewTracker::PaintPreviewTracker(
const base::UnguessableToken& guid,
const std::optional<base::UnguessableToken>& embedding_token,
bool is_main_frame)
: guid_(guid),
embedding_token_(embedding_token),
is_main_frame_(is_main_frame) {}
PaintPreviewTracker::~PaintPreviewTracker() {
DCHECK(states_.empty());
}
void PaintPreviewTracker::Save() {
states_.push_back(matrix_);
}
void PaintPreviewTracker::SetMatrix(const SkMatrix& matrix) {
matrix_ = matrix;
}
void PaintPreviewTracker::Restore() {
if (states_.size() == 0) {
DLOG(ERROR) << "No state to restore";
return;
}
matrix_ = states_.back();
states_.pop_back();
}
void PaintPreviewTracker::Concat(const SkMatrix& matrix) {
if (matrix.isIdentity())
return;
matrix_.preConcat(matrix);
}
void PaintPreviewTracker::Scale(SkScalar x, SkScalar y) {
if (x != 1 || y != 1) {
matrix_.preScale(x, y);
}
}
void PaintPreviewTracker::Rotate(SkScalar degrees) {
SkMatrix m;
m.setRotate(degrees);
Concat(m);
}
void PaintPreviewTracker::Translate(SkScalar x, SkScalar y) {
if (x || y) {
matrix_.preTranslate(x, y);
}
}
uint32_t PaintPreviewTracker::CreateContentForRemoteFrame(
const gfx::Rect& rect,
const base::UnguessableToken& embedding_token) {
sk_sp<SkPicture> pic = SkPicture::MakePlaceholder(
SkRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()));
const uint32_t content_id = pic->uniqueID();
DCHECK(!base::Contains(picture_context_.content_id_to_embedding_token,
content_id));
picture_context_.content_id_to_embedding_token[content_id] = embedding_token;
subframe_pics_[content_id] = pic;
return content_id;
}
void PaintPreviewTracker::AddGlyphs(const SkTextBlob* blob) {
if (!blob)
return;
SkTextBlob::Iter::Run run;
for (SkTextBlob::Iter it(*blob); it.next(&run);) {
SkTypeface* typeface = run.fTypeface;
// Fail fast if the number of glyphs is undetermined or 0.
if (typeface->countGlyphs() <= 0)
continue;
if (!base::Contains(typeface_glyph_usage_, typeface->uniqueID())) {
if (ShouldUseDenseGlyphUsage(typeface)) {
typeface_glyph_usage_.insert(
{typeface->uniqueID(),
std::make_unique<DenseGlyphUsage>(
static_cast<uint16_t>(typeface->countGlyphs() - 1))});
} else {
typeface_glyph_usage_.insert(
{typeface->uniqueID(),
std::make_unique<SparseGlyphUsage>(
static_cast<uint16_t>(typeface->countGlyphs() - 1))});
}
// Always set the 0th glyph.
typeface_glyph_usage_[typeface->uniqueID()]->Set(0U);
}
const uint16_t* glyphs = run.fGlyphIndices;
for (int i = 0; i < run.fGlyphCount; ++i)
typeface_glyph_usage_[typeface->uniqueID()]->Set(glyphs[i]);
}
}
void PaintPreviewTracker::AnnotateLink(const GURL& url, const SkRect& rect) {
SkRect out_rect;
matrix_.mapRect(&out_rect, rect);
links_.push_back(mojom::LinkData::New(
url, gfx::Rect(out_rect.x(), out_rect.y(), out_rect.width(),
out_rect.height())));
}
void PaintPreviewTracker::TransformClipForFrame(uint32_t id) {
auto pic_it = subframe_pics_.find(id);
if (pic_it == subframe_pics_.end())
return;
SkRect out_rect;
matrix_.mapRect(&out_rect, pic_it->second->cullRect());
picture_context_.content_id_to_transformed_clip.emplace(id, out_rect);
}
void PaintPreviewTracker::CustomDataToSkPictureCallback(SkCanvas* canvas,
uint32_t content_id) {
auto map_it = picture_context_.content_id_to_embedding_token.find(content_id);
if (map_it == picture_context_.content_id_to_embedding_token.end())
return;
auto it = subframe_pics_.find(content_id);
// DCHECK is sufficient as |subframe_pics_| has same entries as
// |content_id_to_proxy_id|.
CHECK(it != subframe_pics_.end());
SkRect rect = it->second->cullRect();
SkMatrix subframe_offset = SkMatrix::Translate(rect.x(), rect.y());
canvas->drawPicture(it->second, &subframe_offset, nullptr);
}
void PaintPreviewTracker::MoveLinks(std::vector<mojom::LinkDataPtr>* out) {
std::move(links_.begin(), links_.end(), std::back_inserter(*out));
links_.clear();
}
} // namespace paint_preview
|