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