1 /*
2  * Copyright (C) 2008, The Android Open Source Project
3  * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <unistd.h>
19 #include <fcntl.h>
20 
21 #include <sys/mman.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <cutils/properties.h>
26 
27 #include "gr.h"
28 #include "gpu.h"
29 #include "memalloc.h"
30 #include "alloc_controller.h"
31 
32 using namespace gralloc;
33 
34 int fb_device_open(const hw_module_t* module, const char* name,
35                    hw_device_t** device);
36 
37 static int gralloc_device_open(const hw_module_t* module, const char* name,
38                                hw_device_t** device);
39 
40 extern int gralloc_lock(gralloc_module_t const* module,
41                         buffer_handle_t handle, int usage,
42                         int l, int t, int w, int h,
43                         void** vaddr);
44 
45 extern int gralloc_lock_ycbcr(gralloc_module_t const* module,
46                         buffer_handle_t handle, int usage,
47                         int l, int t, int w, int h,
48                         struct android_ycbcr *ycbcr);
49 
50 extern int gralloc_unlock(gralloc_module_t const* module,
51                           buffer_handle_t handle);
52 
53 extern int gralloc_register_buffer(gralloc_module_t const* module,
54                                    buffer_handle_t handle);
55 
56 extern int gralloc_unregister_buffer(gralloc_module_t const* module,
57                                      buffer_handle_t handle);
58 
59 extern int gralloc_perform(struct gralloc_module_t const* module,
60                            int operation, ... );
61 
62 // HAL module methods
63 static struct hw_module_methods_t gralloc_module_methods = {
64     .open = gralloc_device_open
65 };
66 
67 // HAL module initialize
68 struct private_module_t HAL_MODULE_INFO_SYM = {
69     .base = {
70         .common = {
71             .tag = HARDWARE_MODULE_TAG,
72             .version_major = 1,
73             .version_minor = 0,
74             .id = GRALLOC_HARDWARE_MODULE_ID,
75             .name = "Graphics Memory Allocator Module",
76             .author = "The Android Open Source Project",
77             .methods = &gralloc_module_methods,
78             .dso = 0,
79             .reserved = {0},
80         },
81         .registerBuffer = gralloc_register_buffer,
82         .unregisterBuffer = gralloc_unregister_buffer,
83         .lock = gralloc_lock,
84         .unlock = gralloc_unlock,
85         .perform = gralloc_perform,
86         .lock_ycbcr = gralloc_lock_ycbcr,
87         .validateBufferSize = NULL,
88         .getTransportSize = NULL,
89     },
90     .framebuffer = 0,
91     .fbFormat = 0,
92     .flags = 0,
93     .numBuffers = 0,
94     .bufferMask = 0,
95     .lock = PTHREAD_MUTEX_INITIALIZER,
96 };
97 
98 // Open Gralloc device
gralloc_device_open(const hw_module_t * module,const char * name,hw_device_t ** device)99 int gralloc_device_open(const hw_module_t* module, const char* name,
100                         hw_device_t** device)
101 {
102     int status = -EINVAL;
103     if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) {
104         const private_module_t* m = reinterpret_cast<const private_module_t*>(
105             module);
106         gpu_context_t *dev;
107         IAllocController* alloc_ctrl = IAllocController::getInstance();
108         dev = new gpu_context_t(m, alloc_ctrl);
109         if(!dev)
110             return status;
111 
112         *device = &dev->common;
113         status = 0;
114     } else {
115         status = fb_device_open(module, name, device);
116     }
117     return status;
118 }
119