ImageTest.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  1. /*
  2. * Copyright 2015 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 <functional>
  8. #include <initializer_list>
  9. #include <vector>
  10. #include "include/core/SkBitmap.h"
  11. #include "include/core/SkCanvas.h"
  12. #include "include/core/SkData.h"
  13. #include "include/core/SkImageEncoder.h"
  14. #include "include/core/SkImageGenerator.h"
  15. #include "include/core/SkPicture.h"
  16. #include "include/core/SkPictureRecorder.h"
  17. #include "include/core/SkRRect.h"
  18. #include "include/core/SkSerialProcs.h"
  19. #include "include/core/SkStream.h"
  20. #include "include/core/SkSurface.h"
  21. #include "include/gpu/GrContextThreadSafeProxy.h"
  22. #include "include/gpu/GrTexture.h"
  23. #include "src/core/SkAutoPixmapStorage.h"
  24. #include "src/core/SkColorSpacePriv.h"
  25. #include "src/core/SkImagePriv.h"
  26. #include "src/core/SkMakeUnique.h"
  27. #include "src/core/SkUtils.h"
  28. #include "src/gpu/GrContextPriv.h"
  29. #include "src/gpu/GrGpu.h"
  30. #include "src/gpu/GrResourceCache.h"
  31. #include "src/gpu/SkGr.h"
  32. #include "src/image/SkImage_Base.h"
  33. #include "src/image/SkImage_GpuYUVA.h"
  34. #include "tests/Test.h"
  35. #include "tests/TestUtils.h"
  36. #include "tools/Resources.h"
  37. #include "tools/ToolUtils.h"
  38. using namespace sk_gpu_test;
  39. SkImageInfo read_pixels_info(SkImage* image) {
  40. if (image->colorSpace()) {
  41. return SkImageInfo::MakeS32(image->width(), image->height(), image->alphaType());
  42. }
  43. return SkImageInfo::MakeN32(image->width(), image->height(), image->alphaType());
  44. }
  45. static void assert_equal(skiatest::Reporter* reporter, SkImage* a, const SkIRect* subsetA,
  46. SkImage* b) {
  47. const int widthA = subsetA ? subsetA->width() : a->width();
  48. const int heightA = subsetA ? subsetA->height() : a->height();
  49. REPORTER_ASSERT(reporter, widthA == b->width());
  50. REPORTER_ASSERT(reporter, heightA == b->height());
  51. // see https://bug.skia.org/3965
  52. //REPORTER_ASSERT(reporter, a->isOpaque() == b->isOpaque());
  53. SkAutoPixmapStorage pmapA, pmapB;
  54. pmapA.alloc(read_pixels_info(a));
  55. pmapB.alloc(read_pixels_info(b));
  56. const int srcX = subsetA ? subsetA->x() : 0;
  57. const int srcY = subsetA ? subsetA->y() : 0;
  58. REPORTER_ASSERT(reporter, a->readPixels(pmapA, srcX, srcY));
  59. REPORTER_ASSERT(reporter, b->readPixels(pmapB, 0, 0));
  60. const size_t widthBytes = widthA * 4;
  61. for (int y = 0; y < heightA; ++y) {
  62. REPORTER_ASSERT(reporter, !memcmp(pmapA.addr32(0, y), pmapB.addr32(0, y), widthBytes));
  63. }
  64. }
  65. static void draw_image_test_pattern(SkCanvas* canvas) {
  66. canvas->clear(SK_ColorWHITE);
  67. SkPaint paint;
  68. paint.setColor(SK_ColorBLACK);
  69. canvas->drawRect(SkRect::MakeXYWH(5, 5, 10, 10), paint);
  70. }
  71. static sk_sp<SkImage> create_image() {
  72. const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
  73. auto surface(SkSurface::MakeRaster(info));
  74. draw_image_test_pattern(surface->getCanvas());
  75. return surface->makeImageSnapshot();
  76. }
  77. static sk_sp<SkData> create_image_data(SkImageInfo* info) {
  78. *info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
  79. const size_t rowBytes = info->minRowBytes();
  80. sk_sp<SkData> data(SkData::MakeUninitialized(rowBytes * info->height()));
  81. {
  82. SkBitmap bm;
  83. bm.installPixels(*info, data->writable_data(), rowBytes);
  84. SkCanvas canvas(bm);
  85. draw_image_test_pattern(&canvas);
  86. }
  87. return data;
  88. }
  89. static sk_sp<SkImage> create_data_image() {
  90. SkImageInfo info;
  91. sk_sp<SkData> data(create_image_data(&info));
  92. return SkImage::MakeRasterData(info, std::move(data), info.minRowBytes());
  93. }
  94. static sk_sp<SkImage> create_image_large(int maxTextureSize) {
  95. const SkImageInfo info = SkImageInfo::MakeN32(maxTextureSize + 1, 32, kOpaque_SkAlphaType);
  96. auto surface(SkSurface::MakeRaster(info));
  97. surface->getCanvas()->clear(SK_ColorWHITE);
  98. SkPaint paint;
  99. paint.setColor(SK_ColorBLACK);
  100. surface->getCanvas()->drawRect(SkRect::MakeXYWH(4000, 2, 28000, 30), paint);
  101. return surface->makeImageSnapshot();
  102. }
  103. static sk_sp<SkImage> create_picture_image() {
  104. SkPictureRecorder recorder;
  105. SkCanvas* canvas = recorder.beginRecording(10, 10);
  106. canvas->clear(SK_ColorCYAN);
  107. return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
  108. nullptr, nullptr, SkImage::BitDepth::kU8,
  109. SkColorSpace::MakeSRGB());
  110. };
  111. // Want to ensure that our Release is called when the owning image is destroyed
  112. struct RasterDataHolder {
  113. RasterDataHolder() : fReleaseCount(0) {}
  114. sk_sp<SkData> fData;
  115. int fReleaseCount;
  116. static void Release(const void* pixels, void* context) {
  117. RasterDataHolder* self = static_cast<RasterDataHolder*>(context);
  118. self->fReleaseCount++;
  119. self->fData.reset();
  120. }
  121. };
  122. static sk_sp<SkImage> create_rasterproc_image(RasterDataHolder* dataHolder) {
  123. SkASSERT(dataHolder);
  124. SkImageInfo info;
  125. dataHolder->fData = create_image_data(&info);
  126. return SkImage::MakeFromRaster(SkPixmap(info, dataHolder->fData->data(), info.minRowBytes()),
  127. RasterDataHolder::Release, dataHolder);
  128. }
  129. static sk_sp<SkImage> create_codec_image() {
  130. SkImageInfo info;
  131. sk_sp<SkData> data(create_image_data(&info));
  132. SkBitmap bitmap;
  133. bitmap.installPixels(info, data->writable_data(), info.minRowBytes());
  134. auto src = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 100);
  135. return SkImage::MakeFromEncoded(std::move(src));
  136. }
  137. static sk_sp<SkImage> create_gpu_image(GrContext* context, bool withMips = false) {
  138. const SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
  139. auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
  140. kBottomLeft_GrSurfaceOrigin, nullptr, withMips));
  141. draw_image_test_pattern(surface->getCanvas());
  142. return surface->makeImageSnapshot();
  143. }
  144. static void test_encode(skiatest::Reporter* reporter, SkImage* image) {
  145. const SkIRect ir = SkIRect::MakeXYWH(5, 5, 10, 10);
  146. sk_sp<SkData> origEncoded = image->encodeToData();
  147. REPORTER_ASSERT(reporter, origEncoded);
  148. REPORTER_ASSERT(reporter, origEncoded->size() > 0);
  149. sk_sp<SkImage> decoded(SkImage::MakeFromEncoded(origEncoded));
  150. if (!decoded) {
  151. ERRORF(reporter, "failed to decode image!");
  152. return;
  153. }
  154. REPORTER_ASSERT(reporter, decoded);
  155. assert_equal(reporter, image, nullptr, decoded.get());
  156. // Now see if we can instantiate an image from a subset of the surface/origEncoded
  157. decoded = SkImage::MakeFromEncoded(origEncoded, &ir);
  158. REPORTER_ASSERT(reporter, decoded);
  159. assert_equal(reporter, image, &ir, decoded.get());
  160. }
  161. DEF_TEST(ImageEncode, reporter) {
  162. test_encode(reporter, create_image().get());
  163. }
  164. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageEncode_Gpu, reporter, ctxInfo) {
  165. test_encode(reporter, create_gpu_image(ctxInfo.grContext()).get());
  166. }
  167. DEF_TEST(Image_MakeFromRasterBitmap, reporter) {
  168. const struct {
  169. SkCopyPixelsMode fCPM;
  170. bool fExpectSameAsMutable;
  171. bool fExpectSameAsImmutable;
  172. } recs[] = {
  173. { kIfMutable_SkCopyPixelsMode, false, true },
  174. { kAlways_SkCopyPixelsMode, false, false },
  175. { kNever_SkCopyPixelsMode, true, true },
  176. };
  177. for (auto rec : recs) {
  178. SkPixmap pm;
  179. SkBitmap bm;
  180. bm.allocN32Pixels(100, 100);
  181. auto img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
  182. REPORTER_ASSERT(reporter, img->peekPixels(&pm));
  183. const bool sameMutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
  184. REPORTER_ASSERT(reporter, rec.fExpectSameAsMutable == sameMutable);
  185. REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameMutable);
  186. bm.notifyPixelsChanged(); // force a new generation ID
  187. bm.setImmutable();
  188. img = SkMakeImageFromRasterBitmap(bm, rec.fCPM);
  189. REPORTER_ASSERT(reporter, img->peekPixels(&pm));
  190. const bool sameImmutable = pm.addr32(0, 0) == bm.getAddr32(0, 0);
  191. REPORTER_ASSERT(reporter, rec.fExpectSameAsImmutable == sameImmutable);
  192. REPORTER_ASSERT(reporter, (bm.getGenerationID() == img->uniqueID()) == sameImmutable);
  193. }
  194. }
  195. // Test that image encoding failures do not break picture serialization/deserialization.
  196. DEF_TEST(Image_Serialize_Encoding_Failure, reporter) {
  197. auto surface(SkSurface::MakeRasterN32Premul(100, 100));
  198. surface->getCanvas()->clear(SK_ColorGREEN);
  199. sk_sp<SkImage> image(surface->makeImageSnapshot());
  200. REPORTER_ASSERT(reporter, image);
  201. SkPictureRecorder recorder;
  202. SkCanvas* canvas = recorder.beginRecording(100, 100);
  203. canvas->drawImage(image, 0, 0);
  204. sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
  205. REPORTER_ASSERT(reporter, picture);
  206. REPORTER_ASSERT(reporter, picture->approximateOpCount() > 0);
  207. bool was_called = false;
  208. SkSerialProcs procs;
  209. procs.fImageProc = [](SkImage*, void* called) {
  210. *(bool*)called = true;
  211. return SkData::MakeEmpty();
  212. };
  213. procs.fImageCtx = &was_called;
  214. REPORTER_ASSERT(reporter, !was_called);
  215. auto data = picture->serialize(&procs);
  216. REPORTER_ASSERT(reporter, was_called);
  217. REPORTER_ASSERT(reporter, data && data->size() > 0);
  218. auto deserialized = SkPicture::MakeFromData(data->data(), data->size());
  219. REPORTER_ASSERT(reporter, deserialized);
  220. REPORTER_ASSERT(reporter, deserialized->approximateOpCount() > 0);
  221. }
  222. // Test that a draw that only partially covers the drawing surface isn't
  223. // interpreted as covering the entire drawing surface (i.e., exercise one of the
  224. // conditions of SkCanvas::wouldOverwriteEntireSurface()).
  225. DEF_TEST(Image_RetainSnapshot, reporter) {
  226. const SkPMColor red = SkPackARGB32(0xFF, 0xFF, 0, 0);
  227. const SkPMColor green = SkPackARGB32(0xFF, 0, 0xFF, 0);
  228. SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
  229. auto surface(SkSurface::MakeRaster(info));
  230. surface->getCanvas()->clear(0xFF00FF00);
  231. SkPMColor pixels[4];
  232. memset(pixels, 0xFF, sizeof(pixels)); // init with values we don't expect
  233. const SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(2, 2);
  234. const size_t dstRowBytes = 2 * sizeof(SkPMColor);
  235. sk_sp<SkImage> image1(surface->makeImageSnapshot());
  236. REPORTER_ASSERT(reporter, image1->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
  237. for (size_t i = 0; i < SK_ARRAY_COUNT(pixels); ++i) {
  238. REPORTER_ASSERT(reporter, pixels[i] == green);
  239. }
  240. SkPaint paint;
  241. paint.setBlendMode(SkBlendMode::kSrc);
  242. paint.setColor(SK_ColorRED);
  243. surface->getCanvas()->drawRect(SkRect::MakeXYWH(1, 1, 1, 1), paint);
  244. sk_sp<SkImage> image2(surface->makeImageSnapshot());
  245. REPORTER_ASSERT(reporter, image2->readPixels(dstInfo, pixels, dstRowBytes, 0, 0));
  246. REPORTER_ASSERT(reporter, pixels[0] == green);
  247. REPORTER_ASSERT(reporter, pixels[1] == green);
  248. REPORTER_ASSERT(reporter, pixels[2] == green);
  249. REPORTER_ASSERT(reporter, pixels[3] == red);
  250. }
  251. /////////////////////////////////////////////////////////////////////////////////////////////////
  252. static void make_bitmap_mutable(SkBitmap* bm) {
  253. bm->allocN32Pixels(10, 10);
  254. }
  255. static void make_bitmap_immutable(SkBitmap* bm) {
  256. bm->allocN32Pixels(10, 10);
  257. bm->setImmutable();
  258. }
  259. DEF_TEST(image_newfrombitmap, reporter) {
  260. const struct {
  261. void (*fMakeProc)(SkBitmap*);
  262. bool fExpectPeekSuccess;
  263. bool fExpectSharedID;
  264. bool fExpectLazy;
  265. } rec[] = {
  266. { make_bitmap_mutable, true, false, false },
  267. { make_bitmap_immutable, true, true, false },
  268. };
  269. for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
  270. SkBitmap bm;
  271. rec[i].fMakeProc(&bm);
  272. sk_sp<SkImage> image(SkImage::MakeFromBitmap(bm));
  273. SkPixmap pmap;
  274. const bool sharedID = (image->uniqueID() == bm.getGenerationID());
  275. REPORTER_ASSERT(reporter, sharedID == rec[i].fExpectSharedID);
  276. const bool peekSuccess = image->peekPixels(&pmap);
  277. REPORTER_ASSERT(reporter, peekSuccess == rec[i].fExpectPeekSuccess);
  278. const bool lazy = image->isLazyGenerated();
  279. REPORTER_ASSERT(reporter, lazy == rec[i].fExpectLazy);
  280. }
  281. }
  282. ///////////////////////////////////////////////////////////////////////////////////////////////////
  283. #include "src/core/SkBitmapCache.h"
  284. /*
  285. * This tests the caching (and preemptive purge) of the raster equivalent of a gpu-image.
  286. * We cache it for performance when drawing into a raster surface.
  287. *
  288. * A cleaner test would know if each drawImage call triggered a read-back from the gpu,
  289. * but we don't have that facility (at the moment) so we use a little internal knowledge
  290. * of *how* the raster version is cached, and look for that.
  291. */
  292. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_Gpu2Cpu, reporter, ctxInfo) {
  293. SkImageInfo info = SkImageInfo::MakeN32(20, 20, kOpaque_SkAlphaType);
  294. sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
  295. const auto desc = SkBitmapCacheDesc::Make(image.get());
  296. auto surface(SkSurface::MakeRaster(info));
  297. // now we can test drawing a gpu-backed image into a cpu-backed surface
  298. {
  299. SkBitmap cachedBitmap;
  300. REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
  301. }
  302. surface->getCanvas()->drawImage(image, 0, 0);
  303. {
  304. SkBitmap cachedBitmap;
  305. if (SkBitmapCache::Find(desc, &cachedBitmap)) {
  306. REPORTER_ASSERT(reporter, cachedBitmap.isImmutable());
  307. REPORTER_ASSERT(reporter, cachedBitmap.getPixels());
  308. } else {
  309. // unexpected, but not really a bug, since the cache is global and this test may be
  310. // run w/ other threads competing for its budget.
  311. SkDebugf("SkImage_Gpu2Cpu : cachedBitmap was already purged\n");
  312. }
  313. }
  314. image.reset(nullptr);
  315. {
  316. SkBitmap cachedBitmap;
  317. REPORTER_ASSERT(reporter, !SkBitmapCache::Find(desc, &cachedBitmap));
  318. }
  319. }
  320. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeTextureImage, reporter, contextInfo) {
  321. GrContext* context = contextInfo.grContext();
  322. sk_gpu_test::TestContext* testContext = contextInfo.testContext();
  323. GrContextFactory otherFactory;
  324. ContextInfo otherContextInfo = otherFactory.getContextInfo(contextInfo.type());
  325. testContext->makeCurrent();
  326. std::function<sk_sp<SkImage>()> imageFactories[] = {
  327. create_image,
  328. create_codec_image,
  329. create_data_image,
  330. // Create an image from a picture.
  331. create_picture_image,
  332. // Create a texture image.
  333. [context] { return create_gpu_image(context); },
  334. // Create a texture image with mips
  335. //[context] { return create_gpu_image(context, true); },
  336. // Create a texture image in a another GrContext.
  337. [otherContextInfo] {
  338. auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
  339. sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
  340. otherContextInfo.grContext()->flush();
  341. return otherContextImage;
  342. }
  343. };
  344. sk_sp<SkColorSpace> dstColorSpaces[] ={
  345. nullptr,
  346. SkColorSpace::MakeSRGB(),
  347. };
  348. for (auto& dstColorSpace : dstColorSpaces) {
  349. for (auto mipMapped : {GrMipMapped::kNo, GrMipMapped::kYes}) {
  350. for (auto factory : imageFactories) {
  351. sk_sp<SkImage> image(factory());
  352. if (!image) {
  353. ERRORF(reporter, "Error creating image.");
  354. continue;
  355. }
  356. sk_sp<SkImage> texImage(image->makeTextureImage(context, dstColorSpace.get(),
  357. mipMapped));
  358. if (!texImage) {
  359. GrContext* imageContext = as_IB(image)->context();
  360. // We expect to fail if image comes from a different GrContext.
  361. if (!image->isTextureBacked() || imageContext == context) {
  362. ERRORF(reporter, "makeTextureImage failed.");
  363. }
  364. continue;
  365. }
  366. if (!texImage->isTextureBacked()) {
  367. ERRORF(reporter, "makeTextureImage returned non-texture image.");
  368. continue;
  369. }
  370. if (GrMipMapped::kYes == mipMapped &&
  371. as_IB(texImage)->peekProxy()->mipMapped() != mipMapped &&
  372. context->priv().caps()->mipMapSupport()) {
  373. ERRORF(reporter, "makeTextureImage returned non-mipmapped texture.");
  374. continue;
  375. }
  376. if (image->isTextureBacked()) {
  377. GrSurfaceProxy* origProxy = as_IB(image)->peekProxy();
  378. GrSurfaceProxy* copyProxy = as_IB(texImage)->peekProxy();
  379. if (origProxy->underlyingUniqueID() != copyProxy->underlyingUniqueID()) {
  380. SkASSERT(origProxy->asTextureProxy());
  381. if (GrMipMapped::kNo == mipMapped ||
  382. GrMipMapped::kYes == origProxy->asTextureProxy()->mipMapped()) {
  383. ERRORF(reporter, "makeTextureImage made unnecessary texture copy.");
  384. }
  385. }
  386. }
  387. if (image->width() != texImage->width() || image->height() != texImage->height()) {
  388. ERRORF(reporter, "makeTextureImage changed the image size.");
  389. }
  390. if (image->alphaType() != texImage->alphaType()) {
  391. ERRORF(reporter, "makeTextureImage changed image alpha type.");
  392. }
  393. }
  394. }
  395. context->flush();
  396. }
  397. }
  398. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SkImage_makeNonTextureImage, reporter, contextInfo) {
  399. GrContext* context = contextInfo.grContext();
  400. std::function<sk_sp<SkImage>()> imageFactories[] = {
  401. create_image,
  402. create_codec_image,
  403. create_data_image,
  404. create_picture_image,
  405. [context] { return create_gpu_image(context); },
  406. };
  407. SkColorSpace* legacyColorSpace = nullptr;
  408. for (auto factory : imageFactories) {
  409. sk_sp<SkImage> image = factory();
  410. if (!image->isTextureBacked()) {
  411. REPORTER_ASSERT(reporter, image->makeNonTextureImage().get() == image.get());
  412. if (!(image = image->makeTextureImage(context, legacyColorSpace))) {
  413. continue;
  414. }
  415. }
  416. auto rasterImage = image->makeNonTextureImage();
  417. if (!rasterImage) {
  418. ERRORF(reporter, "makeNonTextureImage failed for texture-backed image.");
  419. }
  420. REPORTER_ASSERT(reporter, !rasterImage->isTextureBacked());
  421. assert_equal(reporter, image.get(), nullptr, rasterImage.get());
  422. }
  423. }
  424. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrContext_colorTypeSupportedAsImage, reporter, ctxInfo) {
  425. GrContext* context = ctxInfo.grContext();
  426. static constexpr int kSize = 10;
  427. for (int ct = 0; ct < kLastEnum_SkColorType; ++ct) {
  428. SkColorType colorType = static_cast<SkColorType>(ct);
  429. bool can = context->colorTypeSupportedAsImage(colorType);
  430. GrBackendTexture backendTex = context->createBackendTexture(
  431. kSize, kSize, colorType, SkColors::kTransparent,
  432. GrMipMapped::kNo, GrRenderable::kNo, GrProtected::kNo);
  433. auto img = SkImage::MakeFromTexture(context, backendTex, kTopLeft_GrSurfaceOrigin,
  434. colorType, kOpaque_SkAlphaType, nullptr);
  435. REPORTER_ASSERT(reporter, can == SkToBool(img),
  436. "colorTypeSupportedAsImage:%d, actual:%d, ct:%d", can, SkToBool(img),
  437. colorType);
  438. img.reset();
  439. context->flush();
  440. context->deleteBackendTexture(backendTex);
  441. }
  442. }
  443. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(UnpremulTextureImage, reporter, ctxInfo) {
  444. SkBitmap bmp;
  445. bmp.allocPixels(
  446. SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kUnpremul_SkAlphaType, nullptr));
  447. for (int y = 0; y < 256; ++y) {
  448. for (int x = 0; x < 256; ++x) {
  449. *bmp.getAddr32(x, y) =
  450. SkColorSetARGB((U8CPU)y, 255 - (U8CPU)y, (U8CPU)x, 255 - (U8CPU)x);
  451. }
  452. }
  453. auto texImage = SkImage::MakeFromBitmap(bmp)->makeTextureImage(ctxInfo.grContext(), nullptr);
  454. if (!texImage || texImage->alphaType() != kUnpremul_SkAlphaType) {
  455. ERRORF(reporter, "Failed to make unpremul texture image.");
  456. return;
  457. }
  458. SkBitmap unpremul;
  459. unpremul.allocPixels(SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType,
  460. kUnpremul_SkAlphaType, nullptr));
  461. if (!texImage->readPixels(unpremul.info(), unpremul.getPixels(), unpremul.rowBytes(), 0,
  462. 0)) {
  463. ERRORF(reporter, "Unpremul readback failed.");
  464. return;
  465. }
  466. for (int y = 0; y < 256; ++y) {
  467. for (int x = 0; x < 256; ++x) {
  468. if (*bmp.getAddr32(x, y) != *unpremul.getAddr32(x, y)) {
  469. ERRORF(reporter, "unpremul(0x%08x)->unpremul(0x%08x) at %d, %d.",
  470. *bmp.getAddr32(x, y), *unpremul.getAddr32(x, y), x, y);
  471. return;
  472. }
  473. }
  474. }
  475. SkBitmap premul;
  476. premul.allocPixels(
  477. SkImageInfo::Make(256, 256, kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr));
  478. if (!texImage->readPixels(premul.info(), premul.getPixels(), premul.rowBytes(), 0, 0)) {
  479. ERRORF(reporter, "Unpremul readback failed.");
  480. return;
  481. }
  482. for (int y = 0; y < 256; ++y) {
  483. for (int x = 0; x < 256; ++x) {
  484. uint32_t origColor = *bmp.getAddr32(x, y);
  485. int32_t origA = (origColor >> 24) & 0xff;
  486. float a = origA / 255.f;
  487. int32_t origB = sk_float_round2int(((origColor >> 16) & 0xff) * a);
  488. int32_t origG = sk_float_round2int(((origColor >> 8) & 0xff) * a);
  489. int32_t origR = sk_float_round2int(((origColor >> 0) & 0xff) * a);
  490. uint32_t read = *premul.getAddr32(x, y);
  491. int32_t readA = (read >> 24) & 0xff;
  492. int32_t readB = (read >> 16) & 0xff;
  493. int32_t readG = (read >> 8) & 0xff;
  494. int32_t readR = (read >> 0) & 0xff;
  495. // We expect that alpha=1 and alpha=0 should come out exact. Otherwise allow a little
  496. // bit of tolerance for GPU vs CPU premul math.
  497. int32_t tol = (origA == 0 || origA == 255) ? 0 : 1;
  498. if (origA != readA || SkTAbs(readB - origB) > tol || SkTAbs(readG - origG) > tol ||
  499. SkTAbs(readR - origR) > tol) {
  500. ERRORF(reporter, "unpremul(0x%08x)->premul(0x%08x) expected(0x%08x) at %d, %d.",
  501. *bmp.getAddr32(x, y), *premul.getAddr32(x, y), origColor, x, y);
  502. return;
  503. }
  504. }
  505. }
  506. }
  507. DEF_GPUTEST(AbandonedContextImage, reporter, options) {
  508. using Factory = sk_gpu_test::GrContextFactory;
  509. for (int ct = 0; ct < Factory::kContextTypeCnt; ++ct) {
  510. auto type = static_cast<Factory::ContextType>(ct);
  511. std::unique_ptr<Factory> factory(new Factory);
  512. if (!factory->get(type)) {
  513. continue;
  514. }
  515. sk_sp<SkImage> img;
  516. auto gsurf = SkSurface::MakeRenderTarget(
  517. factory->get(type), SkBudgeted::kYes,
  518. SkImageInfo::Make(100, 100, kRGBA_8888_SkColorType, kPremul_SkAlphaType), 1,
  519. nullptr);
  520. if (!gsurf) {
  521. continue;
  522. }
  523. img = gsurf->makeImageSnapshot();
  524. gsurf.reset();
  525. auto rsurf = SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(100, 100));
  526. REPORTER_ASSERT(reporter, img->isValid(factory->get(type)));
  527. REPORTER_ASSERT(reporter, img->isValid(rsurf->getCanvas()->getGrContext()));
  528. factory->get(type)->abandonContext();
  529. REPORTER_ASSERT(reporter, !img->isValid(factory->get(type)));
  530. REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
  531. // This shouldn't crash.
  532. rsurf->getCanvas()->drawImage(img, 0, 0);
  533. // Give up all other refs on GrContext.
  534. factory.reset(nullptr);
  535. REPORTER_ASSERT(reporter, !img->isValid(rsurf->getCanvas()->getGrContext()));
  536. // This shouldn't crash.
  537. rsurf->getCanvas()->drawImage(img, 0, 0);
  538. }
  539. }
  540. class EmptyGenerator : public SkImageGenerator {
  541. public:
  542. EmptyGenerator() : SkImageGenerator(SkImageInfo::MakeN32Premul(0, 0)) {}
  543. };
  544. DEF_TEST(ImageEmpty, reporter) {
  545. const SkImageInfo info = SkImageInfo::Make(0, 0, kN32_SkColorType, kPremul_SkAlphaType);
  546. SkPixmap pmap(info, nullptr, 0);
  547. REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterCopy(pmap));
  548. REPORTER_ASSERT(reporter, nullptr == SkImage::MakeRasterData(info, nullptr, 0));
  549. REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromRaster(pmap, nullptr, nullptr));
  550. REPORTER_ASSERT(reporter, nullptr == SkImage::MakeFromGenerator(
  551. skstd::make_unique<EmptyGenerator>()));
  552. }
  553. DEF_TEST(ImageDataRef, reporter) {
  554. SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
  555. size_t rowBytes = info.minRowBytes();
  556. size_t size = info.computeByteSize(rowBytes);
  557. sk_sp<SkData> data = SkData::MakeUninitialized(size);
  558. REPORTER_ASSERT(reporter, data->unique());
  559. sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, rowBytes);
  560. REPORTER_ASSERT(reporter, !data->unique());
  561. image.reset();
  562. REPORTER_ASSERT(reporter, data->unique());
  563. }
  564. static bool has_pixels(const SkPMColor pixels[], int count, SkPMColor expected) {
  565. for (int i = 0; i < count; ++i) {
  566. if (pixels[i] != expected) {
  567. return false;
  568. }
  569. }
  570. return true;
  571. }
  572. static void image_test_read_pixels(skiatest::Reporter* reporter, SkImage* image) {
  573. if (!image) {
  574. ERRORF(reporter, "Failed to create image!");
  575. return;
  576. }
  577. const SkPMColor expected = SkPreMultiplyColor(SK_ColorWHITE);
  578. const SkPMColor notExpected = ~expected;
  579. const int w = 2, h = 2;
  580. const size_t rowBytes = w * sizeof(SkPMColor);
  581. SkPMColor pixels[w*h];
  582. SkImageInfo info;
  583. info = SkImageInfo::MakeUnknown(w, h);
  584. REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, 0));
  585. // out-of-bounds should fail
  586. info = SkImageInfo::MakeN32Premul(w, h);
  587. REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, -w, 0));
  588. REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, -h));
  589. REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, image->width(), 0));
  590. REPORTER_ASSERT(reporter, !image->readPixels(info, pixels, rowBytes, 0, image->height()));
  591. // top-left should succeed
  592. sk_memset32(pixels, notExpected, w*h);
  593. REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, 0, 0));
  594. REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
  595. // bottom-right should succeed
  596. sk_memset32(pixels, notExpected, w*h);
  597. REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
  598. image->width() - w, image->height() - h));
  599. REPORTER_ASSERT(reporter, has_pixels(pixels, w*h, expected));
  600. // partial top-left should succeed
  601. sk_memset32(pixels, notExpected, w*h);
  602. REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes, -1, -1));
  603. REPORTER_ASSERT(reporter, pixels[3] == expected);
  604. REPORTER_ASSERT(reporter, has_pixels(pixels, w*h - 1, notExpected));
  605. // partial bottom-right should succeed
  606. sk_memset32(pixels, notExpected, w*h);
  607. REPORTER_ASSERT(reporter, image->readPixels(info, pixels, rowBytes,
  608. image->width() - 1, image->height() - 1));
  609. REPORTER_ASSERT(reporter, pixels[0] == expected);
  610. REPORTER_ASSERT(reporter, has_pixels(&pixels[1], w*h - 1, notExpected));
  611. }
  612. DEF_TEST(ImageReadPixels, reporter) {
  613. sk_sp<SkImage> image(create_image());
  614. image_test_read_pixels(reporter, image.get());
  615. image = create_data_image();
  616. image_test_read_pixels(reporter, image.get());
  617. RasterDataHolder dataHolder;
  618. image = create_rasterproc_image(&dataHolder);
  619. image_test_read_pixels(reporter, image.get());
  620. image.reset();
  621. REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
  622. image = create_codec_image();
  623. image_test_read_pixels(reporter, image.get());
  624. }
  625. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageReadPixels_Gpu, reporter, ctxInfo) {
  626. image_test_read_pixels(reporter, create_gpu_image(ctxInfo.grContext()).get());
  627. }
  628. static void check_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image,
  629. const SkBitmap& bitmap) {
  630. REPORTER_ASSERT(reporter, image->width() == bitmap.width());
  631. REPORTER_ASSERT(reporter, image->height() == bitmap.height());
  632. REPORTER_ASSERT(reporter, image->alphaType() == bitmap.alphaType());
  633. REPORTER_ASSERT(reporter, bitmap.isImmutable());
  634. REPORTER_ASSERT(reporter, bitmap.getPixels());
  635. const SkImageInfo info = SkImageInfo::MakeN32(1, 1, bitmap.alphaType());
  636. SkPMColor imageColor;
  637. REPORTER_ASSERT(reporter, image->readPixels(info, &imageColor, sizeof(SkPMColor), 0, 0));
  638. REPORTER_ASSERT(reporter, imageColor == *bitmap.getAddr32(0, 0));
  639. }
  640. static void test_legacy_bitmap(skiatest::Reporter* reporter, const SkImage* image) {
  641. if (!image) {
  642. ERRORF(reporter, "Failed to create image.");
  643. return;
  644. }
  645. SkBitmap bitmap;
  646. REPORTER_ASSERT(reporter, image->asLegacyBitmap(&bitmap));
  647. check_legacy_bitmap(reporter, image, bitmap);
  648. // Test subsetting to exercise the rowBytes logic.
  649. SkBitmap tmp;
  650. REPORTER_ASSERT(reporter, bitmap.extractSubset(&tmp, SkIRect::MakeWH(image->width() / 2,
  651. image->height() / 2)));
  652. sk_sp<SkImage> subsetImage(SkImage::MakeFromBitmap(tmp));
  653. REPORTER_ASSERT(reporter, subsetImage.get());
  654. SkBitmap subsetBitmap;
  655. REPORTER_ASSERT(reporter, subsetImage->asLegacyBitmap(&subsetBitmap));
  656. check_legacy_bitmap(reporter, subsetImage.get(), subsetBitmap);
  657. }
  658. DEF_TEST(ImageLegacyBitmap, reporter) {
  659. sk_sp<SkImage> image(create_image());
  660. test_legacy_bitmap(reporter, image.get());
  661. image = create_data_image();
  662. test_legacy_bitmap(reporter, image.get());
  663. RasterDataHolder dataHolder;
  664. image = create_rasterproc_image(&dataHolder);
  665. test_legacy_bitmap(reporter, image.get());
  666. image.reset();
  667. REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
  668. image = create_codec_image();
  669. test_legacy_bitmap(reporter, image.get());
  670. }
  671. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageLegacyBitmap_Gpu, reporter, ctxInfo) {
  672. sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
  673. test_legacy_bitmap(reporter, image.get());
  674. }
  675. static void test_peek(skiatest::Reporter* reporter, SkImage* image, bool expectPeekSuccess) {
  676. if (!image) {
  677. ERRORF(reporter, "Failed to create image!");
  678. return;
  679. }
  680. SkPixmap pm;
  681. bool success = image->peekPixels(&pm);
  682. REPORTER_ASSERT(reporter, expectPeekSuccess == success);
  683. if (success) {
  684. const SkImageInfo& info = pm.info();
  685. REPORTER_ASSERT(reporter, 20 == info.width());
  686. REPORTER_ASSERT(reporter, 20 == info.height());
  687. REPORTER_ASSERT(reporter, kN32_SkColorType == info.colorType());
  688. REPORTER_ASSERT(reporter, kPremul_SkAlphaType == info.alphaType() ||
  689. kOpaque_SkAlphaType == info.alphaType());
  690. REPORTER_ASSERT(reporter, info.minRowBytes() <= pm.rowBytes());
  691. REPORTER_ASSERT(reporter, SkPreMultiplyColor(SK_ColorWHITE) == *pm.addr32(0, 0));
  692. }
  693. }
  694. DEF_TEST(ImagePeek, reporter) {
  695. sk_sp<SkImage> image(create_image());
  696. test_peek(reporter, image.get(), true);
  697. image = create_data_image();
  698. test_peek(reporter, image.get(), true);
  699. RasterDataHolder dataHolder;
  700. image = create_rasterproc_image(&dataHolder);
  701. test_peek(reporter, image.get(), true);
  702. image.reset();
  703. REPORTER_ASSERT(reporter, 1 == dataHolder.fReleaseCount);
  704. image = create_codec_image();
  705. test_peek(reporter, image.get(), false);
  706. }
  707. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImagePeek_Gpu, reporter, ctxInfo) {
  708. sk_sp<SkImage> image(create_gpu_image(ctxInfo.grContext()));
  709. test_peek(reporter, image.get(), false);
  710. }
  711. struct TextureReleaseChecker {
  712. TextureReleaseChecker() : fReleaseCount(0) {}
  713. int fReleaseCount;
  714. static void Release(void* self) {
  715. static_cast<TextureReleaseChecker*>(self)->fReleaseCount++;
  716. }
  717. };
  718. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SkImage_NewFromTextureRelease, reporter, ctxInfo) {
  719. const int kWidth = 10;
  720. const int kHeight = 10;
  721. GrContext* ctx = ctxInfo.grContext();
  722. SkImageInfo ii = SkImageInfo::Make(kWidth, kHeight, SkColorType::kRGBA_8888_SkColorType,
  723. kPremul_SkAlphaType);
  724. GrBackendTexture backendTex;
  725. if (!create_backend_texture(ctx, &backendTex, ii, SkColors::kRed,
  726. GrMipMapped::kNo, GrRenderable::kNo)) {
  727. ERRORF(reporter, "couldn't create backend texture\n");
  728. }
  729. TextureReleaseChecker releaseChecker;
  730. GrSurfaceOrigin texOrigin = kBottomLeft_GrSurfaceOrigin;
  731. sk_sp<SkImage> refImg(
  732. SkImage::MakeFromTexture(ctx, backendTex, texOrigin, kRGBA_8888_SkColorType,
  733. kPremul_SkAlphaType, nullptr,
  734. TextureReleaseChecker::Release, &releaseChecker));
  735. GrSurfaceOrigin readBackOrigin;
  736. GrBackendTexture readBackBackendTex = refImg->getBackendTexture(false, &readBackOrigin);
  737. readBackBackendTex.setPixelConfig(kRGBA_8888_GrPixelConfig);
  738. if (!GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex)) {
  739. ERRORF(reporter, "backend mismatch\n");
  740. }
  741. REPORTER_ASSERT(reporter, GrBackendTexture::TestingOnly_Equals(readBackBackendTex, backendTex));
  742. if (readBackOrigin != texOrigin) {
  743. ERRORF(reporter, "origin mismatch %d %d\n", readBackOrigin, texOrigin);
  744. }
  745. REPORTER_ASSERT(reporter, readBackOrigin == texOrigin);
  746. // Now exercise the release proc
  747. REPORTER_ASSERT(reporter, 0 == releaseChecker.fReleaseCount);
  748. refImg.reset(nullptr); // force a release of the image
  749. REPORTER_ASSERT(reporter, 1 == releaseChecker.fReleaseCount);
  750. delete_backend_texture(ctx, backendTex);
  751. }
  752. static void test_cross_context_image(skiatest::Reporter* reporter, const GrContextOptions& options,
  753. const char* testName,
  754. std::function<sk_sp<SkImage>(GrContext*)> imageMaker) {
  755. for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
  756. GrContextFactory testFactory(options);
  757. GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
  758. ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
  759. GrContext* ctx = ctxInfo.grContext();
  760. if (!ctx) {
  761. continue;
  762. }
  763. // If we don't have proper support for this feature, the factory will fallback to returning
  764. // codec-backed images. Those will "work", but some of our checks will fail because we
  765. // expect the cross-context images not to work on multiple contexts at once.
  766. if (!ctx->priv().caps()->crossContextTextureSupport()) {
  767. continue;
  768. }
  769. // We test three lifetime patterns for a single context:
  770. // 1) Create image, free image
  771. // 2) Create image, draw, flush, free image
  772. // 3) Create image, draw, free image, flush
  773. // ... and then repeat the last two patterns with drawing on a second* context:
  774. // 4) Create image, draw*, flush*, free image
  775. // 5) Create image, draw*, free iamge, flush*
  776. // Case #1: Create image, free image
  777. {
  778. sk_sp<SkImage> refImg(imageMaker(ctx));
  779. refImg.reset(nullptr); // force a release of the image
  780. }
  781. SkImageInfo info = SkImageInfo::MakeN32Premul(128, 128);
  782. sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info);
  783. if (!surface) {
  784. ERRORF(reporter, "SkSurface::MakeRenderTarget failed for %s.", testName);
  785. continue;
  786. }
  787. SkCanvas* canvas = surface->getCanvas();
  788. // Case #2: Create image, draw, flush, free image
  789. {
  790. sk_sp<SkImage> refImg(imageMaker(ctx));
  791. canvas->drawImage(refImg, 0, 0);
  792. surface->flush();
  793. refImg.reset(nullptr); // force a release of the image
  794. }
  795. // Case #3: Create image, draw, free image, flush
  796. {
  797. sk_sp<SkImage> refImg(imageMaker(ctx));
  798. canvas->drawImage(refImg, 0, 0);
  799. refImg.reset(nullptr); // force a release of the image
  800. surface->flush();
  801. }
  802. // Configure second context
  803. sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
  804. ContextInfo otherContextInfo = testFactory.getSharedContextInfo(ctx);
  805. GrContext* otherCtx = otherContextInfo.grContext();
  806. sk_gpu_test::TestContext* otherTestContext = otherContextInfo.testContext();
  807. // Creating a context in a share group may fail
  808. if (!otherCtx) {
  809. continue;
  810. }
  811. surface = SkSurface::MakeRenderTarget(otherCtx, SkBudgeted::kNo, info);
  812. canvas = surface->getCanvas();
  813. // Case #4: Create image, draw*, flush*, free image
  814. {
  815. testContext->makeCurrent();
  816. sk_sp<SkImage> refImg(imageMaker(ctx));
  817. otherTestContext->makeCurrent();
  818. canvas->drawImage(refImg, 0, 0);
  819. surface->flush();
  820. testContext->makeCurrent();
  821. refImg.reset(nullptr); // force a release of the image
  822. }
  823. // Case #5: Create image, draw*, free image, flush*
  824. {
  825. testContext->makeCurrent();
  826. sk_sp<SkImage> refImg(imageMaker(ctx));
  827. otherTestContext->makeCurrent();
  828. canvas->drawImage(refImg, 0, 0);
  829. testContext->makeCurrent();
  830. refImg.reset(nullptr); // force a release of the image
  831. otherTestContext->makeCurrent();
  832. surface->flush();
  833. // This is specifically here for vulkan to guarantee the command buffer will finish
  834. // which is when we call the ReleaseProc.
  835. otherCtx->priv().getGpu()->testingOnly_flushGpuAndSync();
  836. }
  837. // Case #6: Verify that only one context can be using the image at a time
  838. {
  839. testContext->makeCurrent();
  840. sk_sp<SkImage> refImg(imageMaker(ctx));
  841. // Any context should be able to borrow the texture at this point
  842. sk_sp<GrTextureProxy> proxy = as_IB(refImg)->asTextureProxyRef(
  843. ctx, GrSamplerState::ClampNearest(), nullptr);
  844. REPORTER_ASSERT(reporter, proxy);
  845. // But once it's borrowed, no other context should be able to borrow
  846. otherTestContext->makeCurrent();
  847. sk_sp<GrTextureProxy> otherProxy = as_IB(refImg)->asTextureProxyRef(
  848. otherCtx, GrSamplerState::ClampNearest(), nullptr);
  849. REPORTER_ASSERT(reporter, !otherProxy);
  850. // Original context (that's already borrowing) should be okay
  851. testContext->makeCurrent();
  852. sk_sp<GrTextureProxy> proxySecondRef = as_IB(refImg)->asTextureProxyRef(
  853. ctx, GrSamplerState::ClampNearest(), nullptr);
  854. REPORTER_ASSERT(reporter, proxySecondRef);
  855. // Release first ref from the original context
  856. proxy.reset(nullptr);
  857. // We released one proxy but not the other from the current borrowing context. Make sure
  858. // a new context is still not able to borrow the texture.
  859. otherTestContext->makeCurrent();
  860. otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(),
  861. nullptr);
  862. REPORTER_ASSERT(reporter, !otherProxy);
  863. // Release second ref from the original context
  864. testContext->makeCurrent();
  865. proxySecondRef.reset(nullptr);
  866. // Now we should be able to borrow the texture from the other context
  867. otherTestContext->makeCurrent();
  868. otherProxy = as_IB(refImg)->asTextureProxyRef(otherCtx, GrSamplerState::ClampNearest(),
  869. nullptr);
  870. REPORTER_ASSERT(reporter, otherProxy);
  871. // Release everything
  872. otherProxy.reset(nullptr);
  873. refImg.reset(nullptr);
  874. }
  875. }
  876. }
  877. DEF_GPUTEST(SkImage_MakeCrossContextFromEncodedRelease, reporter, options) {
  878. sk_sp<SkData> data = GetResourceAsData("images/mandrill_128.png");
  879. if (!data) {
  880. ERRORF(reporter, "missing resource");
  881. return;
  882. }
  883. test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromEncodedRelease",
  884. [&data](GrContext* ctx) {
  885. return SkImage::MakeCrossContextFromEncoded(ctx, data, false, nullptr);
  886. });
  887. }
  888. DEF_GPUTEST(SkImage_MakeCrossContextFromPixmapRelease, reporter, options) {
  889. SkBitmap bitmap;
  890. SkPixmap pixmap;
  891. if (!GetResourceAsBitmap("images/mandrill_128.png", &bitmap) || !bitmap.peekPixels(&pixmap)) {
  892. ERRORF(reporter, "missing resource");
  893. return;
  894. }
  895. test_cross_context_image(reporter, options, "SkImage_MakeCrossContextFromPixmapRelease",
  896. [&pixmap](GrContext* ctx) {
  897. return SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr);
  898. });
  899. }
  900. DEF_GPUTEST(SkImage_CrossContextGrayAlphaConfigs, reporter, options) {
  901. for (SkColorType ct : { kGray_8_SkColorType, kAlpha_8_SkColorType }) {
  902. SkAutoPixmapStorage pixmap;
  903. pixmap.alloc(SkImageInfo::Make(4, 4, ct, kPremul_SkAlphaType));
  904. for (int i = 0; i < GrContextFactory::kContextTypeCnt; ++i) {
  905. GrContextFactory testFactory(options);
  906. GrContextFactory::ContextType ctxType = static_cast<GrContextFactory::ContextType>(i);
  907. ContextInfo ctxInfo = testFactory.getContextInfo(ctxType);
  908. GrContext* ctx = ctxInfo.grContext();
  909. if (!ctx || !ctx->priv().caps()->crossContextTextureSupport()) {
  910. continue;
  911. }
  912. sk_sp<SkImage> image = SkImage::MakeCrossContextFromPixmap(ctx, pixmap, false, nullptr);
  913. REPORTER_ASSERT(reporter, image);
  914. sk_sp<GrTextureProxy> proxy = as_IB(image)->asTextureProxyRef(
  915. ctx, GrSamplerState::ClampNearest(), nullptr);
  916. REPORTER_ASSERT(reporter, proxy);
  917. bool expectAlpha = kAlpha_8_SkColorType == ct;
  918. REPORTER_ASSERT(reporter, expectAlpha == GrPixelConfigIsAlphaOnly(proxy->config()));
  919. }
  920. }
  921. }
  922. DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(makeBackendTexture, reporter, ctxInfo) {
  923. GrContext* context = ctxInfo.grContext();
  924. sk_gpu_test::TestContext* testContext = ctxInfo.testContext();
  925. sk_sp<GrContextThreadSafeProxy> proxy = context->threadSafeProxy();
  926. GrContextFactory otherFactory;
  927. ContextInfo otherContextInfo = otherFactory.getContextInfo(ctxInfo.type());
  928. testContext->makeCurrent();
  929. REPORTER_ASSERT(reporter, proxy);
  930. auto createLarge = [context] {
  931. return create_image_large(context->priv().caps()->maxTextureSize());
  932. };
  933. struct {
  934. std::function<sk_sp<SkImage> ()> fImageFactory;
  935. bool fExpectation;
  936. bool fCanTakeDirectly;
  937. } testCases[] = {
  938. { create_image, true, false },
  939. { create_codec_image, true, false },
  940. { create_data_image, true, false },
  941. { create_picture_image, true, false },
  942. { [context] { return create_gpu_image(context); }, true, true },
  943. // Create a texture image in a another GrContext.
  944. { [otherContextInfo] {
  945. auto restore = otherContextInfo.testContext()->makeCurrentAndAutoRestore();
  946. sk_sp<SkImage> otherContextImage = create_gpu_image(otherContextInfo.grContext());
  947. otherContextInfo.grContext()->flush();
  948. return otherContextImage;
  949. }, false, false },
  950. // Create an image that is too large to be texture backed.
  951. { createLarge, false, false }
  952. };
  953. for (auto testCase : testCases) {
  954. sk_sp<SkImage> image(testCase.fImageFactory());
  955. if (!image) {
  956. ERRORF(reporter, "Failed to create image!");
  957. continue;
  958. }
  959. GrBackendTexture origBackend = image->getBackendTexture(true);
  960. if (testCase.fCanTakeDirectly) {
  961. SkASSERT(origBackend.isValid());
  962. }
  963. GrBackendTexture newBackend;
  964. SkImage::BackendTextureReleaseProc proc;
  965. bool result = SkImage::MakeBackendTextureFromSkImage(context, std::move(image),
  966. &newBackend, &proc);
  967. if (result != testCase.fExpectation) {
  968. static const char *const kFS[] = { "fail", "succeed" };
  969. ERRORF(reporter, "This image was expected to %s but did not.",
  970. kFS[testCase.fExpectation]);
  971. }
  972. if (result) {
  973. SkASSERT(newBackend.isValid());
  974. }
  975. bool tookDirectly = result && GrBackendTexture::TestingOnly_Equals(origBackend, newBackend);
  976. if (testCase.fCanTakeDirectly != tookDirectly) {
  977. static const char *const kExpectedState[] = { "not expected", "expected" };
  978. ERRORF(reporter, "This backend texture was %s to be taken directly.",
  979. kExpectedState[testCase.fCanTakeDirectly]);
  980. }
  981. context->flush();
  982. }
  983. }
  984. ///////////////////////////////////////////////////////////////////////////////////////////////////
  985. static sk_sp<SkImage> create_picture_image(sk_sp<SkColorSpace> space) {
  986. SkPictureRecorder recorder;
  987. SkCanvas* canvas = recorder.beginRecording(10, 10);
  988. canvas->clear(SK_ColorCYAN);
  989. return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(), SkISize::Make(10, 10),
  990. nullptr, nullptr, SkImage::BitDepth::kU8, std::move(space));
  991. };
  992. DEF_TEST(Image_ColorSpace, r) {
  993. sk_sp<SkColorSpace> srgb = SkColorSpace::MakeSRGB();
  994. sk_sp<SkImage> image = GetResourceAsImage("images/mandrill_512_q075.jpg");
  995. REPORTER_ASSERT(r, srgb.get() == image->colorSpace());
  996. image = GetResourceAsImage("images/webp-color-profile-lossy.webp");
  997. skcms_TransferFunction fn;
  998. bool success = image->colorSpace()->isNumericalTransferFn(&fn);
  999. REPORTER_ASSERT(r, success);
  1000. REPORTER_ASSERT(r, color_space_almost_equal(1.8f, fn.g));
  1001. sk_sp<SkColorSpace> rec2020 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
  1002. SkNamedGamut::kRec2020);
  1003. image = create_picture_image(rec2020);
  1004. REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
  1005. SkBitmap bitmap;
  1006. SkImageInfo info = SkImageInfo::MakeN32(10, 10, kPremul_SkAlphaType, rec2020);
  1007. bitmap.allocPixels(info);
  1008. image = SkImage::MakeFromBitmap(bitmap);
  1009. REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
  1010. sk_sp<SkSurface> surface = SkSurface::MakeRaster(
  1011. SkImageInfo::MakeN32Premul(SkISize::Make(10, 10)));
  1012. image = surface->makeImageSnapshot();
  1013. REPORTER_ASSERT(r, nullptr == image->colorSpace());
  1014. surface = SkSurface::MakeRaster(info);
  1015. image = surface->makeImageSnapshot();
  1016. REPORTER_ASSERT(r, SkColorSpace::Equals(rec2020.get(), image->colorSpace()));
  1017. }
  1018. DEF_TEST(Image_makeColorSpace, r) {
  1019. sk_sp<SkColorSpace> p3 = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
  1020. skcms_TransferFunction fn;
  1021. fn.a = 1.f; fn.b = 0.f; fn.c = 0.f; fn.d = 0.f; fn.e = 0.f; fn.f = 0.f; fn.g = 1.8f;
  1022. sk_sp<SkColorSpace> adobeGamut = SkColorSpace::MakeRGB(fn, SkNamedGamut::kAdobeRGB);
  1023. SkBitmap srgbBitmap;
  1024. srgbBitmap.allocPixels(SkImageInfo::MakeS32(1, 1, kOpaque_SkAlphaType));
  1025. *srgbBitmap.getAddr32(0, 0) = SkSwizzle_RGBA_to_PMColor(0xFF604020);
  1026. srgbBitmap.setImmutable();
  1027. sk_sp<SkImage> srgbImage = SkImage::MakeFromBitmap(srgbBitmap);
  1028. sk_sp<SkImage> p3Image = srgbImage->makeColorSpace(p3);
  1029. SkBitmap p3Bitmap;
  1030. bool success = p3Image->asLegacyBitmap(&p3Bitmap);
  1031. auto almost_equal = [](int a, int b) { return SkTAbs(a - b) <= 2; };
  1032. REPORTER_ASSERT(r, success);
  1033. REPORTER_ASSERT(r, almost_equal(0x28, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
  1034. REPORTER_ASSERT(r, almost_equal(0x40, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
  1035. REPORTER_ASSERT(r, almost_equal(0x5E, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
  1036. sk_sp<SkImage> adobeImage = srgbImage->makeColorSpace(adobeGamut);
  1037. SkBitmap adobeBitmap;
  1038. success = adobeImage->asLegacyBitmap(&adobeBitmap);
  1039. REPORTER_ASSERT(r, success);
  1040. REPORTER_ASSERT(r, almost_equal(0x21, SkGetPackedR32(*adobeBitmap.getAddr32(0, 0))));
  1041. REPORTER_ASSERT(r, almost_equal(0x31, SkGetPackedG32(*adobeBitmap.getAddr32(0, 0))));
  1042. REPORTER_ASSERT(r, almost_equal(0x4C, SkGetPackedB32(*adobeBitmap.getAddr32(0, 0))));
  1043. srgbImage = GetResourceAsImage("images/1x1.png");
  1044. p3Image = srgbImage->makeColorSpace(p3);
  1045. success = p3Image->asLegacyBitmap(&p3Bitmap);
  1046. REPORTER_ASSERT(r, success);
  1047. REPORTER_ASSERT(r, almost_equal(0x8B, SkGetPackedR32(*p3Bitmap.getAddr32(0, 0))));
  1048. REPORTER_ASSERT(r, almost_equal(0x82, SkGetPackedG32(*p3Bitmap.getAddr32(0, 0))));
  1049. REPORTER_ASSERT(r, almost_equal(0x77, SkGetPackedB32(*p3Bitmap.getAddr32(0, 0))));
  1050. }
  1051. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1052. static void make_all_premul(SkBitmap* bm) {
  1053. bm->allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
  1054. for (int a = 0; a < 256; ++a) {
  1055. for (int r = 0; r < 256; ++r) {
  1056. // make all valid premul combinations
  1057. int c = SkTMin(a, r);
  1058. *bm->getAddr32(a, r) = SkPackARGB32(a, c, c, c);
  1059. }
  1060. }
  1061. }
  1062. static bool equal(const SkBitmap& a, const SkBitmap& b) {
  1063. SkASSERT(a.width() == b.width());
  1064. SkASSERT(a.height() == b.height());
  1065. for (int y = 0; y < a.height(); ++y) {
  1066. for (int x = 0; x < a.width(); ++x) {
  1067. SkPMColor pa = *a.getAddr32(x, y);
  1068. SkPMColor pb = *b.getAddr32(x, y);
  1069. if (pa != pb) {
  1070. return false;
  1071. }
  1072. }
  1073. }
  1074. return true;
  1075. }
  1076. DEF_TEST(image_roundtrip_encode, reporter) {
  1077. SkBitmap bm0;
  1078. make_all_premul(&bm0);
  1079. auto img0 = SkImage::MakeFromBitmap(bm0);
  1080. sk_sp<SkData> data = img0->encodeToData(SkEncodedImageFormat::kPNG, 100);
  1081. auto img1 = SkImage::MakeFromEncoded(data);
  1082. SkBitmap bm1;
  1083. bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
  1084. img1->readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
  1085. REPORTER_ASSERT(reporter, equal(bm0, bm1));
  1086. }
  1087. DEF_TEST(image_roundtrip_premul, reporter) {
  1088. SkBitmap bm0;
  1089. make_all_premul(&bm0);
  1090. SkBitmap bm1;
  1091. bm1.allocPixels(SkImageInfo::MakeN32(256, 256, kUnpremul_SkAlphaType));
  1092. bm0.readPixels(bm1.info(), bm1.getPixels(), bm1.rowBytes(), 0, 0);
  1093. SkBitmap bm2;
  1094. bm2.allocPixels(SkImageInfo::MakeN32(256, 256, kPremul_SkAlphaType));
  1095. bm1.readPixels(bm2.info(), bm2.getPixels(), bm2.rowBytes(), 0, 0);
  1096. REPORTER_ASSERT(reporter, equal(bm0, bm2));
  1097. }
  1098. ///////////////////////////////////////////////////////////////////////////////////////////////////
  1099. static void check_scaled_pixels(skiatest::Reporter* reporter, SkPixmap* pmap, uint32_t expected) {
  1100. // Verify that all pixels contain the original test color
  1101. for (auto y = 0; y < pmap->height(); ++y) {
  1102. for (auto x = 0; x < pmap->width(); ++x) {
  1103. uint32_t pixel = *pmap->addr32(x, y);
  1104. if (pixel != expected) {
  1105. ERRORF(reporter, "Expected scaled pixels to be the same. At %d,%d 0x%08x != 0x%08x",
  1106. x, y, pixel, expected);
  1107. return;
  1108. }
  1109. }
  1110. }
  1111. }
  1112. static void test_scale_pixels(skiatest::Reporter* reporter, const SkImage* image,
  1113. uint32_t expected) {
  1114. SkImageInfo info = SkImageInfo::MakeN32Premul(image->width() * 2, image->height() * 2);
  1115. // Make sure to test kDisallow first, so we don't just get a cache hit in that case
  1116. for (auto chint : { SkImage::kDisallow_CachingHint, SkImage::kAllow_CachingHint }) {
  1117. SkAutoPixmapStorage scaled;
  1118. scaled.alloc(info);
  1119. if (!image->scalePixels(scaled, kLow_SkFilterQuality, chint)) {
  1120. ERRORF(reporter, "Failed to scale image");
  1121. continue;
  1122. }
  1123. check_scaled_pixels(reporter, &scaled, expected);
  1124. }
  1125. }
  1126. DEF_TEST(ImageScalePixels, reporter) {
  1127. const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
  1128. const SkColor red = SK_ColorRED;
  1129. // Test raster image
  1130. SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
  1131. sk_sp<SkSurface> surface = SkSurface::MakeRaster(info);
  1132. surface->getCanvas()->clear(red);
  1133. sk_sp<SkImage> rasterImage = surface->makeImageSnapshot();
  1134. test_scale_pixels(reporter, rasterImage.get(), pmRed);
  1135. // Test encoded image
  1136. sk_sp<SkData> data = rasterImage->encodeToData();
  1137. sk_sp<SkImage> codecImage = SkImage::MakeFromEncoded(data);
  1138. test_scale_pixels(reporter, codecImage.get(), pmRed);
  1139. }
  1140. DEF_GPUTEST_FOR_RENDERING_CONTEXTS(ImageScalePixels_Gpu, reporter, ctxInfo) {
  1141. const SkPMColor pmRed = SkPackARGB32(0xFF, 0xFF, 0, 0);
  1142. const SkColor red = SK_ColorRED;
  1143. SkImageInfo info = SkImageInfo::MakeN32Premul(16, 16);
  1144. sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kNo,
  1145. info);
  1146. surface->getCanvas()->clear(red);
  1147. sk_sp<SkImage> gpuImage = surface->makeImageSnapshot();
  1148. test_scale_pixels(reporter, gpuImage.get(), pmRed);
  1149. }
  1150. static sk_sp<SkImage> any_image_will_do() {
  1151. return GetResourceAsImage("images/mandrill_32.png");
  1152. }
  1153. DEF_TEST(Image_nonfinite_dst, reporter) {
  1154. auto surf = SkSurface::MakeRasterN32Premul(10, 10);
  1155. auto img = any_image_will_do();
  1156. SkPaint paint;
  1157. for (SkScalar bad : { SK_ScalarInfinity, SK_ScalarNaN}) {
  1158. for (int bits = 1; bits <= 15; ++bits) {
  1159. SkRect dst = { 0, 0, 10, 10 };
  1160. if (bits & 1) dst.fLeft = bad;
  1161. if (bits & 2) dst.fTop = bad;
  1162. if (bits & 4) dst.fRight = bad;
  1163. if (bits & 8) dst.fBottom = bad;
  1164. surf->getCanvas()->drawImageRect(img, dst, &paint);
  1165. // we should draw nothing
  1166. ToolUtils::PixelIter iter(surf.get());
  1167. while (void* addr = iter.next()) {
  1168. REPORTER_ASSERT(reporter, *(SkPMColor*)addr == 0);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. static sk_sp<SkImage> make_yuva_image(GrContext* c) {
  1174. SkAutoPixmapStorage pm;
  1175. pm.alloc(SkImageInfo::Make(1, 1, kAlpha_8_SkColorType, kPremul_SkAlphaType));
  1176. const SkPixmap pmaps[] = {pm, pm, pm, pm};
  1177. SkYUVAIndex indices[] = {{0, SkColorChannel::kA},
  1178. {1, SkColorChannel::kA},
  1179. {2, SkColorChannel::kA},
  1180. {3, SkColorChannel::kA}};
  1181. return SkImage::MakeFromYUVAPixmaps(c, kJPEG_SkYUVColorSpace, pmaps, indices,
  1182. SkISize::Make(1, 1), kTopLeft_GrSurfaceOrigin, false);
  1183. }
  1184. DEF_GPUTEST_FOR_ALL_CONTEXTS(ImageFlush, reporter, ctxInfo) {
  1185. auto c = ctxInfo.grContext();
  1186. auto ii = SkImageInfo::Make(10, 10, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
  1187. auto s = SkSurface::MakeRenderTarget(ctxInfo.grContext(), SkBudgeted::kYes, ii, 1, nullptr);
  1188. s->getCanvas()->clear(SK_ColorRED);
  1189. auto i0 = s->makeImageSnapshot();
  1190. s->getCanvas()->clear(SK_ColorBLUE);
  1191. auto i1 = s->makeImageSnapshot();
  1192. s->getCanvas()->clear(SK_ColorGREEN);
  1193. // Make a YUVA image.
  1194. auto i2 = make_yuva_image(c);
  1195. // Flush all the setup work we did above and then make little lambda that reports the flush
  1196. // count delta since the last time it was called.
  1197. c->flush();
  1198. auto numFlushes = [c, flushCnt = c->priv().getGpu()->stats()->numFinishFlushes()]() mutable {
  1199. int curr = c->priv().getGpu()->stats()->numFinishFlushes();
  1200. int n = curr - flushCnt;
  1201. flushCnt = curr;
  1202. return n;
  1203. };
  1204. // Images aren't used therefore flush is ignored.
  1205. i0->flush(c);
  1206. i1->flush(c);
  1207. i2->flush(c);
  1208. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1209. // Syncing forces the flush to happen even if the images aren't used.
  1210. GrFlushInfo syncInfo;
  1211. syncInfo.fFlags = kSyncCpu_GrFlushFlag;
  1212. i0->flush(c, syncInfo);
  1213. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1214. i1->flush(c, syncInfo);
  1215. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1216. i2->flush(c, syncInfo);
  1217. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1218. // Use image 1
  1219. s->getCanvas()->drawImage(i1, 0, 0);
  1220. // Flushing image 0 should do nothing.
  1221. i0->flush(c);
  1222. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1223. // Flushing image 1 should flush.
  1224. i1->flush(c);
  1225. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1226. // Flushing image 2 should do nothing.
  1227. i2->flush(c);
  1228. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1229. // Use image 2
  1230. s->getCanvas()->drawImage(i2, 0, 0);
  1231. // Flushing image 0 should do nothing.
  1232. i0->flush(c);
  1233. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1234. // Flushing image 1 do nothing.
  1235. i1->flush(c);
  1236. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1237. // Flushing image 2 should flush.
  1238. i2->flush(c);
  1239. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1240. // Since we just did a simple image draw it should not have been flattened.
  1241. REPORTER_ASSERT(reporter,
  1242. !static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
  1243. REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
  1244. // Flatten it and repeat.
  1245. as_IB(i2.get())->asTextureProxyRef(c);
  1246. REPORTER_ASSERT(reporter,
  1247. static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
  1248. REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
  1249. s->getCanvas()->drawImage(i2, 0, 0);
  1250. // Flushing image 0 should do nothing.
  1251. i0->flush(c);
  1252. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1253. // Flushing image 1 do nothing.
  1254. i1->flush(c);
  1255. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1256. // Flushing image 2 should flush.
  1257. i2->flush(c);
  1258. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1259. // Test case where flatten happens before the first flush.
  1260. i2 = make_yuva_image(c);
  1261. // On some systems where preferVRAMUseOverFlushes is false (ANGLE on Windows) the above may
  1262. // actually flush in order to make textures for the YUV planes. TODO: Remove this when we
  1263. // make the YUVA planes from backend textures rather than pixmaps that GrContext must upload.
  1264. // Calling numFlushes rebases the flush count from here.
  1265. numFlushes();
  1266. as_IB(i2.get())->asTextureProxyRef(c);
  1267. REPORTER_ASSERT(reporter,
  1268. static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->testingOnly_IsFlattened());
  1269. REPORTER_ASSERT(reporter, static_cast<SkImage_GpuYUVA*>(as_IB(i2.get()))->isTextureBacked());
  1270. s->getCanvas()->drawImage(i2, 0, 0);
  1271. // Flushing image 0 should do nothing.
  1272. i0->flush(c);
  1273. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1274. // Flushing image 1 do nothing.
  1275. i1->flush(c);
  1276. REPORTER_ASSERT(reporter, numFlushes() == 0);
  1277. // Flushing image 2 should flush.
  1278. i2->flush(c);
  1279. REPORTER_ASSERT(reporter, numFlushes() == 1);
  1280. }