汽車顯示代理服務

這個簡單的框架服務允許供應商進程在 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 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 視窗表面,然後渲染:
    // 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 Data位於高位元中。 IAutomotiveDisplayProxyService::getDisplayIdList()傳回可用於 EVS 服務的實體本機顯示器的顯示器 ID 列表, IEvsEnumerator::getDisplayIdList()傳回偵測到的顯示器連接到的顯示連接埠清單。清單中的第一個 ID 始終是主顯示器的 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 用戶端將會遺失其顯示器,即使它們不相同。