reed@google.com | 1830c7a | 2012-06-04 12:05:43 +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 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkDumpCanvas.h" |
| 10 | #include "SkView.h" |
| 11 | #include "SkCanvas.h" |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
| 14 | #include "SkImageDecoder.h" |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 15 | #include "SkOSFile.h" |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 16 | #include "SkPath.h" |
| 17 | #include "SkPicture.h" |
robertphillips@google.com | 770963f | 2014-04-18 18:04:41 +0000 | [diff] [blame] | 18 | #include "SkPictureRecorder.h" |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 19 | #include "SkRandom.h" |
| 20 | #include "SkRegion.h" |
| 21 | #include "SkShader.h" |
| 22 | #include "SkUtils.h" |
| 23 | #include "SkColorPriv.h" |
| 24 | #include "SkColorFilter.h" |
| 25 | #include "SkTime.h" |
| 26 | #include "SkTypeface.h" |
| 27 | #include "SkXfermode.h" |
| 28 | |
| 29 | #include "SkStream.h" |
reed@google.com | 636d87a | 2013-09-17 20:03:43 +0000 | [diff] [blame] | 30 | #include "SkSurface.h" |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 31 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 32 | #include "SkGlyphCache.h" |
| 33 | |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 34 | #include "SkDrawFilter.h" |
| 35 | class SkCounterDrawFilter : public SkDrawFilter { |
| 36 | public: |
| 37 | SkCounterDrawFilter(int count) : fCount(count) {} |
| 38 | |
| 39 | bool filter(SkPaint*, Type t) override { |
| 40 | return --fCount >= 0; |
| 41 | } |
| 42 | |
| 43 | int fCount; |
| 44 | }; |
| 45 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 46 | class PictFileView : public SampleView { |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 47 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 48 | PictFileView(const char name[] = nullptr) |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 49 | : fFilename(name) |
| 50 | , fBBox(kNo_BBoxType) |
| 51 | , fTileSize(SkSize::Make(0, 0)) { |
fmalita@google.com | 9a65e2c | 2013-12-10 22:25:53 +0000 | [diff] [blame] | 52 | for (int i = 0; i < kBBoxTypeCount; ++i) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 53 | fPictures[i] = nullptr; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 54 | } |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 55 | fCount = 0; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 56 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 57 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 58 | virtual ~PictFileView() { |
fmalita@google.com | 9a65e2c | 2013-12-10 22:25:53 +0000 | [diff] [blame] | 59 | for (int i = 0; i < kBBoxTypeCount; ++i) { |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 60 | SkSafeUnref(fPictures[i]); |
| 61 | } |
| 62 | } |
| 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | void onTileSizeChanged(const SkSize &tileSize) override { |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 65 | if (tileSize != fTileSize) { |
| 66 | fTileSize = tileSize; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | protected: |
| 71 | // overrides from SkEventSink |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 72 | bool onQuery(SkEvent* evt) override { |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 73 | if (SampleCode::TitleQ(*evt)) { |
| 74 | SkString name("P:"); |
| 75 | const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
| 76 | name.append(basename ? basename+1: fFilename.c_str()); |
commit-bot@chromium.org | c22d139 | 2014-02-03 18:08:33 +0000 | [diff] [blame] | 77 | switch (fBBox) { |
| 78 | case kNo_BBoxType: |
| 79 | // No name appended |
| 80 | break; |
| 81 | case kRTree_BBoxType: |
| 82 | name.append(" <bbox: R>"); |
| 83 | break; |
commit-bot@chromium.org | c22d139 | 2014-02-03 18:08:33 +0000 | [diff] [blame] | 84 | default: |
| 85 | SkASSERT(false); |
| 86 | break; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 87 | } |
| 88 | SampleCode::TitleR(evt, name.c_str()); |
| 89 | return true; |
| 90 | } |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 91 | SkUnichar uni; |
| 92 | if (SampleCode::CharQ(*evt, &uni)) { |
| 93 | switch (uni) { |
| 94 | case 'n': fCount += 1; this->inval(nullptr); return true; |
| 95 | case 'p': fCount -= 1; this->inval(nullptr); return true; |
| 96 | case 's': fCount = 0; this->inval(nullptr); return true; |
| 97 | default: break; |
| 98 | } |
| 99 | } |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 100 | return this->INHERITED::onQuery(evt); |
| 101 | } |
| 102 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 103 | bool onEvent(const SkEvent& evt) override { |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 104 | if (evt.isType("PictFileView::toggleBBox")) { |
| 105 | fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount); |
| 106 | return true; |
| 107 | } |
| 108 | return this->INHERITED::onEvent(evt); |
| 109 | } |
| 110 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 111 | void onDrawContent(SkCanvas* canvas) override { |
commit-bot@chromium.org | 8c6f4b3 | 2014-02-03 19:40:32 +0000 | [diff] [blame] | 112 | SkASSERT(static_cast<int>(fBBox) < kBBoxTypeCount); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 113 | SkPicture** picture = fPictures + fBBox; |
| 114 | |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 115 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 116 | SkGraphics::PurgeFontCache(); |
| 117 | #endif |
| 118 | |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 119 | if (!*picture) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 120 | *picture = LoadPicture(fFilename.c_str(), fBBox).release(); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 121 | } |
| 122 | if (*picture) { |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 123 | SkCounterDrawFilter filter(fCount); |
| 124 | if (fCount > 0) { |
| 125 | canvas->setDrawFilter(&filter); |
| 126 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 127 | canvas->drawPicture(*picture); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | canvas->setDrawFilter(nullptr); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 129 | } |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 130 | |
| 131 | #ifdef SK_GLYPHCACHE_TRACK_HASH_STATS |
| 132 | SkGlyphCache::Dump(); |
| 133 | SkDebugf("\n"); |
| 134 | #endif |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private: |
| 138 | enum BBoxType { |
| 139 | kNo_BBoxType, |
| 140 | kRTree_BBoxType, |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 141 | |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 142 | kLast_BBoxType = kRTree_BBoxType, |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 143 | }; |
fmalita@google.com | e6a98d4 | 2013-12-10 22:12:40 +0000 | [diff] [blame] | 144 | static const int kBBoxTypeCount = kLast_BBoxType + 1; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 145 | |
| 146 | SkString fFilename; |
| 147 | SkPicture* fPictures[kBBoxTypeCount]; |
| 148 | BBoxType fBBox; |
| 149 | SkSize fTileSize; |
reed | 093b4e8 | 2015-08-04 18:46:38 -0700 | [diff] [blame] | 150 | int fCount; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 151 | |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 152 | sk_sp<SkPicture> LoadPicture(const char path[], BBoxType bbox) { |
| 153 | sk_sp<SkPicture> pic; |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 154 | |
| 155 | SkBitmap bm; |
| 156 | if (SkImageDecoder::DecodeFile(path, &bm)) { |
| 157 | bm.setImmutable(); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 158 | SkPictureRecorder recorder; |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 159 | SkCanvas* can = recorder.beginRecording(SkIntToScalar(bm.width()), |
| 160 | SkIntToScalar(bm.height()), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | nullptr, 0); |
| 162 | can->drawBitmap(bm, 0, 0, nullptr); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 163 | pic = recorder.finishRecordingAsPicture(); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 164 | } else { |
| 165 | SkFILEStream stream(path); |
| 166 | if (stream.isValid()) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 167 | pic = SkPicture::MakeFromStream(&stream); |
reed@google.com | b8b830e | 2013-06-25 20:42:37 +0000 | [diff] [blame] | 168 | } else { |
| 169 | SkDebugf("coun't load picture at \"path\"\n", path); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 170 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 171 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 172 | if (false) { // re-record |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 173 | SkPictureRecorder recorder; |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 174 | pic->playback(recorder.beginRecording(pic->cullRect().width(), |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 175 | pic->cullRect().height(), |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 176 | nullptr, 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 177 | sk_sp<SkPicture> p2(recorder.finishRecordingAsPicture()); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 178 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 179 | SkString path2(path); |
| 180 | path2.append(".new.skp"); |
| 181 | SkFILEWStream writer(path2.c_str()); |
robertphillips@google.com | 84b18c7 | 2014-04-13 19:09:42 +0000 | [diff] [blame] | 182 | p2->serialize(&writer); |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 183 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 184 | } |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 185 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 186 | if (nullptr == pic) { |
| 187 | return nullptr; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 188 | } |
| 189 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 190 | SkAutoTDelete<SkBBHFactory> factory; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 191 | switch (bbox) { |
| 192 | case kNo_BBoxType: |
| 193 | // no bbox playback necessary |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 194 | return std::move(pic); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 195 | case kRTree_BBoxType: |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 196 | factory.reset(new SkRTreeFactory); |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 197 | break; |
commit-bot@chromium.org | bbe43a9 | 2013-12-10 21:51:06 +0000 | [diff] [blame] | 198 | default: |
| 199 | SkASSERT(false); |
| 200 | } |
| 201 | |
commit-bot@chromium.org | 5fb2ce3 | 2014-04-17 23:35:06 +0000 | [diff] [blame] | 202 | SkPictureRecorder recorder; |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 203 | pic->playback(recorder.beginRecording(pic->cullRect().width(), |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 204 | pic->cullRect().height(), |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 205 | factory.get(), 0)); |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 206 | return recorder.finishRecordingAsPicture(); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 207 | } |
| 208 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 209 | typedef SampleView INHERITED; |
| 210 | }; |
| 211 | |
| 212 | SampleView* CreateSamplePictFileView(const char filename[]); |
| 213 | SampleView* CreateSamplePictFileView(const char filename[]) { |
| 214 | return new PictFileView(filename); |
| 215 | } |
| 216 | |
| 217 | ////////////////////////////////////////////////////////////////////////////// |
| 218 | |
| 219 | #if 0 |
| 220 | static SkView* MyFactory() { return new PictFileView; } |
| 221 | static SkViewRegister reg(MyFactory); |
| 222 | #endif |