SkPictureRecord.h revision a129dfef2aaab0b5995cdf1ab7b2cdd41c29cf72
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(SkCanvas::ClipOp); 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 addImage(const SkImage*); 126 void addMatrix(const SkMatrix& matrix); 127 void addPaint(const SkPaint& paint) { this->addPaintPtr(&paint); } 128 void addPaintPtr(const SkPaint* paint); 129 void addPatch(const SkPoint cubics[12]); 130 void addPath(const SkPath& path); 131 void addPicture(const SkPicture* picture); 132 void addDrawable(SkDrawable* picture); 133 void addPoint(const SkPoint& point); 134 void addPoints(const SkPoint pts[], int count); 135 void addRect(const SkRect& rect); 136 void addRectPtr(const SkRect* rect); 137 void addIRect(const SkIRect& rect); 138 void addIRectPtr(const SkIRect* rect); 139 void addRRect(const SkRRect&); 140 void addRegion(const SkRegion& region); 141 void addText(const void* text, size_t byteLength); 142 void addTextBlob(const SkTextBlob* blob); 143 144 int find(const SkBitmap& bitmap); 145 146protected: 147 void validate(size_t initialOffset, size_t size) const { 148 SkASSERT(fWriter.bytesWritten() == initialOffset + size); 149 } 150 151 sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override; 152 bool onPeekPixels(SkPixmap*) override { return false; } 153 154 void willSave() override; 155 SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override; 156 void willRestore() override; 157 158 void didConcat(const SkMatrix&) override; 159 void didSetMatrix(const SkMatrix&) override; 160 161#ifdef SK_EXPERIMENTAL_SHADOWING 162 void didTranslateZ(SkScalar) override; 163#else 164 void didTranslateZ(SkScalar); 165#endif 166 167 void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override; 168 169 void onDrawText(const void* text, size_t, SkScalar x, SkScalar y, const SkPaint&) override; 170 void onDrawPosText(const void* text, size_t, const SkPoint pos[], const SkPaint&) override; 171 void onDrawPosTextH(const void* text, size_t, const SkScalar xpos[], SkScalar constY, 172 const SkPaint&) override; 173 void onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 174 const SkMatrix* matrix, const SkPaint&) override; 175 void onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], 176 const SkRect* cull, const SkPaint&) override; 177 void onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, 178 const SkPaint& paint) override; 179 180 void onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], 181 const SkPoint texCoords[4], SkBlendMode, const SkPaint& paint) override; 182 void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int, 183 SkBlendMode, const SkRect*, const SkPaint*) override; 184 185 void onDrawPaint(const SkPaint&) override; 186 void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override; 187 void onDrawRect(const SkRect&, const SkPaint&) override; 188 void onDrawRegion(const SkRegion&, const SkPaint&) override; 189 void onDrawOval(const SkRect&, const SkPaint&) override; 190 void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override; 191 void onDrawRRect(const SkRRect&, const SkPaint&) override; 192 void onDrawPath(const SkPath&, const SkPaint&) override; 193 void onDrawImage(const SkImage*, SkScalar left, SkScalar top, const SkPaint*) override; 194 void onDrawImageRect(const SkImage*, const SkRect* src, const SkRect& dst, 195 const SkPaint*, SrcRectConstraint) override; 196 void onDrawImageNine(const SkImage*, const SkIRect& center, const SkRect& dst, 197 const SkPaint*) override; 198 void onDrawImageLattice(const SkImage*, const SkCanvas::Lattice& lattice, const SkRect& dst, 199 const SkPaint*) override; 200 201 void onDrawVertices(VertexMode vmode, int vertexCount, 202 const SkPoint vertices[], const SkPoint texs[], 203 const SkColor colors[], SkBlendMode, 204 const uint16_t indices[], int indexCount, 205 const SkPaint&) override; 206 207 void onClipRect(const SkRect&, ClipOp, ClipEdgeStyle) override; 208 void onClipRRect(const SkRRect&, ClipOp, ClipEdgeStyle) override; 209 void onClipPath(const SkPath&, ClipOp, ClipEdgeStyle) override; 210 void onClipRegion(const SkRegion&, ClipOp) override; 211 212 void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override; 213 214#ifdef SK_EXPERIMENTAL_SHADOWING 215 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*, 216 const SkPaint*, const SkShadowParams& params) override; 217#else 218 void onDrawShadowedPicture(const SkPicture*, const SkMatrix*, 219 const SkPaint*, const SkShadowParams& params); 220#endif 221 222 void onDrawDrawable(SkDrawable*, const SkMatrix*) override; 223 void onDrawAnnotation(const SkRect&, const char[], SkData*) override; 224 225 int addPathToHeap(const SkPath& path); // does not write to ops stream 226 227 // These entry points allow the writing of matrices, clips, saves & 228 // restores to be deferred (e.g., if the MC state is being collapsed and 229 // only written out as needed). 230 void recordConcat(const SkMatrix& matrix); 231 void recordTranslate(const SkMatrix& matrix); 232 void recordScale(const SkMatrix& matrix); 233 size_t recordClipRect(const SkRect& rect, SkCanvas::ClipOp op, bool doAA); 234 size_t recordClipRRect(const SkRRect& rrect, SkCanvas::ClipOp op, bool doAA); 235 size_t recordClipPath(int pathID, SkCanvas::ClipOp op, bool doAA); 236 size_t recordClipRegion(const SkRegion& region, SkCanvas::ClipOp op); 237 void recordSave(); 238 void recordSaveLayer(const SaveLayerRec&); 239 void recordRestore(bool fillInSkips = true); 240 241 // SHOULD NEVER BE CALLED 242 void onDrawBitmap(const SkBitmap&, SkScalar left, SkScalar top, const SkPaint*) override { 243 sk_throw(); 244 } 245 void onDrawBitmapRect(const SkBitmap&, const SkRect* src, const SkRect& dst, const SkPaint*, 246 SrcRectConstraint) override { 247 sk_throw(); 248 } 249 void onDrawBitmapNine(const SkBitmap&, const SkIRect& center, const SkRect& dst, 250 const SkPaint*) override { 251 sk_throw(); 252 } 253 void onDrawBitmapLattice(const SkBitmap&, const SkCanvas::Lattice& lattice, const SkRect& dst, 254 const SkPaint*) override { 255 sk_throw(); 256 } 257 258private: 259 SkPictureContentInfo fContentInfo; 260 261 SkTArray<SkPaint> fPaints; 262 263 struct PathHash { 264 uint32_t operator()(const SkPath& p) { return p.getGenerationID(); } 265 }; 266 SkTHashMap<SkPath, int, PathHash> fPaths; 267 268 SkWriter32 fWriter; 269 270 // we ref each item in these arrays 271 SkTDArray<const SkImage*> fImageRefs; 272 SkTDArray<const SkPicture*> fPictureRefs; 273 SkTDArray<SkDrawable*> fDrawableRefs; 274 SkTDArray<const SkTextBlob*> fTextBlobRefs; 275 276 uint32_t fRecordFlags; 277 int fInitialSaveCount; 278 279 friend class SkPictureData; // for SkPictureData's SkPictureRecord-based constructor 280 281 typedef SkCanvas INHERITED; 282}; 283 284#endif 285