1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2010-2014 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 <dlfcn.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <linux/fb.h>
22 #include <linux/msm_mdp.h>
23 #include <pthread.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/mman.h>
28 
29 #include <cutils/atomic.h>
30 #include <cutils/properties.h>
31 #include <log/log.h>
32 
33 #include <hardware/hardware.h>
34 
35 #include <GLES/gl.h>
36 
37 #include "gralloc_priv.h"
38 #include "fb_priv.h"
39 #include "gr.h"
40 #include <profiler.h>
41 
42 #define EVEN_OUT(x) if (x & 0x0001) {x--;}
43 
44 enum {
45     PAGE_FLIP = 0x00000001,
46 };
47 
48 struct fb_context_t {
49     framebuffer_device_t  device;
50     //fd - which is returned on open
51     int fbFd;
52 };
53 
fb_setSwapInterval(struct framebuffer_device_t * dev,int interval)54 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
55                               int interval)
56 {
57     //XXX: Get the value here and implement along with
58     //single vsync in HWC
59     char pval[PROPERTY_VALUE_MAX];
60     property_get("debug.egl.swapinterval", pval, "-1");
61     int property_interval = atoi(pval);
62     if (property_interval >= 0)
63         interval = property_interval;
64 
65     private_module_t* m = reinterpret_cast<private_module_t*>(
66         dev->common.module);
67     if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
68         return -EINVAL;
69 
70     m->swapInterval = interval;
71     return 0;
72 }
73 
fb_post(struct framebuffer_device_t * dev,buffer_handle_t buffer)74 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
75 {
76     private_module_t* m =
77         reinterpret_cast<private_module_t*>(dev->common.module);
78     private_handle_t *hnd = static_cast<private_handle_t*>
79         (const_cast<native_handle_t*>(buffer));
80     fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev);
81     const unsigned int offset = (unsigned int) (hnd->base -
82             m->framebuffer->base);
83     m->info.activate = FB_ACTIVATE_VBL;
84     m->info.yoffset = (int)(offset / m->finfo.line_length);
85     if (ioctl(ctx->fbFd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
86         ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s",
87                 __FUNCTION__, strerror(errno));
88         return -errno;
89     }
90     return 0;
91 }
92 
fb_compositionComplete(struct framebuffer_device_t * dev)93 static int fb_compositionComplete(struct framebuffer_device_t* dev)
94 {
95     // TODO: Properly implement composition complete callback
96     if(!dev) {
97         return -1;
98     }
99     glFinish();
100 
101     return 0;
102 }
103 
mapFrameBufferLocked(framebuffer_device_t * dev)104 int mapFrameBufferLocked(framebuffer_device_t *dev)
105 {
106     private_module_t* module =
107         reinterpret_cast<private_module_t*>(dev->common.module);
108     fb_context_t *ctx = reinterpret_cast<fb_context_t*>(dev);
109     // already initialized...
110     if (module->framebuffer) {
111         return 0;
112     }
113     char const * const device_template[] = {
114         "/dev/graphics/fb%u",
115         "/dev/fb%u",
116         0 };
117 
118     int fd = -1;
119     int i=0;
120     char name[64];
121     char property[PROPERTY_VALUE_MAX];
122 
123     while ((fd==-1) && device_template[i]) {
124         snprintf(name, 64, device_template[i], 0);
125         fd = open(name, O_RDWR, 0);
126         i++;
127     }
128     if (fd < 0)
129         return -errno;
130 
131     struct fb_fix_screeninfo finfo;
132     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
133         close(fd);
134         return -errno;
135     }
136 
137     struct fb_var_screeninfo info;
138     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
139         close(fd);
140         return -errno;
141     }
142 
143     info.reserved[0] = 0;
144     info.reserved[1] = 0;
145     info.reserved[2] = 0;
146     info.xoffset = 0;
147     info.yoffset = 0;
148     info.activate = FB_ACTIVATE_NOW;
149 
150     /* Interpretation of offset for color fields: All offsets are from the
151      * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide
152      * (means: you can use the offset as right argument to <<). A pixel
153      * afterwards is a bit stream and is written to video memory as that
154      * unmodified. This implies big-endian byte order if bits_per_pixel is
155      * greater than 8.
156      */
157 
158     if(info.bits_per_pixel == 32) {
159         /*
160          * Explicitly request RGBA_8888
161          */
162         info.bits_per_pixel = 32;
163         info.red.offset     = 24;
164         info.red.length     = 8;
165         info.green.offset   = 16;
166         info.green.length   = 8;
167         info.blue.offset    = 8;
168         info.blue.length    = 8;
169         info.transp.offset  = 0;
170         info.transp.length  = 8;
171 
172         /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we
173          * do not use the MDP for composition (i.e. hw composition == 0), ask
174          * for RGBA instead of RGBX. */
175         if (property_get("debug.sf.hw", property, NULL) > 0 &&
176                                                            atoi(property) == 0)
177             module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
178         else if(property_get("debug.composition.type", property, NULL) > 0 &&
179                 (strncmp(property, "mdp", 3) == 0))
180             module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888;
181         else
182             module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888;
183     } else {
184         /*
185          * Explicitly request 5/6/5
186          */
187         info.bits_per_pixel = 16;
188         info.red.offset     = 11;
189         info.red.length     = 5;
190         info.green.offset   = 5;
191         info.green.length   = 6;
192         info.blue.offset    = 0;
193         info.blue.length    = 5;
194         info.transp.offset  = 0;
195         info.transp.length  = 0;
196         module->fbFormat = HAL_PIXEL_FORMAT_RGB_565;
197     }
198 
199     //adreno needs 4k aligned offsets. Max hole size is 4096-1
200     unsigned int size = roundUpToPageSize(info.yres * info.xres *
201                                                (info.bits_per_pixel/8));
202 
203     /*
204      * Request NUM_BUFFERS screens (at least 2 for page flipping)
205      */
206     int numberOfBuffers = (int)(finfo.smem_len/size);
207     ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers);
208 
209     if (property_get("debug.gr.numframebuffers", property, NULL) > 0) {
210         int num = atoi(property);
211         if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) {
212             numberOfBuffers = num;
213         }
214     }
215     if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX)
216         numberOfBuffers = NUM_FRAMEBUFFERS_MAX;
217 
218     ALOGV("We support %d buffers", numberOfBuffers);
219 
220     //consider the included hole by 4k alignment
221     uint32_t line_length = (info.xres * info.bits_per_pixel / 8);
222     info.yres_virtual = (uint32_t) ((size * numberOfBuffers) / line_length);
223 
224     uint32_t flags = PAGE_FLIP;
225 
226     if (info.yres_virtual < ((size * 2) / line_length) ) {
227         // we need at least 2 for page-flipping
228         info.yres_virtual = (int)(size / line_length);
229         flags &= ~PAGE_FLIP;
230         ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
231               info.yres_virtual, info.yres*2);
232     }
233 
234     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
235         close(fd);
236         return -errno;
237     }
238 
239     if (int(info.width) <= 0 || int(info.height) <= 0) {
240         // the driver doesn't return that information
241         // default to 160 dpi
242         info.width  = (uint32_t)(((float)(info.xres) * 25.4f)/160.0f + 0.5f);
243         info.height = (uint32_t)(((float)(info.yres) * 25.4f)/160.0f + 0.5f);
244     }
245 
246     float xdpi = ((float)(info.xres) * 25.4f) / (float)info.width;
247     float ydpi = ((float)(info.yres) * 25.4f) / (float)info.height;
248 
249 #ifdef MSMFB_METADATA_GET
250     struct msmfb_metadata metadata;
251     memset(&metadata, 0 , sizeof(metadata));
252     metadata.op = metadata_op_frame_rate;
253     if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
254         ALOGE("Error retrieving panel frame rate");
255         close(fd);
256         return -errno;
257     }
258     float fps = (float)metadata.data.panel_frame_rate;
259 #else
260     //XXX: Remove reserved field usage on all baselines
261     //The reserved[3] field is used to store FPS by the driver.
262     float fps  = info.reserved[3] & 0xFF;
263 #endif
264     ALOGI("using (fd=%d)\n"
265           "id           = %s\n"
266           "xres         = %d px\n"
267           "yres         = %d px\n"
268           "xres_virtual = %d px\n"
269           "yres_virtual = %d px\n"
270           "bpp          = %d\n"
271           "r            = %2u:%u\n"
272           "g            = %2u:%u\n"
273           "b            = %2u:%u\n",
274           fd,
275           finfo.id,
276           info.xres,
277           info.yres,
278           info.xres_virtual,
279           info.yres_virtual,
280           info.bits_per_pixel,
281           info.red.offset, info.red.length,
282           info.green.offset, info.green.length,
283           info.blue.offset, info.blue.length
284          );
285 
286     ALOGI("width        = %d mm (%f dpi)\n"
287           "height       = %d mm (%f dpi)\n"
288           "refresh rate = %.2f Hz\n",
289           info.width,  xdpi,
290           info.height, ydpi,
291           fps
292          );
293 
294 
295     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
296         close(fd);
297         return -errno;
298     }
299 
300     if (finfo.smem_len <= 0) {
301         close(fd);
302         return -errno;
303     }
304 
305     module->flags = flags;
306     module->info = info;
307     module->finfo = finfo;
308     module->xdpi = xdpi;
309     module->ydpi = ydpi;
310     module->fps = fps;
311     module->swapInterval = 1;
312 
313     CALC_INIT();
314 
315     /*
316      * map the framebuffer
317      */
318 
319     module->numBuffers = info.yres_virtual / info.yres;
320     module->bufferMask = 0;
321     //adreno needs page aligned offsets. Align the fbsize to pagesize.
322     unsigned int fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
323                     module->numBuffers;
324     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
325     if (vaddr == MAP_FAILED) {
326         ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
327         close(fd);
328         return -errno;
329     }
330     //store the framebuffer fd in the ctx
331     ctx->fbFd = fd;
332 #ifdef MSMFB_METADATA_GET
333     memset(&metadata, 0 , sizeof(metadata));
334     metadata.op = metadata_op_get_ion_fd;
335     // get the ION fd for the framebuffer, as GPU needs ION fd
336     if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
337         ALOGE("Error getting ION fd (%s)", strerror(errno));
338         close(fd);
339         return -errno;
340     }
341     if(metadata.data.fbmem_ionfd < 0) {
342         ALOGE("Error: Ioctl returned invalid ION fd = %d",
343                                         metadata.data.fbmem_ionfd);
344         close(fd);
345         return -errno;
346     }
347     fd = metadata.data.fbmem_ionfd;
348 #endif
349     // Create framebuffer handle using the ION fd
350     module->framebuffer = new private_handle_t(fd, fbSize,
351                                         private_handle_t::PRIV_FLAGS_USES_ION,
352                                         BUFFER_TYPE_UI,
353                                         module->fbFormat, info.xres, info.yres);
354     module->framebuffer->base = uint64_t(vaddr);
355     memset(vaddr, 0, fbSize);
356     //Enable vsync
357     int enable = 1;
358     ioctl(ctx->fbFd, MSMFB_OVERLAY_VSYNC_CTRL, &enable);
359     return 0;
360 }
361 
mapFrameBuffer(framebuffer_device_t * dev)362 static int mapFrameBuffer(framebuffer_device_t *dev)
363 {
364     int err = -1;
365     char property[PROPERTY_VALUE_MAX];
366     if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
367        (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
368         (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
369         private_module_t* module =
370             reinterpret_cast<private_module_t*>(dev->common.module);
371         pthread_mutex_lock(&module->lock);
372         err = mapFrameBufferLocked(dev);
373         pthread_mutex_unlock(&module->lock);
374     }
375     return err;
376 }
377 
378 /*****************************************************************************/
379 
fb_close(struct hw_device_t * dev)380 static int fb_close(struct hw_device_t *dev)
381 {
382     fb_context_t* ctx = (fb_context_t*)dev;
383     if (ctx) {
384 #ifdef MSMFB_METADATA_GET
385         if(ctx->fbFd >=0) {
386             close(ctx->fbFd);
387         }
388 #endif
389         //Hack until fbdev is removed. Framework could close this causing hwc a
390         //pain.
391         //free(ctx);
392     }
393     return 0;
394 }
395 
fb_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)396 int fb_device_open(hw_module_t const* module, const char* name,
397                    hw_device_t** device)
398 {
399     int status = -EINVAL;
400     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
401         alloc_device_t* gralloc_device;
402         status = gralloc_open(module, &gralloc_device);
403         if (status < 0)
404             return status;
405 
406         /* initialize our state here */
407         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
408         if(dev == NULL) {
409             gralloc_close(gralloc_device);
410             return status;
411         }
412         memset(dev, 0, sizeof(*dev));
413 
414         /* initialize the procs */
415         dev->device.common.tag      = HARDWARE_DEVICE_TAG;
416         dev->device.common.version  = 0;
417         dev->device.common.module   = const_cast<hw_module_t*>(module);
418         dev->device.common.close    = fb_close;
419         dev->device.setSwapInterval = fb_setSwapInterval;
420         dev->device.post            = fb_post;
421         dev->device.setUpdateRect   = 0;
422         dev->device.compositionComplete = fb_compositionComplete;
423 
424         status = mapFrameBuffer((framebuffer_device_t*)dev);
425         private_module_t* m = (private_module_t*)dev->device.common.module;
426         if (status >= 0) {
427             int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
428             const_cast<uint32_t&>(dev->device.flags) = 0;
429             const_cast<uint32_t&>(dev->device.width) = m->info.xres;
430             const_cast<uint32_t&>(dev->device.height) = m->info.yres;
431             const_cast<int&>(dev->device.stride) = stride;
432             const_cast<int&>(dev->device.format) = m->fbFormat;
433             const_cast<float&>(dev->device.xdpi) = m->xdpi;
434             const_cast<float&>(dev->device.ydpi) = m->ydpi;
435             const_cast<float&>(dev->device.fps) = m->fps;
436             const_cast<int&>(dev->device.minSwapInterval) =
437                                                         PRIV_MIN_SWAP_INTERVAL;
438             const_cast<int&>(dev->device.maxSwapInterval) =
439                                                         PRIV_MAX_SWAP_INTERVAL;
440             const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
441             dev->device.setUpdateRect = 0;
442 
443             *device = &dev->device.common;
444         }
445 
446         // Close the gralloc module
447         gralloc_close(gralloc_device);
448     }
449     return status;
450 }
451