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_stride, 36 uint32_t p_bufSize, 37 uint64_t p_mmapedOffset) 38 : magic(p_magic), 39 hostHandle(p_hostHandle), 40 format(p_format), 41 bufferSize(p_bufSize), 42 stride(p_stride), 43 mmapedOffsetLo(static_cast<uint32_t>(p_mmapedOffset)), 44 mmapedOffsetHi(static_cast<uint32_t>(p_mmapedOffset >> 32)) { 45 version = sizeof(native_handle); 46 } 47 getMmapedOffsetcb_handle_t48 uint64_t getMmapedOffset() const { 49 return (uint64_t(mmapedOffsetHi) << 32) | mmapedOffsetLo; 50 } 51 allocatedSizecb_handle_t52 uint32_t allocatedSize() const { 53 return bufferSize; 54 } 55 isValidcb_handle_t56 bool isValid() const { 57 return (version == sizeof(native_handle)) 58 && (magic & CB_HANDLE_MAGIC_MASK) == CB_HANDLE_MAGIC_BASE; 59 } 60 fromcb_handle_t61 static cb_handle_t* from(void* p) { 62 if (!p) { return NULL; } 63 cb_handle_t* cb = static_cast<cb_handle_t*>(p); 64 return cb->isValid() ? cb : NULL; 65 } 66 fromcb_handle_t67 static const cb_handle_t* from(const void* p) { 68 return from(const_cast<void*>(p)); 69 } 70 from_unconstcb_handle_t71 static cb_handle_t* from_unconst(const void* p) { 72 return from(const_cast<void*>(p)); 73 } 74 75 int32_t fds[2]; 76 77 // ints 78 uint32_t magic; // magic number in order to validate a pointer 79 uint32_t hostHandle; // the host reference to this buffer 80 uint32_t format; // real internal pixel format format 81 uint32_t bufferSize; 82 uint32_t stride; 83 uint32_t mmapedOffsetLo; 84 uint32_t mmapedOffsetHi; 85 }; 86 87 #endif //__GRALLOC_CB_H__ 88