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