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