1 /* 2 * Copyright (C) 2011 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 #include <string.h> 17 #include <pthread.h> 18 #ifdef HAVE_ANDROID_OS // just want PAGE_SIZE define 19 # include <asm/page.h> 20 #else 21 # include <sys/user.h> 22 #endif 23 #include <cutils/ashmem.h> 24 #include <unistd.h> 25 #include <errno.h> 26 #include <dlfcn.h> 27 #include <sys/mman.h> 28 #include "gralloc_cb.h" 29 #include "HostConnection.h" 30 #include "glUtils.h" 31 #include <cutils/log.h> 32 #include <cutils/properties.h> 33 34 /* Set to 1 or 2 to enable debug traces */ 35 #define DEBUG 0 36 37 #if DEBUG >= 1 38 # define D(...) LOGD(__VA_ARGS__) 39 #else 40 # define D(...) ((void)0) 41 #endif 42 43 #if DEBUG >= 2 44 # define DD(...) LOGD(__VA_ARGS__) 45 #else 46 # define DD(...) ((void)0) 47 #endif 48 49 #define DBG_FUNC DBG("%s\n", __FUNCTION__) 50 // 51 // our private gralloc module structure 52 // 53 struct private_module_t { 54 gralloc_module_t base; 55 }; 56 57 /* If not NULL, this is a pointer to the fallback module. 58 * This really is gralloc.default, which we'll use if we detect 59 * that the emulator we're running in does not support GPU emulation. 60 */ 61 static gralloc_module_t* sFallback; 62 static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT; 63 64 static void fallback_init(void); // forward 65 66 67 typedef struct _alloc_list_node { 68 buffer_handle_t handle; 69 _alloc_list_node *next; 70 _alloc_list_node *prev; 71 } AllocListNode; 72 73 // 74 // Our gralloc device structure (alloc interface) 75 // 76 struct gralloc_device_t { 77 alloc_device_t device; 78 79 AllocListNode *allocListHead; // double linked list of allocated buffers 80 pthread_mutex_t lock; 81 }; 82 83 // 84 // Our framebuffer device structure 85 // 86 struct fb_device_t { 87 framebuffer_device_t device; 88 }; 89 90 static int map_buffer(cb_handle_t *cb, void **vaddr) 91 { 92 if (cb->fd < 0 || cb->ashmemSize <= 0) { 93 return -EINVAL; 94 } 95 96 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE, 97 MAP_SHARED, cb->fd, 0); 98 if (addr == MAP_FAILED) { 99 return -errno; 100 } 101 102 cb->ashmemBase = intptr_t(addr); 103 cb->ashmemBasePid = getpid(); 104 105 *vaddr = addr; 106 return 0; 107 } 108 109 #define DEFINE_HOST_CONNECTION \ 110 HostConnection *hostCon = HostConnection::get(); \ 111 renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL) 112 113 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \ 114 HostConnection *hostCon = HostConnection::get(); \ 115 if (!hostCon) { \ 116 LOGE("gralloc: Failed to get host connection\n"); \ 117 return -EIO; \ 118 } \ 119 renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \ 120 if (!rcEnc) { \ 121 LOGE("gralloc: Failed to get renderControl encoder context\n"); \ 122 return -EIO; \ 123 } 124 125 126 // 127 // gralloc device functions (alloc interface) 128 // 129 static int gralloc_alloc(alloc_device_t* dev, 130 int w, int h, int format, int usage, 131 buffer_handle_t* pHandle, int* pStride) 132 { 133 D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage); 134 135 gralloc_device_t *grdev = (gralloc_device_t *)dev; 136 if (!grdev || !pHandle || !pStride) 137 return -EINVAL; 138 139 // 140 // Validate usage: buffer cannot be written both by s/w and h/w access. 141 // 142 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); 143 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); 144 if (hw_write && sw_write) { 145 return -EINVAL; 146 } 147 148 int ashmem_size = 0; 149 *pStride = 0; 150 GLenum glFormat = 0; 151 GLenum glType = 0; 152 153 int bpp = 0; 154 switch (format) { 155 case HAL_PIXEL_FORMAT_RGBA_8888: 156 case HAL_PIXEL_FORMAT_RGBX_8888: 157 case HAL_PIXEL_FORMAT_BGRA_8888: 158 bpp = 4; 159 glFormat = GL_RGBA; 160 glType = GL_UNSIGNED_BYTE; 161 break; 162 case HAL_PIXEL_FORMAT_RGB_888: 163 bpp = 3; 164 glFormat = GL_RGB; 165 glType = GL_UNSIGNED_BYTE; 166 break; 167 case HAL_PIXEL_FORMAT_RGB_565: 168 bpp = 2; 169 glFormat = GL_RGB; 170 glType = GL_UNSIGNED_SHORT_5_6_5; 171 break; 172 case HAL_PIXEL_FORMAT_RGBA_5551: 173 bpp = 2; 174 glFormat = GL_RGB5_A1_OES; 175 glType = GL_UNSIGNED_SHORT_5_5_5_1; 176 break; 177 case HAL_PIXEL_FORMAT_RGBA_4444: 178 bpp = 2; 179 glFormat = GL_RGBA4_OES; 180 glType = GL_UNSIGNED_SHORT_4_4_4_4; 181 break; 182 183 default: 184 return -EINVAL; 185 } 186 187 if (usage & GRALLOC_USAGE_HW_FB) { 188 // keep space for postCounter 189 ashmem_size += sizeof(uint32_t); 190 } 191 192 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { 193 // keep space for image on guest memory if SW access is needed 194 int align = 1; 195 size_t bpr = (w*bpp + (align-1)) & ~(align-1); 196 ashmem_size += (bpr * h); 197 *pStride = bpr / bpp; 198 } 199 200 D("gralloc_alloc ashmem_size=%d, tid %d\n", ashmem_size, gettid()); 201 202 // 203 // Allocate space in ashmem if needed 204 // 205 int fd = -1; 206 if (ashmem_size > 0) { 207 // round to page size; 208 ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); 209 210 fd = ashmem_create_region("gralloc-buffer", ashmem_size); 211 if (fd < 0) { 212 LOGE("gralloc_alloc failed to create ashmem region: %s\n", strerror(errno)); 213 return -errno; 214 } 215 } 216 217 cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage, 218 w, h, glFormat, glType); 219 220 if (ashmem_size > 0) { 221 // 222 // map ashmem region if exist 223 // 224 void *vaddr; 225 int err = map_buffer(cb, &vaddr); 226 if (err) { 227 close(fd); 228 delete cb; 229 return err; 230 } 231 232 cb->setFd(fd); 233 } 234 235 // 236 // Allocate ColorBuffer handle on the host (only if h/w access is allowed) 237 // 238 if (usage & GRALLOC_USAGE_HW_MASK) { 239 DEFINE_HOST_CONNECTION; 240 if (hostCon && rcEnc) { 241 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat); 242 D("Created host ColorBuffer 0x%x\n", cb->hostHandle); 243 } 244 245 if (!cb->hostHandle) { 246 // Could not create colorbuffer on host !!! 247 close(fd); 248 delete cb; 249 return -EIO; 250 } 251 } 252 253 // 254 // alloc succeeded - insert the allocated handle to the allocated list 255 // 256 AllocListNode *node = new AllocListNode(); 257 pthread_mutex_lock(&grdev->lock); 258 node->handle = cb; 259 node->next = grdev->allocListHead; 260 node->prev = NULL; 261 if (grdev->allocListHead) { 262 grdev->allocListHead->prev = node; 263 } 264 grdev->allocListHead = node; 265 pthread_mutex_unlock(&grdev->lock); 266 267 *pHandle = cb; 268 return 0; 269 } 270 271 static int gralloc_free(alloc_device_t* dev, 272 buffer_handle_t handle) 273 { 274 const cb_handle_t *cb = (const cb_handle_t *)handle; 275 if (!cb_handle_t::validate((cb_handle_t*)cb)) { 276 ERR("gralloc_free: invalid handle"); 277 return -EINVAL; 278 } 279 280 if (cb->hostHandle != 0) { 281 DEFINE_AND_VALIDATE_HOST_CONNECTION; 282 D("Destroying host ColorBuffer 0x%x\n", cb->hostHandle); 283 rcEnc->rcDestroyColorBuffer(rcEnc, cb->hostHandle); 284 } 285 286 // 287 // detach and unmap ashmem area if present 288 // 289 if (cb->fd > 0) { 290 if (cb->ashmemSize > 0 && cb->ashmemBase) { 291 munmap((void *)cb->ashmemBase, cb->ashmemSize); 292 } 293 close(cb->fd); 294 } 295 296 // remove it from the allocated list 297 gralloc_device_t *grdev = (gralloc_device_t *)dev; 298 pthread_mutex_lock(&grdev->lock); 299 AllocListNode *n = grdev->allocListHead; 300 while( n && n->handle != cb ) { 301 n = n->next; 302 } 303 if (n) { 304 // buffer found on list - remove it from list 305 if (n->next) { 306 n->next->prev = n->prev; 307 } 308 if (n->prev) { 309 n->prev->next = n->next; 310 } 311 else { 312 grdev->allocListHead = n->next; 313 } 314 315 delete n; 316 } 317 pthread_mutex_unlock(&grdev->lock); 318 319 delete cb; 320 321 return 0; 322 } 323 324 static int gralloc_device_close(struct hw_device_t *dev) 325 { 326 gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev); 327 if (d) { 328 329 // free still allocated buffers 330 while( d->allocListHead != NULL ) { 331 gralloc_free(&d->device, d->allocListHead->handle); 332 } 333 334 // free device 335 free(d); 336 } 337 return 0; 338 } 339 340 static int fb_compositionComplete(struct framebuffer_device_t* dev) 341 { 342 return 0; 343 } 344 345 // 346 // Framebuffer device functions 347 // 348 static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 349 { 350 fb_device_t *fbdev = (fb_device_t *)dev; 351 cb_handle_t *cb = (cb_handle_t *)buffer; 352 353 if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) { 354 return -EINVAL; 355 } 356 357 // Make sure we have host connection 358 DEFINE_AND_VALIDATE_HOST_CONNECTION; 359 360 // increment the post count of the buffer 361 uint32_t *postCountPtr = (uint32_t *)cb->ashmemBase; 362 if (!postCountPtr) { 363 // This should not happen 364 return -EINVAL; 365 } 366 (*postCountPtr)++; 367 368 // send post request to host 369 rcEnc->rcFBPost(rcEnc, cb->hostHandle); 370 hostCon->flush(); 371 372 return 0; 373 } 374 375 static int fb_setUpdateRect(struct framebuffer_device_t* dev, 376 int l, int t, int w, int h) 377 { 378 fb_device_t *fbdev = (fb_device_t *)dev; 379 380 if (!fbdev) { 381 return -EINVAL; 382 } 383 384 // Make sure we have host connection 385 DEFINE_AND_VALIDATE_HOST_CONNECTION; 386 387 // send request to host 388 // TODO: XXX - should be implemented 389 //rcEnc->rc_XXX 390 391 return 0; 392 } 393 394 static int fb_setSwapInterval(struct framebuffer_device_t* dev, 395 int interval) 396 { 397 fb_device_t *fbdev = (fb_device_t *)dev; 398 399 if (!fbdev) { 400 return -EINVAL; 401 } 402 403 // Make sure we have host connection 404 DEFINE_AND_VALIDATE_HOST_CONNECTION; 405 406 // send request to host 407 rcEnc->rcFBSetSwapInterval(rcEnc, interval); 408 hostCon->flush(); 409 410 return 0; 411 } 412 413 static int fb_close(struct hw_device_t *dev) 414 { 415 fb_device_t *fbdev = (fb_device_t *)dev; 416 417 delete fbdev; 418 419 return 0; 420 } 421 422 423 // 424 // gralloc module functions - refcount + locking interface 425 // 426 static int gralloc_register_buffer(gralloc_module_t const* module, 427 buffer_handle_t handle) 428 { 429 pthread_once(&sFallbackOnce, fallback_init); 430 if (sFallback != NULL) { 431 return sFallback->registerBuffer(sFallback, handle); 432 } 433 434 D("gralloc_register_buffer(%p) called", handle); 435 436 private_module_t *gr = (private_module_t *)module; 437 cb_handle_t *cb = (cb_handle_t *)handle; 438 if (!gr || !cb_handle_t::validate(cb)) { 439 ERR("gralloc_register_buffer(%p): invalid buffer", cb); 440 return -EINVAL; 441 } 442 443 // 444 // if the color buffer has ashmem region and it is not mapped in this 445 // process map it now. 446 // 447 if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) { 448 void *vaddr; 449 int err = map_buffer(cb, &vaddr); 450 if (err) { 451 ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err)); 452 return -err; 453 } 454 cb->mappedPid = getpid(); 455 } 456 457 return 0; 458 } 459 460 static int gralloc_unregister_buffer(gralloc_module_t const* module, 461 buffer_handle_t handle) 462 { 463 if (sFallback != NULL) { 464 return sFallback->unregisterBuffer(sFallback, handle); 465 } 466 467 private_module_t *gr = (private_module_t *)module; 468 cb_handle_t *cb = (cb_handle_t *)handle; 469 if (!gr || !cb_handle_t::validate(cb)) { 470 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb); 471 return -EINVAL; 472 } 473 474 // 475 // unmap ashmem region if it was previously mapped in this process 476 // (through register_buffer) 477 // 478 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) { 479 void *vaddr; 480 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize); 481 if (err) { 482 ERR("gralloc_unregister_buffer(%p): unmap failed", cb); 483 return -EINVAL; 484 } 485 cb->ashmemBase = NULL; 486 cb->mappedPid = 0; 487 } 488 489 D("gralloc_unregister_buffer(%p) done\n", cb); 490 491 return 0; 492 } 493 494 static int gralloc_lock(gralloc_module_t const* module, 495 buffer_handle_t handle, int usage, 496 int l, int t, int w, int h, 497 void** vaddr) 498 { 499 if (sFallback != NULL) { 500 return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr); 501 } 502 503 private_module_t *gr = (private_module_t *)module; 504 cb_handle_t *cb = (cb_handle_t *)handle; 505 if (!gr || !cb_handle_t::validate(cb)) { 506 LOGE("gralloc_lock bad handle\n"); 507 return -EINVAL; 508 } 509 510 // Validate usage, 511 // 1. cannot be locked for hw access 512 // 2. lock for either sw read or write. 513 // 3. locked sw access must match usage during alloc time. 514 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); 515 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); 516 bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE); 517 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); 518 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK)); 519 bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK)); 520 521 if ( (hw_read || hw_write) || 522 (!sw_read && !sw_write) || 523 (sw_read && !sw_read_allowed) || 524 (sw_write && !sw_write_allowed) ) { 525 LOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage, cb->usage); 526 return -EINVAL; 527 } 528 529 EGLint postCount = 0; 530 void *cpu_addr = NULL; 531 532 // 533 // make sure ashmem area is mapped if needed 534 // 535 if (cb->canBePosted() || sw_read || sw_write) { 536 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) { 537 return -EACCES; 538 } 539 540 if (cb->canBePosted()) { 541 postCount = *((int *)cb->ashmemBase); 542 cpu_addr = (void *)(cb->ashmemBase + sizeof(int)); 543 } 544 else { 545 cpu_addr = (void *)(cb->ashmemBase); 546 } 547 } 548 549 if (cb->hostHandle) { 550 // Make sure we have host connection 551 DEFINE_AND_VALIDATE_HOST_CONNECTION; 552 553 // 554 // flush color buffer write cache on host and get its sync status. 555 // 556 int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle, 557 postCount, 558 sw_read); 559 if (hostSyncStatus < 0) { 560 // host failed the color buffer sync - probably since it was already 561 // locked for write access. fail the lock. 562 LOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n", 563 postCount, sw_read); 564 return -EBUSY; 565 } 566 567 // 568 // is virtual address required ? 569 // 570 if (sw_read || sw_write) { 571 *vaddr = cpu_addr; 572 } 573 } 574 575 if (sw_write) { 576 // 577 // Keep locked region if locked for s/w write access. 578 // 579 cb->lockedLeft = l; 580 cb->lockedTop = t; 581 cb->lockedWidth = w; 582 cb->lockedHeight = h; 583 } 584 585 return 0; 586 } 587 588 static int gralloc_unlock(gralloc_module_t const* module, 589 buffer_handle_t handle) 590 { 591 if (sFallback != NULL) { 592 return sFallback->unlock(sFallback, handle); 593 } 594 595 private_module_t *gr = (private_module_t *)module; 596 cb_handle_t *cb = (cb_handle_t *)handle; 597 if (!gr || !cb_handle_t::validate(cb)) { 598 return -EINVAL; 599 } 600 601 // 602 // if buffer was locked for s/w write, we need to update the host with 603 // the updated data 604 // 605 if (cb->lockedWidth > 0 && cb->lockedHeight > 0 && cb->hostHandle) { 606 607 // Make sure we have host connection 608 DEFINE_AND_VALIDATE_HOST_CONNECTION; 609 610 void *cpu_addr; 611 if (cb->canBePosted()) { 612 cpu_addr = (void *)(cb->ashmemBase + sizeof(int)); 613 } 614 else { 615 cpu_addr = (void *)(cb->ashmemBase); 616 } 617 618 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) { 619 int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3; 620 char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp]; 621 622 int dst_line_len = cb->lockedWidth * bpp; 623 int src_line_len = cb->width * bpp; 624 char *src = (char *)cpu_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp; 625 char *dst = tmpBuf; 626 for (int y=0; y<cb->lockedHeight; y++) { 627 memcpy(dst, src, dst_line_len); 628 src += src_line_len; 629 dst += dst_line_len; 630 } 631 632 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 633 cb->lockedLeft, cb->lockedTop, 634 cb->lockedWidth, cb->lockedHeight, 635 cb->glFormat, cb->glType, 636 tmpBuf); 637 638 delete [] tmpBuf; 639 } 640 else { 641 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0, 642 cb->width, cb->height, 643 cb->glFormat, cb->glType, 644 cpu_addr); 645 } 646 } 647 648 cb->lockedWidth = cb->lockedHeight = 0; 649 return 0; 650 } 651 652 653 static int gralloc_device_open(const hw_module_t* module, 654 const char* name, 655 hw_device_t** device) 656 { 657 int status = -EINVAL; 658 659 D("gralloc_device_open %s\n", name); 660 661 pthread_once( &sFallbackOnce, fallback_init ); 662 if (sFallback != NULL) { 663 return sFallback->common.methods->open(&sFallback->common, name, device); 664 } 665 666 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { 667 668 // Create host connection and keep it in the TLS. 669 // return error if connection with host can not be established 670 HostConnection *hostCon = HostConnection::get(); 671 if (!hostCon) { 672 LOGE("gralloc: failed to get host connection while opening %s\n", name); 673 return -EIO; 674 } 675 676 // 677 // Allocate memory for the gralloc device (alloc interface) 678 // 679 gralloc_device_t *dev; 680 dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t)); 681 if (NULL == dev) { 682 return -ENOMEM; 683 } 684 685 // Initialize our device structure 686 // 687 dev->device.common.tag = HARDWARE_DEVICE_TAG; 688 dev->device.common.version = 0; 689 dev->device.common.module = const_cast<hw_module_t*>(module); 690 dev->device.common.close = gralloc_device_close; 691 692 dev->device.alloc = gralloc_alloc; 693 dev->device.free = gralloc_free; 694 dev->allocListHead = NULL; 695 pthread_mutex_init(&dev->lock, NULL); 696 697 *device = &dev->device.common; 698 status = 0; 699 } 700 else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 701 702 // return error if connection with host can not be established 703 DEFINE_AND_VALIDATE_HOST_CONNECTION; 704 705 // 706 // Query the host for Framebuffer attributes 707 // 708 D("gralloc: query Frabuffer attribs\n"); 709 EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH); 710 D("gralloc: width=%d\n", width); 711 EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT); 712 D("gralloc: height=%d\n", height); 713 EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI); 714 D("gralloc: xdpi=%d\n", xdpi); 715 EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI); 716 D("gralloc: ydpi=%d\n", ydpi); 717 EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS); 718 D("gralloc: fps=%d\n", fps); 719 EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL); 720 D("gralloc: min_swap=%d\n", min_si); 721 EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL); 722 D("gralloc: max_swap=%d\n", max_si); 723 724 // 725 // Allocate memory for the framebuffer device 726 // 727 fb_device_t *dev; 728 dev = (fb_device_t*)malloc(sizeof(fb_device_t)); 729 if (NULL == dev) { 730 return -ENOMEM; 731 } 732 memset(dev, 0, sizeof(fb_device_t)); 733 734 // Initialize our device structure 735 // 736 dev->device.common.tag = HARDWARE_DEVICE_TAG; 737 dev->device.common.version = 0; 738 dev->device.common.module = const_cast<hw_module_t*>(module); 739 dev->device.common.close = fb_close; 740 dev->device.setSwapInterval = fb_setSwapInterval; 741 dev->device.post = fb_post; 742 dev->device.setUpdateRect = 0; //fb_setUpdateRect; 743 dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy 744 745 const_cast<uint32_t&>(dev->device.flags) = 0; 746 const_cast<uint32_t&>(dev->device.width) = width; 747 const_cast<uint32_t&>(dev->device.height) = height; 748 const_cast<int&>(dev->device.stride) = width; 749 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888; 750 const_cast<float&>(dev->device.xdpi) = xdpi; 751 const_cast<float&>(dev->device.ydpi) = ydpi; 752 const_cast<float&>(dev->device.fps) = fps; 753 const_cast<int&>(dev->device.minSwapInterval) = min_si; 754 const_cast<int&>(dev->device.maxSwapInterval) = max_si; 755 *device = &dev->device.common; 756 757 status = 0; 758 } 759 760 return status; 761 } 762 763 // 764 // define the HMI symbol - our module interface 765 // 766 static struct hw_module_methods_t gralloc_module_methods = { 767 open: gralloc_device_open 768 }; 769 770 struct private_module_t HAL_MODULE_INFO_SYM = { 771 base: { 772 common: { 773 tag: HARDWARE_MODULE_TAG, 774 version_major: 1, 775 version_minor: 0, 776 id: GRALLOC_HARDWARE_MODULE_ID, 777 name: "Graphics Memory Allocator Module", 778 author: "The Android Open Source Project", 779 methods: &gralloc_module_methods, 780 dso: NULL, 781 reserved: {0, } 782 }, 783 registerBuffer: gralloc_register_buffer, 784 unregisterBuffer: gralloc_unregister_buffer, 785 lock: gralloc_lock, 786 unlock: gralloc_unlock, 787 perform: NULL, 788 reserved_proc : {NULL, } 789 } 790 }; 791 792 /* This function is called once to detect whether the emulator supports 793 * GPU emulation (this is done by looking at the qemu.gles kernel 794 * parameter, which must be > 0 if this is the case). 795 * 796 * If not, then load gralloc.default instead as a fallback. 797 */ 798 static void 799 fallback_init(void) 800 { 801 char prop[PROPERTY_VALUE_MAX]; 802 void* module; 803 804 property_get("ro.kernel.qemu.gles", prop, "0"); 805 if (atoi(prop) > 0) { 806 return; 807 } 808 LOGD("Emulator without GPU emulation detected."); 809 module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL); 810 if (module != NULL) { 811 sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR)); 812 if (sFallback == NULL) { 813 dlclose(module); 814 } 815 } 816 if (sFallback == NULL) { 817 LOGE("Could not find software fallback module!?"); 818 } 819 } 820