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 <cutils/native_handle.h> 21 #include <qemu_pipe_types_bp.h> 22 23 #include <cinttypes> 24 25 const uint32_t CB_HANDLE_MAGIC_MASK = 0xFFFFFFF0; 26 const uint32_t CB_HANDLE_MAGIC_BASE = 0xABFABFA0; 27 28 #define CB_HANDLE_NUM_INTS(nfd) \ 29 ((sizeof(*this)-sizeof(native_handle_t)-nfd*sizeof(int32_t))/sizeof(int32_t)) 30 31 struct cb_handle_t : public native_handle_t { cb_handle_tcb_handle_t32 cb_handle_t(uint32_t p_magic, 33 uint32_t p_hostHandle, 34 int32_t p_format, 35 uint32_t p_drmformat, 36 uint32_t p_stride, 37 uint32_t p_bufSize, 38 uint64_t p_mmapedOffset) 39 : magic(p_magic), 40 hostHandle(p_hostHandle), 41 format(p_format), 42 drmformat(p_drmformat), 43 bufferSize(p_bufSize), 44 stride(p_stride), 45 mmapedOffsetLo(static_cast<uint32_t>(p_mmapedOffset)), 46 mmapedOffsetHi(static_cast<uint32_t>(p_mmapedOffset >> 32)) { 47 version = sizeof(native_handle); 48 } 49 getMmapedOffsetcb_handle_t50 uint64_t getMmapedOffset() const { 51 return (uint64_t(mmapedOffsetHi) << 32) | mmapedOffsetLo; 52 } 53 allocatedSizecb_handle_t54 uint32_t allocatedSize() const { 55 return bufferSize; 56 } 57 isValidcb_handle_t58 bool isValid() const { 59 return (version == sizeof(native_handle)) 60 && (magic & CB_HANDLE_MAGIC_MASK) == CB_HANDLE_MAGIC_BASE; 61 } 62 fromcb_handle_t63 static cb_handle_t* from(void* p) { 64 if (!p) { return NULL; } 65 cb_handle_t* cb = static_cast<cb_handle_t*>(p); 66 return cb->isValid() ? cb : NULL; 67 } 68 fromcb_handle_t69 static const cb_handle_t* from(const void* p) { 70 return from(const_cast<void*>(p)); 71 } 72 from_unconstcb_handle_t73 static cb_handle_t* from_unconst(const void* p) { 74 return from(const_cast<void*>(p)); 75 } 76 77 int32_t fds[2]; 78 79 // ints 80 uint32_t magic; // magic number in order to validate a pointer 81 uint32_t hostHandle; // the host reference to this buffer 82 uint32_t format; // real internal pixel format format 83 uint32_t drmformat; // drm format 84 uint32_t bufferSize; 85 uint32_t stride; 86 uint32_t mmapedOffsetLo; 87 uint32_t mmapedOffsetHi; 88 }; 89 90 #endif //__GRALLOC_CB_H__ 91