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 const uint32_t CB_HANDLE_MAGIC_MASK = 0xFFFFFFF0; 24 const uint32_t CB_HANDLE_MAGIC_BASE = 0xABFABFA0; 25 26 #define CB_HANDLE_NUM_INTS(nfd) \ 27 ((sizeof(*this)-sizeof(native_handle_t)-nfd*sizeof(int32_t))/sizeof(int32_t)) 28 29 struct cb_handle_t : public native_handle_t { cb_handle_tcb_handle_t30 cb_handle_t(int32_t p_bufferFd, 31 QEMU_PIPE_HANDLE p_hostHandleRefCountFd, 32 uint32_t p_magic, 33 uint32_t p_hostHandle, 34 int32_t p_usage, 35 int32_t p_width, 36 int32_t p_height, 37 int32_t p_format, 38 int32_t p_glFormat, 39 int32_t p_glType, 40 uint32_t p_bufSize, 41 void* p_bufPtr, 42 uint64_t p_mmapedOffset) 43 : bufferFd(p_bufferFd), 44 hostHandleRefCountFd(p_hostHandleRefCountFd), 45 magic(p_magic), 46 hostHandle(p_hostHandle), 47 usage(p_usage), 48 width(p_width), 49 height(p_height), 50 format(p_format), 51 glFormat(p_glFormat), 52 glType(p_glType), 53 bufferSize(p_bufSize), 54 mmapedOffsetLo(static_cast<uint32_t>(p_mmapedOffset)), 55 mmapedOffsetHi(static_cast<uint32_t>(p_mmapedOffset >> 32)), 56 lockedLeft(0), 57 lockedTop(0), 58 lockedWidth(0), 59 lockedHeight(0) { 60 version = sizeof(native_handle); 61 numFds = ((bufferFd >= 0) ? 1 : 0) + (qemu_pipe_valid(hostHandleRefCountFd) ? 1 : 0); 62 numInts = 0; // has to be overwritten in children classes 63 setBufferPtr(p_bufPtr); 64 } 65 getBufferPtrcb_handle_t66 void* getBufferPtr() const { 67 const uint64_t addr = (uint64_t(bufferPtrHi) << 32) | bufferPtrLo; 68 return reinterpret_cast<void*>(static_cast<uintptr_t>(addr)); 69 } 70 setBufferPtrcb_handle_t71 void setBufferPtr(void* ptr) { 72 const uint64_t addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); 73 bufferPtrLo = uint32_t(addr); 74 bufferPtrHi = uint32_t(addr >> 32); 75 } 76 getMmapedOffsetcb_handle_t77 uint64_t getMmapedOffset() const { 78 return (uint64_t(mmapedOffsetHi) << 32) | mmapedOffsetLo; 79 } 80 allocatedSizecb_handle_t81 uint32_t allocatedSize() const { 82 return getBufferPtr() ? bufferSize : 0; 83 } 84 isValidcb_handle_t85 bool isValid() const { 86 return (version == sizeof(native_handle)) 87 && (magic & CB_HANDLE_MAGIC_MASK) == CB_HANDLE_MAGIC_BASE; 88 } 89 fromcb_handle_t90 static cb_handle_t* from(void* p) { 91 if (!p) { return NULL; } 92 cb_handle_t* cb = static_cast<cb_handle_t*>(p); 93 return cb->isValid() ? cb : NULL; 94 } 95 fromcb_handle_t96 static const cb_handle_t* from(const void* p) { 97 return from(const_cast<void*>(p)); 98 } 99 from_unconstcb_handle_t100 static cb_handle_t* from_unconst(const void* p) { 101 return from(const_cast<void*>(p)); 102 } 103 104 // fds 105 int32_t bufferFd; // underlying buffer file handle 106 QEMU_PIPE_HANDLE hostHandleRefCountFd; // guest side refcounter to hostHandle 107 108 // ints 109 uint32_t magic; // magic number in order to validate a pointer 110 uint32_t hostHandle; // the host reference to this buffer 111 int32_t usage; // usage bits the buffer was created with 112 int32_t width; // buffer width 113 int32_t height; // buffer height 114 int32_t format; // real internal pixel format format 115 int32_t glFormat; // OpenGL format enum used for host h/w color buffer 116 int32_t glType; // OpenGL type enum used when uploading to host 117 uint32_t bufferSize; // buffer size and location 118 uint32_t bufferPtrLo; 119 uint32_t bufferPtrHi; 120 uint32_t mmapedOffsetLo; 121 uint32_t mmapedOffsetHi; 122 int32_t lockedLeft; // region of buffer locked for s/w write 123 int32_t lockedTop; 124 int32_t lockedWidth; 125 int32_t lockedHeight; 126 }; 127 128 #endif //__GRALLOC_CB_H__ 129