blob: 7ab7cfc4ce17ef228d933cc0ffa7bc8b48316c2d [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));
robertphillips0533d6e2016-04-26 14:33:36 -070078 REPORTER_ASSERT(reporter, size == bitmap.width());
79 REPORTER_ASSERT(reporter, size == bitmap.height());
robertphillipsb6c65e92016-02-04 10:52:42 -080080
81 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070082 // Test that draw restricts itself to the subset
robertphillipsb6c65e92016-02-04 10:52:42 -080083 SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
84
robertphillips37bd7c32016-03-17 14:31:39 -070085 sk_sp<SkSpecialSurface> surf(img->makeSurface(info));
robertphillipsb6c65e92016-02-04 10:52:42 -080086
87 SkCanvas* canvas = surf->getCanvas();
88
89 canvas->clear(SK_ColorBLUE);
robertphillipse8c34972016-02-16 12:09:36 -080090 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
robertphillipsb6c65e92016-02-04 10:52:42 -080091
92 SkBitmap bm;
93 bm.allocN32Pixels(kFullSize, kFullSize, true);
94
95 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
96 SkASSERT_RELEASE(result);
97
98 // Only the center (red) portion should've been drawn into the canvas
99 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kPad-1, kPad-1));
100 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kPad, kPad));
101 REPORTER_ASSERT(reporter, SK_ColorRED == bm.getColor(kSmallerSize+kPad-1,
102 kSmallerSize+kPad-1));
103 REPORTER_ASSERT(reporter, SK_ColorBLUE == bm.getColor(kSmallerSize+kPad,
104 kSmallerSize+kPad));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700105
106 //--------------
107 // Test that makeTightSubset & makeTightSurface return appropriately sized objects
108 // of the correct backing type
109 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
110 {
111 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset));
112
113 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
114 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
115 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture());
116 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700117 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700118 }
119 {
120 SkImageInfo info = SkImageInfo::MakeN32(subset.width(), subset.height(),
121 kPremul_SkAlphaType);
122 sk_sp<SkSurface> tightSurf(img->makeTightSurface(info));
123
124 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
125 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
126 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
127 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
128 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700129 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700130 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800131}
132
133DEF_TEST(SpecialImage_Raster, reporter) {
134 SkBitmap bm = create_bm();
135
robertphillips37bd7c32016-03-17 14:31:39 -0700136 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700137 SkIRect::MakeWH(kFullSize, kFullSize),
138 bm));
139
robertphillipsb6c65e92016-02-04 10:52:42 -0800140 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
141
robertphillipsc5035e72016-03-17 06:58:39 -0700142 {
robertphillips3e302272016-04-20 11:48:36 -0700143 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700144 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700145 }
146
147 {
robertphillips37bd7c32016-03-17 14:31:39 -0700148 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700149 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700150 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800151}
152
153DEF_TEST(SpecialImage_Image, reporter) {
154 SkBitmap bm = create_bm();
155
reed9ce9d672016-03-17 10:51:11 -0700156 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800157
robertphillips37bd7c32016-03-17 14:31:39 -0700158 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700159 SkIRect::MakeWH(kFullSize, kFullSize),
robertphillips37bd7c32016-03-17 14:31:39 -0700160 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700161
robertphillipsb6c65e92016-02-04 10:52:42 -0800162 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
163
robertphillipsc5035e72016-03-17 06:58:39 -0700164 {
robertphillips3e302272016-04-20 11:48:36 -0700165 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage));
robertphillips0533d6e2016-04-26 14:33:36 -0700166 test_image(subSImg1, reporter, nullptr, false, kPad, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700167 }
168
169 {
robertphillips37bd7c32016-03-17 14:31:39 -0700170 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips0533d6e2016-04-26 14:33:36 -0700171 test_image(subSImg2, reporter, nullptr, false, kPad, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700172 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800173}
174
robertphillipsc5035e72016-03-17 06:58:39 -0700175DEF_TEST(SpecialImage_Pixmap, reporter) {
176 SkAutoPixmapStorage pixmap;
177
178 const SkImageInfo info = SkImageInfo::MakeN32(kFullSize, kFullSize, kOpaque_SkAlphaType);
179 pixmap.alloc(info);
180 pixmap.erase(SK_ColorGREEN);
181
182 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
183
184 pixmap.erase(SK_ColorRED, subset);
185
186 {
robertphillips3e302272016-04-20 11:48:36 -0700187 sk_sp<SkSpecialImage> img(SkSpecialImage::MakeFromPixmap(subset, pixmap,
robertphillips37bd7c32016-03-17 14:31:39 -0700188 nullptr, nullptr));
robertphillips64612512016-04-08 12:10:42 -0700189 test_image(img, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700190 }
robertphillipsc5035e72016-03-17 06:58:39 -0700191}
192
193
robertphillipsb6c65e92016-02-04 10:52:42 -0800194#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700195
196static void test_texture_backed(skiatest::Reporter* reporter,
197 const sk_sp<SkSpecialImage>& orig,
198 const sk_sp<SkSpecialImage>& gpuBacked) {
199 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700200 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700201 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
202 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
203 gpuBacked->subset().height() == orig->subset().height());
204}
205
206// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700207DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700208 GrContext* context = ctxInfo.fGrContext;
robertphillips83c17fa2016-03-18 08:14:27 -0700209 SkBitmap bm = create_bm();
210
211 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
212
213 {
214 // raster
215 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700216 SkIRect::MakeWH(kFullSize,
217 kFullSize),
218 bm));
219
220 {
robertphillips3e302272016-04-20 11:48:36 -0700221 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700222 test_texture_backed(reporter, rasterImage, fromRaster);
223 }
224
225 {
226 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
227
robertphillips3e302272016-04-20 11:48:36 -0700228 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700229 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
robertphillipsc91fd342016-04-25 12:32:54 -0700241 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
242 SkBudgeted::kNo,
243 bm.getPixels(),
244 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700245 if (!texture) {
246 return;
247 }
248
249 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700250 SkIRect::MakeWH(kFullSize,
251 kFullSize),
252 kNeedNewImageUniqueID_SpecialImage,
robertphillipsc91fd342016-04-25 12:32:54 -0700253 std::move(texture)));
robertphillips83c17fa2016-03-18 08:14:27 -0700254
255 {
robertphillips3e302272016-04-20 11:48:36 -0700256 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700257 test_texture_backed(reporter, gpuImage, fromGPU);
258 }
259
260 {
261 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
262
robertphillips3e302272016-04-20 11:48:36 -0700263 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700264 test_texture_backed(reporter, subGPUImage, fromSubGPU);
265 }
266 }
267}
268
bsalomon68d91342016-04-12 09:59:58 -0700269DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomonf2f1c172016-04-05 12:59:06 -0700270 GrContext* context = ctxInfo.fGrContext;
robertphillipsb6c65e92016-02-04 10:52:42 -0800271 SkBitmap bm = create_bm();
272
273 GrSurfaceDesc desc;
274 desc.fConfig = kSkia8888_GrPixelConfig;
275 desc.fFlags = kNone_GrSurfaceFlags;
276 desc.fWidth = kFullSize;
277 desc.fHeight = kFullSize;
278
robertphillipsc91fd342016-04-25 12:32:54 -0700279 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
280 SkBudgeted::kNo,
281 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800282 if (!texture) {
283 return;
284 }
285
robertphillips37bd7c32016-03-17 14:31:39 -0700286 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700287 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(
robertphillips3e302272016-04-20 11:48:36 -0700295 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700296 kNeedNewImageUniqueID_SpecialImage,
297 texture));
robertphillips0533d6e2016-04-26 14:33:36 -0700298 test_image(subSImg1, reporter, context, true, kPad, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700299 }
300
301 {
robertphillips37bd7c32016-03-17 14:31:39 -0700302 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillips0533d6e2016-04-26 14:33:36 -0700303 test_image(subSImg2, reporter, context, true, kPad, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700304 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800305}
306
307#endif