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 18 class SkPipeCanvas; 19 class SkPipeWriter; 20 21 template <typename T> class SkTIndexSet { 22 public: reset()23 void reset() { fArray.reset(); } 24 25 // returns the found index or 0 find(const T & key)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 add(const T & key)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 44 private: 45 struct Rec { 46 T fKey; 47 int fIndex; 48 }; 49 50 SkTDArray<Rec> fArray; 51 int fNextIndex = 1; 52 }; 53 54 class SkPipeDeduper : public SkDeduper { 55 public: resetCaches()56 void resetCaches() { 57 fImages.reset(); 58 fPictures.reset(); 59 fTypefaces.reset(); 60 fFactories.reset(); 61 } 62 setCanvas(SkPipeCanvas * canvas)63 void setCanvas(SkPipeCanvas* canvas) { fPipeCanvas = canvas; } setStream(SkWStream * stream)64 void setStream(SkWStream* stream) { fStream = stream; } setTypefaceSerializer(SkTypefaceSerializer * tfs)65 void setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; } setImageSerializer(SkImageSerializer * ims)66 void setImageSerializer(SkImageSerializer* ims) { fIMSerializer = ims; } 67 68 // returns 0 if not found findImage(SkImage * image)69 int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); } findPicture(SkPicture * picture)70 int findPicture(SkPicture* picture) const { return fPictures.find(picture->uniqueID()); } 71 72 int findOrDefineImage(SkImage*) override; 73 int findOrDefinePicture(SkPicture*) override; 74 int findOrDefineTypeface(SkTypeface*) override; 75 int findOrDefineFactory(SkFlattenable*) override; 76 77 private: 78 SkPipeCanvas* fPipeCanvas = nullptr; 79 SkWStream* fStream = nullptr; 80 81 SkTypefaceSerializer* fTFSerializer = nullptr; 82 SkImageSerializer* fIMSerializer = nullptr; 83 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 92 class SkPipeCanvas : public SkNoDrawCanvas { 93 public: 94 SkPipeCanvas(const SkRect& cull, SkPipeDeduper*, SkWStream*); 95 ~SkPipeCanvas() override; 96 97 protected: 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[], 108 int count, SkBlendMode, const SkRect* cull, const SkPaint*) override; 109 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], 122 SkBlendMode, const SkPaint&) override; 123 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; 139 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 140 141 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; 145 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 158 private: 159 SkPipeDeduper* fDeduper; 160 SkWStream* fStream; 161 162 friend class SkPipeWriter; 163 164 typedef SkNoDrawCanvas INHERITED; 165 }; 166 167 168 #endif 169