2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file
8 #include "SkAutoPixmapStorage.h"
13 #include "SkSpecialImage.h"
14 #include "SkSpecialSurface.h"
15 #include "SkSurface.h"
19 #include "GrContext.h"
20 #include "GrSurfaceProxy.h"
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.
29 static const int kSmallerSize = 10;
30 static const int kPad = 3;
31 static const int kFullSize = kSmallerSize + 2 * kPad;
33 // Create a bitmap with red in the center and green around it
34 static SkBitmap create_bm() {
36 bm.allocN32Pixels(kFullSize, kFullSize, true);
40 temp.clear(SK_ColorGREEN);
42 p.setColor(SK_ColorRED);
43 p.setAntiAlias(false);
45 temp.drawRect(SkRect::MakeXYWH(SkIntToScalar(kPad), SkIntToScalar(kPad),
46 SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
52 // Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
53 static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
54 GrContext* context, bool peekTextureSucceeds,
55 int offset, int size) {
56 const SkIRect subset = img->subset();
57 REPORTER_ASSERT(reporter, offset == subset.left());
58 REPORTER_ASSERT(reporter, offset == subset.top());
59 REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
60 REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
63 // Test that peekTexture reports the correct backing type
64 REPORTER_ASSERT(reporter, peekTextureSucceeds == img->isTextureBacked());
68 // Test getTextureAsRef - as long as there is a context this should succeed
70 sk_sp<GrTexture> texture(img->asTextureRef(context));
71 REPORTER_ASSERT(reporter, texture);
76 // Test getROPixels - this should always succeed regardless of backing store
78 REPORTER_ASSERT(reporter, img->getROPixels(&bitmap));
80 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.width());
81 REPORTER_ASSERT(reporter, kSmallerSize == bitmap.height());
83 REPORTER_ASSERT(reporter, size == bitmap.width());
84 REPORTER_ASSERT(reporter, size == bitmap.height());
88 // Test that draw restricts itself to the subset
89 SkImageFilter::OutputProperties outProps(img->getColorSpace());
90 sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
91 kOpaque_SkAlphaType));
93 SkCanvas* canvas = surf->getCanvas();
95 canvas->clear(SK_ColorBLUE);
96 img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
99 bm.allocN32Pixels(kFullSize, kFullSize, true);
101 bool result = canvas->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), 0, 0);
102 SkASSERT_RELEASE(result);
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,
113 // Test that makeTightSubset & makeTightSurface return appropriately sized objects
114 // of the correct backing type
115 SkIRect newSubset = SkIRect::MakeWH(subset.width(), subset.height());
117 sk_sp<SkImage> tightImg(img->makeTightSubset(newSubset));
119 REPORTER_ASSERT(reporter, tightImg->width() == subset.width());
120 REPORTER_ASSERT(reporter, tightImg->height() == subset.height());
121 REPORTER_ASSERT(reporter, peekTextureSucceeds == !!tightImg->getTexture());
123 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap));
126 SkImageFilter::OutputProperties outProps(img->getColorSpace());
127 sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
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));
134 REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap));
138 DEF_TEST(SpecialImage_Raster, reporter) {
139 SkBitmap bm = create_bm();
141 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
142 SkIRect::MakeWH(kFullSize, kFullSize),
145 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
148 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
149 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
153 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
154 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
158 static void test_specialimage_image(skiatest::Reporter* reporter, SkColorSpace* dstColorSpace) {
159 SkBitmap bm = create_bm();
161 sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
163 sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
164 SkIRect::MakeWH(kFullSize, kFullSize),
165 fullImage, dstColorSpace));
167 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
170 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage,
172 test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
176 sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
177 test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
181 DEF_TEST(SpecialImage_Image_Legacy, reporter) {
182 SkColorSpace* legacyColorSpace = nullptr;
183 test_specialimage_image(reporter, legacyColorSpace);
186 DEF_TEST(SpecialImage_Image_ColorSpaceAware, reporter) {
187 sk_sp<SkColorSpace> srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
188 test_specialimage_image(reporter, srgbColorSpace.get());
193 static void test_texture_backed(skiatest::Reporter* reporter,
194 const sk_sp<SkSpecialImage>& orig,
195 const sk_sp<SkSpecialImage>& gpuBacked) {
196 REPORTER_ASSERT(reporter, gpuBacked);
197 REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
198 REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
199 REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
200 gpuBacked->subset().height() == orig->subset().height());
201 REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
204 // Test out the SkSpecialImage::makeTextureImage entry point
205 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
206 GrContext* context = ctxInfo.grContext();
207 SkBitmap bm = create_bm();
209 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
213 sk_sp<SkSpecialImage> rasterImage(SkSpecialImage::MakeFromRaster(
214 SkIRect::MakeWH(kFullSize,
219 sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
220 test_texture_backed(reporter, rasterImage, fromRaster);
224 sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
226 sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
227 test_texture_backed(reporter, subRasterImage, fromSubRaster);
234 desc.fConfig = kSkia8888_GrPixelConfig;
235 desc.fFlags = kNone_GrSurfaceFlags;
236 desc.fWidth = kFullSize;
237 desc.fHeight = kFullSize;
239 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
247 sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
248 SkIRect::MakeWH(kFullSize,
250 kNeedNewImageUniqueID_SpecialImage,
251 std::move(texture), nullptr));
254 sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
255 test_texture_backed(reporter, gpuImage, fromGPU);
259 sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
261 sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
262 test_texture_backed(reporter, subGPUImage, fromSubGPU);
267 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
268 GrContext* context = ctxInfo.grContext();
269 SkBitmap bm = create_bm();
272 desc.fConfig = kSkia8888_GrPixelConfig;
273 desc.fFlags = kNone_GrSurfaceFlags;
274 desc.fWidth = kFullSize;
275 desc.fHeight = kFullSize;
277 sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
284 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
285 SkIRect::MakeWH(kFullSize, kFullSize),
286 kNeedNewImageUniqueID_SpecialImage,
289 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
292 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
294 kNeedNewImageUniqueID_SpecialImage,
296 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
300 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
301 test_image(subSImg2, reporter, context, true, kPad, kFullSize);
305 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_DeferredGpu, reporter, ctxInfo) {
306 GrContext* context = ctxInfo.grContext();
307 SkBitmap bm = create_bm();
310 desc.fConfig = kSkia8888_GrPixelConfig;
311 desc.fFlags = kNone_GrSurfaceFlags;
312 desc.fWidth = kFullSize;
313 desc.fHeight = kFullSize;
315 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeDeferred(*context->caps(),
316 context->textureProvider(),
317 desc, SkBudgeted::kNo,
323 sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeDeferredFromGpu(
325 SkIRect::MakeWH(kFullSize, kFullSize),
326 kNeedNewImageUniqueID_SpecialImage,
329 const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
332 sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeDeferredFromGpu(
335 kNeedNewImageUniqueID_SpecialImage,
337 test_image(subSImg1, reporter, context, true, kPad, kFullSize);
341 sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
342 test_image(subSImg2, reporter, context, true, kPad, kFullSize);