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

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 26 matching lines...) Expand all
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[],
104 const SkBitmap& bm) { 98 const SkImage* image) {
105 SkBitmap copy; 99 SkAutoTUnref<SkData> data(image->encode(SkImageEncoder::kPNG_Type, 100));
106 if (!bm.copyTo(&copy, SkBitmap::kARGB_8888_Config)) { 100 if (NULL == data.get()) {
107 return; 101 return;
108 } 102 }
109 103
110 if (bm.config() == SkBitmap::kA8_Config) {
111 // turn alpha into gray-scale
112 size_t size = copy.getSize() >> 2;
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 }
121
122 SkString filename; 104 SkString filename;
123 make_filename(name, &filename); 105 make_filename(name, &filename);
124 filename.appendf("_%s.png", config); 106 filename.appendf("_%s.png", config);
125 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str()); 107 SkString path = SkOSPath::SkPathJoin(dir, filename.c_str());
126 ::remove(path.c_str()); 108 ::remove(path.c_str());
127 SkImageEncoder::EncodeFile(path.c_str(), copy, SkImageEncoder::kPNG_Type, 10 0); 109
110 SkFILEWStream stream(filename.c_str());
111 stream.write(data->data(), data->size());
128 } 112 }
129 113
130 static void performClip(SkCanvas* canvas, int w, int h) { 114 static void performClip(SkCanvas* canvas, int w, int h) {
131 SkRect r; 115 SkRect r;
132 116
133 r.set(SkIntToScalar(10), SkIntToScalar(10), 117 r.set(SkIntToScalar(10), SkIntToScalar(10),
134 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3)); 118 SkIntToScalar(w*2/3), SkIntToScalar(h*2/3));
135 canvas->clipRect(r, SkRegion::kIntersect_Op); 119 canvas->clipRect(r, SkRegion::kIntersect_Op);
136 120
137 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3), 121 r.set(SkIntToScalar(w/3), SkIntToScalar(h/3),
(...skipping 13 matching lines...) Expand all
151 static void performScale(SkCanvas* canvas, int w, int h) { 135 static void performScale(SkCanvas* canvas, int w, int h) {
152 const SkScalar x = SkIntToScalar(w) / 2; 136 const SkScalar x = SkIntToScalar(w) / 2;
153 const SkScalar y = SkIntToScalar(h) / 2; 137 const SkScalar y = SkIntToScalar(h) / 2;
154 138
155 canvas->translate(x, y); 139 canvas->translate(x, y);
156 // just enough so we can't take the sprite case 140 // just enough so we can't take the sprite case
157 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100); 141 canvas->scale(SK_Scalar1 * 99/100, SK_Scalar1 * 99/100);
158 canvas->translate(-x, -y); 142 canvas->translate(-x, -y);
159 } 143 }
160 144
161 static SkBaseDevice* make_device(SkBitmap::Config config, const SkIPoint& size, 145 static SkSurface* make_surface(SkColorType colorType, const SkIPoint& size,
162 SkBenchmark::Backend backend, int sampleCount, GrContext* context) { 146 SkBenchmark::Backend backend, int sampleCount,
163 SkBaseDevice* device = NULL; 147 GrContext* context) {
164 SkBitmap bitmap; 148 SkSurface* surface = NULL;
165 bitmap.setConfig(config, size.fX, size.fY); 149 SkImageInfo info = SkImageInfo::Make(size.fX, size.fY, colorType,
150 kPremul_SkAlphaType);
166 151
167 switch (backend) { 152 switch (backend) {
168 case SkBenchmark::kRaster_Backend: 153 case SkBenchmark::kRaster_Backend:
169 bitmap.allocPixels(); 154 surface = SkSurface::NewRaster(info);
170 erase(bitmap); 155 surface->getCanvas()->clear(SK_ColorWHITE);
171 device = SkNEW_ARGS(SkBitmapDevice, (bitmap));
172 break; 156 break;
173 #if SK_SUPPORT_GPU 157 #if SK_SUPPORT_GPU
174 case SkBenchmark::kGPU_Backend: { 158 case SkBenchmark::kGPU_Backend: {
175 GrTextureDesc desc; 159 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; 160 break;
187 } 161 }
188 #endif 162 #endif
189 case SkBenchmark::kPDF_Backend: 163 case SkBenchmark::kPDF_Backend:
190 default: 164 default:
191 SkDEBUGFAIL("unsupported"); 165 SkDEBUGFAIL("unsupported");
192 } 166 }
193 return device; 167 return surface;
194 } 168 }
195 169
196 #if SK_SUPPORT_GPU 170 #if SK_SUPPORT_GPU
197 GrContextFactory gContextFactory; 171 GrContextFactory gContextFactory;
198 typedef GrContextFactory::GLContextType GLContextType; 172 typedef GrContextFactory::GLContextType GLContextType;
199 static const GLContextType kNative = GrContextFactory::kNative_GLContextType; 173 static const GLContextType kNative = GrContextFactory::kNative_GLContextType;
200 static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType; 174 static const GLContextType kNVPR = GrContextFactory::kNVPR_GLContextType;
201 #if SK_ANGLE 175 #if SK_ANGLE
202 static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType; 176 static const GLContextType kANGLE = GrContextFactory::kANGLE_GLContextType;
203 #endif 177 #endif
204 static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType; 178 static const GLContextType kDebug = GrContextFactory::kDebug_GLContextType;
205 static const GLContextType kNull = GrContextFactory::kNull_GLContextType; 179 static const GLContextType kNull = GrContextFactory::kNull_GLContextType;
206 #else 180 #else
207 typedef int GLContextType; 181 typedef int GLContextType;
208 static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0; 182 static const GLContextType kNative = 0, kANGLE = 0, kDebug = 0, kNull = 0;
209 #endif 183 #endif
210 184
211 #ifdef SK_DEBUG 185 #ifdef SK_DEBUG
212 static const bool kIsDebug = true; 186 static const bool kIsDebug = true;
213 #else 187 #else
214 static const bool kIsDebug = false; 188 static const bool kIsDebug = false;
215 #endif 189 #endif
216 190
217 static const struct Config { 191 static const struct Config {
218 SkBitmap::Config config; 192 SkColorType fColorType;
219 const char* name; 193 const char* name;
220 int sampleCount; 194 int sampleCount;
221 SkBenchmark::Backend backend; 195 SkBenchmark::Backend backend;
222 GLContextType contextType; 196 GLContextType contextType;
223 bool runByDefault; 197 bool runByDefault;
224 } gConfigs[] = { 198 } gConfigs[] = {
225 { SkBitmap::kNo_Config, "NONRENDERING", 0, SkBenchmark::kNonRendering _Backend, kNative, true}, 199 { kPMColor_SkColorType, "NONRENDERING", 0, SkBenchmark::kNonRendering_Backen d, kNative, true},
226 { SkBitmap::kARGB_8888_Config, "8888", 0, SkBenchmark::kRaster_Backe nd, kNative, true}, 200 { kPMColor_SkColorType, "8888", 0, SkBenchmark::kRaster_Backend, kNative, true},
227 { SkBitmap::kRGB_565_Config, "565", 0, SkBenchmark::kRaster_Backe nd, kNative, true}, 201 { kRGB_565_SkColorType, "565", 0, SkBenchmark::kRaster_Backend, kNative, true},
228 #if SK_SUPPORT_GPU 202 #if SK_SUPPORT_GPU
229 { SkBitmap::kARGB_8888_Config, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true}, 203 { kPMColor_SkColorType, "GPU", 0, SkBenchmark::kGPU_Backend, kNative, true},
230 { SkBitmap::kARGB_8888_Config, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false}, 204 { kPMColor_SkColorType, "MSAA4", 4, SkBenchmark::kGPU_Backend, kNative, false},
231 { SkBitmap::kARGB_8888_Config, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false}, 205 { kPMColor_SkColorType, "MSAA16", 16, SkBenchmark::kGPU_Backend, kNative, false},
232 { SkBitmap::kARGB_8888_Config, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true}, 206 { kPMColor_SkColorType, "NVPRMSAA4", 4, SkBenchmark::kGPU_Backend, kNVPR, true},
233 { SkBitmap::kARGB_8888_Config, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false}, 207 { kPMColor_SkColorType, "NVPRMSAA16", 16, SkBenchmark::kGPU_Backend, kNVPR, false},
234 #if SK_ANGLE 208 #if SK_ANGLE
235 { SkBitmap::kARGB_8888_Config, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true}, 209 { kPMColor_SkColorType, "ANGLE", 0, SkBenchmark::kGPU_Backend, kANGLE, true},
236 #endif // SK_ANGLE 210 #endif // SK_ANGLE
237 { SkBitmap::kARGB_8888_Config, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug}, 211 { kPMColor_SkColorType, "Debug", 0, SkBenchmark::kGPU_Backend, kDebug, kIsDebug},
238 { SkBitmap::kARGB_8888_Config, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true}, 212 { kPMColor_SkColorType, "NULLGPU", 0, SkBenchmark::kGPU_Backend, kNull, true},
239 #endif // SK_SUPPORT_GPU 213 #endif // SK_SUPPORT_GPU
240 }; 214 };
241 215
242 DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir." ); 216 DEFINE_string(outDir, "", "If given, image of each bench will be put in outDir." );
243 DEFINE_string(timers, "cg", "Timers to display. " 217 DEFINE_string(timers, "cg", "Timers to display. "
244 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)"); 218 "Options: w(all) W(all, truncated) c(pu) C(pu, truncated) g(pu)");
245 219
246 DEFINE_bool(rotate, false, "Rotate canvas before bench run?"); 220 DEFINE_bool(rotate, false, "Rotate canvas before bench run?");
247 DEFINE_bool(scale, false, "Scale canvas before bench run?"); 221 DEFINE_bool(scale, false, "Scale canvas before bench run?");
248 DEFINE_bool(clip, false, "Clip canvas before bench run?"); 222 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 458 #if SK_SUPPORT_GPU
485 SkGLContextHelper* glContext = NULL; 459 SkGLContextHelper* glContext = NULL;
486 if (SkBenchmark::kGPU_Backend == config.backend) { 460 if (SkBenchmark::kGPU_Backend == config.backend) {
487 context = gContextFactory.get(config.contextType); 461 context = gContextFactory.get(config.contextType);
488 if (NULL == context) { 462 if (NULL == context) {
489 continue; 463 continue;
490 } 464 }
491 glContext = gContextFactory.getGLContext(config.contextType); 465 glContext = gContextFactory.getGLContext(config.contextType);
492 } 466 }
493 #endif 467 #endif
494 SkAutoTUnref<SkBaseDevice> device; 468
495 SkAutoTUnref<SkCanvas> canvas; 469 SkAutoTUnref<SkCanvas> canvas;
496 SkPicture recordFrom, recordTo; 470 SkPicture recordFrom, recordTo;
497 const SkIPoint dim = bench->getSize(); 471 const SkIPoint dim = bench->getSize();
498 472
499 const SkPicture::RecordingFlags kRecordFlags = 473 const SkPicture::RecordingFlags kRecordFlags =
500 SkPicture::kUsePathBoundsForClip_RecordingFlag; 474 SkPicture::kUsePathBoundsForClip_RecordingFlag;
501 475
476 SkAutoTUnref<SkSurface> surface;
502 if (SkBenchmark::kNonRendering_Backend != config.backend) { 477 if (SkBenchmark::kNonRendering_Backend != config.backend) {
503 device.reset(make_device(config.config, 478 surface.reset(make_surface(config.fColorType,
504 dim, 479 dim,
505 config.backend, 480 config.backend,
506 config.sampleCount, 481 config.sampleCount,
507 context)); 482 context));
508 if (!device.get()) { 483 if (!surface.get()) {
509 logger.logError(SkStringPrintf( 484 logger.logError(SkStringPrintf(
510 "Device creation failure for config %s. Will skip.\n", c onfig.name)); 485 "Device creation failure for config %s. Will skip.\n", c onfig.name));
511 continue; 486 continue;
512 } 487 }
513 488
514 switch(benchMode) { 489 switch(benchMode) {
515 case kDeferredSilent_BenchMode: 490 case kDeferredSilent_BenchMode:
516 case kDeferred_BenchMode: 491 case kDeferred_BenchMode:
517 canvas.reset(SkDeferredCanvas::Create(device.get())); 492 canvas.reset(SkDeferredCanvas::Create(surface.get()));
518 break; 493 break;
519 case kRecord_BenchMode: 494 case kRecord_BenchMode:
520 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags))); 495 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags)));
521 break; 496 break;
522 case kPictureRecord_BenchMode: 497 case kPictureRecord_BenchMode:
523 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags)); 498 bench->draw(1, recordFrom.beginRecording(dim.fX, dim.fY, kRecordFlags));
524 recordFrom.endRecording(); 499 recordFrom.endRecording();
525 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags))); 500 canvas.reset(SkRef(recordTo.beginRecording(dim.fX, dim.f Y, kRecordFlags)));
526 break; 501 break;
527 case kNormal_BenchMode: 502 case kNormal_BenchMode:
528 canvas.reset(new SkCanvas(device.get())); 503 canvas.reset(SkRef(surface->getCanvas()));
529 break; 504 break;
530 default: 505 default:
531 SkASSERT(false); 506 SkASSERT(false);
532 } 507 }
533 } 508 }
534 509
535 if (NULL != canvas) { 510 if (NULL != canvas) {
536 canvas->clear(SK_ColorWHITE); 511 canvas->clear(SK_ColorWHITE);
537 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); } 512 if (FLAGS_clip) { performClip(canvas, dim.fX, dim.fY); }
538 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); } 513 if (FLAGS_scale) { performScale(canvas, dim.fX, dim.fY); }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 628
654 const double current = timer.fWall / loopsPerIter; 629 const double current = timer.fWall / loopsPerIter;
655 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); } 630 if (FLAGS_verbose && current > previous) { SkDebugf("↑"); }
656 if (FLAGS_verbose) { SkDebugf("%.3g ", current); } 631 if (FLAGS_verbose) { SkDebugf("%.3g ", current); }
657 converged = HasConverged(previous, current, timer.fWall); 632 converged = HasConverged(previous, current, timer.fWall);
658 previous = current; 633 previous = current;
659 } while (!kIsDebug && !converged); 634 } while (!kIsDebug && !converged);
660 if (FLAGS_verbose) { SkDebugf("\n"); } 635 if (FLAGS_verbose) { SkDebugf("\n"); }
661 636
662 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != co nfig.backend) { 637 if (FLAGS_outDir.count() && SkBenchmark::kNonRendering_Backend != co nfig.backend) {
663 saveFile(bench->getName(), 638 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
664 config.name, 639 if (image.get()) {
665 FLAGS_outDir[0], 640 saveFile(bench->getName(), config.name, FLAGS_outDir[0],
666 device->accessBitmap(false)); 641 image);
642 }
667 } 643 }
668 644
669 if (kIsDebug) { 645 if (kIsDebug) {
670 // Let's not mislead ourselves by looking at Debug build bench t imes! 646 // Let's not mislead ourselves by looking at Debug build bench t imes!
671 continue; 647 continue;
672 } 648 }
673 649
674 // Normalize to ms per 1000 iterations. 650 // Normalize to ms per 1000 iterations.
675 const double normalize = 1000.0 / loopsPerIter; 651 const double normalize = 1000.0 / loopsPerIter;
676 const struct { char shortName; const char* longName; double ms; } ti mes[] = { 652 const struct { char shortName; const char* longName; double ms; } ti mes[] = {
(...skipping 16 matching lines...) Expand all
693 gContextFactory.destroyContexts(); 669 gContextFactory.destroyContexts();
694 #endif 670 #endif
695 return 0; 671 return 0;
696 } 672 }
697 673
698 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL) 674 #if !defined(SK_BUILD_FOR_IOS) && !defined(SK_BUILD_FOR_NACL)
699 int main(int argc, char * const argv[]) { 675 int main(int argc, char * const argv[]) {
700 return tool_main(argc, (char**) argv); 676 return tool_main(argc, (char**) argv);
701 } 677 }
702 #endif 678 #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