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 <dlfcn.h>
31 #include <string.h>
32 
33 #include <cutils/properties.h>
34 #include <log/log.h>
35 
36 #include "gralloc_priv.h"
37 #include "gr_adreno_info.h"
38 #include "gr_utils.h"
39 
40 namespace gralloc1 {
41 
AdrenoMemInfo()42 AdrenoMemInfo::AdrenoMemInfo() {
43 }
44 
Init()45 bool AdrenoMemInfo::Init() {
46   libadreno_utils_ = ::dlopen("libadreno_utils.so", RTLD_NOW);
47   if (libadreno_utils_) {
48     *reinterpret_cast<void **>(&LINK_adreno_compute_aligned_width_and_height) =
49         ::dlsym(libadreno_utils_, "compute_aligned_width_and_height");
50     *reinterpret_cast<void **>(&LINK_adreno_compute_padding) =
51         ::dlsym(libadreno_utils_, "compute_surface_padding");
52     *reinterpret_cast<void **>(&LINK_adreno_compute_compressedfmt_aligned_width_and_height) =
53         ::dlsym(libadreno_utils_, "compute_compressedfmt_aligned_width_and_height");
54     *reinterpret_cast<void **>(&LINK_adreno_isUBWCSupportedByGpu) =
55         ::dlsym(libadreno_utils_, "isUBWCSupportedByGpu");
56     *reinterpret_cast<void **>(&LINK_adreno_get_gpu_pixel_alignment) =
57         ::dlsym(libadreno_utils_, "get_gpu_pixel_alignment");
58   } else {
59     ALOGW(" Failed to load libadreno_utils.so");
60   }
61 
62   // Check if the overriding property debug.gralloc.gfx_ubwc_disable_
63   // that disables UBWC allocations for the graphics stack is set
64   char property[PROPERTY_VALUE_MAX];
65   property_get("debug.gralloc.gfx_ubwc_disable_", property, "0");
66   if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
67       !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
68     gfx_ubwc_disable_ = true;
69   }
70 
71   if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
72       (!strncmp(property, "1", PROPERTY_VALUE_MAX) ||
73        (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) {
74     map_fb_ = true;
75   }
76 
77   return true;
78 }
79 
~AdrenoMemInfo()80 AdrenoMemInfo::~AdrenoMemInfo() {
81   if (libadreno_utils_) {
82     ::dlclose(libadreno_utils_);
83   }
84 }
85 
AlignUnCompressedRGB(int width,int height,int format,int tile_enabled,unsigned int * aligned_w,unsigned int * aligned_h)86 void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled,
87                                          unsigned int *aligned_w, unsigned int *aligned_h) {
88   *aligned_w = (unsigned int)ALIGN(width, 32);
89   *aligned_h = (unsigned int)ALIGN(height, 32);
90 
91   // Don't add any additional padding if debug.gralloc.map_fb_memory
92   // is enabled
93   if (map_fb_) {
94     return;
95   }
96 
97   int bpp = 4;
98   switch (format) {
99     case HAL_PIXEL_FORMAT_RGB_888:
100       bpp = 3;
101       break;
102     case HAL_PIXEL_FORMAT_RGB_565:
103     case HAL_PIXEL_FORMAT_BGR_565:
104     case HAL_PIXEL_FORMAT_RGBA_5551:
105     case HAL_PIXEL_FORMAT_RGBA_4444:
106       bpp = 2;
107       break;
108     default:
109       break;
110   }
111 
112   int raster_mode = 0;          // Adreno unknown raster mode.
113   int padding_threshold = 512;  // Threshold for padding surfaces.
114   // the function below computes aligned width and aligned height
115   // based on linear or macro tile mode selected.
116   if (LINK_adreno_compute_aligned_width_and_height) {
117     LINK_adreno_compute_aligned_width_and_height(
118         width, height, bpp, tile_enabled, raster_mode, padding_threshold,
119         reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h));
120   } else if (LINK_adreno_compute_padding) {
121     int surface_tile_height = 1;  // Linear surface
122     *aligned_w = UINT(LINK_adreno_compute_padding(width, bpp, surface_tile_height, raster_mode,
123                                                   padding_threshold));
124     ALOGW("%s: Warning!! Old GFX API is used to calculate stride", __FUNCTION__);
125   } else {
126     ALOGW(
127         "%s: Warning!! Symbols compute_surface_padding and "
128         "compute_aligned_width_and_height not found",
129         __FUNCTION__);
130   }
131 }
132 
AlignCompressedRGB(int width,int height,int format,unsigned int * aligned_w,unsigned int * aligned_h)133 void AdrenoMemInfo::AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w,
134                                        unsigned int *aligned_h) {
135   if (LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
136     int bytesPerPixel = 0;
137     int raster_mode = 0;          // Adreno unknown raster mode.
138     int padding_threshold = 512;  // Threshold for padding
139     // surfaces.
140 
141     LINK_adreno_compute_compressedfmt_aligned_width_and_height(
142         width, height, format, 0, raster_mode, padding_threshold,
143         reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h), &bytesPerPixel);
144   } else {
145     *aligned_w = (unsigned int)ALIGN(width, 32);
146     *aligned_h = (unsigned int)ALIGN(height, 32);
147     ALOGW("%s: Warning!! compute_compressedfmt_aligned_width_and_height not found", __FUNCTION__);
148   }
149 }
150 
IsUBWCSupportedByGPU(int format)151 bool AdrenoMemInfo::IsUBWCSupportedByGPU(int format) {
152   if (!gfx_ubwc_disable_ && LINK_adreno_isUBWCSupportedByGpu) {
153     ADRENOPIXELFORMAT gpu_format = GetGpuPixelFormat(format);
154     return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
155   }
156 
157   return false;
158 }
159 
GetGpuPixelAlignment()160 uint32_t AdrenoMemInfo::GetGpuPixelAlignment() {
161   if (LINK_adreno_get_gpu_pixel_alignment) {
162     return LINK_adreno_get_gpu_pixel_alignment();
163   }
164 
165   return 1;
166 }
167 
GetGpuPixelFormat(int hal_format)168 ADRENOPIXELFORMAT AdrenoMemInfo::GetGpuPixelFormat(int hal_format) {
169   switch (hal_format) {
170     case HAL_PIXEL_FORMAT_RGBA_8888:
171       return ADRENO_PIXELFORMAT_R8G8B8A8;
172     case HAL_PIXEL_FORMAT_RGBX_8888:
173       return ADRENO_PIXELFORMAT_R8G8B8X8;
174     case HAL_PIXEL_FORMAT_RGB_565:
175       return ADRENO_PIXELFORMAT_B5G6R5;
176     case HAL_PIXEL_FORMAT_BGR_565:
177       return ADRENO_PIXELFORMAT_R5G6B5;
178     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
179       return ADRENO_PIXELFORMAT_NV12;
180     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
181     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
182       return ADRENO_PIXELFORMAT_NV12_EXT;
183     case HAL_PIXEL_FORMAT_RGBA_1010102:
184        return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
185     case HAL_PIXEL_FORMAT_RGBX_1010102:
186        return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
187     case HAL_PIXEL_FORMAT_ABGR_2101010:
188        return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
189     default:
190       ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
191       break;
192   }
193 
194   return ADRENO_PIXELFORMAT_UNKNOWN;
195 }
196 
197 }  // namespace gralloc1
198