Home | History | Annotate | Download | only in libgralloc1
      1 /*
      2  * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved.
      3 
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *   * Redistributions of source code must retain the above copyright
      8  *     notice, this list of conditions and the following disclaimer.
      9  *   * Redistributions in binary form must reproduce the above
     10  *     copyright notice, this list of conditions and the following
     11  *     disclaimer in the documentation and/or other materials provided
     12  *     with the distribution.
     13  *   * Neither the name of The Linux Foundation nor the names of its
     14  *     contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "gr_utils.h"
     31 
     32 namespace gralloc1 {
     33 
     34 bool IsUncompressedRGBFormat(int format) {
     35   switch (format) {
     36     case HAL_PIXEL_FORMAT_RGBA_8888:
     37     case HAL_PIXEL_FORMAT_RGBX_8888:
     38     case HAL_PIXEL_FORMAT_RGB_888:
     39     case HAL_PIXEL_FORMAT_RGB_565:
     40     case HAL_PIXEL_FORMAT_BGR_565:
     41     case HAL_PIXEL_FORMAT_BGRA_8888:
     42     case HAL_PIXEL_FORMAT_RGBA_5551:
     43     case HAL_PIXEL_FORMAT_RGBA_4444:
     44     case HAL_PIXEL_FORMAT_R_8:
     45     case HAL_PIXEL_FORMAT_RG_88:
     46     case HAL_PIXEL_FORMAT_BGRX_8888:
     47     case HAL_PIXEL_FORMAT_RGBA_1010102:
     48     case HAL_PIXEL_FORMAT_ARGB_2101010:
     49     case HAL_PIXEL_FORMAT_RGBX_1010102:
     50     case HAL_PIXEL_FORMAT_XRGB_2101010:
     51     case HAL_PIXEL_FORMAT_BGRA_1010102:
     52     case HAL_PIXEL_FORMAT_ABGR_2101010:
     53     case HAL_PIXEL_FORMAT_BGRX_1010102:
     54     case HAL_PIXEL_FORMAT_XBGR_2101010:
     55       return true;
     56     default:
     57       break;
     58   }
     59 
     60   return false;
     61 }
     62 
     63 bool IsCompressedRGBFormat(int format) {
     64   switch (format) {
     65     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
     66     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
     67     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
     68     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
     69     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
     70     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
     71     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
     72     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
     73     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
     74     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
     75     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
     76     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
     77     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
     78     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
     79     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
     80     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
     81     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
     82     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
     83     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
     84     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
     85     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
     86     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
     87     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
     88     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
     89     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
     90     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
     91     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
     92     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
     93       return true;
     94     default:
     95       break;
     96   }
     97 
     98   return false;
     99 }
    100 
    101 uint32_t GetBppForUncompressedRGB(int format) {
    102   uint32_t bpp = 0;
    103   switch (format) {
    104     case HAL_PIXEL_FORMAT_RGBA_8888:
    105     case HAL_PIXEL_FORMAT_RGBX_8888:
    106     case HAL_PIXEL_FORMAT_BGRA_8888:
    107     case HAL_PIXEL_FORMAT_BGRX_8888:
    108       bpp = 4;
    109       break;
    110     case HAL_PIXEL_FORMAT_RGB_888:
    111       bpp = 3;
    112       break;
    113     case HAL_PIXEL_FORMAT_RGB_565:
    114     case HAL_PIXEL_FORMAT_BGR_565:
    115     case HAL_PIXEL_FORMAT_RGBA_5551:
    116     case HAL_PIXEL_FORMAT_RGBA_4444:
    117       bpp = 2;
    118       break;
    119     default:
    120       ALOGE("Error : %s New format request", __FUNCTION__);
    121       break;
    122   }
    123 
    124   return bpp;
    125 }
    126 
    127 bool CpuCanAccess(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) {
    128   return CpuCanRead(prod_usage, cons_usage) || CpuCanWrite(prod_usage);
    129 }
    130 
    131 bool CpuCanRead(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) {
    132   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) {
    133     return true;
    134   }
    135 
    136   if (cons_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) {
    137     return true;
    138   }
    139 
    140   return false;
    141 }
    142 
    143 bool CpuCanWrite(gralloc1_producer_usage_t prod_usage) {
    144   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) {
    145     // Application intends to use CPU for rendering
    146     return true;
    147   }
    148 
    149   return false;
    150 }
    151 
    152 }  // namespace gralloc1
    153