blob: 9e9764c4330ff843c36371620669b1bcfdc5f52d [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
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());
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000064 switch (fBBox) {
65 case kNo_BBoxType:
66 // No name appended
67 break;
68 case kRTree_BBoxType:
69 name.append(" <bbox: R>");
70 break;
71 case kQuadTree_BBoxType:
72 name.append(" <bbox: Q>");
73 break;
74 case kTileGrid_BBoxType:
75 name.append(" <bbox: T>");
76 break;
77 default:
78 SkASSERT(false);
79 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000080 }
81 SampleCode::TitleR(evt, name.c_str());
82 return true;
83 }
84 return this->INHERITED::onQuery(evt);
85 }
86
87 virtual bool onEvent(const SkEvent& evt) SK_OVERRIDE {
88 if (evt.isType("PictFileView::toggleBBox")) {
89 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
90 return true;
91 }
92 return this->INHERITED::onEvent(evt);
93 }
94
95 virtual void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org8c6f4b32014-02-03 19:40:32 +000096 SkASSERT(static_cast<int>(fBBox) < kBBoxTypeCount);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000097 SkPicture** picture = fPictures + fBBox;
98
99 if (!*picture) {
100 *picture = LoadPicture(fFilename.c_str(), fBBox);
101 }
102 if (*picture) {
robertphillips9b14f262014-06-04 05:40:44 -0700103 canvas->drawPicture(*picture);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000104 }
105 }
106
107private:
108 enum BBoxType {
109 kNo_BBoxType,
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000110 kQuadTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000111 kRTree_BBoxType,
112 kTileGrid_BBoxType,
113
114 kLast_BBoxType = kTileGrid_BBoxType
115 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000116 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000117
118 SkString fFilename;
119 SkPicture* fPictures[kBBoxTypeCount];
120 BBoxType fBBox;
121 SkSize fTileSize;
122
123 SkPicture* LoadPicture(const char path[], BBoxType bbox) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000124 SkAutoTUnref<SkPicture> pic;
reed@google.comb1963742012-08-03 13:39:57 +0000125
126 SkBitmap bm;
127 if (SkImageDecoder::DecodeFile(path, &bm)) {
128 bm.setImmutable();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000129 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000130 SkCanvas* can = recorder.beginRecording(bm.width(), bm.height(), NULL, 0);
reed@google.comb1963742012-08-03 13:39:57 +0000131 can->drawBitmap(bm, 0, 0, NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000132 pic.reset(recorder.endRecording());
reed@google.comb1963742012-08-03 13:39:57 +0000133 } else {
134 SkFILEStream stream(path);
135 if (stream.isValid()) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000136 pic.reset(SkPicture::CreateFromStream(&stream));
reed@google.comb8b830e2013-06-25 20:42:37 +0000137 } else {
138 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000139 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000140
reed@google.com636d87a2013-09-17 20:03:43 +0000141 if (false) {
142 SkSurface* surf = SkSurface::NewRasterPMColor(pic->width(), pic->height());
robertphillips9b14f262014-06-04 05:40:44 -0700143 surf->getCanvas()->drawPicture(pic);
reed@google.com636d87a2013-09-17 20:03:43 +0000144 surf->unref();
145 }
reed@google.com0a5c18b2012-08-31 13:32:47 +0000146 if (false) { // re-record
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000147 SkPictureRecorder recorder;
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000148 pic->draw(recorder.beginRecording(pic->width(), pic->height(), NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000149 SkAutoTUnref<SkPicture> p2(recorder.endRecording());
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000150
reed@google.com0a5c18b2012-08-31 13:32:47 +0000151 SkString path2(path);
152 path2.append(".new.skp");
153 SkFILEWStream writer(path2.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000154 p2->serialize(&writer);
reed@google.com0a5c18b2012-08-31 13:32:47 +0000155 }
reed@google.comb1963742012-08-03 13:39:57 +0000156 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000157
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000158 if (NULL == pic) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000159 return NULL;
160 }
161
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000162 SkAutoTDelete<SkBBHFactory> factory;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000163 switch (bbox) {
164 case kNo_BBoxType:
165 // no bbox playback necessary
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000166 return pic.detach();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000167 case kRTree_BBoxType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000168 factory.reset(SkNEW(SkRTreeFactory));
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000169 break;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000170 case kQuadTree_BBoxType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000171 factory.reset(SkNEW(SkQuadTreeFactory));
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +0000172 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000173 case kTileGrid_BBoxType: {
174 SkASSERT(!fTileSize.isEmpty());
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000175 SkTileGridFactory::TileGridInfo gridInfo;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000176 gridInfo.fMargin = SkISize::Make(0, 0);
177 gridInfo.fOffset = SkIPoint::Make(0, 0);
178 gridInfo.fTileInterval = fTileSize.toRound();
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000179 factory.reset(SkNEW_ARGS(SkTileGridFactory, (gridInfo)));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000180 break;
181 }
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000182 default:
183 SkASSERT(false);
184 }
185
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000186 SkPictureRecorder recorder;
187 pic->draw(recorder.beginRecording(pic->width(), pic->height(), factory.get(), 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000188 return recorder.endRecording();
reed@google.comb1963742012-08-03 13:39:57 +0000189 }
190
reed@google.com1830c7a2012-06-04 12:05:43 +0000191 typedef SampleView INHERITED;
192};
193
194SampleView* CreateSamplePictFileView(const char filename[]);
195SampleView* CreateSamplePictFileView(const char filename[]) {
196 return new PictFileView(filename);
197}
198
199//////////////////////////////////////////////////////////////////////////////
200
201#if 0
202static SkView* MyFactory() { return new PictFileView; }
203static SkViewRegister reg(MyFactory);
204#endif