Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkTextBlobTrace.cpp
Go to the documentation of this file.
1// Copyright 2019 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
5
14#include "src/base/SkTLazy.h"
15#include "src/core/SkChecksum.h"
16#include "src/core/SkFontPriv.h"
21#include "src/text/GlyphRun.h"
22
23#include <utility>
24
25std::vector<SkTextBlobTrace::Record> SkTextBlobTrace::CreateBlobTrace(
26 SkStream* stream, sk_sp<SkFontMgr> lastResortMgr) {
27 std::vector<SkTextBlobTrace::Record> trace;
28
29 uint32_t typefaceCount;
30 if (!stream->readU32(&typefaceCount)) {
31 return trace;
32 }
33
34 std::vector<sk_sp<SkTypeface>> typefaceArray;
35 for (uint32_t i = 0; i < typefaceCount; i++) {
36 typefaceArray.push_back(SkTypeface::MakeDeserialize(stream, lastResortMgr));
37 }
38
39 uint32_t restOfFile;
40 if (!stream->readU32(&restOfFile)) {
41 return trace;
42 }
43 sk_sp<SkData> data = SkData::MakeFromStream(stream, restOfFile);
44 SkReadBuffer readBuffer{data->data(), data->size()};
45 readBuffer.setTypefaceArray(typefaceArray.data(), typefaceArray.size());
46
47 while (!readBuffer.eof()) {
49 record.origUniqueID = readBuffer.readUInt();
50 record.paint = readBuffer.readPaint();
51 readBuffer.readPoint(&record.offset);
52 record.blob = SkTextBlobPriv::MakeFromBuffer(readBuffer);
53 trace.push_back(std::move(record));
54 }
55 return trace;
56}
57
58void SkTextBlobTrace::DumpTrace(const std::vector<SkTextBlobTrace::Record>& trace) {
59 for (const SkTextBlobTrace::Record& record : trace) {
60 const SkTextBlob* blob = record.blob.get();
61 const SkPaint& p = record.paint;
62 bool weirdPaint = p.getStyle() != SkPaint::kFill_Style
63 || p.getMaskFilter() != nullptr
64 || p.getPathEffect() != nullptr;
65
66 SkDebugf("Blob %u ( %g %g ) %d\n ",
67 blob->uniqueID(), record.offset.x(), record.offset.y(), weirdPaint);
68 SkTextBlobRunIterator iter(blob);
69 int runNumber = 0;
70 while (!iter.done()) {
71 SkDebugf("Run %d\n ", runNumber);
72 SkFont font = iter.font();
73 SkDebugf("Font %u %g %g %g %d %d %d\n ",
74 font.getTypeface()->uniqueID(),
75 font.getSize(),
76 font.getScaleX(),
77 font.getSkewX(),
79 (int)font.getEdging(),
80 (int)font.getHinting());
81 uint32_t glyphCount = iter.glyphCount();
82 const uint16_t* glyphs = iter.glyphs();
83 for (uint32_t i = 0; i < glyphCount; i++) {
84 SkDebugf("%02X ", glyphs[i]);
85 }
86 SkDebugf("\n");
87 runNumber += 1;
88 iter.next();
89 }
90 }
91}
92
93SkTextBlobTrace::Capture::Capture() : fTypefaceSet(new SkRefCntSet), fWriteBuffer({}) {
94 fWriteBuffer.setTypefaceRecorder(fTypefaceSet);
95}
96
98
100 const sktext::GlyphRunList& glyphRunList, const SkPaint& paint) {
101 const SkTextBlob* blob = glyphRunList.blob();
102 if (blob != nullptr) {
103 fWriteBuffer.writeUInt(blob->uniqueID());
104 fWriteBuffer.writePaint(paint);
105 fWriteBuffer.writePoint(glyphRunList.origin());
106 SkTextBlobPriv::Flatten(*blob, fWriteBuffer);
107 fBlobCount++;
108 }
109}
110
112 SkTLazy<SkFILEWStream> fileStream;
113 if (!dst) {
114 uint32_t id = SkChecksum::Mix(reinterpret_cast<uintptr_t>(this));
115 SkString f = SkStringPrintf("diff-canvas-%08x-%04zu.trace", id, fBlobCount);
116 dst = fileStream.init(f.c_str());
117 if (!fileStream->isValid()) {
118 SkDebugf("Error opening '%s'.\n", f.c_str());
119 return;
120 }
121 SkDebugf("Saving trace to '%s'.\n", f.c_str());
122 }
123 SkASSERT(dst);
124 int count = fTypefaceSet->count();
125 dst->write32(count);
126 SkPtrSet::Iter iter(*fTypefaceSet);
127 while (void* ptr = iter.next()) {
128 ((const SkTypeface*)ptr)->serialize(dst, SkTypeface::SerializeBehavior::kDoIncludeData);
129 }
130 dst->write32(fWriteBuffer.bytesWritten());
131 fWriteBuffer.writeToStream(dst);
132}
uint16_t glyphs[5]
int count
#define SkASSERT(cond)
Definition SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
static sk_sp< SkData > MakeFromStream(SkStream *, size_t size)
Definition SkData.cpp:208
static uint8_t Flags(const SkFont &font)
Definition SkFontPriv.h:90
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
T * init(Args &&... args)
Definition SkTLazy.h:45
bool isValid() const
Definition SkTLazy.h:77
static void Flatten(const SkTextBlob &, SkWriteBuffer &)
static sk_sp< SkTextBlob > MakeFromBuffer(SkReadBuffer &)
const uint16_t * glyphs() const
uint32_t glyphCount() const
const SkFont & font() const
void capture(const sktext::GlyphRunList &, const SkPaint &)
void dump(SkWStream *dst=nullptr) const
uint32_t uniqueID() const
Definition SkTextBlob.h:59
static sk_sp< SkTypeface > MakeDeserialize(SkStream *, sk_sp< SkFontMgr > lastResortMgr)
const SkTextBlob * blob() const
Definition GlyphRun.h:117
SkPoint origin() const
Definition GlyphRun.h:114
const Paint & paint
static uint32_t Mix(uint32_t hash)
Definition SkChecksum.h:30
std::vector< SkTextBlobTrace::Record > CreateBlobTrace(SkStream *stream, sk_sp< SkFontMgr > lastResortMgr)
void DumpTrace(const std::vector< SkTextBlobTrace::Record > &)
sk_sp< SkTextBlob > blob