Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkReader32.h" | 8 #include "SkReader32.h" |
| 9 #include "SkString.h" | 9 #include "SkString.h" |
| 10 #include "SkWriter32.h" | 10 #include "SkWriter32.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 const bool wasExternal = (fExternal != NULL) && (fData == fExternal); | 67 const bool wasExternal = (fExternal != NULL) && (fData == fExternal); |
| 68 | 68 |
| 69 fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2)); | 69 fCapacity = 4096 + SkTMax(size, fCapacity + (fCapacity / 2)); |
| 70 fInternal.realloc(fCapacity); | 70 fInternal.realloc(fCapacity); |
| 71 fData = fInternal.get(); | 71 fData = fInternal.get(); |
| 72 | 72 |
| 73 if (wasExternal) { | 73 if (wasExternal) { |
| 74 // we were external, so copy in the data | 74 // we were external, so copy in the data |
| 75 memcpy(fData, fExternal, fUsed); | 75 memcpy(fData, fExternal, fUsed); |
| 76 } | 76 } |
| 77 // Invalidate the snapshot, we know it is no longer useful. | |
| 78 fSnapshot.reset(NULL); | |
| 77 } | 79 } |
| 80 | |
| 81 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.
| |
| 82 | |
| 83 SkData* SkWriter32::snapshotAsData() const { | |
| 84 SkAutoMutexAcquire am(gSnapshotMutex); | |
| 85 // get a non const version of this, we are only conceptually const | |
| 86 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.
| |
| 87 // we use size change detection to invalidate the cached data | |
| 88 if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) { | |
| 89 self.fSnapshot.reset(NULL); | |
| 90 } | |
| 91 if (fSnapshot.get() == NULL) { | |
| 92 uint8_t* buffer = NULL; | |
| 93 if ((fExternal != NULL) && (fData == fExternal)) { | |
| 94 // We need to copy to an allocated buffer before returning. | |
| 95 buffer = (uint8_t*)sk_malloc_throw(fUsed); | |
| 96 memcpy(buffer, fData, fUsed); | |
| 97 } else { | |
| 98 buffer = self.fInternal.detach(); | |
| 99 // prepare us to do copy on write, by pretending the data buffer | |
| 100 // is external and size limited | |
| 101 self.fData = buffer; | |
| 102 self.fCapacity = fUsed; | |
| 103 self.fExternal = buffer; | |
| 104 } | |
| 105 self.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed)); | |
| 106 } | |
| 107 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.
| |
| 108 } | |
| OLD | NEW |