blob: 70d6fd4b38c215dc41298cbf217a9aeac2b001bf [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
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
Robert Phillips37430132016-11-09 06:50:43 -050020#include "GrSurfaceProxy.h"
Mike Reed84dd8572017-03-08 22:21:00 -050021#include "GrTextureProxy.h"
Brian Osman3b655982017-03-07 16:58:08 -050022#include "SkGr.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080023#endif
24
robertphillipsb6c65e92016-02-04 10:52:42 -080025
26// This test creates backing resources exactly sized to [kFullSize x kFullSize].
27// It then wraps them in an SkSpecialImage with only the center (red) region being active.
28// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
29// of the inactive (green) region leaked out.
30
31static const int kSmallerSize = 10;
32static const int kPad = 3;
33static const int kFullSize = kSmallerSize + 2 * kPad;
34
35// Create a bitmap with red in the center and green around it
36static SkBitmap create_bm() {
37 SkBitmap bm;
38 bm.allocN32Pixels(kFullSize, kFullSize, true);
39
40 SkCanvas temp(bm);
41
42 temp.clear(SK_ColorGREEN);
43 SkPaint p;
44 p.setColor(SK_ColorRED);
45 p.setAntiAlias(false);
46
47 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
halcanary9d524f22016-03-29 09:03:52 -070048 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
robertphillipsb6c65e92016-02-04 10:52:42 -080049 p);
50
51 return bm;
52}
53
54// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
robertphillips37bd7c32016-03-17 14:31:39 -070055static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
Robert Phillips6de99042017-01-31 11:31:39 -050056 GrContext* context, bool isGPUBacked,
robertphillipsc5035e72016-03-17 06:58:39 -070057 int offset, int size) {
robertphillips3e302272016-04-20 11:48:36 -070058 const SkIRect subset = img->subset();
robertphillipsc5035e72016-03-17 06:58:39 -070059 REPORTER_ASSERT(reporter, offset == subset.left());
60 REPORTER_ASSERT(reporter, offset == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080061 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
62 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
63
64 //--------------
Robert Phillips2c6d2bf2017-02-21 10:19:29 -050065 // Test that isTextureBacked reports the correct backing type
Robert Phillips6de99042017-01-31 11:31:39 -050066 REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
robertphillips64612512016-04-08 12:10:42 -070067
68#if SK_SUPPORT_GPU
69 //--------------
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 }
75#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080076
77 //--------------
robertphillips64612512016-04-08 12:10:42 -070078 // Test getROPixels - this should always succeed regardless of backing store
79 SkBitmap bitmap;
80 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
robertphillipsed086ca2016-04-26 15:02:25 -070081 if (context) {
82 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
83 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
84 } else {
85 REPORTER_ASSERT(reporter, size == bitmap.width());
86 REPORTER_ASSERT(reporter, size == bitmap.height());
87 }
robertphillipsb6c65e92016-02-04 10:52:42 -080088
89 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070090 // Test that draw restricts itself to the subset
brianosmaneed6b0e2016-09-23 13:04:05 -070091 SkImageFilter::OutputProperties outProps(img->getColorSpace());
92 sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
Matt Sarettcb6266b2017-01-17 10:48:53 -050093 kPremul_SkAlphaType));
robertphillipsb6c65e92016-02-04 10:52:42 -080094
95 SkCanvas* canvas = surf->getCanvas();
96
97 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080098 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080099
100 SkBitmap bm;
Matt Sarettcb6266b2017-01-17 10:48:53 -0500101 bm.allocN32Pixels(kFullSize, kFullSize, false);
robertphillipsb6c65e92016-02-04 10:52:42 -0800102
103 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
104 SkASSERT_RELEASE(result);
105
106 // Only the center (red) portion should've been drawn into the canvas
107 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
108 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
109 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
110 kSmallerSize+kPad-1));
111 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
112 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700113
114 //--------------
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500115 // Test that asImage & makeTightSurface return appropriately sized objects
robertphillipsb4bd11e2016-03-21 13:44:18 -0700116 // of the correct backing type
117 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
118 {
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500119 sk_sp<SkImage> tightImg(img->asImage(&newSubset));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700120
121 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
122 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400123 REPORTER_ASSERT(reporter, isGPUBacked == tightImg->isTextureBacked());
robertphillipsb4bd11e2016-03-21 13:44:18 -0700124 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500125 REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700126 }
127 {
brianosmaneed6b0e2016-09-23 13:04:05 -0700128 SkImageFilter::OutputProperties outProps(img->getColorSpace());
129 sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700130
131 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
132 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
Robert Phillips6de99042017-01-31 11:31:39 -0500133 REPORTER_ASSERT(reporter, isGPUBacked ==
robertphillipsb4bd11e2016-03-21 13:44:18 -0700134 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
135 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 Osman61624f02016-12-09 14:51:59 -0500160static void test_specialimage_image(skiatest::Reporter* reporter, SkColorSpace* dstColorSpace) {
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 Osman61624f02016-12-09 14:51:59 -0500167 fullImage, dstColorSpace));
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 Osman61624f02016-12-09 14:51:59 -0500172 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage,
173 dstColorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700174 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700175 }
176
177 {
robertphillips37bd7c32016-03-17 14:31:39 -0700178 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700179 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700180 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800181}
182
Brian Osman7992da32016-11-18 11:28:24 -0500183DEF_TEST(SpecialImage_Image_Legacy, reporter) {
Brian Osman61624f02016-12-09 14:51:59 -0500184 SkColorSpace* legacyColorSpace = nullptr;
185 test_specialimage_image(reporter, legacyColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -0500186}
187
188DEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500189 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman61624f02016-12-09 14:51:59 -0500190 test_specialimage_image(reporter, srgbColorSpace.get());
Brian Osman7992da32016-11-18 11:28:24 -0500191}
192
robertphillipsb6c65e92016-02-04 10:52:42 -0800193#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700194
195static void test_texture_backed(skiatest::Reporter* reporter,
196 const sk_sp<SkSpecialImage>& orig,
197 const sk_sp<SkSpecialImage>& gpuBacked) {
198 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700199 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700200 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
201 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
202 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700203 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700204}
205
206// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700208 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700209 SkBitmap bm = create_bm();
210
211 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
212
213 {
214 // raster
215 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700216 SkIRect::MakeWH(kFullSize,
217 kFullSize),
218 bm));
219
220 {
robertphillips3e302272016-04-20 11:48:36 -0700221 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700222 test_texture_backed(reporter, rasterImage, fromRaster);
223 }
224
225 {
226 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
227
robertphillips3e302272016-04-20 11:48:36 -0700228 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700229 test_texture_backed(reporter, subRasterImage, fromSubRaster);
230 }
231 }
232
233 {
234 // gpu
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500235 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info(), *context->caps());
robertphillips83c17fa2016-03-18 08:14:27 -0700236
Robert Phillips26c90e02017-03-14 14:39:29 -0400237 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500238 desc, SkBudgeted::kNo,
239 bm.getPixels(), bm.rowBytes()));
Robert Phillips2f493142017-03-02 18:18:38 -0500240 if (!proxy) {
robertphillips83c17fa2016-03-18 08:14:27 -0700241 return;
242 }
243
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500244 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500245 context,
246 SkIRect::MakeWH(kFullSize, kFullSize),
247 kNeedNewImageUniqueID_SpecialImage,
248 std::move(proxy), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700249
250 {
robertphillips3e302272016-04-20 11:48:36 -0700251 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700252 test_texture_backed(reporter, gpuImage, fromGPU);
253 }
254
255 {
256 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
257
robertphillips3e302272016-04-20 11:48:36 -0700258 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700259 test_texture_backed(reporter, subGPUImage, fromSubGPU);
260 }
261 }
262}
263
bsalomon68d91342016-04-12 09:59:58 -0700264DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700265 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800266 SkBitmap bm = create_bm();
267
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500268 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info(), *context->caps());
robertphillipsb6c65e92016-02-04 10:52:42 -0800269
Robert Phillips26c90e02017-03-14 14:39:29 -0400270 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500271 desc, SkBudgeted::kNo,
272 bm.getPixels(), bm.rowBytes()));
Robert Phillips2f493142017-03-02 18:18:38 -0500273 if (!proxy) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800274 return;
275 }
276
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500277 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
278 context,
robertphillipsc5035e72016-03-17 06:58:39 -0700279 SkIRect::MakeWH(kFullSize, kFullSize),
280 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500281 proxy, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700282
robertphillipsb6c65e92016-02-04 10:52:42 -0800283 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
284
robertphillipsc5035e72016-03-17 06:58:39 -0700285 {
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500286 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500287 context, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700288 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500289 std::move(proxy), nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700290 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700291 }
292
293 {
robertphillips37bd7c32016-03-17 14:31:39 -0700294 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700295 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700296 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800297}
298
Robert Phillips8bc06d02016-11-01 17:28:40 -0400299DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
300 GrContext* context = ctxInfo.grContext();
301 SkBitmap bm = create_bm();
302
303 GrSurfaceDesc desc;
304 desc.fConfig = kSkia8888_GrPixelConfig;
305 desc.fFlags = kNone_GrSurfaceFlags;
306 desc.fWidth = kFullSize;
307 desc.fHeight = kFullSize;
308
Robert Phillips26c90e02017-03-14 14:39:29 -0400309 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips37430132016-11-09 06:50:43 -0500310 desc, SkBudgeted::kNo,
311 bm.getPixels(), 0));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400312 if (!proxy) {
313 return;
314 }
315
316 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
317 context,
318 SkIRect::MakeWH(kFullSize, kFullSize),
319 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500320 proxy, nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400321
322 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
323
324 {
325 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500326 context, subset,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400327 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500328 std::move(proxy), nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400329 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
330 }
331
332 {
333 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
334 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
335 }
336}
337
robertphillipsb6c65e92016-02-04 10:52:42 -0800338#endif