• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/mman.h>
18 
19 #include <dlfcn.h>
20 
21 #include <cutils/ashmem.h>
22 #include <cutils/log.h>
23 
24 #include <hardware/hardware.h>
25 #include <hardware/gralloc.h>
26 
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <sys/ioctl.h>
30 #include <string.h>
31 #include <stdlib.h>
32 
33 #include <cutils/log.h>
34 #include <cutils/atomic.h>
35 
36 #if HAVE_ANDROID_OS
37 #include <linux/fb.h>
38 #endif
39 
40 #include "gralloc_priv.h"
41 #include "gr.h"
42 
43 /*****************************************************************************/
44 
45 // numbers of buffers for page flipping
46 #if defined(NO_PAGE_FLIPPING)
47 // page-flipping is buggy on some devices
48 #define NUM_BUFFERS 1
49 #else
50 #define NUM_BUFFERS 2
51 #endif
52 
53 
54 enum {
55     PAGE_FLIP = 0x00000001,
56     LOCKED = 0x00000002
57 };
58 
59 struct fb_context_t {
60     framebuffer_device_t  device;
61 };
62 
63 /*****************************************************************************/
64 
fb_setSwapInterval(struct framebuffer_device_t * dev,int interval)65 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
66             int interval)
67 {
68     fb_context_t* ctx = (fb_context_t*)dev;
69     if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
70         return -EINVAL;
71     // FIXME: implement fb_setSwapInterval
72     return 0;
73 }
74 
fb_setUpdateRect(struct framebuffer_device_t * dev,int l,int t,int w,int h)75 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
76         int l, int t, int w, int h)
77 {
78     if (((w|h) <= 0) || ((l|t)<0))
79         return -EINVAL;
80 
81     fb_context_t* ctx = (fb_context_t*)dev;
82     private_module_t* m = reinterpret_cast<private_module_t*>(
83             dev->common.module);
84     m->info.reserved[0] = 0x54445055; // "UPDT";
85     m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
86     m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
87     return 0;
88 }
89 
fb_post(struct framebuffer_device_t * dev,buffer_handle_t buffer)90 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
91 {
92     if (private_handle_t::validate(buffer) < 0)
93         return -EINVAL;
94 
95     fb_context_t* ctx = (fb_context_t*)dev;
96 
97     private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
98     private_module_t* m = reinterpret_cast<private_module_t*>(
99             dev->common.module);
100 
101     if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
102         const size_t offset = hnd->base - m->framebuffer->base;
103         m->info.activate = FB_ACTIVATE_VBL;
104         m->info.yoffset = offset / m->finfo.line_length;
105         if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
106             LOGE("FBIOPUT_VSCREENINFO failed");
107             m->base.unlock(&m->base, buffer);
108             return -errno;
109         }
110         m->currentBuffer = buffer;
111 
112     } else {
113         // If we can't do the page_flip, just copy the buffer to the front
114         // FIXME: use copybit HAL instead of memcpy
115 
116         void* fb_vaddr;
117         void* buffer_vaddr;
118 
119         m->base.lock(&m->base, m->framebuffer,
120                 GRALLOC_USAGE_SW_WRITE_RARELY,
121                 0, 0, m->info.xres, m->info.yres,
122                 &fb_vaddr);
123 
124         m->base.lock(&m->base, buffer,
125                 GRALLOC_USAGE_SW_READ_RARELY,
126                 0, 0, m->info.xres, m->info.yres,
127                 &buffer_vaddr);
128 
129         memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
130 
131         m->base.unlock(&m->base, buffer);
132         m->base.unlock(&m->base, m->framebuffer);
133     }
134 
135     return 0;
136 }
137 
138 /*****************************************************************************/
139 
mapFrameBufferLocked(struct private_module_t * module)140 int mapFrameBufferLocked(struct private_module_t* module)
141 {
142     // already initialized...
143     if (module->framebuffer) {
144         return 0;
145     }
146 
147     char const * const device_template[] = {
148             "/dev/graphics/fb%u",
149             "/dev/fb%u",
150             0 };
151 
152     int fd = -1;
153     int i=0;
154     char name[64];
155 
156     while ((fd==-1) && device_template[i]) {
157         snprintf(name, 64, device_template[i], 0);
158         fd = open(name, O_RDWR, 0);
159         i++;
160     }
161     if (fd < 0)
162         return -errno;
163 
164     struct fb_fix_screeninfo finfo;
165     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
166         return -errno;
167 
168     struct fb_var_screeninfo info;
169     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
170         return -errno;
171 
172     info.reserved[0] = 0;
173     info.reserved[1] = 0;
174     info.reserved[2] = 0;
175     info.xoffset = 0;
176     info.yoffset = 0;
177     info.activate = FB_ACTIVATE_NOW;
178 
179 #if defined(NO_32BPP)
180     /*
181      * Explicitly request 5/6/5
182      */
183     info.bits_per_pixel = 16;
184     info.red.offset     = 11;
185     info.red.length     = 5;
186     info.green.offset   = 5;
187     info.green.length   = 6;
188     info.blue.offset    = 0;
189     info.blue.length    = 5;
190     info.transp.offset  = 0;
191     info.transp.length  = 0;
192 #endif
193 
194     /*
195      * Request NUM_BUFFERS screens (at lest 2 for page flipping)
196      */
197     info.yres_virtual = info.yres * NUM_BUFFERS;
198 
199 
200     uint32_t flags = PAGE_FLIP;
201     if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
202         info.yres_virtual = info.yres;
203         flags &= ~PAGE_FLIP;
204         LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
205     }
206 
207     if (info.yres_virtual < info.yres * 2) {
208         // we need at least 2 for page-flipping
209         info.yres_virtual = info.yres;
210         flags &= ~PAGE_FLIP;
211         LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
212                 info.yres_virtual, info.yres*2);
213     }
214 
215     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
216         return -errno;
217 
218     uint64_t  refreshQuotient =
219     (
220             uint64_t( info.upper_margin + info.lower_margin + info.yres )
221             * ( info.left_margin  + info.right_margin + info.xres )
222             * info.pixclock
223     );
224 
225     /* Beware, info.pixclock might be 0 under emulation, so avoid a
226      * division-by-0 here (SIGFPE on ARM) */
227     int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0;
228 
229     if (refreshRate == 0) {
230         // bleagh, bad info from the driver
231         refreshRate = 60*1000;  // 60 Hz
232     }
233 
234     if (int(info.width) <= 0 || int(info.height) <= 0) {
235         // the driver doesn't return that information
236         // default to 160 dpi
237         info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
238         info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
239     }
240 
241     float xdpi = (info.xres * 25.4f) / info.width;
242     float ydpi = (info.yres * 25.4f) / info.height;
243     float fps  = refreshRate / 1000.0f;
244 
245     LOGI(   "using (fd=%d)\n"
246             "id           = %s\n"
247             "xres         = %d px\n"
248             "yres         = %d px\n"
249             "xres_virtual = %d px\n"
250             "yres_virtual = %d px\n"
251             "bpp          = %d\n"
252             "r            = %2u:%u\n"
253             "g            = %2u:%u\n"
254             "b            = %2u:%u\n",
255             fd,
256             finfo.id,
257             info.xres,
258             info.yres,
259             info.xres_virtual,
260             info.yres_virtual,
261             info.bits_per_pixel,
262             info.red.offset, info.red.length,
263             info.green.offset, info.green.length,
264             info.blue.offset, info.blue.length
265     );
266 
267     LOGI(   "width        = %d mm (%f dpi)\n"
268             "height       = %d mm (%f dpi)\n"
269             "refresh rate = %.2f Hz\n",
270             info.width,  xdpi,
271             info.height, ydpi,
272             fps
273     );
274 
275 
276     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
277         return -errno;
278 
279     if (finfo.smem_len <= 0)
280         return -errno;
281 
282 
283     module->flags = flags;
284     module->info = info;
285     module->finfo = finfo;
286     module->xdpi = xdpi;
287     module->ydpi = ydpi;
288     module->fps = fps;
289 
290     /*
291      * map the framebuffer
292      */
293 
294     int err;
295     size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
296     module->framebuffer = new private_handle_t(dup(fd), fbSize, 0);
297 
298     module->numBuffers = info.yres_virtual / info.yres;
299     module->bufferMask = 0;
300 
301     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
302     if (vaddr == MAP_FAILED) {
303         LOGE("Error mapping the framebuffer (%s)", strerror(errno));
304         return -errno;
305     }
306     module->framebuffer->base = intptr_t(vaddr);
307     memset(vaddr, 0, fbSize);
308     return 0;
309 }
310 
mapFrameBuffer(struct private_module_t * module)311 static int mapFrameBuffer(struct private_module_t* module)
312 {
313     pthread_mutex_lock(&module->lock);
314     int err = mapFrameBufferLocked(module);
315     pthread_mutex_unlock(&module->lock);
316     return err;
317 }
318 
319 /*****************************************************************************/
320 
fb_close(struct hw_device_t * dev)321 static int fb_close(struct hw_device_t *dev)
322 {
323     fb_context_t* ctx = (fb_context_t*)dev;
324     if (ctx) {
325         free(ctx);
326     }
327     return 0;
328 }
329 
fb_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)330 int fb_device_open(hw_module_t const* module, const char* name,
331         hw_device_t** device)
332 {
333     int status = -EINVAL;
334     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
335         alloc_device_t* gralloc_device;
336         status = gralloc_open(module, &gralloc_device);
337         if (status < 0)
338             return status;
339 
340         /* initialize our state here */
341         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
342         memset(dev, 0, sizeof(*dev));
343 
344         /* initialize the procs */
345         dev->device.common.tag = HARDWARE_DEVICE_TAG;
346         dev->device.common.version = 0;
347         dev->device.common.module = const_cast<hw_module_t*>(module);
348         dev->device.common.close = fb_close;
349         dev->device.setSwapInterval = fb_setSwapInterval;
350         dev->device.post            = fb_post;
351         dev->device.setUpdateRect = 0;
352 
353         private_module_t* m = (private_module_t*)module;
354         status = mapFrameBuffer(m);
355         if (status >= 0) {
356             int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
357             int format = (m->info.bits_per_pixel == 32)
358                          ? HAL_PIXEL_FORMAT_RGBX_8888
359                          : HAL_PIXEL_FORMAT_RGB_565;
360 #ifdef NO_32BPP
361             format = HAL_PIXEL_FORMAT_RGB_565;
362 #endif
363             const_cast<uint32_t&>(dev->device.flags) = 0;
364             const_cast<uint32_t&>(dev->device.width) = m->info.xres;
365             const_cast<uint32_t&>(dev->device.height) = m->info.yres;
366             const_cast<int&>(dev->device.stride) = stride;
367             const_cast<int&>(dev->device.format) = format;
368             const_cast<float&>(dev->device.xdpi) = m->xdpi;
369             const_cast<float&>(dev->device.ydpi) = m->ydpi;
370             const_cast<float&>(dev->device.fps) = m->fps;
371             const_cast<int&>(dev->device.minSwapInterval) = 1;
372             const_cast<int&>(dev->device.maxSwapInterval) = 1;
373             *device = &dev->device.common;
374         }
375     }
376     return status;
377 }
378