/* * Copyright (C) 2010 ARM Limited. All rights reserved. * * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include "alloc_device.h" #include "gralloc_priv.h" #include "gralloc_helper.h" #include "framebuffer_device.h" #if PLATFORM_SDK_VERSION >= 24 #include "gralloc_usage_ext.h" #endif #include "alloc_device_allocator_specific.h" #if MALI_AFBC_GRALLOC == 1 #include "gralloc_buffer_priv.h" #endif #define AFBC_PIXELS_PER_BLOCK 16 #define AFBC_BODY_BUFFER_BYTE_ALIGNMENT 1024 #define AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY 16 #define AFBC_WIDEBLK_WIDTH_ALIGN 32 #define OMX_VIDEOLAYER_ALLOC_BUFFER_WIDTH 192 #define OMX_VIDEOLAYER_ALLOC_BUFFER_HEIGHT 90 static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle, int* stride, int* byte_stride) { private_module_t* private_t = reinterpret_cast(dev->common.module); framebuffer_mapper_t* m = NULL; #ifdef DEBUG_EXTERNAL_DISPLAY_ON_PANEL ALOGD("always alloc from fb0"); m = &(private_t->fb_primary); #else if (usage & GRALLOC_USAGE_EXTERNAL_DISP) { m = &(private_t->fb_external); } else { m = &(private_t->fb_primary); } #endif // allocate the framebuffer if (m->framebuffer == NULL) { #if 0//not a good idea to init here. remove it. // initialize the framebuffer, the framebuffer is mapped once and forever. int err = init_frame_buffer_locked(m); if (err < 0) { return err; } #endif AERR("Should register fb before alloc it. display %d ",usage & GRALLOC_USAGE_EXTERNAL_DISP); return -1; } const uint32_t bufferMask = m->bufferMask; const uint32_t numBuffers = m->numBuffers; /* framebufferSize is used for allocating the handle to the framebuffer and refers * to the size of the actual framebuffer. * alignedFramebufferSize is used for allocating a possible internal buffer and * thus need to consider internal alignment requirements. */ //const size_t framebufferSize = m->finfo.line_length * m->info.yres; const size_t framebufferSize = m->bufferSize; const size_t alignedFramebufferSize = GRALLOC_ALIGN(m->fb_info.finfo.line_length, 64) * m->fb_info.info.yres; *stride = m->fb_info.info.xres; if (numBuffers == 1) { // If we have only one buffer, we never use page-flipping. Instead, // we return a regular buffer which will be memcpy'ed to the main // screen when post is called. int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; //AWAR( "fallback to single buffering. Virtual Y-res too small %d", m->info.yres ); AWAR("fallback to single buffering. Virtual Y-res too small %d", numBuffers); *byte_stride = GRALLOC_ALIGN(m->fb_info.finfo.line_length, 64); return alloc_backend_alloc(dev, alignedFramebufferSize, newUsage, pHandle); } if (bufferMask >= ((1LU<framebuffer->base; // find a free slot for (uint32_t i=0 ; ibufferMask |= (1LU<framebuffer->fd, (framebufferVaddr - (uintptr_t)m->framebuffer->base), 0); /* * Perform allocator specific actions. If these fail we fall back to a regular buffer * which will be memcpy'ed to the main screen when fb_post is called. */ uint32_t index = (framebufferVaddr - (uintptr_t)m->framebuffer->base) / framebufferSize; if (alloc_backend_alloc_framebuffer(private_t, hnd, index) == -1) { delete hnd; int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; AERR( "Fallback to single buffering. Unable to map framebuffer memory to handle:%p", hnd ); *byte_stride = GRALLOC_ALIGN(m->fb_info.finfo.line_length, 64); return alloc_backend_alloc(dev, alignedFramebufferSize, newUsage, pHandle); } *pHandle = hnd; *byte_stride = m->fb_info.finfo.line_length; return 0; } static int gralloc_alloc_framebuffer(alloc_device_t* dev, size_t size, int usage, buffer_handle_t* pHandle, int* stride, int* byte_stride) { private_module_t* m = reinterpret_cast(dev->common.module); pthread_mutex_lock(&m->lock); int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle, stride, byte_stride); pthread_mutex_unlock(&m->lock); return err; } /* * Type of allocation */ enum AllocType { UNCOMPRESSED = 0, AFBC, /* AFBC_WIDEBLK mode requires buffer to have 32 * 16 pixels alignment */ AFBC_WIDEBLK, /* AN AFBC buffer with additional padding to ensure a 64-bte alignment * for each row of blocks in the header */ AFBC_PADDED }; /* * Computes the strides and size for an RGB buffer * * width width of the buffer in pixels * height height of the buffer in pixels * pixel_size size of one pixel in bytes * * pixel_stride (out) stride of the buffer in pixels * byte_stride (out) stride of the buffer in bytes * size (out) size of the buffer in bytes * type (in) if buffer should be allocated for afbc */ static void get_rgb_stride_and_size(int width, int height, int pixel_size, int* pixel_stride, int* byte_stride, size_t* size, AllocType type) { int stride; stride = width * pixel_size; /* Align the lines to 64 bytes. * It's more efficient to write to 64-byte aligned addresses because it's the burst size on the bus */ stride = GRALLOC_ALIGN(stride, 64); if (size != NULL) { *size = stride * height; } if (byte_stride != NULL) { *byte_stride = stride; } if (pixel_stride != NULL) { *pixel_stride = stride / pixel_size; } if (type != UNCOMPRESSED) { int w_aligned; int h_aligned = GRALLOC_ALIGN( height, AFBC_PIXELS_PER_BLOCK ); int nblocks; if (type == AFBC_PADDED) { w_aligned = GRALLOC_ALIGN( width, 64 ); } else if (type == AFBC_WIDEBLK) { w_aligned = GRALLOC_ALIGN( width, AFBC_WIDEBLK_WIDTH_ALIGN ); } else { w_aligned = GRALLOC_ALIGN( width, AFBC_PIXELS_PER_BLOCK ); } nblocks = w_aligned / AFBC_PIXELS_PER_BLOCK * h_aligned / AFBC_PIXELS_PER_BLOCK; if ( size != NULL ) { *size = w_aligned * h_aligned * pixel_size + GRALLOC_ALIGN( nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, AFBC_BODY_BUFFER_BYTE_ALIGNMENT ); } } } /* * Computes the strides and size for an AFBC 8BIT YUV 4:2:0 buffer * * width Public known width of the buffer in pixels * height Public known height of the buffer in pixels * * pixel_stride (out) stride of the buffer in pixels * byte_stride (out) stride of the buffer in bytes * size (out) size of the buffer in bytes * type if buffer should be allocated for a certain afbc type * internalHeight (out) Will store internal height if it is required to have a greater height than * known to public. If not it will be left untouched. */ static bool get_afbc_yuv420_8bit_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type, int *internalHeight) { int yuv420_afbc_luma_stride, yuv420_afbc_chroma_stride; if (type == UNCOMPRESSED) { AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_10BIT_AFBC!"); return false; } if (type == AFBC_PADDED) { AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV"); return false; } if (type == AFBC_WIDEBLK) { width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN); } else { width = GRALLOC_ALIGN(width, AFBC_PIXELS_PER_BLOCK); } #if AFBC_YUV420_EXTRA_MB_ROW_NEEDED /* If we have a greater internal height than public we set the internalHeight. This * implies that cropping will be applied of internal dimensions to fit the public one. */ *internalHeight += AFBC_PIXELS_PER_BLOCK; #endif /* The actual height used in size calculation must include the possible extra row. But * it must also be AFBC-aligned. Only the extra row-padding should be reported back in * internalHeight. This as only this row needs to be considered when cropping. */ height = GRALLOC_ALIGN( *internalHeight, AFBC_PIXELS_PER_BLOCK ); yuv420_afbc_luma_stride = width; yuv420_afbc_chroma_stride = GRALLOC_ALIGN(yuv420_afbc_luma_stride / 2, 16); /* Horizontal downsampling*/ if (size != NULL) { int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK; /* Simplification of (height * luma-stride + 2 * (height /2 * chroma_stride) */ *size = ( yuv420_afbc_luma_stride + yuv420_afbc_chroma_stride ) * height + GRALLOC_ALIGN( nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, AFBC_BODY_BUFFER_BYTE_ALIGNMENT ); } if (byte_stride != NULL) { *byte_stride = yuv420_afbc_luma_stride; } if (pixel_stride != NULL) { *pixel_stride = yuv420_afbc_luma_stride; } return true; } /* * Computes the strides and size for an YV12 buffer * * width Public known width of the buffer in pixels * height Public known height of the buffer in pixels * * pixel_stride (out) stride of the buffer in pixels * byte_stride (out) stride of the buffer in bytes * size (out) size of the buffer in bytes * type (in) if buffer should be allocated for a certain afbc type * internalHeight (out) Will store internal height if it is required to have a greater height than * known to public. If not it will be left untouched. * stride_alignment (in) stride aligment value in bytes. */ static bool get_yv12_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type, int* internalHeight, int stride_alignment) { int luma_stride; /* Android assumes the width and height are even withou checking, so we check here */ if (width % 2 != 0 || height % 2 != 0) { return false; } if (type != UNCOMPRESSED) { return get_afbc_yuv420_8bit_stride_and_size(width, height, pixel_stride, byte_stride, size, type, internalHeight); } /* Android assumes the buffer should be aligned to 16. */ luma_stride = GRALLOC_ALIGN(width, stride_alignment); if (size != NULL) { int chroma_stride = GRALLOC_ALIGN(luma_stride / 2, stride_alignment); /* Simplification of ((height * luma_stride ) + 2 * ((height / 2) * chroma_stride)). */ *size = height * (luma_stride + chroma_stride); } if (byte_stride != NULL) { *byte_stride = luma_stride; } if (pixel_stride != NULL) { *pixel_stride = luma_stride; } return true; } static bool get_blob_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type) { int luma_stride; luma_stride = width; if (size != NULL) { *size = ((height * width) + 4095) & (~4095); } if (byte_stride != NULL) { *byte_stride = luma_stride; } if (pixel_stride != NULL) { *pixel_stride = luma_stride; } return true; } static bool get_yuv_420_888_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size) { int luma_pixel_stride; if ((width & 1) || (height & 1)) { return false; } luma_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN); if (size != NULL) { int chroma_size = luma_pixel_stride * (height / 2); *size = luma_pixel_stride * height + chroma_size; } if (byte_stride != NULL) { *byte_stride = luma_pixel_stride; } if (pixel_stride != NULL) { *pixel_stride = luma_pixel_stride; } return true; } /* * Computes the strides and size for an AFBC 8BIT YUV 4:2:2 buffer * * width width of the buffer in pixels * height height of the buffer in pixels * * pixel_stride (out) stride of the buffer in pixels * byte_stride (out) stride of the buffer in bytes * size (out) size of the buffer in bytes * type if buffer should be allocated for a certain afbc type */ static bool get_afbc_yuv422_8bit_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type) { int yuv422_afbc_luma_stride; if (type == UNCOMPRESSED) { AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_10BIT_AFBC!"); return false; } if (type == AFBC_PADDED) { AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV"); return false; } if (type == AFBC_WIDEBLK) { width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN); } else { width = GRALLOC_ALIGN(width, AFBC_PIXELS_PER_BLOCK); } height = GRALLOC_ALIGN(height, AFBC_PIXELS_PER_BLOCK); yuv422_afbc_luma_stride = width; if (size != NULL) { int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK; /* YUV 4:2:2 luma size equals chroma size */ *size = yuv422_afbc_luma_stride * height * 2 + GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, AFBC_BODY_BUFFER_BYTE_ALIGNMENT); } if (byte_stride != NULL) { *byte_stride = yuv422_afbc_luma_stride; } if (pixel_stride != NULL) { *pixel_stride = yuv422_afbc_luma_stride; } return true; } /* * Calculate strides and sizes for a P010 (Y-UV 4:2:0) or P210 (Y-UV 4:2:2) buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param vss [in] Vertical sub-sampling factor (2 for P010, 1 for * P210. Anything else is invalid). * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv_pX10_stride_and_size(int width, int height, int vss, int* pixel_stride, int* byte_stride, size_t* size) { int luma_pixel_stride, luma_byte_stride; if (vss < 1 || vss > 2) { AERR("Invalid vertical sub-sampling factor: %d, should be 1 or 2", vss); return false; } /* odd height is allowed for P210 (2x1 sub-sampling) */ if ((width & 1) || (vss == 2 && (height & 1))) { return false; } luma_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN); luma_byte_stride = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN); if (size != NULL) { int chroma_size = GRALLOC_ALIGN(width * 2, YUV_MALI_PLANE_ALIGN) * (height / vss); *size = luma_byte_stride * height + chroma_size; } if (byte_stride != NULL) { *byte_stride = luma_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = luma_pixel_stride; } return true; } /* * Calculate strides and strides for Y210 (YUYV packed, 4:2:2) format buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv_y210_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size) { int y210_byte_stride, y210_pixel_stride; if (width & 1) { return false; } y210_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN); y210_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); if (size != NULL) { /* 4x16bits per pixel */ *size = y210_byte_stride * height; } if (byte_stride != NULL) { *byte_stride = y210_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = y210_pixel_stride; } return true; } /* * Calculate strides and strides for Y0L2 (YUYAAYVYAA, 4:2:0) format buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv_y0l2_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size) { int y0l2_byte_stride, y0l2_pixel_stride; if (width & 3) { return false; } y0l2_pixel_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); /* 4 pixels packed per line */ y0l2_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); /* Packed in 64-bit chunks, 2 x downsampled horizontally */ if (size != NULL) { /* 2 x downsampled vertically */ *size = y0l2_byte_stride * (height/2); } if (byte_stride != NULL) { *byte_stride = y0l2_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = y0l2_pixel_stride; } return true; } /* * Calculate strides and strides for Y410 (AVYU packed, 4:4:4) format buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv_y410_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size) { int y410_byte_stride, y410_pixel_stride; y410_pixel_stride = GRALLOC_ALIGN(width, YUV_MALI_PLANE_ALIGN); y410_byte_stride = GRALLOC_ALIGN(width * 4, YUV_MALI_PLANE_ALIGN); if (size != NULL) { /* 4x8bits per pixel */ *size = y410_byte_stride * height; } if (byte_stride != NULL) { *byte_stride = y410_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = y410_pixel_stride; } return true; } /* * Calculate strides and strides for YUV420_10BIT_AFBC (Compressed, 4:2:0) format buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * @param type [in] afbc mode that buffer should be allocated with. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv420_10bit_afbc_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type) { int yuv420_afbc_byte_stride, yuv420_afbc_pixel_stride; if (width & 3) { return false; } if (type == UNCOMPRESSED) { AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_10BIT_AFBC!"); return false; } if (type == AFBC_PADDED) { AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV"); return false; } if (type == AFBC_WIDEBLK) { width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN); } else { width = GRALLOC_ALIGN(width, AFBC_PIXELS_PER_BLOCK); } height = GRALLOC_ALIGN(height/2, AFBC_PIXELS_PER_BLOCK); /* vertically downsampled */ yuv420_afbc_pixel_stride = GRALLOC_ALIGN(width, 16); yuv420_afbc_byte_stride = GRALLOC_ALIGN(width * 4, 16); /* 64-bit packed and horizontally downsampled */ if (size != NULL) { int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK; *size = yuv420_afbc_byte_stride * height + GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, AFBC_BODY_BUFFER_BYTE_ALIGNMENT); } if (byte_stride != NULL) { *byte_stride = yuv420_afbc_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = yuv420_afbc_pixel_stride; } return true; } /* * Calculate strides and strides for YUV422_10BIT_AFBC (Compressed, 4:2:2) format buffer. * * @param width [in] Buffer width. * @param height [in] Buffer height. * @param pixel_stride [out] Pixel stride; number of pixels between * consecutive rows. * @param byte_stride [out] Byte stride; number of bytes between * consecutive rows. * @param size [out] Size of the buffer in bytes. Cumulative sum of * sizes of all planes. * @param type [in] afbc mode that buffer should be allocated with. * * @return true if the calculation was successful; false otherwise (invalid * parameter) */ static bool get_yuv422_10bit_afbc_stride_and_size(int width, int height, int* pixel_stride, int* byte_stride, size_t* size, AllocType type) { int yuv422_afbc_byte_stride, yuv422_afbc_pixel_stride; if (width & 3) { return false; } if (type == UNCOMPRESSED) { AERR(" Buffer must be allocated with AFBC mode for internal pixel format YUV420_10BIT_AFBC!"); return false; } if (type == AFBC_PADDED) { AERR("GRALLOC_USAGE_PRIVATE_2 (64byte header row alignment for AFBC) is not supported for YUV"); return false; } if (type == AFBC_WIDEBLK) { width = GRALLOC_ALIGN(width, AFBC_WIDEBLK_WIDTH_ALIGN); } else { width = GRALLOC_ALIGN(width, AFBC_PIXELS_PER_BLOCK); } height = GRALLOC_ALIGN(height, AFBC_PIXELS_PER_BLOCK); /* total number of rows must be even number */ yuv422_afbc_pixel_stride = GRALLOC_ALIGN(width, 16); yuv422_afbc_byte_stride = GRALLOC_ALIGN(width * 2, 16); if (size != NULL) { int nblocks = width / AFBC_PIXELS_PER_BLOCK * height / AFBC_PIXELS_PER_BLOCK; /* YUV 4:2:2 chroma size equals to luma size */ *size = yuv422_afbc_byte_stride * height * 2 + GRALLOC_ALIGN(nblocks * AFBC_HEADER_BUFFER_BYTES_PER_BLOCKENTRY, AFBC_BODY_BUFFER_BYTE_ALIGNMENT); } if (byte_stride != NULL) { *byte_stride = yuv422_afbc_byte_stride; } if (pixel_stride != NULL) { *pixel_stride = yuv422_afbc_pixel_stride; } return true; } static int alloc_device_alloc(alloc_device_t* dev, int w, int h, int format, int usage, buffer_handle_t* pHandle, int* pStride) { if (!pHandle || !pStride) { return -EINVAL; } size_t size; // Size to be allocated for the buffer int byte_stride; // Stride of the buffer in bytes int pixel_stride; // Stride of the buffer in pixels - as returned in pStride uint64_t internal_format; AllocType type = UNCOMPRESSED; bool alloc_for_extended_yuv = false, alloc_for_arm_afbc_yuv = false; int internalWidth,internalHeight; int buffer_width = w; #if defined(GRALLOC_FB_SWAP_RED_BLUE) /* match the framebuffer format */ if (usage & GRALLOC_USAGE_HW_FB) { #ifdef GRALLOC_16_BITS format = HAL_PIXEL_FORMAT_RGB_565; #else format = HAL_PIXEL_FORMAT_BGRA_8888; #endif } #endif /* Pick the right concrete pixel format given the endpoints as encoded in * the usage bits. Every end-point pair needs explicit listing here. */ if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { // Camera as producer if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { if (usage & GRALLOC_USAGE_HW_TEXTURE) { // Camera-to-display is NV21 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { // Camera-to-encoder is NV21 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; } else if ((usage & GRALLOC_USAGE_HW_CAMERA_MASK) == GRALLOC_USAGE_HW_CAMERA_ZSL) { // Camera-to-ZSL-queue is NV21 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; } } if (usage & GRALLOC_USAGE_HW_COMPOSER) { if (usage & GRALLOC_USAGE_HW_TEXTURE) { // VirtualDisplaySurface-to-encoder is NV21 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; } else if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { // VirtualDisplaySurface-to-encoder is NV21 format = HAL_PIXEL_FORMAT_YCrCb_420_SP; } } if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { ALOGE("gralloc_alloc: Requested auto format selection, " "but no known format for this usage: %d x %d, usage %x", w, h, usage); return -EINVAL; } } /* Some formats require an internal width and height that may be used by * consumers/producers. */ #if PLATFORM_SDK_VERSION >= 24 if (usage & GRALLOC_USAGE_AML_OMX_OVERLAY) { w = OMX_VIDEOLAYER_ALLOC_BUFFER_WIDTH; h = OMX_VIDEOLAYER_ALLOC_BUFFER_HEIGHT; } #endif internalWidth = w; internalHeight = h; internal_format = gralloc_select_format(format, usage, w*h); alloc_for_extended_yuv = (internal_format & GRALLOC_ARM_INTFMT_EXTENDED_YUV) == GRALLOC_ARM_INTFMT_EXTENDED_YUV; alloc_for_arm_afbc_yuv = (internal_format & GRALLOC_ARM_INTFMT_ARM_AFBC_YUV) == GRALLOC_ARM_INTFMT_ARM_AFBC_YUV; if (internal_format & (GRALLOC_ARM_INTFMT_AFBC | GRALLOC_ARM_INTFMT_AFBC_SPLITBLK | GRALLOC_ARM_INTFMT_AFBC_WIDEBLK)) { if (usage & GRALLOC_USAGE_PRIVATE_2) { type = AFBC_PADDED; } else if (internal_format & GRALLOC_ARM_INTFMT_AFBC_WIDEBLK) { #if 1 != MALI_USE_YUV_AFBC_WIDEBLK if (alloc_for_arm_afbc_yuv) { ALOGE("Unsupported format YUV AFBC WIDEBLK."); return -EINVAL; } #endif type = AFBC_WIDEBLK; } else { type = AFBC; } } if (!alloc_for_extended_yuv && !alloc_for_arm_afbc_yuv) { int yv12_align = YUV_MALI_PLANE_ALIGN; switch (internal_format & GRALLOC_ARM_INTFMT_FMT_MASK) { case HAL_PIXEL_FORMAT_RGBA_8888: case HAL_PIXEL_FORMAT_RGBX_8888: case HAL_PIXEL_FORMAT_BGRA_8888: #if PLATFORM_SDK_VERSION >= 19 && PLATFORM_SDK_VERSION <= 22 case HAL_PIXEL_FORMAT_sRGB_A_8888: case HAL_PIXEL_FORMAT_sRGB_X_8888: #endif get_rgb_stride_and_size(w, h, 4, &pixel_stride, &byte_stride, &size, type ); break; case HAL_PIXEL_FORMAT_RGB_888: get_rgb_stride_and_size(w, h, 3, &pixel_stride, &byte_stride, &size, type ); break; case HAL_PIXEL_FORMAT_RGB_565: #if PLATFORM_SDK_VERSION < 19 case HAL_PIXEL_FORMAT_RGBA_5551: case HAL_PIXEL_FORMAT_RGBA_4444: #endif get_rgb_stride_and_size(w, h, 2, &pixel_stride, &byte_stride, &size, type ); break; case HAL_PIXEL_FORMAT_YCrCb_420_SP: case HAL_PIXEL_FORMAT_YCbCr_420_SP: case HAL_PIXEL_FORMAT_YCbCr_420_888: if (!get_yv12_stride_and_size(GRALLOC_ALIGN(w, 2), GRALLOC_ALIGN(h, 2), &pixel_stride, &byte_stride, &size, type, &internalHeight, yv12_align)) { return -EINVAL; } #if PLATFORM_SDK_VERSION >= 24 if (usage & GRALLOC_USAGE_AML_OMX_OVERLAY) { pixel_stride = buffer_width; } #endif break; case HAL_PIXEL_FORMAT_YV12: // Mali subsystem prefers higher stride alignment values (128b) for YUV, but software components assume default of 16. // We only need to care about YV12 as it's the only, implicit, HAL YUV format in Android. if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { yv12_align = YUV_ANDROID_PLANE_ALIGN; } if (!get_yv12_stride_and_size(GRALLOC_ALIGN(w, 2), GRALLOC_ALIGN(h, 2), &pixel_stride, &byte_stride, &size, type, &internalHeight, yv12_align)) { return -EINVAL; } #if PLATFORM_SDK_VERSION >= 24 if (usage & GRALLOC_USAGE_AML_OMX_OVERLAY) { pixel_stride = buffer_width; } #endif break; /* * Additional custom formats can be added here * and must fill the variables pixel_stride, byte_stride and size. */ case HAL_PIXEL_FORMAT_BLOB: get_blob_stride_and_size(w, h, &pixel_stride, &byte_stride, &size, type); break; /*case HAL_PIXEL_FORMAT_YCbCr_420_888: get_yuv_420_888_stride_and_size(w, h, &pixel_stride, &byte_stride, &size); break;*/ default: return -EINVAL; } } else { switch (internal_format & GRALLOC_ARM_INTFMT_FMT_MASK) { case GRALLOC_ARM_HAL_FORMAT_INDEXED_Y0L2: /* YUYAAYUVAA 4:2:0 */ if (false == get_yuv_y0l2_stride_and_size(w, h, &pixel_stride, &byte_stride, &size)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_P010: /* Y-UV 4:2:0 */ if (false == get_yuv_pX10_stride_and_size(w, h, 2, &pixel_stride, &byte_stride, &size)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_P210: /* Y-UV 4:2:2 */ if (false == get_yuv_pX10_stride_and_size(w, h, 1, &pixel_stride, &byte_stride, &size)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_Y210: /* YUYV 4:2:0 */ if (false == get_yuv_y210_stride_and_size(w, h, &pixel_stride, &byte_stride, &size)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_Y410: /* AVYU 2-10-10-10 */ if (false == get_yuv_y410_stride_and_size(w, h, &pixel_stride, &byte_stride, &size)) { return -EINVAL; } break; /* 8BIT AFBC YUV 4:2:0 testing usage */ case GRALLOC_ARM_HAL_FORMAT_INDEXED_YUV420_8BIT_AFBC: if (!get_afbc_yuv420_8bit_stride_and_size(w, h, &pixel_stride, &byte_stride, &size, type, &internalHeight)) { return -EINVAL; } break; /* 8BIT AFBC YUV4:2:2 testing usage */ case GRALLOC_ARM_HAL_FORMAT_INDEXED_YUV422_8BIT_AFBC: if (!get_afbc_yuv422_8bit_stride_and_size(w, h, &pixel_stride, &byte_stride, &size, type)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_YUV420_10BIT_AFBC: /* YUV 4:2:0 compressed */ if (false == get_yuv420_10bit_afbc_stride_and_size(w, h, &pixel_stride, &byte_stride, &size, type)) { return -EINVAL; } break; case GRALLOC_ARM_HAL_FORMAT_INDEXED_YUV422_10BIT_AFBC: /* YUV 4:2:2 compressed */ if (false == get_yuv422_10bit_afbc_stride_and_size(w, h, &pixel_stride, &byte_stride, &size, type)) { return -EINVAL; } break; default: AERR("Invalid internal format %llx", internal_format & GRALLOC_ARM_INTFMT_FMT_MASK); return -EINVAL; } } int err; #if DISABLE_FRAMEBUFFER_HAL != 1 && GRALLOC_ALLOC_FB_FROM_ION != 1 if (usage & GRALLOC_USAGE_HW_FB) { err = gralloc_alloc_framebuffer(dev, size, usage, pHandle, &pixel_stride, &byte_stride); } else #endif { err = alloc_backend_alloc(dev, size, usage, pHandle); } if (err < 0) { return err; } private_handle_t *hnd = (private_handle_t *)*pHandle; #if MALI_AFBC_GRALLOC == 1 err = gralloc_buffer_attr_allocate( hnd ); if ( err < 0 ) { private_module_t* m = reinterpret_cast(dev->common.module); if ( (usage & GRALLOC_USAGE_HW_FB) ) { /* * Having the attribute region is not critical for the framebuffer so let it pass. */ err = 0; } else { alloc_backend_alloc_free( hnd, m ); return err; } } #endif hnd->req_format = format; hnd->byte_stride = byte_stride; hnd->internal_format = internal_format; hnd->format = format; int private_usage = usage & (GRALLOC_USAGE_PRIVATE_0 | GRALLOC_USAGE_PRIVATE_1); switch (private_usage) { case 0: hnd->yuv_info = MALI_YUV_BT601_NARROW; break; case GRALLOC_USAGE_PRIVATE_1: hnd->yuv_info = MALI_YUV_BT601_WIDE; break; case GRALLOC_USAGE_PRIVATE_0: hnd->yuv_info = MALI_YUV_BT709_NARROW; break; case (GRALLOC_USAGE_PRIVATE_0 | GRALLOC_USAGE_PRIVATE_1): hnd->yuv_info = MALI_YUV_BT709_WIDE; break; } if (usage & GRALLOC_USAGE_AML_VIDEO_OVERLAY) { hnd->flags |= private_handle_t::PRIV_FLAGS_VIDEO_OVERLAY; } if (usage & GRALLOC_USAGE_AML_DMA_BUFFER) { hnd->flags |= private_handle_t::PRIV_FLAGS_OSD_VIDEO_OMX; } if (usage & GRALLOC_USAGE_AML_OMX_OVERLAY) { private_handle_t* hnd = (private_handle_t*)(*pHandle); hnd->flags |= private_handle_t::PRIV_FLAGS_VIDEO_OMX; } hnd->width = w; hnd->height = h; hnd->stride = pixel_stride; hnd->internalWidth = internalWidth; hnd->internalHeight = internalHeight; *pStride = pixel_stride; return 0; } static int alloc_device_free(alloc_device_t* dev, buffer_handle_t handle) { if (private_handle_t::validate(handle) < 0) { return -EINVAL; } private_handle_t const* hnd = reinterpret_cast(handle); private_module_t* m = reinterpret_cast(dev->common.module); if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { // free this buffer private_module_t* priv_t = reinterpret_cast(dev->common.module); framebuffer_mapper_t* m = NULL; #ifdef DEBUG_EXTERNAL_DISPLAY_ON_PANEL ALOGD("always free from fb0"); m = &(priv_t->fb_primary); #else if (hnd->usage & GRALLOC_USAGE_EXTERNAL_DISP) { m = &(priv_t->fb_external); } else { m = &(priv_t->fb_primary); } #endif int index = ((uintptr_t)hnd->base - (uintptr_t)m->framebuffer->base) / m->bufferSize; m->bufferMask &= ~(1<common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; dev->common.module = const_cast(module); dev->common.close = alloc_backend_close; dev->alloc = alloc_device_alloc; dev->free = alloc_device_free; if (0 != alloc_backend_open(dev)) { delete dev; return -1; } *device = &dev->common; return 0; }