SampleLayers.cpp revision 8a1c16ff38322f0210116fa7293eb8817c7e477e
1#include "SampleCode.h" 2#include "SkView.h" 3#include "SkCanvas.h" 4#include "SkBlurMaskFilter.h" 5#include "SkCamera.h" 6#include "SkColorFilter.h" 7#include "SkColorPriv.h" 8#include "SkDevice.h" 9#include "SkGradientShader.h" 10#include "SkImageDecoder.h" 11#include "SkInterpolator.h" 12#include "SkMaskFilter.h" 13#include "SkPath.h" 14#include "SkRegion.h" 15#include "SkShader.h" 16#include "SkShaderExtras.h" 17#include "SkTime.h" 18#include "SkTypeface.h" 19#include "SkUtils.h" 20#include "SkKey.h" 21#include "SkPorterDuff.h" 22#include "SkXfermode.h" 23#include "SkDrawFilter.h" 24 25static void make_paint(SkPaint* paint) { 26 SkColor colors[] = { 0, SK_ColorWHITE }; 27 SkPoint pts[] = { 0, 0, 0, SK_Scalar1*20 }; 28 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); 29 30 paint->setShader(s)->unref(); 31 paint->setXfermode(SkPorterDuff::CreateXfermode(SkPorterDuff::kDstIn_Mode))->unref(); 32} 33 34static void dump_layers(const char label[], SkCanvas* canvas) { 35 SkDebugf("Dump Layers(%s)\n", label); 36 37 SkCanvas::LayerIter iter(canvas, true); 38 int index = 0; 39 while (!iter.done()) { 40 const SkBitmap& bm = iter.device()->accessBitmap(false); 41 const SkIRect& clip = iter.clip().getBounds(); 42 SkDebugf("Layer[%d] bitmap [%d %d] X=%d Y=%d clip=[%d %d %d %d] alpha=%d\n", index++, 43 bm.width(), bm.height(), iter.x(), iter.y(), 44 clip.fLeft, clip.fTop, clip.fRight, clip.fBottom, 45 iter.paint().getAlpha()); 46 iter.next(); 47 } 48} 49 50// test drawing with strips of fading gradient above and below 51static void test_fade(SkCanvas* canvas) { 52 SkAutoCanvasRestore ar(canvas, true); 53 54 SkRect r; 55 56 SkPaint p; 57 p.setAlpha(0x88); 58 59 SkAutoCanvasRestore(canvas, false); 60 61 // create the layers 62 63 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); 64 canvas->clipRect(r); 65 66 r.fBottom = SkIntToScalar(20); 67 canvas->saveLayer(&r, NULL, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag)); 68 69 r.fTop = SkIntToScalar(80); 70 r.fBottom = SkIntToScalar(100); 71 canvas->saveLayer(&r, NULL, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag)); 72 73 // now draw the "content" 74 75 if (true) { 76 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); 77 78 canvas->saveLayerAlpha(&r, 0x80); 79 80 SkPaint p; 81 p.setColor(SK_ColorRED); 82 p.setAntiAlias(true); 83 canvas->drawOval(r, p); 84 85 dump_layers("inside layer alpha", canvas); 86 87 canvas->restore(); 88 } else { 89 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(100)); 90 91 SkPaint p; 92 p.setColor(SK_ColorRED); 93 p.setAntiAlias(true); 94 canvas->drawOval(r, p); 95 } 96 97// return; 98 99 dump_layers("outside layer alpha", canvas); 100 101 // now apply an effect 102 103 SkPaint paint; 104 make_paint(&paint); 105 r.set(0, 0, SkIntToScalar(100), SkIntToScalar(20)); 106// SkDebugf("--------- draw top grad\n"); 107 canvas->drawRect(r, paint); 108 109 SkMatrix m; 110 SkShader* s = paint.getShader(); 111 m.setScale(SK_Scalar1, -SK_Scalar1); 112 m.postTranslate(0, SkIntToScalar(100)); 113 s->setLocalMatrix(m); 114 115 r.fTop = SkIntToScalar(80); 116 r.fBottom = SkIntToScalar(100); 117// SkDebugf("--------- draw bot grad\n"); 118 canvas->drawRect(r, paint); 119} 120 121class RedFilter : public SkDrawFilter { 122public: 123 virtual bool filter(SkCanvas*, SkPaint* p, SkDrawFilter::Type) { 124 fColor = p->getColor(); 125 if (fColor == SK_ColorRED) { 126 p->setColor(SK_ColorGREEN); 127 } 128 return true; 129 } 130 virtual void restore(SkCanvas*, SkPaint* p, SkDrawFilter::Type) { 131 p->setColor(fColor); 132 } 133 134private: 135 SkColor fColor; 136}; 137 138class LayersView : public SkView { 139public: 140 LayersView() {} 141 142protected: 143 // overrides from SkEventSink 144 virtual bool onQuery(SkEvent* evt) { 145 if (SampleCode::TitleQ(*evt)) { 146 SampleCode::TitleR(evt, "Layers"); 147 return true; 148 } 149 return this->INHERITED::onQuery(evt); 150 } 151 152 void drawBG(SkCanvas* canvas) { 153 canvas->drawColor(SK_ColorWHITE); 154 } 155 156 virtual void onDraw(SkCanvas* canvas) { 157 this->drawBG(canvas); 158 159 if (false) { 160 SkRect r; 161 r.set(SkIntToScalar(0), SkIntToScalar(0), 162 SkIntToScalar(220), SkIntToScalar(120)); 163 SkPaint p; 164 p.setAlpha(0x88); 165 p.setAntiAlias(true); 166 167 if (true) { 168 canvas->saveLayer(&r, &p); 169 p.setColor(0xFFFF0000); 170 canvas->drawOval(r, p); 171 canvas->restore(); 172 } 173 174 p.setColor(0xFF0000FF); 175 r.offset(SkIntToScalar(20), SkIntToScalar(50)); 176 canvas->drawOval(r, p); 177 } 178 179 if (false) { 180 SkPaint p; 181 p.setAlpha(0x88); 182 p.setAntiAlias(true); 183 184 canvas->translate(SkIntToScalar(300), 0); 185 186 SkRect r; 187 r.set(SkIntToScalar(0), SkIntToScalar(0), 188 SkIntToScalar(220), SkIntToScalar(60)); 189 190 canvas->saveLayer(&r, &p, (SkCanvas::SaveFlags)(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag)); 191// canvas->clipRect(r, SkRegion::kDifference_Op); 192// canvas->clipRect(r, SkRegion::kIntersect_Op); 193 194 r.set(SkIntToScalar(0), SkIntToScalar(0), 195 SkIntToScalar(220), SkIntToScalar(120)); 196 p.setColor(SK_ColorBLUE); 197 canvas->drawOval(r, p); 198 canvas->restore(); 199 return; 200 } 201 202 //canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 203 test_fade(canvas); 204 return; 205 206 // canvas->setDrawFilter(new RedFilter)->unref(); 207 208 SkRect r; 209 SkPaint p; 210 211 canvas->translate(SkIntToScalar(220), SkIntToScalar(20)); 212 213 p.setAntiAlias(true); 214 r.set(SkIntToScalar(20), SkIntToScalar(20), 215 SkIntToScalar(220), SkIntToScalar(120)); 216 217 p.setColor(SK_ColorBLUE); 218 // p.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(8), SkBlurMaskFilter::kNormal_BlurStyle))->unref(); 219 canvas->drawRect(r, p); 220 p.setMaskFilter(NULL); 221 222 SkRect bounds = r; 223 bounds.fBottom = bounds.centerY(); 224 canvas->saveLayer(&bounds, NULL, SkCanvas::kARGB_NoClipLayer_SaveFlag); 225 226 p.setColor(SK_ColorRED); 227 canvas->drawOval(r, p); 228 229 p.setAlpha(0x80); 230 p.setPorterDuffXfermode(SkPorterDuff::kDstIn_Mode); 231 canvas->drawRect(bounds, p); 232 233 canvas->restore(); 234 } 235 236 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { 237 this->inval(NULL); 238 239 return this->INHERITED::onFindClickHandler(x, y); 240 } 241 242 virtual bool onClick(Click* click) { 243 return this->INHERITED::onClick(click); 244 } 245 246 virtual bool handleKey(SkKey key) { 247 this->inval(NULL); 248 return true; 249 } 250 251private: 252 typedef SkView INHERITED; 253}; 254 255////////////////////////////////////////////////////////////////////////////// 256 257static SkView* MyFactory() { return new LayersView; } 258static SkViewRegister reg(MyFactory); 259 260