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