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 SYSTEM_HALS_CB_HANDLE_30_H 18 #define SYSTEM_HALS_CB_HANDLE_30_H 19 20 #include <gralloc_cb_bp.h> 21 #include "goldfish_address_space.h" 22 23 const uint32_t CB_HANDLE_MAGIC_30 = CB_HANDLE_MAGIC_BASE | 0x2; 24 25 struct cb_handle_30_t : public cb_handle_t { cb_handle_30_tcb_handle_30_t26 cb_handle_30_t(int p_bufferFd, 27 int p_hostHandleRefCountFd, 28 uint32_t p_hostHandle, 29 uint32_t p_usage, 30 uint32_t p_width, 31 uint32_t p_height, 32 uint32_t p_format, 33 uint32_t p_glFormat, 34 uint32_t p_glType, 35 uint32_t p_bufSize, 36 void* p_bufPtr, 37 uint32_t p_mmapedSize, 38 uint64_t p_mmapedOffset, 39 uint32_t p_bytesPerPixel, 40 uint32_t p_stride) 41 : cb_handle_t(CB_HANDLE_MAGIC_30, 42 p_hostHandle, 43 p_format, 44 p_stride, 45 p_bufSize, 46 p_mmapedOffset), 47 usage(p_usage), 48 width(p_width), 49 height(p_height), 50 glFormat(p_glFormat), 51 glType(p_glType), 52 bytesPerPixel(p_bytesPerPixel), 53 mmapedSize(p_mmapedSize), 54 lockedUsage(0) { 55 fds[0] = -1; 56 fds[1] = -1; 57 int n = 0; 58 if (p_bufferFd >= 0) { 59 bufferFdIndex = n++; 60 fds[bufferFdIndex] = p_bufferFd; 61 } else { 62 bufferFdIndex = -1; 63 } 64 65 if (p_hostHandleRefCountFd >= 0) { 66 hostHandleRefcountFdIndex = n++; 67 fds[hostHandleRefcountFdIndex] = p_hostHandleRefCountFd; 68 } else { 69 hostHandleRefcountFdIndex = -1; 70 } 71 72 numFds = n; 73 numInts = CB_HANDLE_NUM_INTS(n); 74 setBufferPtr(p_bufPtr); 75 } 76 isValidcb_handle_30_t77 bool isValid() const { return (version == sizeof(native_handle_t)) && (magic == CB_HANDLE_MAGIC_30); } 78 getBufferPtrcb_handle_30_t79 void* getBufferPtr() const { 80 const uint64_t addr = (uint64_t(bufferPtrHi) << 32) | bufferPtrLo; 81 return reinterpret_cast<void*>(static_cast<uintptr_t>(addr)); 82 } 83 setBufferPtrcb_handle_30_t84 void setBufferPtr(void* ptr) { 85 const uint64_t addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); 86 bufferPtrLo = uint32_t(addr); 87 bufferPtrHi = uint32_t(addr >> 32); 88 } 89 fromcb_handle_30_t90 static cb_handle_30_t* from(void* p) { 91 if (!p) { return nullptr; } 92 cb_handle_30_t* cb = static_cast<cb_handle_30_t*>(p); 93 return cb->isValid() ? cb : nullptr; 94 } 95 fromcb_handle_30_t96 static const cb_handle_30_t* from(const void* p) { 97 return from(const_cast<void*>(p)); 98 } 99 from_unconstcb_handle_30_t100 static cb_handle_30_t* from_unconst(const void* p) { 101 return from(const_cast<void*>(p)); 102 } 103 104 uint32_t usage; // usage bits the buffer was created with 105 uint32_t width; // buffer width 106 uint32_t height; // buffer height 107 uint32_t glFormat; // OpenGL format enum used for host h/w color buffer 108 uint32_t glType; // OpenGL type enum used when uploading to host 109 uint32_t bytesPerPixel; 110 uint32_t mmapedSize; // real allocation side 111 uint32_t bufferPtrLo; 112 uint32_t bufferPtrHi; 113 uint8_t lockedUsage; 114 int8_t bufferFdIndex; 115 int8_t hostHandleRefcountFdIndex; 116 int8_t unused; 117 uint32_t lockedLeft; // region of buffer locked for s/w write 118 uint32_t lockedTop; 119 uint32_t lockedWidth; 120 uint32_t lockedHeight; 121 }; 122 123 #endif // SYSTEM_HALS_CB_HANDLE_30_H 124