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