blob: 770810a1c8c9d7e53187cc5a73355dcf408e4ce7 [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
206DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, context) {
207 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(
214 nullptr,
215 SkIRect::MakeWH(kFullSize,
216 kFullSize),
217 bm));
218
219 {
220 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
221 test_texture_backed(reporter, rasterImage, fromRaster);
222 }
223
224 {
225 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
226
227 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
228 test_texture_backed(reporter, subRasterImage, fromSubRaster);
229 }
230 }
231
232 {
233 // gpu
234 GrSurfaceDesc desc;
235 desc.fConfig = kSkia8888_GrPixelConfig;
236 desc.fFlags = kNone_GrSurfaceFlags;
237 desc.fWidth = kFullSize;
238 desc.fHeight = kFullSize;
239
240 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
241 SkBudgeted::kNo,
242 bm.getPixels(),
243 0));
244 if (!texture) {
245 return;
246 }
247
248 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
249 nullptr,
250 SkIRect::MakeWH(kFullSize,
251 kFullSize),
252 kNeedNewImageUniqueID_SpecialImage,
253 texture));
254
255 {
256 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
257 test_texture_backed(reporter, gpuImage, fromGPU);
258 }
259
260 {
261 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
262
263 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
264 test_texture_backed(reporter, subGPUImage, fromSubGPU);
265 }
266 }
267}
268
robertphillipsb6c65e92016-02-04 10:52:42 -0800269DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
270 SkBitmap bm = create_bm();
271
272 GrSurfaceDesc desc;
273 desc.fConfig = kSkia8888_GrPixelConfig;
274 desc.fFlags = kNone_GrSurfaceFlags;
275 desc.fWidth = kFullSize;
276 desc.fHeight = kFullSize;
277
robertphillips83c17fa2016-03-18 08:14:27 -0700278 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
279 SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800280 bm.getPixels(), 0));
281 if (!texture) {
282 return;
283 }
284
robertphillips37bd7c32016-03-17 14:31:39 -0700285 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700286 nullptr,
287 SkIRect::MakeWH(kFullSize, kFullSize),
288 kNeedNewImageUniqueID_SpecialImage,
289 texture));
290
robertphillipsb6c65e92016-02-04 10:52:42 -0800291 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
292
robertphillipsc5035e72016-03-17 06:58:39 -0700293 {
robertphillips37bd7c32016-03-17 14:31:39 -0700294 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
halcanary9d524f22016-03-29 09:03:52 -0700295 nullptr, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700296 kNeedNewImageUniqueID_SpecialImage,
297 texture));
298 test_image(subSImg1, reporter, false, true, kPad, kFullSize);
299 }
300
301 {
robertphillips37bd7c32016-03-17 14:31:39 -0700302 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700303 test_image(subSImg2, reporter, false, true, kPad, kFullSize);
304 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800305}
306
307#endif