aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkRemoteGlyphCache.h
blob: 3dfa8c28e54950e41e5358de7936d6b5f0ca7b4d (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright 2018 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkRemoteGlyphCache_DEFINED
#define SkRemoteGlyphCache_DEFINED

#include <memory>
#include <tuple>
#include <unordered_map>
#include <vector>

#include "SkData.h"
#include "SkDescriptor.h"
#include "SkDrawLooper.h"
#include "SkGlyphCache.h"
#include "SkMakeUnique.h"
#include "SkNoDrawCanvas.h"
#include "SkSerialProcs.h"
#include "SkTextBlobRunIterator.h"
#include "SkTHash.h"
#include "SkTypeface.h"
#include "SkTypeface_remote.h"

class SkScalerContextRecDescriptor;

class SkRemoteStrikeTransport {
public:
    enum IOResult : bool {kFail = false, kSuccess = true};

    virtual ~SkRemoteStrikeTransport() {}
    virtual IOResult write(const void*, size_t) = 0;
    virtual std::tuple<size_t, IOResult> read(void*, size_t) = 0;
    IOResult writeSkData(const SkData&);
    sk_sp<SkData> readSkData();
    IOResult writeVector(const std::vector<uint8_t>&);
    IOResult readVector(std::vector<uint8_t>*);
};

class SkScalerContextRecDescriptor {
public:
    SkScalerContextRecDescriptor() {}
    explicit SkScalerContextRecDescriptor(const SkScalerContextRec& rec) {
        auto desc = reinterpret_cast<SkDescriptor*>(&fDescriptor);
        desc->init();
        desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
        desc->computeChecksum();
        SkASSERT(sizeof(fDescriptor) == desc->getLength());
    }

    explicit SkScalerContextRecDescriptor(const SkDescriptor& desc)
            : SkScalerContextRecDescriptor(ExtractRec(desc)) { }

    SkScalerContextRecDescriptor& operator=(const SkScalerContextRecDescriptor& rhs) {
        std::memcpy(&fDescriptor, &rhs.fDescriptor, rhs.desc().getLength());
        return *this;
    }

    const SkDescriptor& desc() const {
        return *reinterpret_cast<const SkDescriptor*>(&fDescriptor);
    }

    struct Hash {
        uint32_t operator()(SkScalerContextRecDescriptor const& s) const {
            return s.desc().getChecksum();
        }
    };

    friend bool operator==(const SkScalerContextRecDescriptor& lhs,
                           const SkScalerContextRecDescriptor& rhs ) {
        return lhs.desc() == rhs.desc();
    }

private:
    static SkScalerContextRec ExtractRec(const SkDescriptor& desc) {
        uint32_t size;
        auto recPtr = desc.findEntry(kRec_SkDescriptorTag, &size);

        SkScalerContextRec result;
        std::memcpy(&result, recPtr, size);
        return result;
    }
    // The system only passes descriptors without effects. That is why it uses a fixed size
    // descriptor. storageFor is needed because some of the constructors below are private.
    template <typename T>
    using storageFor = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
    struct {
        storageFor<SkDescriptor>        dummy1;
        storageFor<SkDescriptor::Entry> dummy2;
        storageFor<SkScalerContextRec>  dummy3;
    } fDescriptor;
};

class SkStrikeCacheDifferenceSpec {
    class StrikeDifferences;

public:
    StrikeDifferences& findStrikeDifferences(const SkDescriptor& desc, SkFontID typefaceID);
    int strikeCount() const { return fDescriptorToDifferencesMap.size(); }
    size_t sizeBytes() const;
    template <typename PerStrike, typename PerGlyph>
    void iterateDifferences(PerStrike perStrike, PerGlyph perGlyph) const;

private:
    class StrikeDifferences {
    public:
        StrikeDifferences(SkFontID typefaceID, std::unique_ptr<SkDescriptor> desc);
        void operator()(uint16_t glyphID, SkIPoint pos);
        SkFontID fTypefaceID;
        std::unique_ptr<SkDescriptor> fDesc;
        std::unique_ptr<SkTHashSet<SkPackedGlyphID>> fGlyphIDs =
                skstd::make_unique<SkTHashSet<SkPackedGlyphID>>();
    };

    struct DescHash {
        size_t operator()(const SkDescriptor* key) const {
            return key->getChecksum();
        }
    };

    struct DescEq {
        bool operator()(const SkDescriptor* lhs, const SkDescriptor* rhs) const {
            return lhs->getChecksum() == rhs->getChecksum();
        }
    };

    using DescMap = std::unordered_map<const SkDescriptor*, StrikeDifferences, DescHash, DescEq>;
    DescMap fDescriptorToDifferencesMap{16, DescHash(), DescEq()};
};

class SkTextBlobCacheDiffCanvas : public SkNoDrawCanvas {
public:
    SkTextBlobCacheDiffCanvas(int width, int height,
                              const SkMatrix& deviceMatrix,
                              const SkSurfaceProps& props,
                              SkScalerContextFlags flags,
                              SkStrikeCacheDifferenceSpec* strikeDiffs);

protected:
    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec& rec) override;

    void onDrawTextBlob(
            const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint) override;

private:
    void processLooper(
            const SkPoint& position,
            const SkTextBlobRunIterator& it,
            const SkPaint& origPaint,
            SkDrawLooper* looper);

    void processGlyphRun(
            const SkPoint& position,
            const SkTextBlobRunIterator& it,
            const SkPaint& runPaint);

    const SkMatrix fDeviceMatrix;
    const SkSurfaceProps fSurfaceProps;
    const SkScalerContextFlags fScalerContextFlags;

    SkStrikeCacheDifferenceSpec* const fStrikeCacheDiff;
};

class SkStrikeServer {
public:
    SkStrikeServer(SkRemoteStrikeTransport* transport);
    ~SkStrikeServer();

    // embedding clients call these methods
    int serve();  // very negotiable
    void prepareSerializeProcs(SkSerialProcs* procs);

    // mostly called internally by Skia
    SkScalerContext* generateScalerContext(
            const SkScalerContextRecDescriptor& desc, SkFontID typefaceId);

private:
    using DescriptorToContextMap = SkTHashMap<SkScalerContextRecDescriptor,
            std::unique_ptr<SkScalerContext>,
            SkScalerContextRecDescriptor::Hash>;

    sk_sp<SkData> encodeTypeface(SkTypeface* tf);

    int fOpCount = 0;
    SkRemoteStrikeTransport* const fTransport;
    SkTHashMap<SkFontID, sk_sp<SkTypeface>> fTypefaceMap;
    DescriptorToContextMap fScalerContextMap;
};

class SkStrikeClient {
public:
    SkStrikeClient(SkRemoteStrikeTransport*);

    // embedding clients call these methods
    void primeStrikeCache(const SkStrikeCacheDifferenceSpec&);
    void prepareDeserializeProcs(SkDeserialProcs* procs);

    // mostly called internally by Skia
    void generateFontMetrics(
        const SkTypefaceProxy&, const SkScalerContextRec&, SkPaint::FontMetrics*);
    void generateMetricsAndImage(
        const SkTypefaceProxy&, const SkScalerContextRec&, SkArenaAlloc*, SkGlyph*);
    bool generatePath(
        const SkTypefaceProxy&, const SkScalerContextRec&, SkGlyphID glyph, SkPath* path);
    SkTypeface* lookupTypeface(SkFontID id);

private:
    sk_sp<SkTypeface> decodeTypeface(const void* buf, size_t len);

    // TODO: Figure out how to manage the entries for the following maps.
    SkTHashMap<SkFontID, sk_sp<SkTypefaceProxy>> fMapIdToTypeface;

    SkRemoteStrikeTransport* const fTransport;

    std::vector<uint8_t> fBuffer;
};

#endif  // SkRemoteGlyphCache_DEFINED