blob: c82d03e3ea8f278e423a1eb519b64c9867009b52 [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"
djsollen@google.com796763e2012-12-10 14:12:55 +000016#include "SkOSFile.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000017#include "SkPath.h"
18#include "SkPicture.h"
19#include "SkRandom.h"
20#include "SkRegion.h"
21#include "SkShader.h"
22#include "SkUtils.h"
23#include "SkColorPriv.h"
24#include "SkColorFilter.h"
25#include "SkTime.h"
26#include "SkTypeface.h"
27#include "SkXfermode.h"
28
29#include "SkStream.h"
30#include "SkXMLParser.h"
31
32class PictFileView : public SampleView {
33 SkString fFilename;
34 SkPicture* fPicture;
djsollen@google.com796763e2012-12-10 14:12:55 +000035 SkPicture* fBBoxPicture;
36 bool fUseBBox;
rmistry@google.comae933ce2012-08-23 18:19:56 +000037
djsollen@google.com796763e2012-12-10 14:12:55 +000038 static SkPicture* LoadPicture(const char path[], bool useBBox) {
reed@google.comb1963742012-08-03 13:39:57 +000039 SkPicture* pic = NULL;
40
41 SkBitmap bm;
42 if (SkImageDecoder::DecodeFile(path, &bm)) {
43 bm.setImmutable();
44 pic = SkNEW(SkPicture);
45 SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
46 can->drawBitmap(bm, 0, 0, NULL);
47 pic->endRecording();
48 } else {
49 SkFILEStream stream(path);
50 if (stream.isValid()) {
scroggo@google.comf8d7d272013-02-22 21:38:35 +000051 pic = SkNEW_ARGS(SkPicture, (&stream, NULL, &SkImageDecoder::DecodeMemory));
reed@google.comb1963742012-08-03 13:39:57 +000052 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000053
reed@google.com0a5c18b2012-08-31 13:32:47 +000054 if (false) { // re-record
55 SkPicture p2;
56 pic->draw(p2.beginRecording(pic->width(), pic->height()));
57 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000058
reed@google.com0a5c18b2012-08-31 13:32:47 +000059 SkString path2(path);
60 path2.append(".new.skp");
61 SkFILEWStream writer(path2.c_str());
62 p2.serialize(&writer);
63 }
reed@google.comb1963742012-08-03 13:39:57 +000064 }
djsollen@google.com796763e2012-12-10 14:12:55 +000065
66 if (useBBox) {
67 SkPicture* bboxPicture = SkNEW(SkPicture);
68 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
69 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
70 bboxPicture->endRecording();
71 SkDELETE(pic);
72 return bboxPicture;
73
74 } else {
75 return pic;
76 }
reed@google.comb1963742012-08-03 13:39:57 +000077 }
78
reed@google.com1830c7a2012-06-04 12:05:43 +000079public:
80 PictFileView(const char name[] = NULL) : fFilename(name) {
81 fPicture = NULL;
djsollen@google.com796763e2012-12-10 14:12:55 +000082 fBBoxPicture = NULL;
83 fUseBBox = false;
reed@google.com1830c7a2012-06-04 12:05:43 +000084 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000085
reed@google.com1830c7a2012-06-04 12:05:43 +000086 virtual ~PictFileView() {
87 SkSafeUnref(fPicture);
djsollen@google.com796763e2012-12-10 14:12:55 +000088 SkSafeUnref(fBBoxPicture);
reed@google.com1830c7a2012-06-04 12:05:43 +000089 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@google.com1830c7a2012-06-04 12:05:43 +000091protected:
92 // overrides from SkEventSink
93 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
95 SkString name("P:");
djsollen@google.com796763e2012-12-10 14:12:55 +000096 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
97 name.append(basename ? basename+1: fFilename.c_str());
98 if (fUseBBox) {
99 name.append(" <bbox>");
100 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000101 SampleCode::TitleR(evt, name.c_str());
102 return true;
103 }
104 return this->INHERITED::onQuery(evt);
105 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
djsollen@google.com796763e2012-12-10 14:12:55 +0000107 virtual bool onEvent(const SkEvent& evt) {
108 if (evt.isType("PictFileView::toggleBBox")) {
109 fUseBBox = !fUseBBox;
110 return true;
djsollen@google.com57c29f72012-12-10 13:55:02 +0000111 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000112 return this->INHERITED::onEvent(evt);
113 }
114
115 virtual void onDrawContent(SkCanvas* canvas) {
116 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
117
118 if (!*picture) {
119 *picture = LoadPicture(fFilename.c_str(), fUseBBox);
120 }
121 if (*picture) {
122 canvas->drawPicture(**picture);
reed@google.comb1963742012-08-03 13:39:57 +0000123 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000124 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
reed@google.comb1963742012-08-03 13:39:57 +0000126private:
reed@google.com1830c7a2012-06-04 12:05:43 +0000127 typedef SampleView INHERITED;
128};
129
130SampleView* CreateSamplePictFileView(const char filename[]);
131SampleView* CreateSamplePictFileView(const char filename[]) {
132 return new PictFileView(filename);
133}
134
135//////////////////////////////////////////////////////////////////////////////
136
137#if 0
138static SkView* MyFactory() { return new PictFileView; }
139static SkViewRegister reg(MyFactory);
140#endif