2015-09-18 19:09:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-02-14 16:47:35 +00:00
|
|
|
#include "src/core/SkScalerCache.h"
|
2015-09-18 19:09:43 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "bench/Benchmark.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
2019-10-23 16:52:33 +00:00
|
|
|
#include "src/core/SkRemoteGlyphCache.h"
|
2019-04-17 22:01:04 +00:00
|
|
|
#include "src/core/SkStrikeSpec.h"
|
2020-09-29 14:58:21 +00:00
|
|
|
#include "src/core/SkTLazy.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkTaskGroup.h"
|
2019-10-23 16:52:33 +00:00
|
|
|
#include "src/core/SkTextBlobTrace.h"
|
|
|
|
#include "tools/Resources.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2015-09-18 19:09:43 +00:00
|
|
|
|
2018-11-28 15:28:07 +00:00
|
|
|
static void do_font_stuff(SkFont* font) {
|
|
|
|
SkPaint defaultPaint;
|
2015-09-18 19:09:43 +00:00
|
|
|
for (SkScalar i = 8; i < 64; i++) {
|
2018-11-28 15:28:07 +00:00
|
|
|
font->setSize(i);
|
2019-06-06 14:50:56 +00:00
|
|
|
auto strikeSpec = SkStrikeSpec::MakeMask(
|
2018-11-28 15:28:07 +00:00
|
|
|
*font, defaultPaint, SkSurfaceProps(0, kUnknown_SkPixelGeometry),
|
2018-11-08 21:40:26 +00:00
|
|
|
SkScalerContextFlags::kNone, SkMatrix::I());
|
2019-07-03 18:46:05 +00:00
|
|
|
SkPackedGlyphID glyphs['z'];
|
2015-09-18 19:09:43 +00:00
|
|
|
for (int c = ' '; c < 'z'; c++) {
|
2019-07-03 18:46:05 +00:00
|
|
|
glyphs[c] = SkPackedGlyphID{font->unicharToGlyph(c)};
|
2015-09-18 19:09:43 +00:00
|
|
|
}
|
2019-07-03 18:46:05 +00:00
|
|
|
constexpr size_t glyphCount = 'z' - ' ';
|
|
|
|
SkSpan<const SkPackedGlyphID> glyphIDs{&glyphs[SkTo<int>(' ')], glyphCount};
|
|
|
|
SkBulkGlyphMetricsAndImages images{strikeSpec};
|
2015-09-18 19:09:43 +00:00
|
|
|
for (int lookups = 0; lookups < 10; lookups++) {
|
2019-07-03 18:46:05 +00:00
|
|
|
(void)images.glyphs(glyphIDs);
|
2015-09-18 19:09:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SkGlyphCacheBasic : public Benchmark {
|
|
|
|
public:
|
|
|
|
explicit SkGlyphCacheBasic(size_t cacheSize) : fCacheSize(cacheSize) { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
fName.printf("SkGlyphCacheBasic%dK", (int)(fCacheSize >> 10));
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2015-09-18 19:09:43 +00:00
|
|
|
size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
|
|
|
|
SkGraphics::SetFontCacheLimit(fCacheSize);
|
2018-11-28 15:28:07 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
font.setSubpixel(true);
|
2019-03-20 16:12:10 +00:00
|
|
|
font.setTypeface(ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()));
|
2015-09-18 19:09:43 +00:00
|
|
|
|
|
|
|
for (int work = 0; work < loops; work++) {
|
2018-11-28 15:28:07 +00:00
|
|
|
do_font_stuff(&font);
|
2015-09-18 19:09:43 +00:00
|
|
|
}
|
|
|
|
SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2015-09-18 19:09:43 +00:00
|
|
|
const size_t fCacheSize;
|
|
|
|
SkString fName;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SkGlyphCacheStressTest : public Benchmark {
|
|
|
|
public:
|
|
|
|
explicit SkGlyphCacheStressTest(int cacheSize) : fCacheSize(cacheSize) { }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const char* onGetName() override {
|
|
|
|
fName.printf("SkGlyphCacheStressTest%dK", (int)(fCacheSize >> 10));
|
|
|
|
return fName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend backend) override {
|
|
|
|
return backend == kNonRendering_Backend;
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:43:39 +00:00
|
|
|
void onDraw(int loops, SkCanvas*) override {
|
2015-09-18 19:09:43 +00:00
|
|
|
size_t oldCacheLimitSize = SkGraphics::GetFontCacheLimit();
|
|
|
|
SkGraphics::SetFontCacheLimit(fCacheSize);
|
2019-03-20 16:12:10 +00:00
|
|
|
sk_sp<SkTypeface> typefaces[] = {
|
|
|
|
ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic()),
|
|
|
|
ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic())};
|
2015-09-18 19:09:43 +00:00
|
|
|
|
|
|
|
for (int work = 0; work < loops; work++) {
|
2016-01-05 03:13:19 +00:00
|
|
|
SkTaskGroup().batch(16, [&](int threadIndex) {
|
2018-11-28 15:28:07 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setTypeface(typefaces[threadIndex % 2]);
|
|
|
|
do_font_stuff(&font);
|
2015-09-18 19:09:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
SkGraphics::SetFontCacheLimit(oldCacheLimitSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Benchmark;
|
2015-09-18 19:09:43 +00:00
|
|
|
const size_t fCacheSize;
|
|
|
|
SkString fName;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_BENCH( return new SkGlyphCacheBasic(256 * 1024); )
|
|
|
|
DEF_BENCH( return new SkGlyphCacheBasic(32 * 1024 * 1024); )
|
|
|
|
DEF_BENCH( return new SkGlyphCacheStressTest(256 * 1024); )
|
|
|
|
DEF_BENCH( return new SkGlyphCacheStressTest(32 * 1024 * 1024); )
|
2019-10-23 16:52:33 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DiscardableManager : public SkStrikeServer::DiscardableHandleManager,
|
|
|
|
public SkStrikeClient::DiscardableHandleManager {
|
|
|
|
public:
|
|
|
|
DiscardableManager() { sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount)); }
|
|
|
|
~DiscardableManager() override = default;
|
|
|
|
|
|
|
|
// Server implementation.
|
|
|
|
SkDiscardableHandleId createHandle() override {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
// Handles starts as locked.
|
|
|
|
fLockedHandles.add(++fNextHandleId);
|
|
|
|
return fNextHandleId;
|
|
|
|
}
|
|
|
|
bool lockHandle(SkDiscardableHandleId id) override {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
if (id <= fLastDeletedHandleId) return false;
|
|
|
|
fLockedHandles.add(id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client implementation.
|
|
|
|
bool deleteHandle(SkDiscardableHandleId id) override {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
return id <= fLastDeletedHandleId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void notifyCacheMiss(SkStrikeClient::CacheMissType type) override {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
fCacheMissCount[type]++;
|
|
|
|
}
|
|
|
|
bool isHandleDeleted(SkDiscardableHandleId id) override {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
return id <= fLastDeletedHandleId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlockAll() {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
fLockedHandles.reset();
|
|
|
|
}
|
|
|
|
void unlockAndDeleteAll() {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
fLockedHandles.reset();
|
|
|
|
fLastDeletedHandleId = fNextHandleId;
|
|
|
|
}
|
|
|
|
const SkTHashSet<SkDiscardableHandleId>& lockedHandles() const {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
return fLockedHandles;
|
|
|
|
}
|
|
|
|
SkDiscardableHandleId handleCount() {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
return fNextHandleId;
|
|
|
|
}
|
|
|
|
int cacheMissCount(uint32_t type) {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
return fCacheMissCount[type];
|
|
|
|
}
|
|
|
|
bool hasCacheMiss() const {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i <= SkStrikeClient::CacheMissType::kLast; ++i) {
|
|
|
|
if (fCacheMissCount[i] > 0) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void resetCacheMissCounts() {
|
|
|
|
SkAutoMutexExclusive l(fMutex);
|
|
|
|
sk_bzero(&fCacheMissCount, sizeof(fCacheMissCount));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// The tests below run in parallel on multiple threads and use the same
|
|
|
|
// process global SkStrikeCache. So the implementation needs to be
|
|
|
|
// thread-safe.
|
|
|
|
mutable SkMutex fMutex;
|
|
|
|
|
|
|
|
SkDiscardableHandleId fNextHandleId = 0u;
|
|
|
|
SkDiscardableHandleId fLastDeletedHandleId = 0u;
|
|
|
|
SkTHashSet<SkDiscardableHandleId> fLockedHandles;
|
|
|
|
int fCacheMissCount[SkStrikeClient::CacheMissType::kLast + 1u];
|
|
|
|
};
|
|
|
|
|
2019-10-24 17:52:17 +00:00
|
|
|
class DiffCanvasBench : public Benchmark {
|
|
|
|
SkString fBenchName;
|
|
|
|
std::function<std::unique_ptr<SkStreamAsset>()> fDataProvider;
|
2019-10-23 16:52:33 +00:00
|
|
|
std::vector<SkTextBlobTrace::Record> fTrace;
|
|
|
|
sk_sp<DiscardableManager> fDiscardableManager;
|
|
|
|
SkTLazy<SkStrikeServer> fServer;
|
|
|
|
|
|
|
|
const char* onGetName() override { return fBenchName.c_str(); }
|
|
|
|
|
|
|
|
bool isSuitableFor(Backend b) override { return b == kNonRendering_Backend; }
|
|
|
|
|
2020-09-24 18:49:00 +00:00
|
|
|
void onDraw(int loops, SkCanvas* modelCanvas) override {
|
|
|
|
SkSurfaceProps props;
|
|
|
|
if (modelCanvas) { modelCanvas->getProps(&props); }
|
2021-05-06 12:59:26 +00:00
|
|
|
std::unique_ptr<SkCanvas> canvas = fServer->makeAnalysisCanvas(1024, 1024, props,
|
|
|
|
nullptr, true);
|
2019-10-23 16:52:33 +00:00
|
|
|
loops *= 100;
|
|
|
|
while (loops --> 0) {
|
|
|
|
for (const auto& record : fTrace) {
|
2021-05-06 12:59:26 +00:00
|
|
|
canvas->drawTextBlob(
|
2019-10-23 16:52:33 +00:00
|
|
|
record.blob.get(), record.offset.x(), record.offset.y(),record.paint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDelayedSetup() override {
|
2019-10-24 17:52:17 +00:00
|
|
|
auto stream = fDataProvider();
|
2019-10-23 16:52:33 +00:00
|
|
|
fDiscardableManager = sk_make_sp<DiscardableManager>();
|
|
|
|
fServer.init(fDiscardableManager.get());
|
|
|
|
fTrace = SkTextBlobTrace::CreateBlobTrace(stream.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-10-24 17:52:17 +00:00
|
|
|
DiffCanvasBench(SkString n, std::function<std::unique_ptr<SkStreamAsset>()> f)
|
|
|
|
: fBenchName(std::move(n)), fDataProvider(std::move(f)) {}
|
2019-10-23 16:52:33 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
2019-10-24 17:52:17 +00:00
|
|
|
Benchmark* CreateDiffCanvasBench(
|
|
|
|
SkString name, std::function<std::unique_ptr<SkStreamAsset>()> dataSrc) {
|
|
|
|
return new DiffCanvasBench(std::move(name), std::move(dataSrc));
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_BENCH( return CreateDiffCanvasBench(
|
|
|
|
SkString("SkDiffBench-lorem_ipsum"),
|
|
|
|
[](){ return GetResourceAsStream("diff_canvas_traces/lorem_ipsum.trace"); }));
|