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 "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 r = SkMulDiv255Ceiling(r, a); | 129 r = SkMulDiv255Ceiling(r, a); |
130 g = SkMulDiv255Ceiling(g, a); | 130 g = SkMulDiv255Ceiling(g, a); |
131 b = SkMulDiv255Ceiling(b, a); | 131 b = SkMulDiv255Ceiling(b, a); |
132 } | 132 } |
133 return packConfig8888(config8888, a, r, g , b); | 133 return packConfig8888(config8888, a, r, g , b); |
134 } | 134 } |
135 | 135 |
136 static void fillCanvas(SkCanvas* canvas) { | 136 static void fillCanvas(SkCanvas* canvas) { |
137 static SkBitmap bmp; | 137 static SkBitmap bmp; |
138 if (bmp.isNull()) { | 138 if (bmp.isNull()) { |
139 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H); | 139 SkDEBUGCODE(bool alloc = ) bmp.allocN32Pixels(DEV_W, DEV_H); |
140 SkDEBUGCODE(bool alloc = ) bmp.allocPixels(); | |
141 SkASSERT(alloc); | 140 SkASSERT(alloc); |
142 SkAutoLockPixels alp(bmp); | 141 SkAutoLockPixels alp(bmp); |
143 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); | 142 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels()); |
144 for (int y = 0; y < DEV_H; ++y) { | 143 for (int y = 0; y < DEV_H; ++y) { |
145 for (int x = 0; x < DEV_W; ++x) { | 144 for (int x = 0; x < DEV_W; ++x) { |
146 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp
.rowBytes() + x * bmp.bytesPerPixel()); | 145 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp
.rowBytes() + x * bmp.bytesPerPixel()); |
147 *pixel = getCanvasColor(x, y); | 146 *pixel = getCanvasColor(x, y); |
148 } | 147 } |
149 } | 148 } |
150 } | 149 } |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 | 296 |
298 static const CanvasConfig gCanvasConfigs[] = { | 297 static const CanvasConfig gCanvasConfigs[] = { |
299 {kRaster_DevType, true}, | 298 {kRaster_DevType, true}, |
300 {kRaster_DevType, false}, | 299 {kRaster_DevType, false}, |
301 #if SK_SUPPORT_GPU | 300 #if SK_SUPPORT_GPU |
302 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices | 301 {kGpu_BottomLeft_DevType, true}, // row bytes has no meaning on gpu devices |
303 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices | 302 {kGpu_TopLeft_DevType, true}, // row bytes has no meaning on gpu devices |
304 #endif | 303 #endif |
305 }; | 304 }; |
306 | 305 |
| 306 #include "SkMallocPixelRef.h" |
| 307 |
| 308 // This is a tricky pattern, because we have to setConfig+rowBytes AND specify |
| 309 // a custom pixelRef (which also has to specify its rowBytes), so we have to be |
| 310 // sure that the two rowBytes match (and the infos match). |
| 311 // |
| 312 static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes
) { |
| 313 if (!bm->setConfig(info, rowBytes)) { |
| 314 return false; |
| 315 } |
| 316 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL); |
| 317 bm->setPixelRef(pr)->unref(); |
| 318 return true; |
| 319 } |
| 320 |
307 static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { | 321 static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { |
308 switch (c.fDevType) { | 322 switch (c.fDevType) { |
309 case kRaster_DevType: { | 323 case kRaster_DevType: { |
310 SkBitmap bmp; | 324 SkBitmap bmp; |
311 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; | 325 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; |
312 bmp.setConfig(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, rowBytes); | 326 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); |
313 if (!bmp.allocPixels()) { | 327 if (!allocRowBytes(&bmp, info, rowBytes)) { |
314 sk_throw(); | 328 sk_throw(); |
315 return NULL; | 329 return NULL; |
316 } | 330 } |
317 // if rowBytes isn't tight then set the padding to a known value | 331 // if rowBytes isn't tight then set the padding to a known value |
318 if (rowBytes) { | 332 if (rowBytes) { |
319 SkAutoLockPixels alp(bmp); | 333 SkAutoLockPixels alp(bmp); |
320 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize()); | 334 memset(bmp.getPixels(), DEV_PAD, bmp.getSafeSize()); |
321 } | 335 } |
322 return new SkBitmapDevice(bmp); | 336 return new SkBitmapDevice(bmp); |
323 } | 337 } |
(...skipping 13 matching lines...) Expand all Loading... |
337 #endif | 351 #endif |
338 } | 352 } |
339 return NULL; | 353 return NULL; |
340 } | 354 } |
341 | 355 |
342 static bool setupBitmap(SkBitmap* bitmap, | 356 static bool setupBitmap(SkBitmap* bitmap, |
343 SkCanvas::Config8888 config8888, | 357 SkCanvas::Config8888 config8888, |
344 int w, int h, | 358 int w, int h, |
345 bool tightRowBytes) { | 359 bool tightRowBytes) { |
346 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; | 360 size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60; |
347 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h, rowBytes); | 361 SkImageInfo info = SkImageInfo::MakeN32Premul(w, h); |
348 if (!bitmap->allocPixels()) { | 362 if (!allocRowBytes(bitmap, info, rowBytes)) { |
349 return false; | 363 return false; |
350 } | 364 } |
351 SkAutoLockPixels alp(*bitmap); | 365 SkAutoLockPixels alp(*bitmap); |
352 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); | 366 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels()); |
353 for (int y = 0; y < h; ++y) { | 367 for (int y = 0; y < h; ++y) { |
354 for (int x = 0; x < w; ++x) { | 368 for (int x = 0; x < w; ++x) { |
355 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->r
owBytes() + x * 4); | 369 uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->r
owBytes() + x * 4); |
356 *pixel = getBitmapColor(x, y, w, config8888); | 370 *pixel = getBitmapColor(x, y, w, config8888); |
357 } | 371 } |
358 } | 372 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f
Top, | 477 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f
Top, |
464 bmp.width(), bmp.h
eight()); | 478 bmp.width(), bmp.h
eight()); |
465 bool intersects = SkIRect::Intersects(canvasRect, writeR
ect) ; | 479 bool intersects = SkIRect::Intersects(canvasRect, writeR
ect) ; |
466 REPORTER_ASSERT(reporter, intersects == (idBefore != idA
fter)); | 480 REPORTER_ASSERT(reporter, intersects == (idBefore != idA
fter)); |
467 } | 481 } |
468 } | 482 } |
469 } | 483 } |
470 } | 484 } |
471 } | 485 } |
472 } | 486 } |
OLD | NEW |