1 /*
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
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #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"
17 #include "include/core/SkShader.h"
18 #include "include/core/SkSize.h"
19 #include "include/core/SkString.h"
20 #include "include/core/SkTileMode.h"
21 #include "include/core/SkTypes.h"
22
23 namespace {
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
create_bitmap()37 static SkBitmap create_bitmap() {
38 SkBitmap bmp;
39 bmp.allocN32Pixels(2, 2);
40 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);
45
46 return bmp;
47 }
48
49 constexpr SkScalar RECT_SIZE = 64;
50 constexpr SkScalar SLIDE_SIZE = 300;
51
52 class ClippedBitmapShadersGM : public skiagm::GM {
53 public:
ClippedBitmapShadersGM(SkTileMode mode,bool hq=false)54 ClippedBitmapShadersGM(SkTileMode mode, bool hq=false)
55 : fMode(mode), fHQ(hq) {
56 }
57
58 protected:
59 SkTileMode fMode;
60 bool fHQ;
61
onShortName()62 SkString onShortName() override {
63 SkString descriptor;
64 switch (fMode) {
65 case SkTileMode::kRepeat:
66 descriptor = "tile";
67 break;
68 case SkTileMode::kMirror:
69 descriptor = "mirror";
70 break;
71 case SkTileMode::kClamp:
72 descriptor = "clamp";
73 break;
74 case SkTileMode::kDecal:
75 descriptor = "decal";
76 break;
77 }
78 descriptor.prepend("clipped-bitmap-shaders-");
79 if (fHQ) {
80 descriptor.append("-hq");
81 }
82 return descriptor;
83 }
84
onISize()85 SkISize onISize() override { return {300, 300}; }
86
onDraw(SkCanvas * canvas)87 void onDraw(SkCanvas* canvas) override {
88 SkBitmap bmp = create_bitmap();
89 SkMatrix s;
90 s.reset();
91 s.setScale(8, 8);
92 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
93 SkPaint paint;
94 paint.setShader(bmp.makeShader(fMode, fMode, &s));
95
96 if (fHQ) {
97 paint.setFilterQuality(kHigh_SkFilterQuality);
98 }
99
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
118 private:
119 typedef GM INHERITED;
120 };
121 } // namespace
122
123 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat); )
124 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror); )
125 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp); )
126
127 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kRepeat, true); )
128 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kMirror, true); )
129 DEF_GM( return new ClippedBitmapShadersGM(SkTileMode::kClamp, true); )
130