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 "common/include/common/FlagManager.h"
20 #include "ui/Rotation.h"
21
22 #include <compositionengine/CompositionEngine.h>
23 #include <compositionengine/DisplayColorProfileCreationArgs.h>
24 #include <compositionengine/impl/DisplayColorProfile.h>
25 #include <ui/HdrRenderTypeUtils.h>
26 #include <ui/Rotation.h>
27
28 namespace android {
29
createScreenCaptureOutput(ScreenCaptureOutputArgs args)30 std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutputArgs args) {
31 std::shared_ptr<ScreenCaptureOutput> output = compositionengine::impl::createOutputTemplated<
32 ScreenCaptureOutput, compositionengine::CompositionEngine,
33 /* sourceCrop */ const Rect, ftl::Optional<DisplayIdVariant>,
34 const compositionengine::Output::ColorProfile&,
35 /* layerAlpha */ float,
36 /* regionSampling */ bool>(args.compositionEngine, args.sourceCrop,
37 args.displayIdVariant, args.colorProfile, args.layerAlpha,
38 args.regionSampling,
39 args.dimInGammaSpaceForEnhancedScreenshots,
40 args.enableLocalTonemapping);
41 output->editState().isSecure = args.isSecure;
42 output->editState().isProtected = args.isProtected;
43 output->setCompositionEnabled(true);
44 output->setLayerFilter({args.layerStack});
45 output->setRenderSurface(std::make_unique<ScreenCaptureRenderSurface>(std::move(args.buffer)));
46 output->setDisplayBrightness(args.sdrWhitePointNits, args.displayBrightnessNits);
47 output->editState().clientTargetBrightness = args.targetBrightness;
48 output->editState().treat170mAsSrgb = args.treat170mAsSrgb;
49
50 output->setDisplayColorProfile(std::make_unique<compositionengine::impl::DisplayColorProfile>(
51 compositionengine::DisplayColorProfileCreationArgsBuilder()
52 .setHasWideColorGamut(true)
53 .Build()));
54
55 const Rect& sourceCrop = args.sourceCrop;
56 const ui::Rotation orientation = ui::ROTATION_0;
57 output->setDisplaySize({sourceCrop.getWidth(), sourceCrop.getHeight()});
58 output->setProjection(orientation, sourceCrop,
59 {args.reqBufferSize.width, args.reqBufferSize.height});
60
61 {
62 std::string name = args.regionSampling ? "RegionSampling" : "ScreenCaptureOutput";
63 if (const auto id = args.displayIdVariant.and_then(asDisplayIdOfType<DisplayId>)) {
64 base::StringAppendF(&name, " for %" PRIu64, id->value);
65 }
66 output->setName(name);
67 }
68 return output;
69 }
70
ScreenCaptureOutput(const Rect sourceCrop,ftl::Optional<DisplayIdVariant> displayIdVariant,const compositionengine::Output::ColorProfile & colorProfile,float layerAlpha,bool regionSampling,bool dimInGammaSpaceForEnhancedScreenshots,bool enableLocalTonemapping)71 ScreenCaptureOutput::ScreenCaptureOutput(
72 const Rect sourceCrop, ftl::Optional<DisplayIdVariant> displayIdVariant,
73 const compositionengine::Output::ColorProfile& colorProfile, float layerAlpha,
74 bool regionSampling, bool dimInGammaSpaceForEnhancedScreenshots,
75 bool enableLocalTonemapping)
76 : mSourceCrop(sourceCrop),
77 mDisplayIdVariant(displayIdVariant),
78 mColorProfile(colorProfile),
79 mLayerAlpha(layerAlpha),
80 mRegionSampling(regionSampling),
81 mDimInGammaSpaceForEnhancedScreenshots(dimInGammaSpaceForEnhancedScreenshots),
82 mEnableLocalTonemapping(enableLocalTonemapping) {}
83
updateColorProfile(const compositionengine::CompositionRefreshArgs &)84 void ScreenCaptureOutput::updateColorProfile(const compositionengine::CompositionRefreshArgs&) {
85 auto& outputState = editState();
86 outputState.dataspace = mColorProfile.dataspace;
87 outputState.renderIntent = mColorProfile.renderIntent;
88 }
89
generateClientCompositionDisplaySettings(const std::shared_ptr<renderengine::ExternalTexture> & buffer) const90 renderengine::DisplaySettings ScreenCaptureOutput::generateClientCompositionDisplaySettings(
91 const std::shared_ptr<renderengine::ExternalTexture>& buffer) const {
92 auto clientCompositionDisplay =
93 compositionengine::impl::Output::generateClientCompositionDisplaySettings(buffer);
94 clientCompositionDisplay.clip = mSourceCrop;
95
96 auto renderIntent = static_cast<ui::RenderIntent>(clientCompositionDisplay.renderIntent);
97 if (mDimInGammaSpaceForEnhancedScreenshots && renderIntent != ui::RenderIntent::COLORIMETRIC &&
98 renderIntent != ui::RenderIntent::TONE_MAP_COLORIMETRIC) {
99 clientCompositionDisplay.dimmingStage =
100 aidl::android::hardware::graphics::composer3::DimmingStage::GAMMA_OETF;
101 }
102
103 if (mEnableLocalTonemapping) {
104 clientCompositionDisplay.tonemapStrategy =
105 renderengine::DisplaySettings::TonemapStrategy::Local;
106 if (static_cast<ui::PixelFormat>(buffer->getPixelFormat()) == ui::PixelFormat::RGBA_FP16) {
107 clientCompositionDisplay.targetHdrSdrRatio =
108 getState().displayBrightnessNits / getState().sdrWhitePointNits;
109 } else {
110 clientCompositionDisplay.targetHdrSdrRatio = 1.f;
111 }
112 }
113
114 return clientCompositionDisplay;
115 }
116
117 std::unordered_map<int32_t, aidl::android::hardware::graphics::composer3::Luts>
generateLuts()118 ScreenCaptureOutput::generateLuts() {
119 std::unordered_map<int32_t, aidl::android::hardware::graphics::composer3::Luts> lutsMapper;
120 if (FlagManager::getInstance().luts_api()) {
121 std::vector<sp<GraphicBuffer>> buffers;
122 std::vector<int32_t> layerIds;
123
124 for (const auto* layer : getOutputLayersOrderedByZ()) {
125 const auto& layerState = layer->getState();
126 const auto* layerFEState = layer->getLayerFE().getCompositionState();
127 auto pixelFormat = layerFEState->buffer
128 ? std::make_optional(
129 static_cast<ui::PixelFormat>(layerFEState->buffer->getPixelFormat()))
130 : std::nullopt;
131 const auto hdrType = getHdrRenderType(layerState.dataspace, pixelFormat,
132 layerFEState->desiredHdrSdrRatio);
133 if (layerFEState->buffer && !layerFEState->luts &&
134 hdrType == HdrRenderType::GENERIC_HDR) {
135 buffers.push_back(layerFEState->buffer);
136 layerIds.push_back(layer->getLayerFE().getSequence());
137 }
138 }
139
140 std::vector<aidl::android::hardware::graphics::composer3::Luts> luts;
141 if (const auto physicalDisplayId = mDisplayIdVariant.and_then(asPhysicalDisplayId)) {
142 auto& hwc = getCompositionEngine().getHwComposer();
143 hwc.getLuts(*physicalDisplayId, buffers, &luts);
144 }
145
146 if (buffers.size() == luts.size()) {
147 for (size_t i = 0; i < luts.size(); i++) {
148 lutsMapper[layerIds[i]] = std::move(luts[i]);
149 }
150 }
151 }
152 return lutsMapper;
153 }
154
155 std::vector<compositionengine::LayerFE::LayerSettings>
generateClientCompositionRequests(bool supportsProtectedContent,ui::Dataspace outputDataspace,std::vector<compositionengine::LayerFE * > & outLayerFEs)156 ScreenCaptureOutput::generateClientCompositionRequests(
157 bool supportsProtectedContent, ui::Dataspace outputDataspace,
158 std::vector<compositionengine::LayerFE*>& outLayerFEs) {
159 // This map maps the layer unique id to a Lut
160 std::unordered_map<int32_t, aidl::android::hardware::graphics::composer3::Luts> lutsMapper =
161 generateLuts();
162
163 auto clientCompositionLayers = compositionengine::impl::Output::
164 generateClientCompositionRequests(supportsProtectedContent, outputDataspace,
165 outLayerFEs);
166
167 for (auto& layer : clientCompositionLayers) {
168 if (lutsMapper.find(layer.sequence) != lutsMapper.end()) {
169 auto& aidlLuts = lutsMapper[layer.sequence];
170 if (aidlLuts.pfd.get() >= 0 && aidlLuts.offsets) {
171 std::vector<int32_t> offsets = *aidlLuts.offsets;
172 std::vector<int32_t> dimensions;
173 dimensions.reserve(offsets.size());
174 std::vector<int32_t> sizes;
175 sizes.reserve(offsets.size());
176 std::vector<int32_t> keys;
177 keys.reserve(offsets.size());
178 for (size_t j = 0; j < offsets.size(); j++) {
179 dimensions.emplace_back(
180 static_cast<int32_t>(aidlLuts.lutProperties[j].dimension));
181 sizes.emplace_back(aidlLuts.lutProperties[j].size);
182 keys.emplace_back(
183 static_cast<int32_t>(aidlLuts.lutProperties[j].samplingKeys[0]));
184 }
185 layer.luts = std::make_shared<gui::DisplayLuts>(base::unique_fd(
186 aidlLuts.pfd.dup().get()),
187 offsets, dimensions, sizes, keys);
188 }
189 }
190 }
191
192 if (mRegionSampling) {
193 for (auto& layer : clientCompositionLayers) {
194 layer.backgroundBlurRadius = 0;
195 layer.blurRegions.clear();
196 }
197 }
198
199 if (outputDataspace == ui::Dataspace::BT2020_HLG) {
200 for (auto& layer : clientCompositionLayers) {
201 auto transfer = layer.sourceDataspace & ui::Dataspace::TRANSFER_MASK;
202 if (transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_HLG) &&
203 transfer != static_cast<int32_t>(ui::Dataspace::TRANSFER_ST2084)) {
204 layer.whitePointNits *= (1000.0f / 203.0f);
205 }
206 }
207 }
208
209 compositionengine::LayerFE::LayerSettings fillLayer;
210 fillLayer.name = "ScreenCaptureFillLayer";
211 fillLayer.source.buffer.buffer = nullptr;
212 fillLayer.source.solidColor = half3(0.0f, 0.0f, 0.0f);
213 fillLayer.geometry.boundaries =
214 FloatRect(static_cast<float>(mSourceCrop.left), static_cast<float>(mSourceCrop.top),
215 static_cast<float>(mSourceCrop.right),
216 static_cast<float>(mSourceCrop.bottom));
217
218 fillLayer.alpha = half(mLayerAlpha);
219 clientCompositionLayers.insert(clientCompositionLayers.begin(), fillLayer);
220
221 return clientCompositionLayers;
222 }
223
224 } // namespace android
225