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()) { |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 48 | pic = SkNEW_ARGS(SkPicture, |
robertphillips@google.com | 9eb9697 | 2012-11-08 13:46:32 +0000 | [diff] [blame] | 49 | (&stream, NULL, &SkImageDecoder::DecodeStream)); |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 50 | } |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 51 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 52 | if (false) { // re-record |
| 53 | SkPicture p2; |
| 54 | pic->draw(p2.beginRecording(pic->width(), pic->height())); |
| 55 | p2.endRecording(); |
skia.committer@gmail.com | 11f8692 | 2012-08-31 17:14:46 +0000 | [diff] [blame] | 56 | |
reed@google.com | 0a5c18b | 2012-08-31 13:32:47 +0000 | [diff] [blame] | 57 | SkString path2(path); |
| 58 | path2.append(".new.skp"); |
| 59 | SkFILEWStream writer(path2.c_str()); |
| 60 | p2.serialize(&writer); |
| 61 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 62 | } |
| 63 | return pic; |
| 64 | } |
| 65 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 66 | public: |
| 67 | PictFileView(const char name[] = NULL) : fFilename(name) { |
| 68 | fPicture = NULL; |
| 69 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 71 | virtual ~PictFileView() { |
| 72 | SkSafeUnref(fPicture); |
| 73 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 75 | protected: |
| 76 | // overrides from SkEventSink |
| 77 | virtual bool onQuery(SkEvent* evt) { |
| 78 | if (SampleCode::TitleQ(*evt)) { |
| 79 | SkString name("P:"); |
| 80 | name.append(fFilename); |
| 81 | SampleCode::TitleR(evt, name.c_str()); |
| 82 | return true; |
| 83 | } |
| 84 | return this->INHERITED::onQuery(evt); |
| 85 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 87 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 88 | if (!fPicture) { |
| 89 | fPicture = LoadPicture(fFilename.c_str()); |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 90 | } |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 91 | if (fPicture) { |
| 92 | canvas->drawPicture(*fPicture); |
| 93 | } |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 94 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
reed@google.com | b196374 | 2012-08-03 13:39:57 +0000 | [diff] [blame] | 96 | private: |
reed@google.com | 1830c7a | 2012-06-04 12:05:43 +0000 | [diff] [blame] | 97 | typedef SampleView INHERITED; |
| 98 | }; |
| 99 | |
| 100 | SampleView* CreateSamplePictFileView(const char filename[]); |
| 101 | SampleView* CreateSamplePictFileView(const char filename[]) { |
| 102 | return new PictFileView(filename); |
| 103 | } |
| 104 | |
| 105 | ////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | #if 0 |
| 108 | static SkView* MyFactory() { return new PictFileView; } |
| 109 | static SkViewRegister reg(MyFactory); |
| 110 | #endif |
| 111 | |