SpecialImageTest.cpp revision eed6b0e1d865a1f93143c09961debba0aca592ca
1/*
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
8#include "SkAutoPixmapStorage.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkImage.h"
12#include "SkPixmap.h"
13#include "SkSpecialImage.h"
14#include "SkSpecialSurface.h"
15#include "SkSurface.h"
16#include "Test.h"
17
18#if SK_SUPPORT_GPU
19#include "GrContext.h"
20#endif
21
22
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),
45                                   SkIntToScalar(kSmallerSize), SkIntToScalar(kSmallerSize)),
46                  p);
47
48    return bm;
49}
50
51// Basic test of the SkSpecialImage public API (e.g., peekTexture, peekPixels & draw)
52static void test_image(const sk_sp<SkSpecialImage>& img, skiatest::Reporter* reporter,
53                       GrContext* context, bool peekTextureSucceeds,
54                       int offset, int size) {
55    const SkIRect subset = img->subset();
56    REPORTER_ASSERT(reporter, offset == subset.left());
57    REPORTER_ASSERT(reporter, offset == subset.top());
58    REPORTER_ASSERT(reporter, kSmallerSize == subset.width());
59    REPORTER_ASSERT(reporter, kSmallerSize == subset.height());
60
61    //--------------
62    // Test that peekTexture reports the correct backing type
63    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
73
74    //--------------
75    // 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());
84    }
85
86    //--------------
87    // Test that draw restricts itself to the subset
88    SkImageFilter::OutputProperties outProps(img->getColorSpace());
89    sk_sp<SkSpecialSurface> surf(img->makeSurface(outProps, SkISize::Make(kFullSize, kFullSize),
90                                                  kOpaque_SkAlphaType));
91
92    SkCanvas* canvas = surf->getCanvas();
93
94    canvas->clear(SK_ColorBLUE);
95    img->draw(canvas, SkIntToScalar(kPad), SkIntToScalar(kPad), nullptr);
96
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));
110
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;
122        REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightImg->peekPixels(&tmpPixmap));
123    }
124    {
125        SkImageFilter::OutputProperties outProps(img->getColorSpace());
126        sk_sp<SkSurface> tightSurf(img->makeTightSurface(outProps, subset.size()));
127
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;
133        REPORTER_ASSERT(reporter, peekTextureSucceeds != !!tightSurf->peekPixels(&tmpPixmap));
134    }
135}
136
137DEF_TEST(SpecialImage_Raster, reporter) {
138    SkBitmap bm = create_bm();
139
140    sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromRaster(
141                                                            SkIRect::MakeWH(kFullSize, kFullSize),
142                                                            bm));
143
144    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
145
146    {
147        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromRaster(subset, bm));
148        test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
149    }
150
151    {
152        sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
153        test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
154    }
155}
156
157DEF_TEST(SpecialImage_Image, reporter) {
158    SkBitmap bm = create_bm();
159
160    sk_sp<SkImage> fullImage(SkImage::MakeFromBitmap(bm));
161
162    sk_sp<SkSpecialImage> fullSImage(SkSpecialImage::MakeFromImage(
163                                                            SkIRect::MakeWH(kFullSize, kFullSize),
164                                                            fullImage));
165
166    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
167
168    {
169        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromImage(subset, fullImage));
170        test_image(subSImg1, reporter, nullptr, false, kPad, kFullSize);
171    }
172
173    {
174        sk_sp<SkSpecialImage> subSImg2(fullSImage->makeSubset(subset));
175        test_image(subSImg2, reporter, nullptr, false, 0, kSmallerSize);
176    }
177}
178
179#if SK_SUPPORT_GPU
180
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);
185    REPORTER_ASSERT(reporter, gpuBacked->isTextureBacked());
186    REPORTER_ASSERT(reporter, gpuBacked->uniqueID() == orig->uniqueID());
187    REPORTER_ASSERT(reporter, gpuBacked->subset().width() == orig->subset().width() &&
188                              gpuBacked->subset().height() == orig->subset().height());
189    REPORTER_ASSERT(reporter, gpuBacked->getColorSpace() == orig->getColorSpace());
190}
191
192// Test out the SkSpecialImage::makeTextureImage entry point
193DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_MakeTexture, reporter, ctxInfo) {
194    GrContext* context = ctxInfo.grContext();
195    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(
202                                                                        SkIRect::MakeWH(kFullSize,
203                                                                                        kFullSize),
204                                                                        bm));
205
206        {
207            sk_sp<SkSpecialImage> fromRaster(rasterImage->makeTextureImage(context));
208            test_texture_backed(reporter, rasterImage, fromRaster);
209        }
210
211        {
212            sk_sp<SkSpecialImage> subRasterImage(rasterImage->makeSubset(subset));
213
214            sk_sp<SkSpecialImage> fromSubRaster(subRasterImage->makeTextureImage(context));
215            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
227        sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
228                                                                           SkBudgeted::kNo,
229                                                                           bm.getPixels(),
230                                                                           0));
231        if (!texture) {
232            return;
233        }
234
235        sk_sp<SkSpecialImage> gpuImage(SkSpecialImage::MakeFromGpu(
236                                                                SkIRect::MakeWH(kFullSize,
237                                                                                kFullSize),
238                                                                kNeedNewImageUniqueID_SpecialImage,
239                                                                std::move(texture), nullptr));
240
241        {
242            sk_sp<SkSpecialImage> fromGPU(gpuImage->makeTextureImage(context));
243            test_texture_backed(reporter, gpuImage, fromGPU);
244        }
245
246        {
247            sk_sp<SkSpecialImage> subGPUImage(gpuImage->makeSubset(subset));
248
249            sk_sp<SkSpecialImage> fromSubGPU(subGPUImage->makeTextureImage(context));
250            test_texture_backed(reporter, subGPUImage, fromSubGPU);
251        }
252    }
253}
254
255DEF_GPUTEST_FOR_RENDERING_CONTEXTS(SpecialImage_Gpu, reporter, ctxInfo) {
256    GrContext* context = ctxInfo.grContext();
257    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
265    sk_sp<GrTexture> texture(context->textureProvider()->createTexture(desc,
266                                                                       SkBudgeted::kNo,
267                                                                       bm.getPixels(), 0));
268    if (!texture) {
269        return;
270    }
271
272    sk_sp<SkSpecialImage> fullSImg(SkSpecialImage::MakeFromGpu(
273                                                            SkIRect::MakeWH(kFullSize, kFullSize),
274                                                            kNeedNewImageUniqueID_SpecialImage,
275                                                            texture, nullptr));
276
277    const SkIRect& subset = SkIRect::MakeXYWH(kPad, kPad, kSmallerSize, kSmallerSize);
278
279    {
280        sk_sp<SkSpecialImage> subSImg1(SkSpecialImage::MakeFromGpu(
281                                                               subset,
282                                                               kNeedNewImageUniqueID_SpecialImage,
283                                                               texture, nullptr));
284        test_image(subSImg1, reporter, context, true, kPad, kFullSize);
285    }
286
287    {
288        sk_sp<SkSpecialImage> subSImg2(fullSImg->makeSubset(subset));
289        test_image(subSImg2, reporter, context, true, kPad, kFullSize);
290    }
291}
292
293#endif
294