blob: 18d023f9cdc370b5caccd7b980738608435b4f4e [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"
20#endif
21
robertphillipsb6c65e92016-02-04 10:52:42 -080022
23// This test creates backing resources exactly sized to [kFullSize x kFullSize].
24// It then wraps them in an SkSpecialImage with only the center (red) region being active.
25// It then draws the SkSpecialImage to a full sized (all blue) canvas and checks that none
26// of the inactive (green) region leaked out.
27
28static const int kSmallerSize = 10;
29static const int kPad = 3;
30static const int kFullSize = kSmallerSize + 2 * kPad;
31
32// Create a bitmap with red in the center and green around it
33static SkBitmap create_bm() {
34 SkBitmap bm;
35 bm.allocN32Pixels(kFullSize, kFullSize, true);
36
37 SkCanvas temp(bm);
38
39 temp.clear(SK_ColorGREEN);
40 SkPaint p;
41 p.setColor(SK_ColorRED);
42 p.setAntiAlias(false);
43
44 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
halcanary9d524f22016-03-29 09:03:52 -070045 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
robertphillipsb6c65e92016-02-04 10:52:42 -080046 p);
47
48 return bm;
49}
50
51// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
robertphillips37bd7c32016-03-17 14:31:39 -070052static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
robertphillips64612512016-04-08 12:10:42 -070053 GrContext* context, bool peekTextureSucceeds,
robertphillipsc5035e72016-03-17 06:58:39 -070054 int offset, int size) {
robertphillips3e302272016-04-20 11:48:36 -070055 const SkIRect subset = img->subset();
robertphillipsc5035e72016-03-17 06:58:39 -070056 REPORTER_ASSERT(reporter, offset == subset.left());
57 REPORTER_ASSERT(reporter, offset == subset.top());
robertphillipsb6c65e92016-02-04 10:52:42 -080058 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
59 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
60
61 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070062 // Test that peekTexture reports the correct backing type
robertphillips64612512016-04-08 12:10:42 -070063 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked());
64
65#if SK_SUPPORT_GPU
66 //--------------
67 // Test getTextureAsRef - as long as there is a context this should succeed
68 if (context) {
69 sk_sp<GrTexture> texture(img->asTextureRef(context));
70 REPORTER_ASSERT(reporter, texture);
71 }
72#endif
robertphillipsb6c65e92016-02-04 10:52:42 -080073
74 //--------------
robertphillips64612512016-04-08 12:10:42 -070075 // Test getROPixels - this should always succeed regardless of backing store
76 SkBitmap bitmap;
77 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
78 if (context) {
79 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
80 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
81 } else {
82 REPORTER_ASSERT(reporter, size == bitmap.width());
83 REPORTER_ASSERT(reporter, size == bitmap.height());
robertphillipsb6c65e92016-02-04 10:52:42 -080084 }
85
86 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070087 // Test that draw restricts itself to the subset
robertphillipsb6c65e92016-02-04 10:52:42 -080088 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
89
robertphillips37bd7c32016-03-17 14:31:39 -070090 sk_sp<SkSpecialSurface> surf(img->makeSurface(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080091
92 SkCanvas* canvas = surf->getCanvas();
93
94 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080095 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080096
97 SkBitmap bm;
98 bm.allocN32Pixels(kFullSize, kFullSize, true);
99
100 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
101 SkASSERT_RELEASE(result);
102
103 // Only the center (red) portion should've been drawn into the canvas
104 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
105 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
106 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
107 kSmallerSize+kPad-1));
108 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
109 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700110
111 //--------------
112 // Test that makeTightSubset & makeTightSurface return appropriately sized objects
113 // of the correct backing type
114 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
115 {
116 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset));
117
118 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
119 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
120 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture());
121 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700122 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700123 }
124 {
125 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
126 kPremul_SkAlphaType);
127 sk_sp<SkSurface> tightSurf(img->makeTightSurface(info));
128
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));
robertphillips64612512016-04-08 12:10:42 -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));
robertphillips64612512016-04-08 12:10:42 -0700176 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700177 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800178}
179
robertphillipsc5035e72016-03-17 06:58:39 -0700180DEF_TEST(SpecialImage_Pixmap, reporter) {
181 SkAutoPixmapStorage pixmap;
182
183 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
184 pixmap.alloc(info);
185 pixmap.erase(SK_ColorGREEN);
186
187 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
188
189 pixmap.erase(SK_ColorRED, subset);
190
191 {
robertphillips3e302272016-04-20 11:48:36 -0700192 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(subset, pixmap,
robertphillips37bd7c32016-03-17 14:31:39 -0700193 nullptr, nullptr));
robertphillips64612512016-04-08 12:10:42 -0700194 test_image(img, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700195 }
robertphillipsc5035e72016-03-17 06:58:39 -0700196}
197
198
robertphillipsb6c65e92016-02-04 10:52:42 -0800199#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700200
201static void test_texture_backed(skiatest::Reporter* reporter,
202 const sk_sp<SkSpecialImage>& orig,
203 const sk_sp<SkSpecialImage>& gpuBacked) {
204 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700205 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700206 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
207 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
208 gpuBacked->subset().height() == orig->subset().height());
209}
210
211// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700212DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700213 GrContext* context = ctxInfo.fGrContext;
robertphillips83c17fa2016-03-18 08:14:27 -0700214 SkBitmap bm = create_bm();
215
216 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
217
218 {
219 // raster
220 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700221 SkIRect::MakeWH(kFullSize,
222 kFullSize),
223 bm));
224
225 {
robertphillips3e302272016-04-20 11:48:36 -0700226 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700227 test_texture_backed(reporter, rasterImage, fromRaster);
228 }
229
230 {
231 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
232
robertphillips3e302272016-04-20 11:48:36 -0700233 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700234 test_texture_backed(reporter, subRasterImage, fromSubRaster);
235 }
236 }
237
238 {
239 // gpu
240 GrSurfaceDesc desc;
241 desc.fConfig = kSkia8888_GrPixelConfig;
242 desc.fFlags = kNone_GrSurfaceFlags;
243 desc.fWidth = kFullSize;
244 desc.fHeight = kFullSize;
245
246 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
247 SkBudgeted::kNo,
248 bm.getPixels(),
249 0));
250 if (!texture) {
251 return;
252 }
253
254 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700255 SkIRect::MakeWH(kFullSize,
256 kFullSize),
257 kNeedNewImageUniqueID_SpecialImage,
258 texture));
259
260 {
robertphillips3e302272016-04-20 11:48:36 -0700261 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700262 test_texture_backed(reporter, gpuImage, fromGPU);
263 }
264
265 {
266 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
267
robertphillips3e302272016-04-20 11:48:36 -0700268 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700269 test_texture_backed(reporter, subGPUImage, fromSubGPU);
270 }
271 }
272}
273
bsalomon68d91342016-04-12 09:59:58 -0700274DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700275 GrContext* context = ctxInfo.fGrContext;
robertphillipsb6c65e92016-02-04 10:52:42 -0800276 SkBitmap bm = create_bm();
277
278 GrSurfaceDesc desc;
279 desc.fConfig = kSkia8888_GrPixelConfig;
280 desc.fFlags = kNone_GrSurfaceFlags;
281 desc.fWidth = kFullSize;
282 desc.fHeight = kFullSize;
283
robertphillips83c17fa2016-03-18 08:14:27 -0700284 SkAutoTUnref<GrTexture> texture(context->textureProvider()->createTexture(desc,
285 SkBudgeted::kNo,
robertphillipsb6c65e92016-02-04 10:52:42 -0800286 bm.getPixels(), 0));
287 if (!texture) {
288 return;
289 }
290
robertphillips37bd7c32016-03-17 14:31:39 -0700291 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700292 SkIRect::MakeWH(kFullSize, kFullSize),
293 kNeedNewImageUniqueID_SpecialImage,
294 texture));
295
robertphillipsb6c65e92016-02-04 10:52:42 -0800296 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
297
robertphillipsc5035e72016-03-17 06:58:39 -0700298 {
robertphillips37bd7c32016-03-17 14:31:39 -0700299 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700300 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700301 kNeedNewImageUniqueID_SpecialImage,
302 texture));
robertphillips64612512016-04-08 12:10:42 -0700303 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700304 }
305
306 {
robertphillips37bd7c32016-03-17 14:31:39 -0700307 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700308 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700309 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800310}
311
312#endif