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 public: 82 SkBitmap fBM8, fBM4444, fBM16, fBM32; 83 84 FilterGM() { 85 make_bm(&fBM8); 86 fBM8.copyTo(&fBM4444, SkBitmap::kARGB_4444_Config); 87 fBM8.copyTo(&fBM16, SkBitmap::kRGB_565_Config); 88 fBM8.copyTo(&fBM32, SkBitmap::kARGB_8888_Config); 89 this->setBGColor(0xFFDDDDDD); 90 } 91 92 protected: 93 virtual SkString onShortName() { 94 return SkString("bitmapfilters"); 95 } 96 97 virtual SkISize onISize() { 98 return make_isize(540, 330); 99 } 100 101 virtual void onDraw(SkCanvas* canvas) { 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 127 128 129