blob: aa22851723e246829aa492e14ecd7b4eb1af9864 [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/SkFilterQuality.h"
13#include "include/core/SkMatrix.h"
14#include "include/core/SkPaint.h"
15#include "include/core/SkRect.h"
16#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkSize.h"
19#include "include/core/SkString.h"
20#include "include/core/SkTileMode.h"
21#include "include/core/SkTypes.h"
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000022
Hal Canarybd865e22019-07-18 11:51:19 -040023namespace {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000024
25// This GM draws a 3x3 grid (with the center element excluded) of rectangles
26// filled with a bitmap shader. The bitmap shader is transformed so that the
27// pattern cell is at the center (excluded) region.
28//
29// In Repeat and Mirror mode, this tests that the bitmap shader still draws
30// even though the pattern cell is outside the clip.
31//
32// In Clamp mode, this tests that the clamp is handled properly. For PDF,
33// (and possibly other exported formats) this also "tests" that the image itself
34// is not stored (well, you'll need to open it up with an external tool to
35// verify that).
36
37static SkBitmap create_bitmap() {
38 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +000039 bmp.allocN32Pixels(2, 2);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000040 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
41 pixels[0] = SkPreMultiplyColor(SK_ColorRED);
42 pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
43 pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
44 pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000045
46 return bmp;
47}
48
mtkleindbfd7ab2016-09-01 11:24:54 -070049constexpr SkScalar RECT_SIZE = 64;
50constexpr SkScalar SLIDE_SIZE = 300;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000051
Hal Canarybd865e22019-07-18 11:51:19 -040052class ClippedBitmapShadersGM : public skiagm::GM {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000053public:
Mike Reedfae8fce2019-04-03 10:27:45 -040054 ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000055 : fMode(mode), fHQ(hq) {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000056 }
57
58protected:
Mike Reedfae8fce2019-04-03 10:27:45 -040059 SkTileMode fMode;
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000060 bool fHQ;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000061
Hal Canarybd865e22019-07-18 11:51:19 -040062 SkString onShortName() override {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000063 SkString descriptor;
64 switch (fMode) {
Mike Reedfae8fce2019-04-03 10:27:45 -040065 case SkTileMode::kRepeat:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000066 descriptor = "tile";
67 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040068 case SkTileMode::kMirror:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000069 descriptor = "mirror";
70 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040071 case SkTileMode::kClamp:
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000072 descriptor = "clamp";
73 break;
Mike Reedfae8fce2019-04-03 10:27:45 -040074 case SkTileMode::kDecal:
75 descriptor = "decal";
76 break;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000077 }
78 descriptor.prepend("clipped-bitmap-shaders-");
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000079 if (fHQ) {
80 descriptor.append("-hq");
81 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000082 return descriptor;
83 }
84
Hal Canarybd865e22019-07-18 11:51:19 -040085 SkISize onISize() override { return {300, 300}; }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000086
Hal Canarybd865e22019-07-18 11:51:19 -040087 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000088 SkBitmap bmp = create_bitmap();
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000089 SkMatrix s;
90 s.reset();
91 s.setScale(8, 8);
92 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000093 SkPaint paint;
Mike Reed50acf8f2019-04-08 13:20:23 -040094 paint.setShader(bmp.makeShader(fMode, fMode, &s));
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000095
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000096 if (fHQ) {
reed93a12152015-03-16 10:08:34 -070097 paint.setFilterQuality(kHigh_SkFilterQuality);
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000098 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000099
100 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
101 for (int i = 0; i < 3; i++) {
102 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
103 for (int j = 0; j < 3; j++) {
104 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
105 if (i == 1 && j == 1) {
106 continue; // skip center element
107 }
108 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
109 RECT_SIZE, RECT_SIZE);
110 canvas->save();
111 canvas->clipRect(rect);
112 canvas->drawRect(rect, paint);
113 canvas->restore();
114 }
115 }
116 }
117
118private:
119 typedef GM INHERITED;
120};
Hal Canarybd865e22019-07-18 11:51:19 -0400121} // namespace
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000122
Mike Reedfae8fce2019-04-03 10:27:45 -0400123DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
124DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
125DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000126
Mike Reedfae8fce2019-04-03 10:27:45 -0400127DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
128DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
129DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )