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