reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 4 | #include "SkPicture.h" |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 5 | #include "SkStream.h" |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 6 | #include "SkView.h" |
| 7 | |
reed@google.com | a09368c | 2011-02-24 21:42:29 +0000 | [diff] [blame] | 8 | #define DO_AA true |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 9 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 10 | #include "SkRectShape.h" |
| 11 | #include "SkGroupShape.h" |
| 12 | |
| 13 | static SkRect make_rect(int l, int t, int r, int b) { |
| 14 | SkRect rect; |
| 15 | rect.set(SkIntToScalar(l), SkIntToScalar(t), |
| 16 | SkIntToScalar(r), SkIntToScalar(b)); |
| 17 | return rect; |
| 18 | } |
| 19 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 20 | static SkShape* make_shape0(bool red) { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 21 | SkRectShape* s = new SkRectShape; |
| 22 | s->setRect(make_rect(10, 10, 90, 90)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 23 | if (red) { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 24 | s->paint().setColor(SK_ColorRED); |
| 25 | } |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 26 | s->paint().setAntiAlias(DO_AA); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 27 | return s; |
| 28 | } |
| 29 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 30 | static SkShape* make_shape1() { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 31 | SkRectShape* s = new SkRectShape; |
| 32 | s->setOval(make_rect(10, 10, 90, 90)); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 33 | s->paint().setColor(SK_ColorBLUE); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 34 | s->paint().setAntiAlias(DO_AA); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 35 | return s; |
| 36 | } |
| 37 | |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 38 | static SkShape* make_shape2() { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 39 | SkRectShape* s = new SkRectShape; |
| 40 | s->setRRect(make_rect(10, 10, 90, 90), |
| 41 | SkIntToScalar(20), SkIntToScalar(20)); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 42 | s->paint().setColor(SK_ColorGREEN); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 43 | s->paint().setAntiAlias(DO_AA); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 44 | return s; |
| 45 | } |
| 46 | |
| 47 | /////////////////////////////////////////////////////////////////////////////// |
| 48 | |
| 49 | class ShapesView : public SkView { |
| 50 | SkGroupShape fGroup; |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 51 | SkMatrixRef* fMatrixRefs[4]; |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 52 | public: |
| 53 | ShapesView() { |
| 54 | SkMatrix m; |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 55 | fGroup.appendShape(make_shape0(false))->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 56 | m.setRotate(SkIntToScalar(30), SkIntToScalar(50), SkIntToScalar(50)); |
| 57 | m.postTranslate(0, SkIntToScalar(120)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 58 | fGroup.appendShape(make_shape0(true), m)->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 59 | |
| 60 | m.setTranslate(SkIntToScalar(120), 0); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 61 | fGroup.appendShape(make_shape1(), m)->unref(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 62 | m.postTranslate(0, SkIntToScalar(120)); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 63 | fGroup.appendShape(make_shape2(), m)->unref(); |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 64 | |
| 65 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 66 | SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i)); |
| 67 | } |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | virtual ~ShapesView() { |
| 71 | for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) { |
| 72 | SkSafeUnref(fMatrixRefs[i]); |
| 73 | } |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | protected: |
| 77 | // overrides from SkEventSink |
| 78 | virtual bool onQuery(SkEvent* evt) { |
| 79 | if (SampleCode::TitleQ(*evt)) { |
| 80 | SampleCode::TitleR(evt, "Shapes"); |
| 81 | return true; |
| 82 | } |
| 83 | return this->INHERITED::onQuery(evt); |
| 84 | } |
| 85 | |
| 86 | void drawBG(SkCanvas* canvas) { |
| 87 | canvas->drawColor(0xFFDDDDDD); |
| 88 | } |
| 89 | |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 90 | void drawpicture(SkCanvas* canvas, SkPicture& pict) { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 91 | #if 0 |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 92 | SkDynamicMemoryWStream ostream; |
| 93 | pict.serialize(&ostream); |
| 94 | |
| 95 | SkMemoryStream istream(ostream.getStream(), ostream.getOffset()); |
reed@android.com | 8433b5d | 2009-07-03 02:52:27 +0000 | [diff] [blame] | 96 | SkPicture* newPict = new SkPicture(&istream); |
| 97 | canvas->drawPicture(*newPict); |
| 98 | newPict->unref(); |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 99 | #else |
| 100 | canvas->drawPicture(pict); |
| 101 | #endif |
| 102 | } |
| 103 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 104 | virtual void onDraw(SkCanvas* canvas) { |
| 105 | this->drawBG(canvas); |
| 106 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 107 | SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(180), |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 108 | SkIntToScalar(360)); |
| 109 | |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 110 | SkMatrix saveM = *fMatrixRefs[3]; |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 111 | SkScalar c = SkIntToScalar(50); |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 112 | fMatrixRefs[3]->preRotate(angle, c, c); |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 113 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 114 | const SkScalar dx = 350; |
| 115 | const SkScalar dy = 500; |
| 116 | const int N = 1; |
| 117 | for (int v = -N; v <= N; v++) { |
| 118 | for (int h = -N; h <= N; h++) { |
| 119 | SkAutoCanvasRestore acr(canvas, true); |
| 120 | canvas->translate(h * dx, v * dy); |
| 121 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 122 | SkMatrix matrix; |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 123 | |
| 124 | SkGroupShape* gs = new SkGroupShape; |
| 125 | SkAutoUnref aur(gs); |
| 126 | gs->appendShape(&fGroup); |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 127 | matrix.setScale(-SK_Scalar1, SK_Scalar1); |
| 128 | matrix.postTranslate(SkIntToScalar(220), SkIntToScalar(240)); |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 129 | gs->appendShape(&fGroup, matrix); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 130 | matrix.setTranslate(SkIntToScalar(240), 0); |
| 131 | matrix.preScale(SK_Scalar1*2, SK_Scalar1*2); |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 132 | gs->appendShape(&fGroup, matrix); |
| 133 | |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 134 | #if 0 |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 135 | canvas->drawShape(gs); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 136 | #else |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 137 | SkPicture* pict = new SkPicture; |
| 138 | SkCanvas* cv = pict->beginRecording(1000, 1000); |
reed@android.com | 09b84a0 | 2009-06-26 20:22:26 +0000 | [diff] [blame] | 139 | cv->scale(SK_ScalarHalf, SK_ScalarHalf); |
| 140 | cv->drawShape(gs); |
| 141 | cv->translate(SkIntToScalar(680), SkIntToScalar(480)); |
| 142 | cv->scale(-SK_Scalar1, SK_Scalar1); |
| 143 | cv->drawShape(gs); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 144 | pict->endRecording(); |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 145 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 146 | drawpicture(canvas, *pict); |
| 147 | pict->unref(); |
reed@android.com | f48f281 | 2009-05-18 04:20:55 +0000 | [diff] [blame] | 148 | #endif |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 149 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 150 | }} |
| 151 | |
reed@android.com | 88c2d8e | 2009-05-27 01:44:24 +0000 | [diff] [blame] | 152 | *fMatrixRefs[3] = saveM; |
| 153 | this->inval(NULL); |
| 154 | } |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 155 | |
| 156 | private: |
| 157 | typedef SkView INHERITED; |
| 158 | }; |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
| 162 | static SkView* MyFactory() { return new ShapesView; } |
| 163 | static SkViewRegister reg(MyFactory); |
| 164 | |