blob: 5a2b7f040698f28e0ffb05dbf7c123d11d3be26b [file] [log] [blame]
Cody Schuffelen134ff032019-11-22 00:25:32 -08001/*
2 * Copyright (C) 2016 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#include <sys/mman.h>
17
18#include <dlfcn.h>
19
20#include <cutils/ashmem.h>
21#include <log/log.h>
22#include <cutils/properties.h>
23
24#include <sys/system_properties.h>
25
26#include <hardware/hardware.h>
27#include <hardware/gralloc.h>
28
29#include <fcntl.h>
30#include <errno.h>
31#include <sys/ioctl.h>
32#include <string.h>
33#include <stdlib.h>
34
35#include <cutils/atomic.h>
36
37#if defined(__ANDROID__)
38#include <linux/fb.h>
39#endif
40
41#include "gralloc_vsoc_priv.h"
42#include "region_registry.h"
43
44#include "common/libs/auto_resources/auto_resources.h"
45#include "common/libs/threads/cuttlefish_thread.h"
46#include "common/vsoc/lib/screen_region_view.h"
47
48using vsoc::screen::ScreenRegionView;
49
50/*****************************************************************************/
51
52struct fb_context_t {
53 framebuffer_device_t device;
54};
55
56/*****************************************************************************/
57
58static int fb_setSwapInterval(struct framebuffer_device_t* dev, int interval) {
59 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) {
60 return -EINVAL;
61 }
62 // FIXME: implement fb_setSwapInterval
63 return 0;
64}
65
66/*
67 * These functions (and probably the entire framebuffer device) are most likely
68 * not used when the hardware composer device is present, however is hard to be
69 * 100% sure.
70 */
71static int fb_setUpdateRect(
72 struct framebuffer_device_t* dev __unused, int l, int t, int w, int h) {
73 if (((w|h) <= 0) || ((l|t)<0)) {
74 return -EINVAL;
75 }
76 // TODO(jemoreira): Find a way to broadcast this with the framebuffer control.
77 return 0;
78}
79
80static int fb_post(struct framebuffer_device_t* dev __unused,
81 buffer_handle_t buffer_handle) {
82 static int frame_buffer_idx = 0;
83
84 auto screen_view = ScreenRegionView::GetInstance();
85
86 void* frame_buffer = screen_view->GetBuffer(frame_buffer_idx);
87 const private_handle_t* p_handle =
88 reinterpret_cast<const private_handle_t*>(buffer_handle);
89 void* buffer;
90 int retval =
91 reinterpret_cast<gralloc_module_t*>(dev->common.module)
92 ->lock(reinterpret_cast<const gralloc_module_t*>(dev->common.module),
93 buffer_handle, GRALLOC_USAGE_SW_READ_OFTEN, 0, 0,
94 p_handle->x_res, p_handle->y_res, &buffer);
95 if (retval != 0) {
96 ALOGE("Got error code %d from lock function", retval);
97 return -1;
98 }
99 memcpy(frame_buffer, buffer, screen_view->buffer_size());
100 screen_view->BroadcastNewFrame(frame_buffer_idx);
101
102 frame_buffer_idx = (frame_buffer_idx + 1) % screen_view->number_of_buffers();
103
104 return 0;
105}
106
107/*****************************************************************************/
108
109static int fb_close(struct hw_device_t *dev) {
110 fb_context_t* ctx = (fb_context_t*)dev;
111 if (ctx) {
112 free(ctx);
113 }
114 return 0;
115}
116
117int fb_device_open(
118 hw_module_t const* module, const char* name, hw_device_t** device) {
119 if (strcmp(name, GRALLOC_HARDWARE_FB0) != 0) {
120 return -EINVAL;
121 }
122 /* initialize our state here */
123 fb_context_t* dev = (fb_context_t*) malloc(sizeof(*dev));
124 LOG_FATAL_IF(!dev, "%s: malloc returned NULL.", __FUNCTION__);
125 memset(dev, 0, sizeof(*dev));
126
127 /* initialize the procs */
128 dev->device.common.tag = HARDWARE_DEVICE_TAG;
129 dev->device.common.version = 0;
130 dev->device.common.module = const_cast<hw_module_t*>(module);
131 dev->device.common.close = fb_close;
132 dev->device.setSwapInterval = fb_setSwapInterval;
133 dev->device.post = fb_post;
134 dev->device.setUpdateRect = fb_setUpdateRect;
135
136 auto screen_view = ScreenRegionView::GetInstance();
137
138 int stride =
139 screen_view->line_length() / screen_view->bytes_per_pixel();
140 int format = HAL_PIXEL_FORMAT_RGBX_8888;
141 const_cast<uint32_t&>(dev->device.flags) = 0;
142 const_cast<uint32_t&>(dev->device.width) = screen_view->x_res();
143 const_cast<uint32_t&>(dev->device.height) = screen_view->y_res();
144 const_cast<int&>(dev->device.stride) = stride;
145 const_cast<int&>(dev->device.format) = format;
146 const_cast<float&>(dev->device.xdpi) = screen_view->dpi();
147 const_cast<float&>(dev->device.ydpi) = screen_view->dpi();
148 const_cast<float&>(dev->device.fps) =
149 (screen_view->refresh_rate_hz() * 1000) / 1000.0f;
150 const_cast<int&>(dev->device.minSwapInterval) = 1;
151 const_cast<int&>(dev->device.maxSwapInterval) = 1;
152 *device = &dev->device.common;
153
154 return 0;
155}