blob: e992177430c6a01f50b59a4a65f6bd5d52cc7279 [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));
robertphillipsed086ca2016-04-26 15:02:25 -070078 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());
84 }
robertphillipsb6c65e92016-02-04 10:52:42 -080085
86 //--------------
robertphillipsb4bd11e2016-03-21 13:44:18 -070087 // Test that draw restricts itself to the subset
brianosmaneed6b0e2016-09-23 13:04:05 -070088 SkImageFilter::OutputProperties outProps(img->getColorSpace());
89 sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
90 kOpaque_SkAlphaType));
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 {
brianosmaneed6b0e2016-09-23 13:04:05 -0700125 SkImageFilter::OutputProperties outProps(img->getColorSpace());
126 sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700127
128 REPORTER_ASSERT(reporter, tightSurf->width() == subset.width());
129 REPORTER_ASSERT(reporter, tightSurf->height() == subset.height());
130 REPORTER_ASSERT(reporter, peekTextureSucceeds ==
131 !!tightSurf->getTextureHandle(SkSurface::kDiscardWrite_BackendHandleAccess));
132 SkPixmap tmpPixmap;
robertphillips64612512016-04-08 12:10:42 -0700133 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap));
robertphillipsb4bd11e2016-03-21 13:44:18 -0700134 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800135}
136
137DEF_TEST(SpecialImage_Raster, reporter) {
138 SkBitmap bm = create_bm();
139
robertphillips37bd7c32016-03-17 14:31:39 -0700140 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
robertphillipsc5035e72016-03-17 06:58:39 -0700141 SkIRect::MakeWH(kFullSize, kFullSize),
142 bm));
143
robertphillipsb6c65e92016-02-04 10:52:42 -0800144 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
145
robertphillipsc5035e72016-03-17 06:58:39 -0700146 {
robertphillips3e302272016-04-20 11:48:36 -0700147 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
robertphillips64612512016-04-08 12:10:42 -0700148 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700149 }
150
151 {
robertphillips37bd7c32016-03-17 14:31:39 -0700152 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillips64612512016-04-08 12:10:42 -0700153 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700154 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800155}
156
157DEF_TEST(SpecialImage_Image, reporter) {
158 SkBitmap bm = create_bm();
159
reed9ce9d672016-03-17 10:51:11 -0700160 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
robertphillipsb6c65e92016-02-04 10:52:42 -0800161
robertphillips37bd7c32016-03-17 14:31:39 -0700162 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
robertphillipsc5035e72016-03-17 06:58:39 -0700163 SkIRect::MakeWH(kFullSize, kFullSize),
robertphillips37bd7c32016-03-17 14:31:39 -0700164 fullImage));
robertphillipsc5035e72016-03-17 06:58:39 -0700165
robertphillipsb6c65e92016-02-04 10:52:42 -0800166 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
167
robertphillipsc5035e72016-03-17 06:58:39 -0700168 {
robertphillips3e302272016-04-20 11:48:36 -0700169 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage));
robertphillipsed086ca2016-04-26 15:02:25 -0700170 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700171 }
172
173 {
robertphillips37bd7c32016-03-17 14:31:39 -0700174 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700175 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700176 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800177}
178
179#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700180
181static void test_texture_backed(skiatest::Reporter* reporter,
182 const sk_sp<SkSpecialImage>& orig,
183 const sk_sp<SkSpecialImage>& gpuBacked) {
184 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700185 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700186 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
187 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
188 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700189 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700190}
191
192// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700193DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700194 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700195 SkBitmap bm = create_bm();
196
197 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
198
199 {
200 // raster
201 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700202 SkIRect::MakeWH(kFullSize,
203 kFullSize),
204 bm));
205
206 {
robertphillips3e302272016-04-20 11:48:36 -0700207 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700208 test_texture_backed(reporter, rasterImage, fromRaster);
209 }
210
211 {
212 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
213
robertphillips3e302272016-04-20 11:48:36 -0700214 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700215 test_texture_backed(reporter, subRasterImage, fromSubRaster);
216 }
217 }
218
219 {
220 // gpu
221 GrSurfaceDesc desc;
222 desc.fConfig = kSkia8888_GrPixelConfig;
223 desc.fFlags = kNone_GrSurfaceFlags;
224 desc.fWidth = kFullSize;
225 desc.fHeight = kFullSize;
226
robertphillipsc91fd342016-04-25 12:32:54 -0700227 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
228 SkBudgeted::kNo,
229 bm.getPixels(),
230 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700231 if (!texture) {
232 return;
233 }
234
235 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700236 SkIRect::MakeWH(kFullSize,
237 kFullSize),
238 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700239 std::move(texture), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700240
241 {
robertphillips3e302272016-04-20 11:48:36 -0700242 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700243 test_texture_backed(reporter, gpuImage, fromGPU);
244 }
245
246 {
247 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
248
robertphillips3e302272016-04-20 11:48:36 -0700249 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700250 test_texture_backed(reporter, subGPUImage, fromSubGPU);
251 }
252 }
253}
254
bsalomon68d91342016-04-12 09:59:58 -0700255DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700256 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800257 SkBitmap bm = create_bm();
258
259 GrSurfaceDesc desc;
260 desc.fConfig = kSkia8888_GrPixelConfig;
261 desc.fFlags = kNone_GrSurfaceFlags;
262 desc.fWidth = kFullSize;
263 desc.fHeight = kFullSize;
264
robertphillipsc91fd342016-04-25 12:32:54 -0700265 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
266 SkBudgeted::kNo,
267 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800268 if (!texture) {
269 return;
270 }
271
robertphillips37bd7c32016-03-17 14:31:39 -0700272 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700273 SkIRect::MakeWH(kFullSize, kFullSize),
274 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700275 texture, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700276
robertphillipsb6c65e92016-02-04 10:52:42 -0800277 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
278
robertphillipsc5035e72016-03-17 06:58:39 -0700279 {
robertphillips37bd7c32016-03-17 14:31:39 -0700280 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700281 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700282 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700283 texture, nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700284 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700285 }
286
287 {
robertphillips37bd7c32016-03-17 14:31:39 -0700288 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700289 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700290 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800291}
292
293#endif