blob: f9a0147bd7cf61de8a394616dac976aad447b161 [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"
Brian Osman3b655982017-03-07 16:58:08 -050021#include "SkGr.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080022#endif
23
robertphillipsb6c65e92016-02-04 10:52:42 -080024
25// This test creates backing resources exactly sized to [kFullSize x kFullSize].
26// It then wraps them in an SkSpecialImage with only the center (red) region being active.
27// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
28// of the inactive (green) region leaked out.
29
30static const int kSmallerSize = 10;
31static const int kPad = 3;
32static const int kFullSize = kSmallerSize + 2 * kPad;
33
34// Create a bitmap with red in the center and green around it
35static SkBitmap create_bm() {
36 SkBitmap bm;
37 bm.allocN32Pixels(kFullSize, kFullSize, true);
38
39 SkCanvas temp(bm);
40
41 temp.clear(SK_ColorGREEN);
42 SkPaint p;
43 p.setColor(SK_ColorRED);
44 p.setAntiAlias(false);
45
46 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
halcanary9d524f22016-03-29 09:03:52 -070047 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
robertphillipsb6c65e92016-02-04 10:52:42 -080048 p);
49
50 return bm;
51}
52
53// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
robertphillips37bd7c32016-03-17 14:31:39 -070054static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
Robert Phillips6de99042017-01-31 11:31:39 -050055 GrContext* context, bool isGPUBacked,
robertphillipsc5035e72016-03-17 06:58:39 -070056 int offset, int size) {
robertphillips3e302272016-04-20 11:48:36 -070057 const SkIRect subset = img->subset();
robertphillipsc5035e72016-03-17 06:58:39 -070058 REPORTER_ASSERT(reporter, offset == subset.left());
59 REPORTER_ASSERT(reporter, offset == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080060 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
61 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
62
63 //--------------
Robert Phillips2c6d2bf2017-02-21 10:19:29 -050064 // Test that isTextureBacked reports the correct backing type
Robert Phillips6de99042017-01-31 11:31:39 -050065 REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
robertphillips64612512016-04-08 12:10:42 -070066
67#if SK_SUPPORT_GPU
68 //--------------
Robert Phillips8e1c4e62017-02-19 12:27:01 -050069 // Test asTextureProxyRef - as long as there is a context this should succeed
robertphillips64612512016-04-08 12:10:42 -070070 if (context) {
Robert Phillips8e1c4e62017-02-19 12:27:01 -050071 sk_sp<GrTextureProxy> proxy(img->asTextureProxyRef(context));
72 REPORTER_ASSERT(reporter, proxy);
robertphillips64612512016-04-08 12:10:42 -070073 }
74#endif
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
brianosmaneed6b0e2016-09-23 13:04:05 -070090 SkImageFilter::OutputProperties outProps(img->getColorSpace());
91 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 Phillips6de99042017-01-31 11:31:39 -0500122 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 {
brianosmaneed6b0e2016-09-23 13:04:05 -0700127 SkImageFilter::OutputProperties outProps(img->getColorSpace());
128 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 Phillips6de99042017-01-31 11:31:39 -0500132 REPORTER_ASSERT(reporter, isGPUBacked ==
robertphillipsb4bd11e2016-03-21 13:44:18 -0700133 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
134 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500135 REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700136 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800137}
138
139DEF_TEST(SpecialImage_Raster, reporter) {
140 SkBitmap bm = create_bm();
141
robertphillips37bd7c32016-03-17 14:31:39 -0700142 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700143 SkIRect::MakeWH(kFullSize, kFullSize),
144 bm));
145
robertphillipsb6c65e92016-02-04 10:52:42 -0800146 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
147
robertphillipsc5035e72016-03-17 06:58:39 -0700148 {
robertphillips3e302272016-04-20 11:48:36 -0700149 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700150 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700151 }
152
153 {
robertphillips37bd7c32016-03-17 14:31:39 -0700154 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700155 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700156 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800157}
158
Brian Osman61624f02016-12-09 14:51:59 -0500159static void test_specialimage_image(skiatest::Reporter* reporter, SkColorSpace* dstColorSpace) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800160 SkBitmap bm = create_bm();
161
reed9ce9d672016-03-17 10:51:11 -0700162 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800163
robertphillips37bd7c32016-03-17 14:31:39 -0700164 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700165 SkIRect::MakeWH(kFullSize, kFullSize),
Brian Osman61624f02016-12-09 14:51:59 -0500166 fullImage, dstColorSpace));
robertphillipsc5035e72016-03-17 06:58:39 -0700167
robertphillipsb6c65e92016-02-04 10:52:42 -0800168 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
169
robertphillipsc5035e72016-03-17 06:58:39 -0700170 {
Brian Osman61624f02016-12-09 14:51:59 -0500171 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage,
172 dstColorSpace));
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 Osman61624f02016-12-09 14:51:59 -0500183 SkColorSpace* legacyColorSpace = nullptr;
184 test_specialimage_image(reporter, legacyColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -0500185}
186
187DEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500188 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman61624f02016-12-09 14:51:59 -0500189 test_specialimage_image(reporter, srgbColorSpace.get());
Brian Osman7992da32016-11-18 11:28:24 -0500190}
191
robertphillipsb6c65e92016-02-04 10:52:42 -0800192#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700193
194static void test_texture_backed(skiatest::Reporter* reporter,
195 const sk_sp<SkSpecialImage>& orig,
196 const sk_sp<SkSpecialImage>& gpuBacked) {
197 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700198 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700199 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
200 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
201 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700202 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700203}
204
205// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700206DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700207 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700208 SkBitmap bm = create_bm();
209
210 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
211
212 {
213 // raster
214 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700215 SkIRect::MakeWH(kFullSize,
216 kFullSize),
217 bm));
218
219 {
robertphillips3e302272016-04-20 11:48:36 -0700220 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700221 test_texture_backed(reporter, rasterImage, fromRaster);
222 }
223
224 {
225 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
226
robertphillips3e302272016-04-20 11:48:36 -0700227 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700228 test_texture_backed(reporter, subRasterImage, fromSubRaster);
229 }
230 }
231
232 {
233 // gpu
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500234 const GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info(), *context->caps());
robertphillips83c17fa2016-03-18 08:14:27 -0700235
Robert Phillips2f493142017-03-02 18:18:38 -0500236 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -0500237 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 Phillips2f493142017-03-02 18:18:38 -0500270 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -0500271 context->resourceProvider(),
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500272 desc, SkBudgeted::kNo,
273 bm.getPixels(), bm.rowBytes()));
Robert Phillips2f493142017-03-02 18:18:38 -0500274 if (!proxy) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800275 return;
276 }
277
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500278 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
279 context,
robertphillipsc5035e72016-03-17 06:58:39 -0700280 SkIRect::MakeWH(kFullSize, kFullSize),
281 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500282 proxy, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700283
robertphillipsb6c65e92016-02-04 10:52:42 -0800284 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
285
robertphillipsc5035e72016-03-17 06:58:39 -0700286 {
Robert Phillips2c6d2bf2017-02-21 10:19:29 -0500287 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500288 context, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700289 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500290 std::move(proxy), nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700291 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700292 }
293
294 {
robertphillips37bd7c32016-03-17 14:31:39 -0700295 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700296 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700297 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800298}
299
Robert Phillips8bc06d02016-11-01 17:28:40 -0400300DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
301 GrContext* context = ctxInfo.grContext();
302 SkBitmap bm = create_bm();
303
304 GrSurfaceDesc desc;
305 desc.fConfig = kSkia8888_GrPixelConfig;
306 desc.fFlags = kNone_GrSurfaceFlags;
307 desc.fWidth = kFullSize;
308 desc.fHeight = kFullSize;
309
Robert Phillips2f493142017-03-02 18:18:38 -0500310 sk_sp<GrTextureProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
Brian Osman32342f02017-03-04 08:12:46 -0500311 context->resourceProvider(),
Robert Phillips37430132016-11-09 06:50:43 -0500312 desc, SkBudgeted::kNo,
313 bm.getPixels(), 0));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400314 if (!proxy) {
315 return;
316 }
317
318 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
319 context,
320 SkIRect::MakeWH(kFullSize, kFullSize),
321 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500322 proxy, nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400323
324 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
325
326 {
327 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
Robert Phillips2f493142017-03-02 18:18:38 -0500328 context, subset,
Robert Phillips8bc06d02016-11-01 17:28:40 -0400329 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips2f493142017-03-02 18:18:38 -0500330 std::move(proxy), nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400331 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
332 }
333
334 {
335 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
336 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
337 }
338}
339
robertphillipsb6c65e92016-02-04 10:52:42 -0800340#endif