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

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

Issue 110503003: PixelRef now returns (nearly) everything that is currently in SkBitmap. The goal is to refactor bit… (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years 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 | Annotate | Revision Log
« no previous file with comments | « src/core/SkMaskFilter.cpp ('k') | src/effects/gradients/SkGradientShader.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 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SkPixelRef.h" 8 #include "SkPixelRef.h"
9 #include "SkFlattenableBuffers.h" 9 #include "SkFlattenableBuffers.h"
10 #include "SkThread.h" 10 #include "SkThread.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 void SkPixelRef::setMutex(SkBaseMutex* mutex) { 75 void SkPixelRef::setMutex(SkBaseMutex* mutex) {
76 if (NULL == mutex) { 76 if (NULL == mutex) {
77 mutex = get_default_mutex(); 77 mutex = get_default_mutex();
78 } 78 }
79 fMutex = mutex; 79 fMutex = mutex;
80 } 80 }
81 81
82 // just need a > 0 value, so pick a funny one to aid in debugging 82 // just need a > 0 value, so pick a funny one to aid in debugging
83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 83 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789
84 84
85 SkPixelRef::SkPixelRef(const SkImageInfo&, SkBaseMutex* mutex) { 85 SkPixelRef::SkPixelRef(const SkImageInfo& info) {
86 this->setMutex(mutex); 86 this->setMutex(NULL);
87 fPixels = NULL; 87 fInfo = info;
88 fColorTable = NULL; // we do not track ownership of this 88 fRec.zero();
89 fLockCount = 0; 89 fLockCount = 0;
90 this->needsNewGenID(); 90 this->needsNewGenID();
91 fIsImmutable = false; 91 fIsImmutable = false;
92 fPreLocked = false; 92 fPreLocked = false;
93 } 93 }
94 94
95 SkPixelRef::SkPixelRef(const SkImageInfo&) { 95 SkPixelRef::SkPixelRef(const SkImageInfo& info, SkBaseMutex* mutex) {
96 this->setMutex(NULL); 96 this->setMutex(mutex);
97 fPixels = NULL; 97 fInfo = info;
98 fColorTable = NULL; // we do not track ownership of this 98 fRec.zero();
99 fLockCount = 0; 99 fLockCount = 0;
100 this->needsNewGenID(); 100 this->needsNewGenID();
101 fIsImmutable = false; 101 fIsImmutable = false;
102 fPreLocked = false; 102 fPreLocked = false;
103 } 103 }
104 104
105 #ifdef SK_SUPPORT_LEGACY_PIXELREF_CONSTRUCTOR
106 // THIS GUY IS DEPRECATED -- don't use me!
107 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) {
108 this->setMutex(mutex);
109 fPixels = NULL;
110 fColorTable = NULL; // we do not track ownership of this
111 fLockCount = 0;
112 this->needsNewGenID();
113 fIsImmutable = false;
114 fPreLocked = false;
115 }
116 #endif
117
118 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) 105 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
119 : INHERITED(buffer) { 106 : INHERITED(buffer) {
120 this->setMutex(mutex); 107 this->setMutex(mutex);
121 fPixels = NULL; 108
122 fColorTable = NULL; // we do not track ownership of this 109 fInfo.unflatten(buffer);
110 fRec.zero();
123 fLockCount = 0; 111 fLockCount = 0;
124 fIsImmutable = buffer.readBool(); 112 fIsImmutable = buffer.readBool();
125 fGenerationID = buffer.readUInt(); 113 fGenerationID = buffer.readUInt();
126 fUniqueGenerationID = false; // Conservatively assuming the original still exists. 114 fUniqueGenerationID = false; // Conservatively assuming the original still exists.
127 fPreLocked = false; 115 fPreLocked = false;
128 } 116 }
129 117
130 SkPixelRef::~SkPixelRef() { 118 SkPixelRef::~SkPixelRef() {
131 this->callGenIDChangeListeners(); 119 this->callGenIDChangeListeners();
132 } 120 }
133 121
134 void SkPixelRef::needsNewGenID() { 122 void SkPixelRef::needsNewGenID() {
135 fGenerationID = 0; 123 fGenerationID = 0;
136 fUniqueGenerationID = false; 124 fUniqueGenerationID = false;
137 } 125 }
138 126
139 void SkPixelRef::cloneGenID(const SkPixelRef& that) { 127 void SkPixelRef::cloneGenID(const SkPixelRef& that) {
140 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0. 128 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0.
141 this->fGenerationID = that.getGenerationID(); 129 this->fGenerationID = that.getGenerationID();
142 this->fUniqueGenerationID = false; 130 this->fUniqueGenerationID = false;
143 that.fUniqueGenerationID = false; 131 that.fUniqueGenerationID = false;
144 } 132 }
145 133
146 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { 134 void SkPixelRef::setPreLocked(void* pixels, size_t rowBytes, SkColorTable* ctabl e) {
147 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED 135 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED
148 // only call me in your constructor, otherwise fLockCount tracking can get 136 // only call me in your constructor, otherwise fLockCount tracking can get
149 // out of sync. 137 // out of sync.
150 fPixels = pixels; 138 fRec.fPixels = pixels;
151 fColorTable = ctable; 139 fRec.fColorTable = ctable;
140 fRec.fRowBytes = rowBytes;
152 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; 141 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
153 fPreLocked = true; 142 fPreLocked = true;
154 #endif 143 #endif
155 } 144 }
156 145
157 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { 146 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
158 this->INHERITED::flatten(buffer); 147 this->INHERITED::flatten(buffer);
148
149 fInfo.flatten(buffer);
159 buffer.writeBool(fIsImmutable); 150 buffer.writeBool(fIsImmutable);
160 // We write the gen ID into the picture for within-process recording. This 151 // We write the gen ID into the picture for within-process recording. This
161 // is safe since the same genID will never refer to two different sets of 152 // is safe since the same genID will never refer to two different sets of
162 // pixels (barring overflow). However, each process has its own "namespace" 153 // pixels (barring overflow). However, each process has its own "namespace"
163 // of genIDs. So for cross-process recording we write a zero which will 154 // of genIDs. So for cross-process recording we write a zero which will
164 // trigger assignment of a new genID in playback. 155 // trigger assignment of a new genID in playback.
165 if (buffer.isCrossProcess()) { 156 if (buffer.isCrossProcess()) {
166 buffer.writeUInt(0); 157 buffer.writeUInt(0);
167 } else { 158 } else {
168 buffer.writeUInt(fGenerationID); 159 buffer.writeUInt(fGenerationID);
169 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist. 160 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist.
170 } 161 }
171 } 162 }
172 163
173 void SkPixelRef::lockPixels() { 164 bool SkPixelRef::lockPixels(LockRec* rec) {
174 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 165 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
175 166
176 if (!fPreLocked) { 167 if (!fPreLocked) {
177 SkAutoMutexAcquire ac(*fMutex); 168 SkAutoMutexAcquire ac(*fMutex);
178 169
179 if (1 == ++fLockCount) { 170 if (1 == ++fLockCount) {
180 fPixels = this->onLockPixels(&fColorTable); 171 LockRec rec;
172 if (!this->onNewLockPixels(&rec)) {
173 return false;
174 }
175 fRec = rec;
181 } 176 }
182 } 177 }
178 *rec = fRec;
179 return true;
180 }
181
182 bool SkPixelRef::lockPixels() {
183 LockRec rec;
184 return this->lockPixels(&rec);
183 } 185 }
184 186
185 void SkPixelRef::unlockPixels() { 187 void SkPixelRef::unlockPixels() {
186 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 188 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
187 189
188 if (!fPreLocked) { 190 if (!fPreLocked) {
189 SkAutoMutexAcquire ac(*fMutex); 191 SkAutoMutexAcquire ac(*fMutex);
190 192
191 SkASSERT(fLockCount > 0); 193 SkASSERT(fLockCount > 0);
192 if (0 == --fLockCount) { 194 if (0 == --fLockCount) {
193 this->onUnlockPixels(); 195 this->onUnlockPixels();
194 fPixels = NULL; 196 fRec.zero();
195 fColorTable = NULL;
196 } 197 }
197 } 198 }
198 } 199 }
199 200
200 bool SkPixelRef::lockPixelsAreWritable() const { 201 bool SkPixelRef::lockPixelsAreWritable() const {
201 return this->onLockPixelsAreWritable(); 202 return this->onLockPixelsAreWritable();
202 } 203 }
203 204
204 bool SkPixelRef::onLockPixelsAreWritable() const { 205 bool SkPixelRef::onLockPixelsAreWritable() const {
205 return true; 206 return true;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 SkData* SkPixelRef::onRefEncodedData() { 267 SkData* SkPixelRef::onRefEncodedData() {
267 return NULL; 268 return NULL;
268 } 269 }
269 270
270 size_t SkPixelRef::getAllocatedSizeInBytes() const { 271 size_t SkPixelRef::getAllocatedSizeInBytes() const {
271 return 0; 272 return 0;
272 } 273 }
273 274
274 /////////////////////////////////////////////////////////////////////////////// 275 ///////////////////////////////////////////////////////////////////////////////
275 276
277 #ifdef SK_SUPPORT_LEGACY_ONLOCKPIXELS
278
279 void* SkPixelRef::onLockPixels(SkColorTable** ctable) {
280 return NULL;
281 }
282
283 bool SkPixelRef::onNewLockPixels(LockRec* rec) {
284 SkColorTable* ctable;
285 void* pixels = this->onLockPixels(&ctable);
286 if (!pixels) {
287 return false;
288 }
289
290 rec->fPixels = pixels;
291 rec->fColorTable = ctable;
292 rec->fRowBytes = 0; // callers don't currently need this (thank goodness)
293 return true;
294 }
295
296 #endif
297
298 ///////////////////////////////////////////////////////////////////////////////
299
276 #ifdef SK_BUILD_FOR_ANDROID 300 #ifdef SK_BUILD_FOR_ANDROID
277 void SkPixelRef::globalRef(void* data) { 301 void SkPixelRef::globalRef(void* data) {
278 this->ref(); 302 this->ref();
279 } 303 }
280 304
281 void SkPixelRef::globalUnref() { 305 void SkPixelRef::globalUnref() {
282 this->unref(); 306 this->unref();
283 } 307 }
284 #endif 308 #endif
OLDNEW
« no previous file with comments | « src/core/SkMaskFilter.cpp ('k') | src/effects/gradients/SkGradientShader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698