#ifndef SkWriter32_DEFINED
#define SkWriter32_DEFINED
+#include "SkData.h"
#include "SkMatrix.h"
#include "SkPath.h"
#include "SkPoint.h"
SkASSERT(SkIsAlign4((uintptr_t)external));
SkASSERT(SkIsAlign4(externalBytes));
+ fSnapshot.reset(NULL);
fData = (uint8_t*)external;
fCapacity = externalBytes;
fUsed = 0;
/**
* Read a T record at offset, which must be a multiple of 4. Only legal if the record
- * was writtern atomically using the write methods below.
+ * was written atomically using the write methods below.
*/
template<typename T>
const T& readTAt(size_t offset) const {
/**
* Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record
- * was writtern atomically using the write methods below.
+ * was written atomically using the write methods below.
*/
template<typename T>
void overwriteTAt(size_t offset, const T& value) {
SkASSERT(SkAlign4(offset) == offset);
SkASSERT(offset < fUsed);
+ SkASSERT(fSnapshot.get() == NULL);
*(T*)(fData + offset) = value;
}
return stream->read(this->reservePad(length), length);
}
+ /**
+ * Captures a snapshot of the data as it is right now, and return it.
+ * Multiple calls without intervening writes may return the same SkData,
+ * but this is not guaranteed.
+ * Future appends will not affect the returned buffer.
+ * It is illegal to call overwriteTAt after this without an intervening
+ * append. It may cause the snapshot buffer to be corrupted.
+ * Callers must unref the returned SkData.
+ * This is not thread safe, it should only be called on the writing thread,
+ * the result however can be shared across threads.
+ */
+ SkData* snapshotAsData() const;
private:
void growToAtLeast(size_t size);
size_t fUsed; // Number of bytes written.
void* fExternal; // Unmanaged memory block.
SkAutoTMalloc<uint8_t> fInternal; // Managed memory block.
+ SkAutoTUnref<SkData> fSnapshot; // Holds the result of last asData.
};
/**
record.validate(record.writeStream().bytesWritten(), 0);
const SkWriter32& writer = record.writeStream();
init();
+ SkASSERT(!fOpData);
if (writer.bytesWritten() == 0) {
fOpData = SkData::NewEmpty();
return;
}
+ fOpData = writer.snapshotAsData();
fBoundingHierarchy = record.fBoundingHierarchy;
fStateTree = record.fStateTree;
fBoundingHierarchy->flushDeferredInserts();
}
- {
- size_t size = writer.bytesWritten();
- void* buffer = sk_malloc_throw(size);
- writer.flatten(buffer);
- SkASSERT(!fOpData);
- fOpData = SkData::NewFromMalloc(buffer, size);
- }
-
// copy over the refcnt dictionary to our reader
record.fFlattenableHeap.setupPlaybacks();
// we were external, so copy in the data
memcpy(fData, fExternal, fUsed);
}
+ // Invalidate the snapshot, we know it is no longer useful.
+ fSnapshot.reset(NULL);
+}
+
+SkData* SkWriter32::snapshotAsData() const {
+ // get a non const version of this, we are only conceptually const
+ SkWriter32& mutable_this = *const_cast<SkWriter32*>(this);
+ // we use size change detection to invalidate the cached data
+ if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) {
+ mutable_this.fSnapshot.reset(NULL);
+ }
+ if (fSnapshot.get() == NULL) {
+ uint8_t* buffer = NULL;
+ if ((fExternal != NULL) && (fData == fExternal)) {
+ // We need to copy to an allocated buffer before returning.
+ buffer = (uint8_t*)sk_malloc_throw(fUsed);
+ memcpy(buffer, fData, fUsed);
+ } else {
+ buffer = mutable_this.fInternal.detach();
+ // prepare us to do copy on write, by pretending the data buffer
+ // is external and size limited
+ mutable_this.fData = buffer;
+ mutable_this.fCapacity = fUsed;
+ mutable_this.fExternal = buffer;
+ }
+ mutable_this.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed));
+ }
+ return SkRef(fSnapshot.get()); // Take an extra ref for the caller.
}
test_ptr(reporter);
test_rewind(reporter);
}
+
+DEF_TEST(Writer32_snapshot, reporter) {
+ int32_t array[] = { 1, 2, 4, 11 };
+ SkSWriter32<sizeof(array) + 4> writer;
+ writer.write(array, sizeof(array));
+ check_contents(reporter, writer, array, sizeof(array));
+ const void* beforeData = writer.contiguousArray();
+ SkAutoDataUnref snapshot(writer.snapshotAsData());
+ // check the snapshot forced a copy of the static data
+ REPORTER_ASSERT(reporter, snapshot->data() != beforeData);
+ REPORTER_ASSERT(reporter, snapshot->size() == writer.bytesWritten());
+}
+
+DEF_TEST(Writer32_snapshot_dynamic, reporter) {
+ int32_t array[] = { 1, 2, 4, 11 };
+ SkWriter32 writer;
+ writer.write(array, sizeof(array));
+ check_contents(reporter, writer, array, sizeof(array));
+ // force a capacity increase so we can test COW behaviour
+ writer.write(array, sizeof(array));
+ writer.rewindToOffset(sizeof(array));
+ const void* beforeData = writer.contiguousArray();
+ SkAutoDataUnref snapshot(writer.snapshotAsData());
+ // check the snapshot still points to the same data as the writer
+ REPORTER_ASSERT(reporter, writer.contiguousArray() == beforeData);
+ REPORTER_ASSERT(reporter, snapshot->data() == beforeData);
+ REPORTER_ASSERT(reporter, snapshot->size() == writer.bytesWritten());
+ // write more data that would fit in the buffer
+ writer.write(array, sizeof(array));
+ // test it triggered COW anyway
+ REPORTER_ASSERT(reporter, writer.contiguousArray() != beforeData);
+}
+