• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkFont.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkSurfaceProps.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypeface.h"
28 #include "include/core/SkTypes.h"
29 #include "include/effects/SkGradientShader.h"
30 #include "include/gpu/GrDirectContext.h"
31 #include "include/gpu/GrRecordingContext.h"
32 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
33 #if defined(SK_GRAPHITE)
34 #include "include/gpu/graphite/Surface.h"
35 #endif
36 #include "include/utils/SkTextUtils.h"
37 #include "tools/ToolUtils.h"
38 #include "tools/fonts/FontToolUtils.h"
39 #include "tools/gpu/BackendSurfaceFactory.h"
40 
41 #define W 800
42 #define H 100
43 
make_shader()44 static sk_sp<SkShader> make_shader() {
45     int a = 0x99;
46     int b = 0xBB;
47     SkPoint pts[] = { { 0, 0 }, { W, H } };
48     SkColor colors[] = { SkColorSetRGB(a, a, a), SkColorSetRGB(b, b, b) };
49     return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
50 }
51 
make_surface(GrRecordingContext * ctx,skgpu::graphite::Recorder * recorder,const SkImageInfo & info,uint32_t flags,SkPixelGeometry geo,SkScalar contrast,SkScalar gamma)52 static sk_sp<SkSurface> make_surface(GrRecordingContext* ctx,
53                                      skgpu::graphite::Recorder* recorder,
54                                      const SkImageInfo& info,
55                                      uint32_t flags,
56                                      SkPixelGeometry geo,
57                                      SkScalar contrast,
58                                      SkScalar gamma) {
59     SkSurfaceProps props(flags, geo, contrast, gamma);
60 #if defined(SK_GRAPHITE)
61     if (recorder) {
62             return SkSurfaces::RenderTarget(recorder, info, skgpu::Mipmapped::kNo, &props);
63     } else
64 #endif
65     if (ctx) {
66         return SkSurfaces::RenderTarget(ctx, skgpu::Budgeted::kNo, info, 0, &props);
67     } else {
68         return SkSurfaces::Raster(info, &props);
69     }
70 }
71 
test_draw(SkCanvas * canvas,const char label[])72 static void test_draw(SkCanvas* canvas, const char label[]) {
73     SkPaint paint;
74 
75     paint.setAntiAlias(true);
76     paint.setDither(true);
77 
78     paint.setShader(make_shader());
79     canvas->drawRect(SkRect::MakeWH(W, H), paint);
80     paint.setShader(nullptr);
81 
82     paint.setColor(SK_ColorWHITE);
83     SkFont font(ToolUtils::DefaultPortableTypeface(), 32);
84     font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
85     SkTextUtils::DrawString(canvas, label, W / 2, H * 3 / 4, font, paint,
86                             SkTextUtils::kCenter_Align);
87 }
88 
89 class SurfacePropsGM : public skiagm::GM {
90 public:
SurfacePropsGM(uint32_t flags)91     SurfacePropsGM(uint32_t flags) : fFlags(flags) {
92         recs = {
93                 {kUnknown_SkPixelGeometry,
94                  "Unknown geometry, default contrast/gamma",
95                  SK_GAMMA_CONTRAST,
96                  SK_GAMMA_EXPONENT},
97                 {kRGB_H_SkPixelGeometry,
98                  "RGB_H, default contrast/gamma",
99                  SK_GAMMA_CONTRAST,
100                  SK_GAMMA_EXPONENT},
101                 {kBGR_H_SkPixelGeometry,
102                  "BGR_H, default contrast/gamma",
103                  SK_GAMMA_CONTRAST,
104                  SK_GAMMA_EXPONENT},
105                 {kRGB_V_SkPixelGeometry,
106                  "RGB_V, default contrast/gamma",
107                  SK_GAMMA_CONTRAST,
108                  SK_GAMMA_EXPONENT},
109                 {kBGR_V_SkPixelGeometry,
110                  "BGR_V, default contrast/gamma",
111                  SK_GAMMA_CONTRAST,
112                  SK_GAMMA_EXPONENT},
113                 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 0", 0, 0},
114                 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 0", 1, 0},
115                 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 0 gamma: 3.9", 0, 3.9f},
116                 {kRGB_H_SkPixelGeometry, "RGB_H contrast : 1 gamma: 3.9", 1, 3.9f},
117         };
118     }
119 
120 protected:
getName() const121     SkString getName() const override {
122         return SkStringPrintf("surfaceprops%s",
123                               fFlags != 0 ? "_df" : "");
124     }
125 
getISize()126     SkISize getISize() override { return SkISize::Make(W, H * recs.size()); }
127 
onDraw(SkCanvas * canvas)128     void onDraw(SkCanvas* canvas) override {
129         auto ctx = canvas->recordingContext();
130         auto recorder = canvas->recorder();
131 
132         // must be opaque to have a hope of testing LCD text
133         const SkImageInfo info = SkImageInfo::MakeN32(W, H, kOpaque_SkAlphaType);
134 
135         SkScalar x = 0;
136         SkScalar y = 0;
137         for (const auto& rec : recs) {
138             auto surface(make_surface(ctx, recorder, info, fFlags, rec.fGeo, rec.fContrast,
139                                       rec.fGamma));
140             if (!surface) {
141                 SkDebugf("failed to create surface! label: %s", rec.fLabel);
142                 continue;
143             }
144             test_draw(surface->getCanvas(), rec.fLabel);
145             surface->draw(canvas, x, y);
146             y += H;
147         }
148     }
149 
150 private:
151     struct SurfacePropsInput {
152         SkPixelGeometry fGeo;
153         const char*     fLabel;
154         SkScalar fContrast;
155         SkScalar fGamma;
156     };
157     std::vector<SurfacePropsInput> recs;
158 
159     uint32_t fFlags;
160 
161     using INHERITED = GM;
162 };
163 DEF_GM( return new SurfacePropsGM(0); )
DEF_GM(return new SurfacePropsGM (SkSurfaceProps::kUseDeviceIndependentFonts_Flag);)164 DEF_GM( return new SurfacePropsGM(SkSurfaceProps::kUseDeviceIndependentFonts_Flag); )
165 
166 #ifdef SK_DEBUG
167 static bool equal(const SkSurfaceProps& a, const SkSurfaceProps& b) {
168     return a == b;
169 }
170 #endif
171 
172 class NewSurfaceGM : public skiagm::GM {
173 public:
NewSurfaceGM()174     NewSurfaceGM() {}
175 
176 protected:
getName() const177     SkString getName() const override { return SkString("surfacenew"); }
178 
getISize()179     SkISize getISize() override { return SkISize::Make(300, 140); }
180 
drawInto(SkCanvas * canvas)181     static void drawInto(SkCanvas* canvas) {
182         canvas->drawColor(SK_ColorRED);
183     }
184 
onDraw(SkCanvas * canvas)185     void onDraw(SkCanvas* canvas) override {
186         SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
187 
188         auto surf(ToolUtils::makeSurface(canvas, info, nullptr));
189         drawInto(surf->getCanvas());
190 
191         sk_sp<SkImage> image(surf->makeImageSnapshot());
192         canvas->drawImage(image, 10, 10);
193 
194         auto surf2(surf->makeSurface(info));
195         drawInto(surf2->getCanvas());
196 
197         // Assert that the props were communicated transitively through the first image
198         SkASSERT(equal(surf->props(), surf2->props()));
199 
200         sk_sp<SkImage> image2(surf2->makeImageSnapshot());
201         canvas->drawImage(image2.get(), 10 + SkIntToScalar(image->width()) + 10, 10);
202     }
203 
204 private:
205     using INHERITED = GM;
206 };
207 DEF_GM( return new NewSurfaceGM )
208 
209 ///////////////////////////////////////////////////////////////////////////////////////////////////
210 
211 // The GPU backend may behave differently when images are snapped from wrapped textures and
212 // render targets compared.
213 namespace {
214 enum SurfaceType {
215     kManaged,
216     kBackendTexture,
217     kBackendRenderTarget
218 };
219 }
220 
make_surface(const SkImageInfo & ii,SkCanvas * canvas,SurfaceType type)221 static sk_sp<SkSurface> make_surface(const SkImageInfo& ii, SkCanvas* canvas, SurfaceType type) {
222     GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());
223     switch (type) {
224         case kManaged:
225             return ToolUtils::makeSurface(canvas, ii);
226         case kBackendTexture:
227             if (!direct) {
228                 return nullptr;
229             }
230             return sk_gpu_test::MakeBackendTextureSurface(direct, ii, kTopLeft_GrSurfaceOrigin, 1);
231         case kBackendRenderTarget:
232             return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
233                                                                ii,
234                                                                kTopLeft_GrSurfaceOrigin,
235                                                                1);
236     }
237     return nullptr;
238 }
239 
240 using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
241 
242 #define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H)            \
243     DEF_SIMPLE_GM(name, canvas, W, H) {                             \
244         auto make = [canvas](const SkImageInfo& ii) {               \
245             return make_surface(ii, canvas, SurfaceType::kManaged); \
246         };                                                          \
247         main(canvas, MakeSurfaceFn(make));                          \
248     }
249 
250 #define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H)                                \
251     DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) {                                       \
252         GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());                \
253         if (!direct || direct->abandoned()) {                                                   \
254             *err_msg = "Requires non-abandoned GrDirectContext";                                \
255             return skiagm::DrawResult::kSkip;                                                   \
256         }                                                                                       \
257         auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
258         main(canvas, MakeSurfaceFn(make));                                                      \
259         return skiagm::DrawResult::kOk;                                                         \
260     }
261 
262 #define DEF_BET_SURFACE_TEST(name, canvas, main, W, H)                  \
263     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
264                              SurfaceType::kBackendTexture, W, H)
265 
266 #define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H)                  \
267     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
268                              SurfaceType::kBackendRenderTarget, W, H)
269 
270 // This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
271 // render target.
272 #define DEF_SURFACE_TESTS(name, canvas, W, H)                                  \
273     static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
274     DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
275     DEF_BET_SURFACE_TEST  (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
276     DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
277     static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
278 
279 DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
280     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
281     sk_sp<SkSurface> surf = make(info);
282 
283     surf->getCanvas()->clear(SK_ColorRED);
284     // its important that image survives longer than the next draw, so the surface will see
285     // an outstanding image, and have to decide if it should retain or discard those pixels
286     sk_sp<SkImage> image = surf->makeImageSnapshot();
287 
288     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
289     // it should not (we need the previous red pixels).
290     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
291     surf->getCanvas()->clear(SK_ColorBLUE);
292 
293     // expect to see two rects: blue | red
294     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
295 }
296 
297 // Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
298 DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
299     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
300     sk_sp<SkSurface> surf = make(info);
301 
302     surf->getCanvas()->clear(SK_ColorBLUE);
303     // its important that image survives longer than the next draw, so the surface will see
304     // an outstanding image, and have to decide if it should retain or discard those pixels
305     sk_sp<SkImage> image = surf->makeImageSnapshot();
306 
307     surf->getCanvas()->clear(SK_ColorRED);
308     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
309     // it should not (we need the previous red pixels).
310     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
311     surf->getCanvas()->drawImage(image, 0, 0);
312 
313     // expect to see two rects: blue | red
314     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
315 }
316 
317 DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
318     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
319     sk_sp<SkSurface> surf = make(info);
320 
321     surf->getCanvas()->clear(SK_ColorRED);
322     sk_sp<SkImage> image = surf->makeImageSnapshot();
323     // expect to see just red
324     canvas->drawImage(std::move(image), 0, 0);
325 }
326 
327 // Like simple_snap_image but the surface dies before the image.
328 DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
329     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
330     sk_sp<SkSurface> surf = make(info);
331 
332     surf->getCanvas()->clear(SK_ColorRED);
333     sk_sp<SkImage> image = surf->makeImageSnapshot();
334     surf.reset();
335     // expect to see just red
336     canvas->drawImage(std::move(image), 0, 0);
337 }
338 
339 DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
340     auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
341                       ? kRGBA_8888_SkColorType
342                       : canvas->imageInfo().colorType();
343     auto ii = SkImageInfo::Make({32, 32},
344                                 ct,
345                                 kPremul_SkAlphaType,
346                                 canvas->imageInfo().refColorSpace());
347     auto surface = SkSurfaces::Raster(ii);
348 
__anon88b14e790202(SkColor color) 349     auto nextImage = [&](SkColor color) {
350         surface->getCanvas()->clear(color);
351         SkPaint paint;
352         paint.setColor(~color | 0xFF000000);
353         surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
354                                                         surface->height()*2/5.f,
355                                                         surface->width() *3/5.f,
356                                                         surface->height()*3/5.f),
357                                     paint);
358         return surface->makeImageSnapshot()->withDefaultMipmaps();
359     };
360 
361     static constexpr int kPad = 8;
362     static const SkSamplingOptions kSampling{SkFilterMode::kLinear, SkMipmapMode::kLinear};
363 
364     canvas->save();
365     for (int y = 0; y < 3; ++y) {
366         canvas->save();
367         SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
368         for (int x = 0; x < 2; ++x) {
369             auto image = nextImage(kColors[x]);
370             canvas->drawImage(image, 0, 0, kSampling);
371             canvas->translate(ii.width() + kPad, 0);
372         }
373         canvas->restore();
374         canvas->translate(0, ii.width() + kPad);
375         canvas->scale(.4f, .4f);
376     }
377     canvas->restore();
378 }
379 
380 DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
381     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
382     sk_sp<SkSurface> surf = make(info);
383     surf->getCanvas()->clear(SK_ColorRED);
384     // its important that image survives longer than the next draw, so the surface will see
385     // an outstanding image, and have to decide if it should retain or discard those pixels
386     sk_sp<SkImage> image = surf->makeImageSnapshot();
387 
388     // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
389     // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
390     // with a non-opaque paint.
391     SkPaint paint;
392     paint.setAlphaf(0.25f);
393     surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
394     surf->getCanvas()->clear(SK_ColorBLUE);
395     surf->getCanvas()->restore();
396 
397     // expect to see two rects: blue blended on red
398     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
399 }
400 
401 DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
402     SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
403     auto surf = make(info);
404 
405     const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
406 
407     // noisy background
408     {
409         SkPoint pts[] = {{0, 0}, {40, 50}};
410         SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
411         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
412         SkPaint paint;
413         paint.setShader(sh);
414         surf->getCanvas()->drawPaint(paint);
415     }
416 
417     // save away the right-hand strip, then clear it
418     sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
419     {
420         SkPaint paint;
421         paint.setBlendMode(SkBlendMode::kClear);
422         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
423     }
424 
425     // draw the "foreground"
426     {
427         SkPaint paint;
428         paint.setColor(SK_ColorGREEN);
429         SkRect r = { 0, 10, 256, 35 };
430         while (r.fBottom < 256) {
431             surf->getCanvas()->drawRect(r, paint);
432             r.offset(0, r.height() * 2);
433         }
434     }
435 
436     // apply the "fade"
437     {
438         SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
439         SkColor colors[] = {0xFF000000, 0};
440         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
441         SkPaint paint;
442         paint.setShader(sh);
443         paint.setBlendMode(SkBlendMode::kDstIn);
444         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
445     }
446 
447     // restore the original strip, drawing it "under" the current foreground
448     {
449         SkPaint paint;
450         paint.setBlendMode(SkBlendMode::kDstOver);
451         surf->getCanvas()->drawImage(saveImg,
452                                      SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
453                                      SkSamplingOptions(), &paint);
454     }
455 
456     // show it on screen
457    surf->draw(canvas, 0, 0);
458 }
459