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 #include "ui/Rotation.h"
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 
createScreenCaptureOutput(ScreenCaptureOutputArgs args)28 std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutputArgs args) {
29     std::shared_ptr<ScreenCaptureOutput> output = compositionengine::impl::createOutputTemplated<
30             ScreenCaptureOutput, compositionengine::CompositionEngine, const RenderArea&,
31             const compositionengine::Output::ColorProfile&,
32             bool>(args.compositionEngine, args.renderArea, args.colorProfile, args.regionSampling,
33                   args.dimInGammaSpaceForEnhancedScreenshots, args.enableLocalTonemapping);
34     output->editState().isSecure = args.renderArea.isSecure();
35     output->editState().isProtected = args.isProtected;
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);
40     output->editState().clientTargetBrightness = args.targetBrightness;
41     output->editState().treat170mAsSrgb = args.treat170mAsSrgb;
42 
43     output->setDisplayColorProfile(std::make_unique<compositionengine::impl::DisplayColorProfile>(
44             compositionengine::DisplayColorProfileCreationArgsBuilder()
45                     .setHasWideColorGamut(true)
46                     .Build()));
47 
48     const Rect& sourceCrop = args.renderArea.getSourceCrop();
49     const ui::Rotation orientation = ui::ROTATION_0;
50     output->setDisplaySize({sourceCrop.getWidth(), sourceCrop.getHeight()});
51     output->setProjection(orientation, sourceCrop,
52                           {args.renderArea.getReqWidth(), args.renderArea.getReqHeight()});
53 
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     }
61     return output;
62 }
63 
ScreenCaptureOutput(const RenderArea & renderArea,const compositionengine::Output::ColorProfile & colorProfile,bool regionSampling,bool dimInGammaSpaceForEnhancedScreenshots,bool enableLocalTonemapping)64 ScreenCaptureOutput::ScreenCaptureOutput(
65         const RenderArea& renderArea, const compositionengine::Output::ColorProfile& colorProfile,
66         bool regionSampling, bool dimInGammaSpaceForEnhancedScreenshots,
67         bool enableLocalTonemapping)
68       : mRenderArea(renderArea),
69         mColorProfile(colorProfile),
70         mRegionSampling(regionSampling),
71         mDimInGammaSpaceForEnhancedScreenshots(dimInGammaSpaceForEnhancedScreenshots),
72         mEnableLocalTonemapping(enableLocalTonemapping) {}
73 
updateColorProfile(const compositionengine::CompositionRefreshArgs &)74 void ScreenCaptureOutput::updateColorProfile(const compositionengine::CompositionRefreshArgs&) {
75     auto& outputState = editState();
76     outputState.dataspace = mColorProfile.dataspace;
77     outputState.renderIntent = mColorProfile.renderIntent;
78 }
79 
generateClientCompositionDisplaySettings(const std::shared_ptr<renderengine::ExternalTexture> & buffer) const80 renderengine::DisplaySettings ScreenCaptureOutput::generateClientCompositionDisplaySettings(
81         const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
82     auto clientCompositionDisplay =
83             compositionengine::impl::Output::generateClientCompositionDisplaySettings(buffer);
84     clientCompositionDisplay.clip = mRenderArea.getSourceCrop();
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 
93     if (mEnableLocalTonemapping) {
94         clientCompositionDisplay.tonemapStrategy =
95                 renderengine::DisplaySettings::TonemapStrategy::Local;
96     }
97 
98     return clientCompositionDisplay;
99 }
100 
101 std::vector<compositionengine::LayerFE::LayerSettings>
generateClientCompositionRequests(bool supportsProtectedContent,ui::Dataspace outputDataspace,std::vector<compositionengine::LayerFE * > & outLayerFEs)102 ScreenCaptureOutput::generateClientCompositionRequests(
103         bool supportsProtectedContent, ui::Dataspace outputDataspace,
104         std::vector<compositionengine::LayerFE*>& outLayerFEs) {
105     auto clientCompositionLayers = compositionengine::impl::Output::
106             generateClientCompositionRequests(supportsProtectedContent, outputDataspace,
107                                               outLayerFEs);
108 
109     if (mRegionSampling) {
110         for (auto& layer : clientCompositionLayers) {
111             layer.backgroundBlurRadius = 0;
112             layer.blurRegions.clear();
113         }
114     }
115 
116     if (outputDataspace == ui::Dataspace::BT2020_HLG) {
117         for (auto& layer : clientCompositionLayers) {
118             auto transfer = layer.sourceDataspace & ui::Dataspace::TRANSFER_MASK;
119             if (transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_HLG) &&
120                 transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_ST2084)) {
121                 layer.whitePointNits *= (1000.0f / 203.0f);
122             }
123         }
124     }
125 
126     Rect sourceCrop = mRenderArea.getSourceCrop();
127     compositionengine::LayerFE::LayerSettings fillLayer;
128     fillLayer.source.buffer.buffer = nullptr;
129     fillLayer.source.solidColor = half3(0.0f, 0.0f, 0.0f);
130     fillLayer.geometry.boundaries =
131             FloatRect(static_cast<float>(sourceCrop.left), static_cast<float>(sourceCrop.top),
132                       static_cast<float>(sourceCrop.right), static_cast<float>(sourceCrop.bottom));
133     fillLayer.alpha = half(RenderArea::getCaptureFillValue(mRenderArea.getCaptureFill()));
134     clientCompositionLayers.insert(clientCompositionLayers.begin(), fillLayer);
135 
136     return clientCompositionLayers;
137 }
138 
139 } // namespace android
140