xref: /aosp_15_r20/frameworks/base/native/android/display_luts.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #define LOG_TAG "DisplayLuts"
17 
18 #include <android/display_luts.h>
19 #include <display_luts_private.h>
20 #include <utils/Log.h>
21 
22 #include <cmath>
23 
24 #define ADISPLAYLUTS_BUFFER_LENGTH_LIMIT (100000)
25 
26 #define CHECK_NOT_NULL(name) \
27     LOG_ALWAYS_FATAL_IF(name == nullptr, "nullptr passed as " #name " argument");
28 
ADisplayLutsEntry_createEntry(float * buffer,int32_t length,ADisplayLuts_Dimension dimension,ADisplayLuts_SamplingKey key)29 ADisplayLutsEntry* ADisplayLutsEntry_createEntry(float* buffer, int32_t length,
30                                                  ADisplayLuts_Dimension dimension,
31                                                  ADisplayLuts_SamplingKey key) {
32     CHECK_NOT_NULL(buffer);
33     LOG_ALWAYS_FATAL_IF(length >= ADISPLAYLUTS_BUFFER_LENGTH_LIMIT,
34                         "the lut raw buffer length is too big to handle");
35     if (dimension != ADISPLAYLUTS_ONE_DIMENSION && dimension != ADISPLAYLUTS_THREE_DIMENSION) {
36         LOG_ALWAYS_FATAL("the lut dimension is be either 1 or 3");
37     }
38     int32_t size = 0;
39     if (dimension == ADISPLAYLUTS_THREE_DIMENSION) {
40         LOG_ALWAYS_FATAL_IF(length % 3 != 0, "the 3d lut raw buffer is not divisible by 3");
41         int32_t lengthPerChannel = length / 3;
42         float sizeForDim = std::cbrt(static_cast<float>(lengthPerChannel));
43         LOG_ALWAYS_FATAL_IF(sizeForDim != (int)(sizeForDim),
44                             "the 3d lut buffer length is incorrect");
45         size = (int)sizeForDim;
46     } else {
47         size = length;
48     }
49     LOG_ALWAYS_FATAL_IF(size < 2, "the lut size for each dimension is too small");
50 
51     ADisplayLutsEntry* entry = new ADisplayLutsEntry();
52     entry->buffer.data.resize(length);
53     std::copy(buffer, buffer + length, entry->buffer.data.begin());
54     entry->properties = {dimension, size, key};
55 
56     entry->incStrong((void*)ADisplayLutsEntry_createEntry);
57     return static_cast<ADisplayLutsEntry*>(entry);
58 }
59 
ADisplayLutsEntry_destroy(ADisplayLutsEntry * entry)60 void ADisplayLutsEntry_destroy(ADisplayLutsEntry* entry) {
61     if (entry != NULL) {
62         entry->decStrong((void*)ADisplayLutsEntry_createEntry);
63     }
64 }
65 
ADisplayLutsEntry_getDimension(const ADisplayLutsEntry * entry)66 ADisplayLuts_Dimension ADisplayLutsEntry_getDimension(const ADisplayLutsEntry* entry) {
67     CHECK_NOT_NULL(entry);
68     return entry->properties.dimension;
69 }
70 
ADisplayLutsEntry_getSize(const ADisplayLutsEntry * entry)71 int32_t ADisplayLutsEntry_getSize(const ADisplayLutsEntry* entry) {
72     CHECK_NOT_NULL(entry);
73     return entry->properties.size;
74 }
75 
ADisplayLutsEntry_getSamplingKey(const ADisplayLutsEntry * entry)76 ADisplayLuts_SamplingKey ADisplayLutsEntry_getSamplingKey(const ADisplayLutsEntry* entry) {
77     CHECK_NOT_NULL(entry);
78     return entry->properties.samplingKey;
79 }
80 
ADisplayLutsEntry_getBuffer(const ADisplayLutsEntry * _Nonnull entry)81 const float* ADisplayLutsEntry_getBuffer(const ADisplayLutsEntry* _Nonnull entry) {
82     CHECK_NOT_NULL(entry);
83     return entry->buffer.data.data();
84 }
85 
ADisplayLuts_create()86 ADisplayLuts* ADisplayLuts_create() {
87     ADisplayLuts* luts = new ADisplayLuts();
88     if (luts == NULL) {
89         delete luts;
90         return NULL;
91     }
92     luts->incStrong((void*)ADisplayLuts_create);
93     return static_cast<ADisplayLuts*>(luts);
94 }
95 
ADisplayLuts_clearLuts(ADisplayLuts * luts)96 void ADisplayLuts_clearLuts(ADisplayLuts* luts) {
97     for (auto& entry : luts->entries) {
98         entry->decStrong((void*)ADisplayLuts_setEntries); // Decrement ref count
99     }
100     luts->entries.clear();
101     luts->offsets.clear();
102     luts->totalBufferSize = 0;
103 }
104 
ADisplayLuts_destroy(ADisplayLuts * luts)105 void ADisplayLuts_destroy(ADisplayLuts* luts) {
106     if (luts != NULL) {
107         ADisplayLuts_clearLuts(luts);
108         luts->decStrong((void*)ADisplayLuts_create);
109     }
110 }
111 
ADisplayLuts_setEntries(ADisplayLuts * luts,ADisplayLutsEntry ** entries,int32_t numEntries)112 void ADisplayLuts_setEntries(ADisplayLuts* luts, ADisplayLutsEntry** entries, int32_t numEntries) {
113     CHECK_NOT_NULL(luts);
114     // always clear the previously set lut(s)
115     ADisplayLuts_clearLuts(luts);
116 
117     // do nothing
118     if (!entries || numEntries == 0) {
119         return;
120     }
121 
122     LOG_ALWAYS_FATAL_IF(numEntries > 2, "The number of entries should be not over 2!");
123     if (numEntries == 2 && entries[0]->properties.dimension != ADISPLAYLUTS_ONE_DIMENSION &&
124         entries[1]->properties.dimension != ADISPLAYLUTS_THREE_DIMENSION) {
125         LOG_ALWAYS_FATAL("The entries should be 1D and 3D in order!");
126     }
127 
128     luts->offsets.reserve(numEntries);
129     luts->entries.reserve(numEntries);
130     for (int32_t i = 0; i < numEntries; i++) {
131         luts->offsets.emplace_back(luts->totalBufferSize);
132         luts->totalBufferSize += entries[i]->buffer.data.size();
133         luts->entries.emplace_back(entries[i]);
134         luts->entries.back()->incStrong((void*)ADisplayLuts_setEntries);
135     }
136 }