blob: 8cd874a3c8ca2cb16ff8786efa0bfd2565becef1 [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,
robertphillips64612512016-04-08 12:10:42 -070054 GrContext* context, bool peekTextureSucceeds,
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
robertphillips64612512016-04-08 12:10:42 -070064 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked());
65
66#if SK_SUPPORT_GPU
67 //--------------
68 // Test getTextureAsRef - as long as there is a context this should succeed
69 if (context) {
70 sk_sp<GrTexture> texture(img->asTextureRef(context));
71 REPORTER_ASSERT(reporter, texture);
72 }
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),
91 kOpaque_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;
99 bm.allocN32Pixels(kFullSize, kFullSize, true);
100
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 //--------------
113 // Test that makeTightSubset & makeTightSurface return appropriately sized objects
114 // of the correct backing type
115 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
116 {
117 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset));
118
119 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
120 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
121 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture());
122 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700123 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!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());
131 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
132 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
133 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700134 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!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 Osman7992da32016-11-18 11:28:24 -0500158static void test_specialimage_image(skiatest::Reporter* reporter,
159 SkDestinationSurfaceColorMode colorMode) {
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 Osman7992da32016-11-18 11:28:24 -0500166 fullImage, colorMode));
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 Osman7992da32016-11-18 11:28:24 -0500171 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage, colorMode));
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) {
182 test_specialimage_image(reporter, SkDestinationSurfaceColorMode::kLegacy);
183}
184
185DEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
186 test_specialimage_image(reporter, SkDestinationSurfaceColorMode::kGammaAndColorSpaceAware);
187}
188
robertphillipsb6c65e92016-02-04 10:52:42 -0800189#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700190
191static void test_texture_backed(skiatest::Reporter* reporter,
192 const sk_sp<SkSpecialImage>& orig,
193 const sk_sp<SkSpecialImage>& gpuBacked) {
194 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700195 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700196 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
197 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
198 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700199 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700200}
201
202// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700203DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700204 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700205 SkBitmap bm = create_bm();
206
207 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
208
209 {
210 // raster
211 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700212 SkIRect::MakeWH(kFullSize,
213 kFullSize),
214 bm));
215
216 {
robertphillips3e302272016-04-20 11:48:36 -0700217 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700218 test_texture_backed(reporter, rasterImage, fromRaster);
219 }
220
221 {
222 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
223
robertphillips3e302272016-04-20 11:48:36 -0700224 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700225 test_texture_backed(reporter, subRasterImage, fromSubRaster);
226 }
227 }
228
229 {
230 // gpu
231 GrSurfaceDesc desc;
232 desc.fConfig = kSkia8888_GrPixelConfig;
233 desc.fFlags = kNone_GrSurfaceFlags;
234 desc.fWidth = kFullSize;
235 desc.fHeight = kFullSize;
236
robertphillipsc91fd342016-04-25 12:32:54 -0700237 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
238 SkBudgeted::kNo,
239 bm.getPixels(),
240 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700241 if (!texture) {
242 return;
243 }
244
245 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700246 SkIRect::MakeWH(kFullSize,
247 kFullSize),
248 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700249 std::move(texture), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700250
251 {
robertphillips3e302272016-04-20 11:48:36 -0700252 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700253 test_texture_backed(reporter, gpuImage, fromGPU);
254 }
255
256 {
257 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
258
robertphillips3e302272016-04-20 11:48:36 -0700259 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700260 test_texture_backed(reporter, subGPUImage, fromSubGPU);
261 }
262 }
263}
264
bsalomon68d91342016-04-12 09:59:58 -0700265DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700266 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800267 SkBitmap bm = create_bm();
268
269 GrSurfaceDesc desc;
270 desc.fConfig = kSkia8888_GrPixelConfig;
271 desc.fFlags = kNone_GrSurfaceFlags;
272 desc.fWidth = kFullSize;
273 desc.fHeight = kFullSize;
274
robertphillipsc91fd342016-04-25 12:32:54 -0700275 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
276 SkBudgeted::kNo,
277 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800278 if (!texture) {
279 return;
280 }
281
robertphillips37bd7c32016-03-17 14:31:39 -0700282 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700283 SkIRect::MakeWH(kFullSize, kFullSize),
284 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700285 texture, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700286
robertphillipsb6c65e92016-02-04 10:52:42 -0800287 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
288
robertphillipsc5035e72016-03-17 06:58:39 -0700289 {
robertphillips37bd7c32016-03-17 14:31:39 -0700290 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700291 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700292 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700293 texture, nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700294 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700295 }
296
297 {
robertphillips37bd7c32016-03-17 14:31:39 -0700298 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700299 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700300 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800301}
302
Robert Phillips8bc06d02016-11-01 17:28:40 -0400303DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
304 GrContext* context = ctxInfo.grContext();
305 SkBitmap bm = create_bm();
306
307 GrSurfaceDesc desc;
308 desc.fConfig = kSkia8888_GrPixelConfig;
309 desc.fFlags = kNone_GrSurfaceFlags;
310 desc.fWidth = kFullSize;
311 desc.fHeight = kFullSize;
312
Robert Phillips37430132016-11-09 06:50:43 -0500313 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
314 context->textureProvider(),
315 desc, SkBudgeted::kNo,
316 bm.getPixels(), 0));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400317 if (!proxy) {
318 return;
319 }
320
321 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
322 context,
323 SkIRect::MakeWH(kFullSize, kFullSize),
324 kNeedNewImageUniqueID_SpecialImage,
325 proxy, nullptr));
326
327 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
328
329 {
330 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
331 context,
332 subset,
333 kNeedNewImageUniqueID_SpecialImage,
334 proxy, nullptr));
335 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
336 }
337
338 {
339 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
340 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
341 }
342}
343
robertphillipsb6c65e92016-02-04 10:52:42 -0800344#endif