| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "SkDumpCanvas.h" | 9 #include "SkDumpCanvas.h" |
| 10 #include "SkView.h" | 10 #include "SkView.h" |
| 11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
| 12 #include "Sk64.h" | 12 #include "Sk64.h" |
| 13 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
| 14 #include "SkGraphics.h" | 14 #include "SkGraphics.h" |
| 15 #include "SkImageDecoder.h" | 15 #include "SkImageDecoder.h" |
| 16 #include "SkOSFile.h" | 16 #include "SkOSFile.h" |
| 17 #include "SkPath.h" | 17 #include "SkPath.h" |
| 18 #include "SkPicture.h" | 18 #include "SkPicture.h" |
| 19 #include "SkRandom.h" | 19 #include "SkRandom.h" |
| 20 #include "SkRegion.h" | 20 #include "SkRegion.h" |
| 21 #include "SkShader.h" | 21 #include "SkShader.h" |
| 22 #include "SkTileGridPicture.h" |
| 22 #include "SkUtils.h" | 23 #include "SkUtils.h" |
| 23 #include "SkColorPriv.h" | 24 #include "SkColorPriv.h" |
| 24 #include "SkColorFilter.h" | 25 #include "SkColorFilter.h" |
| 25 #include "SkTime.h" | 26 #include "SkTime.h" |
| 26 #include "SkTypeface.h" | 27 #include "SkTypeface.h" |
| 27 #include "SkXfermode.h" | 28 #include "SkXfermode.h" |
| 28 | 29 |
| 29 #include "SkStream.h" | 30 #include "SkStream.h" |
| 30 #include "SkSurface.h" | 31 #include "SkSurface.h" |
| 31 #include "SkXMLParser.h" | 32 #include "SkXMLParser.h" |
| 32 | 33 |
| 33 class PictFileView : public SampleView { | 34 class PictFileView : public SampleView { |
| 35 public: |
| 36 PictFileView(const char name[] = NULL) |
| 37 : fFilename(name) |
| 38 , fBBox(kNo_BBoxType) |
| 39 , fTileSize(SkSize::Make(0, 0)) { |
| 40 for (unsigned i = 0; i < kBBoxTypeCount; ++i) { |
| 41 fPictures[i] = NULL; |
| 42 } |
| 43 } |
| 44 |
| 45 virtual ~PictFileView() { |
| 46 for (unsigned i = 0; i < kBBoxTypeCount; ++i) { |
| 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 }; |
| 102 static const unsigned kBBoxTypeCount = kLast_BBoxType + 1; |
| 103 |
| 34 SkString fFilename; | 104 SkString fFilename; |
| 35 SkPicture* fPicture; | 105 SkPicture* fPictures[kBBoxTypeCount]; |
| 36 SkPicture* fBBoxPicture; | 106 BBoxType fBBox; |
| 37 bool fUseBBox; | 107 SkSize fTileSize; |
| 38 | 108 |
| 39 static SkPicture* LoadPicture(const char path[], bool useBBox) { | 109 SkPicture* LoadPicture(const char path[], BBoxType bbox) { |
| 40 SkPicture* pic = NULL; | 110 SkPicture* pic = NULL; |
| 41 | 111 |
| 42 SkBitmap bm; | 112 SkBitmap bm; |
| 43 if (SkImageDecoder::DecodeFile(path, &bm)) { | 113 if (SkImageDecoder::DecodeFile(path, &bm)) { |
| 44 bm.setImmutable(); | 114 bm.setImmutable(); |
| 45 pic = SkNEW(SkPicture); | 115 pic = SkNEW(SkPicture); |
| 46 SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); | 116 SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); |
| 47 can->drawBitmap(bm, 0, 0, NULL); | 117 can->drawBitmap(bm, 0, 0, NULL); |
| 48 pic->endRecording(); | 118 pic->endRecording(); |
| 49 } else { | 119 } else { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 64 pic->draw(p2.beginRecording(pic->width(), pic->height())); | 134 pic->draw(p2.beginRecording(pic->width(), pic->height())); |
| 65 p2.endRecording(); | 135 p2.endRecording(); |
| 66 | 136 |
| 67 SkString path2(path); | 137 SkString path2(path); |
| 68 path2.append(".new.skp"); | 138 path2.append(".new.skp"); |
| 69 SkFILEWStream writer(path2.c_str()); | 139 SkFILEWStream writer(path2.c_str()); |
| 70 p2.serialize(&writer); | 140 p2.serialize(&writer); |
| 71 } | 141 } |
| 72 } | 142 } |
| 73 | 143 |
| 74 if (useBBox) { | 144 if (!pic) { |
| 75 SkPicture* bboxPicture = SkNEW(SkPicture); | 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->heig
ht(), gridInfo)); |
| 163 } break; |
| 164 default: |
| 165 SkASSERT(false); |
| 166 } |
| 167 |
| 168 if (bboxPicture) { |
| 76 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(), | 169 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(), |
| 77 SkPicture::kOptimizeForClippedPlayback_RecordingFlag)); | 170 SkPicture::kOptimizeForClippedPlayback_RecordingFlag)); |
| 78 bboxPicture->endRecording(); | 171 bboxPicture->endRecording(); |
| 79 SkDELETE(pic); | 172 SkDELETE(pic); |
| 80 return bboxPicture; | 173 return bboxPicture; |
| 174 } |
| 81 | 175 |
| 82 } else { | 176 return pic; |
| 83 return pic; | |
| 84 } | |
| 85 } | 177 } |
| 86 | 178 |
| 87 public: | |
| 88 PictFileView(const char name[] = NULL) : fFilename(name) { | |
| 89 fPicture = NULL; | |
| 90 fBBoxPicture = NULL; | |
| 91 fUseBBox = false; | |
| 92 } | |
| 93 | |
| 94 virtual ~PictFileView() { | |
| 95 SkSafeUnref(fPicture); | |
| 96 SkSafeUnref(fBBoxPicture); | |
| 97 } | |
| 98 | |
| 99 protected: | |
| 100 // overrides from SkEventSink | |
| 101 virtual bool onQuery(SkEvent* evt) { | |
| 102 if (SampleCode::TitleQ(*evt)) { | |
| 103 SkString name("P:"); | |
| 104 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); | |
| 105 name.append(basename ? basename+1: fFilename.c_str()); | |
| 106 if (fUseBBox) { | |
| 107 name.append(" <bbox>"); | |
| 108 } | |
| 109 SampleCode::TitleR(evt, name.c_str()); | |
| 110 return true; | |
| 111 } | |
| 112 return this->INHERITED::onQuery(evt); | |
| 113 } | |
| 114 | |
| 115 virtual bool onEvent(const SkEvent& evt) { | |
| 116 if (evt.isType("PictFileView::toggleBBox")) { | |
| 117 fUseBBox = !fUseBBox; | |
| 118 return true; | |
| 119 } | |
| 120 return this->INHERITED::onEvent(evt); | |
| 121 } | |
| 122 | |
| 123 virtual void onDrawContent(SkCanvas* canvas) { | |
| 124 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture; | |
| 125 | |
| 126 if (!*picture) { | |
| 127 *picture = LoadPicture(fFilename.c_str(), fUseBBox); | |
| 128 } | |
| 129 if (*picture) { | |
| 130 canvas->drawPicture(**picture); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 private: | |
| 135 typedef SampleView INHERITED; | 179 typedef SampleView INHERITED; |
| 136 }; | 180 }; |
| 137 | 181 |
| 138 SampleView* CreateSamplePictFileView(const char filename[]); | 182 SampleView* CreateSamplePictFileView(const char filename[]); |
| 139 SampleView* CreateSamplePictFileView(const char filename[]) { | 183 SampleView* CreateSamplePictFileView(const char filename[]) { |
| 140 return new PictFileView(filename); | 184 return new PictFileView(filename); |
| 141 } | 185 } |
| 142 | 186 |
| 143 ////////////////////////////////////////////////////////////////////////////// | 187 ////////////////////////////////////////////////////////////////////////////// |
| 144 | 188 |
| 145 #if 0 | 189 #if 0 |
| 146 static SkView* MyFactory() { return new PictFileView; } | 190 static SkView* MyFactory() { return new PictFileView; } |
| 147 static SkViewRegister reg(MyFactory); | 191 static SkViewRegister reg(MyFactory); |
| 148 #endif | 192 #endif |
| OLD | NEW |