Revert "Create a buffer independent cb_handle_t base class"

This reverts commit 1c1504300cb26896287058103327099c7c5771d3.

Reason for revert: error: 'nullptr' was not declared in this scope

Change-Id: I8cfbaf9182f6d15f8d2d2560f47360f43ceb47ac
diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index 3167ca0..2d80f26 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -17,103 +17,7 @@
 #ifndef __GRALLOC_CB_H__
 #define __GRALLOC_CB_H__
 
-#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;
-};
+#include "gralloc_cb_old.h"
+typedef cb_handle_old_t cb_handle_t;
 
 #endif //__GRALLOC_CB_H__
diff --git a/shared/OpenglCodecCommon/gralloc_cb_common.h b/shared/OpenglCodecCommon/gralloc_cb_common.h
new file mode 100644
index 0000000..94ada2f
--- /dev/null
+++ b/shared/OpenglCodecCommon/gralloc_cb_common.h
@@ -0,0 +1,28 @@
+/*
+* 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
new file mode 100644
index 0000000..1c14579
--- /dev/null
+++ b/shared/OpenglCodecCommon/gralloc_cb_old.h
@@ -0,0 +1,146 @@
+/*
+* 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 ba059a4..a8e02bd 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(handle)->hostHandle;
+        return cb_handle_t::from_native_handle(handle)->hostHandle;
     }
 
     int getFormat(native_handle_t const* handle)
     {
-        return cb_handle_t::from(handle)->format;
+        return cb_handle_t::from_native_handle(handle)->format;
     }
 };
 
diff --git a/system/gralloc/gralloc_common.h b/system/gralloc/gralloc_common.h
index 6dba9cd..574e659 100644
--- a/system/gralloc/gralloc_common.h
+++ b/system/gralloc/gralloc_common.h
@@ -17,14 +17,6 @@
 #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 6ab38a2..eecf698 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,7 +13,6 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
-
 #include <string.h>
 #include <pthread.h>
 #include <limits.h>
@@ -22,12 +21,11 @@
 #include <errno.h>
 #include <dlfcn.h>
 #include <sys/mman.h>
-#include <hardware/gralloc.h>
 
 #if PLATFORM_SDK_VERSION < 28
-#include "gralloc_cb.h"
+#include "gralloc_cb_old.h"
 #else
-#include "../../shared/OpenglCodecCommon/gralloc_cb.h"
+#include "../../shared/OpenglCodecCommon/gralloc_cb_old.h"
 #endif
 
 #include "gralloc_common.h"
@@ -83,69 +81,8 @@
 static const bool isHidlGralloc = false;
 #endif
 
-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;
+int32_t* getOpenCountPtr(cb_handle_old_t* cb) {
+    return ((int32_t*)cb->ashmemBase) + 1;
 }
 
 uint32_t getAshmemColorOffset(cb_handle_old_t* cb) {
@@ -394,7 +331,7 @@
     dump_regions(rcEnc);
 #endif
 
-    get_mem_region(cb->getBufferPtr());
+    get_mem_region((void*)cb->ashmemBase);
 
 #if DEBUG
     dump_regions(rcEnc);
@@ -408,13 +345,13 @@
     dump_regions(rcEnc);
 #endif
 
-    const bool should_unmap = put_mem_region(rcEnc, cb->getBufferPtr());
+    const bool should_unmap = put_mem_region(rcEnc, (void*)cb->ashmemBase);
 
 #if DEBUG
     dump_regions(rcEnc);
 #endif
 
-    put_gralloc_region(rcEnc, cb->bufferSize);
+    put_gralloc_region(rcEnc, cb->ashmemSize);
 
     return should_unmap;
 }
@@ -428,21 +365,21 @@
 
 static int map_buffer(cb_handle_old_t *cb, void **vaddr)
 {
-    if (cb->bufferFd < 0) {
+    if (cb->fd < 0 || cb->ashmemSize <= 0) {
         return -EINVAL;
     }
 
-    void *addr = mmap(0, cb->bufferSize, PROT_READ | PROT_WRITE,
-                      MAP_SHARED, cb->bufferFd, 0);
+    void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE,
+                      MAP_SHARED, cb->fd, 0);
     if (addr == MAP_FAILED) {
         ALOGE("%s: failed to map ashmem region!", __FUNCTION__);
         return -errno;
     }
 
-    cb->setBufferPtr(addr);
+    cb->ashmemBase = intptr_t(addr);
     cb->ashmemBasePid = getpid();
     D("%s: %p mapped ashmem base %p size %d\n", __FUNCTION__,
-      cb, addr, cb->bufferSize);
+      cb, cb->ashmemBase, cb->ashmemSize);
 
     *vaddr = addr;
     return 0;
@@ -851,6 +788,7 @@
             delete cb;
             return err;
         }
+        cb->setFd(fd);
     }
 
     const bool hasDMA = has_DMA_support(rcEnc);
@@ -927,20 +865,20 @@
 {
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
-    const cb_handle_old_t *cb = cb_handle_old_t::from(handle);
-    if (!cb) {
-        ERR("gralloc_free: invalid handle %p", handle);
+    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");
         return -EINVAL;
     }
 
     D("%s: for buf %p ptr %p size %d\n",
-      __FUNCTION__, handle, cb->getBufferPtr(), cb->bufferSize);
+      __FUNCTION__, handle, cb->ashmemBase, cb->ashmemSize);
 
     if (cb->hostHandle && !cb->hasRefcountPipe()) {
         int32_t openCount = 1;
         int32_t* openCountPtr = &openCount;
 
-        if (isHidlGralloc && cb->getBufferPtr()) {
+        if (isHidlGralloc && cb->ashmemBase) {
             openCountPtr = getOpenCountPtr(cb);
         }
 
@@ -958,17 +896,17 @@
     //
     // detach and unmap ashmem area if present
     //
-    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);
+    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);
         }
-        close(cb->bufferFd);
+        close(cb->fd);
     }
 
-    if(qemu_pipe_valid(cb->hostHandleRefCountFd)) {
-        qemu_pipe_close(cb->hostHandleRefCountFd);
+    if(qemu_pipe_valid(cb->refcount_pipe_fd)) {
+        qemu_pipe_close(cb->refcount_pipe_fd);
     }
     D("%s: done", __FUNCTION__);
     // remove it from the allocated list
@@ -1011,14 +949,9 @@
 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
 {
     fb_device_t *fbdev = (fb_device_t *)dev;
-    if (!fbdev) {
-        return -EINVAL;
-    }
-    const cb_handle_old_t *cb = cb_handle_old_t::from(buffer);
-    if (!cb) {
-        return -EINVAL;
-    }
-    if (!cb->canBePosted()) {
+    cb_handle_old_t *cb = (cb_handle_old_t *)buffer;
+
+    if (!fbdev || !cb_handle_old_t::validate(cb) || !cb->canBePosted()) {
         return -EINVAL;
     }
 
@@ -1026,7 +959,7 @@
     DEFINE_AND_VALIDATE_HOST_CONNECTION;
 
     // increment the post count of the buffer
-    int32_t *postCountPtr = (int32_t *)cb->getBufferPtr();
+    intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase;
     if (!postCountPtr) {
         // This should not happen
         return -EINVAL;
@@ -1114,12 +1047,9 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    if (!gr) {
-        return -EINVAL;
-    }
+    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
 
-    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
-    if (!cb) {
+    if (!gr || !cb_handle_old_t::validate(cb)) {
         ERR("gralloc_register_buffer(%p): invalid buffer", cb);
         return -EINVAL;
     }
@@ -1138,7 +1068,7 @@
     // if the color buffer has ashmem region and it is not mapped in this
     // process map it now.
     //
-    if (cb->bufferSize > 0 && cb->mappedPid != getpid()) {
+    if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) {
         void *vaddr;
         int err = map_buffer(cb, &vaddr);
         if (err) {
@@ -1153,7 +1083,7 @@
         }
     }
 
-    if (cb->bufferSize > 0) {
+    if (cb->ashmemSize > 0) {
         get_ashmem_region(rcEnc, cb);
     }
 
@@ -1170,12 +1100,9 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    if (!gr) {
-        return -EINVAL;
-    }
+    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
 
-    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
-    if (!cb) {
+    if (!gr || !cb_handle_old_t::validate(cb)) {
         ERR("gralloc_unregister_buffer(%p): invalid buffer", cb);
         return -EINVAL;
     }
@@ -1189,7 +1116,7 @@
         if (isHidlGralloc) {
             // Queue up another rcCloseColorBuffer if applicable.
             // invariant: have ashmem.
-            if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
+            if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
                 int32_t* openCountPtr = getOpenCountPtr(cb);
                 if (*openCountPtr == -1) {
                     D("%s: revenge of the rcCloseColorBuffer!", __func__);
@@ -1205,16 +1132,16 @@
     // unmap ashmem region if it was previously mapped in this process
     // (through register_buffer)
     //
-    if (cb->bufferSize > 0 && cb->mappedPid == getpid()) {
+    if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) {
         const bool should_unmap = put_ashmem_region(rcEnc, cb);
         if (!should_unmap) goto done;
 
-        int err = munmap(cb->getBufferPtr(), cb->bufferSize);
+        int err = munmap((void *)cb->ashmemBase, cb->ashmemSize);
         if (err) {
             ERR("gralloc_unregister_buffer(%p): unmap failed", cb);
             return -EINVAL;
         }
-        cb->bufferSize = 0;
+        cb->ashmemBase = 0;
         cb->mappedPid = 0;
         D("%s: Unregister buffer previous mapped to pid %d", __FUNCTION__, getpid());
     }
@@ -1234,12 +1161,9 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    if (!gr) {
-        return -EINVAL;
-    }
+    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
 
-    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
-    if (!cb) {
+    if (!gr || !cb_handle_old_t::validate(cb)) {
         ALOGE("gralloc_lock bad handle\n");
         return -EINVAL;
     }
@@ -1299,11 +1223,11 @@
     if (cb->canBePosted() || sw_read || sw_write ||
             hw_cam_write || hw_cam_read ||
             hw_vid_enc_read) {
-        if (cb->ashmemBasePid != getpid() || !cb->getBufferPtr()) {
+        if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) {
             return -EACCES;
         }
 
-        cpu_addr = (void *)((char*)cb->getBufferPtr() + getAshmemColorOffset(cb));
+        cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
     }
 
     if (cb->hostHandle) {
@@ -1367,7 +1291,7 @@
         }
 
         if (has_DMA_support(rcEnc)) {
-            gralloc_dmaregion_register_ashmem(rcEnc, cb->bufferSize);
+            gralloc_dmaregion_register_ashmem(rcEnc, cb->ashmemSize);
         }
         hostCon->unlock();
     }
@@ -1403,13 +1327,10 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    if (!gr) {
-        return -EINVAL;
-    }
+    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
 
-    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
-    if (!cb) {
-        ALOGD("%s: invalid cb handle. -EINVAL", __FUNCTION__);
+    if (!gr || !cb_handle_old_t::validate(cb)) {
+        ALOGD("%s: invalid gr or cb handle. -EINVAL", __FUNCTION__);
         return -EINVAL;
     }
 
@@ -1423,13 +1344,14 @@
         DEFINE_AND_VALIDATE_HOST_CONNECTION;
         hostCon->lock();
 
-        char *cpu_addr = (char*)cb->getBufferPtr() + getAshmemColorOffset(cb);
+        void *cpu_addr = (void *)(cb->ashmemBase + getAshmemColorOffset(cb));
 
+        char* rgb_addr = (char *)cpu_addr;
         if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) {
-            updateHostColorBuffer(cb, true, cpu_addr);
+            updateHostColorBuffer(cb, true, rgb_addr);
         }
         else {
-            updateHostColorBuffer(cb, false, cpu_addr);
+            updateHostColorBuffer(cb, false, rgb_addr);
         }
 
         hostCon->unlock();
@@ -1458,12 +1380,8 @@
     }
 
     private_module_t *gr = (private_module_t *)module;
-    if (!gr) {
-        return -EINVAL;
-    }
-
-    cb_handle_old_t *cb = cb_handle_old_t::from_unconst(handle);
-    if (!cb) {
+    cb_handle_old_t *cb = (cb_handle_old_t *)handle;
+    if (!gr || !cb_handle_old_t::validate(cb)) {
         ALOGE("%s: bad colorbuffer handle. -EINVAL", __FUNCTION__);
         return -EINVAL;
     }
diff --git a/system/hwc2/EmuHWC2.cpp b/system/hwc2/EmuHWC2.cpp
index edf6397..0f5c631 100644
--- a/system/hwc2/EmuHWC2.cpp
+++ b/system/hwc2/EmuHWC2.cpp
@@ -392,7 +392,8 @@
     if (mHandle != nullptr) {
         mGralloc->unregisterBuffer(mGralloc, mHandle);
         mAllocDev->free(mAllocDev, mHandle);
-        ALOGI("free targetCb %u", cb_handle_t::from(mHandle)->hostHandle);
+        ALOGI("free targetCb %u",
+            cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
     }
 }
 
@@ -407,9 +408,9 @@
                                &mHandle, &stride);
         assert(ret == 0 && "Fail to allocate target ColorBuffer");
         mGralloc->registerBuffer(mGralloc, mHandle);
-        ALOGI("targetCb %u", cb_handle_t::from(mHandle)->hostHandle);
+        ALOGI("targetCb %u", cb_handle_t::from_raw_pointer(mHandle)->hostHandle);
     }
-    return cb_handle_t::from(mHandle)->hostHandle;
+    return cb_handle_t::from_raw_pointer(mHandle)->hostHandle;
 }
 
 // Display functions
@@ -813,7 +814,7 @@
                           __FUNCTION__, (uint32_t)layer->getId());
                 }
                 const cb_handle_t *cb =
-                    cb_handle_t::from(layer->getLayerBuffer().getBuffer());
+                    cb_handle_t::from_raw_pointer(layer->getLayerBuffer().getBuffer());
                 if (cb != nullptr) {
                     l->cbHandle = cb->hostHandle;
                 }
@@ -923,7 +924,7 @@
         int32_t acquireFence, int32_t /*dataspace*/, hwc_region_t /*damage*/) {
     ALOGVV("%s", __FUNCTION__);
 
-    const cb_handle_t *cb = cb_handle_t::from(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 0a71d6d..e2f2ad1 100644
--- a/system/vulkan_enc/AndroidHardwareBuffer.cpp
+++ b/system/vulkan_enc/AndroidHardwareBuffer.cpp
@@ -111,12 +111,9 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(buffer);
-    const cb_handle_t* cb_handle = cb_handle_t::from(handle);
-    if (!cb_handle) {
-        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
-    }
-
+    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
     uint32_t colorBufferHandle = cb_handle->hostHandle;
+
     if (!colorBufferHandle) {
         return VK_ERROR_INVALID_EXTERNAL_HANDLE;
     }
@@ -131,7 +128,7 @@
     }
 
     pProperties->memoryTypeBits = memoryTypeBits;
-    pProperties->allocationSize = cb_handle->allocatedSize();
+    pProperties->allocationSize = cb_handle->allocationSize();
 
     return VK_SUCCESS;
 }
@@ -167,12 +164,9 @@
 
     const native_handle_t *handle =
        AHardwareBuffer_getNativeHandle(info->buffer);
-    const cb_handle_t* cb_handle = cb_handle_t::from(handle);
-    if (!cb_handle) {
-        return VK_ERROR_INVALID_EXTERNAL_HANDLE;
-    }
-
+    const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(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 363d3b9..98bf049 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(handle);
+            const cb_handle_t* cb_handle = cb_handle_t::from_native_handle(handle);
             importCbInfo.colorBuffer = cb_handle->hostHandle;
             vk_append_struct(&structChainIter, &importCbInfo);
         }
@@ -3463,7 +3463,9 @@
             return;
         }
 
-        const cb_handle_t* cb_handle = cb_handle_t::from(nativeInfo->handle);
+        const cb_handle_t* cb_handle =
+            cb_handle_t::from_raw_pointer(nativeInfo->handle);
+
         if (!cb_handle) return;
 
         VkNativeBufferANDROID* nativeInfoOut =