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