在 Android 中新增像素格式

新增至 Android 的所有新像素格式,都必須包含在 Android 介面定義語言 (AIDL)Android 硬體緩衝區 (AHB) 中。AIDL 和 AHB 設有嚴格的穩定性和標準化要求,因此在擴充功能時,必須謹慎進行。所有新的像素格式都必須納入 AOSP,且所有更新都必須由 AIDL 和 AHB 專家個別確認。這項程序相當謹慎 確認網頁上任何新像素格式時 平台。

本頁概述必要的 Android 開放原始碼計畫 程式碼變更,以及在 Android 開放原始碼計畫中新增像素格式所需的程序。

新增像素格式之前,請先按照 提交修補程式

在 AIDL 中新增像素格式

新增支援的新像素格式時,必須同時變更 位於 AIDL 中的 PixelFormat.aidl 檔案。詳情請見 hardware/interfaces/graphics/common/aidl/ 您對於 AIDL 原始碼的熟悉度

如要在 AIDL 新增正式像素,請按照下列步驟操作:

  1. 請按照現有的程式碼慣例,將新的像素格式附加為 PixelFormat.aidlPixelFormat 列舉的結尾新項目,並將項目的 16 進位值設為比前一個項目多一個。將程式碼變更與先前的項目進行比對。請參閱以下範例,瞭解 RGBA_8888 像素格式項目:
    /**
     * 32-bit format that has 8-bit R, G, B, and A components, in that order,
     * from the lowest memory address to the highest memory address.
     *
     * The component values are unsigned normalized to the range [0, 1], whose
     * interpretation is defined by the dataspace.
     */
    RGBA_8888 = 0x1,
    

    變更以下項目後建構程式碼時,系統會顯示以下錯誤訊息 PixelFormat.aidl:

    android_developer:~/android/aosp-android-latest-release: m
    ...
    ###############################################################################
    # ERROR: AIDL API change detected                                             #
    ###############################################################################
    Above AIDL file(s) has changed. Run `m android.hardware.graphics.common-update-api` to reflect the changes
    to the current version so that it is reviewed by
    android-aidl-api-council@google.com
    And then you need to change dependency on android.hardware.graphics.common-V(n)-* to android.hardware.graphics.common-V(n+1)-* to use
    new APIs.
    
  2. 如要清除這項錯誤,請按照錯誤訊息中指定的方式執行下列指令,變更 aidl_api 目錄中的 PixelFormat.aidl

    m android.hardware.graphics.common-update-api
    

    執行上述指令可更新正確的檔案,以便正常建構。

為 AHB 新增像素格式

如要新增對新像素格式的支援,必須變更 hardware_buffer.hAHardwareBuffer.cpp。查看「frameworks/native/libs/nativewindow」 ,為 AHB 原始碼。

如要在 AHB 中新增正式像素,請按照下列步驟操作:

  1. hardware_buffer.h 中,將新的像素格式附加為 AHardwareBuffer_Format 列舉的結尾處的新項目。遵循現有的程式碼慣例。

    RGBA_8888 像素格式為例,請新增下列新的像素格式項目:

    /**
     * Corresponding formats:
     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
     *   OpenGL ES: GL_RGBA8
     */
    AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1,
    

    請注意,新的像素格式會在 AHB 中指定名稱,開頭必須是 AHARDWAREBUFFER_FORMAT_,後面接著頻道縮寫和位元深度,結尾則是編碼。這個列舉項目必須具有與 PixelFormat.aidl 相同的十六進位值。

    像素格式應包含一或兩種相關聯的 VulkanOpenGL ES 格式。在適當情況下指定相關格式。如果沒有相關聯的格式,請指定 N/A

  2. 如果像素格式具有相關的 OpenGL ES 格式,請將該格式新增至 CTS 的選用測試。如要這麼做,請使用 FORMAT_CASE(...)GL_FORMAT_CASE(...) 為新格式,將新的 GL 格式新增至 AHBFormatAsString(int32_t format) 中的 AHardwareBufferGLTest.cpp,如下所示:

    const char* AHBFormatAsString(int32_t format) {
      switch (format) {
          ...
          FORMAT_CASE(R8G8B8A8_UNORM);
          ...
          GL_FORMAT_CASE(GL_RGB8);
      }
      return "";
    }
    
  3. 接下來,請將新測試新增至 AHardwareBufferGLTest.cpp,如下所示:

    class RGBA8Test : public AHardwareBufferGLTest {};
    
    // Verify that if we can allocate an RGBA8 AHB we can render to it.
    TEST_P(RGBA8Test, Write) {
        AHardwareBuffer_Desc desc = GetParam();
        desc.usage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
        if (!SetUpBuffer(desc)) {
            return;
        }
    
        ASSERT_NO_FATAL_FAILURE(SetUpFramebuffer(desc.width, desc.height, 0, kBufferAsRenderbuffer));
        ASSERT_NO_FATAL_FAILURE(
            SetUpProgram(kVertexShader, kColorFragmentShader, kPyramidPositions, 0.5f));
    
        glDrawArrays(GL_TRIANGLES, 0, kPyramidVertexCount);
        ASSERT_EQ(GLenum{GL_NO_ERROR}, glGetError());
    }
    
    INSTANTIATE_TEST_CASE_P(
        SingleLayer, RGBA8Test,
        ::testing::Values(
            AHardwareBuffer_Desc{57, 33, 1, AHARDWAREBUFFER_FORMAT_R16G16_UINT, 0, 0, 0, 0}),
        &GetTestName);
    

    請至少指定一組 AHardwareBuffer_Desc 值。視需要新增更多值。

  4. AHardwareBuffer.cpp 中找出其中的靜態斷言結尾:

    // ----------------------------------------------------------------------------
    // Validate hardware_buffer.h and PixelFormat.aidl agree
    // ----------------------------------------------------------------------------
    

    使用static_assert PixelFormat:: 列舉,而非 HAL_PIXEL_FORMAT 常數。 從另一個 RGBA_8888像素格式 在 AIDL 中新增像素格式、 新增像素格式項目,如下所示:

    static_assert(static_cast(aidl::android::hardware::graphics::common::PixelFormat::RGBA_8888) ==
      AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
    "HAL and AHardwareBuffer pixel format don't match");
    
  5. AHardwareBufferTest.cppPrintAhbFormat() 結尾處附加新像素格式,即可將新像素格式新增至適當的測試。請遵循現有的程式碼慣例,如下所示:

    void PrintAhbFormat(std::ostream& os, uint64_t format) {
        switch (format) {
            ...
            FORMAT_CASE(R8G8B8A8_UNORM);
            default: os << "unknown"; break;
        }
    }
    
  6. HardwareBuffer.java 中將新的像素格式新增至 HardwareBuffer SDK:將新的項目附加至 @IntDef。舉例來說, RGBA_8888 格式如下所示:

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(prefix = { "RGB", "BLOB", "YCBCR_", "D_", "DS_", "S_" }, value = {
      ...
      RGBA_8888,
    })
    

    如果元件值未經過正負號正規化,請明確指出該值 的值。舉例來說,如果是無號整數 16 位元紅色通道專用格式,變數名稱必須是 R_16UI;如果是同樣格式,但加上無號整數 16 位元綠色通道格式,則變數名稱必須是 RG_16UI16UI

  7. HardwareBuffer.java 中,將新的像素格式新增為 static int,方法是在 @Format 結尾附加新的公開成員變數:

    @Format
    ...
    /** Format: 8 bits each red, green, blue, alpha */
    public static final int RGBA_8888 = 0x1;
    

    此列舉項目的十六進位值必須與 PixelFormat.aidl 相同 和 hardware_buffer.h。依循現有慣例。

  8. 嘗試使用這些程式碼變更進行建構時,會產生建構錯誤:

    android_developer:~/android/aosp-android-latest-release: m
    ...
    ******************************
    You have tried to change the API from what has been previously approved.
    
    To make these errors go away, you have two choices:
       1. You can add '@hide' javadoc comments (and remove @SystemApi/@TestApi/etc)
          to the new methods, etc. shown in the above diff.
    
       2. You can update current.txt and/or removed.txt by executing the following command:
             m api-stubs-docs-non-updatable-update-current-api
    
          To submit the revised current.txt to the main Android repository,
          you will need approval.
    ******************************
    ...
    

    如要清除這個錯誤,請按照錯誤訊息中的指示執行下列指令, 要變更 current.txt:

    m api-stubs-docs-non-updatable-update-current-api
    

    執行上述指令可更新正確的檔案,以便正常建構。

  9. 將新的像素格式附加至 HardwareBufferTest.javaparamsForTestCreateOptionalFormats(), 如下所示:

    private static Object[] paramsForTestCreateOptionalFormats() {
      return new Integer[]{
          HardwareBuffer.RGBA_8888
      };
    

在 Windows 系統整合中新增像素格式

如要將新像素格式用於圖形 API 中的框架緩衝區格式,請將該格式新增至相關圖形 API 的適當視窗系統整合 (WSI)。對於使用 Vulkan API 的應用程式或系統程序,請更新 Vulkan Swapchain。 若是使用 OpenGL ES API 的應用程式或系統程序,請更新 EGL API。

針對新像素格式進行的 Vulkan WSI 變更

更新 Vulkan WSI,如下所示:
  1. swapchain.cpp 中,將新情況新增至 GetNativePixelFormat(VkFormat format) 函式:

    android::PixelFormat GetNativePixelFormat(VkFormat format) {
      ...
      switch (format) {
          ...
          case VK_FORMAT_R8G8B8A8_UNORM:
              native_format = PixelFormat::RGBA_8888;
              break;
          ...
          default:
              ALOGV("unsupported swapchain format %d", format);
              break;
      }
      return native_format;
    }
    
  2. 如果像素格式需要 Vulkan 擴充功能才能運作,請查詢 Vulkan 擴充功能。針對例項側邊擴充功能,請使用 instance_data,如下所示:
    bool colorspace_ext = instance_data.hook_extensions.test(ProcHook::EXT_swapchain_colorspace);
    

    如要使用裝置端擴充功能,請使用下列方式:

    bool rgba10x6_formats_ext = false;
    uint32_t exts_count;
    const auto& driver = GetData(pdev).driver;
    driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
                                              nullptr);
    std::vector props(exts_count);
    driver.EnumerateDeviceExtensionProperties(pdev, nullptr, &exts_count,
                                              props.data());
    for (uint32_t i = 0; i < exts_count; i++) {
        VkExtensionProperties prop = props[i];
        if (strcmp(prop.extensionName,
                   VK_EXT_RGBA10X6_FORMATS_EXTENSION_NAME) == 0) {
            rgba10x6_formats_ext = true;
        }
    }
    

    Google 會處理公開執行個體或裝置擴充功能所需的基礎架構 至 swapchain.cpp。初始變更清單不需要從 Vulkan 載入器正確設定擴充功能。

  3. 接著,請列舉格式和色彩空間組合:
    desc.format = AHARDWAREBUFFER_FORMAT_R10G10B10A10_UNORM;
    if (AHardwareBuffer_isSupported(&desc) && rgba10x6_formats_ext) {
      all_formats.emplace_back(
          VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
                             VK_COLOR_SPACE_SRGB_NONLINEAR_KHR});
      if (colorspace_ext) {
        all_formats.emplace_back(
            VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
                               VK_COLOR_SPACE_PASS_THROUGH_EXT});
        all_formats.emplace_back(
            VkSurfaceFormatKHR{VK_FORMAT_R10X6G10X6B10X6A10X6_UNORM_4PACK16,
                               VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT});
    }
    

    您必須瞭解相容的格式和色彩空間組合。

  4. 將新格式新增至位於 external/deqpdEQP-VK
  5. 更新以下程式碼的 Vulkan 合規測試: vktApiExternalMemoryTests.cppvktExternalMemoryUtil.cpp 可從現有來源中推測出需要變更的內容,或是與 Android 支援團隊聯絡 。

EGL 對新像素格式所做的變更

請依照下列方式更新 EGL:

  1. getNativePixelFormat() 函式,修改 if-else 樹狀結構,傳回新像素格式的 AIDL 列舉。 以下是 RGBA_8888 像素格式的範例:
    if (a == 0) {
      ...
    } else {
      if (componentType == EGL_COLOR_COMPONENT_TYPE_FIXED_EXT) {
          if (colorDepth > 24) {
              ...
          } else {
              *format = PixelFormat::RGBA_8888;
          }
      } else {
        ...
      }
    }
    
  2. 如要將新格式新增至 dEQP,請在 androidFormats 列舉中新增項目,如下所示:
    static const GLenum androidFormats[] =
    {
      ...
      GL_RGBA8,
      ...
    };
    

提交更新內容

請按照「提供者」一節的說明,建立變更清單並與適當團隊分享。