blob: 85483288c71eef79ef376cab15c8fad0c75fa275 [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
msarettd15750c2016-03-18 15:48:49 -07008#include "DecodeFile.h"
reed@google.com1830c7a2012-06-04 12:05:43 +00009#include "SampleCode.h"
10#include "SkDumpCanvas.h"
11#include "SkView.h"
12#include "SkCanvas.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.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"
robertphillips@google.com770963f2014-04-18 18:04:41 +000018#include "SkPictureRecorder.h"
reed@google.com1830c7a2012-06-04 12:05:43 +000019#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
reed40dab982015-01-28 13:28:53 -080032#include "SkGlyphCache.h"
33
reed093b4e82015-08-04 18:46:38 -070034#include "SkDrawFilter.h"
35class SkCounterDrawFilter : public SkDrawFilter {
36public:
37 SkCounterDrawFilter(int count) : fCount(count) {}
38
39 bool filter(SkPaint*, Type t) override {
40 return --fCount >= 0;
41 }
42
43 int fCount;
44};
45
reed@google.com1830c7a2012-06-04 12:05:43 +000046class PictFileView : public SampleView {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000047public:
halcanary96fcdcc2015-08-27 07:41:13 -070048 PictFileView(const char name[] = nullptr)
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000049 : fFilename(name)
50 , fBBox(kNo_BBoxType)
51 , fTileSize(SkSize::Make(0, 0)) {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000052 for (int i = 0; i < kBBoxTypeCount; ++i) {
halcanary96fcdcc2015-08-27 07:41:13 -070053 fPictures[i] = nullptr;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000054 }
reed093b4e82015-08-04 18:46:38 -070055 fCount = 0;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000056 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000057
reedccebcec2016-08-22 07:41:46 -070058 ~PictFileView() override {
59 this->freePictures();
60 }
61
62 void freePictures() {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000063 for (int i = 0; i < kBBoxTypeCount; ++i) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000064 SkSafeUnref(fPictures[i]);
reedccebcec2016-08-22 07:41:46 -070065 fPictures[i] = nullptr;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000066 }
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 void onTileSizeChanged(const SkSize &tileSize) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000070 if (tileSize != fTileSize) {
71 fTileSize = tileSize;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000072 }
73 }
74
75protected:
76 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070077 bool onQuery(SkEvent* evt) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000078 if (SampleCode::TitleQ(*evt)) {
79 SkString name("P:");
80 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
81 name.append(basename ? basename+1: fFilename.c_str());
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000082 switch (fBBox) {
83 case kNo_BBoxType:
84 // No name appended
85 break;
86 case kRTree_BBoxType:
87 name.append(" <bbox: R>");
88 break;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000089 default:
90 SkASSERT(false);
91 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000092 }
93 SampleCode::TitleR(evt, name.c_str());
94 return true;
95 }
reed093b4e82015-08-04 18:46:38 -070096 SkUnichar uni;
97 if (SampleCode::CharQ(*evt, &uni)) {
98 switch (uni) {
99 case 'n': fCount += 1; this->inval(nullptr); return true;
100 case 'p': fCount -= 1; this->inval(nullptr); return true;
101 case 's': fCount = 0; this->inval(nullptr); return true;
reedccebcec2016-08-22 07:41:46 -0700102 case 'F':
103 fFilterQuality = (kNone_SkFilterQuality == fFilterQuality) ?
104 kHigh_SkFilterQuality : kNone_SkFilterQuality;
105 this->freePictures();
106 this->inval(nullptr);
107 return true;
reed093b4e82015-08-04 18:46:38 -0700108 default: break;
109 }
110 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000111 return this->INHERITED::onQuery(evt);
112 }
113
mtklein36352bf2015-03-25 18:17:31 -0700114 bool onEvent(const SkEvent& evt) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000115 if (evt.isType("PictFileView::toggleBBox")) {
116 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
117 return true;
118 }
119 return this->INHERITED::onEvent(evt);
120 }
121
mtklein36352bf2015-03-25 18:17:31 -0700122 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org8c6f4b32014-02-03 19:40:32 +0000123 SkASSERT(static_cast<int>(fBBox) < kBBoxTypeCount);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000124 SkPicture** picture = fPictures + fBBox;
125
reed40dab982015-01-28 13:28:53 -0800126#ifdef SK_GLYPHCACHE_TRACK_HASH_STATS
127 SkGraphics::PurgeFontCache();
128#endif
129
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000130 if (!*picture) {
reedca2622b2016-03-18 07:25:55 -0700131 *picture = LoadPicture(fFilename.c_str(), fBBox).release();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000132 }
133 if (*picture) {
reed093b4e82015-08-04 18:46:38 -0700134 SkCounterDrawFilter filter(fCount);
135 if (fCount > 0) {
136 canvas->setDrawFilter(&filter);
137 }
robertphillips9b14f262014-06-04 05:40:44 -0700138 canvas->drawPicture(*picture);
halcanary96fcdcc2015-08-27 07:41:13 -0700139 canvas->setDrawFilter(nullptr);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000140 }
reed40dab982015-01-28 13:28:53 -0800141
142#ifdef SK_GLYPHCACHE_TRACK_HASH_STATS
143 SkGlyphCache::Dump();
144 SkDebugf("\n");
145#endif
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000146 }
147
148private:
149 enum BBoxType {
150 kNo_BBoxType,
151 kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000152
mtklein703dd2e2015-01-09 06:41:48 -0800153 kLast_BBoxType = kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000154 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000155 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000156
157 SkString fFilename;
158 SkPicture* fPictures[kBBoxTypeCount];
159 BBoxType fBBox;
160 SkSize fTileSize;
reed093b4e82015-08-04 18:46:38 -0700161 int fCount;
reedccebcec2016-08-22 07:41:46 -0700162 SkFilterQuality fFilterQuality = kNone_SkFilterQuality;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000163
reedca2622b2016-03-18 07:25:55 -0700164 sk_sp<SkPicture> LoadPicture(const char path[], BBoxType bbox) {
165 sk_sp<SkPicture> pic;
reed@google.comb1963742012-08-03 13:39:57 +0000166
reedccebcec2016-08-22 07:41:46 -0700167 if (sk_sp<SkImage> img = decode_file(path)) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000168 SkPictureRecorder recorder;
reedccebcec2016-08-22 07:41:46 -0700169 SkCanvas* can = recorder.beginRecording(SkIntToScalar(img->width()),
170 SkIntToScalar(img->height()),
halcanary96fcdcc2015-08-27 07:41:13 -0700171 nullptr, 0);
reedccebcec2016-08-22 07:41:46 -0700172 SkPaint paint;
173 paint.setFilterQuality(fFilterQuality);
174 can->drawImage(img, 0, 0, &paint);
reedca2622b2016-03-18 07:25:55 -0700175 pic = recorder.finishRecordingAsPicture();
reed@google.comb1963742012-08-03 13:39:57 +0000176 } else {
177 SkFILEStream stream(path);
178 if (stream.isValid()) {
reedca2622b2016-03-18 07:25:55 -0700179 pic = SkPicture::MakeFromStream(&stream);
reed@google.comb8b830e2013-06-25 20:42:37 +0000180 } else {
181 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000182 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000183
reed@google.com0a5c18b2012-08-31 13:32:47 +0000184 if (false) { // re-record
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000185 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700186 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800187 pic->cullRect().height(),
halcanary96fcdcc2015-08-27 07:41:13 -0700188 nullptr, 0));
reedca2622b2016-03-18 07:25:55 -0700189 sk_sp<SkPicture> p2(recorder.finishRecordingAsPicture());
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000190
reed@google.com0a5c18b2012-08-31 13:32:47 +0000191 SkString path2(path);
192 path2.append(".new.skp");
193 SkFILEWStream writer(path2.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000194 p2->serialize(&writer);
reed@google.com0a5c18b2012-08-31 13:32:47 +0000195 }
reed@google.comb1963742012-08-03 13:39:57 +0000196 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000197
halcanary96fcdcc2015-08-27 07:41:13 -0700198 if (nullptr == pic) {
199 return nullptr;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000200 }
201
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000202 SkAutoTDelete<SkBBHFactory> factory;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000203 switch (bbox) {
204 case kNo_BBoxType:
205 // no bbox playback necessary
fmalita3e33e6f2016-03-18 11:07:50 -0700206 return pic;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000207 case kRTree_BBoxType:
halcanary385fe4d2015-08-26 13:07:48 -0700208 factory.reset(new SkRTreeFactory);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000209 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000210 default:
211 SkASSERT(false);
212 }
213
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000214 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700215 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800216 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700217 factory.get(), 0));
reedca2622b2016-03-18 07:25:55 -0700218 return recorder.finishRecordingAsPicture();
reed@google.comb1963742012-08-03 13:39:57 +0000219 }
220
reed@google.com1830c7a2012-06-04 12:05:43 +0000221 typedef SampleView INHERITED;
222};
223
224SampleView* CreateSamplePictFileView(const char filename[]);
225SampleView* CreateSamplePictFileView(const char filename[]) {
226 return new PictFileView(filename);
227}
228
229//////////////////////////////////////////////////////////////////////////////
230
231#if 0
232static SkView* MyFactory() { return new PictFileView; }
233static SkViewRegister reg(MyFactory);
234#endif