blob: 4f52e0914af60d78b8f70a5ff4b8cf25922732c3 [file] [log] [blame]
Mike Klein0cc60b82017-06-22 11:00:17 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkFilterQuality.h"
12#include "include/core/SkImage.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "include/core/SkSurface.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040019#include "include/core/SkTileMode.h"
Mike Klein0cc60b82017-06-22 11:00:17 -070020
21// This GM reproduces skia:6783, which demonstrated a bug in repeat and mirror
22// image sampling tiling modes as implemented in software. We want to tile to
23// [0,limit), and the old incorrect logic was:
24//
25// limit = ulp_before(limit)
26// val = val - floor(val/limit)*limit (This is repeat; mirror is similar.)
27//
28// while the correct logic is more like:
29//
30// val = val - floor(val/limit)*limit
31// val = min(val, ulp_before(limit))
32//
33// You would see ugly jaggies on the blue/yellow edge near the bottom left if
34// the bug were still present. All stripes should now look roughly the same.
35
36DEF_SIMPLE_GM(bug6783, canvas, 500, 500) {
37 sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(100, 100);
38
39 SkPaint p;
40 p.setColor(SK_ColorYELLOW);
41 surface->getCanvas()->drawPaint(p);
42 p.setColor(SK_ColorBLUE);
43 surface->getCanvas()->drawRect(SkRect::MakeWH(50, 100), p);
44
45 sk_sp<SkImage> img = surface->makeImageSnapshot();
46
Mike Reed1f607332020-05-21 12:11:27 -040047 SkMatrix m = SkMatrix::Translate(25, 214) * SkMatrix::Scale(2, 2);
Mike Klein0cc60b82017-06-22 11:00:17 -070048 m.preSkew(0.5f, 0.5f);
49
50 // The bug was present at all filter levels, but you might not notice it at kNone.
51 p.setFilterQuality(kLow_SkFilterQuality);
52
53 // It's only important to repeat or mirror in x to show off the bug.
Mike Reedfae8fce2019-04-03 10:27:45 -040054 p.setShader(img->makeShader(SkTileMode::kRepeat, SkTileMode::kClamp, &m));
Mike Klein0cc60b82017-06-22 11:00:17 -070055 canvas->drawPaint(p);
56}