summaryrefslogtreecommitdiff
path: root/libhwc2.1/libcolormanager/DisplayColorModule.cpp
blob: 496caf68305d7c1994d75e078ce1a8b6a1f705fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * Copyright (C) 2022 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 <drm/samsung_drm.h>
#include "DisplayColorModule.h"

using namespace android;
namespace gs {

template <typename T, typename M>
int32_t convertDqeMatrixDataToDrmMatrix(T &colorMatrix, M &mat, uint32_t dimension) {
    if (colorMatrix.coeffs.size() != (dimension * dimension)) {
        ALOGE("Invalid coeff size(%zu)",
                colorMatrix.coeffs.size());
        return -EINVAL;
    }
    if (colorMatrix.offsets.size() != dimension) {
        ALOGE("Invalid offset size(%zu)",
                colorMatrix.offsets.size());
        return -EINVAL;
    }
    for (uint32_t i = 0; i < (dimension * dimension); i++) {
        mat.coeffs[i] = colorMatrix.coeffs[i];
    }

    for (uint32_t i = 0; i < dimension; i++) {
        mat.offsets[i] = colorMatrix.offsets[i];
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::eotf(const GsInterfaceType::IDpp::EotfData::ConfigType *config,
                                  DrmDevice *drm, uint32_t &blobId) {
    struct hdr_eotf_lut eotfLut;

    if (config == nullptr) {
        ALOGE("no dpp eotf config");
        return -EINVAL;
    }

    if ((config->tf_data.posx.size() != DRM_SAMSUNG_HDR_EOTF_LUT_LEN) ||
        (config->tf_data.posy.size() != DRM_SAMSUNG_HDR_EOTF_LUT_LEN)) {
        ALOGE("%s: eotf pos size (%zu, %zu)", __func__, config->tf_data.posx.size(),
              config->tf_data.posy.size());
        return -EINVAL;
    }

    for (uint32_t i = 0; i < DRM_SAMSUNG_HDR_EOTF_LUT_LEN; i++) {
        eotfLut.posx[i] = config->tf_data.posx[i];
        eotfLut.posy[i] = config->tf_data.posy[i];
    }
    int ret = drm->CreatePropertyBlob(&eotfLut, sizeof(eotfLut), &blobId);
    if (ret) {
        ALOGE("Failed to create eotf lut blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::gm(const GsInterfaceType::IDpp::GmData::ConfigType *config,
                                DrmDevice *drm, uint32_t &blobId) {
    int ret = 0;
    struct hdr_gm_data gmMatrix;

    if (config == nullptr) {
        ALOGE("no dpp GM config");
        return -EINVAL;
    }

    if ((ret = convertDqeMatrixDataToDrmMatrix(config->matrix_data, gmMatrix,
                                            DRM_SAMSUNG_HDR_GM_DIMENS)) != NO_ERROR) {
        ALOGE("Failed to convert gm matrix");
        return ret;
    }
    ret = drm->CreatePropertyBlob(&gmMatrix, sizeof(gmMatrix), &blobId);
    if (ret) {
        ALOGE("Failed to create gm matrix blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::dtm(const GsInterfaceType::IDpp::DtmData::ConfigType *config,
                                 DrmDevice *drm, uint32_t &blobId) {
    struct hdr_tm_data tmData;

    if (config == nullptr) {
        ALOGE("no dpp DTM config");
        return -EINVAL;
    }

    if ((config->tf_data.posx.size() != DRM_SAMSUNG_HDR_TM_LUT_LEN) ||
        (config->tf_data.posy.size() != DRM_SAMSUNG_HDR_TM_LUT_LEN)) {
        ALOGE("%s: dtm pos size (%zu, %zu)", __func__, config->tf_data.posx.size(),
              config->tf_data.posy.size());
        return -EINVAL;
    }

    for (uint32_t i = 0; i < DRM_SAMSUNG_HDR_TM_LUT_LEN; i++) {
        tmData.posx[i] = config->tf_data.posx[i];
        tmData.posy[i] = config->tf_data.posy[i];
    }

    tmData.coeff_r = config->coeff_r;
    tmData.coeff_g = config->coeff_g;
    tmData.coeff_b = config->coeff_b;
    tmData.rng_x_min = config->rng_x_min;
    tmData.rng_x_max = config->rng_x_max;
    tmData.rng_y_min = config->rng_y_min;
    tmData.rng_y_max = config->rng_y_max;

    int ret = drm->CreatePropertyBlob(&tmData, sizeof(tmData), &blobId);
    if (ret) {
        ALOGE("Failed to create tmData blob %d", ret);
        return ret;
    }

    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::oetf(const GsInterfaceType::IDpp::OetfData::ConfigType *config,
                                  DrmDevice *drm, uint32_t &blobId) {
    struct hdr_oetf_lut oetfLut;

    if (config == nullptr) {
        ALOGE("no dpp OETF config");
        return -EINVAL;
    }

    if ((config->tf_data.posx.size() != DRM_SAMSUNG_HDR_OETF_LUT_LEN) ||
        (config->tf_data.posy.size() != DRM_SAMSUNG_HDR_OETF_LUT_LEN)) {
        ALOGE("%s: oetf pos size (%zu, %zu)", __func__, config->tf_data.posx.size(),
              config->tf_data.posy.size());
        return -EINVAL;
    }

    for (uint32_t i = 0; i < DRM_SAMSUNG_HDR_OETF_LUT_LEN; i++) {
        oetfLut.posx[i] = config->tf_data.posx[i];
        oetfLut.posy[i] = config->tf_data.posy[i];
    }
    int ret = drm->CreatePropertyBlob(&oetfLut, sizeof(oetfLut), &blobId);
    if (ret) {
        ALOGE("Failed to create oetf lut blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::gammaMatrix(
        const GsInterfaceType::IDqe::DqeMatrixData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    int ret = 0;
    struct exynos_matrix gammaMatrix;
    if ((ret = convertDqeMatrixDataToDrmMatrix(config->matrix_data, gammaMatrix,
                                            DRM_SAMSUNG_MATRIX_DIMENS)) != NO_ERROR) {
        ALOGE("Failed to convert gamma matrix");
        return ret;
    }
    ret = drm->CreatePropertyBlob(&gammaMatrix, sizeof(gammaMatrix), &blobId);
    if (ret) {
        ALOGE("Failed to create gamma matrix blob %d", ret);
        return ret;
    }

    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::degamma(const uint64_t drmLutSize,
        const GsInterfaceType::IDqe::DegammaLutData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    if (config == nullptr) {
        ALOGE("no degamma config");
        return -EINVAL;
    }
    using ConfigType = typename GsInterfaceType::IDqe::DegammaLutData::ConfigType;
    if (drmLutSize != ConfigType::kLutLen) {
        ALOGE("degamma lut size mismatch");
        return -EINVAL;
    }

    struct drm_color_lut colorLut[ConfigType::kLutLen];
    for (uint32_t i = 0; i < ConfigType::kLutLen; i++) {
        colorLut[i].red = config->values[i];
    }
    int ret = drm->CreatePropertyBlob(colorLut, sizeof(colorLut), &blobId);
    if (ret) {
        ALOGE("Failed to create degamma lut blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::linearMatrix(
        const GsInterfaceType::IDqe::DqeMatrixData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    int ret = 0;
    struct exynos_matrix linear_matrix;
    if ((ret = convertDqeMatrixDataToDrmMatrix(config->matrix_data, linear_matrix,
                                            DRM_SAMSUNG_MATRIX_DIMENS)) != NO_ERROR) {
        ALOGE("Failed to convert linear matrix");
        return ret;
    }
    ret = drm->CreatePropertyBlob(&linear_matrix, sizeof(linear_matrix), &blobId);
    if (ret) {
        ALOGE("Failed to create linear matrix blob %d", ret);
        return ret;
    }

    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::cgc(const GsInterfaceType::IDqe::CgcData::ConfigType *config,
                                 DrmDevice *drm, uint32_t &blobId) {
    struct cgc_lut cgc;
    if (config == nullptr) {
        ALOGE("no CGC config");
        return -EINVAL;
    }

    if ((config->r_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT) ||
        (config->g_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT) ||
        (config->b_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT)) {
        ALOGE("CGC data size is not same (r: %zu, g: %zu: b: %zu)", config->r_values.size(),
              config->g_values.size(), config->b_values.size());
        return -EINVAL;
    }

    for (uint32_t i = 0; i < DRM_SAMSUNG_CGC_LUT_REG_CNT; i++) {
        cgc.r_values[i] = config->r_values[i];
        cgc.g_values[i] = config->g_values[i];
        cgc.b_values[i] = config->b_values[i];
    }
    int ret = drm->CreatePropertyBlob(&cgc, sizeof(cgc_lut), &blobId);
    if (ret) {
        ALOGE("Failed to create cgc blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::cgcDither(
        const GsInterfaceType::IDqe::DqeControlData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    int ret = 0;
    if (config->cgc_dither_override == false) {
        blobId = 0;
        return ret;
    }

    ret = drm->CreatePropertyBlob((void *)&config->cgc_dither_reg, sizeof(config->cgc_dither_reg),
                                  &blobId);
    if (ret) {
        ALOGE("Failed to create disp dither blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::regamma(
        const uint64_t drmLutSize,
        const GsInterfaceType::IDqe::RegammaLutData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    if (config == nullptr) {
        ALOGE("no regamma config");
        return -EINVAL;
    }

    using ConfigType = typename GsInterfaceType::IDqe::RegammaLutData::ConfigType;
    if (drmLutSize != ConfigType::kChannelLutLen) {
        ALOGE("gamma lut size mismatch");
        return -EINVAL;
    }

    struct drm_color_lut colorLut[ConfigType::kChannelLutLen];
    for (uint32_t i = 0; i < ConfigType::kChannelLutLen; i++) {
        colorLut[i].red = config->r_values[i];
        colorLut[i].green = config->g_values[i];
        colorLut[i].blue = config->b_values[i];
    }
    int ret = drm->CreatePropertyBlob(colorLut, sizeof(colorLut), &blobId);
    if (ret) {
        ALOGE("Failed to create gamma lut blob %d", ret);
        return ret;
    }
    return NO_ERROR;
}

int32_t ColorDrmBlobFactory::displayDither(
        const GsInterfaceType::IDqe::DqeControlData::ConfigType *config, DrmDevice *drm,
        uint32_t &blobId) {
    int ret = 0;
    if (config->disp_dither_override == false) {
        blobId = 0;
        return ret;
    }

    ret = drm->CreatePropertyBlob((void *)&config->disp_dither_reg, sizeof(config->disp_dither_reg),
                                  &blobId);
    if (ret) {
        ALOGE("Failed to create disp dither blob %d", ret);
        return ret;
    }

    return NO_ERROR;
}

} // namespace gs