reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 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(); |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 56 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 57 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 58 | SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i)); |
| 59 | } |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 60 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 61 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 62 | virtual ~ShapesGM() { |
| 63 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 64 | SkSafeUnref(fMatrixRefs[i]); |
| 65 | } |
| 66 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 67 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 68 | protected: |
| 69 | virtual SkString onShortName() { |
| 70 | return SkString("shapes"); |
| 71 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 72 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 73 | virtual SkISize onISize() { |
| 74 | return make_isize(380, 480); |
| 75 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 76 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 77 | void drawBG(SkCanvas* canvas) { |
| 78 | canvas->drawColor(0xFFDDDDDD); |
| 79 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 80 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 81 | virtual void onDraw(SkCanvas* canvas) { |
| 82 | this->drawBG(canvas); |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 83 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 84 | SkMatrix saveM = *fMatrixRefs[3]; |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 85 | SkScalar c = SkIntToScalar(50); |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 86 | fMatrixRefs[3]->preRotate(SkIntToScalar(30), c, c); |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 87 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 88 | SkMatrix matrix; |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 89 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 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); |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 99 | |
| 100 | #if 0 |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 101 | canvas->drawShape(gs); |
| 102 | #else |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 103 | SkPicture* pict = new SkPicture; |
| 104 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 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); |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 110 | pict->endRecording(); |
| 111 | canvas->drawPicture(*pict); |
| 112 | pict->unref(); |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 113 | #endif |
| 114 | |
| 115 | *fMatrixRefs[3] = saveM; |
| 116 | } |
reed@google.com | 0e4a885 | 2011-01-12 17:14:04 +0000 | [diff] [blame] | 117 | |
reed@android.com | 0bfffc5 | 2009-06-26 20:22:44 +0000 | [diff] [blame] | 118 | private: |
| 119 | typedef GM INHERITED; |
| 120 | }; |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
| 124 | static GM* MyFactory(void*) { return new ShapesGM; } |
| 125 | static GMRegistry reg(MyFactory); |
| 126 | |
| 127 | } |