blob: d7eb3b6e9836829c7038adc21147a8c82938b7d9 [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;
30 bmp.setConfig(SkBitmap::kARGB_8888_Config, 2, 2);
31 bmp.allocPixels();
32 bmp.lockPixels();
33 uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
34 pixels[0] = SkPreMultiplyColor(SK_ColorRED);
35 pixels[1] = SkPreMultiplyColor(SK_ColorGREEN);
36 pixels[2] = SkPreMultiplyColor(SK_ColorBLACK);
37 pixels[3] = SkPreMultiplyColor(SK_ColorBLUE);
38 bmp.unlockPixels();
39
40 return bmp;
41}
42
43static const SkScalar RECT_SIZE = 64;
44static const SkScalar SLIDE_SIZE = 300;
45
46class ClippedBitmapShadersGM : public GM {
47public:
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000048 ClippedBitmapShadersGM(SkShader::TileMode mode, bool hq=false)
49 : fMode(mode), fHQ(hq) {
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000050 }
51
52protected:
53 SkShader::TileMode fMode;
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000054 bool fHQ;
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000055
56 virtual SkString onShortName() {
57 SkString descriptor;
58 switch (fMode) {
59 case SkShader::kRepeat_TileMode:
60 descriptor = "tile";
61 break;
62 case SkShader::kMirror_TileMode:
63 descriptor = "mirror";
64 break;
65 case SkShader::kClamp_TileMode:
66 descriptor = "clamp";
67 break;
68 default:
69 SkASSERT(false);
70 }
71 descriptor.prepend("clipped-bitmap-shaders-");
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000072 if (fHQ) {
73 descriptor.append("-hq");
74 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000075 return descriptor;
76 }
77
78 virtual SkISize onISize() {
79 return SkISize::Make(300, 300);
80 }
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000081
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000082 virtual void onDraw(SkCanvas* canvas) {
83 SkBitmap bmp = create_bitmap();
84 SkShader* shader = SkShader::CreateBitmapShader(
85 bmp, fMode, fMode);
86
87 SkPaint paint;
88 SkMatrix s;
89 s.reset();
90 s.setScale(8, 8);
91 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
92 shader->setLocalMatrix(s);
93 paint.setShader(shader)->unref();
commit-bot@chromium.org759befb2013-10-23 16:18:50 +000094
95 if (fHQ) {
96 paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
97 }
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +000098
99 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
100 for (int i = 0; i < 3; i++) {
101 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
102 for (int j = 0; j < 3; j++) {
103 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
104 if (i == 1 && j == 1) {
105 continue; // skip center element
106 }
107 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
108 RECT_SIZE, RECT_SIZE);
109 canvas->save();
110 canvas->clipRect(rect);
111 canvas->drawRect(rect, paint);
112 canvas->restore();
113 }
114 }
115 }
116
117private:
118 typedef GM INHERITED;
119};
120
121//////////////////////////////////////////////////////////////////////////////
122
123DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode); )
124DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode); )
125DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode); )
commit-bot@chromium.org759befb2013-10-23 16:18:50 +0000126
127DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode, true); )
128DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode, true); )
129DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode, true); )
130
131
commit-bot@chromium.orge324cc62013-08-21 23:10:45 +0000132}