Home | History | Annotate | Download | only in gm
      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 "gm.h"
      9 #include "SkCanvas.h"
     10 //#include "SkParsePath.h"
     11 #include "SkPath.h"
     12 //#include "SkRandom.h"
     13 
     14 namespace skiagm {
     15 
     16 static const SkColor gPathColor = SK_ColorBLACK;
     17 static const SkColor gClipAColor = SK_ColorBLUE;
     18 static const SkColor gClipBColor = SK_ColorRED;
     19 
     20 class ComplexClipGM : public GM {
     21     bool fDoAAClip;
     22     bool fDoSaveLayer;
     23 public:
     24     ComplexClipGM(bool aaclip, bool saveLayer)
     25     : fDoAAClip(aaclip)
     26     , fDoSaveLayer(saveLayer) {
     27         this->setBGColor(0xFFDDDDDD);
     28 //        this->setBGColor(SkColorSetRGB(0xB0,0xDD,0xB0));
     29     }
     30 
     31 protected:
     32     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     33         return kSkipTiled_Flag;
     34     }
     35 
     36 
     37     SkString onShortName() {
     38         SkString str;
     39         str.printf("complexclip_%s%s",
     40                    fDoAAClip ? "aa" : "bw",
     41                    fDoSaveLayer ? "_layer" : "");
     42         return str;
     43     }
     44 
     45     SkISize onISize() { return SkISize::Make(970, 780); }
     46 
     47     virtual void onDraw(SkCanvas* canvas) {
     48         SkPath path;
     49         path.moveTo(SkIntToScalar(0),   SkIntToScalar(50));
     50         path.quadTo(SkIntToScalar(0),   SkIntToScalar(0),   SkIntToScalar(50),  SkIntToScalar(0));
     51         path.lineTo(SkIntToScalar(175), SkIntToScalar(0));
     52         path.quadTo(SkIntToScalar(200), SkIntToScalar(0),   SkIntToScalar(200), SkIntToScalar(25));
     53         path.lineTo(SkIntToScalar(200), SkIntToScalar(150));
     54         path.quadTo(SkIntToScalar(200), SkIntToScalar(200), SkIntToScalar(150), SkIntToScalar(200));
     55         path.lineTo(SkIntToScalar(0),   SkIntToScalar(200));
     56         path.close();
     57         path.moveTo(SkIntToScalar(50),  SkIntToScalar(50));
     58         path.lineTo(SkIntToScalar(150), SkIntToScalar(50));
     59         path.lineTo(SkIntToScalar(150), SkIntToScalar(125));
     60         path.quadTo(SkIntToScalar(150), SkIntToScalar(150), SkIntToScalar(125), SkIntToScalar(150));
     61         path.lineTo(SkIntToScalar(50),  SkIntToScalar(150));
     62         path.close();
     63         path.setFillType(SkPath::kEvenOdd_FillType);
     64         SkPaint pathPaint;
     65         pathPaint.setAntiAlias(true);
     66         pathPaint.setColor(gPathColor);
     67 
     68         SkPath clipA;
     69         clipA.moveTo(SkIntToScalar(10),  SkIntToScalar(20));
     70         clipA.lineTo(SkIntToScalar(165), SkIntToScalar(22));
     71         clipA.lineTo(SkIntToScalar(70),  SkIntToScalar(105));
     72         clipA.lineTo(SkIntToScalar(165), SkIntToScalar(177));
     73         clipA.lineTo(SkIntToScalar(-5),  SkIntToScalar(180));
     74         clipA.close();
     75 
     76         SkPath clipB;
     77         clipB.moveTo(SkIntToScalar(40),  SkIntToScalar(10));
     78         clipB.lineTo(SkIntToScalar(190), SkIntToScalar(15));
     79         clipB.lineTo(SkIntToScalar(195), SkIntToScalar(190));
     80         clipB.lineTo(SkIntToScalar(40),  SkIntToScalar(185));
     81         clipB.lineTo(SkIntToScalar(155), SkIntToScalar(100));
     82         clipB.close();
     83 
     84         SkPaint paint;
     85         paint.setAntiAlias(true);
     86         sk_tool_utils::set_portable_typeface(&paint);
     87         paint.setTextSize(SkIntToScalar(20));
     88 
     89         static const struct {
     90             SkRegion::Op fOp;
     91             const char*  fName;
     92         } gOps[] = { //extra spaces in names for measureText
     93             {SkRegion::kIntersect_Op,         "Isect "},
     94             {SkRegion::kDifference_Op,        "Diff " },
     95             {SkRegion::kUnion_Op,             "Union "},
     96             {SkRegion::kXOR_Op,               "Xor "  },
     97             {SkRegion::kReverseDifference_Op, "RDiff "}
     98         };
     99 
    100         canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
    101         canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
    102 
    103         if (fDoSaveLayer) {
    104             // We want the layer to appear symmetric relative to actual
    105             // device boundaries so we need to "undo" the effect of the
    106             // scale and translate
    107             SkRect bounds = SkRect::MakeLTRB(
    108               4.0f/3.0f * -20,
    109               4.0f/3.0f * -20,
    110               4.0f/3.0f * (this->getISize().fWidth - 20),
    111               4.0f/3.0f * (this->getISize().fHeight - 20));
    112 
    113             bounds.inset(SkIntToScalar(100), SkIntToScalar(100));
    114             SkPaint boundPaint;
    115             boundPaint.setColor(SK_ColorRED);
    116             boundPaint.setStyle(SkPaint::kStroke_Style);
    117             canvas->drawRect(bounds, boundPaint);
    118             canvas->saveLayer(&bounds, NULL);
    119         }
    120 
    121         for (int invBits = 0; invBits < 4; ++invBits) {
    122             canvas->save();
    123             for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
    124                 this->drawHairlines(canvas, path, clipA, clipB);
    125 
    126                 bool doInvA = SkToBool(invBits & 1);
    127                 bool doInvB = SkToBool(invBits & 2);
    128                 canvas->save();
    129                     // set clip
    130                     clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
    131                                       SkPath::kEvenOdd_FillType);
    132                     clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
    133                                       SkPath::kEvenOdd_FillType);
    134                     canvas->clipPath(clipA, SkRegion::kIntersect_Op, fDoAAClip);
    135                     canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
    136 
    137                     // draw path clipped
    138                     canvas->drawPath(path, pathPaint);
    139                 canvas->restore();
    140 
    141 
    142                 SkScalar txtX = SkIntToScalar(45);
    143                 paint.setColor(gClipAColor);
    144                 const char* aTxt = doInvA ? "InvA " : "A ";
    145                 canvas->drawText(aTxt, strlen(aTxt), txtX, SkIntToScalar(220), paint);
    146                 txtX += paint.measureText(aTxt, strlen(aTxt));
    147                 paint.setColor(SK_ColorBLACK);
    148                 canvas->drawText(gOps[op].fName, strlen(gOps[op].fName),
    149                                     txtX, SkIntToScalar(220), paint);
    150                 txtX += paint.measureText(gOps[op].fName, strlen(gOps[op].fName));
    151                 paint.setColor(gClipBColor);
    152                 const char* bTxt = doInvB ? "InvB " : "B ";
    153                 canvas->drawText(bTxt, strlen(bTxt), txtX, SkIntToScalar(220), paint);
    154 
    155                 canvas->translate(SkIntToScalar(250),0);
    156             }
    157             canvas->restore();
    158             canvas->translate(0, SkIntToScalar(250));
    159         }
    160 
    161         if (fDoSaveLayer) {
    162             canvas->restore();
    163         }
    164     }
    165 private:
    166     void drawHairlines(SkCanvas* canvas, const SkPath& path,
    167                        const SkPath& clipA, const SkPath& clipB) {
    168         SkPaint paint;
    169         paint.setAntiAlias(true);
    170         paint.setStyle(SkPaint::kStroke_Style);
    171         const SkAlpha fade = 0x33;
    172 
    173         // draw path in hairline
    174         paint.setColor(gPathColor); paint.setAlpha(fade);
    175         canvas->drawPath(path, paint);
    176 
    177         // draw clips in hair line
    178         paint.setColor(gClipAColor); paint.setAlpha(fade);
    179         canvas->drawPath(clipA, paint);
    180         paint.setColor(gClipBColor); paint.setAlpha(fade);
    181         canvas->drawPath(clipB, paint);
    182     }
    183 
    184     typedef GM INHERITED;
    185 };
    186 
    187 //////////////////////////////////////////////////////////////////////////////
    188 
    189 // aliased and anti-aliased w/o a layer
    190 static GM* gFact0(void*) { return new ComplexClipGM(false, false); }
    191 static GM* gFact1(void*) { return new ComplexClipGM(true, false); }
    192 
    193 // aliased and anti-aliased w/ a layer
    194 static GM* gFact2(void*) { return new ComplexClipGM(false, true); }
    195 static GM* gFact3(void*) { return new ComplexClipGM(true, true); }
    196 
    197 static GMRegistry gReg0(gFact0);
    198 static GMRegistry gReg1(gFact1);
    199 static GMRegistry gReg2(gFact2);
    200 static GMRegistry gReg3(gFact3);
    201 
    202 }
    203