OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
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" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 virtual ~PictFileView() { | 44 virtual ~PictFileView() { |
45 for (int i = 0; i < kBBoxTypeCount; ++i) { | 45 for (int i = 0; i < kBBoxTypeCount; ++i) { |
46 SkSafeUnref(fPictures[i]); | 46 SkSafeUnref(fPictures[i]); |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 virtual void onTileSizeChanged(const SkSize &tileSize) SK_OVERRIDE { | 50 virtual void onTileSizeChanged(const SkSize &tileSize) SK_OVERRIDE { |
51 if (tileSize != fTileSize) { | 51 if (tileSize != fTileSize) { |
52 fTileSize = tileSize; | 52 fTileSize = tileSize; |
53 SkSafeSetNull(fPictures[kTileGrid_BBoxType]); | |
54 } | 53 } |
55 } | 54 } |
56 | 55 |
57 protected: | 56 protected: |
58 // overrides from SkEventSink | 57 // overrides from SkEventSink |
59 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { | 58 virtual bool onQuery(SkEvent* evt) SK_OVERRIDE { |
60 if (SampleCode::TitleQ(*evt)) { | 59 if (SampleCode::TitleQ(*evt)) { |
61 SkString name("P:"); | 60 SkString name("P:"); |
62 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); | 61 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
63 name.append(basename ? basename+1: fFilename.c_str()); | 62 name.append(basename ? basename+1: fFilename.c_str()); |
64 switch (fBBox) { | 63 switch (fBBox) { |
65 case kNo_BBoxType: | 64 case kNo_BBoxType: |
66 // No name appended | 65 // No name appended |
67 break; | 66 break; |
68 case kRTree_BBoxType: | 67 case kRTree_BBoxType: |
69 name.append(" <bbox: R>"); | 68 name.append(" <bbox: R>"); |
70 break; | 69 break; |
71 case kTileGrid_BBoxType: | |
72 name.append(" <bbox: T>"); | |
73 break; | |
74 default: | 70 default: |
75 SkASSERT(false); | 71 SkASSERT(false); |
76 break; | 72 break; |
77 } | 73 } |
78 SampleCode::TitleR(evt, name.c_str()); | 74 SampleCode::TitleR(evt, name.c_str()); |
79 return true; | 75 return true; |
80 } | 76 } |
81 return this->INHERITED::onQuery(evt); | 77 return this->INHERITED::onQuery(evt); |
82 } | 78 } |
83 | 79 |
(...skipping 14 matching lines...) Expand all Loading... |
98 } | 94 } |
99 if (*picture) { | 95 if (*picture) { |
100 canvas->drawPicture(*picture); | 96 canvas->drawPicture(*picture); |
101 } | 97 } |
102 } | 98 } |
103 | 99 |
104 private: | 100 private: |
105 enum BBoxType { | 101 enum BBoxType { |
106 kNo_BBoxType, | 102 kNo_BBoxType, |
107 kRTree_BBoxType, | 103 kRTree_BBoxType, |
108 kTileGrid_BBoxType, | |
109 | 104 |
110 kLast_BBoxType = kTileGrid_BBoxType | 105 kLast_BBoxType = kRTree_BBoxType, |
111 }; | 106 }; |
112 static const int kBBoxTypeCount = kLast_BBoxType + 1; | 107 static const int kBBoxTypeCount = kLast_BBoxType + 1; |
113 | 108 |
114 SkString fFilename; | 109 SkString fFilename; |
115 SkPicture* fPictures[kBBoxTypeCount]; | 110 SkPicture* fPictures[kBBoxTypeCount]; |
116 BBoxType fBBox; | 111 BBoxType fBBox; |
117 SkSize fTileSize; | 112 SkSize fTileSize; |
118 | 113 |
119 SkPicture* LoadPicture(const char path[], BBoxType bbox) { | 114 SkPicture* LoadPicture(const char path[], BBoxType bbox) { |
120 SkAutoTUnref<SkPicture> pic; | 115 SkAutoTUnref<SkPicture> pic; |
121 | 116 |
122 SkBitmap bm; | 117 SkBitmap bm; |
123 if (SkImageDecoder::DecodeFile(path, &bm)) { | 118 if (SkImageDecoder::DecodeFile(path, &bm)) { |
124 bm.setImmutable(); | 119 bm.setImmutable(); |
125 SkPictureRecorder recorder; | 120 SkPictureRecorder recorder; |
126 SkCanvas* can = recorder.beginRecording(SkIntToScalar(bm.width()), | 121 SkCanvas* can = recorder.beginRecording(SkIntToScalar(bm.width()), |
127 SkIntToScalar(bm.height()), | 122 SkIntToScalar(bm.height()), |
128 NULL, 0); | 123 NULL, 0); |
129 can->drawBitmap(bm, 0, 0, NULL); | 124 can->drawBitmap(bm, 0, 0, NULL); |
130 pic.reset(recorder.endRecording()); | 125 pic.reset(recorder.endRecording()); |
131 } else { | 126 } else { |
132 SkFILEStream stream(path); | 127 SkFILEStream stream(path); |
133 if (stream.isValid()) { | 128 if (stream.isValid()) { |
134 pic.reset(SkPicture::CreateFromStream(&stream)); | 129 pic.reset(SkPicture::CreateFromStream(&stream)); |
135 } else { | 130 } else { |
136 SkDebugf("coun't load picture at \"path\"\n", path); | 131 SkDebugf("coun't load picture at \"path\"\n", path); |
137 } | 132 } |
138 | 133 |
139 if (false) { // re-record | 134 if (false) { // re-record |
140 SkPictureRecorder recorder; | 135 SkPictureRecorder recorder; |
141 pic->playback(recorder.beginRecording(pic->cullRect().width(), | 136 pic->playback(recorder.beginRecording(pic->cullRect().width(), |
142 pic->cullRect().height(), | 137 pic->cullRect().height(), |
143 NULL, 0)); | 138 NULL, 0)); |
144 SkAutoTUnref<SkPicture> p2(recorder.endRecording()); | 139 SkAutoTUnref<SkPicture> p2(recorder.endRecording()); |
145 | 140 |
146 SkString path2(path); | 141 SkString path2(path); |
147 path2.append(".new.skp"); | 142 path2.append(".new.skp"); |
148 SkFILEWStream writer(path2.c_str()); | 143 SkFILEWStream writer(path2.c_str()); |
149 p2->serialize(&writer); | 144 p2->serialize(&writer); |
150 } | 145 } |
151 } | 146 } |
152 | 147 |
153 if (NULL == pic) { | 148 if (NULL == pic) { |
154 return NULL; | 149 return NULL; |
155 } | 150 } |
156 | 151 |
157 SkAutoTDelete<SkBBHFactory> factory; | 152 SkAutoTDelete<SkBBHFactory> factory; |
158 switch (bbox) { | 153 switch (bbox) { |
159 case kNo_BBoxType: | 154 case kNo_BBoxType: |
160 // no bbox playback necessary | 155 // no bbox playback necessary |
161 return pic.detach(); | 156 return pic.detach(); |
162 case kRTree_BBoxType: | 157 case kRTree_BBoxType: |
163 factory.reset(SkNEW(SkRTreeFactory)); | 158 factory.reset(SkNEW(SkRTreeFactory)); |
164 break; | 159 break; |
165 case kTileGrid_BBoxType: { | |
166 SkASSERT(!fTileSize.isEmpty()); | |
167 SkTileGridFactory::TileGridInfo gridInfo; | |
168 gridInfo.fMargin = SkISize::Make(0, 0); | |
169 gridInfo.fOffset = SkIPoint::Make(0, 0); | |
170 gridInfo.fTileInterval = fTileSize.toRound(); | |
171 factory.reset(SkNEW_ARGS(SkTileGridFactory, (gridInfo))); | |
172 break; | |
173 } | |
174 default: | 160 default: |
175 SkASSERT(false); | 161 SkASSERT(false); |
176 } | 162 } |
177 | 163 |
178 SkPictureRecorder recorder; | 164 SkPictureRecorder recorder; |
179 pic->playback(recorder.beginRecording(pic->cullRect().width(), | 165 pic->playback(recorder.beginRecording(pic->cullRect().width(), |
180 pic->cullRect().height(), | 166 pic->cullRect().height(), |
181 factory.get(), 0)); | 167 factory.get(), 0)); |
182 return recorder.endRecording(); | 168 return recorder.endRecording(); |
183 } | 169 } |
184 | 170 |
185 typedef SampleView INHERITED; | 171 typedef SampleView INHERITED; |
186 }; | 172 }; |
187 | 173 |
188 SampleView* CreateSamplePictFileView(const char filename[]); | 174 SampleView* CreateSamplePictFileView(const char filename[]); |
189 SampleView* CreateSamplePictFileView(const char filename[]) { | 175 SampleView* CreateSamplePictFileView(const char filename[]) { |
190 return new PictFileView(filename); | 176 return new PictFileView(filename); |
191 } | 177 } |
192 | 178 |
193 ////////////////////////////////////////////////////////////////////////////// | 179 ////////////////////////////////////////////////////////////////////////////// |
194 | 180 |
195 #if 0 | 181 #if 0 |
196 static SkView* MyFactory() { return new PictFileView; } | 182 static SkView* MyFactory() { return new PictFileView; } |
197 static SkViewRegister reg(MyFactory); | 183 static SkViewRegister reg(MyFactory); |
198 #endif | 184 #endif |
OLD | NEW |