epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | bf0001d | 2014-01-13 14:53:55 +0000 | [diff] [blame] | 7 | |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | |
| 10 | #include "Resources.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SampleCode.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 12 | #include "SkCanvas.h" |
| 13 | #include "SkColorFilter.h" |
| 14 | #include "SkColorPriv.h" |
halcanary@google.com | e238057 | 2013-12-18 15:08:35 +0000 | [diff] [blame] | 15 | #include "SkData.h" |
| 16 | #include "SkDecodingImageGenerator.h" |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 17 | #include "SkDumpCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkGradientShader.h" |
| 19 | #include "SkGraphics.h" |
| 20 | #include "SkImageDecoder.h" |
halcanary@google.com | e238057 | 2013-12-18 15:08:35 +0000 | [diff] [blame] | 21 | #include "SkOSFile.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | #include "SkPath.h" |
| 23 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 24 | #include "SkPictureRecorder.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkRandom.h" |
| 26 | #include "SkRegion.h" |
| 27 | #include "SkShader.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 28 | #include "SkStream.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkTime.h" |
| 30 | #include "SkTypeface.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 31 | #include "SkUtils.h" |
| 32 | #include "SkView.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | #include "SkXMLParser.h" |
tfarina | bcbc178 | 2014-06-18 14:32:48 -0700 | [diff] [blame] | 34 | #include "SkXfermode.h" |
reed@android.com | 7d970c7 | 2010-04-22 16:07:49 +0000 | [diff] [blame] | 35 | |
halcanary@google.com | e238057 | 2013-12-18 15:08:35 +0000 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 37 | |
| 38 | static SkBitmap load_bitmap() { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 39 | SkBitmap bm; |
tfarina | c846f4a | 2014-07-01 12:35:49 -0700 | [diff] [blame] | 40 | SkString pngFilename = GetResourcePath("mandrill_512.png"); |
| 41 | SkAutoDataUnref data(SkData::NewFromFileName(pngFilename.c_str())); |
halcanary@google.com | e238057 | 2013-12-18 15:08:35 +0000 | [diff] [blame] | 42 | if (data.get() != NULL) { |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 43 | SkInstallDiscardablePixelRef(SkDecodingImageGenerator::Create( |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 44 | data, SkDecodingImageGenerator::Options()), &bm); |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 45 | } |
| 46 | return bm; |
| 47 | } |
| 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | static void drawCircle(SkCanvas* canvas, int r, SkColor color) { |
| 50 | SkPaint paint; |
| 51 | paint.setAntiAlias(true); |
| 52 | paint.setColor(color); |
| 53 | |
| 54 | canvas->drawCircle(SkIntToScalar(r), SkIntToScalar(r), SkIntToScalar(r), |
| 55 | paint); |
| 56 | } |
| 57 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 58 | class PictureView : public SampleView { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 59 | SkBitmap fBitmap; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | public: |
halcanary@google.com | e238057 | 2013-12-18 15:08:35 +0000 | [diff] [blame] | 61 | PictureView() { |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 62 | |
| 63 | fBitmap = load_bitmap(); |
| 64 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 65 | SkPictureRecorder recorder; |
| 66 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 67 | recorder.beginRecording(100, 100, NULL, 0); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 68 | fSubPicture = recorder.endRecording(); |
| 69 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 70 | SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | SkPaint paint; |
| 72 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 73 | |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 74 | canvas->drawBitmap(fBitmap, 0, 0, NULL); |
| 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | drawCircle(canvas, 50, SK_ColorBLACK); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 77 | canvas->drawPicture(fSubPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | canvas->translate(SkIntToScalar(50), 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 79 | canvas->drawPicture(fSubPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | canvas->translate(0, SkIntToScalar(50)); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 81 | canvas->drawPicture(fSubPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | canvas->translate(SkIntToScalar(-50), 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 83 | canvas->drawPicture(fSubPicture); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 84 | |
| 85 | fPicture = recorder.endRecording(); |
| 86 | |
skia.committer@gmail.com | 5199701 | 2014-04-14 03:04:57 +0000 | [diff] [blame] | 87 | // fPicture now has (4) references to fSubPicture. We can release our ref, |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 88 | // and just unref fPicture in our destructor, and it will in turn take care of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | // the other references to fSubPicture |
| 90 | fSubPicture->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 | virtual ~PictureView() { |
| 94 | fPicture->unref(); |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | protected: |
| 98 | // overrides from SkEventSink |
| 99 | virtual bool onQuery(SkEvent* evt) { |
| 100 | if (SampleCode::TitleQ(*evt)) { |
| 101 | SampleCode::TitleR(evt, "Picture"); |
| 102 | return true; |
| 103 | } |
| 104 | return this->INHERITED::onQuery(evt); |
| 105 | } |
| 106 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | void drawSomething(SkCanvas* canvas) { |
| 108 | SkPaint paint; |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 109 | |
| 110 | canvas->save(); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 111 | canvas->scale(0.5f, 0.5f); |
reed@android.com | c6ddc11 | 2009-11-10 15:54:55 +0000 | [diff] [blame] | 112 | canvas->drawBitmap(fBitmap, 0, 0, NULL); |
| 113 | canvas->restore(); |
| 114 | |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 115 | const char beforeStr[] = "before circle"; |
| 116 | const char afterStr[] = "after circle"; |
| 117 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | paint.setColor(SK_ColorRED); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 121 | canvas->drawData(beforeStr, sizeof(beforeStr)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | canvas->drawCircle(SkIntToScalar(50), SkIntToScalar(50), |
| 123 | SkIntToScalar(40), paint); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 124 | canvas->drawData(afterStr, sizeof(afterStr)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | paint.setColor(SK_ColorBLACK); |
| 126 | paint.setTextSize(SkIntToScalar(40)); |
| 127 | canvas->drawText("Picture", 7, SkIntToScalar(50), SkIntToScalar(62), |
| 128 | paint); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 129 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 132 | virtual void onDrawContent(SkCanvas* canvas) { |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 133 | this->drawSomething(canvas); |
reed@android.com | 149e2f6 | 2009-05-22 14:39:03 +0000 | [diff] [blame] | 134 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 135 | SkPictureRecorder recorder; |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 136 | this->drawSomething(recorder.beginRecording(100, 100, NULL, 0)); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 137 | SkAutoTUnref<SkPicture> pict(recorder.endRecording()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | canvas->save(); |
| 140 | canvas->translate(SkIntToScalar(300), SkIntToScalar(50)); |
| 141 | canvas->scale(-SK_Scalar1, -SK_Scalar1); |
| 142 | canvas->translate(-SkIntToScalar(100), -SkIntToScalar(50)); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 143 | canvas->drawPicture(pict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | canvas->restore(); |
| 145 | |
| 146 | canvas->save(); |
| 147 | canvas->translate(SkIntToScalar(200), SkIntToScalar(150)); |
| 148 | canvas->scale(SK_Scalar1, -SK_Scalar1); |
| 149 | canvas->translate(0, -SkIntToScalar(50)); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 150 | canvas->drawPicture(pict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 152 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 153 | canvas->save(); |
| 154 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 155 | canvas->scale(-SK_Scalar1, SK_Scalar1); |
| 156 | canvas->translate(-SkIntToScalar(100), 0); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 157 | canvas->drawPicture(pict); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | canvas->restore(); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 159 | |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 160 | #ifdef SK_DEVELOPER |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 161 | if (false) { |
| 162 | SkDebugfDumper dumper; |
| 163 | SkDumpCanvas dumpCanvas(&dumper); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 164 | dumpCanvas.drawPicture(pict); |
reed@android.com | cb60844 | 2009-12-04 21:32:27 +0000 | [diff] [blame] | 165 | } |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 166 | #endif |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 167 | |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 168 | // This used to re-record the sub-picture and redraw the parent |
| 169 | // A capability that is now forbidden! |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 170 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 171 | SkRandom rand(SampleCode::GetAnimTime()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | canvas->translate(SkIntToScalar(10), SkIntToScalar(250)); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 173 | canvas->drawPicture(fPicture); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | delayInval(500); |
| 175 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | private: |
| 178 | #define INVAL_ALL_TYPE "inval-all" |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | void delayInval(SkMSec delay) { |
reed@google.com | 87fac4a | 2011-08-04 13:50:17 +0000 | [diff] [blame] | 181 | (new SkEvent(INVAL_ALL_TYPE, this->getSinkID()))->postDelay(delay); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | virtual bool onEvent(const SkEvent& evt) { |
| 185 | if (evt.isType(INVAL_ALL_TYPE)) { |
| 186 | this->inval(NULL); |
| 187 | return true; |
| 188 | } |
| 189 | return this->INHERITED::onEvent(evt); |
| 190 | } |
| 191 | |
| 192 | SkPicture* fPicture; |
| 193 | SkPicture* fSubPicture; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 195 | typedef SampleView INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | ////////////////////////////////////////////////////////////////////////////// |
| 199 | |
| 200 | static SkView* MyFactory() { return new PictureView; } |
| 201 | static SkViewRegister reg(MyFactory); |