blob: 014ae9365545964f2305902c52e8312ab9d4780e [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()) {
48 pic = SkNEW_ARGS(SkPicture, (&stream));
49 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000050
reed@google.com0a5c18b2012-08-31 13:32:47 +000051 if (false) { // re-record
52 SkPicture p2;
53 pic->draw(p2.beginRecording(pic->width(), pic->height()));
54 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000055
reed@google.com0a5c18b2012-08-31 13:32:47 +000056 SkString path2(path);
57 path2.append(".new.skp");
58 SkFILEWStream writer(path2.c_str());
59 p2.serialize(&writer);
60 }
reed@google.comb1963742012-08-03 13:39:57 +000061 }
62 return pic;
63 }
64
reed@google.com1830c7a2012-06-04 12:05:43 +000065public:
66 PictFileView(const char name[] = NULL) : fFilename(name) {
67 fPicture = NULL;
68 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@google.com1830c7a2012-06-04 12:05:43 +000070 virtual ~PictFileView() {
71 SkSafeUnref(fPicture);
72 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000073
reed@google.com1830c7a2012-06-04 12:05:43 +000074protected:
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.comae933ce2012-08-23 18:19:56 +000085
reed@google.com1830c7a2012-06-04 12:05:43 +000086 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.comb1963742012-08-03 13:39:57 +000087 if (!fPicture) {
88 fPicture = LoadPicture(fFilename.c_str());
reed@google.com1830c7a2012-06-04 12:05:43 +000089 }
reed@google.comb1963742012-08-03 13:39:57 +000090 if (fPicture) {
91 canvas->drawPicture(*fPicture);
92 }
reed@google.com1830c7a2012-06-04 12:05:43 +000093 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
reed@google.comb1963742012-08-03 13:39:57 +000095private:
reed@google.com1830c7a2012-06-04 12:05:43 +000096 typedef SampleView INHERITED;
97};
98
99SampleView* CreateSamplePictFileView(const char filename[]);
100SampleView* CreateSamplePictFileView(const char filename[]) {
101 return new PictFileView(filename);
102}
103
104//////////////////////////////////////////////////////////////////////////////
105
106#if 0
107static SkView* MyFactory() { return new PictFileView; }
108static SkViewRegister reg(MyFactory);
109#endif
110