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