blob: ad8c50a4a885af7675c4d4f3a36187266c91ea51 [file] [log] [blame]
robertphillipsb6c65e92016-02-04 10:52:42 -08001/*
2 * Copyright 2016 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
robertphillipsc5035e72016-03-17 06:58:39 -07008#include "SkAutoPixmapStorage.h"
robertphillipsb6c65e92016-02-04 10:52:42 -08009#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkImage.h"
robertphillipsc5035e72016-03-17 06:58:39 -070012#include "SkPixmap.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080013#include "SkSpecialImage.h"
14#include "SkSpecialSurface.h"
robertphillipsb4bd11e2016-03-21 13:44:18 -070015#include "SkSurface.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080016#include "Test.h"
17
Robert Phillips8caf85f2018-04-05 09:30:38 -040018#include "GrBackendSurface.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080019#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050020#include "GrContextPriv.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050021#include "GrProxyProvider.h"
Robert Phillips37430132016-11-09 06:50:43 -050022#include "GrSurfaceProxy.h"
Mike Reed84dd8572017-03-08 22:21:00 -050023#include "GrTextureProxy.h"
Brian Osman3b655982017-03-07 16:58:08 -050024#include "SkGr.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080025
robertphillipsb6c65e92016-02-04 10:52:42 -080026
27// This test creates backing resources exactly sized to [kFullSize x kFullSize].
28// It then wraps them in an SkSpecialImage with only the center (red) region being active.
29// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
30// of the inactive (green) region leaked out.
31
32static const int kSmallerSize = 10;
33static const int kPad = 3;
34static const int kFullSize = kSmallerSize + 2 * kPad;
35
36// Create a bitmap with red in the center and green around it
37static SkBitmap create_bm() {
38 SkBitmap bm;
39 bm.allocN32Pixels(kFullSize, kFullSize, true);
40
41 SkCanvas temp(bm);
42
43 temp.clear(SK_ColorGREEN);
44 SkPaint p;
45 p.setColor(SK_ColorRED);
46 p.setAntiAlias(false);
47
48 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
halcanary9d524f22016-03-29 09:03:52 -070049 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
robertphillipsb6c65e92016-02-04 10:52:42 -080050 p);
51
52 return bm;
53}
54
55// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
robertphillips37bd7c32016-03-17 14:31:39 -070056static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
Robert Phillips6de99042017-01-31 11:31:39 -050057 GrContext* context, bool isGPUBacked,
robertphillipsc5035e72016-03-17 06:58:39 -070058 int offset, int size) {
robertphillips3e302272016-04-20 11:48:36 -070059 const SkIRect subset = img->subset();
robertphillipsc5035e72016-03-17 06:58:39 -070060 REPORTER_ASSERT(reporter, offset == subset.left());
61 REPORTER_ASSERT(reporter, offset == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080062 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
63 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
64
65 //--------------
Robert Phillips2c6d2bf2017-02-21 10:19:29 -050066 // Test that isTextureBacked reports the correct backing type
Robert Phillips6de99042017-01-31 11:31:39 -050067 REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
robertphillips64612512016-04-08 12:10:42 -070068
robertphillips64612512016-04-08 12:10:42 -070069 //--------------
Robert Phillips8e1c4e62017-02-19 12:27:01 -050070 // Test asTextureProxyRef - as long as there is a context this should succeed
robertphillips64612512016-04-08 12:10:42 -070071 if (context) {
Robert Phillips8e1c4e62017-02-19 12:27:01 -050072 sk_sp<GrTextureProxy> proxy(img->asTextureProxyRef(context));
73 REPORTER_ASSERT(reporter, proxy);
robertphillips64612512016-04-08 12:10:42 -070074 }
robertphillipsb6c65e92016-02-04 10:52:42 -080075
76 //--------------
robertphillips64612512016-04-08 12:10:42 -070077 // Test getROPixels - this should always succeed regardless of backing store
78 SkBitmap bitmap;
79 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
robertphillipsed086ca2016-04-26 15:02:25 -070080 if (context) {
81 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
82 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
83 } else {
84 REPORTER_ASSERT(reporter, size == bitmap.width());
85 REPORTER_ASSERT(reporter, size == bitmap.height());
86 }
robertphillipsb6c65e92016-02-04 10:52:42 -080087
88 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070089 // Test that draw restricts itself to the subset
Brian Osmana50205f2018-07-06 13:57:01 -040090 SkImageFilter::OutputProperties outProps(kN32_SkColorType, img->getColorSpace());
brianosmaneed6b0e2016-09-23 13:04:05 -070091 sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
Matt Sarettcb6266b2017-01-17 10:48:53 -050092 kPremul_SkAlphaType));
robertphillipsb6c65e92016-02-04 10:52:42 -080093
94 SkCanvas* canvas = surf->getCanvas();
95
96 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080097 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080098
99 SkBitmap bm;
Matt Sarettcb6266b2017-01-17 10:48:53 -0500100 bm.allocN32Pixels(kFullSize, kFullSize, false);
robertphillipsb6c65e92016-02-04 10:52:42 -0800101
102 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
103 SkASSERT_RELEASE(result);
104
105 // Only the center (red) portion should've been drawn into the canvas
106 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
107 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
108 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
109 kSmallerSize+kPad-1));
110 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
111 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700112
113 //--------------
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500114 // Test that asImage & makeTightSurface return appropriately sized objects
robertphillipsb4bd11e2016-03-21 13:44:18 -0700115 // of the correct backing type
116 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
117 {
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500118 sk_sp<SkImage> tightImg(img->asImage(&newSubset));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700119
120 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
121 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400122 REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked());
robertphillipsb4bd11e2016-03-21 13:44:18 -0700123 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500124 REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700125 }
126 {
Brian Osmana50205f2018-07-06 13:57:01 -0400127 SkImageFilter::OutputProperties outProps(kN32_SkColorType, img->getColorSpace());
brianosmaneed6b0e2016-09-23 13:04:05 -0700128 sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700129
130 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
131 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
Robert Phillips8caf85f2018-04-05 09:30:38 -0400132 GrBackendTexture backendTex = tightSurf->getBackendTexture(
133 SkSurface::kDiscardWrite_BackendHandleAccess);
134 REPORTER_ASSERT(reporter, isGPUBacked == backendTex.isValid());
robertphillipsb4bd11e2016-03-21 13:44:18 -0700135 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500136 REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700137 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800138}
139
140DEF_TEST(SpecialImage_Raster, reporter) {
141 SkBitmap bm = create_bm();
142
robertphillips37bd7c32016-03-17 14:31:39 -0700143 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700144 SkIRect::MakeWH(kFullSize, kFullSize),
145 bm));
146
robertphillipsb6c65e92016-02-04 10:52:42 -0800147 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
148
robertphillipsc5035e72016-03-17 06:58:39 -0700149 {
robertphillips3e302272016-04-20 11:48:36 -0700150 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700151 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700152 }
153
154 {
robertphillips37bd7c32016-03-17 14:31:39 -0700155 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700156 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700157 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800158}
159
Brian Osmanb9c49782018-10-12 12:01:22 -0400160static void test_specialimage_image(skiatest::Reporter* reporter) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800161 SkBitmap bm = create_bm();
162
reed9ce9d672016-03-17 10:51:11 -0700163 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800164
robertphillips37bd7c32016-03-17 14:31:39 -0700165 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700166 SkIRect::MakeWH(kFullSize, kFullSize),
Brian Osmanb9c49782018-10-12 12:01:22 -0400167 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700168
robertphillipsb6c65e92016-02-04 10:52:42 -0800169 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
170
robertphillipsc5035e72016-03-17 06:58:39 -0700171 {
Brian Osmanb9c49782018-10-12 12:01:22 -0400172 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage));
robertphillipsed086ca2016-04-26 15:02:25 -0700173 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700174 }
175
176 {
robertphillips37bd7c32016-03-17 14:31:39 -0700177 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700178 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700179 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800180}
181
Brian Osman7992da32016-11-18 11:28:24 -0500182DEF_TEST(SpecialImage_Image_Legacy, reporter) {
Brian Osmanb9c49782018-10-12 12:01:22 -0400183 test_specialimage_image(reporter);
Brian Osman7992da32016-11-18 11:28:24 -0500184}
185
robertphillips83c17fa2016-03-18 08:14:27 -0700186static void test_texture_backed(skiatest::Reporter* reporter,
187 const sk_sp<SkSpecialImage>& orig,
188 const sk_sp<SkSpecialImage>& gpuBacked) {
189 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700190 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700191 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
192 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
193 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700194 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700195}
196
197// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700198DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700199 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500200 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
robertphillips83c17fa2016-03-18 08:14:27 -0700201 SkBitmap bm = create_bm();
202
203 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
204
205 {
206 // raster
207 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700208 SkIRect::MakeWH(kFullSize,
209 kFullSize),
210 bm));
211
212 {
robertphillips3e302272016-04-20 11:48:36 -0700213 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700214 test_texture_backed(reporter, rasterImage, fromRaster);
215 }
216
217 {
218 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
219
robertphillips3e302272016-04-20 11:48:36 -0700220 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700221 test_texture_backed(reporter, subRasterImage, fromSubRaster);
222 }
223 }
224
225 {
226 // gpu
Brian Osman2700abc2018-09-12 10:19:41 -0400227 sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm);
228 sk_sp<GrTextureProxy> proxy =
229 proxyProvider->createTextureProxy(rasterImage, kNone_GrSurfaceFlags, 1,
230 SkBudgeted::kNo, SkBackingFit::kExact);
Robert Phillips2f493142017-03-02 18:18:38 -0500231 if (!proxy) {
robertphillips83c17fa2016-03-18 08:14:27 -0700232 return;
233 }
234
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500235 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500236 context,
237 SkIRect::MakeWH(kFullSize, kFullSize),
238 kNeedNewImageUniqueID_SpecialImage,
239 std::move(proxy), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700240
241 {
robertphillips3e302272016-04-20 11:48:36 -0700242 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700243 test_texture_backed(reporter, gpuImage, fromGPU);
244 }
245
246 {
247 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
248
robertphillips3e302272016-04-20 11:48:36 -0700249 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700250 test_texture_backed(reporter, subGPUImage, fromSubGPU);
251 }
252 }
253}
254
bsalomon68d91342016-04-12 09:59:58 -0700255DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700256 GrContext* context = ctxInfo.grContext();
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500257 GrProxyProvider* proxyProvider = context->contextPriv().proxyProvider();
robertphillipsb6c65e92016-02-04 10:52:42 -0800258 SkBitmap bm = create_bm();
Brian Osman2700abc2018-09-12 10:19:41 -0400259 sk_sp<SkImage> rasterImage = SkImage::MakeFromBitmap(bm);
robertphillipsb6c65e92016-02-04 10:52:42 -0800260
Brian Salomon58389b92018-03-07 13:01:25 -0500261 sk_sp<GrTextureProxy> proxy =
Brian Osman2700abc2018-09-12 10:19:41 -0400262 proxyProvider->createTextureProxy(rasterImage, kNone_GrSurfaceFlags, 1,
263 SkBudgeted::kNo, SkBackingFit::kExact);
Robert Phillips2f493142017-03-02 18:18:38 -0500264 if (!proxy) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800265 return;
266 }
267
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500268 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
269 context,
robertphillipsc5035e72016-03-17 06:58:39 -0700270 SkIRect::MakeWH(kFullSize, kFullSize),
271 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500272 proxy, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700273
robertphillipsb6c65e92016-02-04 10:52:42 -0800274 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
275
robertphillipsc5035e72016-03-17 06:58:39 -0700276 {
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500277 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500278 context, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700279 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500280 std::move(proxy), nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700281 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700282 }
283
284 {
robertphillips37bd7c32016-03-17 14:31:39 -0700285 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700286 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700287 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800288}
Brian Osmanba651682018-10-05 11:13:04 -0400289
290DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_ReadbackAndCachingSubsets_Gpu, reporter, ctxInfo) {
291 GrContext* context = ctxInfo.grContext();
292 SkImageInfo ii = SkImageInfo::Make(50, 50, kN32_SkColorType, kPremul_SkAlphaType);
293 auto surface = SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii);
294
295 // Fill out our surface:
296 // Green | Blue
297 // Red | Green
298 {
299 surface->getCanvas()->clear(SK_ColorGREEN);
300 SkPaint p;
301 p.setColor(SK_ColorRED);
302 surface->getCanvas()->drawRect(SkRect::MakeXYWH(0, 25, 25, 25), p);
303 p.setColor(SK_ColorBLUE);
304 surface->getCanvas()->drawRect(SkRect::MakeXYWH(25, 0, 25, 25), p);
305 }
306
307 auto image = surface->makeImageSnapshot();
Brian Osmanb9c49782018-10-12 12:01:22 -0400308 auto redImg = SkSpecialImage::MakeFromImage(SkIRect::MakeXYWH(10, 30, 10, 10), image);
309 auto blueImg = SkSpecialImage::MakeFromImage(SkIRect::MakeXYWH(30, 10, 10, 10), image);
Brian Osmanba651682018-10-05 11:13:04 -0400310
311 // This isn't necessary, but if it ever becomes false, then the cache collision bug that we're
312 // checking below is irrelevant.
313 REPORTER_ASSERT(reporter, redImg->uniqueID() == blueImg->uniqueID());
314
315 SkBitmap redBM, blueBM;
316 SkAssertResult(redImg->getROPixels(&redBM));
317 SkAssertResult(blueImg->getROPixels(&blueBM));
318
319 // Each image should read from the correct sub-rect. Past bugs (skbug.com/8448) have included:
320 // - Always reading back from (0, 0), producing green
321 // - Incorrectly hitting the cache on the 2nd read-back, causing blueBM to be red
322 REPORTER_ASSERT(reporter, redBM.getColor(0, 0) == SK_ColorRED,
323 "0x%08x != 0x%08x", redBM.getColor(0, 0), SK_ColorRED);
324 REPORTER_ASSERT(reporter, blueBM.getColor(0, 0) == SK_ColorBLUE,
325 "0x%08x != 0x%08x", blueBM.getColor(0, 0), SK_ColorBLUE);
326}