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