• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * Copyright (c) 2010-2013 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 = 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     glFinish();
105 
106     return 0;
107 }
108 
mapFrameBufferLocked(struct private_module_t * module)109 int mapFrameBufferLocked(struct private_module_t* module)
110 {
111     // already initialized...
112     if (module->framebuffer) {
113         return 0;
114     }
115     char const * const device_template[] = {
116         "/dev/graphics/fb%u",
117         "/dev/fb%u",
118         0 };
119 
120     int fd = -1;
121     int i=0;
122     char name[64];
123     char property[PROPERTY_VALUE_MAX];
124 
125     while ((fd==-1) && device_template[i]) {
126         snprintf(name, 64, device_template[i], 0);
127         fd = open(name, O_RDWR, 0);
128         i++;
129     }
130     if (fd < 0)
131         return -errno;
132 
133     memset(&module->commit, 0, sizeof(struct mdp_display_commit));
134 
135     struct fb_fix_screeninfo finfo;
136     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
137         return -errno;
138 
139     struct fb_var_screeninfo info;
140     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
141         return -errno;
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     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 = (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 = 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         return -errno;
236 
237     if (int(info.width) <= 0 || int(info.height) <= 0) {
238         // the driver doesn't return that information
239         // default to 160 dpi
240         info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
241         info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
242     }
243 
244     float xdpi = (info.xres * 25.4f) / info.width;
245     float ydpi = (info.yres * 25.4f) / info.height;
246 #ifdef MSMFB_METADATA_GET
247     struct msmfb_metadata metadata;
248     memset(&metadata, 0 , sizeof(metadata));
249     metadata.op = metadata_op_frame_rate;
250     if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) {
251         ALOGE("Error retrieving panel frame rate");
252         return -errno;
253     }
254     float fps  = metadata.data.panel_frame_rate;
255 #else
256     //XXX: Remove reserved field usage on all baselines
257     //The reserved[3] field is used to store FPS by the driver.
258     float fps  = info.reserved[3] & 0xFF;
259 #endif
260     ALOGI("using (fd=%d)\n"
261           "id           = %s\n"
262           "xres         = %d px\n"
263           "yres         = %d px\n"
264           "xres_virtual = %d px\n"
265           "yres_virtual = %d px\n"
266           "bpp          = %d\n"
267           "r            = %2u:%u\n"
268           "g            = %2u:%u\n"
269           "b            = %2u:%u\n",
270           fd,
271           finfo.id,
272           info.xres,
273           info.yres,
274           info.xres_virtual,
275           info.yres_virtual,
276           info.bits_per_pixel,
277           info.red.offset, info.red.length,
278           info.green.offset, info.green.length,
279           info.blue.offset, info.blue.length
280          );
281 
282     ALOGI("width        = %d mm (%f dpi)\n"
283           "height       = %d mm (%f dpi)\n"
284           "refresh rate = %.2f Hz\n",
285           info.width,  xdpi,
286           info.height, ydpi,
287           fps
288          );
289 
290 
291     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
292         return -errno;
293 
294     if (finfo.smem_len <= 0)
295         return -errno;
296 
297     module->flags = flags;
298     module->info = info;
299     module->finfo = finfo;
300     module->xdpi = xdpi;
301     module->ydpi = ydpi;
302     module->fps = fps;
303     module->swapInterval = 1;
304 
305     CALC_INIT();
306 
307     /*
308      * map the framebuffer
309      */
310 
311     module->numBuffers = info.yres_virtual / info.yres;
312     module->bufferMask = 0;
313     //adreno needs page aligned offsets. Align the fbsize to pagesize.
314     size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)*
315                     module->numBuffers;
316     module->framebuffer = new private_handle_t(fd, fbSize,
317                                         private_handle_t::PRIV_FLAGS_USES_ION,
318                                         BUFFER_TYPE_UI,
319                                         module->fbFormat, info.xres, info.yres);
320     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
321     if (vaddr == MAP_FAILED) {
322         ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
323         return -errno;
324     }
325     module->framebuffer->base = intptr_t(vaddr);
326     memset(vaddr, 0, fbSize);
327     module->currentOffset = 0;
328     //Enable vsync
329     int enable = 1;
330     ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL,
331              &enable);
332     return 0;
333 }
334 
mapFrameBuffer(struct private_module_t * module)335 static int mapFrameBuffer(struct private_module_t* module)
336 {
337     pthread_mutex_lock(&module->lock);
338     int err = mapFrameBufferLocked(module);
339     pthread_mutex_unlock(&module->lock);
340     return err;
341 }
342 
343 /*****************************************************************************/
344 
fb_close(struct hw_device_t * dev)345 static int fb_close(struct hw_device_t *dev)
346 {
347     fb_context_t* ctx = (fb_context_t*)dev;
348     if (ctx) {
349         free(ctx);
350     }
351     return 0;
352 }
353 
fb_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)354 int fb_device_open(hw_module_t const* module, const char* name,
355                    hw_device_t** device)
356 {
357     int status = -EINVAL;
358     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
359         alloc_device_t* gralloc_device;
360         status = gralloc_open(module, &gralloc_device);
361         if (status < 0)
362             return status;
363 
364         /* initialize our state here */
365         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
366         memset(dev, 0, sizeof(*dev));
367 
368         /* initialize the procs */
369         dev->device.common.tag      = HARDWARE_DEVICE_TAG;
370         dev->device.common.version  = 0;
371         dev->device.common.module   = const_cast<hw_module_t*>(module);
372         dev->device.common.close    = fb_close;
373         dev->device.setSwapInterval = fb_setSwapInterval;
374         dev->device.post            = fb_post;
375         dev->device.setUpdateRect   = 0;
376         dev->device.compositionComplete = fb_compositionComplete;
377 
378         private_module_t* m = (private_module_t*)module;
379         status = mapFrameBuffer(m);
380         if (status >= 0) {
381             int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
382             const_cast<uint32_t&>(dev->device.flags) = 0;
383             const_cast<uint32_t&>(dev->device.width) = m->info.xres;
384             const_cast<uint32_t&>(dev->device.height) = m->info.yres;
385             const_cast<int&>(dev->device.stride) = stride;
386             const_cast<int&>(dev->device.format) = m->fbFormat;
387             const_cast<float&>(dev->device.xdpi) = m->xdpi;
388             const_cast<float&>(dev->device.ydpi) = m->ydpi;
389             const_cast<float&>(dev->device.fps) = m->fps;
390             const_cast<int&>(dev->device.minSwapInterval) =
391                                                         PRIV_MIN_SWAP_INTERVAL;
392             const_cast<int&>(dev->device.maxSwapInterval) =
393                                                         PRIV_MAX_SWAP_INTERVAL;
394             const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers;
395             dev->device.setUpdateRect = 0;
396 
397             *device = &dev->device.common;
398         }
399 
400         // Close the gralloc module
401         gralloc_close(gralloc_device);
402     }
403     return status;
404 }
405