PageRenderTime 54ms CodeModel.GetById 10ms app.highlight 39ms RepoModel.GetById 2ms app.codeStats 0ms

/libgralloc/gralloc.cpp

http://github.com/CyanogenMod/android_device_zte_blade
C++ | 205 lines | 143 code | 40 blank | 22 comment | 4 complexity | b46858af53eb7be220dedbe5c3105054 MD5 | raw file
  1/*
  2 * Copyright (C) 2008 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#include <unistd.h>
 18#include <fcntl.h>
 19
 20#include <sys/mman.h>
 21#include <sys/stat.h>
 22#include <sys/types.h>
 23#include <sys/ioctl.h>
 24
 25#include <linux/android_pmem.h>
 26
 27#include "allocator.h"
 28#include "gr.h"
 29#include "gpu.h"
 30
 31/*****************************************************************************/
 32
 33static int gralloc_alloc_buffer(alloc_device_t* dev,
 34        size_t size, int usage, buffer_handle_t* pHandle);
 35
 36/*****************************************************************************/
 37
 38int fb_device_open(const hw_module_t* module, const char* name,
 39        hw_device_t** device);
 40
 41static int gralloc_device_open(const hw_module_t* module, const char* name,
 42        hw_device_t** device);
 43
 44extern int gralloc_lock(gralloc_module_t const* module,
 45        buffer_handle_t handle, int usage,
 46        int l, int t, int w, int h,
 47        void** vaddr);
 48
 49extern int gralloc_unlock(gralloc_module_t const* module, 
 50        buffer_handle_t handle);
 51
 52extern int gralloc_register_buffer(gralloc_module_t const* module,
 53        buffer_handle_t handle);
 54
 55extern int gralloc_unregister_buffer(gralloc_module_t const* module,
 56        buffer_handle_t handle);
 57
 58extern int gralloc_perform(struct gralloc_module_t const* module,
 59        int operation, ... );
 60
 61/*****************************************************************************/
 62
 63/* On-device dependency implementation */
 64class PmemAllocatorDepsDeviceImpl : public PmemUserspaceAllocator::Deps,
 65        public PmemKernelAllocator::Deps {
 66
 67    virtual size_t getPmemTotalSize(int fd, size_t* size) {
 68        int err = 0;
 69#ifndef TARGET_MSM7x27
 70        pmem_region region;
 71        err = ioctl(fd, PMEM_GET_TOTAL_SIZE, &region);
 72        if (err == 0) {
 73            *size = region.len;
 74        }
 75#else
 76#ifdef USE_ASHMEM
 77	*size = m->info.xres * m->info.yres * 2 * 2;
 78#else
 79	*size = 23<<20; //23MB for 7x27
 80#endif
 81#endif
 82        return err;
 83    }
 84
 85    virtual int connectPmem(int fd, int master_fd) {
 86        return ioctl(fd, PMEM_CONNECT, master_fd);
 87    }
 88
 89    virtual int mapPmem(int fd, int offset, size_t size) {
 90        struct pmem_region sub = { offset, size };
 91        return ioctl(fd, PMEM_MAP, &sub);
 92    }
 93
 94    virtual int unmapPmem(int fd, int offset, size_t size) {
 95        struct pmem_region sub = { offset, size };
 96        return ioctl(fd, PMEM_UNMAP, &sub);
 97    }
 98
 99    virtual int getErrno() {
100        return errno;
101    }
102
103    virtual void* mmap(void* start, size_t length, int prot, int flags, int fd,
104            off_t offset) {
105        return ::mmap(start, length, prot, flags, fd, offset);
106    }
107
108    virtual int munmap(void* start, size_t length) {
109        return ::munmap(start, length);
110    }
111
112    virtual int open(const char* pathname, int flags, int mode) {
113        return ::open(pathname, flags, mode);
114    }
115
116    virtual int close(int fd) {
117        return ::close(fd);
118    }
119};
120
121class GpuContextDepsDeviceImpl : public gpu_context_t::Deps {
122
123 public:
124
125    virtual int ashmem_create_region(const char *name, size_t size) {
126        return ::ashmem_create_region(name, size);
127    }
128
129    virtual int mapFrameBufferLocked(struct private_module_t* module) {
130        return ::mapFrameBufferLocked(module);
131    }
132
133    virtual int terminateBuffer(gralloc_module_t const* module,
134            private_handle_t* hnd) {
135        return ::terminateBuffer(module, hnd);
136    }
137
138    virtual int close(int fd) {
139        return ::close(fd);
140    }
141};
142
143static PmemAllocatorDepsDeviceImpl pmemAllocatorDeviceDepsImpl;
144static GpuContextDepsDeviceImpl gpuContextDeviceDepsImpl;
145
146/*****************************************************************************/
147
148static SimpleBestFitAllocator pmemAllocMgr;
149static PmemUserspaceAllocator pmemAllocator(pmemAllocatorDeviceDepsImpl, pmemAllocMgr,
150        "/dev/pmem");
151
152static PmemKernelAllocator pmemAdspAllocator(pmemAllocatorDeviceDepsImpl,
153        "/dev/pmem_adsp");
154
155/*****************************************************************************/
156
157static struct hw_module_methods_t gralloc_module_methods = {
158    open: gralloc_device_open
159};
160
161struct private_module_t HAL_MODULE_INFO_SYM = {
162    base: {
163        common: {
164            tag: HARDWARE_MODULE_TAG,
165            version_major: 1,
166            version_minor: 0,
167            id: GRALLOC_HARDWARE_MODULE_ID,
168            name: "Graphics Memory Allocator Module",
169            author: "The Android Open Source Project",
170            methods: &gralloc_module_methods
171        },
172        registerBuffer: gralloc_register_buffer,
173        unregisterBuffer: gralloc_unregister_buffer,
174        lock: gralloc_lock,
175        unlock: gralloc_unlock,
176        perform: gralloc_perform,
177    },
178    framebuffer: 0,
179    fbFormat: 0,
180    flags: 0,
181    numBuffers: 0,
182    bufferMask: 0,
183    lock: PTHREAD_MUTEX_INITIALIZER,
184    currentBuffer: 0,
185};
186
187/*****************************************************************************/
188
189int gralloc_device_open(const hw_module_t* module, const char* name,
190        hw_device_t** device)
191{
192    int status = -EINVAL;
193    if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
194        const private_module_t* m = reinterpret_cast<const private_module_t*>(
195                module);
196        gpu_context_t *dev;
197        dev = new gpu_context_t(gpuContextDeviceDepsImpl, pmemAllocator,
198                pmemAdspAllocator, m);
199        *device = &dev->common;
200        status = 0;
201    } else {
202        status = fb_device_open(module, name, device);
203    }
204    return status;
205}