SampleHairModes.cpp revision 0591897548c8fcb7d53cc94053e01702e58f1ac5
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 "SampleCode.h" 9#include "SkView.h" 10#include "SkCanvas.h" 11#include "SkColorPriv.h" 12#include "SkShader.h" 13 14static const struct { 15 SkBlendMode fMode; 16 const char* fLabel; 17} gModes[] = { 18 { SkBlendMode::kClear, "Clear" }, 19 { SkBlendMode::kSrc, "Src" }, 20 { SkBlendMode::kDst, "Dst" }, 21 { SkBlendMode::kSrcOver, "SrcOver" }, 22 { SkBlendMode::kDstOver, "DstOver" }, 23 { SkBlendMode::kSrcIn, "SrcIn" }, 24 { SkBlendMode::kDstIn, "DstIn" }, 25 { SkBlendMode::kSrcOut, "SrcOut" }, 26 { SkBlendMode::kDstOut, "DstOut" }, 27 { SkBlendMode::kSrcATop, "SrcATop" }, 28 { SkBlendMode::kDstATop, "DstATop" }, 29 { SkBlendMode::kXor, "Xor" }, 30}; 31 32const int gWidth = 64; 33const int gHeight = 64; 34const SkScalar W = SkIntToScalar(gWidth); 35const SkScalar H = SkIntToScalar(gHeight); 36 37static SkScalar drawCell(SkCanvas* canvas, SkBlendMode mode, SkAlpha a0, SkAlpha a1) { 38 SkPaint paint; 39 paint.setAntiAlias(true); 40 41 SkRect r = SkRect::MakeWH(W, H); 42 r.inset(W/10, H/10); 43 44 paint.setColor(SK_ColorBLUE); 45 paint.setAlpha(a0); 46 canvas->drawOval(r, paint); 47 48 paint.setColor(SK_ColorRED); 49 paint.setAlpha(a1); 50 paint.setBlendMode(mode); 51 for (int angle = 0; angle < 24; ++angle) { 52 SkScalar x = SkScalarCos(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gWidth; 53 SkScalar y = SkScalarSin(SkIntToScalar(angle) * (SK_ScalarPI * 2) / 24) * gHeight; 54 paint.setStrokeWidth(SK_Scalar1 * angle * 2 / 24); 55 canvas->drawLine(W/2, H/2, W/2 + x, H/2 + y, paint); 56 } 57 58 return H; 59} 60 61static sk_sp<SkShader> make_bg_shader() { 62 SkBitmap bm; 63 bm.allocN32Pixels(2, 2); 64 *bm.getAddr32(0, 0) = *bm.getAddr32(1, 1) = 0xFFFFFFFF; 65 *bm.getAddr32(1, 0) = *bm.getAddr32(0, 1) = SkPackARGB32(0xFF, 0xCC, 0xCC, 0xCC); 66 67 SkMatrix m; 68 m.setScale(SkIntToScalar(6), SkIntToScalar(6)); 69 70 return SkShader::MakeBitmapShader(bm, SkShader::kRepeat_TileMode, 71 SkShader::kRepeat_TileMode, &m); 72} 73 74class HairModesView : public SampleView { 75 SkPaint fBGPaint; 76public: 77 HairModesView() { 78 fBGPaint.setShader(make_bg_shader()); 79 } 80 81protected: 82 // overrides from SkEventSink 83 virtual bool onQuery(SkEvent* evt) { 84 if (SampleCode::TitleQ(*evt)) { 85 SampleCode::TitleR(evt, "HairlineModes"); 86 return true; 87 } 88 return this->INHERITED::onQuery(evt); 89 } 90 91 virtual void onDrawContent(SkCanvas* canvas) { 92 const SkRect bounds = SkRect::MakeWH(W, H); 93 static const SkAlpha gAlphaValue[] = { 0xFF, 0x88, 0x88 }; 94 95 canvas->translate(SkIntToScalar(4), SkIntToScalar(4)); 96 97 for (int alpha = 0; alpha < 4; ++alpha) { 98 canvas->save(); 99 canvas->save(); 100 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); ++i) { 101 if (6 == i) { 102 canvas->restore(); 103 canvas->translate(W * 5, 0); 104 canvas->save(); 105 } 106 canvas->drawRect(bounds, fBGPaint); 107 canvas->saveLayer(&bounds, nullptr); 108 SkScalar dy = drawCell(canvas, gModes[i].fMode, 109 gAlphaValue[alpha & 1], 110 gAlphaValue[alpha & 2]); 111 canvas->restore(); 112 113 canvas->translate(0, dy * 5 / 4); 114 } 115 canvas->restore(); 116 canvas->restore(); 117 canvas->translate(W * 5 / 4, 0); 118 } 119 } 120 121private: 122 typedef SampleView INHERITED; 123}; 124 125/////////////////////////////////////////////////////////////////////////////// 126 127static SkView* MyFactory() { return new HairModesView; } 128static SkViewRegister reg(MyFactory); 129