Revert "Switch cbmanager to HIDL HALs"

This reverts commit bff41297bdb7c141871ea365ed34530937d90f69.

Reason for revert: selinux prevents using HIDL HALs

Change-Id: Iaa8a140b6aa51223b61bd66391f1cbe96ce4a4e1
diff --git a/system/cbmanager/Android.mk b/system/cbmanager/Android.mk
index b067a44..418902f 100644
--- a/system/cbmanager/Android.mk
+++ b/system/cbmanager/Android.mk
@@ -34,12 +34,17 @@
 LOCAL_CFLAGS += \
     -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION) \
 
+ifeq ($(shell test $(PLATFORM_SDK_VERSION) -ge 30; echo $$?), 0)
 LOCAL_SRC_FILES := hidl.cpp
 LOCAL_SHARED_LIBRARIES += \
     libhidlbase \
     android.hardware.graphics.mapper@2.0 \
     android.hardware.graphics.allocator@2.0
-
+else
+LOCAL_SRC_FILES := gralloc.cpp
+LOCAL_SHARED_LIBRARIES += \
+    libhardware
+endif
 endif
 
 LOCAL_C_INCLUDES += \
diff --git a/system/cbmanager/debug.h b/system/cbmanager/debug.h
deleted file mode 100644
index c28b8e7..0000000
--- a/system/cbmanager/debug.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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.
- */
-
-#ifndef ANDROID_GOLDFISH_OPENGL_SYSTEM_CBMANAGER_DEBUG_H
-#define ANDROID_GOLDFISH_OPENGL_SYSTEM_CBMANAGER_DEBUG_H
-
-#include <log/log.h>
-
-#define CRASH(MSG) \
-    do { \
-        ALOGE("%s:%d crashed with '%s'", __func__, __LINE__, MSG); \
-        ::abort(); \
-    } while (false)
-
-#define CRASH_IF(COND, MSG) \
-    do { \
-        if ((COND)) { \
-            ALOGE("%s:%d crashed on '%s' with '%s'", __func__, __LINE__, #COND, MSG); \
-            ::abort(); \
-        } \
-    } while (false)
-
-#define RETURN_ERROR_CODE(X) \
-    do { \
-        ALOGE("%s:%d failed with '%s' (%d)", \
-              __func__, __LINE__, strerror(-(X)), -(X)); \
-        return (X); \
-    } while (false)
-
-#define RETURN_ERROR(X) \
-    do { \
-        ALOGE("%s:%d failed with '%s'", __func__, __LINE__, #X); \
-        return (X); \
-    } while (false)
-
-#if 1
-#define RETURN(X) return (X)
-#else
-#define RETURN(X) \
-    do { \
-        ALOGE("%s:%d", __func__, __LINE__); \
-        return (X); \
-    } while (false)
-#endif
-
-#endif  // ANDROID_GOLDFISH_OPENGL_SYSTEM_CBMANAGER_DEBUG_H
diff --git a/system/cbmanager/gralloc.cpp b/system/cbmanager/gralloc.cpp
new file mode 100644
index 0000000..dab4e39
--- /dev/null
+++ b/system/cbmanager/gralloc.cpp
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2019 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 <hardware/gralloc.h>
+#include "cbmanager.h"
+
+namespace android {
+namespace {
+
+class CbManagerGrallocImpl : public CbManager::CbManagerImpl {
+public:
+    CbManagerGrallocImpl(const hw_module_t* hwModule, alloc_device_t* allocDev)
+      : mHwModule(hwModule), mAllocDev(allocDev) {}
+
+    ~CbManagerGrallocImpl() {
+        gralloc_close(mAllocDev);
+    }
+
+    const cb_handle_t* allocateBuffer(int width, int height, int format) {
+        int ret;
+        int stride;
+        buffer_handle_t handle;
+
+        ret = mAllocDev->alloc(mAllocDev, width, height, format,
+                               GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER,
+                               &handle, &stride);
+        return ret ? nullptr : cb_handle_t::from(handle);
+    }
+
+    void freeBuffer(const cb_handle_t* h) {
+        mAllocDev->free(mAllocDev, h);
+    }
+
+private:
+    const hw_module_t* mHwModule;
+    alloc_device_t* mAllocDev;
+};
+
+std::unique_ptr<CbManager::CbManagerImpl> buildGrallocImpl() {
+    int ret;
+    const hw_module_t* hwModule;
+
+    ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &hwModule);
+    if (ret) {
+        return nullptr;
+    }
+
+    alloc_device_t* allocDev;
+    ret = gralloc_open(hwModule, &allocDev);
+    if (ret) {
+        return nullptr;
+    }
+
+    return std::make_unique<CbManagerGrallocImpl>(hwModule, allocDev);
+}
+}  // namespace
+
+CbManager::CbManager() : mImpl(buildGrallocImpl()) {}
+
+}  // namespace android
diff --git a/system/cbmanager/hidl.cpp b/system/cbmanager/hidl.cpp
index 5488191..ff08642 100644
--- a/system/cbmanager/hidl.cpp
+++ b/system/cbmanager/hidl.cpp
@@ -17,10 +17,8 @@
 #include <cutils/native_handle.h>
 #include <android/hardware/graphics/allocator/2.0/IAllocator.h>
 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
-#include <log/log.h>
 
 #include "cbmanager.h"
-#include "debug.h"
 
 namespace android {
 namespace {
@@ -32,9 +30,9 @@
 namespace IMapper2ns = ::android::hardware::graphics::mapper::V2_0;
 namespace IAllocator2ns = ::android::hardware::graphics::allocator::V2_0;
 
-class CbManagerHidlV2Impl : public CbManager::CbManagerImpl {
+class CbManagerHidlV3Impl : public CbManager::CbManagerImpl {
 public:
-    CbManagerHidlV2Impl(::android::sp<IMapper2ns::IMapper> mapper,
+    CbManagerHidlV3Impl(::android::sp<IMapper2ns::IMapper> mapper,
                         ::android::sp<IAllocator2ns::IAllocator> allocator)
       : mMapper(mapper), mAllocator(allocator) {}
 
@@ -59,20 +57,19 @@
             descriptor = _descriptor;
         });
         if (hidl_err != Error::NONE) {
-            RETURN_ERROR(nullptr);
+          return nullptr;
         }
 
         hidl_handle raw_handle = nullptr;
         mAllocator->allocate(descriptor, 1,
                              [&](const Error &_error,
-                                 uint32_t _stride,
+                                 uint32_t /*_stride*/,
                                  const hidl_vec<hidl_handle> &_buffers) {
             hidl_err = _error;
-            (void)_stride;
             raw_handle = _buffers[0];
         });
         if (hidl_err != Error::NONE) {
-            RETURN_ERROR(nullptr);
+            return nullptr;
         }
 
         const cb_handle_t *buf = nullptr;
@@ -82,10 +79,10 @@
             buf = cb_handle_t::from(_buf);
         });
         if (hidl_err != Error::NONE) {
-            RETURN_ERROR(nullptr);
+          return nullptr;
         }
 
-        RETURN(buf);
+        return buf;
     }
 
     void freeBuffer(const cb_handle_t* _h) {
@@ -98,107 +95,25 @@
         native_handle_delete(h);
     }
 
-    int lockBuffer(cb_handle_t& handle,
-                   const int usage,
-                   const int left, const int top, const int width, const int height,
-                   void** vaddr) {
-        using IMapper2ns::Error;
-
-        Error hidl_err = Error::NONE;
-        mMapper->lock(
-            &handle,
-            usage,
-            { left, top, width, height },  // rect
-            hidl_handle(),  // fence
-            [&hidl_err, vaddr](const Error &_error,
-                               void* _ptr) {
-                hidl_err = _error;
-                if (_error == Error::NONE) {
-                    *vaddr = _ptr;
-                }
-            });
-
-        if (hidl_err == Error::NONE) {
-            RETURN(0);
-        } else {
-            RETURN_ERROR(-1);
-        }
-    }
-
-    int lockYCbCrBuffer(cb_handle_t& handle,
-                        const int usage,
-                        const int left, const int top, const int width, const int height,
-                        android_ycbcr* a_ycbcr) {
-        using IMapper2ns::Error;
-        using IMapper2ns::YCbCrLayout;
-
-        Error hidl_err = Error::NONE;
-        mMapper->lockYCbCr(
-            &handle,
-            usage,
-            { left, top, width, height },  // rect
-            hidl_handle(),  // fence
-            [&hidl_err, &a_ycbcr](const Error &_error,
-                                  const YCbCrLayout &h_ycbcr) {
-                hidl_err = _error;
-                if (_error == Error::NONE) {
-                    a_ycbcr->y = h_ycbcr.y;
-                    a_ycbcr->cb = h_ycbcr.cb;
-                    a_ycbcr->cr = h_ycbcr.cr;
-                    a_ycbcr->ystride = h_ycbcr.yStride;
-                    a_ycbcr->cstride = h_ycbcr.cStride;
-                    a_ycbcr->chroma_step = h_ycbcr.chromaStep;
-                }
-            });
-
-        if (hidl_err == Error::NONE) {
-            RETURN(0);
-        } else {
-            RETURN_ERROR(-1);
-        }
-    }
-
-    int unlockBuffer(cb_handle_t& handle) {
-        using IMapper2ns::Error;
-
-        Error hidl_err = Error::NONE;
-        int fence = -1;
-        mMapper->unlock(
-            &handle,
-            [&hidl_err, &fence](const Error &_error,
-                                const hidl_handle &_fence) {
-                hidl_err = _error;
-                (void)_fence;
-            });
-
-        if (hidl_err == Error::NONE) {
-            RETURN(0);
-        } else {
-            RETURN_ERROR(-1);
-        }
-    }
-
 private:
     const ::android::sp<IMapper2ns::IMapper> mMapper;
     const ::android::sp<IAllocator2ns::IAllocator> mAllocator;
 };
 
 std::unique_ptr<CbManager::CbManagerImpl> buildHidlImpl() {
-    ::android::sp<IMapper2ns::IMapper> mapper =
-        IMapper2ns::IMapper::getService();
-    if (!mapper) {
-        ALOGE("%s:%d: no IMapper implementation found", __func__, __LINE__);
-        RETURN_ERROR(nullptr);
+    {
+        ::android::sp<IMapper2ns::IMapper> mapper =
+            IMapper2ns::IMapper::getService();
+        ::android::sp<IAllocator2ns::IAllocator> allocator =
+            IAllocator2ns::IAllocator::getService();
+        if (mapper && allocator) {
+            return std::make_unique<CbManagerHidlV3Impl>(mapper, allocator);
+        }
     }
 
-    ::android::sp<IAllocator2ns::IAllocator> allocator =
-        IAllocator2ns::IAllocator::getService();
-    if (!allocator) {
-        ALOGE("%s:%d: no IAllocator implementation found", __func__, __LINE__);
-        RETURN_ERROR(nullptr);
-    }
-
-    return std::make_unique<CbManagerHidlV2Impl>(mapper, allocator);
+    ALOGE("%s:%d: no IMapper and IAllocator implementations found",
+          __func__, __LINE__);
+    return nullptr;
 }
 
 }  // namespace
diff --git a/system/include/cbmanager.h b/system/include/cbmanager.h
index d10a109..4d98641 100644
--- a/system/include/cbmanager.h
+++ b/system/include/cbmanager.h
@@ -18,7 +18,6 @@
 #define ANDROID_GOLDFISH_OPENGL_SYSTEM_CBMANAGER_CBMANAGER_H
 
 #include <memory>
-#include <system/graphics.h>
 #include "gralloc_cb.h"
 
 namespace android {
@@ -34,18 +33,6 @@
                                                   int height,
                                                   int format) = 0;
         virtual void freeBuffer(const cb_handle_t* h) = 0;
-
-        virtual int lockBuffer(cb_handle_t& handle,
-             const int usage,
-             const int left, const int top, const int width, const int height,
-             void** vaddr) = 0;
-
-        virtual int lockYCbCrBuffer(cb_handle_t& handle,
-             const int usage,
-             const int left, const int top, const int width, const int height,
-             android_ycbcr* ycbcr) = 0;
-
-        virtual int unlockBuffer(cb_handle_t& handle) = 0;
     };
 
     const cb_handle_t* allocateBuffer(int width, int height, int format) {
@@ -56,63 +43,6 @@
         mImpl->freeBuffer(h);
     }
 
-    int lockBuffer(cb_handle_t& handle,
-             const int usage,
-             const int left, const int top, const int width, const int height,
-             void** vaddr) {
-        return mImpl->lockBuffer(handle, usage, left, top, width, height, vaddr);
-    }
-
-    int lockBuffer(buffer_handle_t h,
-             const int usage,
-             const int left, const int top, const int width, const int height,
-             void** vaddr) {
-        cb_handle_t* cb = cb_handle_t::from_unconst(h);
-        if (cb) {
-            return lockBuffer(*cb, usage, left, top, width, height, vaddr);
-        } else {
-            return -1;
-        }
-    }
-
-
-    int lockYCbCrBuffer(cb_handle_t& handle,
-                        const int usage,
-                        const int left, const int top, const int width, const int height,
-                        android_ycbcr* ycbcr) {
-        return mImpl->lockYCbCrBuffer(handle, usage, left, top, width, height, ycbcr);
-    }
-
-    int lockYCbCrBuffer(buffer_handle_t h,
-                        const int usage,
-                        const int left, const int top, const int width, const int height,
-                        struct android_ycbcr* ycbcr) {
-        cb_handle_t* cb = cb_handle_t::from_unconst(h);
-        if (cb) {
-            return lockYCbCrBuffer(*cb, usage, left, top, width, height, ycbcr);
-        } else {
-            return -1;
-        }
-    }
-
-    int unlockBuffer(cb_handle_t& handle) {
-        return mImpl->unlockBuffer(handle);
-    }
-
-    int unlockBuffer(buffer_handle_t h) {
-        cb_handle_t* cb = cb_handle_t::from_unconst(h);
-        if (cb) {
-            return unlockBuffer(*cb);
-        } else {
-            return -1;
-        }
-    }
-
-    static uint64_t getOffset(const buffer_handle_t h) {
-        const cb_handle_t* cb = cb_handle_t::from(h);
-        return cb ? cb->getMmapedOffset() : -1;
-    }
-
 private:
     std::unique_ptr<CbManagerImpl> mImpl;
 };