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