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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); | 178 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b)); |
179 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); | 179 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b)); |
180 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); | 180 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b)); |
181 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); | 181 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b)); |
182 | 182 |
183 return aA == bA && | 183 return aA == bA && |
184 SkAbs32(aR - bR) <= 1 && | 184 SkAbs32(aR - bR) <= 1 && |
185 SkAbs32(aG - bG) <= 1 && | 185 SkAbs32(aG - bG) <= 1 && |
186 SkAbs32(aB - bB) <= 1; | 186 SkAbs32(aB - bB) <= 1; |
187 } | 187 } |
188 | 188 |
robertphillips
2014/06/30 13:36:20
check_write ?
reed1
2014/06/30 14:33:12
Done.
| |
189 static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkB itmap& bitmap, | 189 static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkB itmap& bitmap, |
190 int writeX, int writeY) { | 190 int writeX, int writeY) { |
191 SkImageInfo canvasInfo; | 191 const SkImageInfo canvasInfo = canvas->imageInfo(); |
192 size_t canvasRowBytes; | 192 size_t canvasRowBytes; |
193 const uint32_t* canvasPixels; | 193 const uint32_t* canvasPixels; |
194 | 194 |
195 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels so metimes as well. | 195 // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels so metimes as well. |
196 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call | 196 // At some point this will be unsupported, as we won't allow accessBitmap() to magically call |
197 // readPixels for the client. | 197 // readPixels for the client. |
198 SkBitmap secretDevBitmap; | 198 SkBitmap secretDevBitmap; |
199 { | 199 canvas->readPixels(SkIRect::MakeWH(canvasInfo.width(), canvasInfo.height()), &secretDevBitmap); |
200 SkBaseDevice* dev = canvas->getDevice(); | 200 |
201 if (!dev) { | |
202 return false; | |
203 } | |
204 secretDevBitmap = dev->accessBitmap(false); | |
205 } | |
206 SkAutoLockPixels alp(secretDevBitmap); | 201 SkAutoLockPixels alp(secretDevBitmap); |
207 canvasInfo = secretDevBitmap.info(); | |
208 canvasRowBytes = secretDevBitmap.rowBytes(); | 202 canvasRowBytes = secretDevBitmap.rowBytes(); |
209 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); | 203 canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels()); |
210 | 204 |
211 if (NULL == canvasPixels) { | 205 if (NULL == canvasPixels) { |
212 return false; | 206 return false; |
213 } | 207 } |
214 | 208 |
215 if (canvasInfo.width() != DEV_W || | 209 if (canvasInfo.width() != DEV_W || |
216 canvasInfo.height() != DEV_H || | 210 canvasInfo.height() != DEV_H || |
217 canvasInfo.colorType() != kN32_SkColorType) { | 211 canvasInfo.colorType() != kN32_SkColorType) { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 // | 286 // |
293 static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes ) { | 287 static bool allocRowBytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes ) { |
294 if (!bm->setInfo(info, rowBytes)) { | 288 if (!bm->setInfo(info, rowBytes)) { |
295 return false; | 289 return false; |
296 } | 290 } |
297 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL); | 291 SkPixelRef* pr = SkMallocPixelRef::NewAllocate(info, rowBytes, NULL); |
298 bm->setPixelRef(pr)->unref(); | 292 bm->setPixelRef(pr)->unref(); |
299 return true; | 293 return true; |
300 } | 294 } |
301 | 295 |
302 static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) { | 296 static void free_pixels(void* pixels, void* ctx) { |
297 sk_free(pixels); | |
298 } | |
299 | |
robertphillips
2014/06/30 13:36:20
create_surface ?
reed1
2014/06/30 14:33:12
Done.
| |
300 static SkSurface* createSurface(const CanvasConfig& c, GrContext* grCtx) { | |
301 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); | |
303 switch (c.fDevType) { | 302 switch (c.fDevType) { |
304 case kRaster_DevType: { | 303 case kRaster_DevType: { |
305 SkBitmap bmp; | 304 const size_t rowBytes = c.fTightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100; |
306 size_t rowBytes = c.fTightRowBytes ? 0 : 4 * DEV_W + 100; | 305 const size_t size = info.getSafeSize(rowBytes); |
307 SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H); | 306 void* pixels = sk_malloc_throw(size); |
308 if (!allocRowBytes(&bmp, info, rowBytes)) { | 307 // if rowBytes isn't tight then set the padding to a known value |
309 sk_throw(); | 308 if (!c.fTightRowBytes) { |
310 return NULL; | 309 memset(pixels, DEV_PAD, size); |
311 } | 310 } |
312 // if rowBytes isn't tight then set the padding to a known value | 311 return SkSurface::NewRasterDirectReleaseProc(info, pixels, rowBytes, free_pixels, NULL); |
313 if (rowBytes) { | |
314 SkAutoLockPixels alp(bmp); | |
315 // We'd just use memset here but GCC 4.8.1 throws up a bogus war ning when we do. | |
316 for (size_t i = 0; i < bmp.getSafeSize(); i++) { | |
317 ((uint8_t*)bmp.getPixels())[i] = DEV_PAD; | |
318 } | |
319 } | |
320 return new SkBitmapDevice(bmp); | |
321 } | 312 } |
322 #if SK_SUPPORT_GPU | 313 #if SK_SUPPORT_GPU |
323 case kGpu_BottomLeft_DevType: | 314 case kGpu_BottomLeft_DevType: |
324 case kGpu_TopLeft_DevType: | 315 case kGpu_TopLeft_DevType: |
325 GrTextureDesc desc; | 316 GrTextureDesc desc; |
326 desc.fFlags = kRenderTarget_GrTextureFlagBit; | 317 desc.fFlags = kRenderTarget_GrTextureFlagBit; |
327 desc.fWidth = DEV_W; | 318 desc.fWidth = DEV_W; |
328 desc.fHeight = DEV_H; | 319 desc.fHeight = DEV_H; |
329 desc.fConfig = kSkia8888_GrPixelConfig; | 320 desc.fConfig = kSkia8888_GrPixelConfig; |
330 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ? | 321 desc.fOrigin = kGpu_TopLeft_DevType == c.fDevType ? |
331 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; | 322 kTopLeft_GrSurfaceOrigin : kBottomLeft_GrSurfaceOrigin; |
332 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMa tch); | 323 GrAutoScratchTexture ast(grCtx, desc, GrContext::kExact_ScratchTexMa tch); |
333 SkAutoTUnref<GrTexture> tex(ast.detach()); | 324 SkAutoTUnref<GrTexture> tex(ast.detach()); |
334 return new SkGpuDevice(grCtx, tex); | 325 return SkSurface::NewRenderTargetDirect(tex->asRenderTarget()); |
335 #endif | 326 #endif |
336 } | 327 } |
337 return NULL; | 328 return NULL; |
338 } | 329 } |
339 | 330 |
340 static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) { | 331 static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) { |
341 size_t rowBytes = tightRB ? 0 : 4 * w + 60; | 332 size_t rowBytes = tightRB ? 0 : 4 * w + 60; |
342 SkImageInfo info = SkImageInfo::Make(w, h, ct, at); | 333 SkImageInfo info = SkImageInfo::Make(w, h, ct, at); |
343 if (!allocRowBytes(bm, info, rowBytes)) { | 334 if (!allocRowBytes(bm, info, rowBytes)) { |
344 return false; | 335 return false; |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 if (!GrContextFactory::IsRenderingGLContext(type)) { | 428 if (!GrContextFactory::IsRenderingGLContext(type)) { |
438 continue; | 429 continue; |
439 } | 430 } |
440 context = factory->get(type); | 431 context = factory->get(type); |
441 if (NULL == context) { | 432 if (NULL == context) { |
442 continue; | 433 continue; |
443 } | 434 } |
444 } | 435 } |
445 #endif | 436 #endif |
446 | 437 |
447 SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], co ntext)); | 438 SkAutoTUnref<SkSurface> surface(createSurface(gCanvasConfigs[i], con text)); |
448 SkCanvas canvas(device); | 439 SkCanvas& canvas = *surface->getCanvas(); |
449 | 440 |
450 static const struct { | 441 static const struct { |
451 SkColorType fColorType; | 442 SkColorType fColorType; |
452 SkAlphaType fAlphaType; | 443 SkAlphaType fAlphaType; |
453 } gSrcConfigs[] = { | 444 } gSrcConfigs[] = { |
454 { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, | 445 { kRGBA_8888_SkColorType, kPremul_SkAlphaType }, |
455 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, | 446 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType }, |
456 { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, | 447 { kBGRA_8888_SkColorType, kPremul_SkAlphaType }, |
457 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, | 448 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType }, |
458 }; | 449 }; |
459 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { | 450 for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) { |
460 const SkIRect& rect = testRects[r]; | 451 const SkIRect& rect = testRects[r]; |
461 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { | 452 for (int tightBmp = 0; tightBmp < 2; ++tightBmp) { |
462 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { | 453 for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) { |
463 const SkColorType ct = gSrcConfigs[c].fColorType; | 454 const SkColorType ct = gSrcConfigs[c].fColorType; |
464 const SkAlphaType at = gSrcConfigs[c].fAlphaType; | 455 const SkAlphaType at = gSrcConfigs[c].fAlphaType; |
465 | 456 |
466 fillCanvas(&canvas); | 457 fillCanvas(&canvas); |
467 SkBitmap bmp; | 458 SkBitmap bmp; |
468 REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect .width(), | 459 REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect .width(), |
469 rect.height(), SkT oBool(tightBmp))); | 460 rect.height(), SkT oBool(tightBmp))); |
470 uint32_t idBefore = canvas.getDevice()->accessBitmap(fal se).getGenerationID(); | 461 uint32_t idBefore = surface->generationID(); |
471 | 462 |
472 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); | 463 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at); |
473 canvas.writePixels(bmp, rect.fLeft, rect.fTop); | 464 canvas.writePixels(bmp, rect.fLeft, rect.fTop); |
474 | 465 |
475 uint32_t idAfter = canvas.getDevice()->accessBitmap(fals e).getGenerationID(); | 466 uint32_t idAfter = surface->generationID(); |
476 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop)); | 467 REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop)); |
477 | 468 |
478 // we should change the genID iff pixels were actually w ritten. | 469 // we should change the genID iff pixels were actually w ritten. |
479 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceS ize()); | 470 SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceS ize()); |
480 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f Top, | 471 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.f Top, |
481 bmp.width(), bmp.h eight()); | 472 bmp.width(), bmp.h eight()); |
482 bool intersects = SkIRect::Intersects(canvasRect, writeR ect) ; | 473 bool intersects = SkIRect::Intersects(canvasRect, writeR ect) ; |
483 REPORTER_ASSERT(reporter, intersects == (idBefore != idA fter)); | 474 REPORTER_ASSERT(reporter, intersects == (idBefore != idA fter)); |
484 } | 475 } |
485 } | 476 } |
486 } | 477 } |
487 } | 478 } |
488 } | 479 } |
489 } | 480 } |
OLD | NEW |