blob: 80cacde960581dd8b915b6836fb8802bffac0caf [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"
robertphillipsb6c65e92016-02-04 10:52:42 -080021#endif
22
robertphillipsb6c65e92016-02-04 10:52:42 -080023
24// This test creates backing resources exactly sized to [kFullSize x kFullSize].
25// It then wraps them in an SkSpecialImage with only the center (red) region being active.
26// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
27// of the inactive (green) region leaked out.
28
29static const int kSmallerSize = 10;
30static const int kPad = 3;
31static const int kFullSize = kSmallerSize + 2 * kPad;
32
33// Create a bitmap with red in the center and green around it
34static SkBitmap create_bm() {
35 SkBitmap bm;
36 bm.allocN32Pixels(kFullSize, kFullSize, true);
37
38 SkCanvas temp(bm);
39
40 temp.clear(SK_ColorGREEN);
41 SkPaint p;
42 p.setColor(SK_ColorRED);
43 p.setAntiAlias(false);
44
45 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
halcanary9d524f22016-03-29 09:03:52 -070046 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
robertphillipsb6c65e92016-02-04 10:52:42 -080047 p);
48
49 return bm;
50}
51
52// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
robertphillips37bd7c32016-03-17 14:31:39 -070053static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
Robert Phillips6de99042017-01-31 11:31:39 -050054 GrContext* context, bool isGPUBacked,
robertphillipsc5035e72016-03-17 06:58:39 -070055 int offset, int size) {
robertphillips3e302272016-04-20 11:48:36 -070056 const SkIRect subset = img->subset();
robertphillipsc5035e72016-03-17 06:58:39 -070057 REPORTER_ASSERT(reporter, offset == subset.left());
58 REPORTER_ASSERT(reporter, offset == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080059 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
60 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
61
62 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070063 // Test that peekTexture reports the correct backing type
Robert Phillips6de99042017-01-31 11:31:39 -050064 REPORTER_ASSERT(reporter, isGPUBacked == img->isTextureBacked());
robertphillips64612512016-04-08 12:10:42 -070065
66#if SK_SUPPORT_GPU
67 //--------------
Robert Phillips8e1c4e62017-02-19 12:27:01 -050068 // Test asTextureProxyRef - as long as there is a context this should succeed
robertphillips64612512016-04-08 12:10:42 -070069 if (context) {
Robert Phillips8e1c4e62017-02-19 12:27:01 -050070 sk_sp<GrTextureProxy> proxy(img->asTextureProxyRef(context));
71 REPORTER_ASSERT(reporter, proxy);
robertphillips64612512016-04-08 12:10:42 -070072 }
73#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080074
75 //--------------
robertphillips64612512016-04-08 12:10:42 -070076 // Test getROPixels - this should always succeed regardless of backing store
77 SkBitmap bitmap;
78 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
robertphillipsed086ca2016-04-26 15:02:25 -070079 if (context) {
80 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
81 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
82 } else {
83 REPORTER_ASSERT(reporter, size == bitmap.width());
84 REPORTER_ASSERT(reporter, size == bitmap.height());
85 }
robertphillipsb6c65e92016-02-04 10:52:42 -080086
87 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070088 // Test that draw restricts itself to the subset
brianosmaneed6b0e2016-09-23 13:04:05 -070089 SkImageFilter::OutputProperties outProps(img->getColorSpace());
90 sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
Matt Sarettcb6266b2017-01-17 10:48:53 -050091 kPremul_SkAlphaType));
robertphillipsb6c65e92016-02-04 10:52:42 -080092
93 SkCanvas* canvas = surf->getCanvas();
94
95 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080096 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080097
98 SkBitmap bm;
Matt Sarettcb6266b2017-01-17 10:48:53 -050099 bm.allocN32Pixels(kFullSize, kFullSize, false);
robertphillipsb6c65e92016-02-04 10:52:42 -0800100
101 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
102 SkASSERT_RELEASE(result);
103
104 // Only the center (red) portion should've been drawn into the canvas
105 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
106 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
107 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
108 kSmallerSize+kPad-1));
109 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
110 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700111
112 //--------------
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500113 // Test that asImage & makeTightSurface return appropriately sized objects
robertphillipsb4bd11e2016-03-21 13:44:18 -0700114 // of the correct backing type
115 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
116 {
Robert Phillipsa5fdc972017-02-18 16:58:09 -0500117 sk_sp<SkImage> tightImg(img->asImage(&newSubset));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700118
119 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
120 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
Robert Phillips6de99042017-01-31 11:31:39 -0500121 REPORTER_ASSERT(reporter, isGPUBacked == !!tightImg->isTextureBacked());
robertphillipsb4bd11e2016-03-21 13:44:18 -0700122 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500123 REPORTER_ASSERT(reporter, isGPUBacked != !!tightImg->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700124 }
125 {
brianosmaneed6b0e2016-09-23 13:04:05 -0700126 SkImageFilter::OutputProperties outProps(img->getColorSpace());
127 sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700128
129 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
130 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
Robert Phillips6de99042017-01-31 11:31:39 -0500131 REPORTER_ASSERT(reporter, isGPUBacked ==
robertphillipsb4bd11e2016-03-21 13:44:18 -0700132 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
133 SkPixmap tmpPixmap;
Robert Phillips6de99042017-01-31 11:31:39 -0500134 REPORTER_ASSERT(reporter, isGPUBacked != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700135 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800136}
137
138DEF_TEST(SpecialImage_Raster, reporter) {
139 SkBitmap bm = create_bm();
140
robertphillips37bd7c32016-03-17 14:31:39 -0700141 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700142 SkIRect::MakeWH(kFullSize, kFullSize),
143 bm));
144
robertphillipsb6c65e92016-02-04 10:52:42 -0800145 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
146
robertphillipsc5035e72016-03-17 06:58:39 -0700147 {
robertphillips3e302272016-04-20 11:48:36 -0700148 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700149 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700150 }
151
152 {
robertphillips37bd7c32016-03-17 14:31:39 -0700153 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700154 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700155 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800156}
157
Brian Osman61624f02016-12-09 14:51:59 -0500158static void test_specialimage_image(skiatest::Reporter* reporter, SkColorSpace* dstColorSpace) {
robertphillipsb6c65e92016-02-04 10:52:42 -0800159 SkBitmap bm = create_bm();
160
reed9ce9d672016-03-17 10:51:11 -0700161 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800162
robertphillips37bd7c32016-03-17 14:31:39 -0700163 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700164 SkIRect::MakeWH(kFullSize, kFullSize),
Brian Osman61624f02016-12-09 14:51:59 -0500165 fullImage, dstColorSpace));
robertphillipsc5035e72016-03-17 06:58:39 -0700166
robertphillipsb6c65e92016-02-04 10:52:42 -0800167 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
168
robertphillipsc5035e72016-03-17 06:58:39 -0700169 {
Brian Osman61624f02016-12-09 14:51:59 -0500170 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage,
171 dstColorSpace));
robertphillipsed086ca2016-04-26 15:02:25 -0700172 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700173 }
174
175 {
robertphillips37bd7c32016-03-17 14:31:39 -0700176 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700177 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700178 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800179}
180
Brian Osman7992da32016-11-18 11:28:24 -0500181DEF_TEST(SpecialImage_Image_Legacy, reporter) {
Brian Osman61624f02016-12-09 14:51:59 -0500182 SkColorSpace* legacyColorSpace = nullptr;
183 test_specialimage_image(reporter, legacyColorSpace);
Brian Osman7992da32016-11-18 11:28:24 -0500184}
185
186DEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500187 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeSRGB();
Brian Osman61624f02016-12-09 14:51:59 -0500188 test_specialimage_image(reporter, srgbColorSpace.get());
Brian Osman7992da32016-11-18 11:28:24 -0500189}
190
robertphillipsb6c65e92016-02-04 10:52:42 -0800191#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700192
193static void test_texture_backed(skiatest::Reporter* reporter,
194 const sk_sp<SkSpecialImage>& orig,
195 const sk_sp<SkSpecialImage>& gpuBacked) {
196 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700197 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700198 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
199 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
200 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700201 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700202}
203
204// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700205DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700206 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700207 SkBitmap bm = create_bm();
208
209 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
210
211 {
212 // raster
213 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700214 SkIRect::MakeWH(kFullSize,
215 kFullSize),
216 bm));
217
218 {
robertphillips3e302272016-04-20 11:48:36 -0700219 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700220 test_texture_backed(reporter, rasterImage, fromRaster);
221 }
222
223 {
224 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
225
robertphillips3e302272016-04-20 11:48:36 -0700226 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700227 test_texture_backed(reporter, subRasterImage, fromSubRaster);
228 }
229 }
230
231 {
232 // gpu
233 GrSurfaceDesc desc;
234 desc.fConfig = kSkia8888_GrPixelConfig;
235 desc.fFlags = kNone_GrSurfaceFlags;
236 desc.fWidth = kFullSize;
237 desc.fHeight = kFullSize;
238
robertphillipsc91fd342016-04-25 12:32:54 -0700239 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
240 SkBudgeted::kNo,
241 bm.getPixels(),
242 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700243 if (!texture) {
244 return;
245 }
246
247 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700248 SkIRect::MakeWH(kFullSize,
249 kFullSize),
250 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700251 std::move(texture), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700252
253 {
robertphillips3e302272016-04-20 11:48:36 -0700254 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700255 test_texture_backed(reporter, gpuImage, fromGPU);
256 }
257
258 {
259 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
260
robertphillips3e302272016-04-20 11:48:36 -0700261 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700262 test_texture_backed(reporter, subGPUImage, fromSubGPU);
263 }
264 }
265}
266
bsalomon68d91342016-04-12 09:59:58 -0700267DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700268 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800269 SkBitmap bm = create_bm();
270
271 GrSurfaceDesc desc;
272 desc.fConfig = kSkia8888_GrPixelConfig;
273 desc.fFlags = kNone_GrSurfaceFlags;
274 desc.fWidth = kFullSize;
275 desc.fHeight = kFullSize;
276
robertphillipsc91fd342016-04-25 12:32:54 -0700277 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
278 SkBudgeted::kNo,
279 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800280 if (!texture) {
281 return;
282 }
283
robertphillips37bd7c32016-03-17 14:31:39 -0700284 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700285 SkIRect::MakeWH(kFullSize, kFullSize),
286 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700287 texture, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700288
robertphillipsb6c65e92016-02-04 10:52:42 -0800289 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
290
robertphillipsc5035e72016-03-17 06:58:39 -0700291 {
robertphillips37bd7c32016-03-17 14:31:39 -0700292 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700293 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700294 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700295 texture, nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700296 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700297 }
298
299 {
robertphillips37bd7c32016-03-17 14:31:39 -0700300 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700301 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700302 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800303}
304
Robert Phillips8bc06d02016-11-01 17:28:40 -0400305DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
306 GrContext* context = ctxInfo.grContext();
307 SkBitmap bm = create_bm();
308
309 GrSurfaceDesc desc;
310 desc.fConfig = kSkia8888_GrPixelConfig;
311 desc.fFlags = kNone_GrSurfaceFlags;
312 desc.fWidth = kFullSize;
313 desc.fHeight = kFullSize;
314
Robert Phillips37430132016-11-09 06:50:43 -0500315 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
316 context->textureProvider(),
317 desc, SkBudgeted::kNo,
318 bm.getPixels(), 0));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400319 if (!proxy) {
320 return;
321 }
322
323 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
324 context,
325 SkIRect::MakeWH(kFullSize, kFullSize),
326 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -0500327 sk_ref_sp(proxy->asTextureProxy()),
328 nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400329
330 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
331
332 {
333 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
334 context,
335 subset,
336 kNeedNewImageUniqueID_SpecialImage,
Robert Phillips63c67462017-02-15 14:19:01 -0500337 sk_ref_sp(proxy->asTextureProxy()),
338 nullptr));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400339 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
340 }
341
342 {
343 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
344 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
345 }
346}
347
robertphillipsb6c65e92016-02-04 10:52:42 -0800348#endif