Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Side by Side Diff: src/core/SkWriter32.cpp

Issue 875403005: Make SkWriter32::snapshotAsData() a dumb copy. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkWriter32.h ('k') | tests/Writer32Test.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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);
79 } 77 }
80 78
81 SkData* SkWriter32::snapshotAsData() const { 79 SkData* SkWriter32::snapshotAsData() const {
82 // get a non const version of this, we are only conceptually const 80 return SkData::NewWithCopy(fData, fUsed);
83 SkWriter32& mutable_this = *const_cast<SkWriter32*>(this);
84 // we use size change detection to invalidate the cached data
85 if ((fSnapshot.get() != NULL) && (fSnapshot->size() != fUsed)) {
86 mutable_this.fSnapshot.reset(NULL);
87 }
88 if (fSnapshot.get() == NULL) {
89 uint8_t* buffer = NULL;
90 if ((fExternal != NULL) && (fData == fExternal)) {
91 // We need to copy to an allocated buffer before returning.
92 buffer = (uint8_t*)sk_malloc_throw(fUsed);
93 memcpy(buffer, fData, fUsed);
94 } else {
95 buffer = mutable_this.fInternal.detach();
96 // prepare us to do copy on write, by pretending the data buffer
97 // is external and size limited
98 mutable_this.fData = buffer;
99 mutable_this.fCapacity = fUsed;
100 mutable_this.fExternal = buffer;
101 }
102 mutable_this.fSnapshot.reset(SkData::NewFromMalloc(buffer, fUsed));
103 }
104 return SkRef(fSnapshot.get()); // Take an extra ref for the caller.
105 } 81 }
OLDNEW
« no previous file with comments | « include/core/SkWriter32.h ('k') | tests/Writer32Test.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698