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