blob: 02d07bf01e5068f926cf9ae1b423c87d76196874 [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:
48 ClippedBitmapShadersGM(SkShader::TileMode mode)
49 : fMode(mode) {
50 }
51
52protected:
53 SkShader::TileMode fMode;
54
55 virtual SkString onShortName() {
56 SkString descriptor;
57 switch (fMode) {
58 case SkShader::kRepeat_TileMode:
59 descriptor = "tile";
60 break;
61 case SkShader::kMirror_TileMode:
62 descriptor = "mirror";
63 break;
64 case SkShader::kClamp_TileMode:
65 descriptor = "clamp";
66 break;
67 default:
68 SkASSERT(false);
69 }
70 descriptor.prepend("clipped-bitmap-shaders-");
71 return descriptor;
72 }
73
74 virtual SkISize onISize() {
75 return SkISize::Make(300, 300);
76 }
77
78 virtual void onDraw(SkCanvas* canvas) {
79 SkBitmap bmp = create_bitmap();
80 SkShader* shader = SkShader::CreateBitmapShader(
81 bmp, fMode, fMode);
82
83 SkPaint paint;
84 SkMatrix s;
85 s.reset();
86 s.setScale(8, 8);
87 s.postTranslate(SLIDE_SIZE / 2, SLIDE_SIZE / 2);
88 shader->setLocalMatrix(s);
89 paint.setShader(shader)->unref();
90
91 SkScalar margin = (SLIDE_SIZE / 3 - RECT_SIZE) / 2;
92 for (int i = 0; i < 3; i++) {
93 SkScalar yOrigin = SLIDE_SIZE / 3 * i + margin;
94 for (int j = 0; j < 3; j++) {
95 SkScalar xOrigin = SLIDE_SIZE / 3 * j + margin;
96 if (i == 1 && j == 1) {
97 continue; // skip center element
98 }
99 SkRect rect = SkRect::MakeXYWH(xOrigin, yOrigin,
100 RECT_SIZE, RECT_SIZE);
101 canvas->save();
102 canvas->clipRect(rect);
103 canvas->drawRect(rect, paint);
104 canvas->restore();
105 }
106 }
107 }
108
109private:
110 typedef GM INHERITED;
111};
112
113//////////////////////////////////////////////////////////////////////////////
114
115DEF_GM( return new ClippedBitmapShadersGM(SkShader::kRepeat_TileMode); )
116DEF_GM( return new ClippedBitmapShadersGM(SkShader::kMirror_TileMode); )
117DEF_GM( return new ClippedBitmapShadersGM(SkShader::kClamp_TileMode); )
118}