ReadPixelsTest.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include <initializer_list>
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkSurface.h"
  10. #include "include/gpu/GrContext.h"
  11. #include "include/private/SkColorData.h"
  12. #include "include/private/SkHalf.h"
  13. #include "include/private/SkImageInfoPriv.h"
  14. #include "src/core/SkAutoPixmapStorage.h"
  15. #include "src/core/SkConvertPixels.h"
  16. #include "src/core/SkMathPriv.h"
  17. #include "src/gpu/GrContextPriv.h"
  18. #include "src/gpu/GrProxyProvider.h"
  19. #include "src/gpu/SkGr.h"
  20. #include "tests/Test.h"
  21. #include "tests/TestUtils.h"
  22. #include "tools/gpu/GrContextFactory.h"
  23. #include "tools/gpu/ProxyUtils.h"
  24. static const int DEV_W = 100, DEV_H = 100;
  25. static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
  26. static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
  27. DEV_H * SK_Scalar1);
  28. static SkPMColor get_src_color(int x, int y) {
  29. SkASSERT(x >= 0 && x < DEV_W);
  30. SkASSERT(y >= 0 && y < DEV_H);
  31. U8CPU r = x;
  32. U8CPU g = y;
  33. U8CPU b = 0xc;
  34. U8CPU a = 0xff;
  35. switch ((x+y) % 5) {
  36. case 0:
  37. a = 0xff;
  38. break;
  39. case 1:
  40. a = 0x80;
  41. break;
  42. case 2:
  43. a = 0xCC;
  44. break;
  45. case 4:
  46. a = 0x01;
  47. break;
  48. case 3:
  49. a = 0x00;
  50. break;
  51. }
  52. return SkPremultiplyARGBInline(a, r, g, b);
  53. }
  54. static SkPMColor get_dst_bmp_init_color(int x, int y, int w) {
  55. int n = y * w + x;
  56. U8CPU b = n & 0xff;
  57. U8CPU g = (n >> 8) & 0xff;
  58. U8CPU r = (n >> 16) & 0xff;
  59. return SkPackARGB32(0xff, r, g , b);
  60. }
  61. // TODO: Make this consider both ATs
  62. static SkPMColor convert_to_pmcolor(SkColorType ct, SkAlphaType at, const uint32_t* addr,
  63. bool* doUnpremul) {
  64. *doUnpremul = (kUnpremul_SkAlphaType == at);
  65. const uint8_t* c = reinterpret_cast<const uint8_t*>(addr);
  66. U8CPU a,r,g,b;
  67. switch (ct) {
  68. case kBGRA_8888_SkColorType:
  69. b = static_cast<U8CPU>(c[0]);
  70. g = static_cast<U8CPU>(c[1]);
  71. r = static_cast<U8CPU>(c[2]);
  72. a = static_cast<U8CPU>(c[3]);
  73. break;
  74. case kRGB_888x_SkColorType: // fallthrough
  75. case kRGBA_8888_SkColorType:
  76. r = static_cast<U8CPU>(c[0]);
  77. g = static_cast<U8CPU>(c[1]);
  78. b = static_cast<U8CPU>(c[2]);
  79. // We set this even when for kRGB_888x because our caller will validate that it is 0xff.
  80. a = static_cast<U8CPU>(c[3]);
  81. break;
  82. default:
  83. SkDEBUGFAIL("Unexpected colortype");
  84. return 0;
  85. }
  86. if (*doUnpremul) {
  87. r = SkMulDiv255Ceiling(r, a);
  88. g = SkMulDiv255Ceiling(g, a);
  89. b = SkMulDiv255Ceiling(b, a);
  90. }
  91. return SkPackARGB32(a, r, g, b);
  92. }
  93. static SkBitmap make_src_bitmap() {
  94. static SkBitmap bmp;
  95. if (bmp.isNull()) {
  96. bmp.allocN32Pixels(DEV_W, DEV_H);
  97. intptr_t pixels = reinterpret_cast<intptr_t>(bmp.getPixels());
  98. for (int y = 0; y < DEV_H; ++y) {
  99. for (int x = 0; x < DEV_W; ++x) {
  100. SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bmp.rowBytes() + x * bmp.bytesPerPixel());
  101. *pixel = get_src_color(x, y);
  102. }
  103. }
  104. }
  105. return bmp;
  106. }
  107. static void fill_src_canvas(SkCanvas* canvas) {
  108. canvas->save();
  109. canvas->setMatrix(SkMatrix::I());
  110. canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
  111. SkPaint paint;
  112. paint.setBlendMode(SkBlendMode::kSrc);
  113. canvas->drawBitmap(make_src_bitmap(), 0, 0, &paint);
  114. canvas->restore();
  115. }
  116. static void fill_dst_bmp_with_init_data(SkBitmap* bitmap) {
  117. int w = bitmap->width();
  118. int h = bitmap->height();
  119. intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
  120. for (int y = 0; y < h; ++y) {
  121. for (int x = 0; x < w; ++x) {
  122. SkPMColor initColor = get_dst_bmp_init_color(x, y, w);
  123. if (kAlpha_8_SkColorType == bitmap->colorType()) {
  124. uint8_t* alpha = reinterpret_cast<uint8_t*>(pixels + y * bitmap->rowBytes() + x);
  125. *alpha = SkGetPackedA32(initColor);
  126. } else {
  127. SkPMColor* pixel = reinterpret_cast<SkPMColor*>(pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel());
  128. *pixel = initColor;
  129. }
  130. }
  131. }
  132. }
  133. static bool check_read_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
  134. if (!didPremulConversion) {
  135. return a == b;
  136. }
  137. int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
  138. int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
  139. int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
  140. int32_t aB = SkGetPackedB32(a);
  141. int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
  142. int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
  143. int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
  144. int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
  145. return aA == bA &&
  146. SkAbs32(aR - bR) <= 1 &&
  147. SkAbs32(aG - bG) <= 1 &&
  148. SkAbs32(aB - bB) <= 1;
  149. }
  150. // checks the bitmap contains correct pixels after the readPixels
  151. // if the bitmap was prefilled with pixels it checks that these weren't
  152. // overwritten in the area outside the readPixels.
  153. static bool check_read(skiatest::Reporter* reporter, const SkBitmap& bitmap, int x, int y,
  154. bool checkSurfacePixels, bool checkBitmapPixels,
  155. SkImageInfo surfaceInfo) {
  156. SkAlphaType bmpAT = bitmap.alphaType();
  157. SkColorType bmpCT = bitmap.colorType();
  158. SkASSERT(!bitmap.isNull());
  159. SkASSERT(checkSurfacePixels || checkBitmapPixels);
  160. int bw = bitmap.width();
  161. int bh = bitmap.height();
  162. SkIRect srcRect = SkIRect::MakeXYWH(x, y, bw, bh);
  163. SkIRect clippedSrcRect = DEV_RECT;
  164. if (!clippedSrcRect.intersect(srcRect)) {
  165. clippedSrcRect.setEmpty();
  166. }
  167. if (kAlpha_8_SkColorType == bmpCT) {
  168. for (int by = 0; by < bh; ++by) {
  169. for (int bx = 0; bx < bw; ++bx) {
  170. int devx = bx + srcRect.fLeft;
  171. int devy = by + srcRect.fTop;
  172. const uint8_t* alpha = bitmap.getAddr8(bx, by);
  173. if (clippedSrcRect.contains(devx, devy)) {
  174. if (checkSurfacePixels) {
  175. uint8_t surfaceAlpha = (surfaceInfo.alphaType() == kOpaque_SkAlphaType)
  176. ? 0xFF
  177. : SkGetPackedA32(get_src_color(devx, devy));
  178. if (surfaceAlpha != *alpha) {
  179. ERRORF(reporter,
  180. "Expected readback alpha (%d, %d) value 0x%02x, got 0x%02x. ",
  181. bx, by, surfaceAlpha, *alpha);
  182. return false;
  183. }
  184. }
  185. } else if (checkBitmapPixels) {
  186. uint32_t origDstAlpha = SkGetPackedA32(get_dst_bmp_init_color(bx, by, bw));
  187. if (origDstAlpha != *alpha) {
  188. ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
  189. "Expected 0x%02x, got 0x%02x", origDstAlpha, *alpha);
  190. return false;
  191. }
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. for (int by = 0; by < bh; ++by) {
  198. for (int bx = 0; bx < bw; ++bx) {
  199. int devx = bx + srcRect.fLeft;
  200. int devy = by + srcRect.fTop;
  201. const uint32_t* pixel = bitmap.getAddr32(bx, by);
  202. if (clippedSrcRect.contains(devx, devy)) {
  203. if (checkSurfacePixels) {
  204. SkPMColor surfacePMColor = get_src_color(devx, devy);
  205. if (SkColorTypeIsAlphaOnly(surfaceInfo.colorType())) {
  206. surfacePMColor &= 0xFF000000;
  207. }
  208. if (kOpaque_SkAlphaType == surfaceInfo.alphaType() || kOpaque_SkAlphaType == bmpAT) {
  209. surfacePMColor |= 0xFF000000;
  210. }
  211. bool didPremul;
  212. SkPMColor pmPixel = convert_to_pmcolor(bmpCT, bmpAT, pixel, &didPremul);
  213. if (!check_read_pixel(pmPixel, surfacePMColor, didPremul)) {
  214. ERRORF(reporter,
  215. "Expected readback pixel (%d, %d) value 0x%08x, got 0x%08x. "
  216. "Readback was unpremul: %d",
  217. bx, by, surfacePMColor, pmPixel, didPremul);
  218. return false;
  219. }
  220. }
  221. } else if (checkBitmapPixels) {
  222. uint32_t origDstPixel = get_dst_bmp_init_color(bx, by, bw);
  223. if (origDstPixel != *pixel) {
  224. ERRORF(reporter, "Expected clipped out area of readback to be unchanged. "
  225. "Expected 0x%08x, got 0x%08x", origDstPixel, *pixel);
  226. return false;
  227. }
  228. }
  229. }
  230. }
  231. return true;
  232. }
  233. enum BitmapInit {
  234. kFirstBitmapInit = 0,
  235. kTight_BitmapInit = kFirstBitmapInit,
  236. kRowBytes_BitmapInit,
  237. kRowBytesOdd_BitmapInit,
  238. kLastAligned_BitmapInit = kRowBytes_BitmapInit,
  239. #if 0 // THIS CAUSES ERRORS ON WINDOWS AND SOME ANDROID DEVICES
  240. kLast_BitmapInit = kRowBytesOdd_BitmapInit
  241. #else
  242. kLast_BitmapInit = kLastAligned_BitmapInit
  243. #endif
  244. };
  245. static BitmapInit nextBMI(BitmapInit bmi) {
  246. int x = bmi;
  247. return static_cast<BitmapInit>(++x);
  248. }
  249. static void init_bitmap(SkBitmap* bitmap, const SkIRect& rect, BitmapInit init, SkColorType ct,
  250. SkAlphaType at) {
  251. SkImageInfo info = SkImageInfo::Make(rect.width(), rect.height(), ct, at);
  252. size_t rowBytes = 0;
  253. switch (init) {
  254. case kTight_BitmapInit:
  255. break;
  256. case kRowBytes_BitmapInit:
  257. rowBytes = SkAlign4((info.width() + 16) * info.bytesPerPixel());
  258. break;
  259. case kRowBytesOdd_BitmapInit:
  260. rowBytes = SkAlign4(info.width() * info.bytesPerPixel()) + 3;
  261. break;
  262. default:
  263. SkASSERT(0);
  264. break;
  265. }
  266. bitmap->allocPixels(info, rowBytes);
  267. }
  268. static const struct {
  269. SkColorType fColorType;
  270. SkAlphaType fAlphaType;
  271. } gReadPixelsConfigs[] = {
  272. {kRGBA_8888_SkColorType, kPremul_SkAlphaType},
  273. {kRGBA_8888_SkColorType, kUnpremul_SkAlphaType},
  274. {kRGB_888x_SkColorType, kOpaque_SkAlphaType},
  275. {kBGRA_8888_SkColorType, kPremul_SkAlphaType},
  276. {kBGRA_8888_SkColorType, kUnpremul_SkAlphaType},
  277. {kAlpha_8_SkColorType, kPremul_SkAlphaType},
  278. };
  279. const SkIRect gReadPixelsTestRects[] = {
  280. // entire thing
  281. DEV_RECT,
  282. // larger on all sides
  283. SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
  284. // fully contained
  285. SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
  286. // outside top left
  287. SkIRect::MakeLTRB(-10, -10, -1, -1),
  288. // touching top left corner
  289. SkIRect::MakeLTRB(-10, -10, 0, 0),
  290. // overlapping top left corner
  291. SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
  292. // overlapping top left and top right corners
  293. SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H / 4),
  294. // touching entire top edge
  295. SkIRect::MakeLTRB(-10, -10, DEV_W + 10, 0),
  296. // overlapping top right corner
  297. SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H / 4),
  298. // contained in x, overlapping top edge
  299. SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W / 4, DEV_H / 4),
  300. // outside top right corner
  301. SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
  302. // touching top right corner
  303. SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
  304. // overlapping top left and bottom left corners
  305. SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
  306. // touching entire left edge
  307. SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
  308. // overlapping bottom left corner
  309. SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
  310. // contained in y, overlapping left edge
  311. SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
  312. // outside bottom left corner
  313. SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
  314. // touching bottom left corner
  315. SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
  316. // overlapping bottom left and bottom right corners
  317. SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
  318. // touching entire left edge
  319. SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
  320. // overlapping bottom right corner
  321. SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
  322. // overlapping top right and bottom right corners
  323. SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
  324. };
  325. bool read_should_succeed(const SkIRect& srcRect, const SkImageInfo& dstInfo,
  326. const SkImageInfo& srcInfo) {
  327. return SkIRect::Intersects(srcRect, DEV_RECT) && SkImageInfoValidConversion(dstInfo, srcInfo);
  328. }
  329. static void test_readpixels(skiatest::Reporter* reporter, const sk_sp<SkSurface>& surface,
  330. const SkImageInfo& surfaceInfo, BitmapInit lastBitmapInit) {
  331. SkCanvas* canvas = surface->getCanvas();
  332. fill_src_canvas(canvas);
  333. for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
  334. const SkIRect& srcRect = gReadPixelsTestRects[rect];
  335. for (BitmapInit bmi = kFirstBitmapInit; bmi <= lastBitmapInit; bmi = nextBMI(bmi)) {
  336. for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
  337. SkBitmap bmp;
  338. init_bitmap(&bmp, srcRect, bmi,
  339. gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
  340. // if the bitmap has pixels allocated before the readPixels,
  341. // note that and fill them with pattern
  342. bool startsWithPixels = !bmp.isNull();
  343. if (startsWithPixels) {
  344. fill_dst_bmp_with_init_data(&bmp);
  345. }
  346. uint32_t idBefore = surface->generationID();
  347. bool success = surface->readPixels(bmp, srcRect.fLeft, srcRect.fTop);
  348. uint32_t idAfter = surface->generationID();
  349. // we expect to succeed when the read isn't fully clipped out and the infos are
  350. // compatible.
  351. bool expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
  352. // determine whether we expected the read to succeed.
  353. REPORTER_ASSERT(reporter, expectSuccess == success,
  354. "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
  355. success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
  356. bmp.info().colorType(), bmp.info().alphaType());
  357. // read pixels should never change the gen id
  358. REPORTER_ASSERT(reporter, idBefore == idAfter);
  359. if (success || startsWithPixels) {
  360. check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success,
  361. startsWithPixels, surfaceInfo);
  362. } else {
  363. // if we had no pixels beforehand and the readPixels
  364. // failed then our bitmap should still not have pixels
  365. REPORTER_ASSERT(reporter, bmp.isNull());
  366. }
  367. }
  368. }
  369. }
  370. }
  371. DEF_TEST(ReadPixels, reporter) {
  372. const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
  373. auto surface(SkSurface::MakeRaster(info));
  374. // SW readback fails a premul check when reading back to an unaligned rowbytes.
  375. test_readpixels(reporter, surface, info, kLastAligned_BitmapInit);
  376. }
  377. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Gpu, reporter, ctxInfo) {
  378. static const SkImageInfo kImageInfos[] = {
  379. SkImageInfo::Make(DEV_W, DEV_H, kRGBA_8888_SkColorType, kPremul_SkAlphaType),
  380. SkImageInfo::Make(DEV_W, DEV_H, kBGRA_8888_SkColorType, kPremul_SkAlphaType),
  381. SkImageInfo::Make(DEV_W, DEV_H, kRGB_888x_SkColorType, kOpaque_SkAlphaType),
  382. SkImageInfo::Make(DEV_W, DEV_H, kAlpha_8_SkColorType, kPremul_SkAlphaType),
  383. };
  384. for (const auto& ii : kImageInfos) {
  385. for (auto& origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
  386. sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(
  387. ctxInfo.grContext(), SkBudgeted::kNo, ii, 0, origin, nullptr));
  388. if (!surface) {
  389. continue;
  390. }
  391. test_readpixels(reporter, surface, ii, kLast_BitmapInit);
  392. }
  393. }
  394. }
  395. static void test_readpixels_texture(skiatest::Reporter* reporter,
  396. sk_sp<GrSurfaceContext> sContext,
  397. const SkImageInfo& surfaceInfo) {
  398. for (size_t rect = 0; rect < SK_ARRAY_COUNT(gReadPixelsTestRects); ++rect) {
  399. const SkIRect& srcRect = gReadPixelsTestRects[rect];
  400. for (BitmapInit bmi = kFirstBitmapInit; bmi <= kLast_BitmapInit; bmi = nextBMI(bmi)) {
  401. for (size_t c = 0; c < SK_ARRAY_COUNT(gReadPixelsConfigs); ++c) {
  402. SkBitmap bmp;
  403. init_bitmap(&bmp, srcRect, bmi,
  404. gReadPixelsConfigs[c].fColorType, gReadPixelsConfigs[c].fAlphaType);
  405. // if the bitmap has pixels allocated before the readPixels,
  406. // note that and fill them with pattern
  407. bool startsWithPixels = !bmp.isNull();
  408. // Try doing the read directly from a non-renderable texture
  409. if (startsWithPixels) {
  410. fill_dst_bmp_with_init_data(&bmp);
  411. bool success = sContext->readPixels(bmp.info(), bmp.getPixels(),
  412. bmp.rowBytes(), {srcRect.fLeft, srcRect.fTop});
  413. auto expectSuccess = read_should_succeed(srcRect, bmp.info(), surfaceInfo);
  414. REPORTER_ASSERT(
  415. reporter, expectSuccess == success,
  416. "Read succeed=%d unexpectedly, src ct/at: %d/%d, dst ct/at: %d/%d",
  417. success, surfaceInfo.colorType(), surfaceInfo.alphaType(),
  418. bmp.info().colorType(), bmp.info().alphaType());
  419. if (success) {
  420. check_read(reporter, bmp, srcRect.fLeft, srcRect.fTop, success, true,
  421. surfaceInfo);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ReadPixels_Texture, reporter, ctxInfo) {
  429. GrContext* context = ctxInfo.grContext();
  430. SkBitmap bmp = make_src_bitmap();
  431. // On the GPU we will also try reading back from a non-renderable texture.
  432. for (auto origin : {kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin}) {
  433. for (auto renderable : {GrRenderable::kNo, GrRenderable::kYes}) {
  434. sk_sp<GrTextureProxy> proxy = sk_gpu_test::MakeTextureProxyFromData(
  435. context, renderable, DEV_W, DEV_H, bmp.colorType(), bmp.alphaType(), origin,
  436. bmp.getPixels(), bmp.rowBytes());
  437. sk_sp<GrSurfaceContext> sContext = context->priv().makeWrappedSurfaceContext(
  438. std::move(proxy), SkColorTypeToGrColorType(bmp.colorType()),
  439. kPremul_SkAlphaType);
  440. auto info = SkImageInfo::Make(DEV_W, DEV_H, kN32_SkColorType, kPremul_SkAlphaType);
  441. test_readpixels_texture(reporter, std::move(sContext), info);
  442. }
  443. }
  444. }
  445. ///////////////////////////////////////////////////////////////////////////////////////////////////
  446. static const uint32_t kNumPixels = 5;
  447. // The five reference pixels are: red, green, blue, white, black.
  448. // Five is an interesting number to test because we'll exercise a full 4-wide SIMD vector
  449. // plus a tail pixel.
  450. static const uint32_t rgba[kNumPixels] = {
  451. 0xFF0000FF, 0xFF00FF00, 0xFFFF0000, 0xFFFFFFFF, 0xFF000000
  452. };
  453. static const uint32_t bgra[kNumPixels] = {
  454. 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFFFFFF, 0xFF000000
  455. };
  456. static const uint16_t rgb565[kNumPixels] = {
  457. SK_R16_MASK_IN_PLACE, SK_G16_MASK_IN_PLACE, SK_B16_MASK_IN_PLACE, 0xFFFF, 0x0
  458. };
  459. static const uint16_t rgba4444[kNumPixels] = { 0xF00F, 0x0F0F, 0x00FF, 0xFFFF, 0x000F };
  460. static const uint64_t kRed = (uint64_t) SK_Half1 << 0;
  461. static const uint64_t kGreen = (uint64_t) SK_Half1 << 16;
  462. static const uint64_t kBlue = (uint64_t) SK_Half1 << 32;
  463. static const uint64_t kAlpha = (uint64_t) SK_Half1 << 48;
  464. static const uint64_t f16[kNumPixels] = {
  465. kAlpha | kRed, kAlpha | kGreen, kAlpha | kBlue, kAlpha | kBlue | kGreen | kRed, kAlpha
  466. };
  467. static const uint8_t alpha8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  468. static const uint8_t gray8[kNumPixels] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  469. static const void* five_reference_pixels(SkColorType colorType) {
  470. switch (colorType) {
  471. case kUnknown_SkColorType:
  472. return nullptr;
  473. case kAlpha_8_SkColorType:
  474. return alpha8;
  475. case kRGB_565_SkColorType:
  476. return rgb565;
  477. case kARGB_4444_SkColorType:
  478. return rgba4444;
  479. case kRGBA_8888_SkColorType:
  480. return rgba;
  481. case kBGRA_8888_SkColorType:
  482. return bgra;
  483. case kGray_8_SkColorType:
  484. return gray8;
  485. case kRGBA_F16_SkColorType:
  486. return f16;
  487. default:
  488. return nullptr;
  489. }
  490. SkASSERT(false);
  491. return nullptr;
  492. }
  493. static void test_conversion(skiatest::Reporter* r, const SkImageInfo& dstInfo,
  494. const SkImageInfo& srcInfo) {
  495. if (!SkImageInfoIsValid(srcInfo)) {
  496. return;
  497. }
  498. const void* srcPixels = five_reference_pixels(srcInfo.colorType());
  499. SkPixmap srcPixmap(srcInfo, srcPixels, srcInfo.minRowBytes());
  500. sk_sp<SkImage> src = SkImage::MakeFromRaster(srcPixmap, nullptr, nullptr);
  501. REPORTER_ASSERT(r, src);
  502. // Enough space for 5 pixels when color type is F16, more than enough space in other cases.
  503. uint64_t dstPixels[kNumPixels];
  504. SkPixmap dstPixmap(dstInfo, dstPixels, dstInfo.minRowBytes());
  505. bool success = src->readPixels(dstPixmap, 0, 0);
  506. REPORTER_ASSERT(r, success == SkImageInfoValidConversion(dstInfo, srcInfo));
  507. if (success) {
  508. if (kGray_8_SkColorType == srcInfo.colorType() &&
  509. kGray_8_SkColorType != dstInfo.colorType()) {
  510. // TODO: test (r,g,b) == (gray,gray,gray)?
  511. return;
  512. }
  513. if (kGray_8_SkColorType == dstInfo.colorType() &&
  514. kGray_8_SkColorType != srcInfo.colorType()) {
  515. // TODO: test gray = luminance?
  516. return;
  517. }
  518. if (kAlpha_8_SkColorType == srcInfo.colorType() &&
  519. kAlpha_8_SkColorType != dstInfo.colorType()) {
  520. // TODO: test output = black with this alpha?
  521. return;
  522. }
  523. REPORTER_ASSERT(r, 0 == memcmp(dstPixels, five_reference_pixels(dstInfo.colorType()),
  524. kNumPixels * SkColorTypeBytesPerPixel(dstInfo.colorType())));
  525. }
  526. }
  527. DEF_TEST(ReadPixels_ValidConversion, reporter) {
  528. const SkColorType kColorTypes[] = {
  529. kUnknown_SkColorType,
  530. kAlpha_8_SkColorType,
  531. kRGB_565_SkColorType,
  532. kARGB_4444_SkColorType,
  533. kRGBA_8888_SkColorType,
  534. kBGRA_8888_SkColorType,
  535. kGray_8_SkColorType,
  536. kRGBA_F16_SkColorType,
  537. };
  538. const SkAlphaType kAlphaTypes[] = {
  539. kUnknown_SkAlphaType,
  540. kOpaque_SkAlphaType,
  541. kPremul_SkAlphaType,
  542. kUnpremul_SkAlphaType,
  543. };
  544. const sk_sp<SkColorSpace> kColorSpaces[] = {
  545. nullptr,
  546. SkColorSpace::MakeSRGB(),
  547. };
  548. for (SkColorType dstCT : kColorTypes) {
  549. for (SkAlphaType dstAT: kAlphaTypes) {
  550. for (sk_sp<SkColorSpace> dstCS : kColorSpaces) {
  551. for (SkColorType srcCT : kColorTypes) {
  552. for (SkAlphaType srcAT: kAlphaTypes) {
  553. for (sk_sp<SkColorSpace> srcCS : kColorSpaces) {
  554. test_conversion(reporter,
  555. SkImageInfo::Make(kNumPixels, 1, dstCT, dstAT, dstCS),
  556. SkImageInfo::Make(kNumPixels, 1, srcCT, srcAT, srcCS));
  557. }
  558. }
  559. }
  560. }
  561. }
  562. }
  563. }
  564. static int min_rgb_channel_bits(SkColorType ct) {
  565. switch (ct) {
  566. case kUnknown_SkColorType: return 0;
  567. case kAlpha_8_SkColorType: return 8;
  568. case kRGB_565_SkColorType: return 5;
  569. case kARGB_4444_SkColorType: return 4;
  570. case kRGBA_8888_SkColorType: return 8;
  571. case kRGB_888x_SkColorType: return 8;
  572. case kBGRA_8888_SkColorType: return 8;
  573. case kRGBA_1010102_SkColorType: return 10;
  574. case kRGB_101010x_SkColorType: return 10;
  575. case kGray_8_SkColorType: return 8; // counting gray as "rgb"
  576. case kRGBA_F16Norm_SkColorType: return 10; // just counting the mantissa
  577. case kRGBA_F16_SkColorType: return 10; // just counting the mantissa
  578. case kRGBA_F32_SkColorType: return 23; // just counting the mantissa
  579. }
  580. SK_ABORT("Unexpected color type.");
  581. return 0;
  582. }
  583. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(AsyncReadPixels, reporter, ctxInfo) {
  584. struct Context {
  585. SkPixmap* fPixmap = nullptr;
  586. bool fSuceeded = false;
  587. bool fCalled = false;
  588. };
  589. auto callback = [](SkSurface::ReleaseContext context, const void* data, size_t rowBytes) {
  590. auto* pm = static_cast<Context*>(context)->fPixmap;
  591. static_cast<Context*>(context)->fCalled = true;
  592. if ((static_cast<Context*>(context)->fSuceeded = SkToBool(data))) {
  593. auto dst = static_cast<char*>(pm->writable_addr());
  594. const auto* src = static_cast<const char*>(data);
  595. for (int y = 0; y < pm->height(); ++y, src += rowBytes, dst += pm->rowBytes()) {
  596. memcpy(dst, src, pm->width() * SkColorTypeBytesPerPixel(pm->colorType()));
  597. }
  598. }
  599. };
  600. for (auto origin : {kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin}) {
  601. static constexpr int kW = 16;
  602. static constexpr int kH = 16;
  603. for (int sct = 0; sct <= kLastEnum_SkColorType; ++sct) {
  604. auto surfCT = static_cast<SkColorType>(sct);
  605. auto info = SkImageInfo::Make(kW, kH, surfCT, kPremul_SkAlphaType, nullptr);
  606. auto surf = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo, info, 1,
  607. origin, nullptr);
  608. if (!surf) {
  609. continue;
  610. }
  611. float d = std::sqrt((float)surf->width() * surf->width() +
  612. (float)surf->height() * surf->height());
  613. for (int j = 0; j < surf->height(); ++j) {
  614. for (int i = 0; i < surf->width(); ++i) {
  615. float r = i / (float)surf->width();
  616. float g = 1.f - i / (float)surf->height();
  617. float b = std::sqrt((float)i * i + (float)j * j) / d;
  618. SkPaint paint;
  619. paint.setColor4f(SkColor4f{r, g, b, 1.f}, nullptr);
  620. surf->getCanvas()->drawRect(SkRect::MakeXYWH(i, j, 1, 1), paint);
  621. }
  622. }
  623. for (const auto& rect : {SkIRect::MakeWH(kW, kH), // full size
  624. SkIRect::MakeLTRB(1, 2, kW - 3, kH - 4), // partial
  625. SkIRect::MakeXYWH(1, 1, 0, 0), // empty: fail
  626. SkIRect::MakeWH(kW + 1, kH / 2)}) { // too wide: fail
  627. for (int rct = 0; rct <= kLastEnum_SkColorType; ++rct) {
  628. auto readCT = static_cast<SkColorType>(rct);
  629. for (const sk_sp<SkColorSpace>& readCS :
  630. {sk_sp<SkColorSpace>(), SkColorSpace::MakeSRGBLinear()}) {
  631. SkAutoPixmapStorage result;
  632. Context context;
  633. context.fPixmap = &result;
  634. info = SkImageInfo::Make(rect.width(), rect.height(), readCT,
  635. kPremul_SkAlphaType, readCS);
  636. result.alloc(info);
  637. memset(result.writable_addr(), 0xAB, result.computeByteSize());
  638. // Rescale quality and linearity don't matter since we're doing a non-
  639. // scaling readback.
  640. surf->asyncRescaleAndReadPixels(info, rect, SkSurface::RescaleGamma::kSrc,
  641. kNone_SkFilterQuality, callback, &context);
  642. while (!context.fCalled) {
  643. ctxInfo.grContext()->checkAsyncWorkCompletion();
  644. }
  645. if (rect.isEmpty() || !SkIRect::MakeWH(kW, kH).contains(rect)) {
  646. REPORTER_ASSERT(reporter, !context.fSuceeded);
  647. }
  648. if (context.fSuceeded) {
  649. REPORTER_ASSERT(reporter, readCT != kUnknown_SkColorType &&
  650. !rect.isEmpty());
  651. } else {
  652. // TODO: Support reading to kGray.
  653. auto surfBounds = SkIRect::MakeWH(surf->width(), surf->height());
  654. if (readCT != kUnknown_SkColorType && readCT != kGray_8_SkColorType &&
  655. !rect.isEmpty() && surfBounds.contains(rect)) {
  656. ERRORF(reporter,
  657. "Async read failed. Surf Color Type: %d, Read CT: %d,"
  658. "Rect [%d, %d, %d, %d], origin: %d, CS conversion: %d\n",
  659. surfCT, readCT, rect.fLeft, rect.fTop, rect.fRight,
  660. rect.fBottom, origin, (bool)readCS);
  661. }
  662. continue;
  663. }
  664. // We use a synchronous read as the source of truth.
  665. SkAutoPixmapStorage ref;
  666. ref.alloc(info);
  667. memset(ref.writable_addr(), 0xCD, ref.computeByteSize());
  668. if (!surf->readPixels(ref, rect.fLeft, rect.fTop)) {
  669. continue;
  670. }
  671. // When there is no conversion, don't allow a difference.
  672. float tol = 0.f;
  673. if (readCS || readCT != surfCT) {
  674. // When there is a conversion allow a diff of two values when no
  675. // color space conversion and three otherwise. Except for alpha where
  676. // we allow no difference. Allow intermediate truncation to an 8 bit per
  677. // channel format.
  678. int rgbBits = std::min({min_rgb_channel_bits(readCT),
  679. min_rgb_channel_bits(surfCT), 8});
  680. tol = (readCS ? 3.f : 2.f) / (1 << rgbBits);
  681. }
  682. const float tols[4] = {tol, tol, tol, 0};
  683. auto error = std::function<ComparePixmapsErrorReporter>(
  684. [&](int x, int y, const float diffs[4]) {
  685. SkASSERT(x >= 0 && y >= 0);
  686. ERRORF(reporter,
  687. "Surf Color Type: %d, Read CT: %d, Rect [%d, %d, %d, %d]"
  688. ", origin: %d, CS conversion: %d\n"
  689. "Error at %d, %d. Diff in floats: (%f, %f, %f %f)",
  690. surfCT, readCT, rect.fLeft, rect.fTop, rect.fRight,
  691. rect.fBottom, origin, (bool)readCS, x, y, diffs[0],
  692. diffs[1], diffs[2], diffs[3]);
  693. });
  694. compare_pixels(ref, result, tols, error);
  695. }
  696. }
  697. }
  698. }
  699. }
  700. }