blob: a0bab7da4ebd016f45097dd645a729bc4f1325ec [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"
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#include "SkXMLParser.h"
32
reed40dab982015-01-28 13:28:53 -080033#include "SkGlyphCache.h"
34
reed@google.com1830c7a2012-06-04 12:05:43 +000035class PictFileView : public SampleView {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000036public:
37 PictFileView(const char name[] = NULL)
38 : fFilename(name)
39 , fBBox(kNo_BBoxType)
40 , fTileSize(SkSize::Make(0, 0)) {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000041 for (int i = 0; i < kBBoxTypeCount; ++i) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000042 fPictures[i] = NULL;
43 }
44 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000045
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000046 virtual ~PictFileView() {
fmalita@google.com9a65e2c2013-12-10 22:25:53 +000047 for (int i = 0; i < kBBoxTypeCount; ++i) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000048 SkSafeUnref(fPictures[i]);
49 }
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 void onTileSizeChanged(const SkSize &tileSize) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000053 if (tileSize != fTileSize) {
54 fTileSize = tileSize;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000055 }
56 }
57
58protected:
59 // overrides from SkEventSink
mtklein36352bf2015-03-25 18:17:31 -070060 bool onQuery(SkEvent* evt) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000061 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;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000072 default:
73 SkASSERT(false);
74 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000075 }
76 SampleCode::TitleR(evt, name.c_str());
77 return true;
78 }
79 return this->INHERITED::onQuery(evt);
80 }
81
mtklein36352bf2015-03-25 18:17:31 -070082 bool onEvent(const SkEvent& evt) override {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000083 if (evt.isType("PictFileView::toggleBBox")) {
84 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
85 return true;
86 }
87 return this->INHERITED::onEvent(evt);
88 }
89
mtklein36352bf2015-03-25 18:17:31 -070090 void onDrawContent(SkCanvas* canvas) override {
commit-bot@chromium.org8c6f4b32014-02-03 19:40:32 +000091 SkASSERT(static_cast<int>(fBBox) < kBBoxTypeCount);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000092 SkPicture** picture = fPictures + fBBox;
93
reed40dab982015-01-28 13:28:53 -080094#ifdef SK_GLYPHCACHE_TRACK_HASH_STATS
95 SkGraphics::PurgeFontCache();
96#endif
97
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000098 if (!*picture) {
99 *picture = LoadPicture(fFilename.c_str(), fBBox);
100 }
101 if (*picture) {
robertphillips9b14f262014-06-04 05:40:44 -0700102 canvas->drawPicture(*picture);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000103 }
reed40dab982015-01-28 13:28:53 -0800104
105#ifdef SK_GLYPHCACHE_TRACK_HASH_STATS
106 SkGlyphCache::Dump();
107 SkDebugf("\n");
108#endif
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000109 }
110
111private:
112 enum BBoxType {
113 kNo_BBoxType,
114 kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000115
mtklein703dd2e2015-01-09 06:41:48 -0800116 kLast_BBoxType = kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000117 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000118 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000119
120 SkString fFilename;
121 SkPicture* fPictures[kBBoxTypeCount];
122 BBoxType fBBox;
123 SkSize fTileSize;
124
125 SkPicture* LoadPicture(const char path[], BBoxType bbox) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000126 SkAutoTUnref<SkPicture> pic;
reed@google.comb1963742012-08-03 13:39:57 +0000127
128 SkBitmap bm;
129 if (SkImageDecoder::DecodeFile(path, &bm)) {
130 bm.setImmutable();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000131 SkPictureRecorder recorder;
mtklein703dd2e2015-01-09 06:41:48 -0800132 SkCanvas* can = recorder.beginRecording(SkIntToScalar(bm.width()),
133 SkIntToScalar(bm.height()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700134 NULL, 0);
reed@google.comb1963742012-08-03 13:39:57 +0000135 can->drawBitmap(bm, 0, 0, NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000136 pic.reset(recorder.endRecording());
reed@google.comb1963742012-08-03 13:39:57 +0000137 } else {
138 SkFILEStream stream(path);
139 if (stream.isValid()) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000140 pic.reset(SkPicture::CreateFromStream(&stream));
reed@google.comb8b830e2013-06-25 20:42:37 +0000141 } else {
142 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000143 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000144
reed@google.com0a5c18b2012-08-31 13:32:47 +0000145 if (false) { // re-record
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000146 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700147 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800148 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700149 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000150 SkAutoTUnref<SkPicture> p2(recorder.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());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000155 p2->serialize(&writer);
reed@google.com0a5c18b2012-08-31 13:32:47 +0000156 }
reed@google.comb1963742012-08-03 13:39:57 +0000157 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000158
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000159 if (NULL == pic) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000160 return NULL;
161 }
162
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000163 SkAutoTDelete<SkBBHFactory> factory;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000164 switch (bbox) {
165 case kNo_BBoxType:
166 // no bbox playback necessary
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000167 return pic.detach();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000168 case kRTree_BBoxType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000169 factory.reset(SkNEW(SkRTreeFactory));
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000170 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000171 default:
172 SkASSERT(false);
173 }
174
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000175 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700176 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800177 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700178 factory.get(), 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000179 return recorder.endRecording();
reed@google.comb1963742012-08-03 13:39:57 +0000180 }
181
reed@google.com1830c7a2012-06-04 12:05:43 +0000182 typedef SampleView INHERITED;
183};
184
185SampleView* CreateSamplePictFileView(const char filename[]);
186SampleView* CreateSamplePictFileView(const char filename[]) {
187 return new PictFileView(filename);
188}
189
190//////////////////////////////////////////////////////////////////////////////
191
192#if 0
193static SkView* MyFactory() { return new PictFileView; }
194static SkViewRegister reg(MyFactory);
195#endif