gralloc.cpp revision 4b03d2c6d0e5c96c27fc5bcc673784113414710c
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#include <limits.h> 19#include <cutils/ashmem.h> 20#include <unistd.h> 21#include <errno.h> 22#include <dlfcn.h> 23#include <sys/mman.h> 24#include "gralloc_cb.h" 25#include "HostConnection.h" 26#include "glUtils.h" 27#include <cutils/log.h> 28#include <cutils/properties.h> 29 30/* Set to 1 or 2 to enable debug traces */ 31#define DEBUG 0 32 33#if DEBUG >= 1 34# define D(...) ALOGD(__VA_ARGS__) 35#else 36# define D(...) ((void)0) 37#endif 38 39#if DEBUG >= 2 40# define DD(...) ALOGD(__VA_ARGS__) 41#else 42# define DD(...) ((void)0) 43#endif 44 45#define DBG_FUNC DBG("%s\n", __FUNCTION__) 46// 47// our private gralloc module structure 48// 49struct private_module_t { 50 gralloc_module_t base; 51}; 52 53/* If not NULL, this is a pointer to the fallback module. 54 * This really is gralloc.default, which we'll use if we detect 55 * that the emulator we're running in does not support GPU emulation. 56 */ 57static gralloc_module_t* sFallback; 58static pthread_once_t sFallbackOnce = PTHREAD_ONCE_INIT; 59 60static void fallback_init(void); // forward 61 62 63typedef struct _alloc_list_node { 64 buffer_handle_t handle; 65 _alloc_list_node *next; 66 _alloc_list_node *prev; 67} AllocListNode; 68 69// 70// Our gralloc device structure (alloc interface) 71// 72struct gralloc_device_t { 73 alloc_device_t device; 74 75 AllocListNode *allocListHead; // double linked list of allocated buffers 76 pthread_mutex_t lock; 77}; 78 79// 80// Our framebuffer device structure 81// 82struct fb_device_t { 83 framebuffer_device_t device; 84}; 85 86static int map_buffer(cb_handle_t *cb, void **vaddr) 87{ 88 if (cb->fd < 0 || cb->ashmemSize <= 0) { 89 return -EINVAL; 90 } 91 92 void *addr = mmap(0, cb->ashmemSize, PROT_READ | PROT_WRITE, 93 MAP_SHARED, cb->fd, 0); 94 if (addr == MAP_FAILED) { 95 return -errno; 96 } 97 98 cb->ashmemBase = intptr_t(addr); 99 cb->ashmemBasePid = getpid(); 100 101 *vaddr = addr; 102 return 0; 103} 104 105#define DEFINE_HOST_CONNECTION \ 106 HostConnection *hostCon = HostConnection::get(); \ 107 renderControl_encoder_context_t *rcEnc = (hostCon ? hostCon->rcEncoder() : NULL) 108 109#define DEFINE_AND_VALIDATE_HOST_CONNECTION \ 110 HostConnection *hostCon = HostConnection::get(); \ 111 if (!hostCon) { \ 112 ALOGE("gralloc: Failed to get host connection\n"); \ 113 return -EIO; \ 114 } \ 115 renderControl_encoder_context_t *rcEnc = hostCon->rcEncoder(); \ 116 if (!rcEnc) { \ 117 ALOGE("gralloc: Failed to get renderControl encoder context\n"); \ 118 return -EIO; \ 119 } 120 121 122// 123// gralloc device functions (alloc interface) 124// 125static int gralloc_alloc(alloc_device_t* dev, 126 int w, int h, int format, int usage, 127 buffer_handle_t* pHandle, int* pStride) 128{ 129 D("gralloc_alloc w=%d h=%d usage=0x%x\n", w, h, usage); 130 131 gralloc_device_t *grdev = (gralloc_device_t *)dev; 132 if (!grdev || !pHandle || !pStride) { 133 ALOGE("gralloc_alloc: Bad inputs (grdev: %p, pHandle: %p, pStride: %p", 134 grdev, pHandle, pStride); 135 return -EINVAL; 136 } 137 138 // 139 // Note: in screen capture mode, both sw_write and hw_write will be on 140 // and this is a valid usage 141 // 142 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); 143 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); 144 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); 145 bool hw_cam_write = usage & GRALLOC_USAGE_HW_CAMERA_WRITE; 146 bool hw_cam_read = usage & GRALLOC_USAGE_HW_CAMERA_READ; 147 bool hw_vid_enc_read = usage & GRALLOC_USAGE_HW_VIDEO_ENCODER; 148 149 // Keep around original requested format for later validation 150 int frameworkFormat = format; 151 // Pick the right concrete pixel format given the endpoints as encoded in 152 // the usage bits. Every end-point pair needs explicit listing here. 153 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 154 // Camera as producer 155 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { 156 if (usage & GRALLOC_USAGE_HW_TEXTURE) { 157 // Camera-to-display is RGBA 158 format = HAL_PIXEL_FORMAT_RGBA_8888; 159 } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { 160 // Camera-to-encoder is NV21 161 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; 162 } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) == 163 GRALLOC_USAGE_HW_CAMERA_ZSL) { 164 // Camera-to-ZSL-queue is RGB_888 165 format = HAL_PIXEL_FORMAT_RGB_888; 166 } 167 } 168 169 if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { 170 ALOGE("gralloc_alloc: Requested auto format selection, " 171 "but no known format for this usage: %d x %d, usage %x", 172 w, h, usage); 173 return -EINVAL; 174 } 175 } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { 176 // Flexible framework-accessible YUV format; map to NV21 for now 177 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { 178 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; 179 } 180 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { 181 ALOGE("gralloc_alloc: Requested YCbCr_420_888, but no known " 182 "specific format for this usage: %d x %d, usage %x", 183 w, h, usage); 184 } 185 } 186 bool yuv_format = false; 187 188 int ashmem_size = 0; 189 int stride = w; 190 191 GLenum glFormat = 0; 192 GLenum glType = 0; 193 194 int bpp = 0; 195 int align = 1; 196 switch (format) { 197 case HAL_PIXEL_FORMAT_RGBA_8888: 198 case HAL_PIXEL_FORMAT_RGBX_8888: 199 case HAL_PIXEL_FORMAT_BGRA_8888: 200 bpp = 4; 201 glFormat = GL_RGBA; 202 glType = GL_UNSIGNED_BYTE; 203 break; 204 case HAL_PIXEL_FORMAT_RGB_888: 205 bpp = 3; 206 glFormat = GL_RGB; 207 glType = GL_UNSIGNED_BYTE; 208 break; 209 case HAL_PIXEL_FORMAT_RGB_565: 210 bpp = 2; 211 glFormat = GL_RGB; 212 glType = GL_UNSIGNED_SHORT_5_6_5; 213 break; 214 case HAL_PIXEL_FORMAT_RAW16: 215 case HAL_PIXEL_FORMAT_Y16: 216 bpp = 2; 217 align = 16*bpp; 218 if (! ((sw_read || hw_cam_read) && (sw_write || hw_cam_write) ) ) { 219 // Raw sensor data or Y16 only goes between camera and CPU 220 return -EINVAL; 221 } 222 // Not expecting to actually create any GL surfaces for this 223 glFormat = GL_LUMINANCE; 224 glType = GL_UNSIGNED_SHORT; 225 break; 226 case HAL_PIXEL_FORMAT_BLOB: 227 bpp = 1; 228 if (! (sw_read && hw_cam_write) ) { 229 // Blob data cannot be used by HW other than camera emulator 230 return -EINVAL; 231 } 232 // Not expecting to actually create any GL surfaces for this 233 glFormat = GL_LUMINANCE; 234 glType = GL_UNSIGNED_BYTE; 235 break; 236 case HAL_PIXEL_FORMAT_YCrCb_420_SP: 237 align = 1; 238 bpp = 1; // per-channel bpp 239 yuv_format = true; 240 // Not expecting to actually create any GL surfaces for this 241 break; 242 case HAL_PIXEL_FORMAT_YV12: 243 align = 16; 244 bpp = 1; // per-channel bpp 245 yuv_format = true; 246 // Not expecting to actually create any GL surfaces for this 247 break; 248 default: 249 ALOGE("gralloc_alloc: Unknown format %d", format); 250 return -EINVAL; 251 } 252 253 if (usage & GRALLOC_USAGE_HW_FB) { 254 // keep space for postCounter 255 ashmem_size += sizeof(uint32_t); 256 } 257 258 if (sw_read || sw_write || hw_cam_write || hw_vid_enc_read) { 259 // keep space for image on guest memory if SW access is needed 260 // or if the camera is doing writing 261 if (yuv_format) { 262 size_t yStride = (w*bpp + (align - 1)) & ~(align-1); 263 size_t uvStride = (yStride / 2 + (align - 1)) & ~(align-1); 264 size_t uvHeight = h / 2; 265 ashmem_size += yStride * h + 2 * (uvHeight * uvStride); 266 stride = yStride / bpp; 267 } else { 268 size_t bpr = (w*bpp + (align-1)) & ~(align-1); 269 ashmem_size += (bpr * h); 270 stride = bpr / bpp; 271 } 272 } 273 274 D("gralloc_alloc format=%d, ashmem_size=%d, stride=%d, tid %d\n", format, 275 ashmem_size, stride, gettid()); 276 277 // 278 // Allocate space in ashmem if needed 279 // 280 int fd = -1; 281 if (ashmem_size > 0) { 282 // round to page size; 283 ashmem_size = (ashmem_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); 284 285 fd = ashmem_create_region("gralloc-buffer", ashmem_size); 286 if (fd < 0) { 287 ALOGE("gralloc_alloc failed to create ashmem region: %s\n", 288 strerror(errno)); 289 return -errno; 290 } 291 } 292 293 cb_handle_t *cb = new cb_handle_t(fd, ashmem_size, usage, 294 w, h, frameworkFormat, format, 295 glFormat, glType); 296 297 if (ashmem_size > 0) { 298 // 299 // map ashmem region if exist 300 // 301 void *vaddr; 302 int err = map_buffer(cb, &vaddr); 303 if (err) { 304 close(fd); 305 delete cb; 306 return err; 307 } 308 309 cb->setFd(fd); 310 } 311 312 // 313 // Allocate ColorBuffer handle on the host (only if h/w access is allowed) 314 // Only do this for some h/w usages, not all. 315 // 316 if (usage & (GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER | 317 GRALLOC_USAGE_HW_2D | GRALLOC_USAGE_HW_COMPOSER | 318 GRALLOC_USAGE_HW_FB) ) { 319 DEFINE_HOST_CONNECTION; 320 if (hostCon && rcEnc) { 321 cb->hostHandle = rcEnc->rcCreateColorBuffer(rcEnc, w, h, glFormat); 322 D("Created host ColorBuffer 0x%x\n", cb->hostHandle); 323 } 324 325 if (!cb->hostHandle) { 326 // Could not create colorbuffer on host !!! 327 close(fd); 328 delete cb; 329 return -EIO; 330 } 331 } 332 333 // 334 // alloc succeeded - insert the allocated handle to the allocated list 335 // 336 AllocListNode *node = new AllocListNode(); 337 pthread_mutex_lock(&grdev->lock); 338 node->handle = cb; 339 node->next = grdev->allocListHead; 340 node->prev = NULL; 341 if (grdev->allocListHead) { 342 grdev->allocListHead->prev = node; 343 } 344 grdev->allocListHead = node; 345 pthread_mutex_unlock(&grdev->lock); 346 347 *pHandle = cb; 348 if (frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) { 349 *pStride = 0; 350 } else { 351 *pStride = stride; 352 } 353 return 0; 354} 355 356static int gralloc_free(alloc_device_t* dev, 357 buffer_handle_t handle) 358{ 359 const cb_handle_t *cb = (const cb_handle_t *)handle; 360 if (!cb_handle_t::validate((cb_handle_t*)cb)) { 361 ERR("gralloc_free: invalid handle"); 362 return -EINVAL; 363 } 364 365 if (cb->hostHandle != 0) { 366 DEFINE_AND_VALIDATE_HOST_CONNECTION; 367 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle); 368 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle); 369 } 370 371 // 372 // detach and unmap ashmem area if present 373 // 374 if (cb->fd > 0) { 375 if (cb->ashmemSize > 0 && cb->ashmemBase) { 376 munmap((void *)cb->ashmemBase, cb->ashmemSize); 377 } 378 close(cb->fd); 379 } 380 381 // remove it from the allocated list 382 gralloc_device_t *grdev = (gralloc_device_t *)dev; 383 pthread_mutex_lock(&grdev->lock); 384 AllocListNode *n = grdev->allocListHead; 385 while( n && n->handle != cb ) { 386 n = n->next; 387 } 388 if (n) { 389 // buffer found on list - remove it from list 390 if (n->next) { 391 n->next->prev = n->prev; 392 } 393 if (n->prev) { 394 n->prev->next = n->next; 395 } 396 else { 397 grdev->allocListHead = n->next; 398 } 399 400 delete n; 401 } 402 pthread_mutex_unlock(&grdev->lock); 403 404 delete cb; 405 406 return 0; 407} 408 409static int gralloc_device_close(struct hw_device_t *dev) 410{ 411 gralloc_device_t* d = reinterpret_cast<gralloc_device_t*>(dev); 412 if (d) { 413 414 // free still allocated buffers 415 while( d->allocListHead != NULL ) { 416 gralloc_free(&d->device, d->allocListHead->handle); 417 } 418 419 // free device 420 free(d); 421 } 422 return 0; 423} 424 425static int fb_compositionComplete(struct framebuffer_device_t* dev) 426{ 427 (void)dev; 428 429 return 0; 430} 431 432// 433// Framebuffer device functions 434// 435static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) 436{ 437 fb_device_t *fbdev = (fb_device_t *)dev; 438 cb_handle_t *cb = (cb_handle_t *)buffer; 439 440 if (!fbdev || !cb_handle_t::validate(cb) || !cb->canBePosted()) { 441 return -EINVAL; 442 } 443 444 // Make sure we have host connection 445 DEFINE_AND_VALIDATE_HOST_CONNECTION; 446 447 // increment the post count of the buffer 448 intptr_t *postCountPtr = (intptr_t *)cb->ashmemBase; 449 if (!postCountPtr) { 450 // This should not happen 451 return -EINVAL; 452 } 453 (*postCountPtr)++; 454 455 // send post request to host 456 rcEnc->rcFBPost(rcEnc, cb->hostHandle); 457 hostCon->flush(); 458 459 return 0; 460} 461 462static int fb_setUpdateRect(struct framebuffer_device_t* dev, 463 int l, int t, int w, int h) 464{ 465 fb_device_t *fbdev = (fb_device_t *)dev; 466 467 (void)l; 468 (void)t; 469 (void)w; 470 (void)h; 471 472 if (!fbdev) { 473 return -EINVAL; 474 } 475 476 // Make sure we have host connection 477 DEFINE_AND_VALIDATE_HOST_CONNECTION; 478 479 // send request to host 480 // TODO: XXX - should be implemented 481 //rcEnc->rc_XXX 482 483 return 0; 484} 485 486static int fb_setSwapInterval(struct framebuffer_device_t* dev, 487 int interval) 488{ 489 fb_device_t *fbdev = (fb_device_t *)dev; 490 491 if (!fbdev) { 492 return -EINVAL; 493 } 494 495 // Make sure we have host connection 496 DEFINE_AND_VALIDATE_HOST_CONNECTION; 497 498 // send request to host 499 rcEnc->rcFBSetSwapInterval(rcEnc, interval); 500 hostCon->flush(); 501 502 return 0; 503} 504 505static int fb_close(struct hw_device_t *dev) 506{ 507 fb_device_t *fbdev = (fb_device_t *)dev; 508 509 delete fbdev; 510 511 return 0; 512} 513 514 515// 516// gralloc module functions - refcount + locking interface 517// 518static int gralloc_register_buffer(gralloc_module_t const* module, 519 buffer_handle_t handle) 520{ 521 pthread_once(&sFallbackOnce, fallback_init); 522 if (sFallback != NULL) { 523 return sFallback->registerBuffer(sFallback, handle); 524 } 525 526 D("gralloc_register_buffer(%p) called", handle); 527 528 private_module_t *gr = (private_module_t *)module; 529 cb_handle_t *cb = (cb_handle_t *)handle; 530 if (!gr || !cb_handle_t::validate(cb)) { 531 ERR("gralloc_register_buffer(%p): invalid buffer", cb); 532 return -EINVAL; 533 } 534 535 if (cb->hostHandle != 0) { 536 DEFINE_AND_VALIDATE_HOST_CONNECTION; 537 D("Opening host ColorBuffer 0x%x\n", cb->hostHandle); 538 rcEnc->rcOpenColorBuffer2(rcEnc, cb->hostHandle); 539 } 540 541 // 542 // if the color buffer has ashmem region and it is not mapped in this 543 // process map it now. 544 // 545 if (cb->ashmemSize > 0 && cb->mappedPid != getpid()) { 546 void *vaddr; 547 int err = map_buffer(cb, &vaddr); 548 if (err) { 549 ERR("gralloc_register_buffer(%p): map failed: %s", cb, strerror(-err)); 550 return -err; 551 } 552 cb->mappedPid = getpid(); 553 } 554 555 return 0; 556} 557 558static int gralloc_unregister_buffer(gralloc_module_t const* module, 559 buffer_handle_t handle) 560{ 561 if (sFallback != NULL) { 562 return sFallback->unregisterBuffer(sFallback, handle); 563 } 564 565 private_module_t *gr = (private_module_t *)module; 566 cb_handle_t *cb = (cb_handle_t *)handle; 567 if (!gr || !cb_handle_t::validate(cb)) { 568 ERR("gralloc_unregister_buffer(%p): invalid buffer", cb); 569 return -EINVAL; 570 } 571 572 if (cb->hostHandle != 0) { 573 DEFINE_AND_VALIDATE_HOST_CONNECTION; 574 D("Closing host ColorBuffer 0x%x\n", cb->hostHandle); 575 rcEnc->rcCloseColorBuffer(rcEnc, cb->hostHandle); 576 } 577 578 // 579 // unmap ashmem region if it was previously mapped in this process 580 // (through register_buffer) 581 // 582 if (cb->ashmemSize > 0 && cb->mappedPid == getpid()) { 583 void *vaddr; 584 int err = munmap((void *)cb->ashmemBase, cb->ashmemSize); 585 if (err) { 586 ERR("gralloc_unregister_buffer(%p): unmap failed", cb); 587 return -EINVAL; 588 } 589 cb->ashmemBase = 0; 590 cb->mappedPid = 0; 591 } 592 593 D("gralloc_unregister_buffer(%p) done\n", cb); 594 595 return 0; 596} 597 598static int gralloc_lock(gralloc_module_t const* module, 599 buffer_handle_t handle, int usage, 600 int l, int t, int w, int h, 601 void** vaddr) 602{ 603 if (sFallback != NULL) { 604 return sFallback->lock(sFallback, handle, usage, l, t, w, h, vaddr); 605 } 606 607 private_module_t *gr = (private_module_t *)module; 608 cb_handle_t *cb = (cb_handle_t *)handle; 609 if (!gr || !cb_handle_t::validate(cb)) { 610 ALOGE("gralloc_lock bad handle\n"); 611 return -EINVAL; 612 } 613 614 // validate format 615 if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) { 616 ALOGE("gralloc_lock can't be used with YCbCr_420_888 format"); 617 return -EINVAL; 618 } 619 620 // Validate usage, 621 // 1. cannot be locked for hw access 622 // 2. lock for either sw read or write. 623 // 3. locked sw access must match usage during alloc time. 624 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); 625 bool sw_write = (0 != (usage & GRALLOC_USAGE_SW_WRITE_MASK)); 626 bool hw_read = (usage & GRALLOC_USAGE_HW_TEXTURE); 627 bool hw_write = (usage & GRALLOC_USAGE_HW_RENDER); 628 bool hw_vid_enc_read = (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER); 629 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE); 630 bool hw_cam_read = (usage & GRALLOC_USAGE_HW_CAMERA_READ); 631 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK)); 632 bool sw_write_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_WRITE_MASK)); 633 634 if ( (hw_read || hw_write) || 635 (!sw_read && !sw_write && 636 !hw_cam_write && !hw_cam_read && 637 !hw_vid_enc_read) || 638 (sw_read && !sw_read_allowed) || 639 (sw_write && !sw_write_allowed) ) { 640 ALOGE("gralloc_lock usage mismatch usage=0x%x cb->usage=0x%x\n", usage, 641 cb->usage); 642 return -EINVAL; 643 } 644 645 intptr_t postCount = 0; 646 void *cpu_addr = NULL; 647 648 // 649 // make sure ashmem area is mapped if needed 650 // 651 if (cb->canBePosted() || sw_read || sw_write || 652 hw_cam_write || hw_cam_read || 653 hw_vid_enc_read) { 654 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) { 655 return -EACCES; 656 } 657 658 if (cb->canBePosted()) { 659 postCount = *((intptr_t *)cb->ashmemBase); 660 cpu_addr = (void *)(cb->ashmemBase + sizeof(intptr_t)); 661 } 662 else { 663 cpu_addr = (void *)(cb->ashmemBase); 664 } 665 } 666 667 if (cb->hostHandle) { 668 // Make sure we have host connection 669 DEFINE_AND_VALIDATE_HOST_CONNECTION; 670 671 // 672 // flush color buffer write cache on host and get its sync status. 673 // 674 int hostSyncStatus = rcEnc->rcColorBufferCacheFlush(rcEnc, cb->hostHandle, 675 postCount, 676 sw_read); 677 if (hostSyncStatus < 0) { 678 // host failed the color buffer sync - probably since it was already 679 // locked for write access. fail the lock. 680 ALOGE("gralloc_lock cacheFlush failed postCount=%d sw_read=%d\n", 681 postCount, sw_read); 682 return -EBUSY; 683 } 684 685 const bool sw_read = (cb->usage & GRALLOC_USAGE_SW_READ_MASK); 686 const bool hw_write = (cb->usage & GRALLOC_USAGE_HW_RENDER); 687 const bool screen_capture_mode = (sw_read && hw_write); 688 if (screen_capture_mode) { 689 D("gralloc_lock read back color buffer %d %d\n", cb->width, cb->height); 690 DEFINE_AND_VALIDATE_HOST_CONNECTION; 691 rcEnc->rcReadColorBuffer(rcEnc, cb->hostHandle, 692 0, 0, cb->width, cb->height, GL_RGBA, GL_UNSIGNED_BYTE, cpu_addr); 693 } 694 } 695 696 // 697 // is virtual address required ? 698 // 699 if (sw_read || sw_write || hw_cam_write || hw_cam_read || hw_vid_enc_read) { 700 *vaddr = cpu_addr; 701 } 702 703 if (sw_write || hw_cam_write) { 704 // 705 // Keep locked region if locked for s/w write access. 706 // 707 cb->lockedLeft = l; 708 cb->lockedTop = t; 709 cb->lockedWidth = w; 710 cb->lockedHeight = h; 711 } 712 713 DD("gralloc_lock success. vaddr: %p, *vaddr: %p, usage: %x, cpu_addr: %p", 714 vaddr, vaddr ? *vaddr : 0, usage, cpu_addr); 715 716 return 0; 717} 718 719static int gralloc_unlock(gralloc_module_t const* module, 720 buffer_handle_t handle) 721{ 722 if (sFallback != NULL) { 723 return sFallback->unlock(sFallback, handle); 724 } 725 726 private_module_t *gr = (private_module_t *)module; 727 cb_handle_t *cb = (cb_handle_t *)handle; 728 if (!gr || !cb_handle_t::validate(cb)) { 729 return -EINVAL; 730 } 731 732 // 733 // if buffer was locked for s/w write, we need to update the host with 734 // the updated data 735 // 736 if (cb->lockedWidth > 0 && cb->lockedHeight > 0 && cb->hostHandle) { 737 738 // Make sure we have host connection 739 DEFINE_AND_VALIDATE_HOST_CONNECTION; 740 741 void *cpu_addr; 742 if (cb->canBePosted()) { 743 cpu_addr = (void *)(cb->ashmemBase + sizeof(int)); 744 } 745 else { 746 cpu_addr = (void *)(cb->ashmemBase); 747 } 748 749 if (cb->lockedWidth < cb->width || cb->lockedHeight < cb->height) { 750 int bpp = glUtilsPixelBitSize(cb->glFormat, cb->glType) >> 3; 751 char *tmpBuf = new char[cb->lockedWidth * cb->lockedHeight * bpp]; 752 753 int dst_line_len = cb->lockedWidth * bpp; 754 int src_line_len = cb->width * bpp; 755 char *src = (char *)cpu_addr + cb->lockedTop*src_line_len + cb->lockedLeft*bpp; 756 char *dst = tmpBuf; 757 for (int y=0; y<cb->lockedHeight; y++) { 758 memcpy(dst, src, dst_line_len); 759 src += src_line_len; 760 dst += dst_line_len; 761 } 762 763 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 764 cb->lockedLeft, cb->lockedTop, 765 cb->lockedWidth, cb->lockedHeight, 766 cb->glFormat, cb->glType, 767 tmpBuf); 768 769 delete [] tmpBuf; 770 } 771 else { 772 rcEnc->rcUpdateColorBuffer(rcEnc, cb->hostHandle, 0, 0, 773 cb->width, cb->height, 774 cb->glFormat, cb->glType, 775 cpu_addr); 776 } 777 } 778 779 cb->lockedWidth = cb->lockedHeight = 0; 780 return 0; 781} 782 783static int gralloc_lock_ycbcr(gralloc_module_t const* module, 784 buffer_handle_t handle, int usage, 785 int l, int t, int w, int h, 786 android_ycbcr *ycbcr) 787{ 788 // Not supporting fallback module for YCbCr 789 if (sFallback != NULL) { 790 return -EINVAL; 791 } 792 793 if (!ycbcr) { 794 ALOGE("gralloc_lock_ycbcr got NULL ycbcr struct"); 795 return -EINVAL; 796 } 797 798 private_module_t *gr = (private_module_t *)module; 799 cb_handle_t *cb = (cb_handle_t *)handle; 800 if (!gr || !cb_handle_t::validate(cb)) { 801 ALOGE("gralloc_lock_ycbcr bad handle\n"); 802 return -EINVAL; 803 } 804 805 if (cb->frameworkFormat != HAL_PIXEL_FORMAT_YCbCr_420_888) { 806 ALOGE("gralloc_lock_ycbcr can only be used with " 807 "HAL_PIXEL_FORMAT_YCbCr_420_888, got %x instead", 808 cb->frameworkFormat); 809 return -EINVAL; 810 } 811 812 // Validate usage 813 // For now, only allow camera write, software read. 814 bool sw_read = (0 != (usage & GRALLOC_USAGE_SW_READ_MASK)); 815 bool hw_cam_write = (usage & GRALLOC_USAGE_HW_CAMERA_WRITE); 816 bool sw_read_allowed = (0 != (cb->usage & GRALLOC_USAGE_SW_READ_MASK)); 817 818 if ( (!hw_cam_write && !sw_read) || 819 (sw_read && !sw_read_allowed) ) { 820 ALOGE("gralloc_lock_ycbcr usage mismatch usage:0x%x cb->usage:0x%x\n", 821 usage, cb->usage); 822 return -EINVAL; 823 } 824 825 // Make sure memory is mapped, get address 826 if (cb->ashmemBasePid != getpid() || !cb->ashmemBase) { 827 return -EACCES; 828 } 829 830 uint8_t *cpu_addr = NULL; 831 832 if (cb->canBePosted()) { 833 cpu_addr = (uint8_t *)(cb->ashmemBase + sizeof(int)); 834 } 835 else { 836 cpu_addr = (uint8_t *)(cb->ashmemBase); 837 } 838 839 // Calculate offsets to underlying YUV data 840 size_t yStride; 841 size_t cStride; 842 size_t yOffset; 843 size_t uOffset; 844 size_t vOffset; 845 size_t cStep; 846 switch (cb->format) { 847 case HAL_PIXEL_FORMAT_YCrCb_420_SP: 848 yStride = cb->width; 849 cStride = cb->width; 850 yOffset = 0; 851 vOffset = yStride * cb->height; 852 uOffset = vOffset + 1; 853 cStep = 2; 854 break; 855 default: 856 ALOGE("gralloc_lock_ycbcr unexpected internal format %x", 857 cb->format); 858 return -EINVAL; 859 } 860 861 ycbcr->y = cpu_addr + yOffset; 862 ycbcr->cb = cpu_addr + uOffset; 863 ycbcr->cr = cpu_addr + vOffset; 864 ycbcr->ystride = yStride; 865 ycbcr->cstride = cStride; 866 ycbcr->chroma_step = cStep; 867 868 // Zero out reserved fields 869 memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved)); 870 871 // 872 // Keep locked region if locked for s/w write access. 873 // 874 cb->lockedLeft = l; 875 cb->lockedTop = t; 876 cb->lockedWidth = w; 877 cb->lockedHeight = h; 878 879 DD("gralloc_lock_ycbcr success. usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, " 880 ".ystride: %d , .cstride: %d, .chroma_step: %d", usage, 881 ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride, 882 ycbcr->chroma_step); 883 884 return 0; 885} 886 887static int gralloc_device_open(const hw_module_t* module, 888 const char* name, 889 hw_device_t** device) 890{ 891 int status = -EINVAL; 892 893 D("gralloc_device_open %s\n", name); 894 895 pthread_once( &sFallbackOnce, fallback_init ); 896 if (sFallback != NULL) { 897 return sFallback->common.methods->open(&sFallback->common, name, device); 898 } 899 900 if (!strcmp(name, GRALLOC_HARDWARE_GPU0)) { 901 902 // Create host connection and keep it in the TLS. 903 // return error if connection with host can not be established 904 HostConnection *hostCon = HostConnection::get(); 905 if (!hostCon) { 906 ALOGE("gralloc: failed to get host connection while opening %s\n", name); 907 return -EIO; 908 } 909 910 // 911 // Allocate memory for the gralloc device (alloc interface) 912 // 913 gralloc_device_t *dev; 914 dev = (gralloc_device_t*)malloc(sizeof(gralloc_device_t)); 915 if (NULL == dev) { 916 return -ENOMEM; 917 } 918 919 // Initialize our device structure 920 // 921 dev->device.common.tag = HARDWARE_DEVICE_TAG; 922 dev->device.common.version = 0; 923 dev->device.common.module = const_cast<hw_module_t*>(module); 924 dev->device.common.close = gralloc_device_close; 925 926 dev->device.alloc = gralloc_alloc; 927 dev->device.free = gralloc_free; 928 dev->allocListHead = NULL; 929 pthread_mutex_init(&dev->lock, NULL); 930 931 *device = &dev->device.common; 932 status = 0; 933 } 934 else if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { 935 936 // return error if connection with host can not be established 937 DEFINE_AND_VALIDATE_HOST_CONNECTION; 938 939 // 940 // Query the host for Framebuffer attributes 941 // 942 D("gralloc: query Frabuffer attribs\n"); 943 EGLint width = rcEnc->rcGetFBParam(rcEnc, FB_WIDTH); 944 D("gralloc: width=%d\n", width); 945 EGLint height = rcEnc->rcGetFBParam(rcEnc, FB_HEIGHT); 946 D("gralloc: height=%d\n", height); 947 EGLint xdpi = rcEnc->rcGetFBParam(rcEnc, FB_XDPI); 948 D("gralloc: xdpi=%d\n", xdpi); 949 EGLint ydpi = rcEnc->rcGetFBParam(rcEnc, FB_YDPI); 950 D("gralloc: ydpi=%d\n", ydpi); 951 EGLint fps = rcEnc->rcGetFBParam(rcEnc, FB_FPS); 952 D("gralloc: fps=%d\n", fps); 953 EGLint min_si = rcEnc->rcGetFBParam(rcEnc, FB_MIN_SWAP_INTERVAL); 954 D("gralloc: min_swap=%d\n", min_si); 955 EGLint max_si = rcEnc->rcGetFBParam(rcEnc, FB_MAX_SWAP_INTERVAL); 956 D("gralloc: max_swap=%d\n", max_si); 957 958 // 959 // Allocate memory for the framebuffer device 960 // 961 fb_device_t *dev; 962 dev = (fb_device_t*)malloc(sizeof(fb_device_t)); 963 if (NULL == dev) { 964 return -ENOMEM; 965 } 966 memset(dev, 0, sizeof(fb_device_t)); 967 968 // Initialize our device structure 969 // 970 dev->device.common.tag = HARDWARE_DEVICE_TAG; 971 dev->device.common.version = 0; 972 dev->device.common.module = const_cast<hw_module_t*>(module); 973 dev->device.common.close = fb_close; 974 dev->device.setSwapInterval = fb_setSwapInterval; 975 dev->device.post = fb_post; 976 dev->device.setUpdateRect = 0; //fb_setUpdateRect; 977 dev->device.compositionComplete = fb_compositionComplete; //XXX: this is a dummy 978 979 const_cast<uint32_t&>(dev->device.flags) = 0; 980 const_cast<uint32_t&>(dev->device.width) = width; 981 const_cast<uint32_t&>(dev->device.height) = height; 982 const_cast<int&>(dev->device.stride) = width; 983 const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGBA_8888; 984 const_cast<float&>(dev->device.xdpi) = xdpi; 985 const_cast<float&>(dev->device.ydpi) = ydpi; 986 const_cast<float&>(dev->device.fps) = fps; 987 const_cast<int&>(dev->device.minSwapInterval) = min_si; 988 const_cast<int&>(dev->device.maxSwapInterval) = max_si; 989 *device = &dev->device.common; 990 991 status = 0; 992 } 993 994 return status; 995} 996 997// 998// define the HMI symbol - our module interface 999// 1000static struct hw_module_methods_t gralloc_module_methods = { 1001 open: gralloc_device_open 1002}; 1003 1004struct private_module_t HAL_MODULE_INFO_SYM = { 1005 base: { 1006 common: { 1007 tag: HARDWARE_MODULE_TAG, 1008 module_api_version: GRALLOC_MODULE_API_VERSION_0_2, 1009 hal_api_version: 0, 1010 id: GRALLOC_HARDWARE_MODULE_ID, 1011 name: "Graphics Memory Allocator Module", 1012 author: "The Android Open Source Project", 1013 methods: &gralloc_module_methods, 1014 dso: NULL, 1015 reserved: {0, } 1016 }, 1017 registerBuffer: gralloc_register_buffer, 1018 unregisterBuffer: gralloc_unregister_buffer, 1019 lock: gralloc_lock, 1020 unlock: gralloc_unlock, 1021 perform: NULL, 1022 lock_ycbcr: gralloc_lock_ycbcr, 1023 } 1024}; 1025 1026/* This function is called once to detect whether the emulator supports 1027 * GPU emulation (this is done by looking at the qemu.gles kernel 1028 * parameter, which must be > 0 if this is the case). 1029 * 1030 * If not, then load gralloc.default instead as a fallback. 1031 */ 1032static void 1033fallback_init(void) 1034{ 1035 char prop[PROPERTY_VALUE_MAX]; 1036 void* module; 1037 1038 property_get("ro.kernel.qemu.gles", prop, "0"); 1039 if (atoi(prop) > 0) { 1040 return; 1041 } 1042 ALOGD("Emulator without GPU emulation detected."); 1043#if __LP64__ 1044 module = dlopen("/system/lib64/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL); 1045#else 1046 module = dlopen("/system/lib/hw/gralloc.default.so", RTLD_LAZY|RTLD_LOCAL); 1047#endif 1048 if (module != NULL) { 1049 sFallback = reinterpret_cast<gralloc_module_t*>(dlsym(module, HAL_MODULE_INFO_SYM_AS_STR)); 1050 if (sFallback == NULL) { 1051 dlclose(module); 1052 } 1053 } 1054 if (sFallback == NULL) { 1055 ALOGE("Could not find software fallback module!?"); 1056 } 1057} 1058