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" |
| 16 | #include "SkPath.h" |
| 17 | #include "SkPicture.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkRegion.h" |
| 20 | #include "SkShader.h" |
| 21 | #include "SkUtils.h" |
| 22 | #include "SkColorPriv.h" |
| 23 | #include "SkColorFilter.h" |
| 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
| 26 | #include "SkXfermode.h" |
| 27 | |
| 28 | #include "SkStream.h" |
| 29 | #include "SkXMLParser.h" |
| 30 | |
| 31 | class PictFileView : public SampleView { |
| 32 | SkString fFilename; |
| 33 | SkPicture* fPicture; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 34 | |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 35 | static SkPicture* LoadPicture(const char path[]) { |
| 36 | SkPicture* pic = NULL; |
| 37 | |
| 38 | SkBitmap bm; |
| 39 | if (SkImageDecoder::DecodeFile(path, &bm)) { |
| 40 | bm.setImmutable(); |
| 41 | pic = SkNEW(SkPicture); |
| 42 | SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); |
| 43 | can->drawBitmap(bm, 0, 0, NULL); |
| 44 | pic->endRecording(); |
| 45 | } else { |
| 46 | SkFILEStream stream(path); |
| 47 | if (stream.isValid()) { |
| 48 | pic = SkNEW_ARGS(SkPicture, (&stream)); |
| 49 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 50 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 51 | if (false) { // re-record |
| 52 | SkPicture p2; |
| 53 | pic->draw(p2.beginRecording(pic->width(), pic->height())); |
| 54 | p2.endRecording(); |
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 | SkString path2(path); |
| 57 | path2.append(".new.skp"); |
| 58 | SkFILEWStream writer(path2.c_str()); |
| 59 | p2.serialize(&writer); |
| 60 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 61 | } |
| 62 | return pic; |
| 63 | } |
| 64 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 65 | public: |
| 66 | PictFileView(const char name[] = NULL) : fFilename(name) { |
| 67 | fPicture = NULL; |
| 68 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 69 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 70 | virtual ~PictFileView() { |
| 71 | SkSafeUnref(fPicture); |
| 72 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 73 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 74 | protected: |
| 75 | // overrides from SkEventSink |
| 76 | virtual bool onQuery(SkEvent* evt) { |
| 77 | if (SampleCode::TitleQ(*evt)) { |
| 78 | SkString name("P:"); |
| 79 | name.append(fFilename); |
| 80 | SampleCode::TitleR(evt, name.c_str()); |
| 81 | return true; |
| 82 | } |
| 83 | return this->INHERITED::onQuery(evt); |
| 84 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 86 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 87 | if (!fPicture) { |
| 88 | fPicture = LoadPicture(fFilename.c_str()); |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 89 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 90 | if (fPicture) { |
| 91 | canvas->drawPicture(*fPicture); |
| 92 | } |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 93 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 94 | |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 95 | private: |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 96 | typedef SampleView INHERITED; |
| 97 | }; |
| 98 | |
| 99 | SampleView* CreateSamplePictFileView(const char filename[]); |
| 100 | SampleView* CreateSamplePictFileView(const char filename[]) { |
| 101 | return new PictFileView(filename); |
| 102 | } |
| 103 | |
| 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | #if 0 |
| 107 | static SkView* MyFactory() { return new PictFileView; } |
| 108 | static SkViewRegister reg(MyFactory); |
| 109 | #endif |
| 110 | |