Home | History | Annotate | Download | only in OpenglSystemCommon
      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 
     32     cb_handle_t(int p_fd, int p_ashmemSize, int p_usage,
     33                 int p_width, int p_height,
     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         glFormat(p_glFormat),
     41         glType(p_glType),
     42         ashmemSize(p_ashmemSize),
     43         ashmemBase(NULL),
     44         ashmemBasePid(0),
     45         mappedPid(0),
     46         lockedLeft(0),
     47         lockedTop(0),
     48         lockedWidth(0),
     49         lockedHeight(0),
     50         hostHandle(0)
     51     {
     52         version = sizeof(native_handle);
     53         numFds = 0;
     54         numInts = CB_HANDLE_NUM_INTS(numFds);
     55     }
     56 
     57     ~cb_handle_t() {
     58         magic = 0;
     59     }
     60 
     61     void setFd(int p_fd) {
     62         if (p_fd >= 0) {
     63             numFds = 1;
     64         }
     65         else {
     66             numFds = 0;
     67         }
     68         fd = p_fd;
     69         numInts = CB_HANDLE_NUM_INTS(numFds);
     70     }
     71 
     72     static bool validate(cb_handle_t * hnd) {
     73         return (hnd &&
     74                 hnd->version == sizeof(native_handle) &&
     75                 hnd->magic == BUFFER_HANDLE_MAGIC &&
     76                 hnd->numInts == CB_HANDLE_NUM_INTS(hnd->numFds));
     77     }
     78 
     79     bool canBePosted() {
     80         return (0 != (usage & GRALLOC_USAGE_HW_FB));
     81     }
     82 
     83     // file-descriptors
     84     int fd;  // ashmem fd (-1 of ashmem region did not allocated, i.e. no SW access needed)
     85 
     86     // ints
     87     int magic;              // magic number in order to validate a pointer to be a cb_handle_t
     88     int usage;              // usage bits the buffer was created with
     89     int width;              // buffer width
     90     int height;             // buffer height
     91     int glFormat;           // OpenGL format enum used for host h/w color buffer
     92     int glType;             // OpenGL type enum used when uploading to host
     93     int ashmemSize;         // ashmem region size for the buffer (0 unless is HW_FB buffer or
     94                             //                                    s/w access is needed)
     95     int ashmemBase;         // CPU address of the mapped ashmem region
     96     int ashmemBasePid;      // process id which mapped the ashmem region
     97     int mappedPid;          // process id which succeeded gralloc_register call
     98     int lockedLeft;         // region of buffer locked for s/w write
     99     int lockedTop;
    100     int lockedWidth;
    101     int lockedHeight;
    102     uint32_t hostHandle;
    103 };
    104 
    105 
    106 #endif //__GRALLOC_CB_H__
    107