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