blob: a26e603a4c1b2ae07a249f45bfc26e23260ee8c4 [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
23namespace skiagm {
24
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
52class ClippedBitmapShadersGM : public GM {
53public:
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
62 virtual SkString onShortName() {
63 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
85 virtual SkISize onISize() {
86 return SkISize::Make(300, 300);
87 }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000088
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000089 virtual void onDraw(SkCanvas* canvas) {
90 SkBitmap bmp = create_bitmap();
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000091 SkMatrix s;
92 s.reset();
93 s.setScale(8, 8);
94 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000095 SkPaint paint;
Mike Reed50acf8f2019-04-08 13:20:23 -040096 paint.setShader(bmp.makeShader(fMode, fMode, &s));
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000097
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000098 if (fHQ) {
reed93a12152015-03-16 10:08:34 -070099 paint.setFilterQuality(kHigh_SkFilterQuality);
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000100 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000101
102 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
103 for (int i = 0; i < 3; i++) {
104 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
105 for (int j = 0; j < 3; j++) {
106 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
107 if (i == 1 && j == 1) {
108 continue; // skip center element
109 }
110 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
111 RECT_SIZE, RECT_SIZE);
112 canvas->save();
113 canvas->clipRect(rect);
114 canvas->drawRect(rect, paint);
115 canvas->restore();
116 }
117 }
118 }
119
120private:
121 typedef GM INHERITED;
122};
123
124//////////////////////////////////////////////////////////////////////////////
125
Mike Reedfae8fce2019-04-03 10:27:45 -0400126DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
127DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
128DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000129
Mike Reedfae8fce2019-04-03 10:27:45 -0400130DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
131DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
132DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000133
134
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000135}