OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkDumpCanvas.h" | 9 #include "SkDumpCanvas.h" |
10 #include "SkView.h" | 10 #include "SkView.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "Sk64.h" | 12 #include "Sk64.h" |
13 #include "SkGradientShader.h" | 13 #include "SkGradientShader.h" |
14 #include "SkGraphics.h" | 14 #include "SkGraphics.h" |
15 #include "SkImageDecoder.h" | 15 #include "SkImageDecoder.h" |
16 #include "SkOSFile.h" | 16 #include "SkOSFile.h" |
17 #include "SkPath.h" | 17 #include "SkPath.h" |
18 #include "SkPicture.h" | 18 #include "SkPicture.h" |
19 #include "SkRandom.h" | 19 #include "SkRandom.h" |
20 #include "SkRegion.h" | 20 #include "SkRegion.h" |
21 #include "SkShader.h" | 21 #include "SkShader.h" |
22 #include "SkTileGridPicture.h" | |
22 #include "SkUtils.h" | 23 #include "SkUtils.h" |
23 #include "SkColorPriv.h" | 24 #include "SkColorPriv.h" |
24 #include "SkColorFilter.h" | 25 #include "SkColorFilter.h" |
25 #include "SkTime.h" | 26 #include "SkTime.h" |
26 #include "SkTypeface.h" | 27 #include "SkTypeface.h" |
27 #include "SkXfermode.h" | 28 #include "SkXfermode.h" |
28 | 29 |
29 #include "SkStream.h" | 30 #include "SkStream.h" |
30 #include "SkSurface.h" | 31 #include "SkSurface.h" |
31 #include "SkXMLParser.h" | 32 #include "SkXMLParser.h" |
32 | 33 |
33 class PictFileView : public SampleView { | 34 class PictFileView : public SampleView { |
35 public: | |
36 PictFileView(const char name[] = NULL) | |
37 : fFilename(name) | |
38 , fBBox(kNo_BBox) | |
39 , fTileSize(SkSize::Make(0, 0)) { | |
40 for (unsigned i = 0; i < kLast_BBox_enum; ++i) { | |
41 fPictures[i] = NULL; | |
42 } | |
43 } | |
44 | |
45 virtual ~PictFileView() { | |
46 for (unsigned i = 0; i < kLast_BBox_enum; ++i) { | |
47 SkSafeUnref(fPictures[i]); | |
48 } | |
49 } | |
50 | |
robertphillips
2013/12/10 18:44:28
override?
fmalita_google_do_not_use
2013/12/10 19:48:17
Done.
| |
51 virtual void onTileSizeChanged(const SkSize &tileSize) { | |
52 if (tileSize != fTileSize) { | |
53 fTileSize = tileSize; | |
robertphillips
2013/12/10 18:44:28
SkSafeSetNull?
fmalita_google_do_not_use
2013/12/10 19:48:17
Done.
| |
54 SkSafeUnref(fPictures[kTileGrid_BBox]); | |
55 fPictures[kTileGrid_BBox] = NULL; | |
56 } | |
57 } | |
58 | |
59 protected: | |
60 // overrides from SkEventSink | |
robertphillips
2013/12/10 18:44:28
override keyword?
fmalita_google_do_not_use
2013/12/10 19:48:17
Done.
| |
61 virtual bool onQuery(SkEvent* evt) { | |
62 if (SampleCode::TitleQ(*evt)) { | |
63 SkString name("P:"); | |
64 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); | |
65 name.append(basename ? basename+1: fFilename.c_str()); | |
66 if (fBBox != kNo_BBox) { | |
67 name.append(fBBox == kRTree_BBox ? " <bbox: R>" : " <bbox: T>"); | |
68 } | |
69 SampleCode::TitleR(evt, name.c_str()); | |
70 return true; | |
71 } | |
72 return this->INHERITED::onQuery(evt); | |
73 } | |
74 | |
75 virtual bool onEvent(const SkEvent& evt) { | |
76 if (evt.isType("PictFileView::toggleBBox")) { | |
77 fBBox = (BBoxType)((fBBox + 1) % kLast_BBox_enum); | |
78 return true; | |
79 } | |
80 return this->INHERITED::onEvent(evt); | |
81 } | |
82 | |
83 virtual void onDrawContent(SkCanvas* canvas) { | |
84 SkASSERT(fBBox < kLast_BBox_enum); | |
85 SkPicture** picture = fPictures + fBBox; | |
86 | |
87 if (!*picture) { | |
88 *picture = LoadPicture(fFilename.c_str(), fBBox); | |
89 } | |
90 if (*picture) { | |
91 canvas->drawPicture(**picture); | |
92 } | |
93 } | |
94 | |
95 private: | |
96 enum BBoxType { | |
robertphillips
2013/12/10 18:44:28
kNo_BBoxType? same for others?
fmalita_google_do_not_use
2013/12/10 19:48:17
Done.
| |
97 kNo_BBox, | |
98 kRTree_BBox, | |
99 kTileGrid_BBox, | |
100 | |
robertphillips
2013/12/10 18:44:28
kLast_BBoxType? (no _enum)?
Also shouldn't this be
fmalita_google_do_not_use
2013/12/10 19:48:17
Oh yeah, the style guide is quite specific about t
| |
101 kLast_BBox_enum | |
102 }; | |
103 | |
34 SkString fFilename; | 104 SkString fFilename; |
35 SkPicture* fPicture; | 105 SkPicture* fPictures[kLast_BBox_enum]; |
36 SkPicture* fBBoxPicture; | 106 BBoxType fBBox; |
37 bool fUseBBox; | 107 SkSize fTileSize; |
38 | 108 |
39 static SkPicture* LoadPicture(const char path[], bool useBBox) { | 109 SkPicture* LoadPicture(const char path[], BBoxType bbox) { |
40 SkPicture* pic = NULL; | 110 SkPicture* pic = NULL; |
41 | 111 |
42 SkBitmap bm; | 112 SkBitmap bm; |
43 if (SkImageDecoder::DecodeFile(path, &bm)) { | 113 if (SkImageDecoder::DecodeFile(path, &bm)) { |
44 bm.setImmutable(); | 114 bm.setImmutable(); |
45 pic = SkNEW(SkPicture); | 115 pic = SkNEW(SkPicture); |
46 SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); | 116 SkCanvas* can = pic->beginRecording(bm.width(), bm.height()); |
47 can->drawBitmap(bm, 0, 0, NULL); | 117 can->drawBitmap(bm, 0, 0, NULL); |
48 pic->endRecording(); | 118 pic->endRecording(); |
49 } else { | 119 } else { |
(...skipping 13 matching lines...) Expand all Loading... | |
63 SkPicture p2; | 133 SkPicture p2; |
64 pic->draw(p2.beginRecording(pic->width(), pic->height())); | 134 pic->draw(p2.beginRecording(pic->width(), pic->height())); |
65 p2.endRecording(); | 135 p2.endRecording(); |
66 | 136 |
67 SkString path2(path); | 137 SkString path2(path); |
68 path2.append(".new.skp"); | 138 path2.append(".new.skp"); |
69 SkFILEWStream writer(path2.c_str()); | 139 SkFILEWStream writer(path2.c_str()); |
70 p2.serialize(&writer); | 140 p2.serialize(&writer); |
71 } | 141 } |
72 } | 142 } |
73 | 143 |
robertphillips
2013/12/10 18:44:28
put const on the left hand side (e.g., kNo_BBox !=
fmalita_google_do_not_use
2013/12/10 19:48:17
Done.
| |
74 if (useBBox) { | 144 if (bbox != kNo_BBox) { |
robertphillips
2013/12/10 18:44:28
Doesn't the next 5 lines only need to be done if w
fmalita_google_do_not_use
2013/12/10 19:48:17
That is correct, I was trading sloppiness for brev
| |
75 SkPicture* bboxPicture = SkNEW(SkPicture); | 145 SkASSERT(!fTileSize.isEmpty()); |
146 SkTileGridPicture::TileGridInfo gridInfo; | |
147 gridInfo.fMargin = SkISize::Make(0, 0); | |
148 gridInfo.fOffset = SkIPoint::Make(0, 0); | |
149 gridInfo.fTileInterval = fTileSize.toRound(); | |
150 | |
151 SkPicture* bboxPicture = (bbox == kRTree_BBox) | |
152 ? SkNEW(SkPicture) | |
153 : SkNEW_ARGS(SkTileGridPicture, (pic->width(), pic->height() , gridInfo)); | |
76 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(), | 154 pic->draw(bboxPicture->beginRecording(pic->width(), pic->height(), |
77 SkPicture::kOptimizeForClippedPlayback_RecordingFlag)); | 155 SkPicture::kOptimizeForClippedPlayback_RecordingFlag)); |
78 bboxPicture->endRecording(); | 156 bboxPicture->endRecording(); |
79 SkDELETE(pic); | 157 SkDELETE(pic); |
80 return bboxPicture; | 158 return bboxPicture; |
81 | 159 |
82 } else { | 160 } else { |
83 return pic; | 161 return pic; |
84 } | 162 } |
85 } | 163 } |
86 | 164 |
87 public: | |
88 PictFileView(const char name[] = NULL) : fFilename(name) { | |
89 fPicture = NULL; | |
90 fBBoxPicture = NULL; | |
91 fUseBBox = false; | |
92 } | |
93 | |
94 virtual ~PictFileView() { | |
95 SkSafeUnref(fPicture); | |
96 SkSafeUnref(fBBoxPicture); | |
97 } | |
98 | |
99 protected: | |
100 // overrides from SkEventSink | |
101 virtual bool onQuery(SkEvent* evt) { | |
102 if (SampleCode::TitleQ(*evt)) { | |
103 SkString name("P:"); | |
104 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); | |
105 name.append(basename ? basename+1: fFilename.c_str()); | |
106 if (fUseBBox) { | |
107 name.append(" <bbox>"); | |
108 } | |
109 SampleCode::TitleR(evt, name.c_str()); | |
110 return true; | |
111 } | |
112 return this->INHERITED::onQuery(evt); | |
113 } | |
114 | |
115 virtual bool onEvent(const SkEvent& evt) { | |
116 if (evt.isType("PictFileView::toggleBBox")) { | |
117 fUseBBox = !fUseBBox; | |
118 return true; | |
119 } | |
120 return this->INHERITED::onEvent(evt); | |
121 } | |
122 | |
123 virtual void onDrawContent(SkCanvas* canvas) { | |
124 SkPicture** picture = fUseBBox ? &fBBoxPicture : &fPicture; | |
125 | |
126 if (!*picture) { | |
127 *picture = LoadPicture(fFilename.c_str(), fUseBBox); | |
128 } | |
129 if (*picture) { | |
130 canvas->drawPicture(**picture); | |
131 } | |
132 } | |
133 | |
134 private: | |
135 typedef SampleView INHERITED; | 165 typedef SampleView INHERITED; |
136 }; | 166 }; |
137 | 167 |
138 SampleView* CreateSamplePictFileView(const char filename[]); | 168 SampleView* CreateSamplePictFileView(const char filename[]); |
139 SampleView* CreateSamplePictFileView(const char filename[]) { | 169 SampleView* CreateSamplePictFileView(const char filename[]) { |
140 return new PictFileView(filename); | 170 return new PictFileView(filename); |
141 } | 171 } |
142 | 172 |
143 ////////////////////////////////////////////////////////////////////////////// | 173 ////////////////////////////////////////////////////////////////////////////// |
144 | 174 |
145 #if 0 | 175 #if 0 |
146 static SkView* MyFactory() { return new PictFileView; } | 176 static SkView* MyFactory() { return new PictFileView; } |
147 static SkViewRegister reg(MyFactory); | 177 static SkViewRegister reg(MyFactory); |
148 #endif | 178 #endif |
OLD | NEW |