blob: 7487ef17a68af816184114e58611ea6364251e8e [file] [log] [blame]
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +00001/*
2 * Copyright 2013 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"
9#include "include/core/SkBitmap.h"
10#include "include/core/SkCanvas.h"
11#include "include/core/SkColor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkMatrix.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
19#include "include/core/SkTileMode.h"
20#include "include/core/SkTypes.h"
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000021
Hal Canarybd865e22019-07-18 11:51:19 -040022namespace {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000023
24// This GM draws a 3x3 grid (with the center element excluded) of rectangles
25// filled with a bitmap shader. The bitmap shader is transformed so that the
26// pattern cell is at the center (excluded) region.
27//
28// In Repeat and Mirror mode, this tests that the bitmap shader still draws
29// even though the pattern cell is outside the clip.
30//
31// In Clamp mode, this tests that the clamp is handled properly. For PDF,
32// (and possibly other exported formats) this also "tests" that the image itself
33// is not stored (well, you'll need to open it up with an external tool to
34// verify that).
35
36static SkBitmap create_bitmap() {
37 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +000038 bmp.allocN32Pixels(2, 2);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000039 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
40 pixels[0] = SkPreMultiplyColor(SK_ColorRED);
41 pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
42 pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
43 pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000044
45 return bmp;
46}
47
mtkleindbfd7ab2016-09-01 11:24:54 -070048constexpr SkScalar RECT_SIZE = 64;
49constexpr SkScalar SLIDE_SIZE = 300;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000050
Hal Canarybd865e22019-07-18 11:51:19 -040051class ClippedBitmapShadersGM : public skiagm::GM {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000052public:
Mike Reedfae8fce2019-04-03 10:27:45 -040053 ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000054 : fMode(mode), fHQ(hq) {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000055 }
56
57protected:
Mike Reedfae8fce2019-04-03 10:27:45 -040058 SkTileMode fMode;
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000059 bool fHQ;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000060
Hal Canarybd865e22019-07-18 11:51:19 -040061 SkString onShortName() override {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000062 SkString descriptor;
63 switch (fMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -040064 case SkTileMode::kRepeat:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000065 descriptor = "tile";
66 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040067 case SkTileMode::kMirror:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000068 descriptor = "mirror";
69 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040070 case SkTileMode::kClamp:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000071 descriptor = "clamp";
72 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040073 case SkTileMode::kDecal:
74 descriptor = "decal";
75 break;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000076 }
77 descriptor.prepend("clipped-bitmap-shaders-");
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000078 if (fHQ) {
79 descriptor.append("-hq");
80 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000081 return descriptor;
82 }
83
Hal Canarybd865e22019-07-18 11:51:19 -040084 SkISize onISize() override { return {300, 300}; }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000085
Hal Canarybd865e22019-07-18 11:51:19 -040086 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000087 SkBitmap bmp = create_bitmap();
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000088 SkMatrix s;
89 s.reset();
90 s.setScale(8, 8);
91 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000092 SkPaint paint;
Mike Reed82abece2020-12-12 09:51:11 -050093 paint.setShader(bmp.makeShader(fMode, fMode,
Mike Reedf3ac2af2021-02-05 12:55:38 -050094 fHQ ? SkSamplingOptions(SkCubicResampler::Mitchell())
Mike Reed82abece2020-12-12 09:51:11 -050095 : SkSamplingOptions(),
96 s));
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000097
98 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
99 for (int i = 0; i < 3; i++) {
100 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
101 for (int j = 0; j < 3; j++) {
102 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
103 if (i == 1 && j == 1) {
104 continue; // skip center element
105 }
106 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
107 RECT_SIZE, RECT_SIZE);
108 canvas->save();
109 canvas->clipRect(rect);
110 canvas->drawRect(rect, paint);
111 canvas->restore();
112 }
113 }
114 }
115
116private:
John Stiles7571f9e2020-09-02 22:42:33 -0400117 using INHERITED = GM;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000118};
Hal Canarybd865e22019-07-18 11:51:19 -0400119} // namespace
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000120
Mike Reedfae8fce2019-04-03 10:27:45 -0400121DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
122DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
123DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000124
Mike Reedfae8fce2019-04-03 10:27:45 -0400125DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
126DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
127DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )