SkPipeCanvas.h revision 7e3ba9f88d64d901243799b61f497319e5970693
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 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 setTypefaceSerializer(SkTypefaceSerializer* tfs) { fTFSerializer = tfs; } 66 67 // returns 0 if not found 68 int findImage(SkImage* image) const { return fImages.find(image->uniqueID()); } 69 70 int findOrDefineImage(SkImage*) override; 71 int findOrDefinePicture(SkPicture*) override; 72 int findOrDefineTypeface(SkTypeface*) override; 73 int findOrDefineFactory(SkFlattenable*) override; 74 75private: 76 SkPipeCanvas* fPipeCanvas = nullptr; 77 SkWStream* fStream = nullptr; 78 79 SkTypefaceSerializer* fTFSerializer = nullptr; 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 SkCanvas { 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, SkXfermode::Mode, 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 SkXfermode*, 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 129 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; 130 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, 131 const SkPaint*, SrcRectConstraint) override; 132 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, 133 const SkPaint*) override; 134 void onDrawImageLattice(const SkImage*, const Lattice& lattice, const SkRect& dst, 135 const SkPaint*) override; 136 void onDrawVertices(VertexMode vmode, int vertexCount, 137 const SkPoint vertices[], const SkPoint texs[], 138 const SkColor colors[], SkXfermode* xmode, 139 const uint16_t indices[], int indexCount, 140 const SkPaint&) override; 141 142 void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) override; 143 void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) override; 144 void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) override; 145 void onClipRegion(const SkRegion&, SkRegion::Op) override; 146 147 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 148 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 149 150 // These we turn into images 151 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override; 152 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*, 153 SrcRectConstraint) override; 154 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, 155 const SkPaint*) override; 156 void onDrawBitmapLattice(const SkBitmap&, const Lattice& lattice, const SkRect& dst, 157 const SkPaint*) override; 158 159private: 160 SkPipeDeduper* fDeduper; 161 SkWStream* fStream; 162 163 friend class SkPipeWriter; 164 165 typedef SkCanvas INHERITED; 166}; 167 168 169#endif 170