blob: 5604ba787a284197b7350f03f088e9140246fe07 [file] [log] [blame]
reed54dc4872016-09-13 08:09:45 -07001/*
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
reed54dc4872016-09-13 08:09:45 -070011#include "SkDeduper.h"
12#include "SkImage.h"
Florin Malita439ace92016-12-02 12:05:41 -050013#include "SkNoDrawCanvas.h"
reed54dc4872016-09-13 08:09:45 -070014#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
reed7e3ba9f2016-09-13 17:25:19 -070026 int find(const T& key) const {
reed54dc4872016-09-13 08:09:45 -070027 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 setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; }
Mike Reed3ac64b42016-10-18 19:34:08 -040066 void setImageSerializer(SkImageSerializer* ims) { fIMSerializer = ims; }
reed54dc4872016-09-13 08:09:45 -070067
reed7e3ba9f2016-09-13 17:25:19 -070068 // returns 0 if not found
69 int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); }
reed262052c2016-09-15 14:24:53 -070070 int findPicture(SkPicture* picture) const { return fPictures.find(picture->uniqueID()); }
reed7e3ba9f2016-09-13 17:25:19 -070071
reed54dc4872016-09-13 08:09:45 -070072 int findOrDefineImage(SkImage*) override;
73 int findOrDefinePicture(SkPicture*) override;
74 int findOrDefineTypeface(SkTypeface*) override;
75 int findOrDefineFactory(SkFlattenable*) override;
76
77private:
78 SkPipeCanvas* fPipeCanvas = nullptr;
79 SkWStream* fStream = nullptr;
80
81 SkTypefaceSerializer* fTFSerializer = nullptr;
Mike Reed3ac64b42016-10-18 19:34:08 -040082 SkImageSerializer* fIMSerializer = nullptr;
reed54dc4872016-09-13 08:09:45 -070083
84 // All our keys (at the moment) are 32bit uniqueIDs
85 SkTIndexSet<uint32_t> fImages;
86 SkTIndexSet<uint32_t> fPictures;
87 SkTIndexSet<uint32_t> fTypefaces;
88 SkTIndexSet<SkFlattenable::Factory> fFactories;
89};
90
91
Florin Malita439ace92016-12-02 12:05:41 -050092class SkPipeCanvas : public SkNoDrawCanvas {
reed54dc4872016-09-13 08:09:45 -070093public:
94 SkPipeCanvas(const SkRect& cull, SkPipeDeduper*, SkWStream*);
95 ~SkPipeCanvas() override;
96
97protected:
98 void willSave() override;
99 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
100 void willRestore() override;
101
102 void didConcat(const SkMatrix&) override;
103 void didSetMatrix(const SkMatrix&) override;
104
105 void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
106 const SkPaint&) override;
107 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[],
Mike Reedfaba3712016-11-03 14:45:31 -0400108 int count, SkBlendMode, const SkRect* cull, const SkPaint*) override;
reed54dc4872016-09-13 08:09:45 -0700109 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
110 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
111 const SkPaint&) override;
112 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
113 const SkPaint&) override;
114 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
115 SkScalar constY, const SkPaint&) override;
116 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath&, const SkMatrix*,
117 const SkPaint&) override;
118 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint&) override;
119 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
120 const SkRect* cull, const SkPaint& paint) override;
121 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4],
Mike Reedfaba3712016-11-03 14:45:31 -0400122 SkBlendMode, const SkPaint&) override;
reed54dc4872016-09-13 08:09:45 -0700123
124 void onDrawPaint(const SkPaint&) override;
125 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
126 void onDrawRect(const SkRect&, const SkPaint&) override;
127 void onDrawOval(const SkRect&, const SkPaint&) override;
128 void onDrawRegion(const SkRegion&, const SkPaint&) override;
129 void onDrawRRect(const SkRRect&, const SkPaint&) override;
130 void onDrawPath(const SkPath&, const SkPaint&) override;
131
132 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override;
133 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst,
134 const SkPaint*, SrcRectConstraint) override;
135 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst,
136 const SkPaint*) override;
137 void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst,
138 const SkPaint*) override;
Mike Reedfed9cfd2017-03-17 12:09:04 -0400139 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override;
reed54dc4872016-09-13 08:09:45 -0700140
Mike Reedc1f77742016-12-09 09:00:50 -0500141 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
142 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
143 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
144 void onClipRegion(const SkRegion&, SkClipOp) override;
reed54dc4872016-09-13 08:09:45 -0700145
146 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
147 void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
148
149 // These we turn into images
150 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override;
151 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*,
152 SrcRectConstraint) override;
153 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst,
154 const SkPaint*) override;
155 void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst,
156 const SkPaint*) override;
157
158private:
159 SkPipeDeduper* fDeduper;
160 SkWStream* fStream;
161
162 friend class SkPipeWriter;
163
Florin Malita439ace92016-12-02 12:05:41 -0500164 typedef SkNoDrawCanvas INHERITED;
reed54dc4872016-09-13 08:09:45 -0700165};
166
167
168#endif