blob: b53ba83600700742bfb48ca1c6fa3c16bf6ad4ad [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()) {
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +000051 pic = SkNEW_ARGS(SkPicture,
robertphillips@google.com9eb96972012-11-08 13:46:32 +000052 (&stream, NULL, &SkImageDecoder::DecodeStream));
reed@google.comb1963742012-08-03 13:39:57 +000053 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000054
reed@google.com0a5c18b2012-08-31 13:32:47 +000055 if (false) { // re-record
56 SkPicture p2;
57 pic->draw(p2.beginRecording(pic->width(), pic->height()));
58 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +000059
reed@google.com0a5c18b2012-08-31 13:32:47 +000060 SkString path2(path);
61 path2.append(".new.skp");
62 SkFILEWStream writer(path2.c_str());
63 p2.serialize(&writer);
64 }
reed@google.comb1963742012-08-03 13:39:57 +000065 }
djsollen@google.com796763e2012-12-10 14:12:55 +000066
67 if (useBBox) {
68 SkPicture* bboxPicture = SkNEW(SkPicture);
69 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
70 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
71 bboxPicture->endRecording();
72 SkDELETE(pic);
73 return bboxPicture;
74
75 } else {
76 return pic;
77 }
reed@google.comb1963742012-08-03 13:39:57 +000078 }
79
reed@google.com1830c7a2012-06-04 12:05:43 +000080public:
81 PictFileView(const char name[] = NULL) : fFilename(name) {
82 fPicture = NULL;
djsollen@google.com796763e2012-12-10 14:12:55 +000083 fBBoxPicture = NULL;
84 fUseBBox = false;
reed@google.com1830c7a2012-06-04 12:05:43 +000085 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000086
reed@google.com1830c7a2012-06-04 12:05:43 +000087 virtual ~PictFileView() {
88 SkSafeUnref(fPicture);
djsollen@google.com796763e2012-12-10 14:12:55 +000089 SkSafeUnref(fBBoxPicture);
reed@google.com1830c7a2012-06-04 12:05:43 +000090 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000091
reed@google.com1830c7a2012-06-04 12:05:43 +000092protected:
93 // overrides from SkEventSink
94 virtual bool onQuery(SkEvent* evt) {
95 if (SampleCode::TitleQ(*evt)) {
96 SkString name("P:");
djsollen@google.com796763e2012-12-10 14:12:55 +000097 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
98 name.append(basename ? basename+1: fFilename.c_str());
99 if (fUseBBox) {
100 name.append(" <bbox>");
101 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000102 SampleCode::TitleR(evt, name.c_str());
103 return true;
104 }
105 return this->INHERITED::onQuery(evt);
106 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
djsollen@google.com796763e2012-12-10 14:12:55 +0000108 virtual bool onEvent(const SkEvent& evt) {
109 if (evt.isType("PictFileView::toggleBBox")) {
110 fUseBBox = !fUseBBox;
111 return true;
djsollen@google.com57c29f72012-12-10 13:55:02 +0000112 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000113 return this->INHERITED::onEvent(evt);
114 }
115
116 virtual void onDrawContent(SkCanvas* canvas) {
117 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture;
118
119 if (!*picture) {
120 *picture = LoadPicture(fFilename.c_str(), fUseBBox);
121 }
122 if (*picture) {
123 canvas->drawPicture(**picture);
reed@google.comb1963742012-08-03 13:39:57 +0000124 }
reed@google.com1830c7a2012-06-04 12:05:43 +0000125 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000126
reed@google.comb1963742012-08-03 13:39:57 +0000127private:
reed@google.com1830c7a2012-06-04 12:05:43 +0000128 typedef SampleView INHERITED;
129};
130
131SampleView* CreateSamplePictFileView(const char filename[]);
132SampleView* CreateSamplePictFileView(const char filename[]) {
133 return new PictFileView(filename);
134}
135
136//////////////////////////////////////////////////////////////////////////////
137
138#if 0
139static SkView* MyFactory() { return new PictFileView; }
140static SkViewRegister reg(MyFactory);
141#endif
142