summaryrefslogtreecommitdiffstats
path: root/exynos4/interfaces/livedisplay/1.0/src/Color.cpp
blob: dd54cac9a35f11cf96c31c980d9b2eaf0a4c9d88 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Copyright (C) 2017-2018 The LineageOS 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.
 */

// #define LOG_NDEBUG 0

#define LOG_TAG "LiveDisplay-HIDL"

#include "Color.h"

#include "ColorBackend.h"
#include "Exynos4.h"

#include <android-base/logging.h>

namespace {

using vendor::lineage::livedisplay::V1_0::DisplayMode;
using vendor::lineage::livedisplay::V1_0::implementation::disp_mode;

DisplayMode modePointerToObj(android::sp<disp_mode> mode) {
    DisplayMode m;
    m.id = mode->id;
    m.name = mode->name;
    return m;
}

DisplayMode invalidDisplayMode() {
    DisplayMode mode;
    mode.id = -1;
    return mode;
}
}  // anonymous namespace

namespace vendor {
namespace lineage {
namespace livedisplay {
namespace V1_0 {
namespace implementation {

using ::android::Mutex;
using ::android::NO_INIT;
using ::android::OK;
using ::android::sp;
using ::android::status_t;

Color::Color() : mConnected(false), mBackend(nullptr) {
}

Color::~Color() {
    reset();
}

void Color::reset() {
    if (mConnected) {
        mBackend = nullptr;
    }
    mFeatures = 0;
    mConnected = false;
}

bool Color::check(Feature f) {
    return connect() && (mFeatures & (uint32_t)f);
}

void Color::error(const char* msg) {
    if (msg != NULL) {
        LOG(ERROR) << msg;
    }

    reset();
}

bool Color::connect() {
    if (mConnected) {
        return true;
    }

    mFeatures = 0;

    mBackend.reset(new Exynos4());
    if (mBackend == nullptr) {
        LOG(ERROR) << "Failed to initialize backend!";
        return false;
    }

    for (uint32_t i = 1; i <= (uint32_t)Feature::MAX; i <<= 1) {
        Feature f = static_cast<Feature>(i);
        if (mBackend->hasFeature(f)) {
            addFeature(f);
        }
    }
    mConnected = true;

    return mFeatures > 0;
}

Return<Features> Color::getSupportedFeatures() {
    connect();
    return mFeatures;
}

Return<void> Color::getDisplayModes(getDisplayModes_cb _hidl_cb) {
    hidl_vec<DisplayMode> profiles;
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::DISPLAY_MODES)) {
        std::vector<sp<disp_mode>> spProfiles;
        rc = mBackend->getDisplayModes(spProfiles);
        if (rc != OK) {
            error("Unable to fetch display modes!");
        } else {
            profiles.resize(spProfiles.size());
            for (size_t i = 0; i < spProfiles.size(); i++) {
                profiles[i].id = spProfiles[i]->id;
                profiles[i].name = spProfiles[i]->name;
            }
        }
    }

    _hidl_cb(profiles);
    return Void();
}

Return<void> Color::getCurrentDisplayMode(getCurrentDisplayMode_cb _hidl_cb) {
    DisplayMode mode;
    Mutex::Autolock _l(mLock);

    if (check(Feature::DISPLAY_MODES)) {
        sp<disp_mode> m = mBackend->getCurrentDisplayMode();
        if (m != nullptr) {
            mode = modePointerToObj(m);
        } else {
            mode = invalidDisplayMode();
        }
    }
    _hidl_cb(mode);
    return Void();
}

Return<void> Color::getDefaultDisplayMode(getDefaultDisplayMode_cb _hidl_cb) {
    DisplayMode mode;
    Mutex::Autolock _l(mLock);

    if (check(Feature::DISPLAY_MODES)) {
        sp<disp_mode> m = mBackend->getDefaultDisplayMode();
        if (m != nullptr) {
            mode = modePointerToObj(m);
        } else {
            mode = invalidDisplayMode();
        }
    }
    _hidl_cb(mode);
    return Void();
}

Return<bool> Color::setDisplayMode(int32_t modeID, bool makeDefault) {
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::DISPLAY_MODES)) {
        rc = mBackend->setDisplayMode(modeID, makeDefault);
        if (rc != OK) {
            error("Unable to set display mode!");
        }
    }
    return rc == OK;
}

Return<bool> Color::setAdaptiveBacklightEnabled(bool enabled) {
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::ADAPTIVE_BACKLIGHT)) {
        rc = mBackend->setAdaptiveBacklightEnabled(enabled);
        if (rc != OK) {
            error("Unable to set adaptive backlight state!");
        }
    }
    return rc == OK;
}

Return<bool> Color::isAdaptiveBacklightEnabled() {
    Mutex::Autolock _l(mLock);

    if (check(Feature::ADAPTIVE_BACKLIGHT)) {
        return mBackend->isAdaptiveBacklightEnabled();
    }
    return false;
}

Return<bool> Color::setOutdoorModeEnabled(bool enabled) {
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::OUTDOOR_MODE)) {
        rc = mBackend->setOutdoorModeEnabled(enabled);
        if (rc != OK) {
            error("Unable to toggle outdoor mode!");
        }
    }
    return rc == OK;
}

Return<bool> Color::isOutdoorModeEnabled() {
    Mutex::Autolock _l(mLock);

    if (check(Feature::OUTDOOR_MODE)) {
        return mBackend->isOutdoorModeEnabled();
    }
    return false;
}

Return<void> Color::getColorBalanceRange(getColorBalanceRange_cb _hidl_cb) {
    Range range;
    Mutex::Autolock _l(mLock);

    if (check(Feature::COLOR_BALANCE)) {
        status_t rc = mBackend->getColorBalanceRange(range);
        if (rc != OK) {
            error("Unable to fetch color balance range!");
            range.max = range.min = 0;
        }
    }

    _hidl_cb(range);
    return Void();
}

Return<int32_t> Color::getColorBalance() {
    Mutex::Autolock _l(mLock);

    if (check(Feature::COLOR_BALANCE)) {
        return mBackend->getColorBalance();
    }

    return 0;
}

Return<bool> Color::setColorBalance(int32_t value) {
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::COLOR_BALANCE)) {
        rc = mBackend->setColorBalance(value);
        if (rc != OK) {
            error("Unable to set color balance!");
        }
    }
    return rc == OK;
}

Return<bool> Color::setPictureAdjustment(const HSIC& hsic) {
    status_t rc = NO_INIT;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        rc = mBackend->setPictureAdjustment(hsic);
        if (rc != OK) {
            error("Unable to set picture adjustment!");
        }
    }
    return rc == OK;
}

Return<void> Color::getPictureAdjustment(getPictureAdjustment_cb _hidl_cb) {
    HSIC hsic;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustment(hsic);
        if (rc != OK) {
            error("Unable to get picture adjustment!");
        }
    }
    _hidl_cb(hsic);
    return Void();
}

Return<void> Color::getDefaultPictureAdjustment(getDefaultPictureAdjustment_cb _hidl_cb) {
    HSIC hsic;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        hsic = mBackend->getDefaultPictureAdjustment();
    }
    _hidl_cb(hsic);
    return Void();
}

Return<void> Color::getHueRange(getHueRange_cb _hidl_cb) {
    HSICRanges ranges;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustmentRanges(ranges);
        if (rc != OK) {
            error("Unable to get hue range!");
        }
    }
    _hidl_cb(ranges.hue);
    return Void();
}

Return<void> Color::getSaturationRange(getSaturationRange_cb _hidl_cb) {
    HSICRanges ranges;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustmentRanges(ranges);
        if (rc != OK) {
            error("Unable to get saturation range!");
        }
    }
    _hidl_cb(ranges.saturation);
    return Void();
}

Return<void> Color::getIntensityRange(getIntensityRange_cb _hidl_cb) {
    HSICRanges ranges;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustmentRanges(ranges);
        if (rc != OK) {
            error("Unable to get intensity range!");
        }
    }
    _hidl_cb(ranges.intensity);
    return Void();
}

Return<void> Color::getContrastRange(getContrastRange_cb _hidl_cb) {
    HSICRanges ranges;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustmentRanges(ranges);
        if (rc != OK) {
            error("Unable to get contrast range!");
        }
    }
    _hidl_cb(ranges.contrast);
    return Void();
}

Return<void> Color::getSaturationThresholdRange(getSaturationThresholdRange_cb _hidl_cb) {
    HSICRanges ranges;
    Mutex::Autolock _l(mLock);

    if (check(Feature::PICTURE_ADJUSTMENT)) {
        status_t rc = mBackend->getPictureAdjustmentRanges(ranges);
        if (rc != OK) {
            error("Unable to get saturation threshold range!");
        }
    }
    _hidl_cb(ranges.saturationThreshold);
    return Void();
}

}  // namespace implementation
}  // namespace V1_0
}  // namespace livedisplay
}  // namespace lineage
}  // namespace vendor