[HWUI] remove libui from HWUI's dependencies

Bug: 136263238
Test: builds
Test: interact with device
Change-Id: I18540f78a4ee5ffcc30c96ff7862480e1d8fe50a
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