keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 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 | |
Roman Kiryanov | 1ab8bab | 2019-10-17 11:41:08 -0700 | [diff] [blame] | 20 | #include <cutils/native_handle.h> |
| 21 | #include "qemu_pipe.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 { |
| 30 | 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 | : bufferFd(p_bufferFd), |
| 43 | hostHandleRefCountFd(p_hostHandleRefCountFd), |
| 44 | magic(p_magic), |
| 45 | hostHandle(p_hostHandle), |
| 46 | usage(p_usage), |
| 47 | width(p_width), |
| 48 | height(p_height), |
| 49 | format(p_format), |
| 50 | glFormat(p_glFormat), |
| 51 | glType(p_glType), |
| 52 | bufferSize(p_bufSize), |
| 53 | lockedLeft(0), |
| 54 | lockedTop(0), |
| 55 | lockedWidth(0), |
| 56 | lockedHeight(0) { |
| 57 | version = sizeof(native_handle); |
| 58 | numFds = ((bufferFd >= 0) ? 1 : 0) + (qemu_pipe_valid(hostHandleRefCountFd) ? 1 : 0); |
| 59 | numInts = 0; // has to be overwritten in children classes |
| 60 | setBufferPtr(p_bufPtr); |
| 61 | } |
| 62 | |
| 63 | void* getBufferPtr() const { |
| 64 | const uint64_t addr = (uint64_t(bufferPtrHi) << 32) | bufferPtrLo; |
| 65 | return reinterpret_cast<void*>(static_cast<uintptr_t>(addr)); |
| 66 | } |
| 67 | |
| 68 | void setBufferPtr(void* ptr) { |
| 69 | const uint64_t addr = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(ptr)); |
| 70 | bufferPtrLo = uint32_t(addr); |
| 71 | bufferPtrHi = uint32_t(addr >> 32); |
| 72 | } |
| 73 | |
| 74 | uint32_t allocatedSize() const { |
| 75 | return getBufferPtr() ? bufferSize : 0; |
| 76 | } |
| 77 | |
| 78 | bool isValid() const { |
| 79 | return (version == sizeof(native_handle)) |
| 80 | && (magic & CB_HANDLE_MAGIC_MASK) == CB_HANDLE_MAGIC_BASE; |
| 81 | } |
| 82 | |
| 83 | static cb_handle_t* from(void* p) { |
| 84 | if (!p) { return NULL; } |
| 85 | cb_handle_t* cb = static_cast<cb_handle_t*>(p); |
| 86 | return cb->isValid() ? cb : NULL; |
| 87 | } |
| 88 | |
| 89 | static const cb_handle_t* from(const void* p) { |
| 90 | return from(const_cast<void*>(p)); |
| 91 | } |
| 92 | |
| 93 | static cb_handle_t* from_unconst(const void* p) { |
| 94 | return from(const_cast<void*>(p)); |
| 95 | } |
| 96 | |
| 97 | // fds |
| 98 | int32_t bufferFd; // underlying buffer file handle |
| 99 | QEMU_PIPE_HANDLE hostHandleRefCountFd; // guest side refcounter to hostHandle |
| 100 | |
| 101 | // ints |
| 102 | uint32_t magic; // magic number in order to validate a pointer |
| 103 | uint32_t hostHandle; // the host reference to this buffer |
| 104 | int32_t usage; // usage bits the buffer was created with |
| 105 | int32_t width; // buffer width |
| 106 | int32_t height; // buffer height |
| 107 | int32_t format; // real internal pixel format format |
| 108 | int32_t glFormat; // OpenGL format enum used for host h/w color buffer |
| 109 | int32_t glType; // OpenGL type enum used when uploading to host |
| 110 | uint32_t bufferSize; // buffer size and location |
| 111 | uint32_t bufferPtrLo; |
| 112 | uint32_t bufferPtrHi; |
| 113 | int32_t lockedLeft; // region of buffer locked for s/w write |
| 114 | int32_t lockedTop; |
| 115 | int32_t lockedWidth; |
| 116 | int32_t lockedHeight; |
| 117 | }; |
keunyoung | b85b275 | 2013-03-08 12:28:03 -0800 | [diff] [blame] | 118 | |
| 119 | #endif //__GRALLOC_CB_H__ |