blob: 70a91daabcf009c17c335094b946209d4c8093c3 [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
8#include "gm.h"
fmalita5598b632015-09-15 11:26:13 -07009#include "SkImage.h"
10#include "SkImageSource.h"
11#include "SkSurface.h"
robertphillipse275fdf2015-04-09 06:47:12 -070012
13namespace skiagm {
14
fmalita5598b632015-09-15 11:26:13 -070015// This GM reproduces the issue in crbug.com/472795. The SkImageSource image
robertphillipse275fdf2015-04-09 06:47:12 -070016// is shifted for high quality mode between cpu and gpu.
fmalita5598b632015-09-15 11:26:13 -070017class ImageSourceGM : public GM {
robertphillipse275fdf2015-04-09 06:47:12 -070018public:
fmalita5598b632015-09-15 11:26:13 -070019 ImageSourceGM(const char* suffix, SkFilterQuality filter) : fSuffix(suffix), fFilter(filter) {
robertphillipse275fdf2015-04-09 06:47:12 -070020 this->setBGColor(0xFFFFFFFF);
21 }
22
23protected:
24 SkString onShortName() override {
fmalita5598b632015-09-15 11:26:13 -070025 SkString name("imagesrc2_");
robertphillipse275fdf2015-04-09 06:47:12 -070026 name.append(fSuffix);
27 return name;
28 }
29
30 SkISize onISize() override { return SkISize::Make(256, 256); }
31
fmalita5598b632015-09-15 11:26:13 -070032 // Create an image with high frequency vertical stripes
robertphillipse275fdf2015-04-09 06:47:12 -070033 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070034 constexpr SkPMColor gColors[] = {
robertphillipse275fdf2015-04-09 06:47:12 -070035 SK_ColorRED, SK_ColorGRAY,
36 SK_ColorGREEN, SK_ColorGRAY,
37 SK_ColorBLUE, SK_ColorGRAY,
38 SK_ColorCYAN, SK_ColorGRAY,
39 SK_ColorMAGENTA, SK_ColorGRAY,
40 SK_ColorYELLOW, SK_ColorGRAY,
41 SK_ColorWHITE, SK_ColorGRAY,
42 };
43
reede8f30622016-03-23 18:59:25 -070044 auto surface(SkSurface::MakeRasterN32Premul(kImageSize, kImageSize));
fmalita5598b632015-09-15 11:26:13 -070045 SkCanvas* canvas = surface->getCanvas();
robertphillipse275fdf2015-04-09 06:47:12 -070046
47 int curColor = 0;
48
49 for (int x = 0; x < kImageSize; x += 3) {
halcanary9d524f22016-03-29 09:03:52 -070050 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(0),
robertphillipse275fdf2015-04-09 06:47:12 -070051 SkIntToScalar(3), SkIntToScalar(kImageSize));
52 SkPaint p;
53 p.setColor(gColors[curColor]);
fmalita5598b632015-09-15 11:26:13 -070054 canvas->drawRect(r, p);
robertphillipse275fdf2015-04-09 06:47:12 -070055
56 curColor = (curColor+1) % SK_ARRAY_COUNT(gColors);
57 }
fmalita5598b632015-09-15 11:26:13 -070058
reed9ce9d672016-03-17 10:51:11 -070059 fImage = surface->makeImageSnapshot();
robertphillipse275fdf2015-04-09 06:47:12 -070060 }
61
62 void onDraw(SkCanvas* canvas) override {
robertphillips549c8992016-04-01 09:28:51 -070063 const SkRect srcRect = SkRect::MakeLTRB(0, 0,
64 SkIntToScalar(kImageSize),
65 SkIntToScalar(kImageSize));
66 const SkRect dstRect = SkRect::MakeLTRB(0.75f, 0.75f, 225.75f, 225.75f);
robertphillipse275fdf2015-04-09 06:47:12 -070067
68 SkPaint p;
robertphillips549c8992016-04-01 09:28:51 -070069 p.setImageFilter(SkImageSource::Make(fImage, srcRect, dstRect, fFilter));
robertphillipse275fdf2015-04-09 06:47:12 -070070
halcanary96fcdcc2015-08-27 07:41:13 -070071 canvas->saveLayer(nullptr, &p);
robertphillipse275fdf2015-04-09 06:47:12 -070072 canvas->restore();
73 }
74
75private:
mtkleindbfd7ab2016-09-01 11:24:54 -070076 static constexpr int kImageSize = 503;
robertphillipse275fdf2015-04-09 06:47:12 -070077
78 SkString fSuffix;
79 SkFilterQuality fFilter;
reed9ce9d672016-03-17 10:51:11 -070080 sk_sp<SkImage> fImage;
robertphillipse275fdf2015-04-09 06:47:12 -070081
82 typedef GM INHERITED;
83};
84
85//////////////////////////////////////////////////////////////////////////////
86
fmalita5598b632015-09-15 11:26:13 -070087DEF_GM(return new ImageSourceGM("none", kNone_SkFilterQuality);)
88DEF_GM(return new ImageSourceGM("low", kLow_SkFilterQuality);)
89DEF_GM(return new ImageSourceGM("med", kMedium_SkFilterQuality);)
90DEF_GM(return new ImageSourceGM("high", kHigh_SkFilterQuality);)
robertphillipse275fdf2015-04-09 06:47:12 -070091}