[HWUI] remove libui from HWUI's dependencies

Bug: 136263238
Test: builds
Test: interact with device
Change-Id: I18540f78a4ee5ffcc30c96ff7862480e1d8fe50a
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 81dedda..0f6972f 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -53,8 +53,6 @@
         host: {
             include_dirs: [
                 "external/vulkan-headers/include",
-                "frameworks/native/libs/math/include",
-                "frameworks/native/libs/ui/include",
             ],
             cflags: [
                 "-Wno-unused-variable",
@@ -71,6 +69,10 @@
         "libminikin",
     ],
 
+    static_libs: [
+        "libui-types",
+    ],
+
     target: {
         android: {
             shared_libs: [
@@ -83,7 +85,6 @@
                 "libGLESv2",
                 "libGLESv3",
                 "libvulkan",
-                "libui",
                 "libnativedisplay",
                 "libnativewindow",
                 "libprotobuf-cpp-lite",
@@ -236,6 +237,7 @@
                 "service/GraphicsStatsService.cpp",
                 "thread/CommonPool.cpp",
                 "utils/GLUtils.cpp",
+                "utils/NdkUtils.cpp",
                 "utils/StringUtils.cpp",
                 "AutoBackendTextureRelease.cpp",
                 "DeferredLayerUpdater.cpp",
@@ -292,6 +294,7 @@
         android: {
             shared_libs: [
 	        "libgui",
+	        "libui",
 	    ],
 	}
     },
diff --git a/libs/hwui/CanvasTransform.cpp b/libs/hwui/CanvasTransform.cpp
index 8c37d73..9d03ce5 100644
--- a/libs/hwui/CanvasTransform.cpp
+++ b/libs/hwui/CanvasTransform.cpp
@@ -22,7 +22,6 @@
 #include <SkGradientShader.h>
 #include <SkPaint.h>
 #include <SkShader.h>
-#include <ui/ColorSpace.h>
 
 #include <algorithm>
 #include <cmath>
diff --git a/libs/hwui/HardwareBitmapUploader.cpp b/libs/hwui/HardwareBitmapUploader.cpp
index a3d552f..be8f852 100644
--- a/libs/hwui/HardwareBitmapUploader.cpp
+++ b/libs/hwui/HardwareBitmapUploader.cpp
@@ -16,12 +16,7 @@
 
 #include "HardwareBitmapUploader.h"
 
-#include "hwui/Bitmap.h"
-#include "renderthread/EglManager.h"
-#include "renderthread/VulkanManager.h"
-#include "thread/ThreadBase.h"
-#include "utils/TimeUtils.h"
-
+#include <EGL/egl.h>
 #include <EGL/eglext.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
@@ -29,11 +24,20 @@
 #include <GrContext.h>
 #include <SkCanvas.h>
 #include <SkImage.h>
+#include <private/android/AHardwareBufferHelpers.h>
 #include <utils/GLUtils.h>
+#include <utils/NdkUtils.h>
 #include <utils/Trace.h>
 #include <utils/TraceUtils.h>
+
 #include <thread>
 
+#include "hwui/Bitmap.h"
+#include "renderthread/EglManager.h"
+#include "renderthread/VulkanManager.h"
+#include "thread/ThreadBase.h"
+#include "utils/TimeUtils.h"
+
 namespace android::uirenderer {
 
 class AHBUploader;
@@ -42,7 +46,7 @@
 static sp<AHBUploader> sUploader = nullptr;
 
 struct FormatInfo {
-    PixelFormat pixelFormat;
+    AHardwareBuffer_Format bufferFormat;
     GLint format, type;
     VkFormat vkFormat;
     bool isSupported = false;
@@ -71,10 +75,10 @@
     }
 
     bool uploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format,
-                              sp<GraphicBuffer> graphicBuffer) {
+                              AHardwareBuffer* ahb) {
         ATRACE_CALL();
         beginUpload();
-        bool result = onUploadHardwareBitmap(bitmap, format, graphicBuffer);
+        bool result = onUploadHardwareBitmap(bitmap, format, ahb);
         endUpload();
         return result;
     }
@@ -93,7 +97,7 @@
     virtual void onDestroy() = 0;
 
     virtual bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format,
-                                        sp<GraphicBuffer> graphicBuffer) = 0;
+                                        AHardwareBuffer* ahb) = 0;
     virtual void onBeginUpload() = 0;
 
     bool shouldTimeOutLocked() {
@@ -165,16 +169,16 @@
     }
 
     bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format,
-                                sp<GraphicBuffer> graphicBuffer) override {
+                                AHardwareBuffer* ahb) override {
         ATRACE_CALL();
 
         EGLDisplay display = getUploadEglDisplay();
 
         LOG_ALWAYS_FATAL_IF(display == EGL_NO_DISPLAY, "Failed to get EGL_DEFAULT_DISPLAY! err=%s",
                             uirenderer::renderthread::EglManager::eglErrorString());
-        // We use an EGLImage to access the content of the GraphicBuffer
+        // We use an EGLImage to access the content of the buffer
         // The EGL image is later bound to a 2D texture
-        EGLClientBuffer clientBuffer = (EGLClientBuffer)graphicBuffer->getNativeBuffer();
+        EGLClientBuffer clientBuffer = (EGLClientBuffer)AHardwareBuffer_to_ANativeWindowBuffer(ahb);
         AutoEglImage autoImage(display, clientBuffer);
         if (autoImage.image == EGL_NO_IMAGE_KHR) {
             ALOGW("Could not create EGL image, err =%s",
@@ -272,13 +276,13 @@
     }
 
     bool onUploadHardwareBitmap(const SkBitmap& bitmap, const FormatInfo& format,
-                                sp<GraphicBuffer> graphicBuffer) override {
+                                AHardwareBuffer* ahb) override {
         ATRACE_CALL();
 
         std::lock_guard _lock{mLock};
 
-        sk_sp<SkImage> image = SkImage::MakeFromAHardwareBufferWithData(mGrContext.get(),
-            bitmap.pixmap(), reinterpret_cast<AHardwareBuffer*>(graphicBuffer.get()));
+        sk_sp<SkImage> image =
+                SkImage::MakeFromAHardwareBufferWithData(mGrContext.get(), bitmap.pixmap(), ahb);
         return (image.get() != nullptr);
     }
 
@@ -294,13 +298,17 @@
     // Gralloc shouldn't let us create a USAGE_HW_TEXTURE if GLES is unable to consume it, so
     // we don't need to double-check the GLES version/extension.
     std::call_once(sOnce, []() {
-        sp<GraphicBuffer> buffer = new GraphicBuffer(1, 1, PIXEL_FORMAT_RGBA_FP16,
-                                                     GraphicBuffer::USAGE_HW_TEXTURE |
-                                                             GraphicBuffer::USAGE_SW_WRITE_NEVER |
-                                                             GraphicBuffer::USAGE_SW_READ_NEVER,
-                                                     "tempFp16Buffer");
-        status_t error = buffer->initCheck();
-        hasFP16Support = !error;
+        AHardwareBuffer_Desc desc = {
+                .width = 1,
+                .height = 1,
+                .layers = 1,
+                .format = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT,
+                .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER |
+                         AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
+                         AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
+        };
+        UniqueAHardwareBuffer buffer = allocateAHardwareBuffer(desc);
+        hasFP16Support = buffer != nullptr;
     });
 
     return hasFP16Support;
@@ -314,7 +322,7 @@
             [[fallthrough]];
         // ARGB_4444 is upconverted to RGBA_8888
         case kARGB_4444_SkColorType:
-            formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
+            formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
             formatInfo.format = GL_RGBA;
             formatInfo.type = GL_UNSIGNED_BYTE;
             formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM;
@@ -323,25 +331,25 @@
             formatInfo.isSupported = HardwareBitmapUploader::hasFP16Support();
             if (formatInfo.isSupported) {
                 formatInfo.type = GL_HALF_FLOAT;
-                formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_FP16;
+                formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
                 formatInfo.vkFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
             } else {
                 formatInfo.type = GL_UNSIGNED_BYTE;
-                formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
+                formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
                 formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM;
             }
             formatInfo.format = GL_RGBA;
             break;
         case kRGB_565_SkColorType:
             formatInfo.isSupported = true;
-            formatInfo.pixelFormat = PIXEL_FORMAT_RGB_565;
+            formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
             formatInfo.format = GL_RGB;
             formatInfo.type = GL_UNSIGNED_SHORT_5_6_5;
             formatInfo.vkFormat = VK_FORMAT_R5G6B5_UNORM_PACK16;
             break;
         case kGray_8_SkColorType:
             formatInfo.isSupported = usingGL;
-            formatInfo.pixelFormat = PIXEL_FORMAT_RGBA_8888;
+            formatInfo.bufferFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
             formatInfo.format = GL_LUMINANCE;
             formatInfo.type = GL_UNSIGNED_BYTE;
             formatInfo.vkFormat = VK_FORMAT_R8G8B8A8_UNORM;
@@ -394,28 +402,27 @@
     }
 
     SkBitmap bitmap = makeHwCompatible(format, sourceBitmap);
-    sp<GraphicBuffer> buffer = new GraphicBuffer(
-            static_cast<uint32_t>(bitmap.width()), static_cast<uint32_t>(bitmap.height()),
-            format.pixelFormat,
-            GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
-                    GraphicBuffer::USAGE_SW_READ_NEVER,
-            std::string("Bitmap::allocateHardwareBitmap pid [") + std::to_string(getpid()) +
-                    "]");
-
-    status_t error = buffer->initCheck();
-    if (error < 0) {
-        ALOGW("createGraphicBuffer() failed in GraphicBuffer.create()");
+    AHardwareBuffer_Desc desc = {
+            .width = static_cast<uint32_t>(bitmap.width()),
+            .height = static_cast<uint32_t>(bitmap.height()),
+            .layers = 1,
+            .format = format.bufferFormat,
+            .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER | AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
+                     AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE,
+    };
+    UniqueAHardwareBuffer ahb = allocateAHardwareBuffer(desc);
+    if (!ahb) {
+        ALOGW("allocateHardwareBitmap() failed in AHardwareBuffer_allocate()");
         return nullptr;
-    }
+    };
 
     createUploader(usingGL);
 
-    if (!sUploader->uploadHardwareBitmap(bitmap, format, buffer)) {
+    if (!sUploader->uploadHardwareBitmap(bitmap, format, ahb.get())) {
         return nullptr;
     }
-    return Bitmap::createFrom(buffer->toAHardwareBuffer(), bitmap.colorType(),
-                              bitmap.refColorSpace(), bitmap.alphaType(),
-			      Bitmap::computePalette(bitmap));
+    return Bitmap::createFrom(ahb.get(), bitmap.colorType(), bitmap.refColorSpace(),
+                              bitmap.alphaType(), Bitmap::computePalette(bitmap));
 }
 
 void HardwareBitmapUploader::initialize() {
diff --git a/libs/hwui/Readback.cpp b/libs/hwui/Readback.cpp
index 84c07d7..b5b97fc 100644
--- a/libs/hwui/Readback.cpp
+++ b/libs/hwui/Readback.cpp
@@ -18,7 +18,6 @@
 
 #include <sync/sync.h>
 #include <system/window.h>
-#include <ui/GraphicBuffer.h>
 
 #include "DeferredLayerUpdater.h"
 #include "Properties.h"
@@ -28,6 +27,7 @@
 #include "renderthread/VulkanManager.h"
 #include "utils/Color.h"
 #include "utils/MathUtils.h"
+#include "utils/NdkUtils.h"
 #include "utils/TraceUtils.h"
 
 using namespace android::uirenderer::renderthread;
@@ -54,8 +54,7 @@
         return CopyResult::SourceEmpty;
     }
 
-    std::unique_ptr<AHardwareBuffer, decltype(&AHardwareBuffer_release)> sourceBuffer(
-            rawSourceBuffer, AHardwareBuffer_release);
+    UniqueAHardwareBuffer sourceBuffer{rawSourceBuffer};
     AHardwareBuffer_Desc description;
     AHardwareBuffer_describe(sourceBuffer.get(), &description);
     if (description.usage & AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT) {
diff --git a/libs/hwui/hwui/Bitmap.cpp b/libs/hwui/hwui/Bitmap.cpp
index 56d951c..df463e2 100644
--- a/libs/hwui/hwui/Bitmap.cpp
+++ b/libs/hwui/hwui/Bitmap.cpp
@@ -33,7 +33,6 @@
 #ifndef _WIN32
 #include <binder/IServiceManager.h>
 #endif
-#include <ui/PixelFormat.h>
 
 #include <SkCanvas.h>
 #include <SkImagePriv.h>
diff --git a/libs/hwui/pipeline/skia/VkFunctorDrawable.h b/libs/hwui/pipeline/skia/VkFunctorDrawable.h
index d3f9777..fbfc6e7 100644
--- a/libs/hwui/pipeline/skia/VkFunctorDrawable.h
+++ b/libs/hwui/pipeline/skia/VkFunctorDrawable.h
@@ -19,7 +19,6 @@
 #include "FunctorDrawable.h"
 
 #include <SkImageInfo.h>
-#include <ui/GraphicBuffer.h>
 #include <utils/RefBase.h>
 
 namespace android {
diff --git a/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.cpp b/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.cpp
index 241d370..43c2869 100644
--- a/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.cpp
+++ b/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.cpp
@@ -15,22 +15,24 @@
  */
 
 #include "VkInteropFunctorDrawable.h"
-#include <private/hwui/DrawGlInfo.h>
 
-#include <utils/Color.h>
-#include <utils/Trace.h>
-#include <utils/TraceUtils.h>
-#include <thread>
-#include "renderthread/EglManager.h"
-#include "thread/ThreadBase.h"
-#include "utils/TimeUtils.h"
-
+#include <EGL/egl.h>
 #include <EGL/eglext.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 #include <GLES3/gl3.h>
-
+#include <private/android/AHardwareBufferHelpers.h>
+#include <private/hwui/DrawGlInfo.h>
+#include <utils/Color.h>
 #include <utils/GLUtils.h>
+#include <utils/Trace.h>
+#include <utils/TraceUtils.h>
+
+#include <thread>
+
+#include "renderthread/EglManager.h"
+#include "thread/ThreadBase.h"
+#include "utils/TimeUtils.h"
 
 namespace android {
 namespace uirenderer {
@@ -75,20 +77,23 @@
 
     SkImageInfo surfaceInfo = canvas->imageInfo();
 
-    if (!mFrameBuffer.get() || mFBInfo != surfaceInfo) {
+    if (mFrameBuffer == nullptr || mFBInfo != surfaceInfo) {
         // Buffer will be used as an OpenGL ES render target.
-        mFrameBuffer = new GraphicBuffer(
-                // TODO: try to reduce the size of the buffer: possibly by using clip bounds.
-                static_cast<uint32_t>(surfaceInfo.width()),
-                static_cast<uint32_t>(surfaceInfo.height()),
-                ColorTypeToPixelFormat(surfaceInfo.colorType()),
-                GraphicBuffer::USAGE_HW_TEXTURE | GraphicBuffer::USAGE_SW_WRITE_NEVER |
-                        GraphicBuffer::USAGE_SW_READ_NEVER | GraphicBuffer::USAGE_HW_RENDER,
-                std::string("VkInteropFunctorDrawable::onDraw pid [") + std::to_string(getpid()) +
-                        "]");
-        status_t error = mFrameBuffer->initCheck();
-        if (error < 0) {
-            ALOGW("VkInteropFunctorDrawable::onDraw() failed in GraphicBuffer.create()");
+        AHardwareBuffer_Desc desc = {
+                .width = static_cast<uint32_t>(surfaceInfo.width()),
+                .height = static_cast<uint32_t>(surfaceInfo.height()),
+                .layers = 1,
+                .format = ColorTypeToBufferFormat(surfaceInfo.colorType()),
+                .usage = AHARDWAREBUFFER_USAGE_CPU_READ_NEVER |
+                         AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER |
+                         AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
+                         AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
+        };
+
+        mFrameBuffer = allocateAHardwareBuffer(desc);
+
+        if (!mFrameBuffer) {
+            ALOGW("VkInteropFunctorDrawable::onDraw() failed in AHardwareBuffer_allocate()");
             return;
         }
 
@@ -106,7 +111,8 @@
                             uirenderer::renderthread::EglManager::eglErrorString());
         // We use an EGLImage to access the content of the GraphicBuffer
         // The EGL image is later bound to a 2D texture
-        EGLClientBuffer clientBuffer = (EGLClientBuffer)mFrameBuffer->getNativeBuffer();
+        EGLClientBuffer clientBuffer =
+                (EGLClientBuffer)AHardwareBuffer_to_ANativeWindowBuffer(mFrameBuffer.get());
         AutoEglImage autoImage(display, clientBuffer);
         if (autoImage.image == EGL_NO_IMAGE_KHR) {
             ALOGW("Could not create EGL image, err =%s",
@@ -179,9 +185,9 @@
     // drawing into the offscreen surface, so we need to reset it here.
     canvas->resetMatrix();
 
-    auto functorImage = SkImage::MakeFromAHardwareBuffer(
-            reinterpret_cast<AHardwareBuffer*>(mFrameBuffer.get()), kPremul_SkAlphaType,
-            canvas->imageInfo().refColorSpace(), kBottomLeft_GrSurfaceOrigin);
+    auto functorImage = SkImage::MakeFromAHardwareBuffer(mFrameBuffer.get(), kPremul_SkAlphaType,
+                                                         canvas->imageInfo().refColorSpace(),
+                                                         kBottomLeft_GrSurfaceOrigin);
     canvas->drawImage(functorImage, 0, 0, &paint);
     canvas->restore();
 }
diff --git a/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.h b/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.h
index c47ee11..b12774f 100644
--- a/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.h
+++ b/libs/hwui/pipeline/skia/VkInteropFunctorDrawable.h
@@ -16,11 +16,12 @@
 
 #pragma once
 
-#include "FunctorDrawable.h"
-
-#include <ui/GraphicBuffer.h>
+#include <android/hardware_buffer.h>
+#include <utils/NdkUtils.h>
 #include <utils/RefBase.h>
 
+#include "FunctorDrawable.h"
+
 namespace android {
 namespace uirenderer {
 
@@ -45,7 +46,7 @@
 
 private:
     // Variables below describe/store temporary offscreen buffer used for Vulkan pipeline.
-    sp<GraphicBuffer> mFrameBuffer;
+    UniqueAHardwareBuffer mFrameBuffer;
     SkImageInfo mFBInfo;
 };
 
diff --git a/libs/hwui/renderthread/EglManager.h b/libs/hwui/renderthread/EglManager.h
index a893e24..f67fb31 100644
--- a/libs/hwui/renderthread/EglManager.h
+++ b/libs/hwui/renderthread/EglManager.h
@@ -21,7 +21,6 @@
 #include <SkImageInfo.h>
 #include <SkRect.h>
 #include <cutils/compiler.h>
-#include <ui/GraphicBuffer.h>
 #include <utils/StrongPointer.h>
 
 #include "IRenderPipeline.h"
diff --git a/libs/hwui/renderthread/ReliableSurface.cpp b/libs/hwui/renderthread/ReliableSurface.cpp
index 8a0b4e8..14a4c28 100644
--- a/libs/hwui/renderthread/ReliableSurface.cpp
+++ b/libs/hwui/renderthread/ReliableSurface.cpp
@@ -143,21 +143,25 @@
         return AHardwareBuffer_to_ANativeWindowBuffer(mScratchBuffer.get());
     }
 
-    AHardwareBuffer_Desc desc;
-    desc.usage = mUsage;
-    desc.format = mFormat;
-    desc.width = 1;
-    desc.height = 1;
-    desc.layers = 1;
-    desc.rfu0 = 0;
-    desc.rfu1 = 0;
-    AHardwareBuffer* newBuffer = nullptr;
-    int err = AHardwareBuffer_allocate(&desc, &newBuffer);
-    if (err) {
+    AHardwareBuffer_Desc desc = AHardwareBuffer_Desc{
+            .usage = mUsage,
+            .format = mFormat,
+            .width = 1,
+            .height = 1,
+            .layers = 1,
+            .rfu0 = 0,
+            .rfu1 = 0,
+    };
+
+    AHardwareBuffer* newBuffer;
+    int result = AHardwareBuffer_allocate(&desc, &newBuffer);
+
+    if (result != NO_ERROR) {
         // Allocate failed, that sucks
-        ALOGW("Failed to allocate scratch buffer, error=%d", err);
+        ALOGW("Failed to allocate scratch buffer, error=%d", result);
         return nullptr;
     }
+
     mScratchBuffer.reset(newBuffer);
     return AHardwareBuffer_to_ANativeWindowBuffer(newBuffer);
 }
diff --git a/libs/hwui/renderthread/ReliableSurface.h b/libs/hwui/renderthread/ReliableSurface.h
index 58cd067..c80a7ea 100644
--- a/libs/hwui/renderthread/ReliableSurface.h
+++ b/libs/hwui/renderthread/ReliableSurface.h
@@ -20,6 +20,7 @@
 #include <apex/window.h>
 #include <utils/Errors.h>
 #include <utils/Macros.h>
+#include <utils/NdkUtils.h>
 #include <utils/StrongPointer.h>
 
 #include <memory>
@@ -56,8 +57,7 @@
 
     uint64_t mUsage = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER;
     AHardwareBuffer_Format mFormat = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
-    std::unique_ptr<AHardwareBuffer, void (*)(AHardwareBuffer*)> mScratchBuffer{
-            nullptr, AHardwareBuffer_release};
+    UniqueAHardwareBuffer mScratchBuffer;
     ANativeWindowBuffer* mReservedBuffer = nullptr;
     base::unique_fd mReservedFenceFd;
     bool mHasDequeuedBuffer = false;
diff --git a/libs/hwui/renderthread/VulkanSurface.cpp b/libs/hwui/renderthread/VulkanSurface.cpp
index a7ea21d..9cbb754b 100644
--- a/libs/hwui/renderthread/VulkanSurface.cpp
+++ b/libs/hwui/renderthread/VulkanSurface.cpp
@@ -207,9 +207,9 @@
         }
     }
 
-    outWindowInfo->pixelFormat = ColorTypeToPixelFormat(colorType);
+    outWindowInfo->bufferFormat = ColorTypeToBufferFormat(colorType);
     VkFormat vkPixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
-    if (outWindowInfo->pixelFormat == PIXEL_FORMAT_RGBA_FP16) {
+    if (outWindowInfo->bufferFormat == AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT) {
         vkPixelFormat = VK_FORMAT_R16G16B16A16_SFLOAT;
     }
 
@@ -263,10 +263,10 @@
 bool VulkanSurface::UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo) {
     ATRACE_CALL();
 
-    int err = native_window_set_buffers_format(window, windowInfo.pixelFormat);
+    int err = native_window_set_buffers_format(window, windowInfo.bufferFormat);
     if (err != 0) {
         ALOGE("VulkanSurface::UpdateWindow() native_window_set_buffers_format(%d) failed: %s (%d)",
-              windowInfo.pixelFormat, strerror(-err), err);
+              windowInfo.bufferFormat, strerror(-err), err);
         return false;
     }
 
diff --git a/libs/hwui/renderthread/VulkanSurface.h b/libs/hwui/renderthread/VulkanSurface.h
index bd23626..40a44b1 100644
--- a/libs/hwui/renderthread/VulkanSurface.h
+++ b/libs/hwui/renderthread/VulkanSurface.h
@@ -17,8 +17,6 @@
 
 #include <system/graphics.h>
 #include <system/window.h>
-#include <ui/BufferQueueDefs.h>
-#include <ui/PixelFormat.h>
 #include <vulkan/vulkan.h>
 
 #include <SkRefCnt.h>
@@ -91,7 +89,7 @@
 
     struct WindowInfo {
         SkISize size;
-        PixelFormat pixelFormat;
+        uint32_t bufferFormat;
         android_dataspace dataspace;
         int transform;
         size_t bufferCount;
@@ -111,8 +109,13 @@
     static bool UpdateWindow(ANativeWindow* window, const WindowInfo& windowInfo);
     void releaseBuffers();
 
+    // TODO: This number comes from ui/BufferQueueDefs. We're not pulling the
+    // header in so that we don't need to depend on libui, but we should share
+    // this constant somewhere. But right now it's okay to keep here because we
+    // can't safely change the slot count anyways.
+    static constexpr size_t kNumBufferSlots = 64;
     // TODO: Just use a vector?
-    NativeBufferInfo mNativeBuffers[android::BufferQueueDefs::NUM_BUFFER_SLOTS];
+    NativeBufferInfo mNativeBuffers[kNumBufferSlots];
 
     sp<ANativeWindow> mNativeWindow;
     WindowInfo mWindowInfo;
diff --git a/libs/hwui/utils/Color.cpp b/libs/hwui/utils/Color.cpp
index 71a27ce..4b3e6002 100644
--- a/libs/hwui/utils/Color.cpp
+++ b/libs/hwui/utils/Color.cpp
@@ -16,8 +16,8 @@
 
 #include "Color.h"
 
-#include <utils/Log.h>
 #include <ui/ColorSpace.h>
+#include <utils/Log.h>
 
 #ifdef __ANDROID__ // Layoutlib does not support hardware buffers or native windows
 #include <android/hardware_buffer.h>
@@ -72,41 +72,29 @@
                                          sk_sp<SkColorSpace> colorSpace) {
     return createImageInfo(bufferDesc.width, bufferDesc.height, bufferDesc.format, colorSpace);
 }
-#endif
 
-android::PixelFormat ColorTypeToPixelFormat(SkColorType colorType) {
+uint32_t ColorTypeToBufferFormat(SkColorType colorType) {
     switch (colorType) {
         case kRGBA_8888_SkColorType:
-            return PIXEL_FORMAT_RGBA_8888;
+            return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
         case kRGBA_F16_SkColorType:
-            return PIXEL_FORMAT_RGBA_FP16;
+            return AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT;
         case kRGB_565_SkColorType:
-            return PIXEL_FORMAT_RGB_565;
+            return AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM;
         case kRGB_888x_SkColorType:
-            return PIXEL_FORMAT_RGBX_8888;
+            return AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM;
         case kRGBA_1010102_SkColorType:
-            return PIXEL_FORMAT_RGBA_1010102;
+            return AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM;
         case kARGB_4444_SkColorType:
-            return PIXEL_FORMAT_RGBA_4444;
+            // Hardcoding the value from android::PixelFormat
+            static constexpr uint64_t kRGBA4444 = 7;
+            return kRGBA4444;
         default:
             ALOGV("Unsupported colorType: %d, return RGBA_8888 by default", (int)colorType);
-            return PIXEL_FORMAT_RGBA_8888;
+            return AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
     }
 }
-
-SkColorType PixelFormatToColorType(android::PixelFormat format) {
-    switch (format) {
-        case PIXEL_FORMAT_RGBX_8888:    return kRGB_888x_SkColorType;
-        case PIXEL_FORMAT_RGBA_8888:    return kRGBA_8888_SkColorType;
-        case PIXEL_FORMAT_RGBA_FP16:    return kRGBA_F16_SkColorType;
-        case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
-        case PIXEL_FORMAT_RGBA_1010102: return kRGBA_1010102_SkColorType;
-        case PIXEL_FORMAT_RGBA_4444:    return kARGB_4444_SkColorType;
-        default:
-            ALOGV("Unsupported PixelFormat: %d, return kUnknown_SkColorType by default", format);
-            return kUnknown_SkColorType;
-    }
-}
+#endif
 
 namespace {
 static constexpr skcms_TransferFunction k2Dot6 = {2.6f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
diff --git a/libs/hwui/utils/Color.h b/libs/hwui/utils/Color.h
index a76f7e4..a29b285 100644
--- a/libs/hwui/utils/Color.h
+++ b/libs/hwui/utils/Color.h
@@ -16,14 +16,12 @@
 #ifndef COLOR_H
 #define COLOR_H
 
-#include <math.h>
-#include <cutils/compiler.h>
-#include <system/graphics.h>
-#include <ui/PixelFormat.h>
-
 #include <SkColor.h>
 #include <SkColorSpace.h>
 #include <SkImageInfo.h>
+#include <cutils/compiler.h>
+#include <math.h>
+#include <system/graphics.h>
 
 struct ANativeWindow_Buffer;
 struct AHardwareBuffer_Desc;
@@ -98,10 +96,9 @@
 
 SkImageInfo BufferDescriptionToImageInfo(const AHardwareBuffer_Desc& bufferDesc,
                                          sk_sp<SkColorSpace> colorSpace);
-#endif
 
-android::PixelFormat ColorTypeToPixelFormat(SkColorType colorType);
-ANDROID_API SkColorType PixelFormatToColorType(android::PixelFormat format);
+uint32_t ColorTypeToBufferFormat(SkColorType colorType);
+#endif
 
 ANDROID_API sk_sp<SkColorSpace> DataSpaceToColorSpace(android_dataspace dataspace);
 
diff --git a/libs/hwui/utils/NdkUtils.cpp b/libs/hwui/utils/NdkUtils.cpp
new file mode 100644
index 0000000..de6274e
--- /dev/null
+++ b/libs/hwui/utils/NdkUtils.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2020 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 <utils/NdkUtils.h>
+
+namespace android {
+namespace uirenderer {
+
+UniqueAHardwareBuffer allocateAHardwareBuffer(const AHardwareBuffer_Desc& desc) {
+    AHardwareBuffer* buffer;
+    if (AHardwareBuffer_allocate(&desc, &buffer) != 0) {
+        return nullptr;
+    } else {
+        return UniqueAHardwareBuffer{buffer};
+    }
+}
+
+}  // namespace uirenderer
+}  // namespace android
diff --git a/libs/hwui/utils/NdkUtils.h b/libs/hwui/utils/NdkUtils.h
new file mode 100644
index 0000000..f218eb2
--- /dev/null
+++ b/libs/hwui/utils/NdkUtils.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2020 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.
+ */
+
+#pragma once
+
+#include <android/hardware_buffer.h>
+
+#include <memory>
+
+namespace android {
+namespace uirenderer {
+
+// Deleter for an AHardwareBuffer, to be passed to an std::unique_ptr.
+struct AHardwareBuffer_deleter {
+    void operator()(AHardwareBuffer* ahb) const { AHardwareBuffer_release(ahb); }
+};
+
+using UniqueAHardwareBuffer = std::unique_ptr<AHardwareBuffer, AHardwareBuffer_deleter>;
+
+// Allocates a UniqueAHardwareBuffer with the provided buffer description.
+// Returns nullptr if allocation did not succeed.
+UniqueAHardwareBuffer allocateAHardwareBuffer(const AHardwareBuffer_Desc& desc);
+
+}  // namespace uirenderer
+}  // namespace android