Function pointers -> templates in SkPictureFlat.
These flatten/unflatten function pointers were driving me nuts when reading the
generated assembly for this code. We don't need the flexibility of function
pointers here, so let's use templates to make it more manageable. You'll
notice we get much better typing now on flatten/unflatten.
BUG=
R=reed@google.com
Author: mtklein@google.com
Review URL: https://codereview.chromium.org/123213004
git-svn-id: http://skia.googlecode.com/svn/trunk@12873 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkPictureFlat.cpp b/src/core/SkPictureFlat.cpp
index 149cf7c..e75023e 100644
--- a/src/core/SkPictureFlat.cpp
+++ b/src/core/SkPictureFlat.cpp
@@ -90,51 +90,3 @@
return set;
}
-///////////////////////////////////////////////////////////////////////////////
-
-SkFlatData* SkFlatData::Create(SkFlatController* controller,
- const void* obj,
- int index,
- void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) {
- // a buffer of 256 bytes should be sufficient for most paints, regions,
- // and matrices.
- intptr_t storage[256];
- SkOrderedWriteBuffer buffer(256, storage, sizeof(storage));
-
- buffer.setBitmapHeap(controller->getBitmapHeap());
- buffer.setTypefaceRecorder(controller->getTypefaceSet());
- buffer.setNamedFactoryRecorder(controller->getNamedFactorySet());
- buffer.setFlags(controller->getWriteBufferFlags());
-
- flattenProc(buffer, obj);
- uint32_t size = buffer.size();
- SkASSERT(SkIsAlign4(size));
-
- // Allocate enough memory to hold SkFlatData struct and the flat data itself.
- size_t allocSize = sizeof(SkFlatData) + size;
- SkFlatData* result = (SkFlatData*) controller->allocThrow(allocSize);
-
- // Put the serialized contents into the data section of the new allocation.
- buffer.writeToMemory(result->data());
- // Stamp the index, size and checksum in the header.
- result->stampHeader(index, size);
- return result;
-}
-
-void SkFlatData::unflatten(void* result,
- void (*unflattenProc)(SkOrderedReadBuffer&, void*),
- SkBitmapHeap* bitmapHeap,
- SkTypefacePlayback* facePlayback) const {
-
- SkOrderedReadBuffer buffer(this->data(), fFlatSize);
-
- if (bitmapHeap) {
- buffer.setBitmapStorage(bitmapHeap);
- }
- if (facePlayback) {
- facePlayback->setupBuffer(buffer);
- }
-
- unflattenProc(buffer, result);
- SkASSERT(fFlatSize == (int32_t)buffer.offset());
-}
diff --git a/src/core/SkPictureFlat.h b/src/core/SkPictureFlat.h
index 5452501..220ae11 100644
--- a/src/core/SkPictureFlat.h
+++ b/src/core/SkPictureFlat.h
@@ -267,16 +267,49 @@
class SkFlatData {
public:
// Flatten obj into an SkFlatData with this index. controller owns the SkFlatData*.
- static SkFlatData* Create(SkFlatController* controller,
- const void* obj,
- int index,
- void (*flattenProc)(SkOrderedWriteBuffer&, const void*));
+ template <typename Traits, typename T>
+ static SkFlatData* Create(SkFlatController* controller, const T& obj, int index) {
+ // A buffer of 256 bytes should fit most paints, regions, and matrices.
+ uint32_t storage[64];
+ SkOrderedWriteBuffer buffer(256, storage, sizeof(storage));
- // Unflatten this into result, using bitmapHeap and facePlayback for bitmaps and fonts if given.
- void unflatten(void* result,
- void (*unflattenProc)(SkOrderedReadBuffer&, void*),
+ buffer.setBitmapHeap(controller->getBitmapHeap());
+ buffer.setTypefaceRecorder(controller->getTypefaceSet());
+ buffer.setNamedFactoryRecorder(controller->getNamedFactorySet());
+ buffer.setFlags(controller->getWriteBufferFlags());
+
+ Traits::flatten(buffer, obj);
+ uint32_t size = buffer.size();
+ SkASSERT(SkIsAlign4(size));
+
+ // Allocate enough memory to hold SkFlatData struct and the flat data itself.
+ size_t allocSize = sizeof(SkFlatData) + size;
+ SkFlatData* result = (SkFlatData*) controller->allocThrow(allocSize);
+
+ // Put the serialized contents into the data section of the new allocation.
+ buffer.writeToMemory(result->data());
+ // Stamp the index, size and checksum in the header.
+ result->stampHeader(index, size);
+ return result;
+ }
+
+ // Unflatten this into result, using bitmapHeap and facePlayback for bitmaps and fonts if given
+ template <typename Traits, typename T>
+ void unflatten(T* result,
SkBitmapHeap* bitmapHeap = NULL,
- SkTypefacePlayback* facePlayback = NULL) const;
+ SkTypefacePlayback* facePlayback = NULL) const {
+ SkOrderedReadBuffer buffer(this->data(), fFlatSize);
+
+ if (bitmapHeap) {
+ buffer.setBitmapStorage(bitmapHeap);
+ }
+ if (facePlayback) {
+ facePlayback->setupBuffer(buffer);
+ }
+
+ Traits::unflatten(buffer, result);
+ SkASSERT(fFlatSize == (int32_t)buffer.offset());
+ }
// Do these contain the same data? Ignores index() and topBot().
bool operator==(const SkFlatData& that) const {
@@ -333,19 +366,17 @@
mutable SkScalar fTopBot[2]; // Cache of FontMetrics fTop, fBottom. Starts as [NaN,?].
// uint32_t flattenedData[] implicitly hangs off the end.
- template <class T> friend class SkFlatDictionary;
+ template <typename T, typename Traits, int kScratchSizeGuess> friend class SkFlatDictionary;
};
-template <class T>
+template <typename T, typename Traits, int kScratchSizeGuess=0>
class SkFlatDictionary {
static const size_t kWriteBufferGrowthBytes = 1024;
public:
- SkFlatDictionary(SkFlatController* controller, size_t scratchSizeGuess = 0)
- : fFlattenProc(NULL)
- , fUnflattenProc(NULL)
- , fController(SkRef(controller))
- , fScratchSize(scratchSizeGuess)
+ explicit SkFlatDictionary(SkFlatController* controller)
+ : fController(SkRef(controller))
+ , fScratchSize(kScratchSizeGuess)
, fScratch(AllocScratch(fScratchSize))
, fWriteBuffer(kWriteBufferGrowthBytes)
, fWriteBufferReady(false) {
@@ -471,10 +502,6 @@
return this->findAndReturnMutableFlat(element);
}
-protected:
- void (*fFlattenProc)(SkOrderedWriteBuffer&, const void*);
- void (*fUnflattenProc)(SkOrderedReadBuffer&, void*);
-
private:
// Layout: [ SkFlatData header, 20 bytes ] [ data ..., 4-byte aligned ]
static size_t SizeWithPadding(size_t flatDataSize) {
@@ -523,7 +550,7 @@
// Flatten element into fWriteBuffer (using fScratch as storage).
fWriteBuffer.reset(fScratch->data(), fScratchSize);
- fFlattenProc(fWriteBuffer, &element);
+ Traits::flatten(fWriteBuffer, element);
const size_t bytesWritten = fWriteBuffer.bytesWritten();
// If all the flattened bytes fit into fScratch, we can skip a call to writeToMemory.
@@ -561,10 +588,9 @@
}
void unflatten(T* dst, const SkFlatData* element) const {
- element->unflatten(dst,
- fUnflattenProc,
- fController->getBitmapHeap(),
- fController->getTypefacePlayback());
+ element->unflatten<Traits>(dst,
+ fController->getBitmapHeap(),
+ fController->getTypefacePlayback());
}
// All SkFlatData* stored in fIndexedData and fHash are owned by the controller.
@@ -589,15 +615,37 @@
// Some common dictionaries are defined here for both reference and convenience
///////////////////////////////////////////////////////////////////////////////
-template <class T>
-static void SkFlattenObjectProc(SkOrderedWriteBuffer& buffer, const void* obj) {
- ((T*)obj)->flatten(buffer);
-}
+struct SkMatrixTraits {
+ static void flatten(SkOrderedWriteBuffer& buffer, const SkMatrix& matrix) {
+ buffer.getWriter32()->writeMatrix(matrix);
+ }
+ static void unflatten(SkOrderedReadBuffer& buffer, SkMatrix* matrix) {
+ buffer.getReader32()->readMatrix(matrix);
+ }
+};
+typedef SkFlatDictionary<SkMatrix, SkMatrixTraits, 36> SkMatrixDictionary;
-template <class T>
-static void SkUnflattenObjectProc(SkOrderedReadBuffer& buffer, void* obj) {
- ((T*)obj)->unflatten(buffer);
-}
+
+struct SkRegionTraits {
+ static void flatten(SkOrderedWriteBuffer& buffer, const SkRegion& region) {
+ buffer.getWriter32()->writeRegion(region);
+ }
+ static void unflatten(SkOrderedReadBuffer& buffer, SkRegion* region) {
+ buffer.getReader32()->readRegion(region);
+ }
+};
+typedef SkFlatDictionary<SkRegion, SkRegionTraits> SkRegionDictionary;
+
+
+struct SkPaintTraits {
+ static void flatten(SkOrderedWriteBuffer& buffer, const SkPaint& paint) {
+ paint.flatten(buffer);
+ }
+ static void unflatten(SkOrderedReadBuffer& buffer, SkPaint* paint) {
+ paint->unflatten(buffer);
+ }
+};
+typedef SkFlatDictionary<SkPaint, SkPaintTraits, 512> SkPaintDictionary;
class SkChunkFlatController : public SkFlatController {
public:
@@ -635,49 +683,4 @@
mutable SkTypefacePlayback fTypefacePlayback;
};
-class SkMatrixDictionary : public SkFlatDictionary<SkMatrix> {
- public:
- // All matrices fit in 36 bytes.
- SkMatrixDictionary(SkFlatController* controller)
- : SkFlatDictionary<SkMatrix>(controller, 36) {
- fFlattenProc = &flattenMatrix;
- fUnflattenProc = &unflattenMatrix;
- }
-
- static void flattenMatrix(SkOrderedWriteBuffer& buffer, const void* obj) {
- buffer.getWriter32()->writeMatrix(*(SkMatrix*)obj);
- }
-
- static void unflattenMatrix(SkOrderedReadBuffer& buffer, void* obj) {
- buffer.getReader32()->readMatrix((SkMatrix*)obj);
- }
-};
-
-class SkPaintDictionary : public SkFlatDictionary<SkPaint> {
- public:
- // The largest paint across ~60 .skps was 500 bytes.
- SkPaintDictionary(SkFlatController* controller)
- : SkFlatDictionary<SkPaint>(controller, 512) {
- fFlattenProc = &SkFlattenObjectProc<SkPaint>;
- fUnflattenProc = &SkUnflattenObjectProc<SkPaint>;
- }
-};
-
-class SkRegionDictionary : public SkFlatDictionary<SkRegion> {
- public:
- SkRegionDictionary(SkFlatController* controller)
- : SkFlatDictionary<SkRegion>(controller) {
- fFlattenProc = &flattenRegion;
- fUnflattenProc = &unflattenRegion;
- }
-
- static void flattenRegion(SkOrderedWriteBuffer& buffer, const void* obj) {
- buffer.getWriter32()->writeRegion(*(SkRegion*)obj);
- }
-
- static void unflattenRegion(SkOrderedReadBuffer& buffer, void* obj) {
- buffer.getReader32()->readRegion((SkRegion*)obj);
- }
-};
-
#endif
diff --git a/src/core/SkPicturePlayback.cpp b/src/core/SkPicturePlayback.cpp
index 195afc6..97e5667 100644
--- a/src/core/SkPicturePlayback.cpp
+++ b/src/core/SkPicturePlayback.cpp
@@ -212,9 +212,10 @@
SkDEBUGCODE(int heapSize = SafeCount(fBitmapHeap.get());)
for (int i = 0; i < paintCount; i++) {
if (needs_deep_copy(src.fPaints->at(i))) {
- deepCopyInfo->paintData[i] = SkFlatData::Create(&deepCopyInfo->controller,
- &src.fPaints->at(i), 0,
- &SkFlattenObjectProc<SkPaint>);
+ deepCopyInfo->paintData[i] =
+ SkFlatData::Create<SkPaintTraits>(&deepCopyInfo->controller,
+ src.fPaints->at(i), 0);
+
} else {
// this is our sentinel, which we use in the unflatten loop
deepCopyInfo->paintData[i] = NULL;
@@ -233,9 +234,8 @@
SkTypefacePlayback* tfPlayback = deepCopyInfo->controller.getTypefacePlayback();
for (int i = 0; i < paintCount; i++) {
if (deepCopyInfo->paintData[i]) {
- deepCopyInfo->paintData[i]->unflatten(&fPaints->writableAt(i),
- &SkUnflattenObjectProc<SkPaint>,
- bmHeap, tfPlayback);
+ deepCopyInfo->paintData[i]->unflatten<SkPaintTraits>(&fPaints->writableAt(i),
+ bmHeap, tfPlayback);
} else {
// needs_deep_copy was false, so just need to assign
fPaints->writableAt(i) = src.fPaints->at(i);
diff --git a/src/pipe/SkGPipeWrite.cpp b/src/pipe/SkGPipeWrite.cpp
index 0131ebb..68acc17 100644
--- a/src/pipe/SkGPipeWrite.cpp
+++ b/src/pipe/SkGPipeWrite.cpp
@@ -150,20 +150,13 @@
///////////////////////////////////////////////////////////////////////////////
-class FlatDictionary : public SkFlatDictionary<SkFlattenable> {
-public:
- FlatDictionary(FlattenableHeap* heap)
- : SkFlatDictionary<SkFlattenable>(heap) {
- fFlattenProc = &flattenFlattenableProc;
- // No need to define fUnflattenProc since the writer will never
- // unflatten the data.
+struct SkFlattenableTraits {
+ static void flatten(SkOrderedWriteBuffer& buffer, const SkFlattenable& flattenable) {
+ buffer.writeFlattenable(&flattenable);
}
- static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
- const void* obj) {
- buffer.writeFlattenable((SkFlattenable*)obj);
- }
-
+ // No need to define unflatten if we never call it.
};
+typedef SkFlatDictionary<SkFlattenable, SkFlattenableTraits> FlatDictionary;
///////////////////////////////////////////////////////////////////////////////