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 | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 9 | #include "SkDumpCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "Sk64.h" |
| 13 | #include "SkGradientShader.h" |
| 14 | #include "SkGraphics.h" |
| 15 | #include "SkImageDecoder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include "SkPath.h" |
| 17 | #include "SkPicture.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkRegion.h" |
| 20 | #include "SkShader.h" |
| 21 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkColorPriv.h" |
| 23 | #include "SkColorFilter.h" |
| 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
| 26 | #include "SkXfermode.h" |
| 27 | |
| 28 | #include "SkStream.h" |
| 29 | #include "SkXMLParser.h" |
| 30 | |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 33 | #include "SkImageRef_GlobalPool.h" |
| 34 | |
| 35 | static SkBitmap load_bitmap() { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 36 | SkBitmap bm; |
reed@google.com | 84cfce1 | 2013-05-29 19:22:20 +0000 | [diff] [blame] | 37 | SkStream* stream = SkStream::NewFromFile("/skimages/sesame_street_ensemble-hp.jpg"); |
| 38 | if (stream) { |
| 39 | SkAutoUnref aur(stream); |
| 40 | |
| 41 | if (SkImageDecoder::DecodeStream(stream, &bm, SkBitmap::kNo_Config, |
| 42 | SkImageDecoder::kDecodeBounds_Mode)) { |
| 43 | SkPixelRef* pr = new SkImageRef_GlobalPool(stream, bm.config(), 1); |
| 44 | bm.setPixelRef(pr)->unref(); |
| 45 | } |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 46 | } |
| 47 | return bm; |
| 48 | } |
| 49 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 50 | static void drawCircle(SkCanvas* canvas, int r, SkColor color) { |
| 51 | SkPaint paint; |
| 52 | paint.setAntiAlias(true); |
| 53 | paint.setColor(color); |
| 54 | |
| 55 | canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r), |
| 56 | paint); |
| 57 | } |
| 58 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 59 | class PictureView : public SampleView { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 60 | SkBitmap fBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | public: |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 62 | PictureView() { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 63 | SkImageRef_GlobalPool::SetRAMBudget(16 * 1024); |
| 64 | |
| 65 | fBitmap = load_bitmap(); |
| 66 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | fPicture = new SkPicture; |
| 68 | SkCanvas* canvas = fPicture->beginRecording(100, 100); |
| 69 | SkPaint paint; |
| 70 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 72 | canvas->drawBitmap(fBitmap, 0, 0, NULL); |
| 73 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | drawCircle(canvas, 50, SK_ColorBLACK); |
| 75 | fSubPicture = new SkPicture; |
| 76 | canvas->drawPicture(*fSubPicture); |
| 77 | canvas->translate(SkIntToScalar(50), 0); |
| 78 | canvas->drawPicture(*fSubPicture); |
| 79 | canvas->translate(0, SkIntToScalar(50)); |
| 80 | canvas->drawPicture(*fSubPicture); |
| 81 | canvas->translate(SkIntToScalar(-50), 0); |
| 82 | canvas->drawPicture(*fSubPicture); |
| 83 | // fPicture now has (4) references to us. We can release ours, and just |
| 84 | // unref fPicture in our destructor, and it will in turn take care of |
| 85 | // the other references to fSubPicture |
| 86 | fSubPicture->unref(); |
| 87 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | virtual ~PictureView() { |
| 90 | fPicture->unref(); |
| 91 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | protected: |
| 94 | // overrides from SkEventSink |
| 95 | virtual bool onQuery(SkEvent* evt) { |
| 96 | if (SampleCode::TitleQ(*evt)) { |
| 97 | SampleCode::TitleR(evt, "Picture"); |
| 98 | return true; |
| 99 | } |
| 100 | return this->INHERITED::onQuery(evt); |
| 101 | } |
| 102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 103 | void drawSomething(SkCanvas* canvas) { |
| 104 | SkPaint paint; |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 105 | |
| 106 | canvas->save(); |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 107 | canvas->scale(SkFloatToScalar(0.5f), SkFloatToScalar(0.5f)); |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 108 | canvas->drawBitmap(fBitmap, 0, 0, NULL); |
| 109 | canvas->restore(); |
| 110 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 111 | const char beforeStr[] = "before circle"; |
| 112 | const char afterStr[] = "after circle"; |
| 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | paint.setColor(SK_ColorRED); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 117 | canvas->drawData(beforeStr, sizeof(beforeStr)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 119 | SkIntToScalar(40), paint); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 120 | canvas->drawData(afterStr, sizeof(afterStr)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 121 | paint.setColor(SK_ColorBLACK); |
| 122 | paint.setTextSize(SkIntToScalar(40)); |
| 123 | canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62), |
| 124 | paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | } |
| 127 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 128 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | drawSomething(canvas); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | SkPicture* pict = new SkPicture; |
| 132 | SkAutoUnref aur(pict); |
| 133 | |
| 134 | drawSomething(pict->beginRecording(100, 100)); |
| 135 | pict->endRecording(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | canvas->save(); |
| 138 | canvas->translate(SkIntToScalar(300), SkIntToScalar(50)); |
| 139 | canvas->scale(-SK_Scalar1, -SK_Scalar1); |
| 140 | canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50)); |
| 141 | canvas->drawPicture(*pict); |
| 142 | canvas->restore(); |
| 143 | |
| 144 | canvas->save(); |
| 145 | canvas->translate(SkIntToScalar(200), SkIntToScalar(150)); |
| 146 | canvas->scale(SK_Scalar1, -SK_Scalar1); |
| 147 | canvas->translate(0, -SkIntToScalar(50)); |
| 148 | canvas->drawPicture(*pict); |
| 149 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | canvas->save(); |
| 152 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 153 | canvas->scale(-SK_Scalar1, SK_Scalar1); |
| 154 | canvas->translate(-SkIntToScalar(100), 0); |
| 155 | canvas->drawPicture(*pict); |
| 156 | canvas->restore(); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 157 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 158 | #ifdef SK_DEVELOPER |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 159 | if (false) { |
| 160 | SkDebugfDumper dumper; |
| 161 | SkDumpCanvas dumpCanvas(&dumper); |
| 162 | dumpCanvas.drawPicture(*pict); |
| 163 | } |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 164 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | // test that we can re-record a subpicture, and see the results |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 167 | |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 168 | SkRandom rand(SampleCode::GetAnimTime()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | canvas->translate(SkIntToScalar(10), SkIntToScalar(250)); |
| 170 | drawCircle(fSubPicture->beginRecording(50, 50), 25, |
reed@android.com | 4417740 | 2009-11-23 21:07:51 +0000 | [diff] [blame] | 171 | rand.nextU() | 0xFF000000); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | canvas->drawPicture(*fPicture); |
| 173 | delayInval(500); |
| 174 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 175 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | private: |
| 177 | #define INVAL_ALL_TYPE "inval-all" |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 179 | void delayInval(SkMSec delay) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 180 | (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | virtual bool onEvent(const SkEvent& evt) { |
| 184 | if (evt.isType(INVAL_ALL_TYPE)) { |
| 185 | this->inval(NULL); |
| 186 | return true; |
| 187 | } |
| 188 | return this->INHERITED::onEvent(evt); |
| 189 | } |
| 190 | |
| 191 | SkPicture* fPicture; |
| 192 | SkPicture* fSubPicture; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 194 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | ////////////////////////////////////////////////////////////////////////////// |
| 198 | |
| 199 | static SkView* MyFactory() { return new PictureView; } |
| 200 | static SkViewRegister reg(MyFactory); |