blob: f16f5ff84c26c5d7eba653d0fe4e6d051a901c29 [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"
robertphillips4418dba2016-03-07 12:45:14 -080017#include "TestingSpecialImageAccess.h"
robertphillipsb6c65e92016-02-04 10:52:42 -080018
19#if SK_SUPPORT_GPU
20#include "GrContext.h"
21#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,
robertphillipsc5035e72016-03-17 06:58:39 -070054 bool peekPixelsSucceeds, bool peekTextureSucceeds,
55 int offset, int size) {
robertphillips37bd7c32016-03-17 14:31:39 -070056 const SkIRect subset = TestingSpecialImageAccess::Subset(img.get());
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
robertphillips37bd7c32016-03-17 14:31:39 -070064 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
65 !!TestingSpecialImageAccess::PeekTexture(img.get()));
robertphillipsb6c65e92016-02-04 10:52:42 -080066
67 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070068 // Test that peekPixels reports the correct backing type
robertphillipsb6c65e92016-02-04 10:52:42 -080069 SkPixmap pixmap;
70 REPORTER_ASSERT(reporter, peekPixelsSucceeds ==
robertphillips37bd7c32016-03-17 14:31:39 -070071 !!TestingSpecialImageAccess::PeekPixels(img.get(), &pixmap));
robertphillipsb6c65e92016-02-04 10:52:42 -080072 if (peekPixelsSucceeds) {
robertphillipsc5035e72016-03-17 06:58:39 -070073 REPORTER_ASSERT(reporter, size == pixmap.width());
74 REPORTER_ASSERT(reporter, size == pixmap.height());
robertphillipsb6c65e92016-02-04 10:52:42 -080075 }
76
77 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070078 // Test that draw restricts itself to the subset
robertphillipsb6c65e92016-02-04 10:52:42 -080079 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
80
robertphillips37bd7c32016-03-17 14:31:39 -070081 sk_sp<SkSpecialSurface> surf(img->makeSurface(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080082
83 SkCanvas* canvas = surf->getCanvas();
84
85 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080086 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080087
88 SkBitmap bm;
89 bm.allocN32Pixels(kFullSize, kFullSize, true);
90
91 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
92 SkASSERT_RELEASE(result);
93
94 // Only the center (red) portion should've been drawn into the canvas
95 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
96 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
97 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
98 kSmallerSize+kPad-1));
99 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
100 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700101
102 //--------------
103 // Test that makeTightSubset & makeTightSurface return appropriately sized objects
104 // of the correct backing type
105 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
106 {
107 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset));
108
109 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
110 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
111 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture());
112 SkPixmap tmpPixmap;
113 REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!tightImg->peekPixels(&tmpPixmap));
114 }
115 {
116 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
117 kPremul_SkAlphaType);
118 sk_sp<SkSurface> tightSurf(img->makeTightSurface(info));
119
120 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
121 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
122 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
123 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
124 SkPixmap tmpPixmap;
125 REPORTER_ASSERT(reporter, peekPixelsSucceeds == !!tightSurf->peekPixels(&tmpPixmap));
126 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800127}
128
129DEF_TEST(SpecialImage_Raster, reporter) {
130 SkBitmap bm = create_bm();
131
robertphillips37bd7c32016-03-17 14:31:39 -0700132 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700133 nullptr,
134 SkIRect::MakeWH(kFullSize, kFullSize),
135 bm));
136
robertphillipsb6c65e92016-02-04 10:52:42 -0800137 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
138
robertphillipsc5035e72016-03-17 06:58:39 -0700139 {
robertphillips37bd7c32016-03-17 14:31:39 -0700140 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, subset, bm));
robertphillipsc5035e72016-03-17 06:58:39 -0700141 test_image(subSImg1, reporter, true, false, kPad, kFullSize);
142 }
143
144 {
robertphillips37bd7c32016-03-17 14:31:39 -0700145 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700146 test_image(subSImg2, reporter, true, false, 0, kSmallerSize);
147 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800148}
149
150DEF_TEST(SpecialImage_Image, reporter) {
151 SkBitmap bm = create_bm();
152
reed9ce9d672016-03-17 10:51:11 -0700153 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800154
robertphillips37bd7c32016-03-17 14:31:39 -0700155 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700156 nullptr,
157 SkIRect::MakeWH(kFullSize, kFullSize),
robertphillips37bd7c32016-03-17 14:31:39 -0700158 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700159
robertphillipsb6c65e92016-02-04 10:52:42 -0800160 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
161
robertphillipsc5035e72016-03-17 06:58:39 -0700162 {
robertphillips37bd7c32016-03-17 14:31:39 -0700163 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset,
164 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700165 test_image(subSImg1, reporter, true, false, kPad, kFullSize);
166 }
167
168 {
robertphillips37bd7c32016-03-17 14:31:39 -0700169 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700170 test_image(subSImg2, reporter, true, false, 0, kSmallerSize);
171 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800172}
173
robertphillipsc5035e72016-03-17 06:58:39 -0700174DEF_TEST(SpecialImage_Pixmap, reporter) {
175 SkAutoPixmapStorage pixmap;
176
177 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
178 pixmap.alloc(info);
179 pixmap.erase(SK_ColorGREEN);
180
181 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
182
183 pixmap.erase(SK_ColorRED, subset);
184
185 {
robertphillips37bd7c32016-03-17 14:31:39 -0700186 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap,
187 nullptr, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700188 test_image(img, reporter, true, false, kPad, kFullSize);
189 }
robertphillipsc5035e72016-03-17 06:58:39 -0700190}
191
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);
199 REPORTER_ASSERT(reporter, gpuBacked->peekTexture());
200 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
201 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
202 gpuBacked->subset().height() == orig->subset().height());
203}
204
205// Test out the SkSpecialImage::makeTextureImage entry point
bsalomonf2f1c172016-04-05 12:59:06 -0700206DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
207 GrContext* context = ctxInfo.fGrContext;
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(
215 nullptr,
216 SkIRect::MakeWH(kFullSize,
217 kFullSize),
218 bm));
219
220 {
221 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
222 test_texture_backed(reporter, rasterImage, fromRaster);
223 }
224
225 {
226 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
227
228 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
229 test_texture_backed(reporter, subRasterImage, fromSubRaster);
230 }
231 }
232
233 {
234 // gpu
235 GrSurfaceDesc desc;
236 desc.fConfig = kSkia8888_GrPixelConfig;
237 desc.fFlags = kNone_GrSurfaceFlags;
238 desc.fWidth = kFullSize;
239 desc.fHeight = kFullSize;
240
241 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
242 SkBudgeted::kNo,
243 bm.getPixels(),
244 0));
245 if (!texture) {
246 return;
247 }
248
249 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
250 nullptr,
251 SkIRect::MakeWH(kFullSize,
252 kFullSize),
253 kNeedNewImageUniqueID_SpecialImage,
254 texture));
255
256 {
257 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
258 test_texture_backed(reporter, gpuImage, fromGPU);
259 }
260
261 {
262 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
263
264 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
265 test_texture_backed(reporter, subGPUImage, fromSubGPU);
266 }
267 }
268}
269
bsalomonf2f1c172016-04-05 12:59:06 -0700270DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
271 GrContext* context = ctxInfo.fGrContext;
robertphillipsb6c65e92016-02-04 10:52:42 -0800272 SkBitmap bm = create_bm();
273
274 GrSurfaceDesc desc;
275 desc.fConfig = kSkia8888_GrPixelConfig;
276 desc.fFlags = kNone_GrSurfaceFlags;
277 desc.fWidth = kFullSize;
278 desc.fHeight = kFullSize;
279
robertphillips83c17fa2016-03-18 08:14:27 -0700280 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
281 SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800282 bm.getPixels(), 0));
283 if (!texture) {
284 return;
285 }
286
robertphillips37bd7c32016-03-17 14:31:39 -0700287 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700288 nullptr,
289 SkIRect::MakeWH(kFullSize, kFullSize),
290 kNeedNewImageUniqueID_SpecialImage,
291 texture));
292
robertphillipsb6c65e92016-02-04 10:52:42 -0800293 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
294
robertphillipsc5035e72016-03-17 06:58:39 -0700295 {
robertphillips37bd7c32016-03-17 14:31:39 -0700296 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
halcanary9d524f22016-03-29 09:03:52 -0700297 nullptr, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700298 kNeedNewImageUniqueID_SpecialImage,
299 texture));
300 test_image(subSImg1, reporter, false, true, kPad, kFullSize);
301 }
302
303 {
robertphillips37bd7c32016-03-17 14:31:39 -0700304 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700305 test_image(subSImg2, reporter, false, true, kPad, kFullSize);
306 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800307}
308
309#endif