diff --git a/gyp/core.gypi b/gyp/core.gypi index b30a973aa5..d8f1715d93 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -18,6 +18,8 @@ '<(skia_src_path)/core/SkBBoxHierarchy.h', '<(skia_src_path)/core/SkBBoxRecord.cpp', '<(skia_src_path)/core/SkBBoxRecord.h', + '<(skia_src_path)/core/SkBBoxHierarchyRecord.cpp', + '<(skia_src_path)/core/SkBBoxHierarchyRecord.h', '<(skia_src_path)/core/SkBitmap.cpp', '<(skia_src_path)/core/SkBitmapHeap.cpp', '<(skia_src_path)/core/SkBitmapHeap.h', diff --git a/src/core/SkBBoxHierarchyRecord.cpp b/src/core/SkBBoxHierarchyRecord.cpp new file mode 100644 index 0000000000..35949b6760 --- /dev/null +++ b/src/core/SkBBoxHierarchyRecord.cpp @@ -0,0 +1,97 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkBBoxHierarchyRecord.h" +#include "SkPictureStateTree.h" +#include "SkBBoxHierarchy.h" + +SkBBoxHierarchyRecord::SkBBoxHierarchyRecord(uint32_t recordFlags, SkBBoxHierarchy* h) + : INHERITED(recordFlags) { + fStateTree = SkNEW(SkPictureStateTree); + fBoundingHierarchy = h; + fBoundingHierarchy->ref(); +} + +void SkBBoxHierarchyRecord::handleBBox(const SkRect& bounds) { + SkIRect r; + bounds.roundOut(&r); + SkPictureStateTree::Draw* draw = fStateTree->appendDraw(this->writeStream().size()); + fBoundingHierarchy->insert(draw, r, true); +} + +int SkBBoxHierarchyRecord::save(SaveFlags flags) { + fStateTree->appendSave(); + return INHERITED::save(flags); +} + +int SkBBoxHierarchyRecord::saveLayer(const SkRect* bounds, const SkPaint* paint, + SaveFlags flags) { + fStateTree->appendSaveLayer(this->writeStream().size()); + return INHERITED::saveLayer(bounds, paint, flags); +} + +void SkBBoxHierarchyRecord::restore() { + fStateTree->appendRestore(); + INHERITED::restore(); +} + +bool SkBBoxHierarchyRecord::translate(SkScalar dx, SkScalar dy) { + bool result = INHERITED::translate(dx, dy); + fStateTree->appendTransform(getTotalMatrix()); + return result; +} + +bool SkBBoxHierarchyRecord::scale(SkScalar sx, SkScalar sy) { + bool result = INHERITED::scale(sx, sy); + fStateTree->appendTransform(getTotalMatrix()); + return result; +} + +bool SkBBoxHierarchyRecord::rotate(SkScalar degrees) { + bool result = INHERITED::rotate(degrees); + fStateTree->appendTransform(getTotalMatrix()); + return result; +} + +bool SkBBoxHierarchyRecord::skew(SkScalar sx, SkScalar sy) { + bool result = INHERITED::skew(sx, sy); + fStateTree->appendTransform(getTotalMatrix()); + return result; +} + +bool SkBBoxHierarchyRecord::concat(const SkMatrix& matrix) { + bool result = INHERITED::concat(matrix); + fStateTree->appendTransform(getTotalMatrix()); + return result; +} + +void SkBBoxHierarchyRecord::setMatrix(const SkMatrix& matrix) { + INHERITED::setMatrix(matrix); + fStateTree->appendTransform(getTotalMatrix()); +} + +bool SkBBoxHierarchyRecord::clipRect(const SkRect& rect, + SkRegion::Op op, + bool doAntiAlias) { + fStateTree->appendClip(this->writeStream().size()); + return INHERITED::clipRect(rect, op, doAntiAlias); +} + +bool SkBBoxHierarchyRecord::clipRegion(const SkRegion& region, + SkRegion::Op op) { + fStateTree->appendClip(this->writeStream().size()); + return INHERITED::clipRegion(region, op); +} + +bool SkBBoxHierarchyRecord::clipPath(const SkPath& path, + SkRegion::Op op, + bool doAntiAlias) { + fStateTree->appendClip(this->writeStream().size()); + return INHERITED::clipPath(path, op, doAntiAlias); +} + diff --git a/src/core/SkBBoxHierarchyRecord.h b/src/core/SkBBoxHierarchyRecord.h new file mode 100644 index 0000000000..6c9e733c96 --- /dev/null +++ b/src/core/SkBBoxHierarchyRecord.h @@ -0,0 +1,51 @@ + +/* + * Copyright 2012 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkRTreeCanvas_DEFINED +#define SkRTreeCanvas_DEFINED + +#include "SkBBoxRecord.h" + +/** + * This records bounding box information into an SkBBoxHierarchy, and clip/transform information + * into an SkPictureStateTree to allow for efficient culling and correct playback of draws. + */ +class SkBBoxHierarchyRecord : public SkBBoxRecord { +public: + /** This will take a ref of h */ + SkBBoxHierarchyRecord(uint32_t recordFlags, SkBBoxHierarchy* h); + + virtual void handleBBox(const SkRect& bounds) SK_OVERRIDE; + + virtual int save(SaveFlags flags = kMatrixClip_SaveFlag) SK_OVERRIDE; + virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, + SaveFlags flags = kARGB_ClipLayer_SaveFlag) SK_OVERRIDE; + virtual void restore() SK_OVERRIDE; + + virtual bool translate(SkScalar dx, SkScalar dy) SK_OVERRIDE; + virtual bool scale(SkScalar sx, SkScalar sy) SK_OVERRIDE; + virtual bool rotate(SkScalar degrees) SK_OVERRIDE; + virtual bool skew(SkScalar sx, SkScalar sy) SK_OVERRIDE; + virtual bool concat(const SkMatrix& matrix) SK_OVERRIDE; + virtual void setMatrix(const SkMatrix& matrix) SK_OVERRIDE; + + virtual bool clipRect(const SkRect& rect, + SkRegion::Op op = SkRegion::kIntersect_Op, + bool doAntiAlias = false) SK_OVERRIDE; + virtual bool clipRegion(const SkRegion& region, + SkRegion::Op op = SkRegion::kIntersect_Op) SK_OVERRIDE; + virtual bool clipPath(const SkPath& path, + SkRegion::Op op = SkRegion::kIntersect_Op, + bool doAntiAlias = false) SK_OVERRIDE; + +private: + typedef SkBBoxRecord INHERITED; +}; + +#endif + diff --git a/src/core/SkPictureRecord.cpp b/src/core/SkPictureRecord.cpp index 10de946080..83f462534f 100644 --- a/src/core/SkPictureRecord.cpp +++ b/src/core/SkPictureRecord.cpp @@ -8,6 +8,8 @@ #include "SkPictureRecord.h" #include "SkTSearch.h" #include "SkPixelRef.h" +#include "SkBBoxHierarchy.h" +#include "SkPictureStateTree.h" #define MIN_WRITER_SIZE 16384 #define HEAP_BLOCK_SIZE 4096 @@ -22,6 +24,8 @@ SkPictureRecord::SkPictureRecord(uint32_t flags) : fPaints(&fFlattenableHeap), fRegions(&fFlattenableHeap), fWriter(MIN_WRITER_SIZE), + fBoundingHierarchy(NULL), + fStateTree(NULL), fRecordFlags(flags) { #ifdef SK_DEBUG_SIZE fPointBytes = fRectBytes = fTextBytes = 0; @@ -40,6 +44,8 @@ SkPictureRecord::SkPictureRecord(uint32_t flags) : SkPictureRecord::~SkPictureRecord() { SkSafeUnref(fBitmapHeap); SkSafeUnref(fPathHeap); + SkSafeUnref(fBoundingHierarchy); + SkSafeUnref(fStateTree); fFlattenableHeap.setBitmapStorage(NULL); fPictureRefs.unrefAll(); } diff --git a/src/core/SkPictureRecord.h b/src/core/SkPictureRecord.h index e4420f3552..a6e0b68050 100644 --- a/src/core/SkPictureRecord.h +++ b/src/core/SkPictureRecord.h @@ -16,6 +16,9 @@ #include "SkTemplates.h" #include "SkWriter32.h" +class SkPictureStateTree; +class SkBBoxHierarchy; + class SkPictureRecord : public SkCanvas { public: SkPictureRecord(uint32_t recordFlags); @@ -162,6 +165,13 @@ public: void validate() const {} #endif +protected: + + // These are set to NULL in our constructor, but may be changed by + // subclasses, in which case they will be SkSafeUnref'd in our destructor. + SkBBoxHierarchy* fBoundingHierarchy; + SkPictureStateTree* fStateTree; + private: SkBitmapHeap* fBitmapHeap; SkChunkFlatController fFlattenableHeap;