blob: 6225abeec1b983e31b8539b4b40afd5978ad6bea [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
8#include "gm.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColor.h"
12#include "SkShader.h"
13
14namespace skiagm {
15
16// This GM draws a 3x3 grid (with the center element excluded) of rectangles
17// filled with a bitmap shader. The bitmap shader is transformed so that the
18// pattern cell is at the center (excluded) region.
19//
20// In Repeat and Mirror mode, this tests that the bitmap shader still draws
21// even though the pattern cell is outside the clip.
22//
23// In Clamp mode, this tests that the clamp is handled properly. For PDF,
24// (and possibly other exported formats) this also "tests" that the image itself
25// is not stored (well, you'll need to open it up with an external tool to
26// verify that).
27
28static SkBitmap create_bitmap() {
29 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +000030 bmp.allocN32Pixels(2, 2);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000031 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
32 pixels[0] = SkPreMultiplyColor(SK_ColorRED);
33 pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
34 pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
35 pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000036
37 return bmp;
38}
39
40static const SkScalar RECT_SIZE = 64;
41static const SkScalar SLIDE_SIZE = 300;
42
43class ClippedBitmapShadersGM : public GM {
44public:
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000045 ClippedBitmapShadersGM(SkShader::TileMode mode, bool hq=false)
46 : fMode(mode), fHQ(hq) {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000047 }
48
49protected:
50 SkShader::TileMode fMode;
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000051 bool fHQ;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000052
53 virtual SkString onShortName() {
54 SkString descriptor;
55 switch (fMode) {
56 case SkShader::kRepeat_TileMode:
57 descriptor = "tile";
58 break;
59 case SkShader::kMirror_TileMode:
60 descriptor = "mirror";
61 break;
62 case SkShader::kClamp_TileMode:
63 descriptor = "clamp";
64 break;
65 default:
66 SkASSERT(false);
67 }
68 descriptor.prepend("clipped-bitmap-shaders-");
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000069 if (fHQ) {
70 descriptor.append("-hq");
71 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000072 return descriptor;
73 }
74
75 virtual SkISize onISize() {
76 return SkISize::Make(300, 300);
77 }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000078
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000079 virtual void onDraw(SkCanvas* canvas) {
80 SkBitmap bmp = create_bitmap();
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000081 SkMatrix s;
82 s.reset();
83 s.setScale(8, 8);
84 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000085 SkShader* shader = SkShader::CreateBitmapShader(
86 bmp, fMode, fMode, &s);
87
88 SkPaint paint;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000089 paint.setShader(shader)->unref();
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +000090
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000091 if (fHQ) {
reed93a12152015-03-16 10:08:34 -070092 paint.setFilterQuality(kHigh_SkFilterQuality);
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000093 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000094
95 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
96 for (int i = 0; i < 3; i++) {
97 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
98 for (int j = 0; j < 3; j++) {
99 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
100 if (i == 1 && j == 1) {
101 continue; // skip center element
102 }
103 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
104 RECT_SIZE, RECT_SIZE);
105 canvas->save();
106 canvas->clipRect(rect);
107 canvas->drawRect(rect, paint);
108 canvas->restore();
109 }
110 }
111 }
112
113private:
114 typedef GM INHERITED;
115};
116
117//////////////////////////////////////////////////////////////////////////////
118
119DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode); )
120DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode); )
121DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000122
123DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode, true); )
124DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode, true); )
125DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode, true); )
126
127
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000128}