Create a buffer independent cb_handle_t base class

Since PLATFORM_SDK_VERSION is not available everywhere, we need
a generic API to get information about gralloc buffers like width or
the host handle. All such fields are moved to a separate class to used
by gralloc implementations.

Bug: 141385612
Test: boot, check the virtual scene camera
Change-Id: I9d5abe31731a354b502d8db86cc4eb013c2f026c
Signed-off-by: Roman Kiryanov <rkir@google.com>
diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index 2d80f26..3167ca0 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -17,7 +17,103 @@
 #ifndef __GRALLOC_CB_H__
 #define __GRALLOC_CB_H__
 
-#include "gralloc_cb_old.h"
-typedef cb_handle_old_t cb_handle_t;
+#include <cutils/native_handle.h>
+#include "qemu_pipe.h"
+
+const uint32_t CB_HANDLE_MAGIC_MASK = 0xFFFFFFF0;
+const uint32_t CB_HANDLE_MAGIC_BASE = 0xABFABFA0;
+
+#define CB_HANDLE_NUM_INTS(nfd) \
+    ((sizeof(*this)-sizeof(native_handle_t)-nfd*sizeof(int32_t))/sizeof(int32_t))
+
+struct cb_handle_t : public native_handle_t {
+    cb_handle_t(int32_t p_bufferFd,
+                QEMU_PIPE_HANDLE p_hostHandleRefCountFd,
+                uint32_t p_magic,
+                uint32_t p_hostHandle,
+                int32_t p_usage,
+                int32_t p_width,
+                int32_t p_height,
+                int32_t p_format,
+                int32_t p_glFormat,
+                int32_t p_glType,
+                uint32_t p_bufSize,
+                void* p_bufPtr)
+        : bufferFd(p_bufferFd),
+          hostHandleRefCountFd(p_hostHandleRefCountFd),
+          magic(p_magic),
+          hostHandle(p_hostHandle),
+          usage(p_usage),
+          width(p_width),
+          height(p_height),
+          format(p_format),
+          glFormat(p_glFormat),
+          glType(p_glType),
+          bufferSize(p_bufSize),
+          lockedLeft(0),
+          lockedTop(0),
+          lockedWidth(0),
+          lockedHeight(0) {
+        version = sizeof(native_handle);
+        numFds = ((bufferFd >= 0) ? 1 : 0) + (qemu_pipe_valid(hostHandleRefCountFd) ? 1 : 0);
+        numInts = 0; // has to be overwritten in children classes
+        setBufferPtr(p_bufPtr);
+    }
+
+    void* getBufferPtr() const {
+        const uint64_t addr = (uint64_t(bufferPtrHi) << 32) | bufferPtrLo;
+        return reinterpret_cast<void*>(static_cast<uintptr_t>(addr));
+    }
+
+    void setBufferPtr(void* ptr) {
+        const uint64_t addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr));
+        bufferPtrLo = uint32_t(addr);
+        bufferPtrHi = uint32_t(addr >> 32);
+    }
+
+    uint32_t allocatedSize() const {
+        return getBufferPtr() ? bufferSize : 0;
+    }
+
+    bool isValid() const {
+        return (version == sizeof(native_handle))
+               && (magic & CB_HANDLE_MAGIC_MASK) == CB_HANDLE_MAGIC_BASE;
+    }
+
+    static cb_handle_t* from(void* p) {
+        if (!p) { return nullptr; }
+        cb_handle_t* cb = static_cast<cb_handle_t*>(p);
+        return cb->isValid() ? cb : nullptr;
+    }
+
+    static const cb_handle_t* from(const void* p) {
+        return from(const_cast<void*>(p));
+    }
+
+    static cb_handle_t* from_unconst(const void* p) {
+        return from(const_cast<void*>(p));
+    }
+
+    // fds
+    int32_t bufferFd;       // underlying buffer file handle
+    QEMU_PIPE_HANDLE hostHandleRefCountFd; // guest side refcounter to hostHandle
+
+    // ints
+    uint32_t magic;         // magic number in order to validate a pointer
+    uint32_t hostHandle;    // the host reference to this buffer
+    int32_t  usage;         // usage bits the buffer was created with
+    int32_t  width;         // buffer width
+    int32_t  height;        // buffer height
+    int32_t  format;        // real internal pixel format format
+    int32_t  glFormat;      // OpenGL format enum used for host h/w color buffer
+    int32_t  glType;        // OpenGL type enum used when uploading to host
+    uint32_t bufferSize;    // buffer size and location
+    uint32_t bufferPtrLo;
+    uint32_t bufferPtrHi;
+    int32_t  lockedLeft;    // region of buffer locked for s/w write
+    int32_t  lockedTop;
+    int32_t  lockedWidth;
+    int32_t  lockedHeight;
+};
 
 #endif //__GRALLOC_CB_H__
diff --git a/shared/OpenglCodecCommon/gralloc_cb_common.h b/shared/OpenglCodecCommon/gralloc_cb_common.h
deleted file mode 100644
index 94ada2f..0000000
--- a/shared/OpenglCodecCommon/gralloc_cb_common.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
-* Copyright 2011 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 __GRALLOC_CB_COMMON_H__
-#define __GRALLOC_CB_COMMON_H__
-
-// Tell the emulator which gralloc formats
-// need special handling.
-enum EmulatorFrameworkFormat {
-    FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
-    FRAMEWORK_FORMAT_YV12 = 1,
-    FRAMEWORK_FORMAT_YUV_420_888 = 2,              // (Y+)(U+)(V+)
-};
-
-#endif //__GRALLOC_CB_COMMON_H__
diff --git a/shared/OpenglCodecCommon/gralloc_cb_old.h b/shared/OpenglCodecCommon/gralloc_cb_old.h
deleted file mode 100644
index 1c14579..0000000
--- a/shared/OpenglCodecCommon/gralloc_cb_old.h
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
-* Copyright 2011 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 __GRALLOC_CB_OLD_H__
-#define __GRALLOC_CB_OLD_H__
-
-#include <hardware/hardware.h>
-#include <hardware/gralloc.h>
-#include <cutils/native_handle.h>
-
-#include "gralloc_cb_common.h"
-#include "qemu_pipe.h"
-
-#define BUFFER_HANDLE_MAGIC ((int)0xabfabfab)
-#define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_old_t) - (nfds)*sizeof(int)) / sizeof(int))
-
-//
-// Our buffer handle structure
-//
-struct cb_handle_old_t : public native_handle {
-
-    cb_handle_old_t(int p_fd, int p_ashmemSize, int p_usage,
-                    int p_width, int p_height,
-                    int p_format, int p_glFormat, int p_glType) :
-        fd(p_fd),
-        magic(BUFFER_HANDLE_MAGIC),
-        usage(p_usage),
-        width(p_width),
-        height(p_height),
-        format(p_format),
-        glFormat(p_glFormat),
-        glType(p_glType),
-        ashmemSize(p_ashmemSize),
-        ashmemBase(0),
-        ashmemBasePid(0),
-        mappedPid(0),
-        lockedLeft(0),
-        lockedTop(0),
-        lockedWidth(0),
-        lockedHeight(0),
-        hostHandle(0)
-    {
-        refcount_pipe_fd = QEMU_PIPE_INVALID_HANDLE;
-        version = sizeof(native_handle);
-        numFds = 0;
-        numInts = CB_HANDLE_NUM_INTS(numFds);
-    }
-
-    ~cb_handle_old_t() {
-        magic = 0;
-    }
-
-    static cb_handle_old_t* from_native_handle(native_handle* n) {
-        return static_cast<cb_handle_old_t*>(n);
-    }
-
-    static const cb_handle_old_t* from_native_handle(const native_handle* n) {
-        return static_cast<const cb_handle_old_t*>(n);
-    }
-
-    static cb_handle_old_t* from_raw_pointer(void* ptr) {
-        return from_native_handle(static_cast<native_handle*>(ptr));
-    }
-
-    static const cb_handle_old_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++;
-        }
-        fd = p_fd;
-        numInts = CB_HANDLE_NUM_INTS(numFds);
-    }
-
-    bool hasRefcountPipe() {
-        return qemu_pipe_valid(refcount_pipe_fd);
-    }
-
-    void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) {
-        if (qemu_pipe_valid(fd)) {
-            numFds++;
-        }
-        refcount_pipe_fd = fd;
-        numInts = CB_HANDLE_NUM_INTS(numFds);
-    }
-
-    static bool validate(const cb_handle_old_t* hnd) {
-        return (hnd &&
-                hnd->version == sizeof(native_handle) &&
-                hnd->magic == BUFFER_HANDLE_MAGIC &&
-                hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds));
-    }
-
-    bool canBePosted() {
-        return (0 != (usage & GRALLOC_USAGE_HW_FB));
-    }
-
-    uint32_t allocationSize() const {
-        return ashmemBase ? ashmemSize : 0;
-    }
-
-    // file-descriptors
-    int fd;  // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
-    QEMU_PIPE_HANDLE refcount_pipe_fd; // goldfish pipe service for gralloc refcounting fd.
-
-    // ints
-    int magic;              // magic number in order to validate a pointer to be a cb_handle_old_t
-    int usage;              // usage bits the buffer was created with
-    int width;              // buffer width
-    int height;             // buffer height
-    int format;             // real internal pixel format format
-    int glFormat;           // OpenGL format enum used for host h/w color buffer
-    int glType;             // OpenGL type enum used when uploading to host
-    int ashmemSize;         // ashmem region size for the buffer (0 unless is HW_FB buffer or
-                            //                                    s/w access is needed)
-    union {
-        intptr_t ashmemBase;    // CPU address of the mapped ashmem region
-        uint64_t padding;       // enforce same size on 32-bit/64-bit
-    } __attribute__((aligned(8)));
-
-    int ashmemBasePid;      // process id which mapped the ashmem region
-    int mappedPid;          // process id which succeeded gralloc_register call
-    int lockedLeft;         // region of buffer locked for s/w write
-    int lockedTop;
-    int lockedWidth;
-    int lockedHeight;
-    uint32_t hostHandle;
-};
-
-
-#endif //__GRALLOC_CB_OLD_H__
diff --git a/system/OpenglSystemCommon/HostConnection.cpp b/system/OpenglSystemCommon/HostConnection.cpp
index a8e02bd..ba059a4 100644
--- a/system/OpenglSystemCommon/HostConnection.cpp
+++ b/system/OpenglSystemCommon/HostConnection.cpp
@@ -110,12 +110,12 @@
 public:
     uint32_t getHostHandle(native_handle_t const* handle)
     {
-        return cb_handle_t::from_native_handle(handle)->hostHandle;
+        return cb_handle_t::from(handle)->hostHandle;
     }
 
     int getFormat(native_handle_t const* handle)
     {
-        return cb_handle_t::from_native_handle(handle)->format;
+        return cb_handle_t::from(handle)->format;
     }
 };
 
diff --git a/system/gralloc/gralloc_common.h b/system/gralloc/gralloc_common.h
index 574e659..6dba9cd 100644
--- a/system/gralloc/gralloc_common.h
+++ b/system/gralloc/gralloc_common.h
@@ -17,6 +17,14 @@
 #ifndef __GRALLOC_COMMON_H__
 #define __GRALLOC_COMMON_H__
 
+// Tell the emulator which gralloc formats
+// need special handling.
+enum EmulatorFrameworkFormat {
+    FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
+    FRAMEWORK_FORMAT_YV12 = 1,
+    FRAMEWORK_FORMAT_YUV_420_888 = 2,              // (Y+)(U+)(V+)
+};
+
 #ifndef GL_RGBA16F
 #define GL_RGBA16F                        0x881A
 #endif // GL_RGBA16F
diff --git a/system/gralloc/gralloc_old.cpp b/system/gralloc/gralloc_old.cpp
index eecf698..6ab38a2 100644
--- a/system/gralloc/gralloc_old.cpp
+++ b/system/gralloc/gralloc_old.cpp
@@ -1,4 +1,4 @@
-/*
+ /*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +13,7 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
+
 #include <string.h>
 #include <pthread.h>
 #include <limits.h>
@@ -21,11 +22,12 @@
 #include <errno.h>
 #include <dlfcn.h>
 #include <sys/mman.h>
+#include <hardware/gralloc.h>
 
 #if PLATFORM_SDK_VERSION < 28
-#include "gralloc_cb_old.h"
+#include "gralloc_cb.h"
 #else
-#include "../../shared/OpenglCodecCommon/gralloc_cb_old.h"
+#include "../../shared/OpenglCodecCommon/gralloc_cb.h"
 #endif
 
 #include "gralloc_common.h"
@@ -81,8 +83,69 @@
 static const bool isHidlGralloc = false;
 #endif
 
-int32_t* getOpenCountPtr(cb_handle_old_t* cb) {
-    return ((int32_t*)cb->ashmemBase) + 1;
+const uint32_t CB_HANDLE_MAGIC_OLD = CB_HANDLE_MAGIC_BASE | 0x1;
+
+struct cb_handle_old_t : public cb_handle_t {
+    cb_handle_old_t(int p_fd, int p_ashmemSize, int p_usage,
+                    int p_width, int p_height,
+                    int p_format, int p_glFormat, int p_glType)
+            : cb_handle_t(p_fd,
+                          QEMU_PIPE_INVALID_HANDLE,
+                          CB_HANDLE_MAGIC_OLD,
+                          0,
+                          p_usage,
+                          p_width,
+                          p_height,
+                          p_format,
+                          p_glFormat,
+                          p_glType,
+                          p_ashmemSize,
+                          nullptr),
+              ashmemBasePid(0),
+              mappedPid(0) {
+        numInts = CB_HANDLE_NUM_INTS(numFds);
+    }
+
+    bool hasRefcountPipe() const {
+        return qemu_pipe_valid(hostHandleRefCountFd);
+    }
+
+    void setRefcountPipeFd(QEMU_PIPE_HANDLE fd) {
+        if (qemu_pipe_valid(fd)) {
+            numFds++;
+        }
+        hostHandleRefCountFd = fd;
+        numInts = CB_HANDLE_NUM_INTS(numFds);
+    }
+
+    bool canBePosted() const {
+        return (0 != (usage & GRALLOC_USAGE_HW_FB));
+    }
+
+    bool isValid() const {
+        return (version == sizeof(native_handle)) && (magic == CB_HANDLE_MAGIC_OLD);
+    }
+
+    static cb_handle_old_t* from(void* p) {
+        if (!p) { return nullptr; }
+        cb_handle_old_t* cb = static_cast<cb_handle_old_t*>(p);
+        return cb->isValid() ? cb : nullptr;
+    }
+
+    static const cb_handle_old_t* from(const void* p) {
+        return from(const_cast<void*>(p));
+    }
+
+    static cb_handle_old_t* from_unconst(const void* p) {
+        return from(const_cast<void*>(p));
+    }
+
+    int32_t ashmemBasePid;      // process id which mapped the ashmem region
+    int32_t mappedPid;          // process id which succeeded gralloc_register call
+};
+
+int32_t* getOpenCountPtr(const cb_handle_old_t* cb) {
+    return ((int32_t*)cb->getBufferPtr()) + 1;
 }
 
 uint32_t getAshmemColorOffset(cb_handle_old_t* cb) {
@@ -331,7 +394,7 @@
     dump_regions(rcEnc);
 #endif
 
-    get_mem_region((void*)cb->ashmemBase);
+    get_mem_region(cb->getBufferPtr());
 
 #if DEBUG
     dump_regions(rcEnc);
@@ -345,13 +408,13 @@
     dump_regions(rcEnc);
 #endif
 
-    const bool should_unmap = put_mem_region(rcEnc, (void*)cb->ashmemBase);
+    const bool should_unmap = put_mem_region(rcEnc, cb->getBufferPtr());
 
 #if DEBUG
     dump_regions(rcEnc);
 #endif
 
-    put_gralloc_region(rcEnc, cb->ashmemSize);
+    put_gralloc_region(rcEnc, cb->bufferSize);
 
     return should_unmap;
 }
@@ -365,21 +428,21 @@
 
 static int map_buffer(cb_handle_old_t *cb, void **vaddr)
 {
-    if (cb->fd < 0 || cb->ashmemSize <= 0) {
+    if (cb->bufferFd < 0) {
         return -EINVAL;
     }
 
-    void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
-                      MAP_SHARED, cb->fd, 0);
+    void *addr = mmap(0, cb->bufferSize, PROT_READ | PROT_WRITE,
+                      MAP_SHARED, cb->bufferFd, 0);
     if (addr == MAP_FAILED) {
         ALOGE("%s: failed to map ashmem region!", __FUNCTION__);
         return -errno;
     }
 
-    cb->ashmemBase = intptr_t(addr);
+    cb->setBufferPtr(addr);
     cb->ashmemBasePid = getpid();
     D("%s: %p mapped ashmem base %p size %d\n", __FUNCTION__,
-      cb, cb->ashmemBase, cb->ashmemSize);
+      cb, addr, cb->bufferSize);
 
     *vaddr = addr;
     return 0;
@@ -788,7 +851,6 @@
             delete cb;
             return err;
         }
-        cb->setFd(fd);
     }
 
     const bool hasDMA = has_DMA_support(rcEnc);
@@ -865,20 +927,20 @@
 {
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
-    if (!cb_handle_old_t::validate((cb_handle_old_t*)cb)) {
-        ERR("gralloc_free: invalid handle");
+    const cb_handle_old_t *cb = cb_handle_old_t::from(handle);
+    if (!cb) {
+        ERR("gralloc_free: invalid handle %p", handle);
         return -EINVAL;
     }
 
     D("%s: for buf %p ptr %p size %d\n",
-      __FUNCTION__, handle, cb->ashmemBase, cb->ashmemSize);
+      __FUNCTION__, handle, cb->getBufferPtr(), cb->bufferSize);
 
     if (cb->hostHandle && !cb->hasRefcountPipe()) {
         int32_t openCount = 1;
         int32_t* openCountPtr = &openCount;
 
-        if (isHidlGralloc && cb->ashmemBase) {
+        if (isHidlGralloc && cb->getBufferPtr()) {
             openCountPtr = getOpenCountPtr(cb);
         }
 
@@ -896,17 +958,17 @@
     //
     // detach and unmap ashmem area if present
     //
-    if (cb->fd > 0) {
-        if (cb->ashmemSize > 0 && cb->ashmemBase) {
-            D("%s: unmapped %p", __FUNCTION__, cb->ashmemBase);
-            munmap((void *)cb->ashmemBase, cb->ashmemSize);
-            put_gralloc_region(rcEnc, cb->ashmemSize);
+    if (cb->bufferFd > 0) {
+        if (cb->bufferSize > 0 && cb->getBufferPtr()) {
+            D("%s: unmapped %p", __FUNCTION__, cb->getBufferPtr());
+            munmap(cb->getBufferPtr(), cb->bufferSize);
+            put_gralloc_region(rcEnc, cb->bufferSize);
         }
-        close(cb->fd);
+        close(cb->bufferFd);
     }
 
-    if(qemu_pipe_valid(cb->refcount_pipe_fd)) {
-        qemu_pipe_close(cb->refcount_pipe_fd);
+    if(qemu_pipe_valid(cb->hostHandleRefCountFd)) {
+        qemu_pipe_close(cb->hostHandleRefCountFd);
     }
     D("%s: done", __FUNCTION__);
     // remove it from the allocated list
@@ -949,9 +1011,14 @@
 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
 {
     fb_device_t *fbdev = (fb_device_t *)dev;
-    cb_handle_old_t *cb = (cb_handle_old_t *)buffer;
-
-    if (!fbdev || !cb_handle_old_t::validate(cb) || !cb->canBePosted()) {
+    if (!fbdev) {
+        return -EINVAL;
+    }
+    const cb_handle_old_t *cb = cb_handle_old_t::from(buffer);
+    if (!cb) {
+        return -EINVAL;
+    }
+    if (!cb->canBePosted()) {
         return -EINVAL;
     }
 
@@ -959,7 +1026,7 @@
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
     // increment the post count of the buffer
-    intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
+    int32_t *postCountPtr = (int32_t *)cb->getBufferPtr();
     if (!postCountPtr) {
         // This should not happen
         return -EINVAL;
@@ -1047,9 +1114,12 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
+    if (!gr) {
+        return -EINVAL;
+    }
 
-    if (!gr || !cb_handle_old_t::validate(cb)) {
+    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
+    if (!cb) {
         ERR("gralloc_register_buffer(%p): invalid buffer", cb);
         return -EINVAL;
     }
@@ -1068,7 +1138,7 @@
     // if the color buffer has ashmem region and it is not mapped in this
     // process map it now.
     //
-    if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
+    if (cb->bufferSize > 0 && cb->mappedPid != getpid()) {
         void *vaddr;
         int err = map_buffer(cb, &vaddr);
         if (err) {
@@ -1083,7 +1153,7 @@
         }
     }
 
-    if (cb->ashmemSize > 0) {
+    if (cb->bufferSize > 0) {
         get_ashmem_region(rcEnc, cb);
     }
 
@@ -1100,9 +1170,12 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
+    if (!gr) {
+        return -EINVAL;
+    }
 
-    if (!gr || !cb_handle_old_t::validate(cb)) {
+    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
+    if (!cb) {
         ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
         return -EINVAL;
     }
@@ -1116,7 +1189,7 @@
         if (isHidlGralloc) {
             // Queue up another rcCloseColorBuffer if applicable.
             // invariant: have ashmem.
-            if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
+            if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
                 int32_t* openCountPtr = getOpenCountPtr(cb);
                 if (*openCountPtr == -1) {
                     D("%s: revenge of the rcCloseColorBuffer!", __func__);
@@ -1132,16 +1205,16 @@
     // unmap ashmem region if it was previously mapped in this process
     // (through register_buffer)
     //
-    if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
+    if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
         const bool should_unmap = put_ashmem_region(rcEnc, cb);
         if (!should_unmap) goto done;
 
-        int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
+        int err = munmap(cb->getBufferPtr(), cb->bufferSize);
         if (err) {
             ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
             return -EINVAL;
         }
-        cb->ashmemBase = 0;
+        cb->bufferSize = 0;
         cb->mappedPid = 0;
         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
     }
@@ -1161,9 +1234,12 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
+    if (!gr) {
+        return -EINVAL;
+    }
 
-    if (!gr || !cb_handle_old_t::validate(cb)) {
+    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
+    if (!cb) {
         ALOGE("gralloc_lock bad handle\n");
         return -EINVAL;
     }
@@ -1223,11 +1299,11 @@
     if (cb->canBePosted() || sw_read || sw_write ||
             hw_cam_write || hw_cam_read ||
             hw_vid_enc_read) {
-        if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
+        if (cb->ashmemBasePid != getpid() || !cb->getBufferPtr()) {
             return -EACCES;
         }
 
-        cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
+        cpu_addr = (void *)((char*)cb->getBufferPtr() + getAshmemColorOffset(cb));
     }
 
     if (cb->hostHandle) {
@@ -1291,7 +1367,7 @@
         }
 
         if (has_DMA_support(rcEnc)) {
-            gralloc_dmaregion_register_ashmem(rcEnc, cb->ashmemSize);
+            gralloc_dmaregion_register_ashmem(rcEnc, cb->bufferSize);
         }
         hostCon->unlock();
     }
@@ -1327,10 +1403,13 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
+    if (!gr) {
+        return -EINVAL;
+    }
 
-    if (!gr || !cb_handle_old_t::validate(cb)) {
-        ALOGD("%s: invalid gr or cb handle. -EINVAL", __FUNCTION__);
+    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
+    if (!cb) {
+        ALOGD("%s: invalid cb handle. -EINVAL", __FUNCTION__);
         return -EINVAL;
     }
 
@@ -1344,14 +1423,13 @@
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         hostCon->lock();
 
-        void *cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
+        char *cpu_addr = (char*)cb->getBufferPtr() + getAshmemColorOffset(cb);
 
-        char* rgb_addr = (char *)cpu_addr;
         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
-            updateHostColorBuffer(cb, true, rgb_addr);
+            updateHostColorBuffer(cb, true, cpu_addr);
         }
         else {
-            updateHostColorBuffer(cb, false, rgb_addr);
+            updateHostColorBuffer(cb, false, cpu_addr);
         }
 
         hostCon->unlock();
@@ -1380,8 +1458,12 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
-    if (!gr || !cb_handle_old_t::validate(cb)) {
+    if (!gr) {
+        return -EINVAL;
+    }
+
+    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
+    if (!cb) {
         ALOGE("%s: bad colorbuffer handle. -EINVAL", __FUNCTION__);
         return -EINVAL;
     }
diff --git a/system/hwc2/EmuHWC2.cpp b/system/hwc2/EmuHWC2.cpp
index 0f5c631..edf6397 100644
--- a/system/hwc2/EmuHWC2.cpp
+++ b/system/hwc2/EmuHWC2.cpp
@@ -392,8 +392,7 @@
     if (mHandle != nullptr) {
         mGralloc->unregisterBuffer(mGralloc, mHandle);
         mAllocDev->free(mAllocDev, mHandle);
-        ALOGI("free targetCb %u",
-            cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
+        ALOGI("free targetCb %u", cb_handle_t::from(mHandle)->hostHandle);
     }
 }
 
@@ -408,9 +407,9 @@
                                &mHandle, &stride);
         assert(ret == 0 && "Fail to allocate target ColorBuffer");
         mGralloc->registerBuffer(mGralloc, mHandle);
-        ALOGI("targetCb %u", cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
+        ALOGI("targetCb %u", cb_handle_t::from(mHandle)->hostHandle);
     }
-    return cb_handle_t::from_raw_pointer(mHandle)->hostHandle;
+    return cb_handle_t::from(mHandle)->hostHandle;
 }
 
 // Display functions
@@ -814,7 +813,7 @@
                           __FUNCTION__, (uint32_t)layer->getId());
                 }
                 const cb_handle_t *cb =
-                    cb_handle_t::from_raw_pointer(layer->getLayerBuffer().getBuffer());
+                    cb_handle_t::from(layer->getLayerBuffer().getBuffer());
                 if (cb != nullptr) {
                     l->cbHandle = cb->hostHandle;
                 }
@@ -924,7 +923,7 @@
         int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) {
     ALOGVV("%s", __FUNCTION__);
 
-    const cb_handle_t *cb = cb_handle_t::from_raw_pointer(target);
+    const cb_handle_t *cb = cb_handle_t::from(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 e2f2ad1..0a71d6d 100644
--- a/system/vulkan_enc/AndroidHardwareBuffer.cpp
+++ b/system/vulkan_enc/AndroidHardwareBuffer.cpp
@@ -111,9 +111,12 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(buffer);
-    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
-    uint32_t colorBufferHandle = cb_handle->hostHandle;
+    const cb_handle_t* cb_handle = cb_handle_t::from(handle);
+    if (!cb_handle) {
+        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+    }
 
+    uint32_t colorBufferHandle = cb_handle->hostHandle;
     if (!colorBufferHandle) {
         return VK_ERROR_INVALID_EXTERNAL_HANDLE;
     }
@@ -128,7 +131,7 @@
     }
 
     pProperties->memoryTypeBits = memoryTypeBits;
-    pProperties->allocationSize = cb_handle->allocationSize();
+    pProperties->allocationSize = cb_handle->allocatedSize();
 
     return VK_SUCCESS;
 }
@@ -164,9 +167,12 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(info->buffer);
-    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
-    uint32_t colorBufferHandle = cb_handle->hostHandle;
+    const cb_handle_t* cb_handle = cb_handle_t::from(handle);
+    if (!cb_handle) {
+        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
+    }
 
+    uint32_t colorBufferHandle = cb_handle->hostHandle;
     if (!colorBufferHandle) {
         return VK_ERROR_INVALID_EXTERNAL_HANDLE;
     }
diff --git a/system/vulkan_enc/ResourceTracker.cpp b/system/vulkan_enc/ResourceTracker.cpp
index 98bf049..363d3b9 100644
--- a/system/vulkan_enc/ResourceTracker.cpp
+++ b/system/vulkan_enc/ResourceTracker.cpp
@@ -1836,7 +1836,7 @@
             ALOGD("%s: Import AHardwareBulffer", __func__);
             const native_handle_t *handle =
                 AHardwareBuffer_getNativeHandle(ahw);
-            const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
+            const cb_handle_t* cb_handle = cb_handle_t::from(handle);
             importCbInfo.colorBuffer = cb_handle->hostHandle;
             vk_append_struct(&structChainIter, &importCbInfo);
         }
@@ -3463,9 +3463,7 @@
             return;
         }
 
-        const cb_handle_t* cb_handle =
-            cb_handle_t::from_raw_pointer(nativeInfo->handle);
-
+        const cb_handle_t* cb_handle = cb_handle_t::from(nativeInfo->handle);
         if (!cb_handle) return;
 
         VkNativeBufferANDROID* nativeInfoOut =