blob: 161db7f2abd0eb8aa20fd88027cf4bd24df2b289 [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"
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000022#include "SkTileGridPicture.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000023#include "SkUtils.h"
24#include "SkColorPriv.h"
25#include "SkColorFilter.h"
26#include "SkTime.h"
27#include "SkTypeface.h"
28#include "SkXfermode.h"
29
30#include "SkStream.h"
reed@google.com636d87a2013-09-17 20:03:43 +000031#include "SkSurface.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000032#include "SkXMLParser.h"
33
34class PictFileView : public SampleView {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000035public:
36 PictFileView(const char name[] = NULL)
37 : fFilename(name)
38 , fBBox(kNo_BBoxType)
39 , fTileSize(SkSize::Make(0, 0)) {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000040 for (int i = 0; i < kBBoxTypeCount; ++i) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000041 fPictures[i] = NULL;
42 }
43 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000045 virtual ~PictFileView() {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000046 for (int i = 0; i < kBBoxTypeCount; ++i) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000047 SkSafeUnref(fPictures[i]);
48 }
49 }
50
51 virtual void onTileSizeChanged(const SkSize &tileSize) SK_OVERRIDE {
52 if (tileSize != fTileSize) {
53 fTileSize = tileSize;
54 SkSafeSetNull(fPictures[kTileGrid_BBoxType]);
55 }
56 }
57
58protected:
59 // overrides from SkEventSink
60 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
61 if (SampleCode::TitleQ(*evt)) {
62 SkString name("P:");
63 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
64 name.append(basename ? basename+1: fFilename.c_str());
65 if (fBBox != kNo_BBoxType) {
66 name.append(fBBox == kRTree_BBoxType ? " <bbox: R>" : " <bbox: T>");
67 }
68 SampleCode::TitleR(evt, name.c_str());
69 return true;
70 }
71 return this->INHERITED::onQuery(evt);
72 }
73
74 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE {
75 if (evt.isType("PictFileView::toggleBBox")) {
76 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
77 return true;
78 }
79 return this->INHERITED::onEvent(evt);
80 }
81
82 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
83 SkASSERT(fBBox < kBBoxTypeCount);
84 SkPicture** picture = fPictures + fBBox;
85
86 if (!*picture) {
87 *picture = LoadPicture(fFilename.c_str(), fBBox);
88 }
89 if (*picture) {
90 canvas->drawPicture(**picture);
91 }
92 }
93
94private:
95 enum BBoxType {
96 kNo_BBoxType,
97 kRTree_BBoxType,
98 kTileGrid_BBoxType,
99
100 kLast_BBoxType = kTileGrid_BBoxType
101 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000102 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000103
104 SkString fFilename;
105 SkPicture* fPictures[kBBoxTypeCount];
106 BBoxType fBBox;
107 SkSize fTileSize;
108
109 SkPicture* LoadPicture(const char path[], BBoxType bbox) {
reed@google.comb1963742012-08-03 13:39:57 +0000110 SkPicture* pic = NULL;
111
112 SkBitmap bm;
113 if (SkImageDecoder::DecodeFile(path, &bm)) {
114 bm.setImmutable();
115 pic = SkNEW(SkPicture);
116 SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
117 can->drawBitmap(bm, 0, 0, NULL);
118 pic->endRecording();
119 } else {
120 SkFILEStream stream(path);
121 if (stream.isValid()) {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000122 pic = SkPicture::CreateFromStream(&stream);
reed@google.comb8b830e2013-06-25 20:42:37 +0000123 } else {
124 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000125 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000126
reed@google.com636d87a2013-09-17 20:03:43 +0000127 if (false) {
128 SkSurface* surf = SkSurface::NewRasterPMColor(pic->width(), pic->height());
129 surf->getCanvas()->drawPicture(*pic);
130 surf->unref();
131 }
reed@google.com0a5c18b2012-08-31 13:32:47 +0000132 if (false) { // re-record
133 SkPicture p2;
134 pic->draw(p2.beginRecording(pic->width(), pic->height()));
135 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000136
reed@google.com0a5c18b2012-08-31 13:32:47 +0000137 SkString path2(path);
138 path2.append(".new.skp");
139 SkFILEWStream writer(path2.c_str());
140 p2.serialize(&writer);
141 }
reed@google.comb1963742012-08-03 13:39:57 +0000142 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000143
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000144 if (!pic) {
145 return NULL;
146 }
147
148 SkPicture* bboxPicture = NULL;
149 switch (bbox) {
150 case kNo_BBoxType:
151 // no bbox playback necessary
152 break;
153 case kRTree_BBoxType:
154 bboxPicture = SkNEW(SkPicture);
155 break;
156 case kTileGrid_BBoxType: {
157 SkASSERT(!fTileSize.isEmpty());
158 SkTileGridPicture::TileGridInfo gridInfo;
159 gridInfo.fMargin = SkISize::Make(0, 0);
160 gridInfo.fOffset = SkIPoint::Make(0, 0);
161 gridInfo.fTileInterval = fTileSize.toRound();
162 bboxPicture = SkNEW_ARGS(SkTileGridPicture, (pic->width(), pic->height(), gridInfo));
163 } break;
164 default:
165 SkASSERT(false);
166 }
167
168 if (bboxPicture) {
djsollen@google.com796763e2012-12-10 14:12:55 +0000169 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000170 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
djsollen@google.com796763e2012-12-10 14:12:55 +0000171 bboxPicture->endRecording();
172 SkDELETE(pic);
173 return bboxPicture;
djsollen@google.com796763e2012-12-10 14:12:55 +0000174 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000175
176 return pic;
reed@google.comb1963742012-08-03 13:39:57 +0000177 }
178
reed@google.com1830c7a2012-06-04 12:05:43 +0000179 typedef SampleView INHERITED;
180};
181
182SampleView* CreateSamplePictFileView(const char filename[]);
183SampleView* CreateSamplePictFileView(const char filename[]) {
184 return new PictFileView(filename);
185}
186
187//////////////////////////////////////////////////////////////////////////////
188
189#if 0
190static SkView* MyFactory() { return new PictFileView; }
191static SkViewRegister reg(MyFactory);
192#endif