2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkColorPriv.h"
10 #include "SkMathPriv.h"
12 #include "SkSurface.h"
16 #include "GrContext.h"
20 #include <initializer_list>
22 static const int DEV_W = 100, DEV_H = 100;
23 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
24 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
27 static SkPMColor get_src_color(int x, int y) {
28 SkASSERT(x >= 0 && x < DEV_W);
29 SkASSERT(y >= 0 && y < DEV_H);
53 return SkPremultiplyARGBInline(a, r, g, b);
56 static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
60 U8CPU g = (n >> 8) & 0xff;
61 U8CPU r = (n >> 16) & 0xff;
62 return SkPackARGB32(0xff, r, g , b);
65 static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
67 *doUnpremul = (kUnpremul_SkAlphaType == at);
69 const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
72 case kBGRA_8888_SkColorType:
73 b = static_cast<U8CPU>(c[0]);
74 g = static_cast<U8CPU>(c[1]);
75 r = static_cast<U8CPU>(c[2]);
76 a = static_cast<U8CPU>(c[3]);
78 case kRGBA_8888_SkColorType:
79 r = static_cast<U8CPU>(c[0]);
80 g = static_cast<U8CPU>(c[1]);
81 b = static_cast<U8CPU>(c[2]);
82 a = static_cast<U8CPU>(c[3]);
85 SkDEBUGFAIL("Unexpected colortype");
90 r = SkMulDiv255Ceiling(r, a);
91 g = SkMulDiv255Ceiling(g, a);
92 b = SkMulDiv255Ceiling(b, a);
94 return SkPackARGB32(a, r, g, b);
97 static SkBitmap make_src_bitmap() {
100 bmp.allocN32Pixels(DEV_W, DEV_H);
101 intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
102 for (int y = 0; y < DEV_H; ++y) {
103 for (int x = 0; x < DEV_W; ++x) {
104 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
105 *pixel = get_src_color(x, y);
112 static void fill_src_canvas(SkCanvas* canvas) {
114 canvas->setMatrix(SkMatrix::I());
115 canvas->clipRect(DEV_RECT_S, SkRegion::kReplace_Op);
117 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
118 canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
123 static void fill_src_texture(GrTexture* texture) {
124 SkBitmap bmp = make_src_bitmap();
126 texture->writePixels(0, 0, DEV_W, DEV_H, kSkia8888_GrPixelConfig, bmp.getPixels(),
132 static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
133 SkASSERT(bitmap->lockPixelsAreWritable());
134 SkAutoLockPixels alp(*bitmap);
135 int w = bitmap->width();
136 int h = bitmap->height();
137 intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
138 for (int y = 0; y < h; ++y) {
139 for (int x = 0; x < w; ++x) {
140 SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
141 *pixel = get_dst_bmp_init_color(x, y, w);
146 static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
147 if (!didPremulConversion) {
150 int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
151 int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
152 int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
153 int32_t aB = SkGetPackedB32(a);
155 int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
156 int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
157 int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
158 int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
161 SkAbs32(aR - bR) <= 1 &&
162 SkAbs32(aG - bG) <= 1 &&
163 SkAbs32(aB - bB) <= 1;
166 // checks the bitmap contains correct pixels after the readPixels
167 // if the bitmap was prefilled with pixels it checks that these weren't
168 // overwritten in the area outside the readPixels.
169 static bool check_read(skiatest::Reporter* reporter,
170 const SkBitmap& bitmap,
172 bool checkCanvasPixels,
173 bool checkBitmapPixels) {
174 SkASSERT(4 == bitmap.bytesPerPixel());
175 SkASSERT(!bitmap.isNull());
176 SkASSERT(checkCanvasPixels || checkBitmapPixels);
178 const SkColorType ct = bitmap.colorType();
179 const SkAlphaType at = bitmap.alphaType();
181 int bw = bitmap.width();
182 int bh = bitmap.height();
184 SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
185 SkIRect clippedSrcRect = DEV_RECT;
186 if (!clippedSrcRect.intersect(srcRect)) {
187 clippedSrcRect.setEmpty();
189 SkAutoLockPixels alp(bitmap);
190 for (int by = 0; by < bh; ++by) {
191 for (int bx = 0; bx < bw; ++bx) {
192 int devx = bx + srcRect.fLeft;
193 int devy = by + srcRect.fTop;
195 const uint32_t* pixel = bitmap.getAddr32(bx, by);
197 if (clippedSrcRect.contains(devx, devy)) {
198 if (checkCanvasPixels) {
199 SkPMColor canvasPixel = get_src_color(devx, devy);
201 SkPMColor pmPixel = convert_to_pmcolor(ct, at, pixel, &didPremul);
202 if (!check_read_pixel(pmPixel, canvasPixel, didPremul)) {
203 ERRORF(reporter, "Expected readback pixel value 0x%08x, got 0x%08x. "
204 "Readback was unpremul: %d", canvasPixel, pmPixel, didPremul);
208 } else if (checkBitmapPixels) {
209 uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
210 if (origDstPixel != *pixel) {
211 ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
212 "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
222 kFirstBitmapInit = 0,
224 kNoPixels_BitmapInit = kFirstBitmapInit,
226 kRowBytes_BitmapInit,
227 kRowBytesOdd_BitmapInit,
229 kLastAligned_BitmapInit = kRowBytes_BitmapInit,
231 #if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
232 kLast_BitmapInit = kRowBytesOdd_BitmapInit
234 kLast_BitmapInit = kLastAligned_BitmapInit
238 static BitmapInit nextBMI(BitmapInit bmi) {
240 return static_cast<BitmapInit>(++x);
243 static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
245 SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
249 case kNoPixels_BitmapInit:
251 case kTight_BitmapInit:
253 case kRowBytes_BitmapInit:
254 rowBytes = (info.width() + 16) * sizeof(SkPMColor);
256 case kRowBytesOdd_BitmapInit:
257 rowBytes = (info.width() * sizeof(SkPMColor)) + 3;
265 bitmap->allocPixels(info, rowBytes);
267 bitmap->setInfo(info, rowBytes);
271 static const struct {
272 SkColorType fColorType;
273 SkAlphaType fAlphaType;
274 } gReadPixelsConfigs[] = {
275 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
276 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
277 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
278 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
280 const SkIRect gReadPixelsTestRects[] = {
283 // larger on all sides
284 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
286 SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
288 SkIRect::MakeLTRB(-10, -10, -1, -1),
289 // touching top left corner
290 SkIRect::MakeLTRB(-10, -10, 0, 0),
291 // overlapping top left corner
292 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
293 // overlapping top left and top right corners
294 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
295 // touching entire top edge
296 SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
297 // overlapping top right corner
298 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
299 // contained in x, overlapping top edge
300 SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
301 // outside top right corner
302 SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
303 // touching top right corner
304 SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
305 // overlapping top left and bottom left corners
306 SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
307 // touching entire left edge
308 SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
309 // overlapping bottom left corner
310 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
311 // contained in y, overlapping left edge
312 SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
313 // outside bottom left corner
314 SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
315 // touching bottom left corner
316 SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
317 // overlapping bottom left and bottom right corners
318 SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
319 // touching entire left edge
320 SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
321 // overlapping bottom right corner
322 SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
323 // overlapping top right and bottom right corners
324 SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
327 static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
328 BitmapInit lastBitmapInit) {
329 SkCanvas* canvas = surface->getCanvas();
330 fill_src_canvas(canvas);
331 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
332 const SkIRect& srcRect = gReadPixelsTestRects[rect];
333 for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
334 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
336 init_bitmap(&bmp, srcRect, bmi,
337 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
339 // if the bitmap has pixels allocated before the readPixels,
340 // note that and fill them with pattern
341 bool startsWithPixels = !bmp.isNull();
342 if (startsWithPixels) {
343 fill_dst_bmp_with_init_data(&bmp);
345 uint32_t idBefore = surface->generationID();
346 bool success = canvas->readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
347 uint32_t idAfter = surface->generationID();
349 // we expect to succeed when the read isn't fully clipped
351 bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
352 // determine whether we expected the read to succeed.
353 REPORTER_ASSERT(reporter, success == expectSuccess);
354 // read pixels should never change the gen id
355 REPORTER_ASSERT(reporter, idBefore == idAfter);
357 if (success || startsWithPixels) {
358 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
359 success, startsWithPixels);
361 // if we had no pixels beforehand and the readPixels
362 // failed then our bitmap should still not have pixels
363 REPORTER_ASSERT(reporter, bmp.isNull());
366 // check the old webkit version of readPixels that clips the
369 bool success = canvas->readPixels(srcRect, &wkbmp);
370 SkIRect clippedRect = DEV_RECT;
371 if (clippedRect.intersect(srcRect)) {
372 REPORTER_ASSERT(reporter, success);
373 REPORTER_ASSERT(reporter, kN32_SkColorType == wkbmp.colorType());
374 REPORTER_ASSERT(reporter, kPremul_SkAlphaType == wkbmp.alphaType());
375 check_read(reporter, wkbmp, clippedRect.fLeft,
376 clippedRect.fTop, true, false);
378 REPORTER_ASSERT(reporter, !success);
383 DEF_TEST(ReadPixels, reporter) {
384 const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
385 auto surface(SkSurface::MakeRaster(info));
386 // SW readback fails a premul check when reading back to an unaligned rowbytes.
387 test_readpixels(reporter, surface, kLastAligned_BitmapInit);
390 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
391 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
393 desc.fFlags = kRenderTarget_GrSurfaceFlag;
395 desc.fHeight = DEV_H;
396 desc.fConfig = kSkia8888_GrPixelConfig;
397 desc.fOrigin = origin;
398 SkAutoTUnref<GrTexture> surfaceTexture(
399 ctxInfo.grContext()->textureProvider()->createTexture(desc, SkBudgeted::kNo));
400 auto surface(SkSurface::MakeRenderTargetDirect(surfaceTexture->asRenderTarget()));
401 desc.fFlags = kNone_GrSurfaceFlags;
402 test_readpixels(reporter, surface, kLast_BitmapInit);
408 static void test_readpixels_texture(skiatest::Reporter* reporter, GrTexture* texture) {
409 fill_src_texture(texture);
410 for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
411 const SkIRect& srcRect = gReadPixelsTestRects[rect];
412 for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
413 for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
415 init_bitmap(&bmp, srcRect, bmi,
416 gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
418 // if the bitmap has pixels allocated before the readPixels,
419 // note that and fill them with pattern
420 bool startsWithPixels = !bmp.isNull();
421 // Try doing the read directly from a non-renderable texture
422 if (startsWithPixels) {
423 fill_dst_bmp_with_init_data(&bmp);
424 GrPixelConfig dstConfig =
425 SkImageInfo2GrPixelConfig(gReadPixelsConfigs[c].fColorType,
426 gReadPixelsConfigs[c].fAlphaType,
427 kLinear_SkColorProfileType,
428 *texture->getContext()->caps());
430 if (gReadPixelsConfigs[c].fAlphaType == kUnpremul_SkAlphaType) {
431 flags = GrContext::kUnpremul_PixelOpsFlag;
434 bool success = texture->readPixels(srcRect.fLeft, srcRect.fTop, bmp.width(),
435 bmp.height(), dstConfig, bmp.getPixels(),
436 bmp.rowBytes(), flags);
438 check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop,
445 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
446 // On the GPU we will also try reading back from a non-renderable texture.
447 for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
448 SkAutoTUnref<GrTexture> texture;
450 desc.fFlags = kRenderTarget_GrSurfaceFlag;
452 desc.fHeight = DEV_H;
453 desc.fConfig = kSkia8888_GrPixelConfig;
454 desc.fOrigin = origin;
455 desc.fFlags = kNone_GrSurfaceFlags;
456 texture.reset(ctxInfo.grContext()->textureProvider()->createTexture(desc,
458 test_readpixels_texture(reporter, texture);
462 /////////////////////
465 // make_ringed_bitmap was lifted from gm/bleed.cpp, as that GM was what showed the following
466 // bug when a change was made to SkImage_Raster.cpp. It is possible that other test bitmaps
467 // would also tickle https://bug.skia.org/4351 but this one is know to do it, so I've pasted the code
468 // here so we have a dependable repro case.
470 // Create a black&white checked texture with 2 1-pixel rings
471 // around the outside edge. The inner ring is red and the outer ring is blue.
472 static void make_ringed_bitmap(SkBitmap* result, int width, int height) {
473 SkASSERT(0 == width % 2 && 0 == height % 2);
475 static const SkPMColor kRed = SkPreMultiplyColor(SK_ColorRED);
476 static const SkPMColor kBlue = SkPreMultiplyColor(SK_ColorBLUE);
477 static const SkPMColor kBlack = SkPreMultiplyColor(SK_ColorBLACK);
478 static const SkPMColor kWhite = SkPreMultiplyColor(SK_ColorWHITE);
480 result->allocN32Pixels(width, height, true);
482 SkPMColor* scanline = result->getAddr32(0, 0);
483 for (int x = 0; x < width; ++x) {
486 scanline = result->getAddr32(0, 1);
488 for (int x = 1; x < width - 1; ++x) {
491 scanline[width-1] = kBlue;
493 for (int y = 2; y < height/2; ++y) {
494 scanline = result->getAddr32(0, y);
497 for (int x = 2; x < width/2; ++x) {
498 scanline[x] = kBlack;
500 for (int x = width/2; x < width-2; ++x) {
501 scanline[x] = kWhite;
503 scanline[width-2] = kRed;
504 scanline[width-1] = kBlue;
507 for (int y = height/2; y < height-2; ++y) {
508 scanline = result->getAddr32(0, y);
511 for (int x = 2; x < width/2; ++x) {
512 scanline[x] = kWhite;
514 for (int x = width/2; x < width-2; ++x) {
515 scanline[x] = kBlack;
517 scanline[width-2] = kRed;
518 scanline[width-1] = kBlue;
521 scanline = result->getAddr32(0, height-2);
523 for (int x = 1; x < width - 1; ++x) {
526 scanline[width-1] = kBlue;
528 scanline = result->getAddr32(0, height-1);
529 for (int x = 0; x < width; ++x) {
532 result->setImmutable();
535 static void compare_textures(skiatest::Reporter* reporter, GrTexture* txa, GrTexture* txb) {
536 REPORTER_ASSERT(reporter, txa->width() == 2);
537 REPORTER_ASSERT(reporter, txa->height() == 2);
538 REPORTER_ASSERT(reporter, txb->width() == 2);
539 REPORTER_ASSERT(reporter, txb->height() == 2);
540 REPORTER_ASSERT(reporter, txa->config() == txb->config());
542 SkPMColor pixelsA[4], pixelsB[4];
543 REPORTER_ASSERT(reporter, txa->readPixels(0, 0, 2, 2, txa->config(), pixelsA));
544 REPORTER_ASSERT(reporter, txb->readPixels(0, 0, 2, 2, txa->config(), pixelsB));
545 REPORTER_ASSERT(reporter, 0 == memcmp(pixelsA, pixelsB, sizeof(pixelsA)));
548 static SkData* draw_into_surface(SkSurface* surf, const SkBitmap& bm, SkFilterQuality quality) {
549 SkCanvas* canvas = surf->getCanvas();
550 canvas->clear(SK_ColorBLUE);
553 paint.setFilterQuality(quality);
555 canvas->translate(40, 100);
557 canvas->scale(20, 30);
558 canvas->translate(-SkScalarHalf(bm.width()), -SkScalarHalf(bm.height()));
559 canvas->drawBitmap(bm, 0, 0, &paint);
561 return surf->makeImageSnapshot()->encode();
564 #include "SkStream.h"
565 static void dump_to_file(const char name[], SkData* data) {
566 SkFILEWStream file(name);
567 file.write(data->data(), data->size());
571 * Test two different ways to turn a subset of a bitmap into a texture
572 * - subset and then upload to a texture
573 * - upload to a texture and then subset
575 * These two techniques result in the same pixels (ala readPixels)
576 * but when we draw them (rotated+scaled) we don't always get the same results.
578 * https://bug.skia.org/4351
580 DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(ReadPixels_Subset_Gpu, reporter, ctxInfo) {
582 make_ringed_bitmap(&bitmap, 6, 6);
583 const SkIRect subset = SkIRect::MakeLTRB(2, 2, 4, 4);
585 // make two textures...
586 SkBitmap bm_subset, tx_subset;
588 // ... one from a texture-subset
589 SkAutoTUnref<GrTexture> fullTx(GrRefCachedBitmapTexture(ctxInfo.grContext(), bitmap,
590 GrTextureParams::ClampNoFilter(),
591 SkSourceGammaTreatment::kRespect));
593 GrWrapTextureInBitmap(fullTx, bitmap.width(), bitmap.height(), true, &tx_full);
594 tx_full.extractSubset(&tx_subset, subset);
596 // ... one from a bitmap-subset
598 bitmap.extractSubset(&tmp_subset, subset);
599 SkAutoTUnref<GrTexture> subsetTx(GrRefCachedBitmapTexture(ctxInfo.grContext(), tmp_subset,
600 GrTextureParams::ClampNoFilter(),
601 SkSourceGammaTreatment::kRespect));
602 GrWrapTextureInBitmap(subsetTx, tmp_subset.width(), tmp_subset.height(), true, &bm_subset);
604 // did we get the same subset?
605 compare_textures(reporter, bm_subset.getTexture(), tx_subset.getTexture());
607 // do they draw the same?
608 const SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
609 auto surfA(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
610 auto surfB(SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info));
614 // BUG: depending on the driver, if we calls this with various quality settings, it
617 SkFilterQuality quality = kLow_SkFilterQuality;
619 SkAutoTUnref<SkData> dataA(draw_into_surface(surfA.get(), bm_subset, quality));
620 SkAutoTUnref<SkData> dataB(draw_into_surface(surfB.get(), tx_subset, quality));
622 REPORTER_ASSERT(reporter, dataA->equals(dataB));
624 dump_to_file("test_image_A.png", dataA);
625 dump_to_file("test_image_B.png", dataB);