epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | |
| 10 | namespace skiagm { |
| 11 | |
| 12 | static void make_bm(SkBitmap* bm) { |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 13 | const SkColor colors[4] = { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 14 | SK_ColorRED, SK_ColorGREEN, |
| 15 | SK_ColorBLUE, SK_ColorWHITE |
| 16 | }; |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 17 | SkPMColor colorsPM[4]; |
| 18 | for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) { |
| 19 | colorsPM[i] = SkPreMultiplyColor(colors[i]); |
| 20 | } |
| 21 | SkColorTable* ctable = new SkColorTable(colorsPM, 4); |
| 22 | |
commit-bot@chromium.org | dac5225 | 2014-02-17 21:21:46 +0000 | [diff] [blame] | 23 | bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType, |
| 24 | kPremul_SkAlphaType), |
| 25 | NULL, ctable); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 26 | ctable->unref(); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 27 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 28 | *bm->getAddr8(0, 0) = 0; |
| 29 | *bm->getAddr8(1, 0) = 1; |
| 30 | *bm->getAddr8(0, 1) = 2; |
| 31 | *bm->getAddr8(1, 1) = 3; |
| 32 | } |
| 33 | |
| 34 | static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, |
| 35 | SkScalar x, SkScalar y, SkPaint* paint) { |
| 36 | canvas->drawBitmap(bm, x, y, paint); |
| 37 | return SkIntToScalar(bm.width()) * 5/4; |
| 38 | } |
| 39 | |
| 40 | static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, |
| 41 | SkPaint* p) { |
| 42 | x += draw_bm(c, bm, x, 0, p); |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 43 | p->setFilterLevel(SkPaint::kLow_FilterLevel); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 44 | x += draw_bm(c, bm, x, 0, p); |
| 45 | p->setDither(true); |
| 46 | return x + draw_bm(c, bm, x, 0, p); |
| 47 | } |
| 48 | |
| 49 | static const char* gConfigNames[] = { |
| 50 | "unknown config", |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 51 | "A8", |
| 52 | "Index8", |
| 53 | "565", |
| 54 | "4444", |
| 55 | "8888" |
| 56 | }; |
| 57 | |
| 58 | static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) { |
| 59 | SkAutoCanvasRestore acr(canvas, true); |
| 60 | |
| 61 | SkPaint paint; |
| 62 | SkScalar x = 0; |
| 63 | const int scale = 32; |
| 64 | |
| 65 | paint.setAntiAlias(true); |
| 66 | const char* name = gConfigNames[bm.config()]; |
| 67 | canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8, |
| 68 | paint); |
| 69 | canvas->translate(SkIntToScalar(48), 0); |
| 70 | |
| 71 | canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 72 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 73 | x += draw_set(canvas, bm, 0, &paint); |
| 74 | paint.reset(); |
| 75 | paint.setAlpha(0x80); |
| 76 | draw_set(canvas, bm, x, &paint); |
| 77 | return x * scale / 3; |
| 78 | } |
| 79 | |
| 80 | class FilterGM : public GM { |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 81 | bool fOnce; |
| 82 | void init() { |
| 83 | if (fOnce) { |
| 84 | return; |
| 85 | } |
| 86 | fOnce = true; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 87 | make_bm(&fBM8); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 88 | fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType); |
| 89 | fBM8.copyTo(&fBM16, kRGB_565_SkColorType); |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame] | 90 | fBM8.copyTo(&fBM32, kN32_SkColorType); |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 91 | } |
| 92 | public: |
| 93 | SkBitmap fBM8, fBM4444, fBM16, fBM32; |
| 94 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | FilterGM() : fOnce(false) { |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 96 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | protected: |
| 100 | virtual SkString onShortName() { |
| 101 | return SkString("bitmapfilters"); |
| 102 | } |
| 103 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 104 | virtual SkISize onISize() { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 105 | return make_isize(540, 330); |
| 106 | } |
| 107 | |
| 108 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 26b73d3 | 2012-03-08 22:13:04 +0000 | [diff] [blame] | 109 | this->init(); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 110 | |
| 111 | SkScalar x = SkIntToScalar(10); |
| 112 | SkScalar y = SkIntToScalar(10); |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 113 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 114 | canvas->translate(x, y); |
| 115 | y = draw_row(canvas, fBM8); |
| 116 | canvas->translate(0, y); |
| 117 | y = draw_row(canvas, fBM4444); |
| 118 | canvas->translate(0, y); |
| 119 | y = draw_row(canvas, fBM16); |
| 120 | canvas->translate(0, y); |
| 121 | draw_row(canvas, fBM32); |
| 122 | } |
bungeman@google.com | 2ed67e8 | 2011-05-18 18:54:23 +0000 | [diff] [blame] | 123 | |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 124 | private: |
| 125 | typedef GM INHERITED; |
| 126 | }; |
| 127 | |
| 128 | ////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | static GM* MyFactory(void*) { return new FilterGM; } |
| 131 | static GMRegistry reg(MyFactory); |
| 132 | |
| 133 | } |