diff --git a/src/gfxstream/guest/android/GfxStreamGralloc.cpp b/src/gfxstream/guest/android/GfxStreamGralloc.cpp new file mode 100644 index 00000000000..d3ace7ebdec --- /dev/null +++ b/src/gfxstream/guest/android/GfxStreamGralloc.cpp @@ -0,0 +1,38 @@ +// Copyright 2024 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. + +#include "gfxstream/guest/GfxStreamGralloc.h" + +#if defined(__ANDROID__) + +#include + +#include "GrallocGoldfish.h" +#include "GrallocMinigbm.h" +#include "android-base/properties.h" + +namespace gfxstream { + +Gralloc* createPlatformGralloc(int32_t descriptor) { + const std::string value = android::base::GetProperty("ro.hardware.gralloc", ""); + if (value == "minigbm") { + auto gralloc = new MinigbmGralloc(descriptor); + return gralloc; + } + return new GoldfishGralloc(); +} + +} // namespace gfxstream + +#endif diff --git a/src/gfxstream/guest/android/GrallocEmulated.h b/src/gfxstream/guest/android/GrallocEmulated.h index f5c0ccfce86..6ea1270bfb6 100644 --- a/src/gfxstream/guest/android/GrallocEmulated.h +++ b/src/gfxstream/guest/android/GrallocEmulated.h @@ -18,7 +18,7 @@ #include #include "VirtGpu.h" -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" namespace gfxstream { diff --git a/src/gfxstream/guest/android/GrallocGoldfish.h b/src/gfxstream/guest/android/GrallocGoldfish.h index 5808e051d2c..6c3210d3a5b 100644 --- a/src/gfxstream/guest/android/GrallocGoldfish.h +++ b/src/gfxstream/guest/android/GrallocGoldfish.h @@ -14,7 +14,7 @@ #pragma once -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" namespace gfxstream { diff --git a/src/gfxstream/guest/android/GrallocMinigbm.h b/src/gfxstream/guest/android/GrallocMinigbm.h index 5e69bdd513b..031fd46880d 100644 --- a/src/gfxstream/guest/android/GrallocMinigbm.h +++ b/src/gfxstream/guest/android/GrallocMinigbm.h @@ -17,7 +17,7 @@ #include #include "VirtGpu.h" -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" namespace gfxstream { diff --git a/src/gfxstream/guest/android/include/gfxstream/guest/ANativeWindow.h b/src/gfxstream/guest/android/include/gfxstream/guest/ANativeWindow.h index 0c59d192dac..3ddfc2f0f07 100644 --- a/src/gfxstream/guest/android/include/gfxstream/guest/ANativeWindow.h +++ b/src/gfxstream/guest/android/include/gfxstream/guest/ANativeWindow.h @@ -19,7 +19,7 @@ #include #include -#include "Gralloc.h" +#include "GfxStreamGralloc.h" using EGLClientBuffer = void*; diff --git a/src/gfxstream/guest/android/include/gfxstream/guest/GfxStreamGralloc.h b/src/gfxstream/guest/android/include/gfxstream/guest/GfxStreamGralloc.h new file mode 100644 index 00000000000..b1d02038b5c --- /dev/null +++ b/src/gfxstream/guest/android/include/gfxstream/guest/GfxStreamGralloc.h @@ -0,0 +1,120 @@ +// Copyright 2023 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 expresso or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#if defined(ANDROID) + +#include +#include +#include + +#include + +typedef struct AHardwareBuffer AHardwareBuffer; + +namespace gfxstream { + +constexpr uint32_t kGlRGB = 0x1907; +constexpr uint32_t kGlRGBA = 0x1908; +constexpr uint32_t kGlRGB565 = 0x8D62; + +// Buffer pixel formats mirrored from Android to avoid extra +// build dependencies on Android libraries. +enum { + GFXSTREAM_AHB_FORMAT_R8G8B8A8_UNORM = 1, + GFXSTREAM_AHB_FORMAT_R8G8B8X8_UNORM = 2, + GFXSTREAM_AHB_FORMAT_R8G8B8_UNORM = 3, + GFXSTREAM_AHB_FORMAT_R5G6B5_UNORM = 4, + GFXSTREAM_AHB_FORMAT_B8G8R8A8_UNORM = 5, + GFXSTREAM_AHB_FORMAT_B5G5R5A1_UNORM = 6, + GFXSTREAM_AHB_FORMAT_B4G4R4A4_UNORM = 7, + GFXSTREAM_AHB_FORMAT_R16G16B16A16_FLOAT = 0x16, + GFXSTREAM_AHB_FORMAT_R10G10B10A2_UNORM = 0x2b, + GFXSTREAM_AHB_FORMAT_BLOB = 0x21, + GFXSTREAM_AHB_FORMAT_D16_UNORM = 0x30, + GFXSTREAM_AHB_FORMAT_D24_UNORM = 0x31, + GFXSTREAM_AHB_FORMAT_D24_UNORM_S8_UINT = 0x32, + GFXSTREAM_AHB_FORMAT_D32_FLOAT = 0x33, + GFXSTREAM_AHB_FORMAT_D32_FLOAT_S8_UINT = 0x34, + GFXSTREAM_AHB_FORMAT_S8_UINT = 0x35, + GFXSTREAM_AHB_FORMAT_Y8Cb8Cr8_420 = 0x23, + GFXSTREAM_AHB_FORMAT_YV12 = 0x32315659, + GFXSTREAM_AHB_FORMAT_IMPLEMENTATION_DEFINED = 0x22, + GFXSTREAM_AHB_FORMAT_R8_UNORM = 0x38, +}; + +enum GrallocType { + GRALLOC_TYPE_GOLDFISH = 1, + GRALLOC_TYPE_MINIGBM = 2, + GRALLOC_TYPE_EMULATED = 3, +}; + +// Abstraction for gralloc handle conversion +class Gralloc { + public: + virtual ~Gralloc() {} + + virtual GrallocType getGrallocType() = 0; + virtual uint32_t createColorBuffer(int width, int height, uint32_t glformat) = 0; + + virtual void acquire(AHardwareBuffer* ahb) = 0; + virtual void release(AHardwareBuffer* ahb) = 0; + + virtual int allocate(uint32_t width, uint32_t height, uint32_t ahbFormat, uint64_t usage, + AHardwareBuffer** outputAhb) = 0; + + virtual int lock(AHardwareBuffer* ahb, uint8_t** ptr) = 0; + struct LockedPlane { + uint8_t* data = nullptr; + uint32_t pixelStrideBytes = 0; + uint32_t rowStrideBytes = 0; + }; + // If AHB is a YUV format, always returns Y, then U, then V. + virtual int lockPlanes(AHardwareBuffer* ahb, std::vector* ahbPlanes) = 0; + virtual int unlock(AHardwareBuffer* ahb) = 0; + + virtual const native_handle_t* getNativeHandle(const AHardwareBuffer* ahb) = 0; + + virtual uint32_t getHostHandle(const native_handle_t* handle) = 0; + virtual uint32_t getHostHandle(const AHardwareBuffer* handle) = 0; + + virtual int getFormat(const native_handle_t* handle) = 0; + virtual int getFormat(const AHardwareBuffer* handle) = 0; + + virtual uint32_t getFormatDrmFourcc(const AHardwareBuffer* /*handle*/) { + // Equal to DRM_FORMAT_INVALID -- see + return 0; + } + virtual uint32_t getFormatDrmFourcc(const native_handle_t* /*handle*/) { + // Equal to DRM_FORMAT_INVALID -- see + return 0; + } + + virtual uint32_t getWidth(const AHardwareBuffer* ahb) = 0; + virtual uint32_t getHeight(const AHardwareBuffer* ahb) = 0; + + virtual size_t getAllocatedSize(const native_handle_t* handle) = 0; + virtual size_t getAllocatedSize(const AHardwareBuffer* handle) = 0; + + virtual int getId(const AHardwareBuffer* ahb, uint64_t* id) = 0; + + virtual bool treatBlobAsImage() { return false; } +}; + +Gralloc* createPlatformGralloc(int32_t descriptor = -1); + +} // namespace gfxstream + +#endif // defined(ANDROID) diff --git a/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.cpp b/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.cpp index 4ac0bdf187e..824d1d213ba 100644 --- a/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.cpp +++ b/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.cpp @@ -29,7 +29,7 @@ #include -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" #include "vk_format_info.h" #include "vk_util.h" #include "util/log.h" diff --git a/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.h b/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.h index 72c51186bc9..d7137ea25c9 100644 --- a/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.h +++ b/src/gfxstream/guest/vulkan_enc/AndroidHardwareBuffer.h @@ -18,7 +18,7 @@ #include -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" #include "HostVisibleMemoryVirtualization.h" // Structure similar to diff --git a/src/gfxstream/guest/vulkan_enc/ResourceTracker.h b/src/gfxstream/guest/vulkan_enc/ResourceTracker.h index 5be0419e1e6..4be8d4f9e5a 100644 --- a/src/gfxstream/guest/vulkan_enc/ResourceTracker.h +++ b/src/gfxstream/guest/vulkan_enc/ResourceTracker.h @@ -80,7 +80,7 @@ typedef uint64_t zx_koid_t; /// Use installed headers or locally defined Android-specific bits #ifdef VK_USE_PLATFORM_ANDROID_KHR #include "AndroidHardwareBuffer.h" -#include "gfxstream/guest/Gralloc.h" +#include "gfxstream/guest/GfxStreamGralloc.h" #include #endif