Introduce safer type castings for cb_handle_t

There is a lot of to-from cb_handle_t type castings, move it into
one place.

Bug: 141385612
Test: build
Change-Id: Iee7194eee513ef531b67cebf3dd8ed98c3face02
Signed-off-by: Roman Kiryanov <rkir@google.com>
diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index bff33f3..af596d8 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -73,6 +73,22 @@
         magic = 0;
     }
 
+    static cb_handle_t* from_native_handle(native_handle* n) {
+        return static_cast<cb_handle_t*>(n);
+    }
+
+    static const cb_handle_t* from_native_handle(const native_handle* n) {
+        return static_cast<const cb_handle_t*>(n);
+    }
+
+    static cb_handle_t* from_raw_pointer(void* ptr) {
+        return from_native_handle(static_cast<native_handle*>(ptr));
+    }
+
+    static const cb_handle_t* from_raw_pointer(const void* ptr) {
+        return from_native_handle(static_cast<const native_handle*>(ptr));
+    }
+
     void setFd(int p_fd) {
         if (p_fd >= 0) {
             numFds++;
diff --git a/system/OpenglSystemCommon/HostConnection.cpp b/system/OpenglSystemCommon/HostConnection.cpp
index f0a3c23..9708829 100644
--- a/system/OpenglSystemCommon/HostConnection.cpp
+++ b/system/OpenglSystemCommon/HostConnection.cpp
@@ -84,12 +84,12 @@
 public:
     uint32_t getHostHandle(native_handle_t const* handle)
     {
-        return ((cb_handle_t *)handle)->hostHandle;
+        return cb_handle_t::from_native_handle(handle)->hostHandle;
     }
 
     int getFormat(native_handle_t const* handle)
     {
-        return ((cb_handle_t *)handle)->format;
+        return cb_handle_t::from_native_handle(handle)->format;
     }
 };
 
diff --git a/system/hwc2/EmuHWC2.cpp b/system/hwc2/EmuHWC2.cpp
index 3bda1a1..ae6be6b 100644
--- a/system/hwc2/EmuHWC2.cpp
+++ b/system/hwc2/EmuHWC2.cpp
@@ -379,7 +379,8 @@
     if (mHandle != nullptr) {
         mGralloc->unregisterBuffer(mGralloc, mHandle);
         mAllocDev->free(mAllocDev, mHandle);
-        ALOGI("free targetCb %d", ((cb_handle_t*)(mHandle))->hostHandle);
+        ALOGI("free targetCb %u",
+            cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
     }
 }
 
@@ -394,10 +395,9 @@
                                &mHandle, &stride);
         assert(ret == 0 && "Fail to allocate target ColorBuffer");
         mGralloc->registerBuffer(mGralloc, mHandle);
-        ALOGI("targetCb %d", reinterpret_cast<const cb_handle_t*>(mHandle)
-              ->hostHandle);
+        ALOGI("targetCb %u", cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
     }
-    return reinterpret_cast<const cb_handle_t*>(mHandle)->hostHandle;
+    return cb_handle_t::from_raw_pointer(mHandle)->hostHandle;
 }
 
 // Display functions
@@ -781,8 +781,8 @@
                     ALOGV("%s: acquire fence not set for layer %u",
                           __FUNCTION__, (uint32_t)layer->getId());
                 }
-                cb_handle_t *cb =
-                    (cb_handle_t *)layer->getLayerBuffer().getBuffer();
+                const cb_handle_t *cb =
+                    cb_handle_t::from_raw_pointer(layer->getLayerBuffer().getBuffer());
                 if (cb != nullptr) {
                     l->cbHandle = cb->hostHandle;
                 }
@@ -892,8 +892,7 @@
         int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) {
     ALOGVV("%s", __FUNCTION__);
 
-    cb_handle_t *cb =
-            (cb_handle_t *)target;
+    const cb_handle_t *cb = cb_handle_t::from_raw_pointer(target);
     ALOGV("%s: display(%u) buffer handle %p cb %d, acquireFence %d", __FUNCTION__,
           (uint32_t)mId, target, cb->hostHandle, acquireFence);
     std::unique_lock<std::mutex> lock(mStateMutex);
diff --git a/system/vulkan_enc/AndroidHardwareBuffer.cpp b/system/vulkan_enc/AndroidHardwareBuffer.cpp
index 62b122a..385b043 100644
--- a/system/vulkan_enc/AndroidHardwareBuffer.cpp
+++ b/system/vulkan_enc/AndroidHardwareBuffer.cpp
@@ -111,8 +111,7 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(buffer);
-    const cb_handle_t* cb_handle =
-        reinterpret_cast<const cb_handle_t*>(handle);
+    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
     uint32_t colorBufferHandle = cb_handle->hostHandle;
 
     if (!colorBufferHandle) {
@@ -166,8 +165,7 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(info->buffer);
-    const cb_handle_t* cb_handle =
-        reinterpret_cast<const cb_handle_t*>(handle);
+    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
     uint32_t colorBufferHandle = cb_handle->hostHandle;
 
     if (!colorBufferHandle) {
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 35a5a38..1db44cc 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -1835,8 +1835,7 @@
             ALOGD("%s: Import AHardwareBulffer", __func__);
             const native_handle_t *handle =
                 AHardwareBuffer_getNativeHandle(ahw);
-            const cb_handle_t* cb_handle =
-                reinterpret_cast<const cb_handle_t*>(handle);
+            const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
             importCbInfo.colorBuffer = cb_handle->hostHandle;
             vk_append_struct(&structChainIter, &importCbInfo);
         }
@@ -3464,7 +3463,7 @@
         }
 
         const cb_handle_t* cb_handle =
-            reinterpret_cast<const cb_handle_t*>(nativeInfo->handle);
+            cb_handle_t::from_raw_pointer(nativeInfo->handle);
 
         if (!cb_handle) return;