blob: 62a5da60894c73a24bb345fb759b8f9730004509 [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
158DEF_TEST(SpecialImage_Image, reporter) {
159 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),
robertphillips37bd7c32016-03-17 14:31:39 -0700165 fullImage));
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 {
robertphillips3e302272016-04-20 11:48:36 -0700170 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage));
robertphillipsed086ca2016-04-26 15:02:25 -0700171 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700172 }
173
174 {
robertphillips37bd7c32016-03-17 14:31:39 -0700175 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700176 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700177 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800178}
179
180#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700181
182static void test_texture_backed(skiatest::Reporter* reporter,
183 const sk_sp<SkSpecialImage>& orig,
184 const sk_sp<SkSpecialImage>& gpuBacked) {
185 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700186 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700187 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
188 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
189 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700190 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700191}
192
193// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700194DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700195 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700196 SkBitmap bm = create_bm();
197
198 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
199
200 {
201 // raster
202 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700203 SkIRect::MakeWH(kFullSize,
204 kFullSize),
205 bm));
206
207 {
robertphillips3e302272016-04-20 11:48:36 -0700208 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700209 test_texture_backed(reporter, rasterImage, fromRaster);
210 }
211
212 {
213 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
214
robertphillips3e302272016-04-20 11:48:36 -0700215 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700216 test_texture_backed(reporter, subRasterImage, fromSubRaster);
217 }
218 }
219
220 {
221 // gpu
222 GrSurfaceDesc desc;
223 desc.fConfig = kSkia8888_GrPixelConfig;
224 desc.fFlags = kNone_GrSurfaceFlags;
225 desc.fWidth = kFullSize;
226 desc.fHeight = kFullSize;
227
robertphillipsc91fd342016-04-25 12:32:54 -0700228 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
229 SkBudgeted::kNo,
230 bm.getPixels(),
231 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700232 if (!texture) {
233 return;
234 }
235
236 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700237 SkIRect::MakeWH(kFullSize,
238 kFullSize),
239 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700240 std::move(texture), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700241
242 {
robertphillips3e302272016-04-20 11:48:36 -0700243 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700244 test_texture_backed(reporter, gpuImage, fromGPU);
245 }
246
247 {
248 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
249
robertphillips3e302272016-04-20 11:48:36 -0700250 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700251 test_texture_backed(reporter, subGPUImage, fromSubGPU);
252 }
253 }
254}
255
bsalomon68d91342016-04-12 09:59:58 -0700256DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700257 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800258 SkBitmap bm = create_bm();
259
260 GrSurfaceDesc desc;
261 desc.fConfig = kSkia8888_GrPixelConfig;
262 desc.fFlags = kNone_GrSurfaceFlags;
263 desc.fWidth = kFullSize;
264 desc.fHeight = kFullSize;
265
robertphillipsc91fd342016-04-25 12:32:54 -0700266 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
267 SkBudgeted::kNo,
268 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800269 if (!texture) {
270 return;
271 }
272
robertphillips37bd7c32016-03-17 14:31:39 -0700273 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700274 SkIRect::MakeWH(kFullSize, kFullSize),
275 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700276 texture, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700277
robertphillipsb6c65e92016-02-04 10:52:42 -0800278 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
279
robertphillipsc5035e72016-03-17 06:58:39 -0700280 {
robertphillips37bd7c32016-03-17 14:31:39 -0700281 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700282 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700283 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700284 texture, nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700285 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700286 }
287
288 {
robertphillips37bd7c32016-03-17 14:31:39 -0700289 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700290 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700291 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800292}
293
Robert Phillips8bc06d02016-11-01 17:28:40 -0400294DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
295 GrContext* context = ctxInfo.grContext();
296 SkBitmap bm = create_bm();
297
298 GrSurfaceDesc desc;
299 desc.fConfig = kSkia8888_GrPixelConfig;
300 desc.fFlags = kNone_GrSurfaceFlags;
301 desc.fWidth = kFullSize;
302 desc.fHeight = kFullSize;
303
Robert Phillips37430132016-11-09 06:50:43 -0500304 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
305 context->textureProvider(),
306 desc, SkBudgeted::kNo,
307 bm.getPixels(), 0));
Robert Phillips8bc06d02016-11-01 17:28:40 -0400308 if (!proxy) {
309 return;
310 }
311
312 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
313 context,
314 SkIRect::MakeWH(kFullSize, kFullSize),
315 kNeedNewImageUniqueID_SpecialImage,
316 proxy, nullptr));
317
318 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
319
320 {
321 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
322 context,
323 subset,
324 kNeedNewImageUniqueID_SpecialImage,
325 proxy, nullptr));
326 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
327 }
328
329 {
330 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
331 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
332 }
333}
334
robertphillipsb6c65e92016-02-04 10:52:42 -0800335#endif