• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 namespace {
28 
getDisplaySize(ui::Rotation orientation,const Rect & sourceCrop)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 
getOrientedDisplaySpaceRect(ui::Rotation orientation,int reqWidth,int reqHeight)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 
createScreenCaptureOutput(ScreenCaptureOutputArgs args)45 std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutputArgs args) {
46     std::shared_ptr<ScreenCaptureOutput> output = compositionengine::impl::createOutputTemplated<
47             ScreenCaptureOutput, compositionengine::CompositionEngine, const RenderArea&,
48             const compositionengine::Output::ColorProfile&,
49             bool>(args.compositionEngine, args.renderArea, args.colorProfile, args.regionSampling,
50                   args.dimInGammaSpaceForEnhancedScreenshots);
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);
56     output->editState().clientTargetBrightness = args.targetBrightness;
57     output->editState().treat170mAsSrgb = args.treat170mAsSrgb;
58 
59     output->setDisplayColorProfile(std::make_unique<compositionengine::impl::DisplayColorProfile>(
60             compositionengine::DisplayColorProfileCreationArgsBuilder()
61                     .setHasWideColorGamut(true)
62                     .Build()));
63 
64     const Rect& sourceCrop = args.renderArea.getSourceCrop();
65     const ui::Rotation orientation = ui::Transform::toRotation(args.renderArea.getRotationFlags());
66     output->setDisplaySize(getDisplaySize(orientation, sourceCrop));
67     output->setProjection(orientation, sourceCrop,
68                           getOrientedDisplaySpaceRect(orientation, args.renderArea.getReqWidth(),
69                                                       args.renderArea.getReqHeight()));
70 
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     }
78     return output;
79 }
80 
ScreenCaptureOutput(const RenderArea & renderArea,const compositionengine::Output::ColorProfile & colorProfile,bool regionSampling,bool dimInGammaSpaceForEnhancedScreenshots)81 ScreenCaptureOutput::ScreenCaptureOutput(
82         const RenderArea& renderArea, const compositionengine::Output::ColorProfile& colorProfile,
83         bool regionSampling, bool dimInGammaSpaceForEnhancedScreenshots)
84       : mRenderArea(renderArea),
85         mColorProfile(colorProfile),
86         mRegionSampling(regionSampling),
87         mDimInGammaSpaceForEnhancedScreenshots(dimInGammaSpaceForEnhancedScreenshots) {}
88 
updateColorProfile(const compositionengine::CompositionRefreshArgs &)89 void ScreenCaptureOutput::updateColorProfile(const compositionengine::CompositionRefreshArgs&) {
90     auto& outputState = editState();
91     outputState.dataspace = mColorProfile.dataspace;
92     outputState.renderIntent = mColorProfile.renderIntent;
93 }
94 
generateClientCompositionDisplaySettings() const95 renderengine::DisplaySettings ScreenCaptureOutput::generateClientCompositionDisplaySettings()
96         const {
97     auto clientCompositionDisplay =
98             compositionengine::impl::Output::generateClientCompositionDisplaySettings();
99     clientCompositionDisplay.clip = mRenderArea.getSourceCrop();
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 
108     return clientCompositionDisplay;
109 }
110 
111 std::vector<compositionengine::LayerFE::LayerSettings>
generateClientCompositionRequests(bool supportsProtectedContent,ui::Dataspace outputDataspace,std::vector<compositionengine::LayerFE * > & outLayerFEs)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 
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 
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 
149 } // namespace android
150