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