2

Environment

  • Windows (x64) Host
  • Android 5.0 USB connected to the Windows machine
  • Samsung Galaxy 5
  • VisualGDB
  • OpenGL (v1.4)
  • ADB CmdLine tool => Native C++ (No Java)

Use-case

Implement an ADB commandline tool that extract the main display resolution ( this is part of the tools functionality )

The following code print gibberish data, How can I get the main display resolution using Native C++ code running as an ADB command line tool ?

Code snippet

HRESULT GetMainDisplayResolution(OUT SIZE& sz) {
    EGLConfig pConfigs[MAX_CONFIGS] = { 0 };
    EGLint iErr = 0;
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (0 == display)
        return E_FAIL;
    int maj, min;
    if (FALSE == eglInitialize(display, &maj, &min)) {
        iErr = eglGetError();
        return E_FAIL;
    }

    EGLint iCfgCount = 0;
    if (FALSE == eglGetConfigs(display, pConfigs, MAX_CONFIGS, &iCfgCount)) {
        iErr = eglGetError();// EGL_NOT_INITIALIZED
        return E_FAIL;
    }

    for (int i = 0; i < iCfgCount; i++) {
        eglGetConfigAttrib(display, pConfigs[i], EGL_HORIZONTAL_RESOLUTION, (EGLint*)&sz.cx);
        eglGetConfigAttrib(display, pConfigs[i], EGL_VERTICAL_RESOLUTION, (EGLint*)&sz.cy);
        printf("Cfg %i, x:%i, y:%i", i, sz.cx, sz.cy);
    }

    return S_OK;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
NadavRub
  • 2,520
  • 27
  • 63
  • 1
    Not sure if it's an exact duplicate, but see http://stackoverflow.com/questions/12131429/android-get-screen-size-via-c. – Reto Koradi Dec 15 '14 at 06:09

1 Answers1

1

EGL doesn't have access to the display itself. It gets a window from the Window Manager, which is then composited by SurfaceFlinger with other things. The architecture is described here.

If you don't mind using non-public APIs, and you're running as "shell" or "root" (which, as an ADB command, you will), you can do what some of the test code does and query the display properties from SurfaceFlinger directly. Take a look at WindowSurface. (You can find the classic "San Angeles" demo nearby if you want to see the code in action.) WindowSurface was added in Android 5.0 "Lollipop", replacing an older implementation that tried to bypass SurfaceFlinger (which required shutting down the Android framework). The new implementation just draws on top of whatever is running.

The screenrecord tool uses the same SurfaceComposerClient APIs to determine how to size the video (see line 540). If all you want is the resolution and don't actually care about EGL, this is a simpler example.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • While I am using a ~patched~ screenrecorder version w/ my solution, to minimize maintenance work while OS is updated, I would like to avoid un-documented APIs as much as possible, I wonder, might it be possible to implement an ADB tool using Java and then get the eg. resolution using conventional APIs ? – NadavRub Dec 14 '14 at 19:36
  • The public API equivalent is the `android.view.Display` class. You can see it used to get the main display's refresh rate in Grafika (https://github.com/google/grafika/blob/master/src/com/android/grafika/MiscUtils.java). The tricky bit is the "launched from ADB" part. You'd need to link against the framework libs and probably have to deal with some of the setup that app_process and zygote normally handle. I haven't tried to do that myself. – fadden Dec 14 '14 at 23:23