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

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

Issue 1156003002: deprecate calling lockPixels (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 5 years, 7 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 | « src/core/SkCanvas.cpp ('k') | no next file » | 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 2013 Google Inc. 2 * Copyright 2013 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 "SkMipMap.h" 8 #include "SkMipMap.h"
9 #include "SkBitmap.h" 9 #include "SkBitmap.h"
10 #include "SkColorPriv.h" 10 #include "SkColorPriv.h"
11 11
12 static void downsample32_nocheck(void* dst, int, int, const void* srcPtr, const SkBitmap& srcBM) { 12 static void downsample32_nocheck(void* dst, int, int, const void* srcPtr, const SkPixmap& srcPM) {
13 const uint32_t* p = static_cast<const uint32_t*>(srcPtr); 13 const uint32_t* p = static_cast<const uint32_t*>(srcPtr);
14 const uint32_t* baseP = p; 14 const uint32_t* baseP = p;
15 uint32_t c, ag, rb; 15 uint32_t c, ag, rb;
16 16
17 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; 17 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
18 p += 1; 18 p += 1;
19 19
20 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 20 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
21 21
22 p = baseP; 22 p = baseP;
23 p += srcBM.rowBytes() >> 2; 23 p += srcPM.rowBytes() >> 2;
24 24
25 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 25 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
26 p += 1; 26 p += 1;
27 27
28 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 28 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
29 29
30 *(uint32_t*)dst = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); 30 *(uint32_t*)dst = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
31 } 31 }
32 32
33 static void downsample32_check(void* dst, int x, int y, const void* srcPtr, cons t SkBitmap& srcBM) { 33 static void downsample32_check(void* dst, int x, int y, const void* srcPtr, cons t SkPixmap& srcPM) {
34 const uint32_t* p = static_cast<const uint32_t*>(srcPtr); 34 const uint32_t* p = static_cast<const uint32_t*>(srcPtr);
35 const uint32_t* baseP = p; 35 const uint32_t* baseP = p;
36 36
37 x <<= 1; 37 x <<= 1;
38 y <<= 1; 38 y <<= 1;
39 SkASSERT(srcBM.getAddr32(x, y) == p); 39 SkASSERT(srcPM.addr32(x, y) == p);
40 40
41 SkPMColor c, ag, rb; 41 SkPMColor c, ag, rb;
42 42
43 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF; 43 c = *p; ag = (c >> 8) & 0xFF00FF; rb = c & 0xFF00FF;
44 if (x < srcBM.width() - 1) { 44 if (x < srcPM.width() - 1) {
45 p += 1; 45 p += 1;
46 } 46 }
47 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 47 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
48 48
49 p = baseP; 49 p = baseP;
50 if (y < srcBM.height() - 1) { 50 if (y < srcPM.height() - 1) {
51 p += srcBM.rowBytes() >> 2; 51 p += srcPM.rowBytes() >> 2;
52 } 52 }
53 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 53 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
54 if (x < srcBM.width() - 1) { 54 if (x < srcPM.width() - 1) {
55 p += 1; 55 p += 1;
56 } 56 }
57 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF; 57 c = *p; ag += (c >> 8) & 0xFF00FF; rb += c & 0xFF00FF;
58 58
59 *((uint32_t*)dst) = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00); 59 *((uint32_t*)dst) = ((rb >> 2) & 0xFF00FF) | ((ag << 6) & 0xFF00FF00);
60 } 60 }
61 61
62 static inline uint32_t expand16(U16CPU c) { 62 static inline uint32_t expand16(U16CPU c) {
63 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16); 63 return (c & ~SK_G16_MASK_IN_PLACE) | ((c & SK_G16_MASK_IN_PLACE) << 16);
64 } 64 }
65 65
66 // returns dirt in the top 16bits, but we don't care, since we only 66 // returns dirt in the top 16bits, but we don't care, since we only
67 // store the low 16bits. 67 // store the low 16bits.
68 static inline U16CPU pack16(uint32_t c) { 68 static inline U16CPU pack16(uint32_t c) {
69 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE); 69 return (c & ~SK_G16_MASK_IN_PLACE) | ((c >> 16) & SK_G16_MASK_IN_PLACE);
70 } 70 }
71 71
72 static void downsample16(void* dst, int x, int y, const void* srcPtr, const SkBi tmap& srcBM) { 72 static void downsample16(void* dst, int x, int y, const void* srcPtr, const SkPi xmap& srcPM) {
73 const uint16_t* p = static_cast<const uint16_t*>(srcPtr); 73 const uint16_t* p = static_cast<const uint16_t*>(srcPtr);
74 const uint16_t* baseP = p; 74 const uint16_t* baseP = p;
75 75
76 x <<= 1; 76 x <<= 1;
77 y <<= 1; 77 y <<= 1;
78 SkASSERT(srcBM.getAddr16(x, y) == p); 78 SkASSERT(srcPM.addr16(x, y) == p);
79 79
80 SkPMColor c; 80 SkPMColor c;
81 81
82 c = expand16(*p); 82 c = expand16(*p);
83 if (x < srcBM.width() - 1) { 83 if (x < srcPM.width() - 1) {
84 p += 1; 84 p += 1;
85 } 85 }
86 c += expand16(*p); 86 c += expand16(*p);
87 87
88 p = baseP; 88 p = baseP;
89 if (y < srcBM.height() - 1) { 89 if (y < srcPM.height() - 1) {
90 p += srcBM.rowBytes() >> 1; 90 p += srcPM.rowBytes() >> 1;
91 } 91 }
92 c += expand16(*p); 92 c += expand16(*p);
93 if (x < srcBM.width() - 1) { 93 if (x < srcPM.width() - 1) {
94 p += 1; 94 p += 1;
95 } 95 }
96 c += expand16(*p); 96 c += expand16(*p);
97 97
98 *((uint16_t*)dst) = (uint16_t)pack16(c >> 2); 98 *((uint16_t*)dst) = (uint16_t)pack16(c >> 2);
99 } 99 }
100 100
101 static uint32_t expand4444(U16CPU c) { 101 static uint32_t expand4444(U16CPU c) {
102 return (c & 0xF0F) | ((c & ~0xF0F) << 12); 102 return (c & 0xF0F) | ((c & ~0xF0F) << 12);
103 } 103 }
104 104
105 static U16CPU collaps4444(uint32_t c) { 105 static U16CPU collaps4444(uint32_t c) {
106 return (c & 0xF0F) | ((c >> 12) & ~0xF0F); 106 return (c & 0xF0F) | ((c >> 12) & ~0xF0F);
107 } 107 }
108 108
109 static void downsample4444(void* dst, int x, int y, const void* srcPtr, const Sk Bitmap& srcBM) { 109 static void downsample4444(void* dst, int x, int y, const void* srcPtr, const Sk Pixmap& srcPM) {
110 const uint16_t* p = static_cast<const uint16_t*>(srcPtr); 110 const uint16_t* p = static_cast<const uint16_t*>(srcPtr);
111 const uint16_t* baseP = p; 111 const uint16_t* baseP = p;
112 112
113 x <<= 1; 113 x <<= 1;
114 y <<= 1; 114 y <<= 1;
115 SkASSERT(srcBM.getAddr16(x, y) == p); 115 SkASSERT(srcPM.addr16(x, y) == p);
116 116
117 uint32_t c; 117 uint32_t c;
118 118
119 c = expand4444(*p); 119 c = expand4444(*p);
120 if (x < srcBM.width() - 1) { 120 if (x < srcPM.width() - 1) {
121 p += 1; 121 p += 1;
122 } 122 }
123 c += expand4444(*p); 123 c += expand4444(*p);
124 124
125 p = baseP; 125 p = baseP;
126 if (y < srcBM.height() - 1) { 126 if (y < srcPM.height() - 1) {
127 p += srcBM.rowBytes() >> 1; 127 p += srcPM.rowBytes() >> 1;
128 } 128 }
129 c += expand4444(*p); 129 c += expand4444(*p);
130 if (x < srcBM.width() - 1) { 130 if (x < srcPM.width() - 1) {
131 p += 1; 131 p += 1;
132 } 132 }
133 c += expand4444(*p); 133 c += expand4444(*p);
134 134
135 *((uint16_t*)dst) = (uint16_t)collaps4444(c >> 2); 135 *((uint16_t*)dst) = (uint16_t)collaps4444(c >> 2);
136 } 136 }
137 137
138 static void downsample8_nocheck(void* dst, int, int, const void* srcPtr, const S kBitmap& srcBM) { 138 static void downsample8_nocheck(void* dst, int, int, const void* srcPtr, const S kPixmap& srcPM) {
139 const size_t rb = srcBM.rowBytes(); 139 const size_t rb = srcPM.rowBytes();
140 const uint8_t* p = static_cast<const uint8_t*>(srcPtr); 140 const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
141 *(uint8_t*)dst = (p[0] + p[1] + p[rb] + p[rb + 1]) >> 2; 141 *(uint8_t*)dst = (p[0] + p[1] + p[rb] + p[rb + 1]) >> 2;
142 } 142 }
143 143
144 static void downsample8_check(void* dst, int x, int y, const void* srcPtr, const SkBitmap& srcBM) { 144 static void downsample8_check(void* dst, int x, int y, const void* srcPtr, const SkPixmap& srcPM) {
145 const uint8_t* p = static_cast<const uint8_t*>(srcPtr); 145 const uint8_t* p = static_cast<const uint8_t*>(srcPtr);
146 const uint8_t* baseP = p; 146 const uint8_t* baseP = p;
147 147
148 x <<= 1; 148 x <<= 1;
149 y <<= 1; 149 y <<= 1;
150 SkASSERT(srcBM.getAddr8(x, y) == p); 150 SkASSERT(srcPM.addr8(x, y) == p);
151 151
152 unsigned c = *p; 152 unsigned c = *p;
153 if (x < srcBM.width() - 1) { 153 if (x < srcPM.width() - 1) {
154 p += 1; 154 p += 1;
155 } 155 }
156 c += *p; 156 c += *p;
157 157
158 p = baseP; 158 p = baseP;
159 if (y < srcBM.height() - 1) { 159 if (y < srcPM.height() - 1) {
160 p += srcBM.rowBytes(); 160 p += srcPM.rowBytes();
161 } 161 }
162 c += *p; 162 c += *p;
163 if (x < srcBM.width() - 1) { 163 if (x < srcPM.width() - 1) {
164 p += 1; 164 p += 1;
165 } 165 }
166 c += *p; 166 c += *p;
167 167
168 *(uint8_t*)dst = c >> 2; 168 *(uint8_t*)dst = c >> 2;
169 } 169 }
170 170
171 size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) { 171 size_t SkMipMap::AllocLevelsSize(int levelCount, size_t pixelSize) {
172 if (levelCount < 0) { 172 if (levelCount < 0) {
173 return 0; 173 return 0;
174 } 174 }
175 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize; 175 int64_t size = sk_64_mul(levelCount + 1, sizeof(Level)) + pixelSize;
176 if (!sk_64_isS32(size)) { 176 if (!sk_64_isS32(size)) {
177 return 0; 177 return 0;
178 } 178 }
179 return sk_64_asS32(size); 179 return sk_64_asS32(size);
180 } 180 }
181 181
182 typedef void SkDownSampleProc(void*, int x, int y, const void* srcPtr, const SkB itmap& srcBM); 182 typedef void SkDownSampleProc(void*, int x, int y, const void* srcPtr, const SkP ixmap& srcPM);
183 183
184 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) { 184 SkMipMap* SkMipMap::Build(const SkBitmap& src, SkDiscardableFactoryProc fact) {
185 SkDownSampleProc* proc_nocheck, *proc_check; 185 SkDownSampleProc* proc_nocheck, *proc_check;
186 186
187 const SkColorType ct = src.colorType(); 187 const SkColorType ct = src.colorType();
188 const SkAlphaType at = src.alphaType(); 188 const SkAlphaType at = src.alphaType();
189 switch (ct) { 189 switch (ct) {
190 case kRGBA_8888_SkColorType: 190 case kRGBA_8888_SkColorType:
191 case kBGRA_8888_SkColorType: 191 case kBGRA_8888_SkColorType:
192 proc_check = downsample32_check; 192 proc_check = downsample32_check;
193 proc_nocheck = downsample32_nocheck; 193 proc_nocheck = downsample32_nocheck;
194 break; 194 break;
195 case kRGB_565_SkColorType: 195 case kRGB_565_SkColorType:
196 proc_check = downsample16; 196 proc_check = downsample16;
197 proc_nocheck = proc_check; 197 proc_nocheck = proc_check;
198 break; 198 break;
199 case kARGB_4444_SkColorType: 199 case kARGB_4444_SkColorType:
200 proc_check = downsample4444; 200 proc_check = downsample4444;
201 proc_nocheck = proc_check; 201 proc_nocheck = proc_check;
202 break; 202 break;
203 case kAlpha_8_SkColorType: 203 case kAlpha_8_SkColorType:
204 case kGray_8_SkColorType: 204 case kGray_8_SkColorType:
205 proc_check = downsample8_check; 205 proc_check = downsample8_check;
206 proc_nocheck = downsample8_nocheck; 206 proc_nocheck = downsample8_nocheck;
207 break; 207 break;
208 default: 208 default:
209 return NULL; // don't build mipmaps for any other colortypes (yet) 209 return NULL; // don't build mipmaps for any other colortypes (yet)
210 } 210 }
211 211
212 SkAutoLockPixels alp(src);
213 if (!src.readyToDraw()) {
214 return NULL;
215 }
216
217 // whip through our loop to compute the exact size needed 212 // whip through our loop to compute the exact size needed
218 size_t size = 0; 213 size_t size = 0;
219 int countLevels = 0; 214 int countLevels = 0;
220 { 215 {
221 int width = src.width(); 216 int width = src.width();
222 int height = src.height(); 217 int height = src.height();
223 for (;;) { 218 for (;;) {
224 width >>= 1; 219 width >>= 1;
225 height >>= 1; 220 height >>= 1;
226 if (0 == width || 0 == height) { 221 if (0 == width || 0 == height) {
227 break; 222 break;
228 } 223 }
229 size += SkColorTypeMinRowBytes(ct, width) * height; 224 size += SkColorTypeMinRowBytes(ct, width) * height;
230 countLevels += 1; 225 countLevels += 1;
231 } 226 }
232 } 227 }
233 if (0 == countLevels) { 228 if (0 == countLevels) {
234 return NULL; 229 return NULL;
235 } 230 }
236 231
237 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size); 232 size_t storageSize = SkMipMap::AllocLevelsSize(countLevels, size);
238 if (0 == storageSize) { 233 if (0 == storageSize) {
239 return NULL; 234 return NULL;
240 } 235 }
241 236
237 SkAutoPixmapUnlock srcUnlocker;
238 if (!src.requestLock(&srcUnlocker)) {
239 return NULL;
240 }
241 const SkPixmap& srcPixmap = srcUnlocker.pixmap();
242
242 SkMipMap* mipmap; 243 SkMipMap* mipmap;
243 if (fact) { 244 if (fact) {
244 SkDiscardableMemory* dm = fact(storageSize); 245 SkDiscardableMemory* dm = fact(storageSize);
245 if (NULL == dm) { 246 if (NULL == dm) {
246 return NULL; 247 return NULL;
247 } 248 }
248 mipmap = SkNEW_ARGS(SkMipMap, (storageSize, dm)); 249 mipmap = SkNEW_ARGS(SkMipMap, (storageSize, dm));
249 } else { 250 } else {
250 mipmap = SkNEW_ARGS(SkMipMap, (sk_malloc_throw(storageSize), storageSize )); 251 mipmap = SkNEW_ARGS(SkMipMap, (sk_malloc_throw(storageSize), storageSize ));
251 } 252 }
252 253
253 // init 254 // init
254 mipmap->fCount = countLevels; 255 mipmap->fCount = countLevels;
255 mipmap->fLevels = (Level*)mipmap->writable_data(); 256 mipmap->fLevels = (Level*)mipmap->writable_data();
256 257
257 Level* levels = mipmap->fLevels; 258 Level* levels = mipmap->fLevels;
258 uint8_t* baseAddr = (uint8_t*)&levels[countLevels]; 259 uint8_t* baseAddr = (uint8_t*)&levels[countLevels];
259 uint8_t* addr = baseAddr; 260 uint8_t* addr = baseAddr;
260 int width = src.width(); 261 int width = src.width();
261 int height = src.height(); 262 int height = src.height();
262 uint32_t rowBytes; 263 uint32_t rowBytes;
263 SkBitmap srcBM(src); 264 SkPixmap srcPM(srcPixmap);
264 265
265 for (int i = 0; i < countLevels; ++i) { 266 for (int i = 0; i < countLevels; ++i) {
266 width >>= 1; 267 width >>= 1;
267 height >>= 1; 268 height >>= 1;
268 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width)); 269 rowBytes = SkToU32(SkColorTypeMinRowBytes(ct, width));
269 270
270 levels[i].fPixels = addr; 271 levels[i].fPixels = addr;
271 levels[i].fWidth = width; 272 levels[i].fWidth = width;
272 levels[i].fHeight = height; 273 levels[i].fHeight = height;
273 levels[i].fRowBytes = rowBytes; 274 levels[i].fRowBytes = rowBytes;
274 levels[i].fScale = (float)width / src.width(); 275 levels[i].fScale = (float)width / src.width();
275 276
276 SkBitmap dstBM; 277 SkPixmap dstPM(SkImageInfo::Make(width, height, ct, at), addr, rowBytes) ;
277 dstBM.installPixels(SkImageInfo::Make(width, height, ct, at), addr, rowB ytes);
278 278
279 srcBM.lockPixels();
280 const int widthEven = width & ~1; 279 const int widthEven = width & ~1;
281 const int heightEven = height & ~1; 280 const int heightEven = height & ~1;
282 const size_t pixelSize = srcBM.info().bytesPerPixel(); 281 const size_t pixelSize = srcPM.info().bytesPerPixel();
283 282
284 const void* srcBasePtr = srcBM.getPixels(); 283 const void* srcBasePtr = srcPM.addr();
285 void* dstBasePtr = dstBM.getPixels(); 284 void* dstBasePtr = dstPM.writable_addr();
286 for (int y = 0; y < heightEven; y++) { 285 for (int y = 0; y < heightEven; y++) {
287 const void* srcPtr = srcBasePtr; 286 const void* srcPtr = srcBasePtr;
288 void* dstPtr = dstBasePtr; 287 void* dstPtr = dstBasePtr;
289 for (int x = 0; x < widthEven; x++) { 288 for (int x = 0; x < widthEven; x++) {
290 proc_nocheck(dstPtr, x, y, srcPtr, srcBM); 289 proc_nocheck(dstPtr, x, y, srcPtr, srcPM);
291 srcPtr = (char*)srcPtr + pixelSize * 2; 290 srcPtr = (char*)srcPtr + pixelSize * 2;
292 dstPtr = (char*)dstPtr + pixelSize; 291 dstPtr = (char*)dstPtr + pixelSize;
293 } 292 }
294 if (width & 1) { 293 if (width & 1) {
295 proc_check(dstPtr, widthEven, y, srcPtr, srcBM); 294 proc_check(dstPtr, widthEven, y, srcPtr, srcPM);
296 } 295 }
297 296
298 srcBasePtr = (char*)srcBasePtr + srcBM.rowBytes() * 2; 297 srcBasePtr = (char*)srcBasePtr + srcPM.rowBytes() * 2;
299 dstBasePtr = (char*)dstBasePtr + dstBM.rowBytes(); 298 dstBasePtr = (char*)dstBasePtr + dstPM.rowBytes();
300 } 299 }
301 if (height & 1) { 300 if (height & 1) {
302 const void* srcPtr = srcBasePtr; 301 const void* srcPtr = srcBasePtr;
303 void* dstPtr = dstBasePtr; 302 void* dstPtr = dstBasePtr;
304 for (int x = 0; x < width; x++) { 303 for (int x = 0; x < width; x++) {
305 proc_check(dstPtr, x, heightEven, srcPtr, srcBM); 304 proc_check(dstPtr, x, heightEven, srcPtr, srcPM);
306 srcPtr = (char*)srcPtr + pixelSize * 2; 305 srcPtr = (char*)srcPtr + pixelSize * 2;
307 dstPtr = (char*)dstPtr + pixelSize; 306 dstPtr = (char*)dstPtr + pixelSize;
308 } 307 }
309 } 308 }
310 309 srcPM = dstPM;
311 srcBM.unlockPixels();
312
313 srcBM = dstBM;
314 addr += height * rowBytes; 310 addr += height * rowBytes;
315 } 311 }
316 SkASSERT(addr == baseAddr + size); 312 SkASSERT(addr == baseAddr + size);
317 313
318 return mipmap; 314 return mipmap;
319 } 315 }
320 316
321 /////////////////////////////////////////////////////////////////////////////// 317 ///////////////////////////////////////////////////////////////////////////////
322 318
323 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const { 319 bool SkMipMap::extractLevel(SkScalar scale, Level* levelPtr) const {
(...skipping 20 matching lines...) Expand all
344 } 340 }
345 341
346 if (level > fCount) { 342 if (level > fCount) {
347 level = fCount; 343 level = fCount;
348 } 344 }
349 if (levelPtr) { 345 if (levelPtr) {
350 *levelPtr = fLevels[level - 1]; 346 *levelPtr = fLevels[level - 1];
351 } 347 }
352 return true; 348 return true;
353 } 349 }
OLDNEW
« no previous file with comments | « src/core/SkCanvas.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698