blob: 3fb335fc3415b02131ab955749415cee6541a698 [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
mtklein72c9faa2015-01-09 10:06:39 -080050 void onTileSizeChanged(const SkSize &tileSize) SK_OVERRIDE {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000051 if (tileSize != fTileSize) {
52 fTileSize = tileSize;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000053 }
54 }
55
56protected:
57 // overrides from SkEventSink
mtklein72c9faa2015-01-09 10:06:39 -080058 bool onQuery(SkEvent* evt) SK_OVERRIDE {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000059 if (SampleCode::TitleQ(*evt)) {
60 SkString name("P:");
61 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
62 name.append(basename ? basename+1: fFilename.c_str());
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000063 switch (fBBox) {
64 case kNo_BBoxType:
65 // No name appended
66 break;
67 case kRTree_BBoxType:
68 name.append(" <bbox: R>");
69 break;
commit-bot@chromium.orgc22d1392014-02-03 18:08:33 +000070 default:
71 SkASSERT(false);
72 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000073 }
74 SampleCode::TitleR(evt, name.c_str());
75 return true;
76 }
77 return this->INHERITED::onQuery(evt);
78 }
79
mtklein72c9faa2015-01-09 10:06:39 -080080 bool onEvent(const SkEvent& evt) SK_OVERRIDE {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000081 if (evt.isType("PictFileView::toggleBBox")) {
82 fBBox = (BBoxType)((fBBox + 1) % kBBoxTypeCount);
83 return true;
84 }
85 return this->INHERITED::onEvent(evt);
86 }
87
mtklein72c9faa2015-01-09 10:06:39 -080088 void onDrawContent(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org8c6f4b32014-02-03 19:40:32 +000089 SkASSERT(static_cast<int>(fBBox) < kBBoxTypeCount);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000090 SkPicture** picture = fPictures + fBBox;
91
92 if (!*picture) {
93 *picture = LoadPicture(fFilename.c_str(), fBBox);
94 }
95 if (*picture) {
robertphillips9b14f262014-06-04 05:40:44 -070096 canvas->drawPicture(*picture);
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +000097 }
98 }
99
100private:
101 enum BBoxType {
102 kNo_BBoxType,
103 kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000104
mtklein703dd2e2015-01-09 06:41:48 -0800105 kLast_BBoxType = kRTree_BBoxType,
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000106 };
fmalita@google.come6a98d42013-12-10 22:12:40 +0000107 static const int kBBoxTypeCount = kLast_BBoxType + 1;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000108
109 SkString fFilename;
110 SkPicture* fPictures[kBBoxTypeCount];
111 BBoxType fBBox;
112 SkSize fTileSize;
113
114 SkPicture* LoadPicture(const char path[], BBoxType bbox) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000115 SkAutoTUnref<SkPicture> pic;
reed@google.comb1963742012-08-03 13:39:57 +0000116
117 SkBitmap bm;
118 if (SkImageDecoder::DecodeFile(path, &bm)) {
119 bm.setImmutable();
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000120 SkPictureRecorder recorder;
mtklein703dd2e2015-01-09 06:41:48 -0800121 SkCanvas* can = recorder.beginRecording(SkIntToScalar(bm.width()),
122 SkIntToScalar(bm.height()),
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700123 NULL, 0);
reed@google.comb1963742012-08-03 13:39:57 +0000124 can->drawBitmap(bm, 0, 0, NULL);
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000125 pic.reset(recorder.endRecording());
reed@google.comb1963742012-08-03 13:39:57 +0000126 } else {
127 SkFILEStream stream(path);
128 if (stream.isValid()) {
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000129 pic.reset(SkPicture::CreateFromStream(&stream));
reed@google.comb8b830e2013-06-25 20:42:37 +0000130 } else {
131 SkDebugf("coun't load picture at \"path\"\n", path);
reed@google.comb1963742012-08-03 13:39:57 +0000132 }
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000133
reed@google.com0a5c18b2012-08-31 13:32:47 +0000134 if (false) { // re-record
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000135 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700136 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800137 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700138 NULL, 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000139 SkAutoTUnref<SkPicture> p2(recorder.endRecording());
skia.committer@gmail.com11f86922012-08-31 17:14:46 +0000140
reed@google.com0a5c18b2012-08-31 13:32:47 +0000141 SkString path2(path);
142 path2.append(".new.skp");
143 SkFILEWStream writer(path2.c_str());
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000144 p2->serialize(&writer);
reed@google.com0a5c18b2012-08-31 13:32:47 +0000145 }
reed@google.comb1963742012-08-03 13:39:57 +0000146 }
djsollen@google.com796763e2012-12-10 14:12:55 +0000147
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000148 if (NULL == pic) {
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000149 return NULL;
150 }
151
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000152 SkAutoTDelete<SkBBHFactory> factory;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000153 switch (bbox) {
154 case kNo_BBoxType:
155 // no bbox playback necessary
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000156 return pic.detach();
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000157 case kRTree_BBoxType:
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000158 factory.reset(SkNEW(SkRTreeFactory));
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000159 break;
commit-bot@chromium.orgbbe43a92013-12-10 21:51:06 +0000160 default:
161 SkASSERT(false);
162 }
163
commit-bot@chromium.org5fb2ce32014-04-17 23:35:06 +0000164 SkPictureRecorder recorder;
robertphillipsc5ba71d2014-09-04 08:42:50 -0700165 pic->playback(recorder.beginRecording(pic->cullRect().width(),
mtklein703dd2e2015-01-09 06:41:48 -0800166 pic->cullRect().height(),
robertphillipsc5ba71d2014-09-04 08:42:50 -0700167 factory.get(), 0));
robertphillips@google.com84b18c72014-04-13 19:09:42 +0000168 return recorder.endRecording();
reed@google.comb1963742012-08-03 13:39:57 +0000169 }
170
reed@google.com1830c7a2012-06-04 12:05:43 +0000171 typedef SampleView INHERITED;
172};
173
174SampleView* CreateSamplePictFileView(const char filename[]);
175SampleView* CreateSamplePictFileView(const char filename[]) {
176 return new PictFileView(filename);
177}
178
179//////////////////////////////////////////////////////////////////////////////
180
181#if 0
182static SkView* MyFactory() { return new PictFileView; }
183static SkViewRegister reg(MyFactory);
184#endif