blob: 847894ce1a1618e108557d8aa5107cf71bc6501a [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.comb8b830e2013-06-25 20:42:37 +000052 } else {
53 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +000054 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000055
reed@google.com0a5c18b2012-08-31 13:32:47 +000056 if (false) { // re-record
57 SkPicture p2;
58 pic->draw(p2.beginRecording(pic->width(), pic->height()));
59 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000060
reed@google.com0a5c18b2012-08-31 13:32:47 +000061 SkString path2(path);
62 path2.append(".new.skp");
63 SkFILEWStream writer(path2.c_str());
64 p2.serialize(&writer);
65 }
reed@google.comb1963742012-08-03 13:39:57 +000066 }
djsollen@google.com796763e2012-12-10 14:12:55 +000067
68 if (useBBox) {
69 SkPicture* bboxPicture = SkNEW(SkPicture);
70 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
71 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
72 bboxPicture->endRecording();
73 SkDELETE(pic);
74 return bboxPicture;
75
76 } else {
77 return pic;
78 }
reed@google.comb1963742012-08-03 13:39:57 +000079 }
80
reed@google.com1830c7a2012-06-04 12:05:43 +000081public:
82 PictFileView(const char name[] = NULL) : fFilename(name) {
83 fPicture = NULL;
djsollen@google.com796763e2012-12-10 14:12:55 +000084 fBBoxPicture = NULL;
85 fUseBBox = false;
reed@google.com1830c7a2012-06-04 12:05:43 +000086 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
reed@google.com1830c7a2012-06-04 12:05:43 +000088 virtual ~PictFileView() {
89 SkSafeUnref(fPicture);
djsollen@google.com796763e2012-12-10 14:12:55 +000090 SkSafeUnref(fBBoxPicture);
reed@google.com1830c7a2012-06-04 12:05:43 +000091 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@google.com1830c7a2012-06-04 12:05:43 +000093protected:
94 // overrides from SkEventSink
95 virtual bool onQuery(SkEvent* evt) {
96 if (SampleCode::TitleQ(*evt)) {
97 SkString name("P:");
djsollen@google.com796763e2012-12-10 14:12:55 +000098 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
99 name.append(basename ? basename+1: fFilename.c_str());
100 if (fUseBBox) {
101 name.append(" <bbox>");
102 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000103 SampleCode::TitleR(evt, name.c_str());
104 return true;
105 }
106 return this->INHERITED::onQuery(evt);
107 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000108
djsollen@google.com796763e2012-12-10 14:12:55 +0000109 virtual bool onEvent(const SkEvent& evt) {
110 if (evt.isType("PictFileView::toggleBBox")) {
111 fUseBBox = !fUseBBox;
112 return true;
djsollen@google.com57c29f72012-12-10 13:55:02 +0000113 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000114 return this->INHERITED::onEvent(evt);
115 }
116
117 virtual void onDrawContent(SkCanvas* canvas) {
118 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
119
120 if (!*picture) {
121 *picture = LoadPicture(fFilename.c_str(), fUseBBox);
122 }
123 if (*picture) {
124 canvas->drawPicture(**picture);
reed@google.comb1963742012-08-03 13:39:57 +0000125 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000126 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000127
reed@google.comb1963742012-08-03 13:39:57 +0000128private:
reed@google.com1830c7a2012-06-04 12:05:43 +0000129 typedef SampleView INHERITED;
130};
131
132SampleView* CreateSamplePictFileView(const char filename[]);
133SampleView* CreateSamplePictFileView(const char filename[]) {
134 return new PictFileView(filename);
135}
136
137//////////////////////////////////////////////////////////////////////////////
138
139#if 0
140static SkView* MyFactory() { return new PictFileView; }
141static SkViewRegister reg(MyFactory);
142#endif