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

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

Issue 68973005: Expand pixelref to return SkImageInfo and rowbytes (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
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 // return a 0 68 // return a 0
69 int32_t genID; 69 int32_t genID;
70 do { 70 do {
71 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1; 71 genID = sk_atomic_inc(&gPixelRefGenerationID) + 1;
72 } while (0 == genID); 72 } while (0 == genID);
73 return genID; 73 return genID;
74 } 74 }
75 75
76 /////////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////////
77 77
78 static void mark_invalid(SkImageInfo* info) {
scroggo 2013/12/04 18:14:03 Any reason to put this here instead of on SkImageI
reed1 2013/12/04 21:12:54 ImageInfo's valid is possibly what I should use, b
79 info->fWidth = -1;
80 }
81
82 static bool is_invalid(const SkImageInfo& info) {
scroggo 2013/12/04 18:14:03 What's the reason for calling this instead of !SkI
reed1 2013/12/04 21:12:54 see above
83 return info.fWidth < 0;
84 }
85
86 ///////////////////////////////////////////////////////////////////////////////
87
78 void SkPixelRef::setMutex(SkBaseMutex* mutex) { 88 void SkPixelRef::setMutex(SkBaseMutex* mutex) {
79 if (NULL == mutex) { 89 if (NULL == mutex) {
80 mutex = get_default_mutex(); 90 mutex = get_default_mutex();
81 } 91 }
82 fMutex = mutex; 92 fMutex = mutex;
83 } 93 }
84 94
85 // just need a > 0 value, so pick a funny one to aid in debugging 95 // just need a > 0 value, so pick a funny one to aid in debugging
86 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789 96 #define SKPIXELREF_PRELOCKED_LOCKCOUNT 123456789
87 97
88 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) { 98 SkPixelRef::SkPixelRef(SkBaseMutex* mutex) {
89 this->setMutex(mutex); 99 this->setMutex(mutex);
90 fPixels = NULL; 100 mark_invalid(&fInfo);
91 fColorTable = NULL; // we do not track ownership of this 101 fRec.zero();
92 fLockCount = 0; 102 fLockCount = 0;
93 this->needsNewGenID(); 103 this->needsNewGenID();
94 fIsImmutable = false; 104 fIsImmutable = false;
95 fPreLocked = false; 105 fPreLocked = false;
96 } 106 }
97 107
98 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex) 108 SkPixelRef::SkPixelRef(SkFlattenableReadBuffer& buffer, SkBaseMutex* mutex)
99 : INHERITED(buffer) { 109 : INHERITED(buffer) {
100 this->setMutex(mutex); 110 this->setMutex(mutex);
101 fPixels = NULL; 111 mark_invalid(&fInfo);
102 fColorTable = NULL; // we do not track ownership of this 112 fRec.zero();
103 fLockCount = 0; 113 fLockCount = 0;
104 fIsImmutable = buffer.readBool(); 114 fIsImmutable = buffer.readBool();
105 fGenerationID = buffer.readUInt(); 115 fGenerationID = buffer.readUInt();
106 fUniqueGenerationID = false; // Conservatively assuming the original still exists. 116 fUniqueGenerationID = false; // Conservatively assuming the original still exists.
107 fPreLocked = false; 117 fPreLocked = false;
108 } 118 }
109 119
110 SkPixelRef::~SkPixelRef() { 120 SkPixelRef::~SkPixelRef() {
111 this->callGenIDChangeListeners(); 121 this->callGenIDChangeListeners();
112 } 122 }
113 123
114 void SkPixelRef::needsNewGenID() { 124 void SkPixelRef::needsNewGenID() {
115 fGenerationID = 0; 125 fGenerationID = 0;
116 fUniqueGenerationID = false; 126 fUniqueGenerationID = false;
117 } 127 }
118 128
119 void SkPixelRef::cloneGenID(const SkPixelRef& that) { 129 void SkPixelRef::cloneGenID(const SkPixelRef& that) {
120 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0. 130 // This is subtle. We must call that.getGenerationID() to make sure its gen ID isn't 0.
121 this->fGenerationID = that.getGenerationID(); 131 this->fGenerationID = that.getGenerationID();
122 this->fUniqueGenerationID = false; 132 this->fUniqueGenerationID = false;
123 that.fUniqueGenerationID = false; 133 that.fUniqueGenerationID = false;
124 } 134 }
125 135
126 void SkPixelRef::setPreLocked(void* pixels, SkColorTable* ctable) { 136 void SkPixelRef::setPreLocked(const SkImageInfo& info, void* pixels,
137 size_t rowBytes, SkColorTable* ctable) {
127 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED 138 #ifndef SK_IGNORE_PIXELREF_SETPRELOCKED
128 // only call me in your constructor, otherwise fLockCount tracking can get 139 // only call me in your constructor, otherwise fLockCount tracking can get
129 // out of sync. 140 // out of sync.
130 fPixels = pixels; 141 fInfo = info;
131 fColorTable = ctable; 142 fRec.fPixels = pixels;
143 fRec.fColorTable = ctable;
144 fRec.fRowBytes = rowBytes;
132 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT; 145 fLockCount = SKPIXELREF_PRELOCKED_LOCKCOUNT;
133 fPreLocked = true; 146 fPreLocked = true;
134 #endif 147 #endif
135 } 148 }
136 149
137 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const { 150 void SkPixelRef::flatten(SkFlattenableWriteBuffer& buffer) const {
138 this->INHERITED::flatten(buffer); 151 this->INHERITED::flatten(buffer);
139 buffer.writeBool(fIsImmutable); 152 buffer.writeBool(fIsImmutable);
140 // We write the gen ID into the picture for within-process recording. This 153 // We write the gen ID into the picture for within-process recording. This
141 // is safe since the same genID will never refer to two different sets of 154 // is safe since the same genID will never refer to two different sets of
142 // pixels (barring overflow). However, each process has its own "namespace" 155 // pixels (barring overflow). However, each process has its own "namespace"
143 // of genIDs. So for cross-process recording we write a zero which will 156 // of genIDs. So for cross-process recording we write a zero which will
144 // trigger assignment of a new genID in playback. 157 // trigger assignment of a new genID in playback.
145 if (buffer.isCrossProcess()) { 158 if (buffer.isCrossProcess()) {
146 buffer.writeUInt(0); 159 buffer.writeUInt(0);
147 } else { 160 } else {
148 buffer.writeUInt(fGenerationID); 161 buffer.writeUInt(fGenerationID);
149 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist. 162 fUniqueGenerationID = false; // Conservative, a copy is probably about to exist.
150 } 163 }
151 } 164 }
152 165
153 void SkPixelRef::lockPixels() { 166 bool SkPixelRef::getInfo(SkImageInfo* info) {
154 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
155
156 if (!fPreLocked) { 167 if (!fPreLocked) {
157 SkAutoMutexAcquire ac(*fMutex); 168 SkAutoMutexAcquire ac(*fMutex);
158 169
159 if (1 == ++fLockCount) { 170 if (is_invalid(fInfo)) {
160 fPixels = this->onLockPixels(&fColorTable); 171 if (!this->onGetInfo(&fInfo)) {
172 mark_invalid(&fInfo);
173 return false;
174 }
161 } 175 }
162 } 176 }
177 *info = fInfo;
178 return true;
179 }
180
181 bool SkPixelRef::lockPixels(LockRec* rec) {
182 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
183
184 if (!fPreLocked) {
185 SkAutoMutexAcquire ac(*fMutex);
186
187 if (0 == fLockCount) {
188 fRec.zero();
189 if (!this->onNewLockPixels(&fRec)) {
190 fRec.zero();
191 return false;
192 }
193 }
194 fLockCount += 1;
195 }
196 *rec = fRec;
197 return true;
198 }
199
200 void SkPixelRef::lockPixels() {
201 LockRec rec;
202 this->lockPixels(&rec);
163 } 203 }
164 204
165 void SkPixelRef::unlockPixels() { 205 void SkPixelRef::unlockPixels() {
166 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount); 206 SkASSERT(!fPreLocked || SKPIXELREF_PRELOCKED_LOCKCOUNT == fLockCount);
167 207
168 if (!fPreLocked) { 208 if (!fPreLocked) {
169 SkAutoMutexAcquire ac(*fMutex); 209 SkAutoMutexAcquire ac(*fMutex);
170 210
171 SkASSERT(fLockCount > 0); 211 SkASSERT(fLockCount > 0);
172 if (0 == --fLockCount) { 212 if (0 == --fLockCount) {
173 this->onUnlockPixels(); 213 this->onUnlockPixels();
174 fPixels = NULL; 214 fRec.zero();
175 fColorTable = NULL;
176 } 215 }
177 } 216 }
178 } 217 }
179 218
180 bool SkPixelRef::lockPixelsAreWritable() const { 219 bool SkPixelRef::lockPixelsAreWritable() const {
181 return this->onLockPixelsAreWritable(); 220 return this->onLockPixelsAreWritable();
182 } 221 }
183 222
184 bool SkPixelRef::onLockPixelsAreWritable() const { 223 bool SkPixelRef::onLockPixelsAreWritable() const {
185 return true; 224 return true;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 290
252 #ifdef SK_BUILD_FOR_ANDROID 291 #ifdef SK_BUILD_FOR_ANDROID
253 void SkPixelRef::globalRef(void* data) { 292 void SkPixelRef::globalRef(void* data) {
254 this->ref(); 293 this->ref();
255 } 294 }
256 295
257 void SkPixelRef::globalUnref() { 296 void SkPixelRef::globalUnref() {
258 this->unref(); 297 this->unref();
259 } 298 }
260 #endif 299 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698