xref: /aosp_15_r20/external/skia/gm/surface.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ganesh/GrDirectContext.h"
31 #include "include/gpu/ganesh/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 #if defined(SK_GRAPHITE)
224     skgpu::graphite::Recorder* recorder = canvas->recorder();
225 #endif
226     switch (type) {
227         case kManaged:
228             return ToolUtils::makeSurface(canvas, ii);
229         case kBackendTexture:
230 #if defined(SK_GRAPHITE)
231             if (recorder) {
232                 return sk_gpu_test::MakeBackendTextureSurface(recorder, ii);
233             }
234 #endif
235             if (!direct) {
236                 return nullptr;
237             }
238             return sk_gpu_test::MakeBackendTextureSurface(direct, ii, kTopLeft_GrSurfaceOrigin, 1);
239         case kBackendRenderTarget:
240 #if defined(SK_GRAPHITE)
241             if (recorder) {
242                 return SkSurfaces::RenderTarget(recorder,
243                                                 ii,
244                                                 skgpu::Mipmapped::kNo,
245                                                 /*surfaceProps=*/nullptr);
246             }
247 #endif
248             return sk_gpu_test::MakeBackendRenderTargetSurface(direct,
249                                                                ii,
250                                                                kTopLeft_GrSurfaceOrigin,
251                                                                1);
252     }
253     return nullptr;
254 }
255 
256 using MakeSurfaceFn = std::function<sk_sp<SkSurface>(const SkImageInfo&)>;
257 
258 #define DEF_BASIC_SURFACE_TEST(name, canvas, main, W, H)            \
259     DEF_SIMPLE_GM(name, canvas, W, H) {                             \
260         auto make = [canvas](const SkImageInfo& ii) {               \
261             return make_surface(ii, canvas, SurfaceType::kManaged); \
262         };                                                          \
263         main(canvas, MakeSurfaceFn(make));                          \
264     }
265 
266 #define DEF_BACKEND_SURFACE_TEST(name, canvas, main, type, W, H)                                \
267     DEF_SIMPLE_GM_CAN_FAIL(name, canvas, err_msg, W, H) {                                       \
268         GrDirectContext* direct = GrAsDirectContext(canvas->recordingContext());                \
269         skgpu::graphite::Recorder* recorder = canvas->recorder();                               \
270         if ((!direct || direct->abandoned()) && !recorder) {                                    \
271             *err_msg = "Requires non-abandoned GrDirectContext or Recorder";                    \
272             return skiagm::DrawResult::kSkip;                                                   \
273         }                                                                                       \
274         auto make = [canvas](const SkImageInfo& ii) { return make_surface(ii, canvas, type); }; \
275         main(canvas, MakeSurfaceFn(make));                                                      \
276         return skiagm::DrawResult::kOk;                                                         \
277     }
278 
279 #define DEF_BET_SURFACE_TEST(name, canvas, main, W, H)                  \
280     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bet), canvas, main, \
281                              SurfaceType::kBackendTexture, W, H)
282 
283 #define DEF_BERT_SURFACE_TEST(name, canvas, main, W, H)                  \
284     DEF_BACKEND_SURFACE_TEST(SK_MACRO_CONCAT(name, _bert), canvas, main, \
285                              SurfaceType::kBackendRenderTarget, W, H)
286 
287 // This makes 3 GMs from the same code, normal, wrapped backend texture, and wrapped backend
288 // render target.
289 #define DEF_SURFACE_TESTS(name, canvas, W, H)                                  \
290     static void SK_MACRO_CONCAT(name, _main)(SkCanvas*, const MakeSurfaceFn&); \
291     DEF_BASIC_SURFACE_TEST(name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
292     DEF_BET_SURFACE_TEST  (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
293     DEF_BERT_SURFACE_TEST (name, canvas, SK_MACRO_CONCAT(name, _main), W, H)   \
294     static void SK_MACRO_CONCAT(name, _main)(SkCanvas * canvas, const MakeSurfaceFn& make)
295 
296 DEF_SURFACE_TESTS(copy_on_write_retain, canvas, 256, 256) {
297     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
298     sk_sp<SkSurface> surf = make(info);
299 
300     surf->getCanvas()->clear(SK_ColorRED);
301     // its important that image survives longer than the next draw, so the surface will see
302     // an outstanding image, and have to decide if it should retain or discard those pixels
303     sk_sp<SkImage> image = surf->makeImageSnapshot();
304 
305     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
306     // it should not (we need the previous red pixels).
307     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
308     surf->getCanvas()->clear(SK_ColorBLUE);
309 
310     // expect to see two rects: blue | red
311     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
312 }
313 
314 // Like copy_on_write_retain but draws the snapped image back to the surface it was snapped from.
315 DEF_SURFACE_TESTS(copy_on_write_retain2, canvas, 256, 256) {
316     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
317     sk_sp<SkSurface> surf = make(info);
318 
319     surf->getCanvas()->clear(SK_ColorBLUE);
320     // its important that image survives longer than the next draw, so the surface will see
321     // an outstanding image, and have to decide if it should retain or discard those pixels
322     sk_sp<SkImage> image = surf->makeImageSnapshot();
323 
324     surf->getCanvas()->clear(SK_ColorRED);
325     // normally a clear+opaque should trigger the discard optimization, but since we have a clip
326     // it should not (we need the previous red pixels).
327     surf->getCanvas()->clipRect(SkRect::MakeWH(128, 256));
328     surf->getCanvas()->drawImage(image, 0, 0);
329 
330     // expect to see two rects: blue | red
331     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
332 }
333 
334 DEF_SURFACE_TESTS(simple_snap_image, canvas, 256, 256) {
335     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
336     sk_sp<SkSurface> surf = make(info);
337 
338     surf->getCanvas()->clear(SK_ColorRED);
339     sk_sp<SkImage> image = surf->makeImageSnapshot();
340     // expect to see just red
341     canvas->drawImage(std::move(image), 0, 0);
342 }
343 
344 // Like simple_snap_image but the surface dies before the image.
345 DEF_SURFACE_TESTS(simple_snap_image2, canvas, 256, 256) {
346     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
347     sk_sp<SkSurface> surf = make(info);
348 
349     surf->getCanvas()->clear(SK_ColorRED);
350     sk_sp<SkImage> image = surf->makeImageSnapshot();
351     surf.reset();
352     // expect to see just red
353     canvas->drawImage(std::move(image), 0, 0);
354 }
355 
356 DEF_SIMPLE_GM(snap_with_mips, canvas, 80, 75) {
357     auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
358                       ? kRGBA_8888_SkColorType
359                       : canvas->imageInfo().colorType();
360     auto ii = SkImageInfo::Make({32, 32},
361                                 ct,
362                                 kPremul_SkAlphaType,
363                                 canvas->imageInfo().refColorSpace());
364     auto surface = SkSurfaces::Raster(ii);
365 
__anon79eaac1b0202(SkColor color) 366     auto nextImage = [&](SkColor color) {
367         surface->getCanvas()->clear(color);
368         SkPaint paint;
369         paint.setColor(~color | 0xFF000000);
370         surface->getCanvas()->drawRect(SkRect::MakeLTRB(surface->width() *2/5.f,
371                                                         surface->height()*2/5.f,
372                                                         surface->width() *3/5.f,
373                                                         surface->height()*3/5.f),
374                                     paint);
375         return surface->makeImageSnapshot()->withDefaultMipmaps();
376     };
377 
378     static constexpr int kPad = 8;
379     static const SkSamplingOptions kSampling{SkFilterMode::kLinear, SkMipmapMode::kLinear};
380 
381     canvas->save();
382     for (int y = 0; y < 3; ++y) {
383         canvas->save();
384         SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE};
385         for (int x = 0; x < 2; ++x) {
386             auto image = nextImage(kColors[x]);
387             canvas->drawImage(image, 0, 0, kSampling);
388             canvas->translate(ii.width() + kPad, 0);
389         }
390         canvas->restore();
391         canvas->translate(0, ii.width() + kPad);
392         canvas->scale(.4f, .4f);
393     }
394     canvas->restore();
395 }
396 
397 DEF_SURFACE_TESTS(copy_on_write_savelayer, canvas, 256, 256) {
398     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
399     sk_sp<SkSurface> surf = make(info);
400     surf->getCanvas()->clear(SK_ColorRED);
401     // its important that image survives longer than the next draw, so the surface will see
402     // an outstanding image, and have to decide if it should retain or discard those pixels
403     sk_sp<SkImage> image = surf->makeImageSnapshot();
404 
405     // now draw into a full-screen layer. This should (a) trigger a copy-on-write, but it should
406     // not trigger discard, even tho its alpha (SK_ColorBLUE) is opaque, since it is in a layer
407     // with a non-opaque paint.
408     SkPaint paint;
409     paint.setAlphaf(0.25f);
410     surf->getCanvas()->saveLayer({0, 0, 256, 256}, &paint);
411     surf->getCanvas()->clear(SK_ColorBLUE);
412     surf->getCanvas()->restore();
413 
414     // expect to see two rects: blue blended on red
415     canvas->drawImage(surf->makeImageSnapshot(), 0, 0);
416 }
417 
418 DEF_SURFACE_TESTS(surface_underdraw, canvas, 256, 256) {
419     SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256, nullptr);
420     auto surf = make(info);
421 
422     const SkIRect subset = SkIRect::MakeLTRB(180, 0, 256, 256);
423 
424     // noisy background
425     {
426         SkPoint pts[] = {{0, 0}, {40, 50}};
427         SkColor colors[] = {SK_ColorRED, SK_ColorBLUE};
428         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kRepeat);
429         SkPaint paint;
430         paint.setShader(sh);
431         surf->getCanvas()->drawPaint(paint);
432     }
433 
434     // save away the right-hand strip, then clear it
435     sk_sp<SkImage> saveImg = surf->makeImageSnapshot(subset);
436     {
437         SkPaint paint;
438         paint.setBlendMode(SkBlendMode::kClear);
439         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
440     }
441 
442     // draw the "foreground"
443     {
444         SkPaint paint;
445         paint.setColor(SK_ColorGREEN);
446         SkRect r = { 0, 10, 256, 35 };
447         while (r.fBottom < 256) {
448             surf->getCanvas()->drawRect(r, paint);
449             r.offset(0, r.height() * 2);
450         }
451     }
452 
453     // apply the "fade"
454     {
455         SkPoint pts[] = {{SkIntToScalar(subset.left()), 0}, {SkIntToScalar(subset.right()), 0}};
456         SkColor colors[] = {0xFF000000, 0};
457         auto sh = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp);
458         SkPaint paint;
459         paint.setShader(sh);
460         paint.setBlendMode(SkBlendMode::kDstIn);
461         surf->getCanvas()->drawRect(SkRect::Make(subset), paint);
462     }
463 
464     // restore the original strip, drawing it "under" the current foreground
465     {
466         SkPaint paint;
467         paint.setBlendMode(SkBlendMode::kDstOver);
468         surf->getCanvas()->drawImage(saveImg,
469                                      SkIntToScalar(subset.left()), SkIntToScalar(subset.top()),
470                                      SkSamplingOptions(), &paint);
471     }
472 
473     // show it on screen
474    surf->draw(canvas, 0, 0);
475 }
476