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 "SkPicture.h"
     10 #include "SkRectShape.h"
     11 #include "SkGroupShape.h"
     12 
     13 namespace skiagm {
     14 
     15 static SkRect make_rect(int l, int t, int r, int b) {
     16     SkRect rect;
     17     rect.set(SkIntToScalar(l), SkIntToScalar(t),
     18              SkIntToScalar(r), SkIntToScalar(b));
     19     return rect;
     20 }
     21 
     22 static SkShape* make_shape0(bool red) {
     23     SkRectShape* s = new SkRectShape;
     24     s->setRect(make_rect(10, 10, 90, 90));
     25     if (red) {
     26         s->paint().setColor(SK_ColorRED);
     27     }
     28     return s;
     29 }
     30 
     31 static SkShape* make_shape1() {
     32     SkRectShape* s = new SkRectShape;
     33     s->setOval(make_rect(10, 10, 90, 90));
     34     s->paint().setColor(SK_ColorBLUE);
     35     return s;
     36 }
     37 
     38 static SkShape* make_shape2() {
     39     SkRectShape* s = new SkRectShape;
     40     s->setRRect(make_rect(10, 10, 90, 90),
     41                 SkIntToScalar(20), SkIntToScalar(20));
     42     s->paint().setColor(SK_ColorGREEN);
     43     return s;
     44 }
     45 
     46 ///////////////////////////////////////////////////////////////////////////////
     47 
     48 class ShapesGM : public GM {
     49     SkGroupShape fGroup;
     50     SkMatrixRef*    fMatrixRefs[4];
     51 public:
     52 	ShapesGM() {
     53         this->setBGColor(0xFFDDDDDD);
     54 
     55         SkMatrix m;
     56         fGroup.appendShape(make_shape0(false))->unref();
     57         m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50));
     58         m.postTranslate(0, SkIntToScalar(120));
     59         fGroup.appendShape(make_shape0(true), m)->unref();
     60 
     61         m.setTranslate(SkIntToScalar(120), 0);
     62         fGroup.appendShape(make_shape1(), m)->unref();
     63         m.postTranslate(0, SkIntToScalar(120));
     64         fGroup.appendShape(make_shape2(), m)->unref();
     65 
     66         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
     67             SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
     68         }
     69         SkScalar c = SkIntToScalar(50);
     70         fMatrixRefs[3]->preRotate(SkIntToScalar(30), c, c);
     71     }
     72 
     73     virtual ~ShapesGM() {
     74         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
     75             SkSafeUnref(fMatrixRefs[i]);
     76         }
     77     }
     78 
     79 protected:
     80     virtual SkString onShortName() {
     81         return SkString("shapes");
     82     }
     83 
     84 	virtual SkISize onISize() {
     85         return make_isize(380, 480);
     86     }
     87 
     88     virtual void onDraw(SkCanvas* canvas) {
     89         SkMatrix matrix;
     90 
     91         SkGroupShape* gs = new SkGroupShape;
     92         SkAutoUnref aur(gs);
     93         gs->appendShape(&fGroup);
     94         matrix.setScale(-SK_Scalar1, SK_Scalar1);
     95         matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240));
     96         gs->appendShape(&fGroup, matrix);
     97         matrix.setTranslate(SkIntToScalar(240), 0);
     98         matrix.preScale(SK_Scalar1*2, SK_Scalar1*2);
     99         gs->appendShape(&fGroup, matrix);
    100 
    101 #if 1
    102         SkPicture* pict = new SkPicture;
    103         SkCanvas* cv = pict->beginRecording(1000, 1000);
    104         cv->scale(SK_ScalarHalf, SK_ScalarHalf);
    105         gs->draw(cv);
    106         cv->translate(SkIntToScalar(680), SkIntToScalar(480));
    107         cv->scale(-SK_Scalar1, SK_Scalar1);
    108         gs->draw(cv);
    109         pict->endRecording();
    110         canvas->drawPicture(*pict);
    111         pict->unref();
    112 #endif
    113 }
    114 
    115 private:
    116     typedef GM INHERITED;
    117 };
    118 
    119 ///////////////////////////////////////////////////////////////////////////////
    120 
    121 static GM* MyFactory(void*) { return new ShapesGM; }
    122 static GMRegistry reg(MyFactory);
    123 
    124 }
    125