blob: 21cea7659da34073ecb3eb101f896cbd48610e25 [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkImageFilter.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkRefCnt.h"
16#include "include/core/SkScalar.h"
17#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkTypes.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040021#include "include/effects/SkImageFilters.h"
robertphillipse275fdf2015-04-09 06:47:12 -070022
23namespace skiagm {
24
fmalita5598b632015-09-15 11:26:13 -070025// This GM reproduces the issue in crbug.com/472795. The SkImageSource image
robertphillipse275fdf2015-04-09 06:47:12 -070026// is shifted for high quality mode between cpu and gpu.
fmalita5598b632015-09-15 11:26:13 -070027class ImageSourceGM : public GM {
robertphillipse275fdf2015-04-09 06:47:12 -070028public:
Mike Reed708dc4f2021-02-13 21:04:58 -050029 ImageSourceGM(const char* suffix, const SkSamplingOptions& sampling)
30 : fSuffix(suffix), fSampling(sampling) {
robertphillipse275fdf2015-04-09 06:47:12 -070031 this->setBGColor(0xFFFFFFFF);
32 }
33
34protected:
35 SkString onShortName() override {
fmalita5598b632015-09-15 11:26:13 -070036 SkString name("imagesrc2_");
robertphillipse275fdf2015-04-09 06:47:12 -070037 name.append(fSuffix);
38 return name;
39 }
40
41 SkISize onISize() override { return SkISize::Make(256, 256); }
42
fmalita5598b632015-09-15 11:26:13 -070043 // Create an image with high frequency vertical stripes
robertphillipse275fdf2015-04-09 06:47:12 -070044 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070045 constexpr SkPMColor gColors[] = {
robertphillipse275fdf2015-04-09 06:47:12 -070046 SK_ColorRED, SK_ColorGRAY,
47 SK_ColorGREEN, SK_ColorGRAY,
48 SK_ColorBLUE, SK_ColorGRAY,
49 SK_ColorCYAN, SK_ColorGRAY,
50 SK_ColorMAGENTA, SK_ColorGRAY,
51 SK_ColorYELLOW, SK_ColorGRAY,
52 SK_ColorWHITE, SK_ColorGRAY,
53 };
54
reede8f30622016-03-23 18:59:25 -070055 auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize));
fmalita5598b632015-09-15 11:26:13 -070056 SkCanvas* canvas = surface->getCanvas();
robertphillipse275fdf2015-04-09 06:47:12 -070057
58 int curColor = 0;
59
60 for (int x = 0; x < kImageSize; x += 3) {
halcanary9d524f22016-03-29 09:03:52 -070061 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
robertphillipse275fdf2015-04-09 06:47:12 -070062 SkIntToScalar(3), SkIntToScalar(kImageSize));
63 SkPaint p;
64 p.setColor(gColors[curColor]);
fmalita5598b632015-09-15 11:26:13 -070065 canvas->drawRect(r, p);
robertphillipse275fdf2015-04-09 06:47:12 -070066
67 curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
68 }
fmalita5598b632015-09-15 11:26:13 -070069
reed9ce9d672016-03-17 10:51:11 -070070 fImage = surface->makeImageSnapshot();
robertphillipse275fdf2015-04-09 06:47:12 -070071 }
72
73 void onDraw(SkCanvas* canvas) override {
robertphillips549c8992016-04-01 09:28:51 -070074 const SkRect srcRect = SkRect::MakeLTRB(0, 0,
75 SkIntToScalar(kImageSize),
76 SkIntToScalar(kImageSize));
77 const SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f);
robertphillipse275fdf2015-04-09 06:47:12 -070078
79 SkPaint p;
Mike Reed708dc4f2021-02-13 21:04:58 -050080 p.setImageFilter(SkImageFilters::Image(fImage, srcRect, dstRect, fSampling));
robertphillipse275fdf2015-04-09 06:47:12 -070081
halcanary96fcdcc2015-08-27 07:41:13 -070082 canvas->saveLayer(nullptr, &p);
robertphillipse275fdf2015-04-09 06:47:12 -070083 canvas->restore();
84 }
85
86private:
mtkleindbfd7ab2016-09-01 11:24:54 -070087 static constexpr int kImageSize = 503;
robertphillipse275fdf2015-04-09 06:47:12 -070088
Mike Reed708dc4f2021-02-13 21:04:58 -050089 SkString fSuffix;
90 SkSamplingOptions fSampling;
91 sk_sp<SkImage> fImage;
robertphillipse275fdf2015-04-09 06:47:12 -070092
John Stiles7571f9e2020-09-02 22:42:33 -040093 using INHERITED = GM;
robertphillipse275fdf2015-04-09 06:47:12 -070094};
95
96//////////////////////////////////////////////////////////////////////////////
97
Mike Reed708dc4f2021-02-13 21:04:58 -050098DEF_GM(return new ImageSourceGM("none", SkSamplingOptions());)
99DEF_GM(return new ImageSourceGM("low", SkSamplingOptions(SkFilterMode::kLinear));)
100DEF_GM(return new ImageSourceGM("med", SkSamplingOptions(SkFilterMode::kLinear,
101 SkMipmapMode::kLinear));)
102DEF_GM(return new ImageSourceGM("high", SkSamplingOptions({1/3.0f, 1/3.0f}));)
John Stilesa6841be2020-08-06 14:11:56 -0400103} // namespace skiagm