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 <hardware/hardware.h> 21 #include <hardware/gralloc.h> 22 #include <cutils/native_handle.h> 23 24 #define BUFFER_HANDLE_MAGIC ((int)0xabfabfab) 25 #define CB_HANDLE_NUM_INTS(nfds) (int)((sizeof(cb_handle_t) - (nfds)*sizeof(int)) / sizeof(int)) 26 27 // 28 // Our buffer handle structure 29 // 30 struct cb_handle_t : public native_handle { 31 cb_handle_tcb_handle_t32 cb_handle_t(int p_fd, int p_ashmemSize, int p_usage, 33 int p_width, int p_height, int p_format, 34 int p_glFormat, int p_glType) : 35 fd(p_fd), 36 magic(BUFFER_HANDLE_MAGIC), 37 usage(p_usage), 38 width(p_width), 39 height(p_height), 40 format(p_format), 41 glFormat(p_glFormat), 42 glType(p_glType), 43 ashmemSize(p_ashmemSize), 44 ashmemBase(0), 45 ashmemBasePid(0), 46 mappedPid(0), 47 lockedLeft(0), 48 lockedTop(0), 49 lockedWidth(0), 50 lockedHeight(0), 51 hostHandle(0) 52 { 53 version = sizeof(native_handle); 54 numFds = 0; 55 numInts = CB_HANDLE_NUM_INTS(numFds); 56 } 57 ~cb_handle_tcb_handle_t58 ~cb_handle_t() { 59 magic = 0; 60 } 61 setFdcb_handle_t62 void setFd(int p_fd) { 63 if (p_fd >= 0) { 64 numFds = 1; 65 } 66 else { 67 numFds = 0; 68 } 69 fd = p_fd; 70 numInts = CB_HANDLE_NUM_INTS(numFds); 71 } 72 validatecb_handle_t73 static bool validate(cb_handle_t * hnd) { 74 return (hnd && 75 hnd->version == sizeof(native_handle) && 76 hnd->magic == BUFFER_HANDLE_MAGIC && 77 hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds)); 78 } 79 canBePostedcb_handle_t80 bool canBePosted() { 81 return (0 != (usage & GRALLOC_USAGE_HW_FB)); 82 } 83 84 // file-descriptors 85 int fd; // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed) 86 87 // ints 88 int magic; // magic number in order to validate a pointer to be a cb_handle_t 89 int usage; // usage bits the buffer was created with 90 int width; // buffer width 91 int height; // buffer height 92 int format; // real internal pixel format format 93 int glFormat; // OpenGL format enum used for host h/w color buffer 94 int glType; // OpenGL type enum used when uploading to host 95 int ashmemSize; // ashmem region size for the buffer (0 unless is HW_FB buffer or 96 // s/w access is needed) 97 int ashmemBase; // CPU address of the mapped ashmem region 98 int ashmemBasePid; // process id which mapped the ashmem region 99 int mappedPid; // process id which succeeded gralloc_register call 100 int lockedLeft; // region of buffer locked for s/w write 101 int lockedTop; 102 int lockedWidth; 103 int lockedHeight; 104 uint32_t hostHandle; 105 }; 106 107 108 #endif //__GRALLOC_CB_H__ 109