1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define ATRACE_TAG ATRACE_TAG_ALWAYS
#include <gui/GraphicBufferAlloc.h>
#include <gui/Surface.h>
#include <gui/SurfaceControl.h>
#include <gui/GLConsumer.h>
#include <gui/Surface.h>
#include <ui/Fence.h>
#include <utils/Trace.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <math.h>
#include <getopt.h>
#include "Flatland.h"
#include "GLHelper.h"
using namespace ::android;
static uint32_t g_SleepBetweenSamplesMs = 0;
static bool g_PresentToWindow = false;
static size_t g_BenchmarkNameLen = 0;
struct BenchmarkDesc {
// The name of the test.
const char* name;
// The dimensions of the space in which window layers are specified.
uint32_t width;
uint32_t height;
// The screen heights at which to run the test.
uint32_t runHeights[MAX_TEST_RUNS];
// The list of window layers.
LayerDesc layers[MAX_NUM_LAYERS];
};
static const BenchmarkDesc benchmarks[] = {
{ "16:10 Single Static Window",
2560, 1600, { 800, 1200, 1600, 2400 },
{
{ // Window
0, staticGradient, opaque,
0, 50, 2560, 1454,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2560, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1504, 2560, 96,
},
},
},
{ "3:2 Single Static Window",
2048, 1536, { 1536 },
{
{ // Window
0, staticGradient, opaque,
0, 50, 2048, 1440,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2048, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1440, 2048, 96,
},
},
},
{ "16:10 App -> Home Transition",
2560, 1600, { 800, 1200, 1600, 2400 },
{
{ // Wallpaper
0, staticGradient, opaque,
0, 50, 2560, 1454,
},
{ // Launcher
0, staticGradient, blend,
0, 50, 2560, 1454,
},
{ // Outgoing activity
0, staticGradient, blendShrink,
20, 70, 2520, 1414,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2560, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1504, 2560, 96,
},
},
},
{ "3:2 App -> Home Transition",
2048, 1536, { 1536 },
{
{ // Wallpaper
0, staticGradient, opaque,
0, 50, 2048, 1440,
},
{ // Launcher
0, staticGradient, blend,
0, 50, 2048, 1440,
},
{ // Outgoing activity
0, staticGradient, blendShrink,
20, 70, 2048, 1400,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2048, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1440, 2048, 96,
},
},
},
{ "16:10 SurfaceView -> Home Transition",
2560, 1600, { 800, 1200, 1600, 2400 },
{
{ // Wallpaper
0, staticGradient, opaque,
0, 50, 2560, 1454,
},
{ // Launcher
0, staticGradient, blend,
0, 50, 2560, 1454,
},
{ // Outgoing SurfaceView
0, staticGradient, blendShrink,
20, 70, 2520, 1414,
},
{ // Outgoing activity
0, staticGradient, blendShrink,
20, 70, 2520, 1414,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2560, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1504, 2560, 96,
},
},
},
{ "3:2 SurfaceView -> Home Transition",
2048, 1536, { 1536 },
{
{ // Wallpaper
0, staticGradient, opaque,
0, 50, 2048, 1440,
},
{ // Launcher
0, staticGradient, blend,
0, 50, 2048, 1440,
},
{ // Outgoing SurfaceView
0, staticGradient, blendShrink,
20, 70, 2048, 1400,
},
{ // Outgoing activity
0, staticGradient, blendShrink,
20, 70, 2048, 1400,
},
{ // Status bar
0, staticGradient, opaque,
0, 0, 2048, 50,
},
{ // Navigation bar
0, staticGradient, opaque,
0, 1440, 2048, 96,
},
},
},
};
static const ShaderDesc shaders[] = {
{
name: "Blit",
vertexShader: {
"precision mediump float;",
"",
"attribute vec4 position;",
"attribute vec4 uv;",
"",
"varying vec4 texCoords;",
"",
"uniform mat4 objToNdc;",
"uniform mat4 uvToTex;",
"",
"void main() {",
" gl_Position = objToNdc * position;",
" texCoords = uvToTex * uv;",
"}",
},
fragmentShader: {
"#extension GL_OES_EGL_image_external : require",
"precision mediump float;",
"",
"varying vec4 texCoords;",
"",
"uniform samplerExternalOES blitSrc;",
"uniform vec4 modColor;",
"",
"void main() {",
" gl_FragColor = texture2D(blitSrc, texCoords.xy);",
" gl_FragColor *= modColor;",
"}",
},
},
{
name: "Gradient",
vertexShader: {
"precision mediump float;",
"",
"attribute vec4 position;",
"attribute vec4 uv;",
"",
"varying float interp;",
"",
"uniform mat4 objToNdc;",
"uniform mat4 uvToInterp;",
"",
"void main() {",
" gl_Position = objToNdc * position;",
" interp = (uvToInterp * uv).x;",
"}",
},
fragmentShader: {
"precision mediump float;",
"",
"varying float interp;",
"",
"uniform vec4 color0;",
"uniform vec4 color1;",
"",
"uniform sampler2D ditherKernel;",
"uniform float invDitherKernelSize;",
"uniform float invDitherKernelSizeSq;",
"",
"void main() {",
" float dither = texture2D(ditherKernel,",
" gl_FragCoord.xy * invDitherKernelSize).a;",
" dither *= invDitherKernelSizeSq;",
" vec4 color = mix(color0, color1, clamp(interp, 0.0, 1.0));",
" gl_FragColor = color + vec4(dither, dither, dither, 0.0);",
"}",
},
},
};
class Layer {
public:
Layer() :
mFirstFrame(true),
mGLHelper(NULL),
mSurface(EGL_NO_SURFACE) {
}
bool setUp(const LayerDesc& desc, GLHelper* helper) {
bool result;
mDesc = desc;
mGLHelper = helper;
result = mGLHelper->createSurfaceTexture(mDesc.width, mDesc.height,
&mGLConsumer, &mSurface, &mTexName);
if (!result) {
return false;
}
mRenderer = desc.rendererFactory();
result = mRenderer->setUp(helper);
if (!result) {
return false;
}
mComposer = desc.composerFactory();
result = mComposer->setUp(desc, helper);
if (!result) {
return false;
}
return true;
}
void tearDown() {
if (mComposer != NULL) {
mComposer->tearDown();
delete mComposer;
mComposer = NULL;
}
if (mRenderer != NULL) {
mRenderer->tearDown();
delete mRenderer;
mRenderer = NULL;
}
if (mSurface != EGL_NO_SURFACE) {
mGLHelper->destroySurface(&mSurface);
mGLConsumer->abandon();
}
mGLHelper = NULL;
mGLConsumer.clear();
}
bool render() {
return mRenderer->render(mSurface);
}
bool prepareComposition() {
status_t err;
err = mGLConsumer->updateTexImage();
if (err < 0) {
fprintf(stderr, "GLConsumer::updateTexImage error: %d\n", err);
return false;
}
return true;
}
bool compose() {
return mComposer->compose(mTexName, mGLConsumer);
}
private:
bool mFirstFrame;
LayerDesc mDesc;
GLHelper* mGLHelper;
GLuint mTexName;
sp<GLConsumer> mGLConsumer;
EGLSurface mSurface;
Renderer* mRenderer;
Composer* mComposer;
};
class BenchmarkRunner {
public:
BenchmarkRunner(const BenchmarkDesc& desc, size_t instance) :
mDesc(desc),
mInstance(instance),
mNumLayers(countLayers(desc)),
mGLHelper(NULL),
mSurface(EGL_NO_SURFACE),
mWindowSurface(EGL_NO_SURFACE) {
}
bool setUp() {
ATRACE_CALL();
bool result;
EGLint resulte;
float scaleFactor = float(mDesc.runHeights[mInstance]) /
float(mDesc.height);
uint32_t w = uint32_t(scaleFactor * float(mDesc.width));
uint32_t h = mDesc.runHeights[mInstance];
mGLHelper = new GLHelper();
result = mGLHelper->setUp(shaders, NELEMS(shaders));
if (!result) {
return false;
}
GLuint texName;
result = mGLHelper->createSurfaceTexture(w, h, &mGLConsumer, &mSurface,
&texName);
if (!result) {
return false;
}
for (size_t i = 0; i < mNumLayers; i++) {
// Scale the layer to match the current screen size.
LayerDesc ld = mDesc.layers[i];
ld.x = int32_t(scaleFactor * float(ld.x));
ld.y = int32_t(scaleFactor * float(ld.y));
ld.width = uint32_t(scaleFactor * float(ld.width));
ld.height = uint32_t(scaleFactor * float(ld.height));
// Set up the layer.
result = mLayers[i].setUp(ld, mGLHelper);
if (!result) {
return false;
}
}
if (g_PresentToWindow) {
result = mGLHelper->createWindowSurface(w, h, &mSurfaceControl,
&mWindowSurface);
if (!result) {
return false;
}
result = doFrame(mWindowSurface);
if (!result) {
return false;
}
}
return true;
}
void tearDown() {
ATRACE_CALL();
for (size_t i = 0; i < mNumLayers; i++) {
mLayers[i].tearDown();
}
if (mGLHelper != NULL) {
if (mWindowSurface != EGL_NO_SURFACE) {
mGLHelper->destroySurface(&mWindowSurface);
}
mGLHelper->destroySurface(&mSurface);
mGLConsumer->abandon();
mGLConsumer.clear();
mSurfaceControl.clear();
mGLHelper->tearDown();
delete mGLHelper;
mGLHelper = NULL;
}
}
nsecs_t run(uint32_t warmUpFrames, uint32_t totalFrames) {
ATRACE_CALL();
bool result;
status_t err;
resetColorGenerator();
// Do the warm-up frames.
for (uint32_t i = 0; i < warmUpFrames; i++) {
result = doFrame(mSurface);
if (!result) {
return -1;
}
}
// Grab the fence for the start timestamp.
sp<Fence> startFence = mGLConsumer->getCurrentFence();
// the timed frames.
for (uint32_t i = warmUpFrames; i < totalFrames; i++) {
result = doFrame(mSurface);
if (!result) {
return -1;
}
}
// Grab the fence for the end timestamp.
sp<Fence> endFence = mGLConsumer->getCurrentFence();
// Keep doing frames until the end fence has signaled.
while (endFence->wait(0) == -ETIME) {
result = doFrame(mSurface);
if (!result) {
return -1;
}
}
// Compute the time delta.
nsecs_t startTime = startFence->getSignalTime();
nsecs_t endTime = endFence->getSignalTime();
return endTime - startTime;
}
private:
bool doFrame(EGLSurface surface) {
bool result;
status_t err;
for (size_t i = 0; i < mNumLayers; i++) {
result = mLayers[i].render();
if (!result) {
return false;
}
}
for (size_t i = 0; i < mNumLayers; i++) {
result = mLayers[i].prepareComposition();
if (!result) {
return false;
}
}
result = mGLHelper->makeCurrent(surface);
if (!result) {
return false;
}
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (size_t i = 0; i < mNumLayers; i++) {
result = mLayers[i].compose();
if (!result) {
return false;
}
}
result = mGLHelper->swapBuffers(surface);
if (!result) {
return false;
}
err = mGLConsumer->updateTexImage();
if (err < 0) {
fprintf(stderr, "GLConsumer::updateTexImage error: %d\n", err);
return false;
}
return true;
}
static size_t countLayers(const BenchmarkDesc& desc) {
size_t i;
for (i = 0; i < MAX_NUM_LAYERS; i++) {
if (desc.layers[i].rendererFactory == NULL) {
break;
}
}
return i;
}
const BenchmarkDesc& mDesc;
const size_t mInstance;
const size_t mNumLayers;
GLHelper* mGLHelper;
// The surface into which layers are composited
sp<GLConsumer> mGLConsumer;
EGLSurface mSurface;
// Used for displaying the surface to a window.
EGLSurface mWindowSurface;
sp<SurfaceControl> mSurfaceControl;
Layer mLayers[MAX_NUM_LAYERS];
};
static int cmpDouble(const double* lhs, const double* rhs) {
if (*lhs < *rhs) {
return -1;
} else if (*rhs < *lhs) {
return 1;
}
return 0;
}
// Run a single benchmark and print the result.
static bool runTest(const BenchmarkDesc b, size_t run) {
bool success = true;
double prevResult = 0.0, result = 0.0;
Vector<double> samples;
uint32_t runHeight = b.runHeights[run];
uint32_t runWidth = b.width * runHeight / b.height;
printf(" %-*s | %4d x %4d | ", g_BenchmarkNameLen, b.name,
runWidth, runHeight);
fflush(stdout);
BenchmarkRunner r(b, run);
if (!r.setUp()) {
fprintf(stderr, "error initializing runner.\n");
return false;
}
// The slowest 1/outlierFraction sample results are ignored as potential
// outliers.
const uint32_t outlierFraction = 16;
const double threshold = .0025;
uint32_t warmUpFrames = 1;
uint32_t totalFrames = 5;
// Find the number of frames needed to run for over 100ms.
double runTime = 0.0;
while (true) {
runTime = double(r.run(warmUpFrames, totalFrames));
if (runTime < 50e6) {
warmUpFrames *= 2;
totalFrames *= 2;
} else {
break;
}
}
if (totalFrames - warmUpFrames > 16) {
// The test runs too fast to get a stable result. Skip it.
printf(" fast");
goto done;
} else if (totalFrames == 5 && runTime > 200e6) {
// The test runs too slow to be very useful. Skip it.
printf(" slow");
goto done;
}
do {
size_t newSamples = samples.size();
if (newSamples == 0) {
newSamples = 4*outlierFraction;
}
if (newSamples > 512) {
printf("varies");
goto done;
}
for (size_t i = 0; i < newSamples; i++) {
double sample = double(r.run(warmUpFrames, totalFrames));
if (g_SleepBetweenSamplesMs > 0) {
usleep(g_SleepBetweenSamplesMs * 1000);
}
if (sample < 0.0) {
success = false;
goto done;
}
samples.add(sample);
}
samples.sort(cmpDouble);
prevResult = result;
size_t elem = (samples.size() * (outlierFraction-1) / outlierFraction);
result = (samples[elem-1] + samples[elem]) * 0.5;
} while (fabs(result - prevResult) > threshold * result);
printf("%6.3f", result / double(totalFrames - warmUpFrames) / 1e6);
done:
printf("\n");
fflush(stdout);
r.tearDown();
return success;
}
static void printResultsTableHeader() {
const char* scenario = "Scenario";
size_t len = strlen(scenario);
size_t leftPad = (g_BenchmarkNameLen - len) / 2;
size_t rightPad = g_BenchmarkNameLen - len - leftPad;
printf(" %*s%s%*s | Resolution | Time (ms)\n", leftPad, "",
"Scenario", rightPad, "");
}
// Run ALL the benchmarks!
static bool runTests() {
printResultsTableHeader();
for (size_t i = 0; i < NELEMS(benchmarks); i++) {
const BenchmarkDesc& b = benchmarks[i];
for (size_t j = 0; j < MAX_TEST_RUNS && b.runHeights[j]; j++) {
if (!runTest(b, j)) {
return false;
}
}
}
return true;
}
// Return the length longest benchmark name.
static size_t maxBenchmarkNameLen() {
size_t maxLen = 0;
for (size_t i = 0; i < NELEMS(benchmarks); i++) {
const BenchmarkDesc& b = benchmarks[i];
size_t len = strlen(b.name);
if (len > maxLen) {
maxLen = len;
}
}
return maxLen;
}
// Print the command usage help to stderr.
static void showHelp(const char *cmd) {
fprintf(stderr, "usage: %s [options]\n", cmd);
fprintf(stderr, "options include:\n"
" -s N sleep for N ms between samples\n"
" -d display the test frame to a window\n"
" --help print this helpful message and exit\n"
);
}
int main(int argc, char** argv) {
if (argc == 2 && 0 == strcmp(argv[1], "--help")) {
showHelp(argv[0]);
exit(0);
}
for (;;) {
int ret;
int option_index = 0;
static struct option long_options[] = {
{"help", no_argument, 0, 0 },
{ 0, 0, 0, 0 }
};
ret = getopt_long(argc, argv, "ds:",
long_options, &option_index);
if (ret < 0) {
break;
}
switch(ret) {
case 'd':
g_PresentToWindow = true;
break;
case 's':
g_SleepBetweenSamplesMs = atoi(optarg);
break;
case 0:
if (strcmp(long_options[option_index].name, "help")) {
showHelp(argv[0]);
exit(0);
}
break;
default:
showHelp(argv[0]);
exit(2);
}
}
g_BenchmarkNameLen = maxBenchmarkNameLen();
printf(" cmdline:");
for (int i = 0; i < argc; i++) {
printf(" %s", argv[i]);
}
printf("\n");
if (!runTests()) {
fprintf(stderr, "exiting due to error.\n");
return 1;
}
}
|