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" 9 #include "sk_tool_utils.h" 10 11 namespace skiagm { 12 13 static void make_bm(SkBitmap* bm) { 14 const SkColor colors[4] = { 15 SK_ColorRED, SK_ColorGREEN, 16 SK_ColorBLUE, SK_ColorWHITE 17 }; 18 SkPMColor colorsPM[4]; 19 for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) { 20 colorsPM[i] = SkPreMultiplyColor(colors[i]); 21 } 22 SkColorTable* ctable = new SkColorTable(colorsPM, 4); 23 24 bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType, 25 kPremul_SkAlphaType), 26 NULL, ctable); 27 ctable->unref(); 28 29 *bm->getAddr8(0, 0) = 0; 30 *bm->getAddr8(1, 0) = 1; 31 *bm->getAddr8(0, 1) = 2; 32 *bm->getAddr8(1, 1) = 3; 33 } 34 35 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, 36 SkScalar x, SkScalar y, SkPaint* paint) { 37 canvas->drawBitmap(bm, x, y, paint); 38 return SkIntToScalar(bm.width()) * 5/4; 39 } 40 41 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, 42 SkPaint* p) { 43 x += draw_bm(c, bm, x, 0, p); 44 p->setFilterLevel(SkPaint::kLow_FilterLevel); 45 x += draw_bm(c, bm, x, 0, p); 46 p->setDither(true); 47 return x + draw_bm(c, bm, x, 0, p); 48 } 49 50 static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) { 51 SkAutoCanvasRestore acr(canvas, true); 52 53 SkPaint paint; 54 SkScalar x = 0; 55 const int scale = 32; 56 57 paint.setAntiAlias(true); 58 const char* name = sk_tool_utils::colortype_name(bm.colorType()); 59 canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8, 60 paint); 61 canvas->translate(SkIntToScalar(48), 0); 62 63 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); 64 65 x += draw_set(canvas, bm, 0, &paint); 66 paint.reset(); 67 paint.setAlpha(0x80); 68 draw_set(canvas, bm, x, &paint); 69 return x * scale / 3; 70 } 71 72 class FilterGM : public GM { 73 bool fOnce; 74 void init() { 75 if (fOnce) { 76 return; 77 } 78 fOnce = true; 79 make_bm(&fBM8); 80 fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType); 81 fBM8.copyTo(&fBM16, kRGB_565_SkColorType); 82 fBM8.copyTo(&fBM32, kN32_SkColorType); 83 } 84 public: 85 SkBitmap fBM8, fBM4444, fBM16, fBM32; 86 87 FilterGM() : fOnce(false) { 88 this->setBGColor(0xFFDDDDDD); 89 } 90 91 protected: 92 virtual SkString onShortName() { 93 return SkString("bitmapfilters"); 94 } 95 96 virtual SkISize onISize() { 97 return SkISize::Make(540, 330); 98 } 99 100 virtual void onDraw(SkCanvas* canvas) { 101 this->init(); 102 103 SkScalar x = SkIntToScalar(10); 104 SkScalar y = SkIntToScalar(10); 105 106 canvas->translate(x, y); 107 y = draw_row(canvas, fBM8); 108 canvas->translate(0, y); 109 y = draw_row(canvas, fBM4444); 110 canvas->translate(0, y); 111 y = draw_row(canvas, fBM16); 112 canvas->translate(0, y); 113 draw_row(canvas, fBM32); 114 } 115 116 private: 117 typedef GM INHERITED; 118 }; 119 120 ////////////////////////////////////////////////////////////////////////////// 121 122 static GM* MyFactory(void*) { return new FilterGM; } 123 static GMRegistry reg(MyFactory); 124 125 } 126