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" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | #include "SkTypeface.h" |
| 12 | #include "SkImageDecoder.h" |
| 13 | #include "SkStream.h" |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 14 | #include "SkPaint.h" |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 15 | |
| 16 | static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) { |
| 17 | SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style))); |
| 18 | } |
| 19 | |
| 20 | class DownsampleBitmapGM : public skiagm::GM { |
| 21 | |
| 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 | |
| 35 | void setName(const char name[]) { |
| 36 | fName.set(name); |
| 37 | } |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 38 | |
| 39 | const char *filterLevelToString() { |
| 40 | switch(fFilterLevel) { |
| 41 | case SkPaint::kNone_FilterLevel: |
| 42 | return "none"; |
| 43 | case SkPaint::kLow_FilterLevel: |
| 44 | return "low"; |
| 45 | case SkPaint::kMedium_FilterLevel: |
| 46 | return "medium"; |
| 47 | case SkPaint::kHigh_FilterLevel: |
| 48 | return "high"; |
| 49 | default: |
| 50 | return "unknown"; |
| 51 | } |
| 52 | } |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 53 | |
| 54 | protected: |
| 55 | virtual SkString onShortName() SK_OVERRIDE { |
| 56 | return fName; |
| 57 | } |
| 58 | |
| 59 | virtual SkISize onISize() SK_OVERRIDE { |
| 60 | make_bitmap_wrapper(); |
| 61 | return SkISize::Make(4 * fBM.width(), fBM.height()); |
| 62 | } |
| 63 | |
| 64 | void make_bitmap_wrapper() { |
| 65 | if (!fBitmapMade) { |
| 66 | fBitmapMade = true; |
| 67 | make_bitmap(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | virtual void make_bitmap() = 0; |
| 72 | |
| 73 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 74 | make_bitmap_wrapper(); |
| 75 | |
| 76 | int curX = 0; |
| 77 | int curWidth; |
| 78 | float curScale = 1; |
| 79 | do { |
| 80 | |
| 81 | SkMatrix matrix; |
| 82 | matrix.setScale( curScale, curScale ); |
| 83 | |
| 84 | SkPaint paint; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 85 | paint.setFilterLevel(fFilterLevel); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 86 | |
| 87 | canvas->save(); |
| 88 | canvas->translate( (SkScalar) curX, 0.f ); |
| 89 | canvas->drawBitmapMatrix( fBM, matrix, &paint ); |
| 90 | canvas->restore(); |
| 91 | |
| 92 | curWidth = (int) (fBM.width() * curScale + 2); |
| 93 | curX += curWidth; |
| 94 | curScale *= 0.75f; |
humper@google.com | 138ebc3 | 2013-07-19 20:20:04 +0000 | [diff] [blame] | 95 | } while (curWidth >= 2 && curX < 4*fBM.width()); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private: |
| 99 | typedef skiagm::GM INHERITED; |
| 100 | }; |
| 101 | |
| 102 | class DownsampleBitmapTextGM: public DownsampleBitmapGM { |
| 103 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 104 | DownsampleBitmapTextGM(float textSize, SkPaint::FilterLevel filterLevel) |
| 105 | : INHERITED(filterLevel), fTextSize(textSize) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 106 | { |
| 107 | char name[1024]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 108 | sprintf(name, "downsamplebitmap_text_%s_%.2fpt", this->filterLevelToString(), fTextSize); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 109 | setName(name); |
| 110 | } |
| 111 | |
| 112 | protected: |
| 113 | float fTextSize; |
| 114 | |
| 115 | virtual void make_bitmap() SK_OVERRIDE { |
| 116 | fBM.setConfig(SkBitmap::kARGB_8888_Config, int(fTextSize * 8), int(fTextSize * 6)); |
| 117 | fBM.allocPixels(); |
| 118 | SkCanvas canvas(fBM); |
| 119 | canvas.drawColor(SK_ColorWHITE); |
| 120 | |
| 121 | SkPaint paint; |
| 122 | paint.setAntiAlias(true); |
| 123 | paint.setSubpixelText(true); |
| 124 | paint.setTextSize(fTextSize); |
| 125 | |
| 126 | setTypeface(&paint, "Times", SkTypeface::kNormal); |
| 127 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 1.2f*fTextSize, paint); |
| 128 | setTypeface(&paint, "Times", SkTypeface::kBold); |
| 129 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 2.4f*fTextSize, paint); |
| 130 | setTypeface(&paint, "Times", SkTypeface::kItalic); |
| 131 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 3.6f*fTextSize, paint); |
| 132 | setTypeface(&paint, "Times", SkTypeface::kBoldItalic); |
| 133 | canvas.drawText("Hamburgefons", 12, fTextSize/2, 4.8f*fTextSize, paint); |
| 134 | } |
| 135 | private: |
| 136 | typedef DownsampleBitmapGM INHERITED; |
| 137 | }; |
| 138 | |
| 139 | class DownsampleBitmapCheckerboardGM: public DownsampleBitmapGM { |
| 140 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 141 | DownsampleBitmapCheckerboardGM(int size, int numChecks, SkPaint::FilterLevel filterLevel) |
| 142 | : INHERITED(filterLevel), fSize(size), fNumChecks(numChecks) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 143 | { |
| 144 | char name[1024]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 145 | sprintf(name, "downsamplebitmap_checkerboard_%s_%d_%d", this->filterLevelToString(), fSize, fNumChecks); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 146 | setName(name); |
| 147 | } |
| 148 | |
| 149 | protected: |
| 150 | int fSize; |
| 151 | int fNumChecks; |
| 152 | |
| 153 | virtual void make_bitmap() SK_OVERRIDE { |
| 154 | fBM.setConfig(SkBitmap::kARGB_8888_Config, fSize, fSize); |
| 155 | fBM.allocPixels(); |
| 156 | SkAutoLockPixels lock(fBM); |
| 157 | for (int y = 0; y < fSize; ++y) { |
| 158 | for (int x = 0; x < fSize; ++x) { |
| 159 | SkPMColor* s = fBM.getAddr32(x, y); |
| 160 | int cx = (x * fNumChecks) / fSize; |
| 161 | int cy = (y * fNumChecks) / fSize; |
| 162 | if ((cx+cy)%2) { |
| 163 | *s = 0xFFFFFFFF; |
| 164 | } else { |
| 165 | *s = 0xFF000000; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | private: |
| 171 | typedef DownsampleBitmapGM INHERITED; |
| 172 | }; |
| 173 | |
| 174 | class DownsampleBitmapImageGM: public DownsampleBitmapGM { |
| 175 | public: |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 176 | DownsampleBitmapImageGM(const char filename[], SkPaint::FilterLevel filterLevel) |
| 177 | : INHERITED(filterLevel), fFilename(filename) |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 178 | { |
| 179 | char name[1024]; |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 180 | sprintf(name, "downsamplebitmap_image_%s_%s", this->filterLevelToString(), filename); |
humper@google.com | 9c96d4b | 2013-07-14 01:44:59 +0000 | [diff] [blame] | 181 | setName(name); |
| 182 | } |
| 183 | |
| 184 | protected: |
| 185 | SkString fFilename; |
| 186 | int fSize; |
| 187 | |
| 188 | virtual void make_bitmap() SK_OVERRIDE { |
| 189 | SkString path(skiagm::GM::gResourcePath); |
| 190 | path.append("/"); |
| 191 | path.append(fFilename); |
| 192 | |
| 193 | SkImageDecoder *codec = NULL; |
| 194 | SkFILEStream stream(path.c_str()); |
| 195 | if (stream.isValid()) { |
| 196 | codec = SkImageDecoder::Factory(&stream); |
| 197 | } |
| 198 | if (codec) { |
| 199 | stream.rewind(); |
| 200 | codec->decode(&stream, &fBM, SkBitmap::kARGB_8888_Config, |
| 201 | SkImageDecoder::kDecodePixels_Mode); |
| 202 | SkDELETE(codec); |
| 203 | } else { |
| 204 | fBM.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 205 | fBM.allocPixels(); |
| 206 | *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad |
| 207 | } |
| 208 | fSize = fBM.height(); |
| 209 | } |
| 210 | private: |
| 211 | typedef DownsampleBitmapGM INHERITED; |
| 212 | }; |
| 213 | |
| 214 | ////////////////////////////////////////////////////////////////////////////// |
| 215 | |
humper@google.com | af42a03 | 2013-07-25 14:34:13 +0000 | [diff] [blame^] | 216 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kHigh_FilterLevel); ) |
| 217 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kHigh_FilterLevel); ) |
| 218 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kHigh_FilterLevel); ) |
| 219 | |
| 220 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kMedium_FilterLevel); ) |
| 221 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kMedium_FilterLevel); ) |
| 222 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kMedium_FilterLevel); ) |
| 223 | |
| 224 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kLow_FilterLevel); ) |
| 225 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kLow_FilterLevel); ) |
| 226 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kLow_FilterLevel); ) |
| 227 | |
| 228 | DEF_GM( return new DownsampleBitmapTextGM(72, SkPaint::kNone_FilterLevel); ) |
| 229 | DEF_GM( return new DownsampleBitmapCheckerboardGM(512,256, SkPaint::kNone_FilterLevel); ) |
| 230 | DEF_GM( return new DownsampleBitmapImageGM("mandrill_512.png", SkPaint::kNone_FilterLevel); ) |