Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Side by Side Diff: bench/benchmain.cpp

Issue 153133002: change benchmark to use surfaces instead of devices to specify its backends (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "BenchTimer.h" 8 #include "BenchTimer.h"
9 #include "ResultsWriter.h" 9 #include "ResultsWriter.h"
10 #include "SkBenchLogger.h" 10 #include "SkBenchLogger.h"
11 #include "SkBenchmark.h" 11 #include "SkBenchmark.h"
12 #include "SkBitmapDevice.h" 12 #include "SkBitmapDevice.h"
13 #include "SkCanvas.h" 13 #include "SkCanvas.h"
14 #include "SkColorPriv.h" 14 #include "SkColorPriv.h"
15 #include "SkCommandLineFlags.h" 15 #include "SkCommandLineFlags.h"
16 #include "SkData.h"
16 #include "SkDeferredCanvas.h" 17 #include "SkDeferredCanvas.h"
17 #include "SkGraphics.h" 18 #include "SkGraphics.h"
18 #include "SkImageEncoder.h" 19 #include "SkImageEncoder.h"
19 #include "SkOSFile.h" 20 #include "SkOSFile.h"
20 #include "SkPicture.h" 21 #include "SkPicture.h"
21 #include "SkString.h" 22 #include "SkString.h"
23 #include "SkSurface.h"
22 24
23 #if SK_SUPPORT_GPU 25 #if SK_SUPPORT_GPU
24 #include "GrContext.h" 26 #include "GrContext.h"
25 #include "GrContextFactory.h" 27 #include "GrContextFactory.h"
26 #include "GrRenderTarget.h" 28 #include "GrRenderTarget.h"
27 #include "SkGpuDevice.h" 29 #include "SkGpuDevice.h"
28 #include "gl/GrGLDefines.h" 30 #include "gl/GrGLDefines.h"
29 #else 31 #else
30 class GrContext; 32 class GrContext;
31 #endif // SK_SUPPORT_GPU 33 #endif // SK_SUPPORT_GPU
32 34
33 #include <limits> 35 #include <limits>
34 36
35 enum BenchMode { 37 enum BenchMode {
36 kNormal_BenchMode, 38 kNormal_BenchMode,
37 kDeferred_BenchMode, 39 kDeferred_BenchMode,
38 kDeferredSilent_BenchMode, 40 kDeferredSilent_BenchMode,
39 kRecord_BenchMode, 41 kRecord_BenchMode,
40 kPictureRecord_BenchMode 42 kPictureRecord_BenchMode
41 }; 43 };
42 const char* BenchMode_Name[] = { 44 const char* BenchMode_Name[] = {
43 "normal", "deferred", "deferredSilent", "record", "picturerecord" 45 "normal", "deferred", "deferredSilent", "record", "picturerecord"
44 }; 46 };
45 47
46 static const char kDefaultsConfigStr[] = "defaults"; 48 static const char kDefaultsConfigStr[] = "defaults";
47 49
48 /////////////////////////////////////////////////////////////////////////////// 50 ///////////////////////////////////////////////////////////////////////////////
49 51
50 static void erase(SkBitmap& bm) {
51 if (bm.config() == SkBitmap::kA8_Config) {
52 bm.eraseColor(SK_ColorTRANSPARENT);
53 } else {
54 bm.eraseColor(SK_ColorWHITE);
55 }
56 }
57
58 class Iter { 52 class Iter {
59 public: 53 public:
60 Iter() : fBench(BenchRegistry::Head()) {} 54 Iter() : fBench(BenchRegistry::Head()) {}
61 55
62 SkBenchmark* next() { 56 SkBenchmark* next() {
63 if (fBench) { 57 if (fBench) {
64 BenchRegistry::Factory f = fBench->factory(); 58 BenchRegistry::Factory f = fBench->factory();
65 fBench = fBench->next(); 59 fBench = fBench->next();
66 return f(); 60 return f();
67 } 61 }
(...skipping 25 matching lines...) Expand all
93 case ' ': 87 case ' ':
94 case ':': 88 case ':':
95 path->writable_str()[i] = '-'; 89 path->writable_str()[i] = '-';
96 break; 90 break;
97 default: 91 default:
98 break; 92 break;
99 } 93 }
100 } 94 }
101 } 95 }
102 96
103 static void saveFile(const char name[], const char config[], const char dir[], 97 static void saveFile(const char name[], const char config[], const char dir[],
mtklein 2014/02/03 21:34:43 This would make a bit more conceptual sense if it
reed1 2014/02/03 21:39:48 Not sure why. I am saving the state of the surface
mtklein 2014/02/03 21:47:32 Yeah, that's sort of why it made more sense to me
104 const SkBitmap& bm) { 98 SkSurface* surface) {
105 SkBitmap copy; 99 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
106 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) { 100 if (NULL == image.get()) {
107 return; 101 return;
108 } 102 }
109 103
110 if (bm.config() == SkBitmap::kA8_Config) { 104 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
111 // turn alpha into gray-scale 105 if (NULL == data.get()) {
112 size_t size = copy.getSize() >> 2; 106 return;
113 SkPMColor* p = copy.getAddr32(0, 0);
114 for (size_t i = 0; i < size; i++) {
115 int c = (*p >> SK_A32_SHIFT) & 0xFF;
116 c = 255 - c;
117 c |= (c << 24) | (c << 16) | (c << 8);
118 *p++ = c | (SK_A32_MASK << SK_A32_SHIFT);
119 }
120 } 107 }
121 108
122 SkString filename; 109 SkString filename;
123 make_filename(name, &filename); 110 make_filename(name, &filename);
124 filename.appendf("_%s.png", config); 111 filename.appendf("_%s.png", config);
125 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str()); 112 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
126 ::remove(path.c_str()); 113 ::remove(path.c_str());
127 SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 10 0); 114
115 SkFILEWStream stream(filename.c_str());
116 stream.write(data->data(), data->size());
128 } 117 }
129 118
130 static void performClip(SkCanvas* canvas, int w, int h) { 119 static void performClip(SkCanvas* canvas, int w, int h) {
131 SkRect r; 120 SkRect r;
132 121
133 r.set(SkIntToScalar(10), SkIntToScalar(10), 122 r.set(SkIntToScalar(10), SkIntToScalar(10),
134 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); 123 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
135 canvas->clipRect(r, SkRegion::kIntersect_Op); 124 canvas->clipRect(r, SkRegion::kIntersect_Op);
136 125
137 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), 126 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
(...skipping 13 matching lines...) Expand all
151 static void performScale(SkCanvas* canvas, int w, int h) { 140 static void performScale(SkCanvas* canvas, int w, int h) {
152 const SkScalar x = SkIntToScalar(w) / 2; 141 const SkScalar x = SkIntToScalar(w) / 2;
153 const SkScalar y = SkIntToScalar(h) / 2; 142 const SkScalar y = SkIntToScalar(h) / 2;
154 143
155 canvas->translate(x, y); 144 canvas->translate(x, y);
156 // just enough so we can't take the sprite case 145 // just enough so we can't take the sprite case
157 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100); 146 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
158 canvas->translate(-x, -y); 147 canvas->translate(-x, -y);
159 } 148 }
160 149
161 static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size, 150 static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
162 SkBenchmark::Backend backend, int sampleCount, GrContext* context) { 151 SkBenchmark::Backend backend, int sampleCount,
163 SkBaseDevice* device = NULL; 152 GrContext* context) {
164 SkBitmap bitmap; 153 SkSurface* surface = NULL;
165 bitmap.setConfig(config, size.fX, size.fY); 154 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
155 kPremul_SkAlphaType);
166 156
167 switch (backend) { 157 switch (backend) {
168 case SkBenchmark::kRaster_Backend: 158 case SkBenchmark::kRaster_Backend:
169 bitmap.allocPixels(); 159 surface = SkSurface::NewRaster(info);
170 erase(bitmap); 160 surface->getCanvas()->clear(SK_ColorWHITE);
171 device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
172 break; 161 break;
173 #if SK_SUPPORT_GPU 162 #if SK_SUPPORT_GPU
174 case SkBenchmark::kGPU_Backend: { 163 case SkBenchmark::kGPU_Backend: {
175 GrTextureDesc desc; 164 surface = SkSurface::NewRenderTarget(context, info, sampleCount);
176 desc.fConfig = kSkia8888_GrPixelConfig;
177 desc.fFlags = kRenderTarget_GrTextureFlagBit;
178 desc.fWidth = size.fX;
179 desc.fHeight = size.fY;
180 desc.fSampleCnt = sampleCount;
181 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
182 if (!texture) {
183 return NULL;
184 }
185 device = SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
186 break; 165 break;
187 } 166 }
188 #endif 167 #endif
189 case SkBenchmark::kPDF_Backend: 168 case SkBenchmark::kPDF_Backend:
190 default: 169 default:
191 SkDEBUGFAIL("unsupported"); 170 SkDEBUGFAIL("unsupported");
192 } 171 }
193 return device; 172 return surface;
194 } 173 }
195 174
196 #if SK_SUPPORT_GPU 175 #if SK_SUPPORT_GPU
197 GrContextFactory gContextFactory; 176 GrContextFactory gContextFactory;
198 typedef GrContextFactory::GLContextType GLContextType; 177 typedef GrContextFactory::GLContextType GLContextType;
199 static const GLContextType kNative = GrContextFactory::kNative_GLContextType; 178 static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
200 static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType; 179 static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
201 #if SK_ANGLE 180 #if SK_ANGLE
202 static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType; 181 static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
203 #endif 182 #endif
204 static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType; 183 static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
205 static const GLContextType kNull = GrContextFactory::kNull_GLContextType; 184 static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
206 #else 185 #else
207 typedef int GLContextType; 186 typedef int GLContextType;
208 static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0; 187 static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
209 #endif 188 #endif
210 189
211 #ifdef SK_DEBUG 190 #ifdef SK_DEBUG
212 static const bool kIsDebug = true; 191 static const bool kIsDebug = true;
213 #else 192 #else
214 static const bool kIsDebug = false; 193 static const bool kIsDebug = false;
215 #endif 194 #endif
216 195
217 static const struct Config { 196 static const struct Config {
218 SkBitmap::Config config; 197 SkColorType fColorType;
219 const char* name; 198 const char* name;
220 int sampleCount; 199 int sampleCount;
221 SkBenchmark::Backend backend; 200 SkBenchmark::Backend backend;
222 GLContextType contextType; 201 GLContextType contextType;
223 bool runByDefault; 202 bool runByDefault;
224 } gConfigs[] = { 203 } gConfigs[] = {
225 { SkBitmap::kNo_Config, "NONRENDERING", 0, SkBenchmark::kNonRendering _Backend, kNative, true}, 204 { kPMColor_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backen d, kNative, true},
226 { SkBitmap::kARGB_8888_Config, "8888", 0, SkBenchmark::kRaster_Backe nd, kNative, true}, 205 { kPMColor_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
227 { SkBitmap::kRGB_565_Config, "565", 0, SkBenchmark::kRaster_Backe nd, kNative, true}, 206 { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
228 #if SK_SUPPORT_GPU 207 #if SK_SUPPORT_GPU
229 { SkBitmap::kARGB_8888_Config, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true}, 208 { kPMColor_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
230 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false}, 209 { kPMColor_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
231 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false}, 210 { kPMColor_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
232 { SkBitmap::kARGB_8888_Config, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true}, 211 { kPMColor_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true},
233 { SkBitmap::kARGB_8888_Config, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false}, 212 { kPMColor_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false},
234 #if SK_ANGLE 213 #if SK_ANGLE
235 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true}, 214 { kPMColor_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
236 #endif // SK_ANGLE 215 #endif // SK_ANGLE
237 { SkBitmap::kARGB_8888_Config, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug}, 216 { kPMColor_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
238 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true}, 217 { kPMColor_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
239 #endif // SK_SUPPORT_GPU 218 #endif // SK_SUPPORT_GPU
240 }; 219 };
241 220
242 DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir." ); 221 DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir." );
243 DEFINE_string(timers, "cg", "Timers to display. " 222 DEFINE_string(timers, "cg", "Timers to display. "
244 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)"); 223 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
245 224
246 DEFINE_bool(rotate, false, "Rotate canvas before bench run?"); 225 DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
247 DEFINE_bool(scale, false, "Scale canvas before bench run?"); 226 DEFINE_bool(scale, false, "Scale canvas before bench run?");
248 DEFINE_bool(clip, false, "Clip canvas before bench run?"); 227 DEFINE_bool(clip, false, "Clip canvas before bench run?");
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 #if SK_SUPPORT_GPU 463 #if SK_SUPPORT_GPU
485 SkGLContextHelper* glContext = NULL; 464 SkGLContextHelper* glContext = NULL;
486 if (SkBenchmark::kGPU_Backend == config.backend) { 465 if (SkBenchmark::kGPU_Backend == config.backend) {
487 context = gContextFactory.get(config.contextType); 466 context = gContextFactory.get(config.contextType);
488 if (NULL == context) { 467 if (NULL == context) {
489 continue; 468 continue;
490 } 469 }
491 glContext = gContextFactory.getGLContext(config.contextType); 470 glContext = gContextFactory.getGLContext(config.contextType);
492 } 471 }
493 #endif 472 #endif
494 SkAutoTUnref<SkBaseDevice> device; 473
495 SkAutoTUnref<SkCanvas> canvas; 474 SkAutoTUnref<SkCanvas> canvas;
496 SkPicture recordFrom, recordTo; 475 SkPicture recordFrom, recordTo;
497 const SkIPoint dim = bench->getSize(); 476 const SkIPoint dim = bench->getSize();
498 477
499 const SkPicture::RecordingFlags kRecordFlags = 478 const SkPicture::RecordingFlags kRecordFlags =
500 SkPicture::kUsePathBoundsForClip_RecordingFlag; 479 SkPicture::kUsePathBoundsForClip_RecordingFlag;
501 480
481 SkAutoTUnref<SkSurface> surface;
502 if (SkBenchmark::kNonRendering_Backend != config.backend) { 482 if (SkBenchmark::kNonRendering_Backend != config.backend) {
503 device.reset(make_device(config.config, 483 surface.reset(make_surface(config.fColorType,
504 dim, 484 dim,
505 config.backend, 485 config.backend,
506 config.sampleCount, 486 config.sampleCount,
507 context)); 487 context));
508 if (!device.get()) { 488 if (!surface.get()) {
509 logger.logError(SkStringPrintf( 489 logger.logError(SkStringPrintf(
510 "Device creation failure for config %s. Will skip.\n", c onfig.name)); 490 "Device creation failure for config %s. Will skip.\n", c onfig.name));
511 continue; 491 continue;
512 } 492 }
513 493
514 switch(benchMode) { 494 switch(benchMode) {
515 case kDeferredSilent_BenchMode: 495 case kDeferredSilent_BenchMode:
516 case kDeferred_BenchMode: 496 case kDeferred_BenchMode:
517 canvas.reset(SkDeferredCanvas::Create(device.get())); 497 canvas.reset(SkDeferredCanvas::Create(surface.get()));
518 break; 498 break;
519 case kRecord_BenchMode: 499 case kRecord_BenchMode:
520 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags))); 500 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags)));
521 break; 501 break;
522 case kPictureRecord_BenchMode: 502 case kPictureRecord_BenchMode:
523 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags)); 503 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
524 recordFrom.endRecording(); 504 recordFrom.endRecording();
525 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags))); 505 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags)));
526 break; 506 break;
527 case kNormal_BenchMode: 507 case kNormal_BenchMode:
528 canvas.reset(new SkCanvas(device.get())); 508 canvas.reset(SkRef(surface->getCanvas()));
529 break; 509 break;
530 default: 510 default:
531 SkASSERT(false); 511 SkASSERT(false);
532 } 512 }
533 } 513 }
534 514
535 if (NULL != canvas) { 515 if (NULL != canvas) {
536 canvas->clear(SK_ColorWHITE); 516 canvas->clear(SK_ColorWHITE);
537 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); } 517 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
538 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); } 518 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 if (FLAGS_verbose) { SkDebugf("%.3g ", current); } 636 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
657 converged = HasConverged(previous, current, timer.fWall); 637 converged = HasConverged(previous, current, timer.fWall);
658 previous = current; 638 previous = current;
659 } while (!kIsDebug && !converged); 639 } while (!kIsDebug && !converged);
660 if (FLAGS_verbose) { SkDebugf("\n"); } 640 if (FLAGS_verbose) { SkDebugf("\n"); }
661 641
662 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != co nfig.backend) { 642 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != co nfig.backend) {
663 saveFile(bench->getName(), 643 saveFile(bench->getName(),
664 config.name, 644 config.name,
665 FLAGS_outDir[0], 645 FLAGS_outDir[0],
666 device->accessBitmap(false)); 646 surface);
667 } 647 }
668 648
669 if (kIsDebug) { 649 if (kIsDebug) {
670 // Let's not mislead ourselves by looking at Debug build bench t imes! 650 // Let's not mislead ourselves by looking at Debug build bench t imes!
671 continue; 651 continue;
672 } 652 }
673 653
674 // Normalize to ms per 1000 iterations. 654 // Normalize to ms per 1000 iterations.
675 const double normalize = 1000.0 / loopsPerIter; 655 const double normalize = 1000.0 / loopsPerIter;
676 const struct { char shortName; const char* longName; double ms; } ti mes[] = { 656 const struct { char shortName; const char* longName; double ms; } ti mes[] = {
(...skipping 16 matching lines...) Expand all
693 gContextFactory.destroyContexts(); 673 gContextFactory.destroyContexts();
694 #endif 674 #endif
695 return 0; 675 return 0;
696 } 676 }
697 677
698 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) 678 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
699 int main(int argc, char * const argv[]) { 679 int main(int argc, char * const argv[]) {
700 return tool_main(argc, (char**) argv); 680 return tool_main(argc, (char**) argv);
701 } 681 }
702 #endif 682 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698