SampleFilter.cpp revision d923288e50b5a69afb0bdd5c161191b24cab8345
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 "SampleCode.h" 9#include "SkView.h" 10#include "SkCanvas.h" 11#include "SkGradientShader.h" 12#include "SkPath.h" 13#include "SkRegion.h" 14#include "SkShader.h" 15#include "SkUtils.h" 16#include "Sk1DPathEffect.h" 17#include "SkCornerPathEffect.h" 18#include "SkPathMeasure.h" 19#include "SkRandom.h" 20#include "SkColorPriv.h" 21#include "SkColorFilter.h" 22#include "SkDither.h" 23 24static void make_bm(SkBitmap* bm) { 25 const SkPMColor colors[] = { 26 SkPreMultiplyColor(SK_ColorRED), SkPreMultiplyColor(SK_ColorGREEN), 27 SkPreMultiplyColor(SK_ColorBLUE), SkPreMultiplyColor(SK_ColorWHITE) 28 }; 29 SkColorTable* ctable = new SkColorTable(colors, 4); 30 bm->allocPixels(SkImageInfo::Make(2, 2, kIndex_8_SkColorType, 31 kOpaque_SkAlphaType), 32 NULL, ctable); 33 ctable->unref(); 34 35 *bm->getAddr8(0, 0) = 0; 36 *bm->getAddr8(1, 0) = 1; 37 *bm->getAddr8(0, 1) = 2; 38 *bm->getAddr8(1, 1) = 3; 39} 40 41static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm, 42 SkScalar x, SkScalar y, SkPaint* paint) { 43 canvas->drawBitmap(bm, x, y, paint); 44 return SkIntToScalar(bm.width()) * 5/4; 45} 46 47static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x, SkPaint* p) { 48 x += draw_bm(c, bm, x, 0, p); 49 p->setFilterLevel(SkPaint::kLow_FilterLevel); 50 x += draw_bm(c, bm, x, 0, p); 51 p->setDither(true); 52 return x + draw_bm(c, bm, x, 0, p); 53} 54 55static const char* gConfigNames[] = { 56 "unknown config", 57 "A8", 58 "Index8", 59 "565", 60 "4444", 61 "8888" 62}; 63 64static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) { 65 SkAutoCanvasRestore acr(canvas, true); 66 67 SkPaint paint; 68 SkScalar x = 0; 69 const int scale = 32; 70 71 paint.setAntiAlias(true); 72 const char* name = gConfigNames[bm.config()]; 73 canvas->drawText(name, strlen(name), x, SkIntToScalar(bm.height())*scale*5/8, 74 paint); 75 canvas->translate(SkIntToScalar(48), 0); 76 77 canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale)); 78 79 x += draw_set(canvas, bm, 0, &paint); 80 paint.reset(); 81 paint.setAlpha(0x80); 82 draw_set(canvas, bm, x, &paint); 83 return x * scale / 3; 84} 85 86class FilterView : public SampleView { 87public: 88 SkBitmap fBM8, fBM4444, fBM16, fBM32; 89 90 FilterView() { 91 make_bm(&fBM8); 92 fBM8.copyTo(&fBM4444, kARGB_4444_SkColorType); 93 fBM8.copyTo(&fBM16, kRGB_565_SkColorType); 94 fBM8.copyTo(&fBM32, kN32_SkColorType); 95 96 this->setBGColor(0xFFDDDDDD); 97 } 98 99protected: 100 // overrides from SkEventSink 101 virtual bool onQuery(SkEvent* evt) { 102 if (SampleCode::TitleQ(*evt)) { 103 SampleCode::TitleR(evt, "Filter"); 104 return true; 105 } 106 return this->INHERITED::onQuery(evt); 107 } 108 109 virtual void onDrawContent(SkCanvas* canvas) { 110 SkScalar x = SkIntToScalar(10); 111 SkScalar y = SkIntToScalar(10); 112 113 canvas->translate(x, y); 114 y = draw_row(canvas, fBM8); 115 canvas->translate(0, y); 116 y = draw_row(canvas, fBM4444); 117 canvas->translate(0, y); 118 y = draw_row(canvas, fBM16); 119 canvas->translate(0, y); 120 draw_row(canvas, fBM32); 121 } 122 123private: 124 typedef SampleView INHERITED; 125}; 126 127////////////////////////////////////////////////////////////////////////////// 128 129static SkView* MyFactory() { return new FilterView; } 130static SkViewRegister reg(MyFactory); 131