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 */ 8 #include "gm.h" 9 10 namespace skiagm { 11 12 static void make_bm(SkBitmap* bm) { 13 const SkColor colors[4] = { 14 SK_ColorRED, SK_ColorGREEN, 15 SK_ColorBLUE, SK_ColorWHITE 16 }; 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 23 bm->setConfig(SkBitmap::kIndex8_Config, 2, 2); 24 bm->allocPixels(ctable); 25 ctable->unref(); 26 27 *bm->getAddr8(0, 0) = 0; 28 *bm->getAddr8(1, 0) = 1; 29 *bm->getAddr8(0, 1) = 2; 30 *bm->getAddr8(1, 1) = 3; 31 } 32 33 static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, 34 SkScalar x, SkScalar y, SkPaint* paint) { 35 canvas->drawBitmap(bm, x, y, paint); 36 return SkIntToScalar(bm.width()) * 5/4; 37 } 38 39 static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, 40 SkPaint* p) { 41 x += draw_bm(c, bm, x, 0, p); 42 p->setFilterBitmap(true); 43 x += draw_bm(c, bm, x, 0, p); 44 p->setDither(true); 45 return x + draw_bm(c, bm, x, 0, p); 46 } 47 48 static const char* gConfigNames[] = { 49 "unknown config", 50 "A1", 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)); 72 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 { 81 bool fOnce; 82 void init() { 83 if (fOnce) { 84 return; 85 } 86 fOnce = true; 87 make_bm(&fBM8); 88 fBM8.copyTo(&fBM4444, SkBitmap::kARGB_4444_Config); 89 fBM8.copyTo(&fBM16, SkBitmap::kRGB_565_Config); 90 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); 91 } 92 public: 93 SkBitmap fBM8, fBM4444, fBM16, fBM32; 94 95 FilterGM() : fOnce(false) { 96 this->setBGColor(0xFFDDDDDD); 97 } 98 99 protected: 100 virtual SkString onShortName() { 101 return SkString("bitmapfilters"); 102 } 103 104 virtual SkISize onISize() { 105 return make_isize(540, 330); 106 } 107 108 virtual void onDraw(SkCanvas* canvas) { 109 this->init(); 110 111 SkScalar x = SkIntToScalar(10); 112 SkScalar y = SkIntToScalar(10); 113 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 } 123 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 } 134