blob: 881760468ec2d415511e0affe8cc1aa5f60744be [file] [log] [blame]
robertphillips@google.comaaa9b292013-07-25 21:34:00 +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"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
robertphillips@google.com2cc0b472013-08-20 16:51:20 +000010#include "SkBlurMaskFilter.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +000011#include "SkCanvas.h"
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000012
13#if SK_SUPPORT_GPU
14#include "GrContext.h"
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000015#endif
16
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000017// Create a black&white checked texture with 2 1-pixel rings
18// around the outside edge. The inner ring is red and the outer ring is blue.
19static void make_ringed_bitmap(SkBitmap* result, int width, int height) {
20 SkASSERT(0 == width % 2 && 0 == height % 2);
skia.committer@gmail.comed000842013-11-09 07:02:23 +000021
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000022 static const SkPMColor kRed = SkPreMultiplyColor(SK_ColorRED);
23 static const SkPMColor kBlue = SkPreMultiplyColor(SK_ColorBLUE);
24 static const SkPMColor kBlack = SkPreMultiplyColor(SK_ColorBLACK);
25 static const SkPMColor kWhite = SkPreMultiplyColor(SK_ColorWHITE);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000026
reed@google.com383a6972013-10-21 14:00:07 +000027 result->setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
28 kOpaque_SkAlphaType);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000029 result->allocPixels();
30 SkAutoLockPixels lock(*result);
31
skia.committer@gmail.com956b3102013-07-26 07:00:58 +000032 SkPMColor* scanline = result->getAddr32(0, 0);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000033 for (int x = 0; x < width; ++x) {
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000034 scanline[x] = kBlue;
35 }
36 scanline = result->getAddr32(0, 1);
37 scanline[0] = kBlue;
38 for (int x = 1; x < width - 1; ++x) {
39 scanline[x] = kRed;
40 }
41 scanline[width-1] = kBlue;
42
43 for (int y = 2; y < height/2; ++y) {
44 scanline = result->getAddr32(0, y);
45 scanline[0] = kBlue;
46 scanline[1] = kRed;
47 for (int x = 2; x < width/2; ++x) {
48 scanline[x] = kBlack;
49 }
50 for (int x = width/2; x < width-2; ++x) {
51 scanline[x] = kWhite;
52 }
53 scanline[width-2] = kRed;
54 scanline[width-1] = kBlue;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000055 }
56
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000057 for (int y = height/2; y < height-2; ++y) {
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000058 scanline = result->getAddr32(0, y);
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000059 scanline[0] = kBlue;
60 scanline[1] = kRed;
61 for (int x = 2; x < width/2; ++x) {
62 scanline[x] = kWhite;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000063 }
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000064 for (int x = width/2; x < width-2; ++x) {
65 scanline[x] = kBlack;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000066 }
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000067 scanline[width-2] = kRed;
68 scanline[width-1] = kBlue;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000069 }
70
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000071 scanline = result->getAddr32(0, height-2);
72 scanline[0] = kBlue;
73 for (int x = 1; x < width - 1; ++x) {
74 scanline[x] = kRed;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000075 }
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000076 scanline[width-1] = kBlue;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000077
78 scanline = result->getAddr32(0, height-1);
79 for (int x = 0; x < width; ++x) {
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +000080 scanline[x] = kBlue;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000081 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000082 result->setImmutable();
83}
84
85// This GM exercises the drawBitmapRectToRect "bleed" flag
86class BleedGM : public skiagm::GM {
87public:
88 BleedGM() {}
89
90protected:
91 virtual SkString onShortName() SK_OVERRIDE {
92 return SkString("bleed");
93 }
94
95 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +000096 return SkISize::Make(kWidth, 780);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +000097 }
98
99 virtual void onOnceBeforeDraw() SK_OVERRIDE {
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000100 make_ringed_bitmap(&fBitmapSmall, kSmallTextureSize, kSmallTextureSize);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000101
102 // To exercise the GPU's tiling path we need a texture
103 // too big for the GPU to handle in one go
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000104 make_ringed_bitmap(&fBitmapBig, 2*kMaxTextureSize, 2*kMaxTextureSize);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000105 }
106
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000107 // Draw only the center of the small bitmap
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000108 void drawCase1(SkCanvas* canvas, int transX, int transY,
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000109 SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
110 SkRect src = SkRect::MakeXYWH(2, 2,
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000111 SkIntToScalar(kSmallTextureSize-4),
112 SkIntToScalar(kSmallTextureSize-4));
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000113 SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
114
115 SkPaint paint;
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000116 paint.setFilterLevel(filter);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000117
118 canvas->save();
119 canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
120 canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint, flags);
121 canvas->restore();
122 }
123
124 // Draw almost all of the large bitmap
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000125 void drawCase2(SkCanvas* canvas, int transX, int transY,
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000126 SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
127 SkRect src = SkRect::MakeXYWH(2, 2,
128 SkIntToScalar(fBitmapBig.width()-4),
129 SkIntToScalar(fBitmapBig.height()-4));
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000130 SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
131
132 SkPaint paint;
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000133 paint.setFilterLevel(filter);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000134
135 canvas->save();
136 canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
137 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint, flags);
138 canvas->restore();
139 }
140
141 // Draw ~1/4 of the large bitmap
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000142 void drawCase3(SkCanvas* canvas, int transX, int transY,
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000143 SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
144 SkRect src = SkRect::MakeXYWH(2, 2,
145 SkIntToScalar(fBitmapBig.width()/2-2),
146 SkIntToScalar(fBitmapBig.height()/2-2));
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000147 SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
148
149 SkPaint paint;
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000150 paint.setFilterLevel(filter);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000151
152 canvas->save();
153 canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
154 canvas->drawBitmapRectToRect(fBitmapBig, &src, dst, &paint, flags);
155 canvas->restore();
156 }
157
robertphillips@google.com2cc0b472013-08-20 16:51:20 +0000158 // Draw the center of the small bitmap with a mask filter
skia.committer@gmail.comb74bdf02013-08-21 07:01:29 +0000159 void drawCase4(SkCanvas* canvas, int transX, int transY,
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000160 SkCanvas::DrawBitmapRectFlags flags, SkPaint::FilterLevel filter) {
161 SkRect src = SkRect::MakeXYWH(2, 2,
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000162 SkIntToScalar(kSmallTextureSize-4),
163 SkIntToScalar(kSmallTextureSize-4));
robertphillips@google.com2cc0b472013-08-20 16:51:20 +0000164 SkRect dst = SkRect::MakeXYWH(0, 0, SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
165
166 SkPaint paint;
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000167 paint.setFilterLevel(filter);
robertphillips@google.comb7061172013-09-06 14:16:12 +0000168 SkMaskFilter* mf = SkBlurMaskFilter::Create(SkBlurMaskFilter::kNormal_BlurStyle,
169 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(3)));
robertphillips@google.com2cc0b472013-08-20 16:51:20 +0000170 paint.setMaskFilter(mf)->unref();
171
172 canvas->save();
173 canvas->translate(SkIntToScalar(transX), SkIntToScalar(transY));
174 canvas->drawBitmapRectToRect(fBitmapSmall, &src, dst, &paint, flags);
175 canvas->restore();
176 }
177
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000178 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000179
180 canvas->clear(SK_ColorGRAY);
181
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000182 for (int m = 0; m < 2; ++m) {
183 canvas->save();
184 if (m) {
185 static const SkScalar kBottom = SkIntToScalar(kRow3Y + kBlockSize + kBlockSpacing);
186 canvas->translate(0, kBottom);
187 SkMatrix rotate;
188 rotate.setRotate(15.f, 0, kBottom + kBlockSpacing);
189 canvas->concat(rotate);
190 canvas->scale(0.71f, 1.22f);
191 }
skia.committer@gmail.comed000842013-11-09 07:02:23 +0000192
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000193 // First draw a column with no bleeding, tiling, or filtering
194 this->drawCase1(canvas, kCol0X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
195 this->drawCase2(canvas, kCol0X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
196 this->drawCase3(canvas, kCol0X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
197 this->drawCase4(canvas, kCol0X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kNone_FilterLevel);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000198
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000199 // Then draw a column with no bleeding or tiling but with low filtering
200 this->drawCase1(canvas, kCol1X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
201 this->drawCase2(canvas, kCol1X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
202 this->drawCase3(canvas, kCol1X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
203 this->drawCase4(canvas, kCol1X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000204
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000205 // Then draw a column with no bleeding or tiling but with high filtering
206 this->drawCase1(canvas, kCol2X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
207 this->drawCase2(canvas, kCol2X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
208 this->drawCase3(canvas, kCol2X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
209 this->drawCase4(canvas, kCol2X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
skia.committer@gmail.comed000842013-11-09 07:02:23 +0000210
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000211#if SK_SUPPORT_GPU
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000212 GrContext* ctx = canvas->getGrContext();
213 int oldMaxTextureSize = 0;
214 if (NULL != ctx) {
215 // shrink the max texture size so all our textures can be reasonably sized
216 oldMaxTextureSize = ctx->getMaxTextureSize();
217 ctx->setMaxTextureSizeOverride(kMaxTextureSize);
218 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000219#endif
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000220
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000221 // Then draw a column with no bleeding but with tiling and low filtering
222 this->drawCase1(canvas, kCol3X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
223 this->drawCase2(canvas, kCol3X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
224 this->drawCase3(canvas, kCol3X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
225 this->drawCase4(canvas, kCol3X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000226
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000227 // Then draw a column with no bleeding but with tiling and high filtering
228 this->drawCase1(canvas, kCol4X, kRow0Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
229 this->drawCase2(canvas, kCol4X, kRow1Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
230 this->drawCase3(canvas, kCol4X, kRow2Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
231 this->drawCase4(canvas, kCol4X, kRow3Y, SkCanvas::kNone_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
skia.committer@gmail.comed000842013-11-09 07:02:23 +0000232
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000233 // Then draw a column with bleeding, tiling, and low filtering
234 this->drawCase1(canvas, kCol5X, kRow0Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
235 this->drawCase2(canvas, kCol5X, kRow1Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
236 this->drawCase3(canvas, kCol5X, kRow2Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
237 this->drawCase4(canvas, kCol5X, kRow3Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kLow_FilterLevel);
skia.committer@gmail.comed000842013-11-09 07:02:23 +0000238
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000239 // Finally draw a column with bleeding, tiling, and high filtering
240 this->drawCase1(canvas, kCol6X, kRow0Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
241 this->drawCase2(canvas, kCol6X, kRow1Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
242 this->drawCase3(canvas, kCol6X, kRow2Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
243 this->drawCase4(canvas, kCol6X, kRow3Y, SkCanvas::kBleed_DrawBitmapRectFlag, SkPaint::kHigh_FilterLevel);
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000244
245#if SK_SUPPORT_GPU
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000246 if (NULL != ctx) {
247 ctx->setMaxTextureSizeOverride(oldMaxTextureSize);
248 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000249#endif
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +0000250 canvas->restore();
251 }
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000252 }
253
254private:
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000255 static const int kBlockSize = 70;
256 static const int kBlockSpacing = 5;
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000257
258 static const int kCol0X = kBlockSpacing;
259 static const int kCol1X = 2*kBlockSpacing + kBlockSize;
260 static const int kCol2X = 3*kBlockSpacing + 2*kBlockSize;
261 static const int kCol3X = 4*kBlockSpacing + 3*kBlockSize;
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000262 static const int kCol4X = 5*kBlockSpacing + 4*kBlockSize;
263 static const int kCol5X = 6*kBlockSpacing + 5*kBlockSize;
264 static const int kCol6X = 7*kBlockSpacing + 6*kBlockSize;
265 static const int kWidth = 8*kBlockSpacing + 7*kBlockSize;
robertphillips@google.comd7ca6612013-08-20 12:09:32 +0000266
267 static const int kRow0Y = kBlockSpacing;
268 static const int kRow1Y = 2*kBlockSpacing + kBlockSize;
269 static const int kRow2Y = 3*kBlockSpacing + 2*kBlockSize;
robertphillips@google.com2cc0b472013-08-20 16:51:20 +0000270 static const int kRow3Y = 4*kBlockSpacing + 3*kBlockSize;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000271
commit-bot@chromium.orgec39b502013-11-08 15:09:22 +0000272 static const int kSmallTextureSize = 6;
robertphillips@google.comaaa9b292013-07-25 21:34:00 +0000273 static const int kMaxTextureSize = 32;
274
275 SkBitmap fBitmapSmall;
276 SkBitmap fBitmapBig;
277
278 typedef GM INHERITED;
279};
280
281DEF_GM( return new BleedGM(); )