blob: 2818ce9ec7388ac1f6e94186966f8bc29708b5a9 [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,
robertphillips64612512016-04-08 12:10:42 -070054 GrContext* context, bool peekTextureSucceeds,
robertphillipsc5035e72016-03-17 06:58:39 -070055 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
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));
79 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());
robertphillipsb6c65e92016-02-04 10:52:42 -080085 }
86
87 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070088 // Test that draw restricts itself to the subset
robertphillipsb6c65e92016-02-04 10:52:42 -080089 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
90
robertphillips37bd7c32016-03-17 14:31:39 -070091 sk_sp<SkSpecialSurface> surf(img->makeSurface(info));
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 {
126 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
127 kPremul_SkAlphaType);
128 sk_sp<SkSurface> tightSurf(img->makeTightSurface(info));
129
130 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
131 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
132 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
133 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
134 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700135 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700136 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800137}
138
139DEF_TEST(SpecialImage_Raster, reporter) {
140 SkBitmap bm = create_bm();
141
robertphillips37bd7c32016-03-17 14:31:39 -0700142 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700143 nullptr,
144 SkIRect::MakeWH(kFullSize, kFullSize),
145 bm));
146
robertphillipsb6c65e92016-02-04 10:52:42 -0800147 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
148
robertphillipsc5035e72016-03-17 06:58:39 -0700149 {
robertphillips37bd7c32016-03-17 14:31:39 -0700150 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(nullptr, subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700151 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700152 }
153
154 {
robertphillips37bd7c32016-03-17 14:31:39 -0700155 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700156 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700157 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800158}
159
160DEF_TEST(SpecialImage_Image, reporter) {
161 SkBitmap bm = create_bm();
162
reed9ce9d672016-03-17 10:51:11 -0700163 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800164
robertphillips37bd7c32016-03-17 14:31:39 -0700165 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700166 nullptr,
167 SkIRect::MakeWH(kFullSize, kFullSize),
robertphillips37bd7c32016-03-17 14:31:39 -0700168 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700169
robertphillipsb6c65e92016-02-04 10:52:42 -0800170 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
171
robertphillipsc5035e72016-03-17 06:58:39 -0700172 {
robertphillips37bd7c32016-03-17 14:31:39 -0700173 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(nullptr, subset,
174 fullImage));
robertphillips64612512016-04-08 12:10:42 -0700175 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700176 }
177
178 {
robertphillips37bd7c32016-03-17 14:31:39 -0700179 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700180 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700181 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800182}
183
robertphillipsc5035e72016-03-17 06:58:39 -0700184DEF_TEST(SpecialImage_Pixmap, reporter) {
185 SkAutoPixmapStorage pixmap;
186
187 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
188 pixmap.alloc(info);
189 pixmap.erase(SK_ColorGREEN);
190
191 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
192
193 pixmap.erase(SK_ColorRED, subset);
194
195 {
robertphillips37bd7c32016-03-17 14:31:39 -0700196 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(nullptr, subset, pixmap,
197 nullptr, nullptr));
robertphillips64612512016-04-08 12:10:42 -0700198 test_image(img, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700199 }
robertphillipsc5035e72016-03-17 06:58:39 -0700200}
201
202
robertphillipsb6c65e92016-02-04 10:52:42 -0800203#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700204
205static void test_texture_backed(skiatest::Reporter* reporter,
206 const sk_sp<SkSpecialImage>& orig,
207 const sk_sp<SkSpecialImage>& gpuBacked) {
208 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700209 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700210 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
211 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
212 gpuBacked->subset().height() == orig->subset().height());
213}
214
215// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon758586c2016-04-06 14:02:39 -0700216DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700217 GrContext* context = ctxInfo.fGrContext;
robertphillips83c17fa2016-03-18 08:14:27 -0700218 SkBitmap bm = create_bm();
219
220 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
221
222 {
223 // raster
224 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
225 nullptr,
226 SkIRect::MakeWH(kFullSize,
227 kFullSize),
228 bm));
229
230 {
231 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(nullptr, context));
232 test_texture_backed(reporter, rasterImage, fromRaster);
233 }
234
235 {
236 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
237
238 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(nullptr, context));
239 test_texture_backed(reporter, subRasterImage, fromSubRaster);
240 }
241 }
242
243 {
244 // gpu
245 GrSurfaceDesc desc;
246 desc.fConfig = kSkia8888_GrPixelConfig;
247 desc.fFlags = kNone_GrSurfaceFlags;
248 desc.fWidth = kFullSize;
249 desc.fHeight = kFullSize;
250
251 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
252 SkBudgeted::kNo,
253 bm.getPixels(),
254 0));
255 if (!texture) {
256 return;
257 }
258
259 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
260 nullptr,
261 SkIRect::MakeWH(kFullSize,
262 kFullSize),
263 kNeedNewImageUniqueID_SpecialImage,
264 texture));
265
266 {
267 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(nullptr, context));
268 test_texture_backed(reporter, gpuImage, fromGPU);
269 }
270
271 {
272 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
273
274 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(nullptr, context));
275 test_texture_backed(reporter, subGPUImage, fromSubGPU);
276 }
277 }
278}
279
bsalomon758586c2016-04-06 14:02:39 -0700280DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700281 GrContext* context = ctxInfo.fGrContext;
robertphillipsb6c65e92016-02-04 10:52:42 -0800282 SkBitmap bm = create_bm();
283
284 GrSurfaceDesc desc;
285 desc.fConfig = kSkia8888_GrPixelConfig;
286 desc.fFlags = kNone_GrSurfaceFlags;
287 desc.fWidth = kFullSize;
288 desc.fHeight = kFullSize;
289
robertphillips83c17fa2016-03-18 08:14:27 -0700290 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
291 SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800292 bm.getPixels(), 0));
293 if (!texture) {
294 return;
295 }
296
robertphillips37bd7c32016-03-17 14:31:39 -0700297 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700298 nullptr,
299 SkIRect::MakeWH(kFullSize, kFullSize),
300 kNeedNewImageUniqueID_SpecialImage,
301 texture));
302
robertphillipsb6c65e92016-02-04 10:52:42 -0800303 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
304
robertphillipsc5035e72016-03-17 06:58:39 -0700305 {
robertphillips37bd7c32016-03-17 14:31:39 -0700306 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
halcanary9d524f22016-03-29 09:03:52 -0700307 nullptr, subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700308 kNeedNewImageUniqueID_SpecialImage,
309 texture));
robertphillips64612512016-04-08 12:10:42 -0700310 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700311 }
312
313 {
robertphillips37bd7c32016-03-17 14:31:39 -0700314 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700315 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700316 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800317}
318
319#endif