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