车载显示屏代理服务

我们提供新的简单框架服务,以允许供应商进程在 HAL 实现中使用 SurfaceFlinger/EGL,而无需链接 libgui。AOSP 提供此服务的默认实现,功能完善。但是,供应商还必须实现 API,以便在其平台上提供此服务。

package android.frameworks.automotive.display@1.0;

import android.hardware.graphics.bufferqueue@2.0::IGraphicBufferProducer;

interface IAutomotiveDisplayProxyService {
    /**
     * Gets an IGraphicBufferProducer instance from the service.
     *
     * @param  id   Target's stable display identifier
     *
     * @return igbp Returns an IGraphicBufferProducer object, that can be
     *              converted to an ANativeWindow object.
     */
    getIGraphicBufferProducer(uint64_t id) generates (IGraphicBufferProducer igbp);

    /**
     * Sets the ANativeWindow, which is associated with the
     * IGraphicBufferProducer, to be visible and to take over the display.
     *
     * @param  id      Target display ID
     *
     * @return success Returns true on success.
     */
    showWindow(uint64_t id) generates (bool success);

    /**
     * Sets the ANativeWindow, which is associated with the
     * IGraphicBufferProducer, to be invisible and to release the control
     * over display.
     *
     * @param  id      Target display ID
     *
     * @return success Returns true on success.
     */
    hideWindow(uint64_t id) generates (bool success);

    /**
     * Returns the stable identifiers of all available displays.
     *
     * @return ids A list of stable display identifiers.
     */
    getDisplayIdList() generates (vec<uint64_t> ids);

    /**
     * Returns the descriptor of the target display.
     *
     * @param  id    Stable ID of a target display.
     * @return cfg   DisplayConfig of the active display.
     * @return state Current state of the active display.
     */
    getDisplayInfo(uint64_t id) generates (HwDisplayConfig cfg, HwDisplayState state);
}

如需使用此服务,请按以下步骤操作:

  1. 获取 IAutomotiveDisplayProxyService
    android::sp<IAutomotiveDisplayProxyService> windowProxyService =
        IAutomotiveDisplayProxyService::getService("default");
    if (windowProxyService == nullptr) {
        LOG(ERROR) << "Cannot use AutomotiveDisplayProxyService. Exiting.";
        return 1;
    }
    
  2. 从服务中检索活跃显示屏的信息,以确定分辨率。
    // We will use the first display in the list as the primary.
    pWindowProxy->getDisplayInfo(displayId, [this](auto dpyConfig, auto dpyState) {
        DisplayConfig *pConfig = (DisplayConfig*)dpyConfig.data();
        mWidth = pConfig->resolution.getWidth();
        mHeight = pConfig->resolution.getHeight();
    
        ui::DisplayState* pState = (ui::DisplayState*)dpyState.data();
        if (pState->orientation != ui::ROTATION_0 &&
            pState->orientation != ui::ROTATION_180) {
            // rotate
            std::swap(mWidth, mHeight);
        }
    
        LOG(DEBUG) << "Display resolution is " << mWidth << " x " << mHeight;
    });
    
  3. IAutomotiveDisplayProxyService 检索硬件 IGraphicBufferProducer(或 HIDL GraphicBufferProducer [HGBP]):
    mGfxBufferProducer = pWindowProxy->getIGraphicBufferProducer(displayId);
    if (mGfxBufferProducer == nullptr) {
        LOG(ERROR) << "Failed to get IGraphicBufferProducer from "
                   << "IAutomotiveDisplayProxyService.";
        return false;
    }
    
  4. 使用 API libbufferqueueconverter 从检索到的 HGBP 获取 SurfaceHolder
    mSurfaceHolder = getSurfaceFromHGBP(mGfxBufferProducer);
    if (mSurfaceHolder == nullptr) {
        LOG(ERROR) << "Failed to get a Surface from HGBP.";
        return false;
    }
    
  5. 使用 API libbufferqueueconverterSurfaceHolder 转换为原生窗口:
    mWindow = getNativeWindow(mSurfaceHolder.get());
    if (mWindow == nullptr) {
        LOG(ERROR) << "Failed to get a native window from Surface.";
        return false;
    }
    
  6. 使用原生窗口创建一个 EGL 窗口 Surface,然后进行渲染:
    // Set up our OpenGL ES context associated with the default display
    mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (mDisplay == EGL_NO_DISPLAY) {
        LOG(ERROR) << "Failed to get egl display";
        return false;
    }
    ...
    
    // Create the EGL render target surface
    mSurface = eglCreateWindowSurface(mDisplay, egl_config, mWindow, nullptr);
    if (mSurface == EGL_NO_SURFACE) {
        LOG(ERROR) << "eglCreateWindowSurface failed.";
        return false;
    }
    ...
    
  7. 调用 IAutomotiveDisplayProxyService::showWindow() 以在屏幕上显示所渲染的视图。此服务具有最高的优先级,因此,始终由当前所有者控制屏幕:
    mAutomotiveDisplayProxyService->showWindow();
    

如需了解更多实现详情,请参阅 $ANDROID_BUILD_TOP/packages/services/Car/evs/sampleDriver/ 中的 service.cppGlWrapper.cpp

EVS HAL 实现需要下面以粗体显示的附加库。

cc_binary {
    name: "android.hardware.automotive.evs@1.1-sample",

    vendor: true,

    srcs: [
        ...
    ],

    shared_libs: [
        ...
        "libbufferqueueconverter",
        "android.hidl.token@1.0-utils",
        "android.frameworks.automotive.display@1.0",
        "android.hardware.graphics.bufferqueue@1.0",
        "android.hardware.graphics.bufferqueue@2.0",
    ],

多显示屏支持

显示设备枚举和检索显示信息

与相机设备枚举一样,EVS 框架提供了一种枚举可用显示屏的方法。静态显示屏标识符编码长型标识符,其中低位字节为屏幕端口信息,高位为 Extended Display IDentification DataIAutomotiveDisplayProxyService::getDisplayIdList() 返回可用于 EVS 服务的实体本地显示屏的显示屏 ID 列表,IEvsEnumerator::getDisplayIdList() 则返回与检测到的显示屏连接的显示屏端口列表。列表中的第一个 ID 始终为主显示屏。

interface IEvsEnumerator extends @1.0::IEvsEnumerator {
    ...
    /**
     * Returns a list of all EVS displays available to the system
     *
     * @return displayIds Identifiers of available displays.
     */
    getDisplayIdList() generates (vec<uint8_t> displayIds);
};

打开目标显示设备

EVS 应用使用目标显示屏端口号调用 IEvsEnumerator::openDisplay_1_1():

android::sp<IEvsDisplay> pDisplay = pEvs->openDisplay_1_1(displayId);
if (pDisplay.get() == nullptr) {
    LOG(ERROR) << "EVS Display unavailable. Exiting.";
    return 1;
}

注意:一次只能使用一个显示屏,也就是说,当另一个 EVS 客户端请求打开显示屏时,当前的 EVS 客户端将停止显示,即使它们并不是同一个显示屏也会如此。