blob: b8298d4e19d2f34695adeab724a383147df01da7 [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"
reed@google.com636d87a2013-09-17 20:03:43 +000030#include "SkSurface.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000031#include "SkXMLParser.h"
32
33class PictFileView : public SampleView {
34 SkString fFilename;
35 SkPicture* fPicture;
djsollen@google.com796763e2012-12-10 14:12:55 +000036 SkPicture* fBBoxPicture;
37 bool fUseBBox;
rmistry@google.comae933ce2012-08-23 18:19:56 +000038
djsollen@google.com796763e2012-12-10 14:12:55 +000039 static SkPicture* LoadPicture(const char path[], bool useBBox) {
reed@google.comb1963742012-08-03 13:39:57 +000040 SkPicture* pic = NULL;
41
42 SkBitmap bm;
43 if (SkImageDecoder::DecodeFile(path, &bm)) {
44 bm.setImmutable();
45 pic = SkNEW(SkPicture);
46 SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
47 can->drawBitmap(bm, 0, 0, NULL);
48 pic->endRecording();
49 } else {
50 SkFILEStream stream(path);
51 if (stream.isValid()) {
scroggo@google.comf1754ec2013-06-28 21:32:00 +000052 pic = SkPicture::CreateFromStream(&stream);
reed@google.comb8b830e2013-06-25 20:42:37 +000053 } else {
54 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +000055 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000056
reed@google.com636d87a2013-09-17 20:03:43 +000057 if (false) {
58 SkSurface* surf = SkSurface::NewRasterPMColor(pic->width(), pic->height());
59 surf->getCanvas()->drawPicture(*pic);
60 surf->unref();
61 }
reed@google.com0a5c18b2012-08-31 13:32:47 +000062 if (false) { // re-record
63 SkPicture p2;
64 pic->draw(p2.beginRecording(pic->width(), pic->height()));
65 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000066
reed@google.com0a5c18b2012-08-31 13:32:47 +000067 SkString path2(path);
68 path2.append(".new.skp");
69 SkFILEWStream writer(path2.c_str());
70 p2.serialize(&writer);
71 }
reed@google.comb1963742012-08-03 13:39:57 +000072 }
djsollen@google.com796763e2012-12-10 14:12:55 +000073
74 if (useBBox) {
75 SkPicture* bboxPicture = SkNEW(SkPicture);
76 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
77 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
78 bboxPicture->endRecording();
79 SkDELETE(pic);
80 return bboxPicture;
81
82 } else {
83 return pic;
84 }
reed@google.comb1963742012-08-03 13:39:57 +000085 }
86
reed@google.com1830c7a2012-06-04 12:05:43 +000087public:
88 PictFileView(const char name[] = NULL) : fFilename(name) {
89 fPicture = NULL;
djsollen@google.com796763e2012-12-10 14:12:55 +000090 fBBoxPicture = NULL;
91 fUseBBox = false;
reed@google.com1830c7a2012-06-04 12:05:43 +000092 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
reed@google.com1830c7a2012-06-04 12:05:43 +000094 virtual ~PictFileView() {
95 SkSafeUnref(fPicture);
djsollen@google.com796763e2012-12-10 14:12:55 +000096 SkSafeUnref(fBBoxPicture);
reed@google.com1830c7a2012-06-04 12:05:43 +000097 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000098
reed@google.com1830c7a2012-06-04 12:05:43 +000099protected:
100 // overrides from SkEventSink
101 virtual bool onQuery(SkEvent* evt) {
102 if (SampleCode::TitleQ(*evt)) {
103 SkString name("P:");
djsollen@google.com796763e2012-12-10 14:12:55 +0000104 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
105 name.append(basename ? basename+1: fFilename.c_str());
106 if (fUseBBox) {
107 name.append(" <bbox>");
108 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000109 SampleCode::TitleR(evt, name.c_str());
110 return true;
111 }
112 return this->INHERITED::onQuery(evt);
113 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
djsollen@google.com796763e2012-12-10 14:12:55 +0000115 virtual bool onEvent(const SkEvent& evt) {
116 if (evt.isType("PictFileView::toggleBBox")) {
117 fUseBBox = !fUseBBox;
118 return true;
djsollen@google.com57c29f72012-12-10 13:55:02 +0000119 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000120 return this->INHERITED::onEvent(evt);
121 }
122
123 virtual void onDrawContent(SkCanvas* canvas) {
124 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
125
126 if (!*picture) {
127 *picture = LoadPicture(fFilename.c_str(), fUseBBox);
128 }
129 if (*picture) {
130 canvas->drawPicture(**picture);
reed@google.comb1963742012-08-03 13:39:57 +0000131 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000132 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000133
reed@google.comb1963742012-08-03 13:39:57 +0000134private:
reed@google.com1830c7a2012-06-04 12:05:43 +0000135 typedef SampleView INHERITED;
136};
137
138SampleView* CreateSamplePictFileView(const char filename[]);
139SampleView* CreateSamplePictFileView(const char filename[]) {
140 return new PictFileView(filename);
141}
142
143//////////////////////////////////////////////////////////////////////////////
144
145#if 0
146static SkView* MyFactory() { return new PictFileView; }
147static SkViewRegister reg(MyFactory);
148#endif