OSDN Git Service

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