reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 10 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 11 | #include "Resources.h" |
| 12 | #include "SkGradientShader.h" |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 13 | #include "SkStream.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 14 | #include "SkTypeface.h" |
reed@google.com | 7b06ba4 | 2013-05-28 16:39:52 +0000 | [diff] [blame] | 15 | |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame] | 16 | static void setTypeface(SkPaint* paint, const char name[], SkFontStyle style) { |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 17 | sk_tool_utils::set_portable_typeface(paint, name, style); |
reed@google.com | 7b06ba4 | 2013-05-28 16:39:52 +0000 | [diff] [blame] | 18 | } |
| 19 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 20 | static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) { |
reed@google.com | 98447b0 | 2013-05-28 15:42:21 +0000 | [diff] [blame] | 21 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()), |
| 22 | SkIntToScalar(bm.height())); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 23 | mat.mapRect(&bounds); |
reed@google.com | 98447b0 | 2013-05-28 15:42:21 +0000 | [diff] [blame] | 24 | return SkSize::Make(bounds.width(), bounds.height()); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 27 | static void draw_cell(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx, |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 28 | SkFilterQuality lvl) { |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 29 | SkPaint paint; |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 30 | paint.setFilterQuality(lvl); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 31 | |
| 32 | SkAutoCanvasRestore acr(canvas, true); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 33 | canvas->translate(dx, 0); |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 34 | canvas->concat(mat); |
| 35 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 36 | } |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 37 | |
Florin Malita | c54d8db | 2014-12-10 12:02:16 -0500 | [diff] [blame] | 38 | static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) { |
reed | 93a1215 | 2015-03-16 10:08:34 -0700 | [diff] [blame] | 39 | draw_cell(canvas, bm, mat, 0 * dx, kNone_SkFilterQuality); |
| 40 | draw_cell(canvas, bm, mat, 1 * dx, kLow_SkFilterQuality); |
| 41 | draw_cell(canvas, bm, mat, 2 * dx, kMedium_SkFilterQuality); |
| 42 | draw_cell(canvas, bm, mat, 3 * dx, kHigh_SkFilterQuality); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | class FilterBitmapGM : public skiagm::GM { |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | void onOnceBeforeDraw() override { |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 47 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 48 | this->makeBitmap(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 49 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 50 | SkScalar cx = SkScalarHalf(fBM.width()); |
| 51 | SkScalar cy = SkScalarHalf(fBM.height()); |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 52 | SkScalar scale = this->getScale(); |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 53 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 54 | // these two matrices use a scale factor configured by the subclass |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 55 | fMatrix[0].setScale(scale, scale); |
| 56 | fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale); |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 57 | |
| 58 | // up/down scaling mix |
| 59 | fMatrix[2].setScale(0.7f, 1.05f); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public: |
| 63 | SkBitmap fBM; |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 64 | SkMatrix fMatrix[3]; |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 65 | SkString fName; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 66 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 67 | FilterBitmapGM() |
| 68 | { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 69 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 73 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | SkString onShortName() override { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 75 | return fName; |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 76 | } |
| 77 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 78 | SkISize onISize() override { |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 79 | return SkISize::Make(1024, 768); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 80 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 82 | virtual void makeBitmap() = 0; |
| 83 | virtual SkScalar getScale() = 0; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 84 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 85 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 86 | |
| 87 | canvas->translate(10, 10); |
| 88 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) { |
| 89 | SkSize size = computeSize(fBM, fMatrix[i]); |
| 90 | size.fWidth += 20; |
| 91 | size.fHeight += 20; |
| 92 | |
commit-bot@chromium.org | 79b7eee | 2013-12-16 21:02:29 +0000 | [diff] [blame] | 93 | draw_row(canvas, fBM, fMatrix[i], size.fWidth); |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 94 | canvas->translate(0, size.fHeight); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | typedef skiagm::GM INHERITED; |
| 100 | }; |
| 101 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 102 | class FilterBitmapTextGM: public FilterBitmapGM { |
| 103 | public: |
humper@google.com | b088947 | 2013-07-09 21:37:14 +0000 | [diff] [blame] | 104 | FilterBitmapTextGM(float textSize) |
| 105 | : fTextSize(textSize) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 106 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 107 | fName.printf("filterbitmap_text_%.2fpt", fTextSize); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 108 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 109 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 110 | protected: |
| 111 | float fTextSize; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 112 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | SkScalar getScale() override { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 114 | return 32.f/fTextSize; |
| 115 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 116 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 117 | void makeBitmap() override { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 118 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 119 | SkCanvas canvas(fBM); |
| 120 | canvas.drawColor(SK_ColorWHITE); |
| 121 | |
| 122 | SkPaint paint; |
| 123 | paint.setAntiAlias(true); |
| 124 | paint.setSubpixelText(true); |
| 125 | paint.setTextSize(fTextSize); |
| 126 | |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 127 | setTypeface(&paint, "serif", SkFontStyle::Normal()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 128 | canvas.drawString("Hamburgefons", fTextSize/2, 1.2f*fTextSize, paint); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 129 | setTypeface(&paint, "serif", SkFontStyle::Bold()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 130 | canvas.drawString("Hamburgefons", fTextSize/2, 2.4f*fTextSize, paint); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 131 | setTypeface(&paint, "serif", SkFontStyle::Italic()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 132 | canvas.drawString("Hamburgefons", fTextSize/2, 3.6f*fTextSize, paint); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 133 | setTypeface(&paint, "serif", SkFontStyle::BoldItalic()); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 134 | canvas.drawString("Hamburgefons", fTextSize/2, 4.8f*fTextSize, paint); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 135 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 136 | private: |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 137 | typedef FilterBitmapGM INHERITED; |
| 138 | }; |
| 139 | |
| 140 | class FilterBitmapCheckerboardGM: public FilterBitmapGM { |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 141 | public: |
| 142 | FilterBitmapCheckerboardGM(int size, int num_checks, bool convertToG8 = false) |
| 143 | : fSize(size), fNumChecks(num_checks), fConvertToG8(convertToG8) |
| 144 | { |
| 145 | fName.printf("filterbitmap_checkerboard_%d_%d%s", |
| 146 | fSize, fNumChecks, convertToG8 ? "_g8" : ""); |
| 147 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 148 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 149 | protected: |
| 150 | int fSize; |
| 151 | int fNumChecks; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 152 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 153 | SkScalar getScale() override { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 154 | return 192.f/fSize; |
| 155 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 156 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 157 | void makeBitmap() override { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 158 | fBM.allocN32Pixels(fSize, fSize); |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 159 | for (int y = 0; y < fSize; y ++) { |
| 160 | for (int x = 0; x < fSize; x ++) { |
| 161 | SkPMColor* s = fBM.getAddr32(x, y); |
| 162 | int cx = (x * fNumChecks) / fSize; |
| 163 | int cy = (y * fNumChecks) / fSize; |
| 164 | if ((cx+cy)%2) { |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 165 | *s = 0xFFFFFFFF; |
| 166 | } else { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 167 | *s = 0xFF000000; |
| 168 | } |
| 169 | } |
| 170 | } |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 171 | if (fConvertToG8) { |
| 172 | SkBitmap tmp; |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 173 | sk_tool_utils::copy_to_g8(&tmp, fBM); |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 174 | fBM = tmp; |
| 175 | } |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 176 | } |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 177 | private: |
| 178 | const bool fConvertToG8; |
| 179 | typedef FilterBitmapGM INHERITED; |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | class FilterBitmapImageGM: public FilterBitmapGM { |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 183 | public: |
| 184 | FilterBitmapImageGM(const char filename[], bool convertToG8 = false) |
| 185 | : fFilename(filename), fConvertToG8(convertToG8) |
| 186 | { |
| 187 | fName.printf("filterbitmap_image_%s%s", filename, convertToG8 ? "_g8" : ""); |
| 188 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 189 | |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 190 | protected: |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 191 | SkString fFilename; |
| 192 | int fSize; |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 193 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 194 | SkScalar getScale() override { |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 195 | return 192.f/fSize; |
| 196 | } |
skia.committer@gmail.com | 26da7f0 | 2013-06-01 07:01:39 +0000 | [diff] [blame] | 197 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 198 | void makeBitmap() override { |
msarett | e820dfe | 2016-03-18 12:13:47 -0700 | [diff] [blame] | 199 | if (!GetResourceAsBitmap(fFilename.c_str(), &fBM)) { |
| 200 | fBM.allocN32Pixels(1, 1); |
| 201 | fBM.eraseARGB(255, 255, 0 , 0); // red == bad |
| 202 | } |
| 203 | fSize = fBM.height(); |
| 204 | |
| 205 | if (fConvertToG8) { |
| 206 | SkBitmap tmp; |
Matt Sarett | 4897fb8 | 2017-01-18 11:49:33 -0500 | [diff] [blame] | 207 | sk_tool_utils::copy_to_g8(&tmp, fBM); |
msarett | e820dfe | 2016-03-18 12:13:47 -0700 | [diff] [blame] | 208 | fBM = tmp; |
| 209 | } |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 210 | } |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 211 | private: |
| 212 | const bool fConvertToG8; |
| 213 | typedef FilterBitmapGM INHERITED; |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
reed@google.com | f2de1e2 | 2013-05-28 15:29:25 +0000 | [diff] [blame] | 216 | ////////////////////////////////////////////////////////////////////////////// |
| 217 | |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 218 | DEF_GM( return new FilterBitmapTextGM(3); ) |
| 219 | DEF_GM( return new FilterBitmapTextGM(7); ) |
| 220 | DEF_GM( return new FilterBitmapTextGM(10); ) |
| 221 | DEF_GM( return new FilterBitmapCheckerboardGM(4,4); ) |
| 222 | DEF_GM( return new FilterBitmapCheckerboardGM(32,32); ) |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 223 | DEF_GM( return new FilterBitmapCheckerboardGM(32,32, true); ) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 224 | DEF_GM( return new FilterBitmapCheckerboardGM(32,8); ) |
| 225 | DEF_GM( return new FilterBitmapCheckerboardGM(32,2); ) |
| 226 | DEF_GM( return new FilterBitmapCheckerboardGM(192,192); ) |
| 227 | DEF_GM( return new FilterBitmapImageGM("mandrill_16.png"); ) |
| 228 | DEF_GM( return new FilterBitmapImageGM("mandrill_32.png"); ) |
| 229 | DEF_GM( return new FilterBitmapImageGM("mandrill_64.png"); ) |
reed | 0c9b1a8 | 2015-03-17 17:44:06 -0700 | [diff] [blame] | 230 | DEF_GM( return new FilterBitmapImageGM("mandrill_64.png", true); ) |
humper@google.com | 04db316 | 2013-05-31 12:50:39 +0000 | [diff] [blame] | 231 | DEF_GM( return new FilterBitmapImageGM("mandrill_128.png"); ) |
| 232 | DEF_GM( return new FilterBitmapImageGM("mandrill_256.png"); ) |
| 233 | DEF_GM( return new FilterBitmapImageGM("mandrill_512.png"); ) |
halcanary | dffa397 | 2014-10-27 10:32:18 -0700 | [diff] [blame] | 234 | DEF_GM( return new FilterBitmapImageGM("color_wheel.png"); ) |