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