1 /*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 #ifndef __GRALLOC_CB_H__
18 #define __GRALLOC_CB_H__
19 
20 #include <cinttypes>
21 #include <cutils/native_handle.h>
22 
23 static_assert(sizeof(int) == sizeof(int32_t));
24 static_assert(sizeof(uint64_t) >= sizeof(uintptr_t));
25 
26 struct cb_handle_t : public native_handle_t {
27     static constexpr uint32_t kCbHandleMagic = 0xABFABF05;
28 
cb_handle_tcb_handle_t29     cb_handle_t(int p_bufferFd,
30                 int p_hostHandleRefCountFd,
31                 uint32_t p_hostHandle,
32                 uint64_t p_usage,
33                 uint32_t p_format, uint32_t p_drmformat,
34                 uint32_t p_stride, uint32_t p_bufSize,
35                 void* p_bufPtr, uint32_t p_mmapedSize, uint64_t p_mmapedOffset,
36                 uint32_t p_externalMetadataOffset)
37         : bufferFd(p_bufferFd),
38           hostHandleRefcountFd(p_hostHandleRefCountFd),
39           usage(p_usage),
40           mmapedOffset(p_mmapedOffset),
41           bufferPtr64(reinterpret_cast<uintptr_t>(p_bufPtr)),
42           magic(kCbHandleMagic),
43           hostHandle(p_hostHandle),
44           format(p_format),
45           drmformat(p_drmformat),
46           mmapedSize(p_mmapedSize),
47           bufferSize(p_bufSize),
48           externalMetadataOffset(p_externalMetadataOffset),
49           stride(p_stride) {
50         version = sizeof(native_handle);
51         numFds = (p_hostHandleRefCountFd >= 0) ? 2 : 1;
52         numInts = (sizeof(*this) - sizeof(native_handle_t) - numFds * sizeof(int)) / sizeof(int);
53     }
54 
getMmapedOffsetcb_handle_t55     uint64_t getMmapedOffset() const {
56         return mmapedOffset;
57     }
58 
allocatedSizecb_handle_t59     uint32_t allocatedSize() const {
60         return bufferSize;
61     }
62 
getBufferPtrcb_handle_t63     char* getBufferPtr() const {
64         return reinterpret_cast<char*>(static_cast<uintptr_t>(bufferPtr64));
65     }
66 
setBufferPtrcb_handle_t67     void setBufferPtr(void* ptr) {
68         bufferPtr64 = reinterpret_cast<uintptr_t>(ptr);
69     }
70 
isValidcb_handle_t71     bool isValid() const {
72         return (version == sizeof(native_handle_t)) &&
73                (sizeof(*this) == ((numFds + numInts) * sizeof(int) + sizeof(native_handle_t))) &&
74                (magic == kCbHandleMagic);
75     }
76 
fromcb_handle_t77     static cb_handle_t* from(void* p) {
78         if (!p) { return NULL; }
79         cb_handle_t* cb = static_cast<cb_handle_t*>(p);
80         return cb->isValid() ? cb : NULL;
81     }
82 
fromcb_handle_t83     static const cb_handle_t* from(const void* p) {
84         return from(const_cast<void*>(p));
85     }
86 
from_unconstcb_handle_t87     static cb_handle_t* from_unconst(const void* p) {
88         return from(const_cast<void*>(p));
89     }
90 
91     const int32_t bufferFd;       // always allocated
92     const int32_t hostHandleRefcountFd;  // optional
93 
94     // ints
95     const uint64_t usage;         // allocation usage
96     const uint64_t mmapedOffset;
97     uint64_t       bufferPtr64;
98     const uint32_t magic;         // magic number in order to validate a pointer
99     const uint32_t hostHandle;    // the host reference to this buffer
100     const uint32_t format;        // real internal pixel format format
101     const uint32_t drmformat;     // drm format
102     const uint32_t mmapedSize;    // real allocation side
103     const uint32_t bufferSize;
104     const uint32_t externalMetadataOffset; // relative to bufferPtr
105     const uint32_t stride;
106     uint8_t        lockedUsage;
107     uint8_t        unused[3];
108 };
109 
110 #endif //__GRALLOC_CB_H__
111