blob: 8825c1b89fc59404b12bb81fc505e24571f21756 [file] [log] [blame]
robertphillipse275fdf2015-04-09 06:47:12 -07001/*
2 * Copyright 2015 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 Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkFilterQuality.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkImageFilter.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkRefCnt.h"
17#include "include/core/SkScalar.h"
18#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkTypes.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040022#include "include/effects/SkImageFilters.h"
robertphillipse275fdf2015-04-09 06:47:12 -070023
24namespace skiagm {
25
fmalita5598b632015-09-15 11:26:13 -070026// This GM reproduces the issue in crbug.com/472795. The SkImageSource image
robertphillipse275fdf2015-04-09 06:47:12 -070027// is shifted for high quality mode between cpu and gpu.
fmalita5598b632015-09-15 11:26:13 -070028class ImageSourceGM : public GM {
robertphillipse275fdf2015-04-09 06:47:12 -070029public:
Mike Reed708dc4f2021-02-13 21:04:58 -050030 ImageSourceGM(const char* suffix, const SkSamplingOptions& sampling)
31 : fSuffix(suffix), fSampling(sampling) {
robertphillipse275fdf2015-04-09 06:47:12 -070032 this->setBGColor(0xFFFFFFFF);
33 }
34
35protected:
36 SkString onShortName() override {
fmalita5598b632015-09-15 11:26:13 -070037 SkString name("imagesrc2_");
robertphillipse275fdf2015-04-09 06:47:12 -070038 name.append(fSuffix);
39 return name;
40 }
41
42 SkISize onISize() override { return SkISize::Make(256, 256); }
43
fmalita5598b632015-09-15 11:26:13 -070044 // Create an image with high frequency vertical stripes
robertphillipse275fdf2015-04-09 06:47:12 -070045 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070046 constexpr SkPMColor gColors[] = {
robertphillipse275fdf2015-04-09 06:47:12 -070047 SK_ColorRED, SK_ColorGRAY,
48 SK_ColorGREEN, SK_ColorGRAY,
49 SK_ColorBLUE, SK_ColorGRAY,
50 SK_ColorCYAN, SK_ColorGRAY,
51 SK_ColorMAGENTA, SK_ColorGRAY,
52 SK_ColorYELLOW, SK_ColorGRAY,
53 SK_ColorWHITE, SK_ColorGRAY,
54 };
55
reede8f30622016-03-23 18:59:25 -070056 auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize));
fmalita5598b632015-09-15 11:26:13 -070057 SkCanvas* canvas = surface->getCanvas();
robertphillipse275fdf2015-04-09 06:47:12 -070058
59 int curColor = 0;
60
61 for (int x = 0; x < kImageSize; x += 3) {
halcanary9d524f22016-03-29 09:03:52 -070062 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
robertphillipse275fdf2015-04-09 06:47:12 -070063 SkIntToScalar(3), SkIntToScalar(kImageSize));
64 SkPaint p;
65 p.setColor(gColors[curColor]);
fmalita5598b632015-09-15 11:26:13 -070066 canvas->drawRect(r, p);
robertphillipse275fdf2015-04-09 06:47:12 -070067
68 curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
69 }
fmalita5598b632015-09-15 11:26:13 -070070
reed9ce9d672016-03-17 10:51:11 -070071 fImage = surface->makeImageSnapshot();
robertphillipse275fdf2015-04-09 06:47:12 -070072 }
73
74 void onDraw(SkCanvas* canvas) override {
robertphillips549c8992016-04-01 09:28:51 -070075 const SkRect srcRect = SkRect::MakeLTRB(0, 0,
76 SkIntToScalar(kImageSize),
77 SkIntToScalar(kImageSize));
78 const SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f);
robertphillipse275fdf2015-04-09 06:47:12 -070079
80 SkPaint p;
Mike Reed708dc4f2021-02-13 21:04:58 -050081 p.setImageFilter(SkImageFilters::Image(fImage, srcRect, dstRect, fSampling));
robertphillipse275fdf2015-04-09 06:47:12 -070082
halcanary96fcdcc2015-08-27 07:41:13 -070083 canvas->saveLayer(nullptr, &p);
robertphillipse275fdf2015-04-09 06:47:12 -070084 canvas->restore();
85 }
86
87private:
mtkleindbfd7ab2016-09-01 11:24:54 -070088 static constexpr int kImageSize = 503;
robertphillipse275fdf2015-04-09 06:47:12 -070089
Mike Reed708dc4f2021-02-13 21:04:58 -050090 SkString fSuffix;
91 SkSamplingOptions fSampling;
92 sk_sp<SkImage> fImage;
robertphillipse275fdf2015-04-09 06:47:12 -070093
John Stiles7571f9e2020-09-02 22:42:33 -040094 using INHERITED = GM;
robertphillipse275fdf2015-04-09 06:47:12 -070095};
96
97//////////////////////////////////////////////////////////////////////////////
98
Mike Reed708dc4f2021-02-13 21:04:58 -050099DEF_GM(return new ImageSourceGM("none", SkSamplingOptions());)
100DEF_GM(return new ImageSourceGM("low", SkSamplingOptions(SkFilterMode::kLinear));)
101DEF_GM(return new ImageSourceGM("med", SkSamplingOptions(SkFilterMode::kLinear,
102 SkMipmapMode::kLinear));)
103DEF_GM(return new ImageSourceGM("high", SkSamplingOptions({1/3.0f, 1/3.0f}));)
John Stilesa6841be2020-08-06 14:11:56 -0400104} // namespace skiagm