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 #include <linux/fb.h>
37 #include <linux/msm_mdp.h>
38
39 #include <GLES/gl.h>
40
41 #include "gralloc_priv.h"
42 #include "gr.h"
43
44 /*****************************************************************************/
45
46 // numbers of buffers for page flipping
47 #define NUM_BUFFERS 2
48
49
50 enum {
51 PAGE_FLIP = 0x00000001,
52 LOCKED = 0x00000002
53 };
54
55 struct fb_context_t {
56 framebuffer_device_t device;
57 };
58
59 /*****************************************************************************/
60
61 static void
62 msm_copy_buffer(buffer_handle_t handle, int fd, int width, int height,
63 int x, int y, int w, int h);
64
fb_setSwapInterval(struct framebuffer_device_t * dev,int interval)65 static int fb_setSwapInterval(struct framebuffer_device_t* dev,
66 int interval)
67 {
68 fb_context_t* ctx = (fb_context_t*)dev;
69 if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
70 return -EINVAL;
71 // FIXME: implement fb_setSwapInterval
72 return 0;
73 }
74
fb_setUpdateRect(struct framebuffer_device_t * dev,int l,int t,int w,int h)75 static int fb_setUpdateRect(struct framebuffer_device_t* dev,
76 int l, int t, int w, int h)
77 {
78 if (((w|h) <= 0) || ((l|t)<0))
79 return -EINVAL;
80
81 fb_context_t* ctx = (fb_context_t*)dev;
82 private_module_t* m = reinterpret_cast<private_module_t*>(
83 dev->common.module);
84 m->info.reserved[0] = 0x54445055; // "UPDT";
85 m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
86 m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
87 return 0;
88 }
89
fb_post(struct framebuffer_device_t * dev,buffer_handle_t buffer)90 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
91 {
92 if (private_handle_t::validate(buffer) < 0)
93 return -EINVAL;
94
95 fb_context_t* ctx = (fb_context_t*)dev;
96
97 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
98 private_module_t* m = reinterpret_cast<private_module_t*>(
99 dev->common.module);
100
101 if (m->currentBuffer) {
102 m->base.unlock(&m->base, m->currentBuffer);
103 m->currentBuffer = 0;
104 }
105
106 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
107
108 m->base.lock(&m->base, buffer,
109 private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
110 0, 0, m->info.xres, m->info.yres, NULL);
111
112 const size_t offset = hnd->base - m->framebuffer->base;
113 m->info.activate = FB_ACTIVATE_VBL;
114 m->info.yoffset = offset / m->finfo.line_length;
115 if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
116 LOGE("FBIOPUT_VSCREENINFO failed");
117 m->base.unlock(&m->base, buffer);
118 return -errno;
119 }
120 m->currentBuffer = buffer;
121
122 } else {
123 // If we can't do the page_flip, just copy the buffer to the front
124 // FIXME: use copybit HAL instead of memcpy
125
126 void* fb_vaddr;
127 void* buffer_vaddr;
128
129 m->base.lock(&m->base, m->framebuffer,
130 GRALLOC_USAGE_SW_WRITE_RARELY,
131 0, 0, m->info.xres, m->info.yres,
132 &fb_vaddr);
133
134 m->base.lock(&m->base, buffer,
135 GRALLOC_USAGE_SW_READ_RARELY,
136 0, 0, m->info.xres, m->info.yres,
137 &buffer_vaddr);
138
139 //memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
140
141 msm_copy_buffer(m->framebuffer, m->framebuffer->fd,
142 m->info.xres, m->info.yres,
143 m->info.xoffset, m->info.yoffset,
144 m->info.width, m->info.height);
145
146 m->base.unlock(&m->base, buffer);
147 m->base.unlock(&m->base, m->framebuffer);
148 }
149
150 return 0;
151 }
152
fb_compositionComplete(struct framebuffer_device_t * dev)153 static int fb_compositionComplete(struct framebuffer_device_t* dev)
154 {
155 // STOPSHIP: Properly implement composition complete callback
156 glFinish();
157
158 return 0;
159 }
160
161 /*****************************************************************************/
162
mapFrameBufferLocked(struct private_module_t * module)163 int mapFrameBufferLocked(struct private_module_t* module)
164 {
165 // already initialized...
166 if (module->framebuffer) {
167 return 0;
168 }
169
170 char const * const device_template[] = {
171 "/dev/graphics/fb%u",
172 "/dev/fb%u",
173 0 };
174
175 int fd = -1;
176 int i=0;
177 char name[64];
178
179 while ((fd==-1) && device_template[i]) {
180 snprintf(name, 64, device_template[i], 0);
181 fd = open(name, O_RDWR, 0);
182 i++;
183 }
184 if (fd < 0)
185 return -errno;
186
187 struct fb_fix_screeninfo finfo;
188 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
189 return -errno;
190
191 struct fb_var_screeninfo info;
192 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
193 return -errno;
194
195 info.reserved[0] = 0;
196 info.reserved[1] = 0;
197 info.reserved[2] = 0;
198 info.xoffset = 0;
199 info.yoffset = 0;
200 info.activate = FB_ACTIVATE_NOW;
201
202 /*
203 * Explicitly request 5/6/5
204 */
205 info.bits_per_pixel = 16;
206 info.red.offset = 11;
207 info.red.length = 5;
208 info.green.offset = 5;
209 info.green.length = 6;
210 info.blue.offset = 0;
211 info.blue.length = 5;
212 info.transp.offset = 0;
213 info.transp.length = 0;
214
215 /*
216 * Request NUM_BUFFERS screens (at lest 2 for page flipping)
217 */
218 info.yres_virtual = info.yres * NUM_BUFFERS;
219
220
221 uint32_t flags = PAGE_FLIP;
222 if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
223 info.yres_virtual = info.yres;
224 flags &= ~PAGE_FLIP;
225 LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
226 }
227
228 if (info.yres_virtual < info.yres * 2) {
229 // we need at least 2 for page-flipping
230 info.yres_virtual = info.yres;
231 flags &= ~PAGE_FLIP;
232 LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
233 info.yres_virtual, info.yres*2);
234 }
235
236 if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
237 return -errno;
238
239 int refreshRate = 1000000000000000LLU /
240 (
241 uint64_t( info.upper_margin + info.lower_margin + info.yres )
242 * ( info.left_margin + info.right_margin + info.xres )
243 * info.pixclock
244 );
245
246 if (refreshRate == 0) {
247 // bleagh, bad info from the driver
248 refreshRate = 60*1000; // 60 Hz
249 }
250
251 if (int(info.width) <= 0 || int(info.height) <= 0) {
252 // the driver doesn't return that information
253 // default to 160 dpi
254 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
255 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
256 }
257
258 float xdpi = (info.xres * 25.4f) / info.width;
259 float ydpi = (info.yres * 25.4f) / info.height;
260 float fps = refreshRate / 1000.0f;
261
262 LOGI( "using (fd=%d)\n"
263 "id = %s\n"
264 "xres = %d px\n"
265 "yres = %d px\n"
266 "xres_virtual = %d px\n"
267 "yres_virtual = %d px\n"
268 "bpp = %d\n"
269 "r = %2u:%u\n"
270 "g = %2u:%u\n"
271 "b = %2u:%u\n",
272 fd,
273 finfo.id,
274 info.xres,
275 info.yres,
276 info.xres_virtual,
277 info.yres_virtual,
278 info.bits_per_pixel,
279 info.red.offset, info.red.length,
280 info.green.offset, info.green.length,
281 info.blue.offset, info.blue.length
282 );
283
284 LOGI( "width = %d mm (%f dpi)\n"
285 "height = %d mm (%f dpi)\n"
286 "refresh rate = %.2f Hz\n",
287 info.width, xdpi,
288 info.height, ydpi,
289 fps
290 );
291
292
293 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
294 return -errno;
295
296 if (finfo.smem_len <= 0)
297 return -errno;
298
299
300 module->flags = flags;
301 module->info = info;
302 module->finfo = finfo;
303 module->xdpi = xdpi;
304 module->ydpi = ydpi;
305 module->fps = fps;
306
307 /*
308 * map the framebuffer
309 */
310
311 int err;
312 size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual);
313 module->framebuffer = new private_handle_t(dup(fd), fbSize,
314 private_handle_t::PRIV_FLAGS_USES_PMEM);
315
316 module->numBuffers = info.yres_virtual / info.yres;
317 module->bufferMask = 0;
318
319 void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
320 if (vaddr == MAP_FAILED) {
321 LOGE("Error mapping the framebuffer (%s)", strerror(errno));
322 return -errno;
323 }
324 module->framebuffer->base = intptr_t(vaddr);
325 module->framebuffer->phys = intptr_t(finfo.smem_start);
326 memset(vaddr, 0, fbSize);
327 return 0;
328 }
329
mapFrameBuffer(struct private_module_t * module)330 static int mapFrameBuffer(struct private_module_t* module)
331 {
332 pthread_mutex_lock(&module->lock);
333 int err = mapFrameBufferLocked(module);
334 pthread_mutex_unlock(&module->lock);
335 return err;
336 }
337
338 /*****************************************************************************/
339
fb_close(struct hw_device_t * dev)340 static int fb_close(struct hw_device_t *dev)
341 {
342 fb_context_t* ctx = (fb_context_t*)dev;
343 if (ctx) {
344 free(ctx);
345 }
346 return 0;
347 }
348
fb_device_open(hw_module_t const * module,const char * name,hw_device_t ** device)349 int fb_device_open(hw_module_t const* module, const char* name,
350 hw_device_t** device)
351 {
352 int status = -EINVAL;
353 if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
354 alloc_device_t* gralloc_device;
355 status = gralloc_open(module, &gralloc_device);
356 if (status < 0)
357 return status;
358
359 /* initialize our state here */
360 fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
361 memset(dev, 0, sizeof(*dev));
362
363 /* initialize the procs */
364 dev->device.common.tag = HARDWARE_DEVICE_TAG;
365 dev->device.common.version = 0;
366 dev->device.common.module = const_cast<hw_module_t*>(module);
367 dev->device.common.close = fb_close;
368 dev->device.setSwapInterval = fb_setSwapInterval;
369 dev->device.post = fb_post;
370 dev->device.setUpdateRect = 0;
371 dev->device.compositionComplete = fb_compositionComplete;
372
373 private_module_t* m = (private_module_t*)module;
374 status = mapFrameBuffer(m);
375 if (status >= 0) {
376 int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
377 const_cast<uint32_t&>(dev->device.flags) = 0;
378 const_cast<uint32_t&>(dev->device.width) = m->info.xres;
379 const_cast<uint32_t&>(dev->device.height) = m->info.yres;
380 const_cast<int&>(dev->device.stride) = stride;
381 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565;
382 const_cast<float&>(dev->device.xdpi) = m->xdpi;
383 const_cast<float&>(dev->device.ydpi) = m->ydpi;
384 const_cast<float&>(dev->device.fps) = m->fps;
385 const_cast<int&>(dev->device.minSwapInterval) = 1;
386 const_cast<int&>(dev->device.maxSwapInterval) = 1;
387
388 if (m->finfo.reserved[0] == 0x5444 &&
389 m->finfo.reserved[1] == 0x5055) {
390 dev->device.setUpdateRect = fb_setUpdateRect;
391 LOGD("UPDATE_ON_DEMAND supported");
392 }
393
394 *device = &dev->device.common;
395 }
396 }
397 return status;
398 }
399
400 /* Copy a pmem buffer to the framebuffer */
401
402 static void
msm_copy_buffer(buffer_handle_t handle,int fd,int width,int height,int x,int y,int w,int h)403 msm_copy_buffer(buffer_handle_t handle, int fd, int width, int height,
404 int x, int y, int w, int h)
405 {
406 struct {
407 unsigned int count;
408 mdp_blit_req req;
409 } blit;
410 private_handle_t *priv = (private_handle_t*) handle;
411
412 memset(&blit, 0, sizeof(blit));
413 blit.count = 1;
414
415 blit.req.flags = 0;
416 blit.req.alpha = 0xff;
417 blit.req.transp_mask = 0xffffffff;
418
419 blit.req.src.width = width;
420 blit.req.src.height = height;
421 blit.req.src.offset = 0;
422 blit.req.src.memory_id = priv->fd;
423
424 blit.req.dst.width = width;
425 blit.req.dst.height = height;
426 blit.req.dst.offset = 0;
427 blit.req.dst.memory_id = fd;
428 blit.req.dst.format = MDP_RGB_565;
429
430 blit.req.src_rect.x = blit.req.dst_rect.x = x;
431 blit.req.src_rect.y = blit.req.dst_rect.y = y;
432 blit.req.src_rect.w = blit.req.dst_rect.w = w;
433 blit.req.src_rect.h = blit.req.dst_rect.h = h;
434
435 if (ioctl(fd, MSMFB_BLIT, &blit))
436 LOGE("MSMFB_BLIT failed = %d", -errno);
437 }
438