123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792 |
- /*
- * 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,
- },
- },
- },
- { "4:3 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,
- },
- },
- },
- { "4:3 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,
- },
- },
- },
- { "4:3 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 | ", static_cast<int>(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",
- static_cast<int>(leftPad), "",
- "Scenario", static_cast<int>(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;
- }
- }
|