1 /*
2  * Copyright (c) 2011-2017, 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 
IsUncompressedRGBFormat(int format)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     case HAL_PIXEL_FORMAT_RGBA_FP16:
56       return true;
57     default:
58       break;
59   }
60 
61   return false;
62 }
63 
IsCompressedRGBFormat(int format)64 bool IsCompressedRGBFormat(int format) {
65   switch (format) {
66     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
67     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
68     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
69     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
70     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
71     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
72     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
73     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
74     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
75     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
76     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
77     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
78     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
79     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
80     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
81     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
82     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
83     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
84     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
85     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
86     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
87     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
88     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
89     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
90     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
91     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
92     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
93     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
94       return true;
95     default:
96       break;
97   }
98 
99   return false;
100 }
101 
GetBppForUncompressedRGB(int format)102 uint32_t GetBppForUncompressedRGB(int format) {
103   uint32_t bpp = 0;
104   switch (format) {
105     case HAL_PIXEL_FORMAT_RGBA_FP16:
106       bpp = 8;
107       break;
108     case HAL_PIXEL_FORMAT_RGBA_8888:
109     case HAL_PIXEL_FORMAT_RGBX_8888:
110     case HAL_PIXEL_FORMAT_BGRA_8888:
111     case HAL_PIXEL_FORMAT_BGRX_8888:
112     case HAL_PIXEL_FORMAT_RGBA_1010102:
113     case HAL_PIXEL_FORMAT_ARGB_2101010:
114     case HAL_PIXEL_FORMAT_RGBX_1010102:
115     case HAL_PIXEL_FORMAT_XRGB_2101010:
116     case HAL_PIXEL_FORMAT_BGRA_1010102:
117     case HAL_PIXEL_FORMAT_ABGR_2101010:
118     case HAL_PIXEL_FORMAT_BGRX_1010102:
119     case HAL_PIXEL_FORMAT_XBGR_2101010:
120       bpp = 4;
121       break;
122     case HAL_PIXEL_FORMAT_RGB_888:
123       bpp = 3;
124       break;
125     case HAL_PIXEL_FORMAT_RGB_565:
126     case HAL_PIXEL_FORMAT_BGR_565:
127     case HAL_PIXEL_FORMAT_RGBA_5551:
128     case HAL_PIXEL_FORMAT_RGBA_4444:
129       bpp = 2;
130       break;
131     default:
132       ALOGE("Error : %s New format request = 0x%x", __FUNCTION__, format);
133       break;
134   }
135 
136   return bpp;
137 }
138 
CpuCanAccess(gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage)139 bool CpuCanAccess(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) {
140   return CpuCanRead(prod_usage, cons_usage) || CpuCanWrite(prod_usage);
141 }
142 
CpuCanRead(gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage)143 bool CpuCanRead(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) {
144   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) {
145     return true;
146   }
147 
148   if (cons_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) {
149     return true;
150   }
151 
152   return false;
153 }
154 
CpuCanWrite(gralloc1_producer_usage_t prod_usage)155 bool CpuCanWrite(gralloc1_producer_usage_t prod_usage) {
156   if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) {
157     // Application intends to use CPU for rendering
158     return true;
159   }
160 
161   return false;
162 }
163 
164 }  // namespace gralloc1
165