humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 1 | /* |
| 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" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 9 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
| 11 | #include "SkGradientShader.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 12 | #include "SkTypeface.h" |
| 13 | #include "SkImageDecoder.h" |
| 14 | #include "SkStream.h" |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 15 | #include "SkPaint.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 16 | |
| 17 | static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 18 | sk_tool_utils::set_portable_typeface(paint, name, style); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | class DownsampleBitmapGM : public skiagm::GM { |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 22 | public: |
| 23 | SkBitmap fBM; |
| 24 | SkString fName; |
| 25 | bool fBitmapMade; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 26 | SkPaint::FilterLevel fFilterLevel; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 27 | |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 28 | DownsampleBitmapGM(SkPaint::FilterLevel filterLevel) |
| 29 | : fFilterLevel(filterLevel) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 30 | { |
| 31 | this->setBGColor(0xFFDDDDDD); |
| 32 | fBitmapMade = false; |
| 33 | } |
| 34 | |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 35 | const char* filterLevelToString() { |
| 36 | static const char *filterLevelNames[] = { |
| 37 | "none", "low", "medium", "high" |
| 38 | }; |
| 39 | return filterLevelNames[fFilterLevel]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 40 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 41 | |
| 42 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 43 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
humper | d73c169 | 2014-08-28 14:27:42 -0700 | [diff] [blame] | 44 | return kSkipTiled_Flag; |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 45 | } |
| 46 | |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 47 | virtual SkString onShortName() SK_OVERRIDE { |
| 48 | return fName; |
| 49 | } |
| 50 | |
| 51 | virtual SkISize onISize() SK_OVERRIDE { |
| 52 | make_bitmap_wrapper(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 53 | return SkISize::Make(fBM.width(), 4 * fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void make_bitmap_wrapper() { |
| 57 | if (!fBitmapMade) { |
| 58 | fBitmapMade = true; |
| 59 | make_bitmap(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | virtual void make_bitmap() = 0; |
| 64 | |
| 65 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 66 | make_bitmap_wrapper(); |
| 67 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 68 | int curY = 0; |
| 69 | int curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 70 | float curScale = 1; |
| 71 | do { |
| 72 | |
| 73 | SkMatrix matrix; |
| 74 | matrix.setScale( curScale, curScale ); |
| 75 | |
| 76 | SkPaint paint; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 77 | paint.setFilterLevel(fFilterLevel); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 78 | |
| 79 | canvas->save(); |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 80 | canvas->translate(0, (SkScalar)curY); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 81 | canvas->drawBitmapMatrix( fBM, matrix, &paint ); |
| 82 | canvas->restore(); |
| 83 | |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 84 | curHeight = (int) (fBM.height() * curScale + 2); |
| 85 | curY += curHeight; |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 86 | curScale *= 0.75f; |
mtklein | 6a2f5fe | 2014-09-03 13:40:52 -0700 | [diff] [blame] | 87 | } while (curHeight >= 2 && curY < 4*fBM.height()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef skiagm::GM INHERITED; |
| 92 | }; |
| 93 | |
| 94 | class DownsampleBitmapTextGM: public DownsampleBitmapGM { |
| 95 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 96 | DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel) |
| 97 | : INHERITED(filterLevel), fTextSize(textSize) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 98 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 99 | fName.printf("downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | protected: |
| 103 | float fTextSize; |
| 104 | |
| 105 | virtual void make_bitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 106 | fBM.allocN32Pixels(int(fTextSize * 8), int(fTextSize * 6)); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 107 | SkCanvas canvas(fBM); |
| 108 | canvas.drawColor(SK_ColorWHITE); |
| 109 | |
| 110 | SkPaint paint; |
| 111 | paint.setAntiAlias(true); |
| 112 | paint.setSubpixelText(true); |
| 113 | paint.setTextSize(fTextSize); |
| 114 | |
| 115 | setTypeface(&paint, "Times", SkTypeface::kNormal); |
| 116 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
| 117 | setTypeface(&paint, "Times", SkTypeface::kBold); |
| 118 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
| 119 | setTypeface(&paint, "Times", SkTypeface::kItalic); |
| 120 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
| 121 | setTypeface(&paint, "Times", SkTypeface::kBoldItalic); |
| 122 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
| 123 | } |
| 124 | private: |
| 125 | typedef DownsampleBitmapGM INHERITED; |
| 126 | }; |
| 127 | |
| 128 | class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM { |
| 129 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 130 | DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel) |
| 131 | : INHERITED(filterLevel), fSize(size), fNumChecks(numChecks) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 132 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 133 | fName.printf("downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | protected: |
| 137 | int fSize; |
| 138 | int fNumChecks; |
| 139 | |
| 140 | virtual void make_bitmap() SK_OVERRIDE { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 141 | fBM.allocN32Pixels(fSize, fSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 142 | for (int y = 0; y < fSize; ++y) { |
| 143 | for (int x = 0; x < fSize; ++x) { |
| 144 | SkPMColor* s = fBM.getAddr32(x, y); |
| 145 | int cx = (x * fNumChecks) / fSize; |
| 146 | int cy = (y * fNumChecks) / fSize; |
| 147 | if ((cx+cy)%2) { |
| 148 | *s = 0xFFFFFFFF; |
| 149 | } else { |
| 150 | *s = 0xFF000000; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | private: |
| 156 | typedef DownsampleBitmapGM INHERITED; |
| 157 | }; |
| 158 | |
| 159 | class DownsampleBitmapImageGM: public DownsampleBitmapGM { |
| 160 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 161 | DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel) |
| 162 | : INHERITED(filterLevel), fFilename(filename) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 163 | { |
humper@google.com | e2cf805 | 2013-07-25 19:14:37 +0000 | [diff] [blame] | 164 | fName.printf("downsamplebitmap_image_%s_%s", this->filterLevelToString(), filename); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | protected: |
| 168 | SkString fFilename; |
| 169 | int fSize; |
| 170 | |
| 171 | virtual void make_bitmap() SK_OVERRIDE { |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 172 | SkImageDecoder* codec = NULL; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 173 | SkString resourcePath = GetResourcePath(fFilename.c_str()); |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 174 | SkFILEStream stream(resourcePath.c_str()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 175 | if (stream.isValid()) { |
| 176 | codec = SkImageDecoder::Factory(&stream); |
| 177 | } |
| 178 | if (codec) { |
| 179 | stream.rewind(); |
reed | bfefc7c | 2014-06-12 17:40:00 -0700 | [diff] [blame] | 180 | codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 181 | SkDELETE(codec); |
| 182 | } else { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 183 | fBM.allocN32Pixels(1, 1); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 184 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 185 | } |
| 186 | fSize = fBM.height(); |
| 187 | } |
| 188 | private: |
| 189 | typedef DownsampleBitmapGM INHERITED; |
| 190 | }; |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////////////// |
| 193 | |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame] | 194 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); ) |
| 195 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); ) |
| 196 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); ) |
| 197 | |
| 198 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); ) |
| 199 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); ) |
| 200 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); ) |
| 201 | |
| 202 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); ) |
| 203 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); ) |
| 204 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); ) |
| 205 | |
| 206 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); ) |
| 207 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); ) |
| 208 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); ) |