blob: b24f9081363a92882cbed593b968b647837b6f2d [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;
reed@google.comb1963742012-08-03 13:39:57 +000034
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 }
50 }
51 return pic;
52 }
53
reed@google.com1830c7a2012-06-04 12:05:43 +000054public:
55 PictFileView(const char name[] = NULL) : fFilename(name) {
56 fPicture = NULL;
57 }
58
59 virtual ~PictFileView() {
60 SkSafeUnref(fPicture);
61 }
62
63protected:
64 // overrides from SkEventSink
65 virtual bool onQuery(SkEvent* evt) {
66 if (SampleCode::TitleQ(*evt)) {
67 SkString name("P:");
68 name.append(fFilename);
69 SampleCode::TitleR(evt, name.c_str());
70 return true;
71 }
72 return this->INHERITED::onQuery(evt);
73 }
74
75 virtual void onDrawContent(SkCanvas* canvas) {
reed@google.comb1963742012-08-03 13:39:57 +000076 if (!fPicture) {
77 fPicture = LoadPicture(fFilename.c_str());
reed@google.com1830c7a2012-06-04 12:05:43 +000078 }
reed@google.comb1963742012-08-03 13:39:57 +000079 if (fPicture) {
80 canvas->drawPicture(*fPicture);
81 }
reed@google.com1830c7a2012-06-04 12:05:43 +000082 }
83
reed@google.comb1963742012-08-03 13:39:57 +000084private:
reed@google.com1830c7a2012-06-04 12:05:43 +000085 typedef SampleView INHERITED;
86};
87
88SampleView* CreateSamplePictFileView(const char filename[]);
89SampleView* CreateSamplePictFileView(const char filename[]) {
90 return new PictFileView(filename);
91}
92
93//////////////////////////////////////////////////////////////////////////////
94
95#if 0
96static SkView* MyFactory() { return new PictFileView; }
97static SkViewRegister reg(MyFactory);
98#endif
99