18d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com/*
28d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com * Copyright 2013 Google Inc.
38d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com *
48d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
58d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com * found in the LICENSE file.
68d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com */
78d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
88d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com#include "gm.h"
98d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com#include "SkRandom.h"
108d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com#include "SkRRect.h"
118d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
128d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comnamespace skiagm {
138d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
148d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com// Test out various combinations of nested rects, ovals and rrects.
158d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comclass NestedGM : public GM {
168d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.compublic:
178d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    NestedGM(bool doAA) : fDoAA(doAA) {
188d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        this->setBGColor(0xFFDDDDDD);
198d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    }
208d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
218d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comprotected:
22a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org    virtual uint32_t onGetFlags() const SK_OVERRIDE {
23a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org        return kSkipTiled_Flag;
24a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org    }
25a90c6803865766d28e92091f56f718f5e41fe80fcommit-bot@chromium.org
268d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    virtual SkString onShortName() SK_OVERRIDE {
278d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        SkString name("nested");
288d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        if (fDoAA) {
298d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            name.append("_aa");
308d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        } else {
318d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            name.append("_bw");
328d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        }
338d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        return name;
348d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    }
358d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
36b74bdf024930e71ca1be8f874ed49dd0b31449b1skia.committer@gmail.com    virtual SkISize onISize() SK_OVERRIDE {
37f539318f0d3dba743ec1886d5d9df0fb1be628a1tfarina        return SkISize::Make(kImageWidth, kImageHeight);
388d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    }
398d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
408d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    enum Shapes {
418d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        kRect_Shape = 0,
428d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        kRRect_Shape,
438d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        kOval_Shape,
448d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        kShapeCount
458d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    };
468d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
478d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    static void AddShape(SkPath* path, const SkRect& rect, Shapes shape, SkPath::Direction dir) {
488d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        switch (shape) {
498d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            case kRect_Shape:
508d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                path->addRect(rect, dir);
518d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                break;
528d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            case kRRect_Shape: {
538d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                SkRRect rr;
548d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                rr.setRectXY(rect, 5, 5);
558d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                path->addRRect(rr, dir);
568d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                break;
578d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                }
588d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            case kOval_Shape:
598d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                path->addOval(rect, dir);
608d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                break;
618d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            default:
628d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                break;
638d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        }
648d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    }
658d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
668d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
678d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
688d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        SkPaint shapePaint;
698d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        shapePaint.setColor(SK_ColorBLACK);
708d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        shapePaint.setAntiAlias(fDoAA);
718d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
728d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        SkRect outerRect = SkRect::MakeWH(40, 40);
738d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
748d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        SkRect innerRects[] = {
758d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            { 10, 10, 30, 30 },     // small
768d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            { .5f, 18, 4.5f, 22 }   // smaller and offset to left
778d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        };
788d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
798d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        // draw a background pattern to make transparency errors more apparent
80e0e7cfe44bb9d66d76120a79e5275c294bacaa22commit-bot@chromium.org        SkRandom rand;
818d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
828d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        for (int y = 0; y < kImageHeight; y += 10) {
838d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            for (int x = 0; x < kImageWidth; x += 10) {
84b74bdf024930e71ca1be8f874ed49dd0b31449b1skia.committer@gmail.com                SkRect r = SkRect::MakeXYWH(SkIntToScalar(x),
85b74bdf024930e71ca1be8f874ed49dd0b31449b1skia.committer@gmail.com                                            SkIntToScalar(y),
868d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                                            10, 10);
878d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                SkPaint p;
888d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                p.setColor(rand.nextU() | 0xFF000000);
898d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                canvas->drawRect(r, p);
908d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            }
918d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        }
928d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
938d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        canvas->translate(2, 2);
948d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        for (int outerShape = 0; outerShape < kShapeCount; ++outerShape) {
958d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            canvas->save();
968d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            for (int innerShape = 0; innerShape < kShapeCount; ++innerShape) {
978d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                for (size_t innerRect = 0; innerRect < SK_ARRAY_COUNT(innerRects); ++innerRect) {
988d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                    SkPath path;
998d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1008d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                    AddShape(&path, outerRect, (Shapes) outerShape, SkPath::kCW_Direction);
101b74bdf024930e71ca1be8f874ed49dd0b31449b1skia.committer@gmail.com                    AddShape(&path, innerRects[innerRect], (Shapes) innerShape,
1028d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                             SkPath::kCCW_Direction);
1038d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1048d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                    canvas->drawPath(path, shapePaint);
1058d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                    canvas->translate(45, 0);
1068d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com                }
1078d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            }
1088d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            canvas->restore();
1098d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com            canvas->translate(0, 45);
1108d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com        }
1118d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1128d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    }
1138d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1148d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comprivate:
1158d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    static const int kImageWidth = 269;
1168d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    static const int kImageHeight = 134;
1178d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1188d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    bool fDoAA;
1198d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1208d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com    typedef GM INHERITED;
1218d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com};
1228d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1238d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com///////////////////////////////////////////////////////////////////////////////
1248d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1258d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comDEF_GM( return new NestedGM(true); )
1268d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.comDEF_GM( return new NestedGM(false); )
1278d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1288d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com
1298d3c640b5aa96d6316a3e52975602b35e1a66ac7robertphillips@google.com}
130