blob: ba0eb58f4eb1a930ce9f4ef7beddcba5297b7cb7 [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
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));
robertphillipsed086ca2016-04-26 15:02:25 -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));
robertphillipsed086ca2016-04-26 15:02:25 -0700176 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700177 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800178}
179
180#if SK_SUPPORT_GPU
robertphillips83c17fa2016-03-18 08:14:27 -0700181
182static void test_texture_backed(skiatest::Reporter* reporter,
183 const sk_sp<SkSpecialImage>& orig,
184 const sk_sp<SkSpecialImage>& gpuBacked) {
185 REPORTER_ASSERT(reporter, gpuBacked);
robertphillips64612512016-04-08 12:10:42 -0700186 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
robertphillips83c17fa2016-03-18 08:14:27 -0700187 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
188 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
189 gpuBacked->subset().height() == orig->subset().height());
brianosmanafbf71d2016-07-21 07:15:37 -0700190 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
robertphillips83c17fa2016-03-18 08:14:27 -0700191}
192
193// Test out the SkSpecialImage::makeTextureImage entry point
bsalomon68d91342016-04-12 09:59:58 -0700194DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700195 GrContext* context = ctxInfo.grContext();
robertphillips83c17fa2016-03-18 08:14:27 -0700196 SkBitmap bm = create_bm();
197
198 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
199
200 {
201 // raster
202 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
robertphillips83c17fa2016-03-18 08:14:27 -0700203 SkIRect::MakeWH(kFullSize,
204 kFullSize),
205 bm));
206
207 {
robertphillips3e302272016-04-20 11:48:36 -0700208 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700209 test_texture_backed(reporter, rasterImage, fromRaster);
210 }
211
212 {
213 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
214
robertphillips3e302272016-04-20 11:48:36 -0700215 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700216 test_texture_backed(reporter, subRasterImage, fromSubRaster);
217 }
218 }
219
220 {
221 // gpu
222 GrSurfaceDesc desc;
223 desc.fConfig = kSkia8888_GrPixelConfig;
224 desc.fFlags = kNone_GrSurfaceFlags;
225 desc.fWidth = kFullSize;
226 desc.fHeight = kFullSize;
227
robertphillipsc91fd342016-04-25 12:32:54 -0700228 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
229 SkBudgeted::kNo,
230 bm.getPixels(),
231 0));
robertphillips83c17fa2016-03-18 08:14:27 -0700232 if (!texture) {
233 return;
234 }
235
236 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
robertphillips83c17fa2016-03-18 08:14:27 -0700237 SkIRect::MakeWH(kFullSize,
238 kFullSize),
239 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700240 std::move(texture), nullptr));
robertphillips83c17fa2016-03-18 08:14:27 -0700241
242 {
robertphillips3e302272016-04-20 11:48:36 -0700243 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700244 test_texture_backed(reporter, gpuImage, fromGPU);
245 }
246
247 {
248 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
249
robertphillips3e302272016-04-20 11:48:36 -0700250 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
robertphillips83c17fa2016-03-18 08:14:27 -0700251 test_texture_backed(reporter, subGPUImage, fromSubGPU);
252 }
253 }
254}
255
bsalomon68d91342016-04-12 09:59:58 -0700256DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700257 GrContext* context = ctxInfo.grContext();
robertphillipsb6c65e92016-02-04 10:52:42 -0800258 SkBitmap bm = create_bm();
259
260 GrSurfaceDesc desc;
261 desc.fConfig = kSkia8888_GrPixelConfig;
262 desc.fFlags = kNone_GrSurfaceFlags;
263 desc.fWidth = kFullSize;
264 desc.fHeight = kFullSize;
265
robertphillipsc91fd342016-04-25 12:32:54 -0700266 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
267 SkBudgeted::kNo,
268 bm.getPixels(), 0));
robertphillipsb6c65e92016-02-04 10:52:42 -0800269 if (!texture) {
270 return;
271 }
272
robertphillips37bd7c32016-03-17 14:31:39 -0700273 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
robertphillipsc5035e72016-03-17 06:58:39 -0700274 SkIRect::MakeWH(kFullSize, kFullSize),
275 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700276 texture, nullptr));
robertphillipsc5035e72016-03-17 06:58:39 -0700277
robertphillipsb6c65e92016-02-04 10:52:42 -0800278 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
279
robertphillipsc5035e72016-03-17 06:58:39 -0700280 {
robertphillips37bd7c32016-03-17 14:31:39 -0700281 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
robertphillips3e302272016-04-20 11:48:36 -0700282 subset,
robertphillipsc5035e72016-03-17 06:58:39 -0700283 kNeedNewImageUniqueID_SpecialImage,
brianosmanafbf71d2016-07-21 07:15:37 -0700284 texture, nullptr));
robertphillipsed086ca2016-04-26 15:02:25 -0700285 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700286 }
287
288 {
robertphillips37bd7c32016-03-17 14:31:39 -0700289 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
robertphillipsed086ca2016-04-26 15:02:25 -0700290 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
robertphillipsc5035e72016-03-17 06:58:39 -0700291 }
robertphillipsb6c65e92016-02-04 10:52:42 -0800292}
293
294#endif