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