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