1 /* 2 * Copyright 2011 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 SkPictureRecord_DEFINED 9 #define SkPictureRecord_DEFINED 10 11 #include "SkCanvas.h" 12 #include "SkFlattenable.h" 13 #include "SkPicture.h" 14 #include "SkPictureData.h" 15 #include "SkTArray.h" 16 #include "SkTDArray.h" 17 #include "SkTHash.h" 18 #include "SkVertices.h" 19 #include "SkWriter32.h" 20 21 // These macros help with packing and unpacking a single byte value and 22 // a 3 byte value into/out of a uint32_t 23 #define MASK_24 0x00FFFFFF 24 #define UNPACK_8_24(combined, small, large) \ 25 small = (combined >> 24) & 0xFF; \ 26 large = combined & MASK_24; 27 #define PACK_8_24(small, large) ((small << 24) | large) 28 29 30 class SkPictureRecord : public SkCanvas { 31 public: 32 SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags); 33 ~SkPictureRecord() override; 34 35 const SkTDArray<const SkPicture* >& getPictureRefs() const { 36 return fPictureRefs; 37 } 38 39 const SkTDArray<SkDrawable* >& getDrawableRefs() const { 40 return fDrawableRefs; 41 } 42 43 const SkTDArray<const SkTextBlob* >& getTextBlobRefs() const { 44 return fTextBlobRefs; 45 } 46 47 const SkTDArray<const SkVertices* >& getVerticesRefs() const { 48 return fVerticesRefs; 49 } 50 51 const SkTDArray<const SkImage* >& getImageRefs() const { 52 return fImageRefs; 53 } 54 55 sk_sp<SkData> opData() const { 56 this->validate(fWriter.bytesWritten(), 0); 57 58 if (fWriter.bytesWritten() == 0) { 59 return SkData::MakeEmpty(); 60 } 61 return fWriter.snapshotAsData(); 62 } 63 64 const SkPictureContentInfo& contentInfo() const { 65 return fContentInfo; 66 } 67 68 void setFlags(uint32_t recordFlags) { 69 fRecordFlags = recordFlags; 70 } 71 72 const SkWriter32& writeStream() const { 73 return fWriter; 74 } 75 76 void beginRecording(); 77 void endRecording(); 78 79 protected: 80 void addNoOp(); 81 82 private: 83 void handleOptimization(int opt); 84 size_t recordRestoreOffsetPlaceholder(SkClipOp); 85 void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset); 86 87 SkTDArray<int32_t> fRestoreOffsetStack; 88 89 SkTDArray<uint32_t> fCullOffsetStack; 90 91 /* 92 * Write the 'drawType' operation and chunk size to the skp. 'size' 93 * can potentially be increased if the chunk size needs its own storage 94 * location (i.e., it overflows 24 bits). 95 * Returns the start offset of the chunk. This is the location at which 96 * the opcode & size are stored. 97 * TODO: since we are handing the size into here we could call reserve 98 * and then return a pointer to the memory storage. This could decrease 99 * allocation overhead but could lead to more wasted space (the tail 100 * end of blocks could go unused). Possibly add a second addDraw that 101 * operates in this manner. 102 */ 103 size_t addDraw(DrawType drawType, size_t* size) { 104 size_t offset = fWriter.bytesWritten(); 105 106 this->predrawNotify(); 107 fContentInfo.addOperation(); 108 109 SkASSERT(0 != *size); 110 SkASSERT(((uint8_t) drawType) == drawType); 111 112 if (0 != (*size & ~MASK_24) || *size == MASK_24) { 113 fWriter.writeInt(PACK_8_24(drawType, MASK_24)); 114 *size += 1; 115 fWriter.writeInt(SkToU32(*size)); 116 } else { 117 fWriter.writeInt(PACK_8_24(drawType, SkToU32(*size))); 118 } 119 120 return offset; 121 } 122 123 void addInt(int value) { 124 fWriter.writeInt(value); 125 } 126 void addScalar(SkScalar scalar) { 127 fWriter.writeScalar(scalar); 128 } 129 130 void addImage(const SkImage*); 131 void addMatrix(const SkMatrix& matrix); 132 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); } 133 void addPaintPtr(const SkPaint* paint); 134 void addPatch(const SkPoint cubics[12]); 135 void addPath(const SkPath& path); 136 void addPicture(const SkPicture* picture); 137 void addDrawable(SkDrawable* picture); 138 void addPoint(const SkPoint& point); 139 void addPoints(const SkPoint pts[], int count); 140 void addRect(const SkRect& rect); 141 void addRectPtr(const SkRect* rect); 142 void addIRect(const SkIRect& rect); 143 void addIRectPtr(const SkIRect* rect); 144 void addRRect(const SkRRect&); 145 void addRegion(const SkRegion& region); 146 void addText(const void* text, size_t byteLength); 147 void addTextBlob(const SkTextBlob* blob); 148 void addVertices(const SkVertices*); 149 150 int find(const SkBitmap& bitmap); 151 152 protected: 153 void validate(size_t initialOffset, size_t size) const { 154 SkASSERT(fWriter.bytesWritten() == initialOffset + size); 155 } 156 157 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override; 158 bool onPeekPixels(SkPixmap*) override { return false; } 159 160 void willSave() override; 161 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 162 void willRestore() override; 163 164 void didConcat(const SkMatrix&) override; 165 void didSetMatrix(const SkMatrix&) override; 166 167 #ifdef SK_EXPERIMENTAL_SHADOWING 168 void didTranslateZ(SkScalar) override; 169 #else 170 void didTranslateZ(SkScalar); 171 #endif 172 173 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 174 175 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override; 176 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override; 177 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY, 178 const SkPaint&) override; 179 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 180 const SkMatrix* matrix, const SkPaint&) override; 181 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], 182 const SkRect* cull, const SkPaint&) override; 183 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 184 const SkPaint& paint) override; 185 186 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 187 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override; 188 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, 189 SkBlendMode, const SkRect*, const SkPaint*) override; 190 191 void onDrawPaint(const SkPaint&) override; 192 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 193 void onDrawRect(const SkRect&, const SkPaint&) override; 194 void onDrawRegion(const SkRegion&, const SkPaint&) override; 195 void onDrawOval(const SkRect&, const SkPaint&) override; 196 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 197 void onDrawRRect(const SkRRect&, const SkPaint&) override; 198 void onDrawPath(const SkPath&, const SkPaint&) override; 199 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; 200 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, 201 const SkPaint*, SrcRectConstraint) override; 202 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, 203 const SkPaint*) override; 204 void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst, 205 const SkPaint*) override; 206 void onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint&) override; 207 208 void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override; 209 void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override; 210 void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override; 211 void onClipRegion(const SkRegion&, SkClipOp) override; 212 213 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 214 215 #ifdef SK_EXPERIMENTAL_SHADOWING 216 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*, 217 const SkPaint*, const SkShadowParams& params) override; 218 #else 219 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*, 220 const SkPaint*, const SkShadowParams& params); 221 #endif 222 223 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 224 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 225 226 int addPathToHeap(const SkPath& path); // does not write to ops stream 227 228 // These entry points allow the writing of matrices, clips, saves & 229 // restores to be deferred (e.g., if the MC state is being collapsed and 230 // only written out as needed). 231 void recordConcat(const SkMatrix& matrix); 232 void recordTranslate(const SkMatrix& matrix); 233 void recordScale(const SkMatrix& matrix); 234 size_t recordClipRect(const SkRect& rect, SkClipOp op, bool doAA); 235 size_t recordClipRRect(const SkRRect& rrect, SkClipOp op, bool doAA); 236 size_t recordClipPath(int pathID, SkClipOp op, bool doAA); 237 size_t recordClipRegion(const SkRegion& region, SkClipOp op); 238 void recordSave(); 239 void recordSaveLayer(const SaveLayerRec&); 240 void recordRestore(bool fillInSkips = true); 241 242 // SHOULD NEVER BE CALLED 243 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override { 244 sk_throw(); 245 } 246 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*, 247 SrcRectConstraint) override { 248 sk_throw(); 249 } 250 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, 251 const SkPaint*) override { 252 sk_throw(); 253 } 254 void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst, 255 const SkPaint*) override { 256 sk_throw(); 257 } 258 259 private: 260 SkPictureContentInfo fContentInfo; 261 262 SkTArray<SkPaint> fPaints; 263 264 struct PathHash { 265 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); } 266 }; 267 SkTHashMap<SkPath, int, PathHash> fPaths; 268 269 SkWriter32 fWriter; 270 271 // we ref each item in these arrays 272 SkTDArray<const SkImage*> fImageRefs; 273 SkTDArray<const SkPicture*> fPictureRefs; 274 SkTDArray<SkDrawable*> fDrawableRefs; 275 SkTDArray<const SkTextBlob*> fTextBlobRefs; 276 SkTDArray<const SkVertices*> fVerticesRefs; 277 278 uint32_t fRecordFlags; 279 int fInitialSaveCount; 280 281 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor 282 283 typedef SkCanvas INHERITED; 284 }; 285 286 #endif 287