1/*
2 * Copyright 2016 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
8#ifndef SkPipeCanvas_DEFINED
9#define SkPipeCanvas_DEFINED
10
11#include "SkDeduper.h"
12#include "SkImage.h"
13#include "SkNoDrawCanvas.h"
14#include "SkPipe.h"
15#include "SkTypeface.h"
16#include "SkWriteBuffer.h"
17
18class SkPipeCanvas;
19class SkPipeWriter;
20
21template <typename T> class SkTIndexSet {
22public:
23    void reset() { fArray.reset(); }
24
25    // returns the found index or 0
26    int find(const T& key) const {
27        const Rec* stop = fArray.end();
28        for (const Rec* curr = fArray.begin(); curr < stop; ++curr) {
29            if (key == curr->fKey) {
30                return curr->fIndex;
31            }
32        }
33        return 0;
34    }
35
36    // returns the new index
37    int add(const T& key) {
38        Rec* rec = fArray.append();
39        rec->fKey = key;
40        rec->fIndex = fNextIndex++;
41        return rec->fIndex;
42    }
43
44private:
45    struct Rec {
46        T   fKey;
47        int fIndex;
48    };
49
50    SkTDArray<Rec>  fArray;
51    int fNextIndex = 1;
52};
53
54class SkPipeDeduper : public SkDeduper {
55public:
56    void resetCaches() {
57        fImages.reset();
58        fPictures.reset();
59        fTypefaces.reset();
60        fFactories.reset();
61    }
62
63    void setCanvas(SkPipeCanvas* canvas) { fPipeCanvas = canvas; }
64    void setStream(SkWStream* stream) { fStream = stream; }
65    void setSerialProcs(const SkSerialProcs& procs) { fProcs = procs; }
66
67    // returns 0 if not found
68    int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); }
69    int findPicture(SkPicture* picture) const { return fPictures.find(picture->uniqueID()); }
70
71    int findOrDefineImage(SkImage*) override;
72    int findOrDefinePicture(SkPicture*) override;
73    int findOrDefineTypeface(SkTypeface*) override;
74    int findOrDefineFactory(SkFlattenable*) override;
75
76private:
77    SkPipeCanvas*           fPipeCanvas = nullptr;
78    SkWStream*              fStream = nullptr;
79    SkSerialProcs           fProcs;
80
81    // All our keys (at the moment) are 32bit uniqueIDs
82    SkTIndexSet<uint32_t>   fImages;
83    SkTIndexSet<uint32_t>   fPictures;
84    SkTIndexSet<uint32_t>   fTypefaces;
85    SkTIndexSet<SkFlattenable::Factory> fFactories;
86};
87
88
89class SkPipeCanvas : public SkNoDrawCanvas {
90public:
91    SkPipeCanvas(const SkRect& cull, SkPipeDeduper*, SkWStream*);
92    ~SkPipeCanvas() override;
93
94protected:
95    void willSave() override;
96    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
97    void willRestore() override;
98
99    void didConcat(const SkMatrix&) override;
100    void didSetMatrix(const SkMatrix&) override;
101
102    void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
103                   const SkPaint&) override;
104    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
105                     int count, SkBlendMode, const SkRect* cull, const SkPaint*) override;
106    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
107    void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
108                    const SkPaint&) override;
109    void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
110                       const SkPaint&) override;
111    void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
112                        SkScalar constY, const SkPaint&) override;
113    void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath&, const SkMatrix*,
114                          const SkPaint&) override;
115    void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint&) override;
116    void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
117                           const SkRect* cull, const SkPaint& paint) override;
118    void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
119                     SkBlendMode, const SkPaint&) override;
120
121    void onDrawPaint(const SkPaint&) override;
122    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
123    void onDrawRect(const SkRect&, const SkPaint&) override;
124    void onDrawOval(const SkRect&, const SkPaint&) override;
125    void onDrawRegion(const SkRegion&, const SkPaint&) override;
126    void onDrawRRect(const SkRRect&, const SkPaint&) override;
127    void onDrawPath(const SkPath&, const SkPaint&) override;
128    void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
129
130    void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
131    void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
132                         const SkPaint*, SrcRectConstraint) override;
133    void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
134                         const SkPaint*) override;
135    void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
136                            const SkPaint*) override;
137    void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
138
139    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
140    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
141    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
142    void onClipRegion(const SkRegion&, SkClipOp) override;
143
144    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
145    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
146
147    // These we turn into images
148    void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
149    void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
150                          SrcRectConstraint) override;
151    void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
152                          const SkPaint*) override;
153    void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
154                             const SkPaint*) override;
155
156private:
157    SkPipeDeduper*  fDeduper;
158    SkWStream*      fStream;
159
160    friend class SkPipeWriter;
161
162    typedef SkNoDrawCanvas INHERITED;
163};
164
165
166#endif
167