1 /*
2 * Copyright 2019 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
17 #include <array>
18 #include <unordered_set>
19
20 #include <compositionengine/CompositionEngine.h>
21 #include <compositionengine/Display.h>
22 #include <compositionengine/DisplayColorProfileCreationArgs.h>
23 #include <compositionengine/RenderSurface.h>
24 #include <compositionengine/impl/DisplayColorProfile.h>
25 #include <compositionengine/impl/DumpHelpers.h>
26 #include <log/log.h>
27 #include <ui/DebugUtils.h>
28
29 #include "DisplayHardware/HWComposer.h"
30
31 namespace android::compositionengine {
32
33 DisplayColorProfile::~DisplayColorProfile() = default;
34
35 namespace impl {
36 namespace {
37
38 using ui::ColorMode;
39 using ui::Dataspace;
40 using ui::RenderIntent;
41
42 // ordered list of known SDR color modes
43 const std::array<ColorMode, 3> sSdrColorModes = {
44 ColorMode::DISPLAY_BT2020,
45 ColorMode::DISPLAY_P3,
46 ColorMode::SRGB,
47 };
48
49 // ordered list of known HDR color modes
50 const std::array<ColorMode, 2> sHdrColorModes = {
51 ColorMode::BT2100_PQ,
52 ColorMode::BT2100_HLG,
53 };
54
55 // ordered list of known SDR render intents
56 const std::array<RenderIntent, 2> sSdrRenderIntents = {
57 RenderIntent::ENHANCE,
58 RenderIntent::COLORIMETRIC,
59 };
60
61 // ordered list of known HDR render intents
62 const std::array<RenderIntent, 2> sHdrRenderIntents = {
63 RenderIntent::TONE_MAP_ENHANCE,
64 RenderIntent::TONE_MAP_COLORIMETRIC,
65 };
66
67 // Returns true if the given colorMode is considered an HDR color mode
isHdrColorMode(const ColorMode colorMode)68 bool isHdrColorMode(const ColorMode colorMode) {
69 return std::any_of(std::begin(sHdrColorModes), std::end(sHdrColorModes),
70 [colorMode](ColorMode hdrColorMode) { return hdrColorMode == colorMode; });
71 }
72
73 // map known color mode to dataspace
colorModeToDataspace(ColorMode mode)74 Dataspace colorModeToDataspace(ColorMode mode) {
75 switch (mode) {
76 case ColorMode::SRGB:
77 return Dataspace::V0_SRGB;
78 case ColorMode::DISPLAY_P3:
79 return Dataspace::DISPLAY_P3;
80 case ColorMode::DISPLAY_BT2020:
81 return Dataspace::DISPLAY_BT2020;
82 case ColorMode::BT2100_HLG:
83 return Dataspace::BT2020_HLG;
84 case ColorMode::BT2100_PQ:
85 return Dataspace::BT2020_PQ;
86 default:
87 return Dataspace::UNKNOWN;
88 }
89 }
90
91 // Return a list of candidate color modes.
getColorModeCandidates(ColorMode mode)92 std::vector<ColorMode> getColorModeCandidates(ColorMode mode) {
93 std::vector<ColorMode> candidates;
94
95 // add mode itself
96 candidates.push_back(mode);
97
98 // check if mode is HDR
99 bool isHdr = isHdrColorMode(mode);
100
101 // add other HDR candidates when mode is HDR
102 if (isHdr) {
103 for (auto hdrMode : sHdrColorModes) {
104 if (hdrMode != mode) {
105 candidates.push_back(hdrMode);
106 }
107 }
108 }
109
110 // add other SDR candidates
111 for (auto sdrMode : sSdrColorModes) {
112 if (sdrMode != mode) {
113 candidates.push_back(sdrMode);
114 }
115 }
116
117 return candidates;
118 }
119
120 // Return a list of candidate render intents.
getRenderIntentCandidates(RenderIntent intent)121 std::vector<RenderIntent> getRenderIntentCandidates(RenderIntent intent) {
122 std::vector<RenderIntent> candidates;
123
124 // add intent itself
125 candidates.push_back(intent);
126
127 // check if intent is HDR
128 bool isHdr = false;
129 for (auto hdrIntent : sHdrRenderIntents) {
130 if (hdrIntent == intent) {
131 isHdr = true;
132 break;
133 }
134 }
135
136 if (isHdr) {
137 // add other HDR candidates when intent is HDR
138 for (auto hdrIntent : sHdrRenderIntents) {
139 if (hdrIntent != intent) {
140 candidates.push_back(hdrIntent);
141 }
142 }
143 } else {
144 // add other SDR candidates when intent is SDR
145 for (auto sdrIntent : sSdrRenderIntents) {
146 if (sdrIntent != intent) {
147 candidates.push_back(sdrIntent);
148 }
149 }
150 }
151
152 return candidates;
153 }
154
155 // Return the best color mode supported by HWC.
getHwcColorMode(const std::unordered_map<ColorMode,std::vector<RenderIntent>> & hwcColorModes,ColorMode mode)156 ColorMode getHwcColorMode(
157 const std::unordered_map<ColorMode, std::vector<RenderIntent>>& hwcColorModes,
158 ColorMode mode) {
159 std::vector<ColorMode> candidates = getColorModeCandidates(mode);
160 for (auto candidate : candidates) {
161 auto iter = hwcColorModes.find(candidate);
162 if (iter != hwcColorModes.end()) {
163 return candidate;
164 }
165 }
166
167 return ColorMode::NATIVE;
168 }
169
170 // Return the best render intent supported by HWC.
getHwcRenderIntent(const std::vector<RenderIntent> & hwcIntents,RenderIntent intent)171 RenderIntent getHwcRenderIntent(const std::vector<RenderIntent>& hwcIntents, RenderIntent intent) {
172 std::vector<RenderIntent> candidates = getRenderIntentCandidates(intent);
173 for (auto candidate : candidates) {
174 for (auto hwcIntent : hwcIntents) {
175 if (candidate == hwcIntent) {
176 return candidate;
177 }
178 }
179 }
180
181 return RenderIntent::COLORIMETRIC;
182 }
183
184 } // anonymous namespace
185
createDisplayColorProfile(const DisplayColorProfileCreationArgs & args)186 std::unique_ptr<compositionengine::DisplayColorProfile> createDisplayColorProfile(
187 const DisplayColorProfileCreationArgs& args) {
188 return std::make_unique<DisplayColorProfile>(args);
189 }
190
DisplayColorProfile(const DisplayColorProfileCreationArgs & args)191 DisplayColorProfile::DisplayColorProfile(const DisplayColorProfileCreationArgs& args)
192 : mHasWideColorGamut(args.hasWideColorGamut),
193 mSupportedPerFrameMetadata(args.supportedPerFrameMetadata) {
194 populateColorModes(args.hwcColorModes);
195
196 std::vector<ui::Hdr> types = args.hdrCapabilities.getSupportedHdrTypes();
197 for (ui::Hdr hdrType : types) {
198 switch (hdrType) {
199 case ui::Hdr::HDR10_PLUS:
200 mHasHdr10Plus = true;
201 break;
202 case ui::Hdr::HDR10:
203 mHasHdr10 = true;
204 break;
205 case ui::Hdr::HLG:
206 mHasHLG = true;
207 break;
208 case ui::Hdr::DOLBY_VISION:
209 mHasDolbyVision = true;
210 break;
211 default:
212 ALOGE("UNKNOWN HDR capability: %d", static_cast<int32_t>(hdrType));
213 }
214 }
215
216 float minLuminance = args.hdrCapabilities.getDesiredMinLuminance();
217 float maxLuminance = args.hdrCapabilities.getDesiredMaxLuminance();
218 float maxAverageLuminance = args.hdrCapabilities.getDesiredMaxAverageLuminance();
219
220 minLuminance = minLuminance <= 0.0 ? sDefaultMinLumiance : minLuminance;
221 maxLuminance = maxLuminance <= 0.0 ? sDefaultMaxLumiance : maxLuminance;
222 maxAverageLuminance = maxAverageLuminance <= 0.0 ? sDefaultMaxLumiance : maxAverageLuminance;
223
224 mHdrCapabilities = HdrCapabilities(types, maxLuminance, maxAverageLuminance, minLuminance);
225 }
226
227 DisplayColorProfile::~DisplayColorProfile() = default;
228
isValid() const229 bool DisplayColorProfile::isValid() const {
230 return true;
231 }
232
hasWideColorGamut() const233 bool DisplayColorProfile::hasWideColorGamut() const {
234 return mHasWideColorGamut;
235 }
236
getSupportedPerFrameMetadata() const237 int32_t DisplayColorProfile::getSupportedPerFrameMetadata() const {
238 return mSupportedPerFrameMetadata;
239 }
240
hasHDR10PlusSupport() const241 bool DisplayColorProfile::hasHDR10PlusSupport() const {
242 return mHasHdr10Plus;
243 }
244
hasHDR10Support() const245 bool DisplayColorProfile::hasHDR10Support() const {
246 return mHasHdr10;
247 }
248
hasHLGSupport() const249 bool DisplayColorProfile::hasHLGSupport() const {
250 return mHasHLG;
251 }
252
hasDolbyVisionSupport() const253 bool DisplayColorProfile::hasDolbyVisionSupport() const {
254 return mHasDolbyVision;
255 }
256
getHdrCapabilities() const257 const HdrCapabilities& DisplayColorProfile::getHdrCapabilities() const {
258 return mHdrCapabilities;
259 }
260
populateColorModes(const DisplayColorProfileCreationArgs::HwcColorModes & hwcColorModes)261 void DisplayColorProfile::populateColorModes(
262 const DisplayColorProfileCreationArgs::HwcColorModes& hwcColorModes) {
263 // collect all known SDR render intents
264 std::unordered_set<RenderIntent> sdrRenderIntents(sSdrRenderIntents.begin(),
265 sSdrRenderIntents.end());
266 auto iter = hwcColorModes.find(ColorMode::SRGB);
267 if (iter != hwcColorModes.end()) {
268 for (auto intent : iter->second) {
269 sdrRenderIntents.insert(intent);
270 }
271 }
272
273 // add all known SDR combinations
274 for (auto intent : sdrRenderIntents) {
275 for (auto mode : sSdrColorModes) {
276 addColorMode(hwcColorModes, mode, intent);
277 }
278 }
279
280 // collect all known HDR render intents
281 std::unordered_set<RenderIntent> hdrRenderIntents(sHdrRenderIntents.begin(),
282 sHdrRenderIntents.end());
283 iter = hwcColorModes.find(ColorMode::BT2100_PQ);
284 if (iter != hwcColorModes.end()) {
285 for (auto intent : iter->second) {
286 hdrRenderIntents.insert(intent);
287 }
288 }
289
290 // add all known HDR combinations
291 for (auto intent : hdrRenderIntents) {
292 for (auto mode : sHdrColorModes) {
293 addColorMode(hwcColorModes, mode, intent);
294 }
295 }
296 }
297
298 // Map dataspace/intent to the best matched dataspace/colorMode/renderIntent
299 // supported by HWC.
addColorMode(const DisplayColorProfileCreationArgs::HwcColorModes & hwcColorModes,const ColorMode mode,const RenderIntent intent)300 void DisplayColorProfile::addColorMode(
301 const DisplayColorProfileCreationArgs::HwcColorModes& hwcColorModes, const ColorMode mode,
302 const RenderIntent intent) {
303 // find the best color mode
304 const ColorMode hwcColorMode = getHwcColorMode(hwcColorModes, mode);
305
306 // find the best render intent
307 auto iter = hwcColorModes.find(hwcColorMode);
308 const auto& hwcIntents =
309 iter != hwcColorModes.end() ? iter->second : std::vector<RenderIntent>();
310 const RenderIntent hwcIntent = getHwcRenderIntent(hwcIntents, intent);
311
312 const Dataspace dataspace = colorModeToDataspace(mode);
313 const Dataspace hwcDataspace = colorModeToDataspace(hwcColorMode);
314
315 ALOGV("DisplayColorProfile: map (%s, %s) to (%s, %s, %s)",
316 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
317 decodeRenderIntent(intent).c_str(),
318 dataspaceDetails(static_cast<android_dataspace_t>(hwcDataspace)).c_str(),
319 decodeColorMode(hwcColorMode).c_str(), decodeRenderIntent(hwcIntent).c_str());
320
321 mColorModes[getColorModeKey(dataspace, intent)] = {hwcDataspace, hwcColorMode, hwcIntent};
322 }
323
hasRenderIntent(RenderIntent intent) const324 bool DisplayColorProfile::hasRenderIntent(RenderIntent intent) const {
325 // assume a render intent is supported when SRGB supports it; we should
326 // get rid of that assumption.
327 auto iter = mColorModes.find(getColorModeKey(Dataspace::V0_SRGB, intent));
328 return iter != mColorModes.end() && iter->second.renderIntent == intent;
329 }
330
hasLegacyHdrSupport(Dataspace dataspace) const331 bool DisplayColorProfile::hasLegacyHdrSupport(Dataspace dataspace) const {
332 if ((dataspace == Dataspace::BT2020_PQ && hasHDR10Support()) ||
333 (dataspace == Dataspace::BT2020_HLG && hasHLGSupport())) {
334 auto iter =
335 mColorModes.find(getColorModeKey(dataspace, RenderIntent::TONE_MAP_COLORIMETRIC));
336 return iter == mColorModes.end() || iter->second.dataspace != dataspace;
337 }
338
339 return false;
340 }
341
getBestColorMode(Dataspace dataspace,RenderIntent intent,Dataspace * outDataspace,ColorMode * outMode,RenderIntent * outIntent) const342 void DisplayColorProfile::getBestColorMode(Dataspace dataspace, RenderIntent intent,
343 Dataspace* outDataspace, ColorMode* outMode,
344 RenderIntent* outIntent) const {
345 auto iter = mColorModes.find(getColorModeKey(dataspace, intent));
346 if (iter != mColorModes.end()) {
347 *outDataspace = iter->second.dataspace;
348 *outMode = iter->second.colorMode;
349 *outIntent = iter->second.renderIntent;
350 } else {
351 ALOGI("map unknown (%s)/(%s) to default color mode",
352 dataspaceDetails(static_cast<android_dataspace_t>(dataspace)).c_str(),
353 decodeRenderIntent(intent).c_str());
354 *outDataspace = Dataspace::UNKNOWN;
355 *outMode = ColorMode::NATIVE;
356 *outIntent = RenderIntent::COLORIMETRIC;
357 }
358 }
359
isDataspaceSupported(Dataspace dataspace) const360 bool DisplayColorProfile::isDataspaceSupported(Dataspace dataspace) const {
361 switch (dataspace) {
362 case Dataspace::BT2020_PQ:
363 case Dataspace::BT2020_ITU_PQ:
364 return hasHDR10Support();
365
366 case Dataspace::BT2020_HLG:
367 case Dataspace::BT2020_ITU_HLG:
368 return hasHLGSupport();
369
370 default:
371 return true;
372 }
373 }
374
dump(std::string & out) const375 void DisplayColorProfile::dump(std::string& out) const {
376 out.append(" Composition Display Color State:");
377
378 out.append("\n HWC Support: ");
379
380 dumpVal(out, "wideColorGamut", hasWideColorGamut());
381 dumpVal(out, "hdr10plus", hasHDR10PlusSupport());
382 dumpVal(out, "hdr10", hasHDR10Support());
383 dumpVal(out, "hlg", hasHLGSupport());
384 dumpVal(out, "dv", hasDolbyVisionSupport());
385 dumpVal(out, "metadata", getSupportedPerFrameMetadata());
386
387 out.append("\n Hdr Luminance Info:");
388 dumpVal(out, "desiredMinLuminance", mHdrCapabilities.getDesiredMinLuminance());
389 dumpVal(out, "desiredMaxLuminance", mHdrCapabilities.getDesiredMaxLuminance());
390 dumpVal(out, "desiredMaxAverageLuminance", mHdrCapabilities.getDesiredMaxAverageLuminance());
391 out.append("\n");
392 }
393
394 } // namespace impl
395 } // namespace android::compositionengine
396