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