blob: 432adc832735ff8eb1246bf8c104d508f94a063e [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),
46 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
47 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 {
186 // The SkAutoPixmapStorage keeps hold of the memory
robertphillips37bd7c32016-03-17 14:31:39 -0700187 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap,
188 nullptr, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700189 test_image(img, reporter, true, false, kPad, kFullSize);
190 }
191
192 {
193 // The image takes ownership of the memory
robertphillips37bd7c32016-03-17 14:31:39 -0700194 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(
robertphillipsc5035e72016-03-17 06:58:39 -0700195 nullptr, subset, pixmap,
robertphillips37bd7c32016-03-17 14:31:39 -0700196 [] (void* addr, void*) -> void {
197 sk_free(addr);
198 },
robertphillipsc5035e72016-03-17 06:58:39 -0700199 nullptr));
200 pixmap.release();
201 test_image(img, reporter, true, false, kPad, kFullSize);
202 }
203}
204
205
robertphillipsb6c65e92016-02-04 10:52:42 -0800206#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700207
208static void test_texture_backed(skiatest::Reporter* reporter,
209 const sk_sp<SkSpecialImage>& orig,
210 const sk_sp<SkSpecialImage>& gpuBacked) {
211 REPORTER_ASSERT(reporter, gpuBacked);
212 REPORTER_ASSERT(reporter, gpuBacked->peekTexture());
213 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
214 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
215 gpuBacked->subset().height() == orig->subset().height());
216}
217
218// Test out the SkSpecialImage::makeTextureImage entry point
219DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, context) {
220 SkBitmap bm = create_bm();
221
222 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
223
224 {
225 // raster
226 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
227 nullptr,
228 SkIRect::MakeWH(kFullSize,
229 kFullSize),
230 bm));
231
232 {
233 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
234 test_texture_backed(reporter, rasterImage, fromRaster);
235 }
236
237 {
238 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
239
240 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
241 test_texture_backed(reporter, subRasterImage, fromSubRaster);
242 }
243 }
244
245 {
246 // gpu
247 GrSurfaceDesc desc;
248 desc.fConfig = kSkia8888_GrPixelConfig;
249 desc.fFlags = kNone_GrSurfaceFlags;
250 desc.fWidth = kFullSize;
251 desc.fHeight = kFullSize;
252
253 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
254 SkBudgeted::kNo,
255 bm.getPixels(),
256 0));
257 if (!texture) {
258 return;
259 }
260
261 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
262 nullptr,
263 SkIRect::MakeWH(kFullSize,
264 kFullSize),
265 kNeedNewImageUniqueID_SpecialImage,
266 texture));
267
268 {
269 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
270 test_texture_backed(reporter, gpuImage, fromGPU);
271 }
272
273 {
274 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
275
276 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
277 test_texture_backed(reporter, subGPUImage, fromSubGPU);
278 }
279 }
280}
281
robertphillipsb6c65e92016-02-04 10:52:42 -0800282DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, context) {
283 SkBitmap bm = create_bm();
284
285 GrSurfaceDesc desc;
286 desc.fConfig = kSkia8888_GrPixelConfig;
287 desc.fFlags = kNone_GrSurfaceFlags;
288 desc.fWidth = kFullSize;
289 desc.fHeight = kFullSize;
290
robertphillips83c17fa2016-03-18 08:14:27 -0700291 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
292 SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800293 bm.getPixels(), 0));
294 if (!texture) {
295 return;
296 }
297
robertphillips37bd7c32016-03-17 14:31:39 -0700298 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700299 nullptr,
300 SkIRect::MakeWH(kFullSize, kFullSize),
301 kNeedNewImageUniqueID_SpecialImage,
302 texture));
303
robertphillipsb6c65e92016-02-04 10:52:42 -0800304 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
305
robertphillipsc5035e72016-03-17 06:58:39 -0700306 {
robertphillips37bd7c32016-03-17 14:31:39 -0700307 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700308 nullptr, subset,
309 kNeedNewImageUniqueID_SpecialImage,
310 texture));
311 test_image(subSImg1, reporter, false, true, kPad, kFullSize);
312 }
313
314 {
robertphillips37bd7c32016-03-17 14:31:39 -0700315 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsc5035e72016-03-17 06:58:39 -0700316 test_image(subSImg2, reporter, false, true, kPad, kFullSize);
317 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800318}
319
320#endif