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