blob: 5cd16bddc1bdd6d3e9c16cf3e33749ea3a356825 [file] [log] [blame]
reed@google.com1830c7a2012-06-04 12:05:43 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@google.combf0001d2014-01-13 14:53:55 +00007
reed@google.com1830c7a2012-06-04 12:05:43 +00008#include "SampleCode.h"
9#include "SkDumpCanvas.h"
10#include "SkView.h"
11#include "SkCanvas.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
djsollen@google.com796763e2012-12-10 14:12:55 +000015#include "SkOSFile.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000016#include "SkPath.h"
17#include "SkPicture.h"
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000018#include "SkQuadTreePicture.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000019#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());
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000065 switch (fBBox) {
66 case kNo_BBoxType:
67 // No name appended
68 break;
69 case kRTree_BBoxType:
70 name.append(" <bbox: R>");
71 break;
72 case kQuadTree_BBoxType:
73 name.append(" <bbox: Q>");
74 break;
75 case kTileGrid_BBoxType:
76 name.append(" <bbox: T>");
77 break;
78 default:
79 SkASSERT(false);
80 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000081 }
82 SampleCode::TitleR(evt, name.c_str());
83 return true;
84 }
85 return this->INHERITED::onQuery(evt);
86 }
87
88 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE {
89 if (evt.isType("PictFileView::toggleBBox")) {
90 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
91 return true;
92 }
93 return this->INHERITED::onEvent(evt);
94 }
95
96 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
97 SkASSERT(fBBox < kBBoxTypeCount);
98 SkPicture** picture = fPictures + fBBox;
99
100 if (!*picture) {
101 *picture = LoadPicture(fFilename.c_str(), fBBox);
102 }
103 if (*picture) {
104 canvas->drawPicture(**picture);
105 }
106 }
107
108private:
109 enum BBoxType {
110 kNo_BBoxType,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000111 kQuadTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000112 kRTree_BBoxType,
113 kTileGrid_BBoxType,
114
115 kLast_BBoxType = kTileGrid_BBoxType
116 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000117 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000118
119 SkString fFilename;
120 SkPicture* fPictures[kBBoxTypeCount];
121 BBoxType fBBox;
122 SkSize fTileSize;
123
124 SkPicture* LoadPicture(const char path[], BBoxType bbox) {
reed@google.comb1963742012-08-03 13:39:57 +0000125 SkPicture* pic = NULL;
126
127 SkBitmap bm;
128 if (SkImageDecoder::DecodeFile(path, &bm)) {
129 bm.setImmutable();
130 pic = SkNEW(SkPicture);
131 SkCanvas* can = pic->beginRecording(bm.width(), bm.height());
132 can->drawBitmap(bm, 0, 0, NULL);
133 pic->endRecording();
134 } else {
135 SkFILEStream stream(path);
136 if (stream.isValid()) {
scroggo@google.comf1754ec2013-06-28 21:32:00 +0000137 pic = SkPicture::CreateFromStream(&stream);
reed@google.comb8b830e2013-06-25 20:42:37 +0000138 } else {
139 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000140 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000141
reed@google.com636d87a2013-09-17 20:03:43 +0000142 if (false) {
143 SkSurface* surf = SkSurface::NewRasterPMColor(pic->width(), pic->height());
144 surf->getCanvas()->drawPicture(*pic);
145 surf->unref();
146 }
reed@google.com0a5c18b2012-08-31 13:32:47 +0000147 if (false) { // re-record
148 SkPicture p2;
149 pic->draw(p2.beginRecording(pic->width(), pic->height()));
150 p2.endRecording();
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000151
reed@google.com0a5c18b2012-08-31 13:32:47 +0000152 SkString path2(path);
153 path2.append(".new.skp");
154 SkFILEWStream writer(path2.c_str());
155 p2.serialize(&writer);
156 }
reed@google.comb1963742012-08-03 13:39:57 +0000157 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000158
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000159 if (!pic) {
160 return NULL;
161 }
162
163 SkPicture* bboxPicture = NULL;
164 switch (bbox) {
165 case kNo_BBoxType:
166 // no bbox playback necessary
167 break;
168 case kRTree_BBoxType:
169 bboxPicture = SkNEW(SkPicture);
170 break;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000171 case kQuadTree_BBoxType:
172 bboxPicture = SkNEW_ARGS(SkQuadTreePicture,
173 (SkIRect::MakeWH(pic->width(), pic->height())));
174 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000175 case kTileGrid_BBoxType: {
176 SkASSERT(!fTileSize.isEmpty());
177 SkTileGridPicture::TileGridInfo gridInfo;
178 gridInfo.fMargin = SkISize::Make(0, 0);
179 gridInfo.fOffset = SkIPoint::Make(0, 0);
180 gridInfo.fTileInterval = fTileSize.toRound();
181 bboxPicture = SkNEW_ARGS(SkTileGridPicture, (pic->width(), pic->height(), gridInfo));
182 } break;
183 default:
184 SkASSERT(false);
185 }
186
187 if (bboxPicture) {
djsollen@google.com796763e2012-12-10 14:12:55 +0000188 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(),
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000189 SkPicture::kOptimizeForClippedPlayback_RecordingFlag));
djsollen@google.com796763e2012-12-10 14:12:55 +0000190 bboxPicture->endRecording();
191 SkDELETE(pic);
192 return bboxPicture;
djsollen@google.com796763e2012-12-10 14:12:55 +0000193 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000194
195 return pic;
reed@google.comb1963742012-08-03 13:39:57 +0000196 }
197
reed@google.com1830c7a2012-06-04 12:05:43 +0000198 typedef SampleView INHERITED;
199};
200
201SampleView* CreateSamplePictFileView(const char filename[]);
202SampleView* CreateSamplePictFileView(const char filename[]) {
203 return new PictFileView(filename);
204}
205
206//////////////////////////////////////////////////////////////////////////////
207
208#if 0
209static SkView* MyFactory() { return new PictFileView; }
210static SkViewRegister reg(MyFactory);
211#endif