Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
blob_cache_sim.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
17
18#include <iostream>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23int main(int argc, char** argv) {
24 std::unordered_map<uint64_t, uint32_t> counts;
25 size_t total = 0;
26
27 for (int i = 1; i < argc; i++) {
28 const char* filename = argv[i];
29
30 SkFILEStream in{filename};
31 std::vector<SkTextBlobTrace::Record> trace = SkTextBlobTrace::CreateBlobTrace(&in, nullptr);
32 for (const SkTextBlobTrace::Record& record : trace) {
33 total++;
34 const SkPaint paint = record.paint;
35 bool fastByPass = paint.getStyle() == SkPaint::kFill_Style
36 && paint.getPathEffect() == nullptr
37 && paint.getMaskFilter() == nullptr;
38 if (fastByPass) {
39 uint64_t blobID = record.origUniqueID;
40 SkPoint offset = record.offset;
42 uint32_t colorBits =
43 (SkColorGetR(c) >> 5u) << 6u
44 | (SkColorGetG(c) >> 5u) << 3u
45 | SkColorGetB(c) >> 5u;
46
47 SkFixed fx = (SkScalarToFixed(offset.x()) >> 13) & 7;
48 SkFixed fy = (SkScalarToFixed(offset.y()) >> 13) & 7;
49 uint32_t posBits = (fx << 3 | fy) << 12;
50
51 uint64_t blobKey = blobID << 32u | posBits | colorBits;
52 auto lookup = counts.find(blobKey);
53 if (lookup == counts.end()) {
54 bool ok;
55 std::tie(lookup, ok) = counts.insert({blobKey, 0});
56 SkASSERT(ok);
57 }
58 lookup->second += 1;
59 std::cout << std::hex << blobKey << "\n";
60 }
61 }
62 std::cerr << "trace: " << filename
63 << " unique: " << counts.size()
64 << " all: " << total
65 << " ratio: " << (float)total/counts.size() << "\n";
66
67 SkRect bounds = {0, 0, 0, 0};
68 for (const SkTextBlobTrace::Record& record : trace) {
69 bounds.join(record.blob->bounds().makeOffset(record.offset.x(), record.offset.y()));
70 }
71 SkIRect iBounds = bounds.roundOut();
72 if (iBounds.size().isEmpty()) {
73 continue;
74 }
75 static constexpr SkColor kBackground = SK_ColorWHITE;
77 SkImageInfo::MakeN32Premul(iBounds.width() + 16, iBounds.height() + 16));
78 SkCanvas* canvas = surf->getCanvas();
79 canvas->translate(8.0f - iBounds.x(), 8.0f - iBounds.y());
80 canvas->clear(kBackground);
81
82 for (const SkTextBlobTrace::Record& record : trace) {
83 canvas->drawTextBlob(
84 record.blob.get(), record.offset.x(), record.offset.y(), record.paint);
85 }
86
87 sk_sp<SkImage> img(surf->makeImageSnapshot());
88 if (img) {
89 sk_sp<SkData> png = SkPngEncoder::Encode(nullptr, img.get(), {});
90 if (png) {
91 SkString path = SkStringPrintf("text_blob_trace_%04d.png", i);
92 SkFILEWStream(path.c_str()).write(png->data(), png->size());
93 }
94 }
95 }
96 return 0;
97}
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkColorGetR(color)
Definition SkColor.h:65
#define SkColorGetG(color)
Definition SkColor.h:69
uint32_t SkColor
Definition SkColor.h:37
#define SkColorGetB(color)
Definition SkColor.h:73
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
int32_t SkFixed
Definition SkFixed.h:25
#define SkScalarToFixed(x)
Definition SkFixed.h:125
static bool ok(int result)
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
void translate(SkScalar dx, SkScalar dy)
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
bool write(const void *buffer, size_t size) override
Definition SkStream.cpp:426
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
static SkColor CanonicalColor(SkColor color)
T * get() const
Definition SkRefCnt.h:303
const Paint & paint
@ kBackground
Suitable for threads that shouldn't disrupt high priority work.
Definition embedder.h:258
char ** argv
Definition library.h:9
SK_API bool Encode(SkWStream *dst, const SkPixmap &src, const Options &options)
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
std::vector< SkTextBlobTrace::Record > CreateBlobTrace(SkStream *stream, sk_sp< SkFontMgr > lastResortMgr)
Definition main.py:1
Point offset
constexpr int32_t x() const
Definition SkRect.h:141
constexpr int32_t y() const
Definition SkRect.h:148
constexpr SkISize size() const
Definition SkRect.h:172
constexpr int32_t height() const
Definition SkRect.h:165
constexpr int32_t width() const
Definition SkRect.h:158
bool isEmpty() const
Definition SkSize.h:31
static SkImageInfo MakeN32Premul(int width, int height)
void offset(float dx, float dy)