Mike Klein | 0cc60b8 | 2017-06-22 11:00:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkSurface.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 18 | #include "include/core/SkTileMode.h" |
Mike Klein | 0cc60b8 | 2017-06-22 11:00:17 -0700 | [diff] [blame] | 19 | |
| 20 | // This GM reproduces skia:6783, which demonstrated a bug in repeat and mirror |
| 21 | // image sampling tiling modes as implemented in software. We want to tile to |
| 22 | // [0,limit), and the old incorrect logic was: |
| 23 | // |
| 24 | // limit = ulp_before(limit) |
| 25 | // val = val - floor(val/limit)*limit (This is repeat; mirror is similar.) |
| 26 | // |
| 27 | // while the correct logic is more like: |
| 28 | // |
| 29 | // val = val - floor(val/limit)*limit |
| 30 | // val = min(val, ulp_before(limit)) |
| 31 | // |
| 32 | // You would see ugly jaggies on the blue/yellow edge near the bottom left if |
| 33 | // the bug were still present. All stripes should now look roughly the same. |
| 34 | |
| 35 | DEF_SIMPLE_GM(bug6783, canvas, 500, 500) { |
| 36 | sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100); |
| 37 | |
| 38 | SkPaint p; |
| 39 | p.setColor(SK_ColorYELLOW); |
| 40 | surface->getCanvas()->drawPaint(p); |
| 41 | p.setColor(SK_ColorBLUE); |
| 42 | surface->getCanvas()->drawRect(SkRect::MakeWH(50, 100), p); |
| 43 | |
| 44 | sk_sp<SkImage> img = surface->makeImageSnapshot(); |
| 45 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 46 | SkMatrix m = SkMatrix::Translate(25, 214) * SkMatrix::Scale(2, 2); |
Mike Klein | 0cc60b8 | 2017-06-22 11:00:17 -0700 | [diff] [blame] | 47 | m.preSkew(0.5f, 0.5f); |
| 48 | |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 49 | // The bug was present at all filter levels, but you might not notice it at nearest. |
Mike Reed | 5ec2238 | 2021-01-14 21:59:01 -0500 | [diff] [blame] | 50 | SkSamplingOptions sampling(SkFilterMode::kLinear); |
Mike Klein | 0cc60b8 | 2017-06-22 11:00:17 -0700 | [diff] [blame] | 51 | |
| 52 | // It's only important to repeat or mirror in x to show off the bug. |
Mike Reed | 99c9446 | 2020-12-08 13:16:56 -0500 | [diff] [blame] | 53 | p.setShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kClamp, sampling, m)); |
Mike Klein | 0cc60b8 | 2017-06-22 11:00:17 -0700 | [diff] [blame] | 54 | canvas->drawPaint(p); |
| 55 | } |