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