blob: 22c81d1d9461b96c439724fe45402f086e3a3e0b [file] [log] [blame]
reed@google.com1830c7a2012-06-04 12:05:43 +00001
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
31class PictFileView : public SampleView {
32 SkString fFilename;
33 SkPicture* fPicture;
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@google.comb1963742012-08-03 13:39:57 +000035 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.comd9f75032012-11-09 02:01:24 +000048 pic = SkNEW_ARGS(SkPicture,
robertphillips@google.com9eb96972012-11-08 13:46:32 +000049 (&stream, NULL, &SkImageDecoder::DecodeStream));
reed@google.comb1963742012-08-03 13:39:57 +000050 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000051
reed@google.com0a5c18b2012-08-31 13:32:47 +000052 if (false) { // re-record
53 SkPicture p2;
54 pic->draw(p2.beginRecording(pic->width(), pic->height()));
55 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000056
reed@google.com0a5c18b2012-08-31 13:32:47 +000057 SkString path2(path);
58 path2.append(".new.skp");
59 SkFILEWStream writer(path2.c_str());
60 p2.serialize(&writer);
61 }
reed@google.comb1963742012-08-03 13:39:57 +000062 }
63 return pic;
64 }
65
reed@google.com1830c7a2012-06-04 12:05:43 +000066public:
67 PictFileView(const char name[] = NULL) : fFilename(name) {
68 fPicture = NULL;
69 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000070
reed@google.com1830c7a2012-06-04 12:05:43 +000071 virtual ~PictFileView() {
72 SkSafeUnref(fPicture);
73 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
reed@google.com1830c7a2012-06-04 12:05:43 +000075protected:
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.comae933ce2012-08-23 18:19:56 +000086
reed@google.com1830c7a2012-06-04 12:05:43 +000087 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.comb1963742012-08-03 13:39:57 +000088 if (!fPicture) {
89 fPicture = LoadPicture(fFilename.c_str());
reed@google.com1830c7a2012-06-04 12:05:43 +000090 }
reed@google.comb1963742012-08-03 13:39:57 +000091 if (fPicture) {
92 canvas->drawPicture(*fPicture);
93 }
reed@google.com1830c7a2012-06-04 12:05:43 +000094 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000095
reed@google.comb1963742012-08-03 13:39:57 +000096private:
reed@google.com1830c7a2012-06-04 12:05:43 +000097 typedef SampleView INHERITED;
98};
99
100SampleView* CreateSamplePictFileView(const char filename[]);
101SampleView* CreateSamplePictFileView(const char filename[]) {
102 return new PictFileView(filename);
103}
104
105//////////////////////////////////////////////////////////////////////////////
106
107#if 0
108static SkView* MyFactory() { return new PictFileView; }
109static SkViewRegister reg(MyFactory);
110#endif
111