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