michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkPictureRecord_DEFINED michael@0: #define SkPictureRecord_DEFINED michael@0: michael@0: #include "SkCanvas.h" michael@0: #include "SkFlattenable.h" michael@0: #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE michael@0: #include "SkMatrixClipStateMgr.h" michael@0: #endif michael@0: #include "SkPathHeap.h" michael@0: #include "SkPicture.h" michael@0: #include "SkPictureFlat.h" michael@0: #include "SkTemplates.h" michael@0: #include "SkWriter32.h" michael@0: michael@0: class SkBBoxHierarchy; michael@0: class SkOffsetTable; michael@0: class SkPictureStateTree; michael@0: michael@0: // These macros help with packing and unpacking a single byte value and michael@0: // a 3 byte value into/out of a uint32_t michael@0: #define MASK_24 0x00FFFFFF michael@0: #define UNPACK_8_24(combined, small, large) \ michael@0: small = (combined >> 24) & 0xFF; \ michael@0: large = combined & MASK_24; michael@0: #define PACK_8_24(small, large) ((small << 24) | large) michael@0: michael@0: michael@0: class SkPictureRecord : public SkCanvas { michael@0: public: michael@0: SkPictureRecord(const SkISize& dimensions, uint32_t recordFlags); michael@0: virtual ~SkPictureRecord(); michael@0: michael@0: virtual void clear(SkColor) SK_OVERRIDE; michael@0: virtual void drawPaint(const SkPaint& paint) SK_OVERRIDE; michael@0: virtual void drawPoints(PointMode, size_t count, const SkPoint pts[], michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawOval(const SkRect&, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawRect(const SkRect&, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawRRect(const SkRRect&, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawPath(const SkPath& path, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawBitmap(const SkBitmap&, SkScalar left, SkScalar top, michael@0: const SkPaint*) SK_OVERRIDE; michael@0: virtual void drawBitmapRectToRect(const SkBitmap&, const SkRect* src, michael@0: const SkRect& dst, const SkPaint* paint, michael@0: DrawBitmapRectFlags flags) SK_OVERRIDE; michael@0: virtual void drawBitmapMatrix(const SkBitmap&, const SkMatrix&, michael@0: const SkPaint*) SK_OVERRIDE; michael@0: virtual void drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, michael@0: const SkRect& dst, const SkPaint*) SK_OVERRIDE; michael@0: virtual void drawSprite(const SkBitmap&, int left, int top, michael@0: const SkPaint*) SK_OVERRIDE; michael@0: virtual void drawText(const void* text, size_t byteLength, SkScalar x, michael@0: SkScalar y, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawPosText(const void* text, size_t byteLength, michael@0: const SkPoint pos[], const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawPosTextH(const void* text, size_t byteLength, michael@0: const SkScalar xpos[], SkScalar constY, const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawTextOnPath(const void* text, size_t byteLength, michael@0: const SkPath& path, const SkMatrix* matrix, michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawPicture(SkPicture& picture) SK_OVERRIDE; michael@0: virtual void drawVertices(VertexMode, int vertexCount, michael@0: const SkPoint vertices[], const SkPoint texs[], michael@0: const SkColor colors[], SkXfermode*, michael@0: const uint16_t indices[], int indexCount, michael@0: const SkPaint&) SK_OVERRIDE; michael@0: virtual void drawData(const void*, size_t) SK_OVERRIDE; michael@0: virtual void beginCommentGroup(const char* description) SK_OVERRIDE; michael@0: virtual void addComment(const char* kywd, const char* value) SK_OVERRIDE; michael@0: virtual void endCommentGroup() SK_OVERRIDE; michael@0: virtual bool isDrawingToLayer() const SK_OVERRIDE; michael@0: michael@0: void addFontMetricsTopBottom(const SkPaint& paint, const SkFlatData&, michael@0: SkScalar minY, SkScalar maxY); michael@0: michael@0: const SkTDArray& getPictureRefs() const { michael@0: return fPictureRefs; michael@0: } michael@0: michael@0: void setFlags(uint32_t recordFlags) { michael@0: fRecordFlags = recordFlags; michael@0: } michael@0: michael@0: const SkWriter32& writeStream() const { michael@0: return fWriter; michael@0: } michael@0: michael@0: void beginRecording(); michael@0: void endRecording(); michael@0: michael@0: void internalOnly_EnableOpts(bool optsEnabled) { michael@0: fOptsEnabled = optsEnabled; michael@0: } michael@0: michael@0: private: michael@0: void handleOptimization(int opt); michael@0: int recordRestoreOffsetPlaceholder(SkRegion::Op); michael@0: void fillRestoreOffsetPlaceholdersForCurrentStackLevel(uint32_t restoreOffset); michael@0: michael@0: #ifndef SK_COLLAPSE_MATRIX_CLIP_STATE michael@0: SkTDArray fRestoreOffsetStack; michael@0: int fFirstSavedLayerIndex; michael@0: enum { michael@0: kNoSavedLayerIndex = -1 michael@0: }; michael@0: #endif michael@0: michael@0: SkTDArray fCullOffsetStack; michael@0: michael@0: /* michael@0: * Write the 'drawType' operation and chunk size to the skp. 'size' michael@0: * can potentially be increased if the chunk size needs its own storage michael@0: * location (i.e., it overflows 24 bits). michael@0: * Returns the start offset of the chunk. This is the location at which michael@0: * the opcode & size are stored. michael@0: * TODO: since we are handing the size into here we could call reserve michael@0: * and then return a pointer to the memory storage. This could decrease michael@0: * allocation overhead but could lead to more wasted space (the tail michael@0: * end of blocks could go unused). Possibly add a second addDraw that michael@0: * operates in this manner. michael@0: */ michael@0: size_t addDraw(DrawType drawType, uint32_t* size) { michael@0: size_t offset = fWriter.bytesWritten(); michael@0: michael@0: this->predrawNotify(); michael@0: michael@0: #ifdef SK_DEBUG_TRACE michael@0: SkDebugf("add %s\n", DrawTypeToString(drawType)); michael@0: #endif michael@0: michael@0: SkASSERT(0 != *size); michael@0: SkASSERT(((uint8_t) drawType) == drawType); michael@0: michael@0: if (0 != (*size & ~MASK_24) || *size == MASK_24) { michael@0: fWriter.writeInt(PACK_8_24(drawType, MASK_24)); michael@0: *size += 1; michael@0: fWriter.writeInt(*size); michael@0: } else { michael@0: fWriter.writeInt(PACK_8_24(drawType, *size)); michael@0: } michael@0: michael@0: return offset; michael@0: } michael@0: michael@0: void addInt(int value) { michael@0: fWriter.writeInt(value); michael@0: } michael@0: void addScalar(SkScalar scalar) { michael@0: fWriter.writeScalar(scalar); michael@0: } michael@0: michael@0: // The command at 'offset' in the skp uses the specified bitmap michael@0: void trackBitmapUse(int bitmapID, size_t offset); michael@0: int addBitmap(const SkBitmap& bitmap); michael@0: void addMatrix(const SkMatrix& matrix); michael@0: const SkFlatData* addPaint(const SkPaint& paint) { return this->addPaintPtr(&paint); } michael@0: const SkFlatData* addPaintPtr(const SkPaint* paint); michael@0: void addFlatPaint(const SkFlatData* flatPaint); michael@0: void addPath(const SkPath& path); michael@0: void addPicture(SkPicture& picture); michael@0: void addPoint(const SkPoint& point); michael@0: void addPoints(const SkPoint pts[], int count); michael@0: void addRect(const SkRect& rect); michael@0: void addRectPtr(const SkRect* rect); michael@0: void addIRect(const SkIRect& rect); michael@0: void addIRectPtr(const SkIRect* rect); michael@0: void addRRect(const SkRRect&); michael@0: void addRegion(const SkRegion& region); michael@0: void addText(const void* text, size_t byteLength); michael@0: michael@0: int find(const SkBitmap& bitmap); michael@0: michael@0: #ifdef SK_DEBUG_DUMP michael@0: public: michael@0: void dumpMatrices(); michael@0: void dumpPaints(); michael@0: #endif michael@0: michael@0: #ifdef SK_DEBUG_SIZE michael@0: public: michael@0: size_t size() const; michael@0: int bitmaps(size_t* size) const; michael@0: int matrices(size_t* size) const; michael@0: int paints(size_t* size) const; michael@0: int paths(size_t* size) const; michael@0: int regions(size_t* size) const; michael@0: size_t streamlen() const; michael@0: michael@0: size_t fPointBytes, fRectBytes, fTextBytes; michael@0: int fPointWrites, fRectWrites, fTextWrites; michael@0: #endif michael@0: michael@0: #ifdef SK_DEBUG_VALIDATE michael@0: public: michael@0: void validate(size_t initialOffset, uint32_t size) const; michael@0: private: michael@0: void validateBitmaps() const; michael@0: void validateMatrices() const; michael@0: void validatePaints() const; michael@0: void validatePaths() const; michael@0: void validateRegions() const; michael@0: #else michael@0: public: michael@0: void validate(size_t initialOffset, uint32_t size) const { michael@0: SkASSERT(fWriter.bytesWritten() == initialOffset + size); michael@0: } michael@0: #endif michael@0: michael@0: protected: michael@0: virtual SkSurface* onNewSurface(const SkImageInfo&) SK_OVERRIDE; michael@0: const void* onPeekPixels(SkImageInfo*, size_t*) SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: michael@0: virtual void willSave(SaveFlags) SK_OVERRIDE; michael@0: virtual SaveLayerStrategy willSaveLayer(const SkRect*, const SkPaint*, SaveFlags) SK_OVERRIDE; michael@0: virtual void willRestore() SK_OVERRIDE; michael@0: michael@0: virtual void didTranslate(SkScalar, SkScalar) SK_OVERRIDE; michael@0: virtual void didScale(SkScalar, SkScalar) SK_OVERRIDE; michael@0: virtual void didRotate(SkScalar) SK_OVERRIDE; michael@0: virtual void didSkew(SkScalar, SkScalar) SK_OVERRIDE; michael@0: virtual void didConcat(const SkMatrix&) SK_OVERRIDE; michael@0: virtual void didSetMatrix(const SkMatrix&) SK_OVERRIDE; michael@0: michael@0: virtual void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) SK_OVERRIDE; michael@0: virtual void onPushCull(const SkRect&) SK_OVERRIDE; michael@0: virtual void onPopCull() SK_OVERRIDE; michael@0: michael@0: virtual void onClipRect(const SkRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; michael@0: virtual void onClipRRect(const SkRRect&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; michael@0: virtual void onClipPath(const SkPath&, SkRegion::Op, ClipEdgeStyle) SK_OVERRIDE; michael@0: virtual void onClipRegion(const SkRegion&, SkRegion::Op) SK_OVERRIDE; michael@0: michael@0: // Return fontmetrics.fTop,fBottom in topbot[0,1], after they have been michael@0: // tweaked by paint.computeFastBounds(). michael@0: static void ComputeFontMetricsTopBottom(const SkPaint& paint, SkScalar topbot[2]); michael@0: michael@0: // Make sure that flat has fTopBot written. michael@0: static void WriteTopBot(const SkPaint& paint, const SkFlatData& flat) { michael@0: if (!flat.isTopBotWritten()) { michael@0: ComputeFontMetricsTopBottom(paint, flat.writableTopBot()); michael@0: SkASSERT(flat.isTopBotWritten()); michael@0: } michael@0: } michael@0: // Will return a cached version when possible. michael@0: const SkFlatData* getFlatPaintData(const SkPaint& paint); michael@0: /** michael@0: * SkBBoxRecord::drawPosTextH gets a flat paint and uses it, michael@0: * then it calls this, using the extra parameter, to avoid duplication. michael@0: */ michael@0: void drawPosTextHImpl(const void* text, size_t byteLength, michael@0: const SkScalar xpos[], SkScalar constY, michael@0: const SkPaint& paint, const SkFlatData* flatPaintData); michael@0: michael@0: int addPathToHeap(const SkPath& path); // does not write to ops stream michael@0: michael@0: // These entry points allow the writing of matrices, clips, saves & michael@0: // restores to be deferred (e.g., if the MC state is being collapsed and michael@0: // only written out as needed). michael@0: void recordConcat(const SkMatrix& matrix); michael@0: int recordClipRect(const SkRect& rect, SkRegion::Op op, bool doAA); michael@0: int recordClipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA); michael@0: int recordClipPath(int pathID, SkRegion::Op op, bool doAA); michael@0: int recordClipRegion(const SkRegion& region, SkRegion::Op op); michael@0: void recordSave(SaveFlags flags); michael@0: void recordSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags); michael@0: void recordRestore(bool fillInSkips = true); michael@0: michael@0: // These are set to NULL in our constructor, but may be changed by michael@0: // subclasses, in which case they will be SkSafeUnref'd in our destructor. michael@0: SkBBoxHierarchy* fBoundingHierarchy; michael@0: SkPictureStateTree* fStateTree; michael@0: michael@0: // Allocated in the constructor and managed by this class. michael@0: SkBitmapHeap* fBitmapHeap; michael@0: michael@0: private: michael@0: friend class MatrixClipState; // for access to *Impl methods michael@0: friend class SkMatrixClipStateMgr; // for access to *Impl methods michael@0: michael@0: SkChunkFlatController fFlattenableHeap; michael@0: michael@0: SkPaintDictionary fPaints; michael@0: michael@0: SkPathHeap* fPathHeap; // reference counted michael@0: SkWriter32 fWriter; michael@0: michael@0: // we ref each item in these arrays michael@0: SkTDArray fPictureRefs; michael@0: michael@0: uint32_t fRecordFlags; michael@0: bool fOptsEnabled; michael@0: int fInitialSaveCount; michael@0: michael@0: SkAutoTUnref fBitmapUseOffsets; michael@0: michael@0: friend class SkPicturePlayback; michael@0: friend class SkPictureTester; // for unit testing michael@0: michael@0: #ifdef SK_COLLAPSE_MATRIX_CLIP_STATE michael@0: SkMatrixClipStateMgr fMCMgr; michael@0: #endif michael@0: michael@0: typedef SkCanvas INHERITED; michael@0: }; michael@0: michael@0: #endif