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