OLD | NEW |
(Empty) | |
| 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 "SkCanvas.h" |
| 12 #include "SkDeduper.h" |
| 13 #include "SkImage.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: |
| 23 void reset() { fArray.reset(); } |
| 24 |
| 25 // returns the found index or 0 |
| 26 int find(const T& key) { |
| 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 |
| 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: |
| 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;
} |
| 66 |
| 67 int findOrDefineImage(SkImage*) override; |
| 68 int findOrDefinePicture(SkPicture*) override; |
| 69 int findOrDefineTypeface(SkTypeface*) override; |
| 70 int findOrDefineFactory(SkFlattenable*) override; |
| 71 |
| 72 private: |
| 73 SkPipeCanvas* fPipeCanvas = nullptr; |
| 74 SkWStream* fStream = nullptr; |
| 75 |
| 76 SkTypefaceSerializer* fTFSerializer = nullptr; |
| 77 |
| 78 // All our keys (at the moment) are 32bit uniqueIDs |
| 79 SkTIndexSet<uint32_t> fImages; |
| 80 SkTIndexSet<uint32_t> fPictures; |
| 81 SkTIndexSet<uint32_t> fTypefaces; |
| 82 SkTIndexSet<SkFlattenable::Factory> fFactories; |
| 83 }; |
| 84 |
| 85 |
| 86 class SkPipeCanvas : public SkCanvas { |
| 87 public: |
| 88 SkPipeCanvas(const SkRect& cull, SkPipeDeduper*, SkWStream*); |
| 89 ~SkPipeCanvas() override; |
| 90 |
| 91 protected: |
| 92 void willSave() override; |
| 93 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; |
| 94 void willRestore() override; |
| 95 |
| 96 void didConcat(const SkMatrix&) override; |
| 97 void didSetMatrix(const SkMatrix&) override; |
| 98 |
| 99 void onDrawArc(const SkRect&, SkScalar startAngle, SkScalar sweepAngle, bool
useCenter, |
| 100 const SkPaint&) override; |
| 101 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const Sk
Color[], |
| 102 int count, SkXfermode::Mode, const SkRect* cull, const SkPa
int*) override; |
| 103 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; |
| 104 void onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 105 const SkPaint&) override; |
| 106 void onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 107 const SkPaint&) override; |
| 108 void onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos
[], |
| 109 SkScalar constY, const SkPaint&) override; |
| 110 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath&, co
nst SkMatrix*, |
| 111 const SkPaint&) override; |
| 112 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const Sk
Paint&) override; |
| 113 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform
xform[], |
| 114 const SkRect* cull, const SkPaint& paint) override; |
| 115 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], const Sk
Point texCoords[4], |
| 116 SkXfermode*, const SkPaint&) override; |
| 117 |
| 118 void onDrawPaint(const SkPaint&) override; |
| 119 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPain
t&) override; |
| 120 void onDrawRect(const SkRect&, const SkPaint&) override; |
| 121 void onDrawOval(const SkRect&, const SkPaint&) override; |
| 122 void onDrawRegion(const SkRegion&, const SkPaint&) override; |
| 123 void onDrawRRect(const SkRRect&, const SkPaint&) override; |
| 124 void onDrawPath(const SkPath&, const SkPaint&) override; |
| 125 |
| 126 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*
) override; |
| 127 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, |
| 128 const SkPaint*, SrcRectConstraint) override; |
| 129 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& ds
t, |
| 130 const SkPaint*) override; |
| 131 void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect
& dst, |
| 132 const SkPaint*) override; |
| 133 void onDrawVertices(VertexMode vmode, int vertexCount, |
| 134 const SkPoint vertices[], const SkPoint texs[], |
| 135 const SkColor colors[], SkXfermode* xmode, |
| 136 const uint16_t indices[], int indexCount, |
| 137 const SkPaint&) override; |
| 138 |
| 139 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override; |
| 140 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override; |
| 141 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override; |
| 142 void onClipRegion(const SkRegion&, SkRegion::Op) override; |
| 143 |
| 144 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) overri
de; |
| 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 SkPain
t*) 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 SkRe
ct& dst, |
| 154 const SkPaint*) override; |
| 155 |
| 156 private: |
| 157 SkPipeDeduper* fDeduper; |
| 158 SkWStream* fStream; |
| 159 |
| 160 friend class SkPipeWriter; |
| 161 |
| 162 typedef SkCanvas INHERITED; |
| 163 }; |
| 164 |
| 165 |
| 166 #endif |
OLD | NEW |