Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 "ScreenCaptureOutput.h" |
| 18 | #include "ScreenCaptureRenderSurface.h" |
Melody Hsu | 70a63e5 | 2023-11-17 19:49:12 +0000 | [diff] [blame] | 19 | #include "ui/Rotation.h" |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 20 | |
| 21 | #include <compositionengine/CompositionEngine.h> |
| 22 | #include <compositionengine/DisplayColorProfileCreationArgs.h> |
| 23 | #include <compositionengine/impl/DisplayColorProfile.h> |
| 24 | #include <ui/Rotation.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
Patrick Williams | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame] | 28 | std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutputArgs args) { |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 29 | std::shared_ptr<ScreenCaptureOutput> output = compositionengine::impl::createOutputTemplated< |
Patrick Williams | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame] | 30 | ScreenCaptureOutput, compositionengine::CompositionEngine, const RenderArea&, |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 31 | const compositionengine::Output::ColorProfile&, |
| 32 | bool>(args.compositionEngine, args.renderArea, args.colorProfile, args.regionSampling, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 33 | args.dimInGammaSpaceForEnhancedScreenshots, args.enableLocalTonemapping); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 34 | output->editState().isSecure = args.renderArea.isSecure(); |
Chavi Weingarten | 18fa7c6 | 2023-11-28 21:16:03 +0000 | [diff] [blame] | 35 | output->editState().isProtected = args.isProtected; |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 36 | output->setCompositionEnabled(true); |
| 37 | output->setLayerFilter({args.layerStack}); |
| 38 | output->setRenderSurface(std::make_unique<ScreenCaptureRenderSurface>(std::move(args.buffer))); |
| 39 | output->setDisplayBrightness(args.sdrWhitePointNits, args.displayBrightnessNits); |
Alec Mouri | 3e5965f | 2023-04-07 18:00:58 +0000 | [diff] [blame] | 40 | output->editState().clientTargetBrightness = args.targetBrightness; |
Alec Mouri | 7dae9c1 | 2023-07-13 19:40:53 +0000 | [diff] [blame] | 41 | output->editState().treat170mAsSrgb = args.treat170mAsSrgb; |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 42 | |
| 43 | output->setDisplayColorProfile(std::make_unique<compositionengine::impl::DisplayColorProfile>( |
| 44 | compositionengine::DisplayColorProfileCreationArgsBuilder() |
| 45 | .setHasWideColorGamut(true) |
| 46 | .Build())); |
| 47 | |
Patrick Williams | 278a88f | 2023-01-27 16:52:40 -0600 | [diff] [blame] | 48 | const Rect& sourceCrop = args.renderArea.getSourceCrop(); |
Melody Hsu | 70a63e5 | 2023-11-17 19:49:12 +0000 | [diff] [blame] | 49 | const ui::Rotation orientation = ui::ROTATION_0; |
| 50 | output->setDisplaySize({sourceCrop.getWidth(), sourceCrop.getHeight()}); |
Patrick Williams | 1c8d842 | 2023-07-06 11:23:17 -0500 | [diff] [blame] | 51 | output->setProjection(orientation, sourceCrop, |
Melody Hsu | 70a63e5 | 2023-11-17 19:49:12 +0000 | [diff] [blame] | 52 | {args.renderArea.getReqWidth(), args.renderArea.getReqHeight()}); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 53 | |
Leon Scroggins III | c03d465 | 2023-01-05 13:03:53 -0500 | [diff] [blame] | 54 | { |
| 55 | std::string name = args.regionSampling ? "RegionSampling" : "ScreenCaptureOutput"; |
| 56 | if (auto displayDevice = args.renderArea.getDisplayDevice()) { |
| 57 | base::StringAppendF(&name, " for %" PRIu64, displayDevice->getId().value); |
| 58 | } |
| 59 | output->setName(name); |
| 60 | } |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 61 | return output; |
| 62 | } |
| 63 | |
Patrick Williams | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame] | 64 | ScreenCaptureOutput::ScreenCaptureOutput( |
Patrick Williams | 278a88f | 2023-01-27 16:52:40 -0600 | [diff] [blame] | 65 | const RenderArea& renderArea, const compositionengine::Output::ColorProfile& colorProfile, |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 66 | bool regionSampling, bool dimInGammaSpaceForEnhancedScreenshots, |
| 67 | bool enableLocalTonemapping) |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 68 | : mRenderArea(renderArea), |
| 69 | mColorProfile(colorProfile), |
| 70 | mRegionSampling(regionSampling), |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 71 | mDimInGammaSpaceForEnhancedScreenshots(dimInGammaSpaceForEnhancedScreenshots), |
| 72 | mEnableLocalTonemapping(enableLocalTonemapping) {} |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 73 | |
| 74 | void ScreenCaptureOutput::updateColorProfile(const compositionengine::CompositionRefreshArgs&) { |
| 75 | auto& outputState = editState(); |
| 76 | outputState.dataspace = mColorProfile.dataspace; |
| 77 | outputState.renderIntent = mColorProfile.renderIntent; |
| 78 | } |
| 79 | |
Alec Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 80 | renderengine::DisplaySettings ScreenCaptureOutput::generateClientCompositionDisplaySettings( |
| 81 | const std::shared_ptr<renderengine::ExternalTexture>& buffer) const { |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 82 | auto clientCompositionDisplay = |
Alec Mouri | f97df4d | 2023-09-06 02:10:05 +0000 | [diff] [blame] | 83 | compositionengine::impl::Output::generateClientCompositionDisplaySettings(buffer); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 84 | clientCompositionDisplay.clip = mRenderArea.getSourceCrop(); |
Alec Mouri | 9af38ba | 2023-08-16 22:41:14 +0000 | [diff] [blame] | 85 | |
| 86 | auto renderIntent = static_cast<ui::RenderIntent>(clientCompositionDisplay.renderIntent); |
| 87 | if (mDimInGammaSpaceForEnhancedScreenshots && renderIntent != ui::RenderIntent::COLORIMETRIC && |
| 88 | renderIntent != ui::RenderIntent::TONE_MAP_COLORIMETRIC) { |
| 89 | clientCompositionDisplay.dimmingStage = |
| 90 | aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF; |
| 91 | } |
| 92 | |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 93 | if (mEnableLocalTonemapping) { |
| 94 | clientCompositionDisplay.tonemapStrategy = |
| 95 | renderengine::DisplaySettings::TonemapStrategy::Local; |
Alec Mouri | 1b1853f | 2024-07-15 22:46:58 +0000 | [diff] [blame] | 96 | if (static_cast<ui::PixelFormat>(buffer->getPixelFormat()) == ui::PixelFormat::RGBA_FP16) { |
| 97 | clientCompositionDisplay.targetHdrSdrRatio = |
| 98 | getState().displayBrightnessNits / getState().sdrWhitePointNits; |
| 99 | } else { |
| 100 | clientCompositionDisplay.targetHdrSdrRatio = 1.f; |
| 101 | } |
Alec Mouri | a7e752e | 2024-05-24 18:21:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 104 | return clientCompositionDisplay; |
| 105 | } |
| 106 | |
| 107 | std::vector<compositionengine::LayerFE::LayerSettings> |
| 108 | ScreenCaptureOutput::generateClientCompositionRequests( |
| 109 | bool supportsProtectedContent, ui::Dataspace outputDataspace, |
| 110 | std::vector<compositionengine::LayerFE*>& outLayerFEs) { |
| 111 | auto clientCompositionLayers = compositionengine::impl::Output:: |
| 112 | generateClientCompositionRequests(supportsProtectedContent, outputDataspace, |
| 113 | outLayerFEs); |
| 114 | |
| 115 | if (mRegionSampling) { |
| 116 | for (auto& layer : clientCompositionLayers) { |
| 117 | layer.backgroundBlurRadius = 0; |
| 118 | layer.blurRegions.clear(); |
| 119 | } |
| 120 | } |
| 121 | |
Alec Mouri | 792b150 | 2023-05-02 00:03:08 +0000 | [diff] [blame] | 122 | if (outputDataspace == ui::Dataspace::BT2020_HLG) { |
| 123 | for (auto& layer : clientCompositionLayers) { |
| 124 | auto transfer = layer.sourceDataspace & ui::Dataspace::TRANSFER_MASK; |
| 125 | if (transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_HLG) && |
| 126 | transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_ST2084)) { |
| 127 | layer.whitePointNits *= (1000.0f / 203.0f); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 132 | Rect sourceCrop = mRenderArea.getSourceCrop(); |
| 133 | compositionengine::LayerFE::LayerSettings fillLayer; |
| 134 | fillLayer.source.buffer.buffer = nullptr; |
| 135 | fillLayer.source.solidColor = half3(0.0f, 0.0f, 0.0f); |
| 136 | fillLayer.geometry.boundaries = |
| 137 | FloatRect(static_cast<float>(sourceCrop.left), static_cast<float>(sourceCrop.top), |
| 138 | static_cast<float>(sourceCrop.right), static_cast<float>(sourceCrop.bottom)); |
| 139 | fillLayer.alpha = half(RenderArea::getCaptureFillValue(mRenderArea.getCaptureFill())); |
| 140 | clientCompositionLayers.insert(clientCompositionLayers.begin(), fillLayer); |
| 141 | |
| 142 | return clientCompositionLayers; |
| 143 | } |
| 144 | |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 145 | } // namespace android |