summaryrefslogtreecommitdiff
path: root/libhwc2.1/libdisplayinterface/ExynosDisplayDrmInterfaceModule.cpp
blob: 6e9cd7b56dd332531bd966ea56372c050c43f549 (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
/*
 * Copyright (C) 2021 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 <hardware/exynos/ion.h>
#include "ExynosDisplayDrmInterfaceModule.h"
#include "ExynosPrimaryDisplayModule.h"

using namespace gs201;

/////////////////////////////////////////////////// ExynosDisplayDrmInterfaceModule //////////////////////////////////////////////////////////////////
ExynosDisplayDrmInterfaceModule::ExynosDisplayDrmInterfaceModule(ExynosDisplay *exynosDisplay)
  : gs101::ExynosDisplayDrmInterfaceModule(exynosDisplay)
{
}

ExynosDisplayDrmInterfaceModule::~ExynosDisplayDrmInterfaceModule()
{
    for (auto p: mCGCDataInfos) {
        if (p.second != nullptr)
            munmap(p.second, sizeCgcDmaLut);

        if (p.first > 0)
            close(p.first);
    }
}

int32_t ExynosDisplayDrmInterfaceModule::initDrmDevice(DrmDevice *drmDevice)
{
    int ret = gs101::ExynosDisplayDrmInterfaceModule::initDrmDevice(drmDevice);
    if (ret != NO_ERROR)
        return ret;

    /* create file descriptors for CGC DMA */
    int32_t fd;
    struct cgc_dma_lut *buf;

    int ionFd = exynos_ion_open();
    if (ionFd >= 0) {
        while (mCGCDataInfos.size() < sizeCgCDataInfo) {
            fd = exynos_ion_alloc(ionFd, sizeCgcDmaLut, EXYNOS_ION_HEAP_SYSTEM_MASK, 0);
            if (fd >= 0) {
                buf = (struct cgc_dma_lut *)mmap(0, sizeCgcDmaLut, PROT_READ | PROT_WRITE,
                                                 MAP_SHARED, fd, 0);
                if (buf == nullptr) {
                    ALOGE("Failed to map buffer for CGC_DMA LUT");
                    close(fd);
                    ret = -ENOMEM;
                    break;
                } else {
                    memset(buf, 0, sizeCgcDmaLut);
                    mCGCDataInfos.emplace_back(CGCDataInfo(fd, buf));
                }
            } else {
                ALOGE("Failed to allocate ION for CGC_DMA LUT");
                ret = -ENOMEM;
                break;
            }
        }

        exynos_ion_close(ionFd);
    } else
        ALOGE("Failed to open ION for CGC_DMA LUT");

    return ret;
}

int32_t ExynosDisplayDrmInterfaceModule::createCgcDMAFromIDqe(
        const IDisplayColorGS101::IDqe::CgcData &cgcData)
{
    if ((cgcData.config->r_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT) ||
        (cgcData.config->g_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT) ||
        (cgcData.config->b_values.size() != DRM_SAMSUNG_CGC_LUT_REG_CNT)) {
        ALOGE("CGC data size is not same (r: %zu, g: %zu: b: %zu)",
                cgcData.config->r_values.size(),
                cgcData.config->g_values.size(),
                cgcData.config->b_values.size());
        return -EINVAL;
    }

    if (iCGCDataInfo >= mCGCDataInfos.size()) {
        ALOGE("CGC Data Infos is empty");
        return -EINVAL;
    }

    struct cgc_dma_lut *buf = mCGCDataInfos.at(iCGCDataInfo).second;
    uint32_t i = 0;
    for (; i < (DRM_SAMSUNG_CGC_LUT_REG_CNT - 1); i++) {
        buf[i * 2].r_value = (uint16_t)(cgcData.config->r_values[i]);
        buf[i * 2].g_value = (uint16_t)(cgcData.config->g_values[i]);
        buf[i * 2].b_value = (uint16_t)(cgcData.config->b_values[i]);
        buf[i * 2 + 1].r_value = (uint16_t)(cgcData.config->r_values[i] >> 16);
        buf[i * 2 + 1].g_value = (uint16_t)(cgcData.config->g_values[i] >> 16);
        buf[i * 2 + 1].b_value = (uint16_t)(cgcData.config->b_values[i] >> 16);
    }
    buf[i * 2].r_value = (uint16_t)cgcData.config->r_values[i];
    buf[i * 2].g_value = (uint16_t)cgcData.config->g_values[i];
    buf[i * 2].b_value = (uint16_t)cgcData.config->b_values[i];

    return mCGCDataInfos.at(iCGCDataInfo).first;
}

int32_t ExynosDisplayDrmInterfaceModule::setCgcLutDmaProperty(
        const DrmProperty &prop,
        ExynosDisplayDrmInterface::DrmModeAtomicReq &drmReq)
{
    if (!prop.id())
        return NO_ERROR;

    ExynosPrimaryDisplayModule* display = (ExynosPrimaryDisplayModule*)mExynosDisplay;
    const IDisplayColorGS101::IDqe &dqe = display->getDqe();
    const IDisplayColorGS101::IDqe::CgcData &cgcData = dqe.Cgc();

    /* dirty bit is valid only if enable is true */
    if (!mForceDisplayColorSetting && cgcData.enable && !cgcData.dirty)
        return NO_ERROR;

    int32_t ret = 0;
    int32_t cgcLutFd = disabledCgc;

    if (cgcData.enable) {
        if (cgcData.config == nullptr) {
            ALOGE("no CGC config");
            return NO_ERROR;
        }

        cgcLutFd = createCgcDMAFromIDqe(cgcData);
        if (cgcLutFd < 0) {
            HWC_LOGE(mExynosDisplay, "%s: create CGC DMA FD fail", __func__);
            return cgcLutFd;
        }

        iCGCDataInfo = (iCGCDataInfo + 1) % sizeCgCDataInfo;
    }

    /* CGC Disabled information should not be delivered at every frame */
    if (cgcLutFd == disabledCgc && !mCgcEnabled)
        return NO_ERROR;

    /* CGC setting when cgc is enabled and dirty */
    if ((ret = drmReq.atomicAddProperty(mDrmCrtc->id(), prop, cgcLutFd, true)) < 0) {
        HWC_LOGE(mExynosDisplay, "%s: Fail to set cgc_dma_fd property", __func__);
        return ret;
    }
    dqe.Cgc().NotifyDataApplied();

    mCgcEnabled = (cgcLutFd != disabledCgc);

    return NO_ERROR;
}

int32_t ExynosDisplayDrmInterfaceModule::setDisplayColorSetting(
        ExynosDisplayDrmInterface::DrmModeAtomicReq &drmReq)
{
    if (isPrimary() == false)
        return NO_ERROR;
    if (!mForceDisplayColorSetting && !mColorSettingChanged)
        return NO_ERROR;

    int32_t ret = gs101::ExynosDisplayDrmInterfaceModule::setDisplayColorSetting(drmReq);
    if (ret != NO_ERROR)
        return ret;

    return setCgcLutDmaProperty(mDrmCrtc->cgc_lut_fd_property(), drmReq);
}

int32_t ExynosDisplayDrmInterfaceModule::setHistoPosProperty(
    const DrmProperty &prop,
    ExynosDisplayDrmInterface::DrmModeAtomicReq &drmReq) {
  if (!prop.id()) return NO_ERROR;

  int32_t ret = 0;

  if ((ret = drmReq.atomicAddProperty(
           mDrmCrtc->id(), prop, (uint64_t)mHistogramInfo->getHistogramPos(),
           true)) < 0) {
    HWC_LOGE(mExynosDisplay, "%s: Fail to set histogram position property",
             __func__);
    return ret;
  }

  return NO_ERROR;
}

int32_t ExynosDisplayDrmInterfaceModule::setDisplayHistogramSetting(
    ExynosDisplayDrmInterface::DrmModeAtomicReq &drmReq) {
  if ((mHistogramInfoRegistered == false) || (isPrimary() == false))
    return NO_ERROR;

  int32_t ret =
      gs101::ExynosDisplayDrmInterfaceModule::setDisplayHistogramSetting(
          drmReq);
  if (ret != NO_ERROR) return ret;

  ret = setHistoPosProperty(mDrmCrtc->histogram_position_property(), drmReq);

  return ret;
}

void ExynosDisplayDrmInterfaceModule::registerHistogramInfo(
    IDLHistogram *info) {
  gs101::ExynosDisplayDrmInterfaceModule::registerHistogramInfo(info);
  mHistogramInfo.reset(info);
}

//////////////////////////////////////////////////// ExynosPrimaryDisplayDrmInterfaceModule //////////////////////////////////////////////////////////////////
ExynosPrimaryDisplayDrmInterfaceModule::ExynosPrimaryDisplayDrmInterfaceModule(ExynosDisplay *exynosDisplay)
  : ExynosDisplayDrmInterfaceModule(exynosDisplay)
{
}

ExynosPrimaryDisplayDrmInterfaceModule::~ExynosPrimaryDisplayDrmInterfaceModule()
{
}