ClockFaceView.cpp revision c73dd5c6880739f26216f198c757028fd28df1a4
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 "SkFlattenableBuffers.h" 12#include "SkGradientShader.h" 13#include "SkPath.h" 14#include "SkRegion.h" 15#include "SkShader.h" 16#include "SkUtils.h" 17#include "SkColorPriv.h" 18#include "SkColorFilter.h" 19#include "SkTypeface.h" 20#include "SkAvoidXfermode.h" 21 22static inline SkPMColor rgb2gray(SkPMColor c) 23{ 24 unsigned r = SkGetPackedR32(c); 25 unsigned g = SkGetPackedG32(c); 26 unsigned b = SkGetPackedB32(c); 27 28 unsigned x = (r * 5 + g * 7 + b * 4) >> 4; 29 30 return SkPackARGB32(0, x, x, x) | (c & (SK_A32_MASK << SK_A32_SHIFT)); 31} 32 33class SkGrayScaleColorFilter : public SkColorFilter { 34public: 35 virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) 36 { 37 for (int i = 0; i < count; i++) 38 result[i] = rgb2gray(src[i]); 39 } 40}; 41 42class SkChannelMaskColorFilter : public SkColorFilter { 43public: 44 SkChannelMaskColorFilter(U8CPU redMask, U8CPU greenMask, U8CPU blueMask) 45 { 46 fMask = SkPackARGB32(0xFF, redMask, greenMask, blueMask); 47 } 48 49 virtual void filterSpan(const SkPMColor src[], int count, SkPMColor result[]) 50 { 51 SkPMColor mask = fMask; 52 for (int i = 0; i < count; i++) 53 result[i] = src[i] & mask; 54 } 55 56private: 57 SkPMColor fMask; 58}; 59 60/////////////////////////////////////////////////////////// 61 62#include "SkGradientShader.h" 63#include "SkLayerRasterizer.h" 64#include "SkBlurMaskFilter.h" 65 66#include "Sk2DPathEffect.h" 67 68class Dot2DPathEffect : public Sk2DPathEffect { 69public: 70 Dot2DPathEffect(SkScalar radius, const SkMatrix& matrix, 71 SkTDArray<SkPoint>* pts) 72 : Sk2DPathEffect(matrix), fRadius(radius), fPts(pts) {} 73 74 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Dot2DPathEffect) 75 76protected: 77 virtual void begin(const SkIRect& uvBounds, SkPath* dst) { 78 if (fPts) { 79 fPts->reset(); 80 } 81 this->INHERITED::begin(uvBounds, dst); 82 } 83// virtual void end(SkPath* dst) {} 84 virtual void next(const SkPoint& loc, int u, int v, SkPath* dst) 85 { 86 if (fPts) { 87 *fPts->append() = loc; 88 } 89 dst->addCircle(loc.fX, loc.fY, fRadius); 90 } 91 92 Dot2DPathEffect(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) 93 { 94 fRadius = buffer.readScalar(); 95 fPts = NULL; 96 } 97 virtual void flatten(SkFlattenableWriteBuffer& buffer) const SK_OVERRIDE { 98 this->INHERITED::flatten(buffer); 99 buffer.writeScalar(fRadius); 100 } 101 102private: 103 SkScalar fRadius; 104 SkTDArray<SkPoint>* fPts; 105 106 typedef Sk2DPathEffect INHERITED; 107}; 108 109class InverseFillPE : public SkPathEffect { 110public: 111 InverseFillPE() {} 112 virtual bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*) SK_OVERRIDE { 113 *dst = src; 114 dst->setFillType(SkPath::kInverseWinding_FillType); 115 return true; 116 } 117 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(InverseFillPE) 118 119protected: 120 InverseFillPE(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {} 121private: 122 123 typedef SkPathEffect INHERITED; 124}; 125 126static SkPathEffect* makepe(float interp, SkTDArray<SkPoint>* pts) { 127 SkMatrix lattice; 128 SkScalar rad = 3 + SkIntToScalar(4) * (1 - interp); 129 lattice.setScale(rad*2, rad*2, 0, 0); 130 lattice.postSkew(SK_Scalar1/3, 0, 0, 0); 131 return new Dot2DPathEffect(rad, lattice, pts); 132} 133 134static void r7(SkLayerRasterizer* rast, SkPaint& p, SkScalar interp) { 135 p.setPathEffect(makepe(SkScalarToFloat(interp), NULL))->unref(); 136 rast->addLayer(p); 137#if 0 138 p.setPathEffect(new InverseFillPE())->unref(); 139 p.setXfermodeMode(SkXfermode::kSrcIn_Mode); 140 p.setXfermodeMode(SkXfermode::kClear_Mode); 141 p.setAlpha((1 - interp) * 255); 142 rast->addLayer(p); 143#endif 144} 145 146typedef void (*raster_proc)(SkLayerRasterizer*, SkPaint&); 147 148#include "SkXfermode.h" 149 150static void apply_shader(SkPaint* paint, float scale) 151{ 152 SkPaint p; 153 SkLayerRasterizer* rast = new SkLayerRasterizer; 154 155 p.setAntiAlias(true); 156 r7(rast, p, SkFloatToScalar(scale)); 157 paint->setRasterizer(rast)->unref(); 158 159 paint->setColor(SK_ColorBLUE); 160} 161 162class ClockFaceView : public SkView { 163 SkTypeface* fFace; 164 SkScalar fInterp; 165 SkScalar fDx; 166public: 167 ClockFaceView() 168 { 169 fFace = SkTypeface::CreateFromFile("/Users/reed/Downloads/p052024l.pfb"); 170 fInterp = 0; 171 fDx = SK_Scalar1/64; 172 } 173 174 virtual ~ClockFaceView() 175 { 176 SkSafeUnref(fFace); 177 } 178 179protected: 180 // overrides from SkEventSink 181 virtual bool onQuery(SkEvent* evt) 182 { 183 if (SampleCode::TitleQ(*evt)) 184 { 185 SampleCode::TitleR(evt, "Text Effects"); 186 return true; 187 } 188 return this->INHERITED::onQuery(evt); 189 } 190 191 void drawBG(SkCanvas* canvas) 192 { 193// canvas->drawColor(0xFFDDDDDD); 194 canvas->drawColor(SK_ColorWHITE); 195 } 196 197 static void drawdots(SkCanvas* canvas, const SkPaint& orig) { 198 SkTDArray<SkPoint> pts; 199 SkPathEffect* pe = makepe(0, &pts); 200 201 SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); 202 SkPath path, dstPath; 203 orig.getTextPath("9", 1, 0, 0, &path); 204 pe->filterPath(&dstPath, path, &rec); 205 206 SkPaint p; 207 p.setAntiAlias(true); 208 p.setStrokeWidth(10); 209 p.setColor(SK_ColorRED); 210 canvas->drawPoints(SkCanvas::kPoints_PointMode, pts.count(), pts.begin(), 211 p); 212 } 213 214 virtual void onDraw(SkCanvas* canvas) { 215 this->drawBG(canvas); 216 217 SkScalar x = SkIntToScalar(20); 218 SkScalar y = SkIntToScalar(300); 219 SkPaint paint; 220 221 paint.setAntiAlias(true); 222 paint.setTextSize(SkIntToScalar(240)); 223 paint.setTypeface(SkTypeface::CreateFromName("sans-serif", 224 SkTypeface::kBold)); 225 226 SkString str("9"); 227 228 paint.setTypeface(fFace); 229 230 apply_shader(&paint, SkScalarToFloat(fInterp)); 231 canvas->drawText(str.c_str(), str.size(), x, y, paint); 232 233 // drawdots(canvas, paint); 234 235 if (false) { 236 fInterp += fDx; 237 if (fInterp > 1) { 238 fInterp = 1; 239 fDx = -fDx; 240 } else if (fInterp < 0) { 241 fInterp = 0; 242 fDx = -fDx; 243 } 244 this->inval(NULL); 245 } 246 } 247 248private: 249 typedef SkView INHERITED; 250}; 251 252////////////////////////////////////////////////////////////////////////////// 253 254static SkView* MyFactory() { return new ClockFaceView; } 255static SkViewRegister reg(MyFactory); 256 257