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" |
| 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" |
| 30 | #include "SkXMLParser.h" |
| 31 | |
| 32 | class PictFileView : public SampleView { |
| 33 | SkString fFilename; |
| 34 | SkPicture* fPicture; |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 35 | SkPicture* fBBoxPicture; |
| 36 | bool fUseBBox; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 37 | |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 38 | static SkPicture* LoadPicture(const char path[], bool useBBox) { |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 39 | SkPicture* pic = NULL; |
| 40 | |
| 41 | SkBitmap bm; |
| 42 | if (SkImageDecoder::DecodeFile(path, &bm)) { |
| 43 | bm.setImmutable(); |
| 44 | pic = SkNEW(SkPicture); |
| 45 | SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); |
| 46 | can->drawBitmap(bm, 0, 0, NULL); |
| 47 | pic->endRecording(); |
| 48 | } else { |
| 49 | SkFILEStream stream(path); |
| 50 | if (stream.isValid()) { |
scroggo@google.com | f1754ec | 2013-06-28 21:32:00 +0000 | [diff] [blame] | 51 | pic = SkPicture::CreateFromStream(&stream); |
reed@google.com | b8b830e | 2013-06-25 20:42:37 +0000 | [diff] [blame] | 52 | } else { |
| 53 | SkDebugf("coun't load picture at \"path\"\n", path); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 54 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 55 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 56 | if (false) { // re-record |
| 57 | SkPicture p2; |
| 58 | pic->draw(p2.beginRecording(pic->width(), pic->height())); |
| 59 | p2.endRecording(); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 60 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 61 | SkString path2(path); |
| 62 | path2.append(".new.skp"); |
| 63 | SkFILEWStream writer(path2.c_str()); |
| 64 | p2.serialize(&writer); |
| 65 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 66 | } |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 67 | |
| 68 | if (useBBox) { |
| 69 | SkPicture* bboxPicture = SkNEW(SkPicture); |
| 70 | pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(), |
| 71 | SkPicture::kOptimizeForClippedPlayback_RecordingFlag)); |
| 72 | bboxPicture->endRecording(); |
| 73 | SkDELETE(pic); |
| 74 | return bboxPicture; |
| 75 | |
| 76 | } else { |
| 77 | return pic; |
| 78 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 79 | } |
| 80 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 81 | public: |
| 82 | PictFileView(const char name[] = NULL) : fFilename(name) { |
| 83 | fPicture = NULL; |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 84 | fBBoxPicture = NULL; |
| 85 | fUseBBox = false; |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 86 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 88 | virtual ~PictFileView() { |
| 89 | SkSafeUnref(fPicture); |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 90 | SkSafeUnref(fBBoxPicture); |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 91 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 93 | protected: |
| 94 | // overrides from SkEventSink |
| 95 | virtual bool onQuery(SkEvent* evt) { |
| 96 | if (SampleCode::TitleQ(*evt)) { |
| 97 | SkString name("P:"); |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 98 | const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
| 99 | name.append(basename ? basename+1: fFilename.c_str()); |
| 100 | if (fUseBBox) { |
| 101 | name.append(" <bbox>"); |
| 102 | } |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 103 | SampleCode::TitleR(evt, name.c_str()); |
| 104 | return true; |
| 105 | } |
| 106 | return this->INHERITED::onQuery(evt); |
| 107 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 108 | |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 109 | virtual bool onEvent(const SkEvent& evt) { |
| 110 | if (evt.isType("PictFileView::toggleBBox")) { |
| 111 | fUseBBox = !fUseBBox; |
| 112 | return true; |
djsollen@google.com | 57c29f7 | 2012-12-10 13:55:02 +0000 | [diff] [blame] | 113 | } |
djsollen@google.com | 796763e | 2012-12-10 14:12:55 +0000 | [diff] [blame] | 114 | return this->INHERITED::onEvent(evt); |
| 115 | } |
| 116 | |
| 117 | virtual void onDrawContent(SkCanvas* canvas) { |
| 118 | SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture; |
| 119 | |
| 120 | if (!*picture) { |
| 121 | *picture = LoadPicture(fFilename.c_str(), fUseBBox); |
| 122 | } |
| 123 | if (*picture) { |
| 124 | canvas->drawPicture(**picture); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 125 | } |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 126 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 127 | |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 128 | private: |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 129 | typedef SampleView INHERITED; |
| 130 | }; |
| 131 | |
| 132 | SampleView* CreateSamplePictFileView(const char filename[]); |
| 133 | SampleView* CreateSamplePictFileView(const char filename[]) { |
| 134 | return new PictFileView(filename); |
| 135 | } |
| 136 | |
| 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
| 139 | #if 0 |
| 140 | static SkView* MyFactory() { return new PictFileView; } |
| 141 | static SkViewRegister reg(MyFactory); |
| 142 | #endif |