Index: src/core/SkWriter32.cpp |
diff --git a/src/core/SkWriter32.cpp b/src/core/SkWriter32.cpp |
index fe33e4a39cf011d1ce54d1d9a08c321d99f707fe..ebdfb36b7fbc6d1c4552c716cae97c1e2a59e8e2 100644 |
--- a/src/core/SkWriter32.cpp |
+++ b/src/core/SkWriter32.cpp |
@@ -74,4 +74,35 @@ void SkWriter32::growToAtLeast(size_t size) { |
// 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); |
+} |
+ |
+SK_DECLARE_STATIC_MUTEX(gSnapshotMutex); |
mtklein
2014/03/06 18:58:08
I think I'd be happier if we declared this to be n
iancottrell
2014/03/06 21:31:52
Done.
|
+ |
+SkData* SkWriter32::snapshotAsData() const { |
+ SkAutoMutexAcquire am(gSnapshotMutex); |
+ // get a non const version of this, we are only conceptually const |
+ SkWriter32& self = *const_cast<SkWriter32*>(this); |
mtklein
2014/03/06 18:58:08
Maybe SkWriter32& self -> SkWriter32* mutable_this
iancottrell
2014/03/06 21:31:52
Done.
|
+ // we use size change detection to invalidate the cached data |
+ if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) { |
+ self.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 = self.fInternal.detach(); |
+ // prepare us to do copy on write, by pretending the data buffer |
+ // is external and size limited |
+ self.fData = buffer; |
+ self.fCapacity = fUsed; |
+ self.fExternal = buffer; |
+ } |
+ self.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed)); |
+ } |
+ return SkRef(fSnapshot.get()); |
mtklein
2014/03/06 18:58:08
Can you tack on // Take an extra ref for the calle
iancottrell
2014/03/06 21:31:52
Done.
|
} |