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 | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame^] | 27 | std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutputArgs args) { |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 28 | std::shared_ptr<ScreenCaptureOutput> output = compositionengine::impl::createOutputTemplated< |
Patrick Williams | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame^] | 29 | ScreenCaptureOutput, compositionengine::CompositionEngine, const RenderArea&, |
| 30 | std::unordered_set<compositionengine::LayerFE*>, |
| 31 | const compositionengine::Output::ColorProfile&, bool>(args.compositionEngine, |
| 32 | args.renderArea, |
| 33 | std::move( |
| 34 | args.filterForScreenshot), |
| 35 | args.colorProfile, |
| 36 | args.regionSampling); |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 37 | output->editState().isSecure = args.renderArea.isSecure(); |
| 38 | output->setCompositionEnabled(true); |
| 39 | output->setLayerFilter({args.layerStack}); |
| 40 | output->setRenderSurface(std::make_unique<ScreenCaptureRenderSurface>(std::move(args.buffer))); |
| 41 | output->setDisplayBrightness(args.sdrWhitePointNits, args.displayBrightnessNits); |
| 42 | |
| 43 | output->setDisplayColorProfile(std::make_unique<compositionengine::impl::DisplayColorProfile>( |
| 44 | compositionengine::DisplayColorProfileCreationArgsBuilder() |
| 45 | .setHasWideColorGamut(true) |
| 46 | .Build())); |
| 47 | |
| 48 | ui::Rotation orientation = ui::Transform::toRotation(args.renderArea.getRotationFlags()); |
| 49 | Rect orientedDisplaySpaceRect{args.renderArea.getReqWidth(), args.renderArea.getReqHeight()}; |
| 50 | output->setProjection(orientation, args.renderArea.getLayerStackSpaceRect(), |
| 51 | orientedDisplaySpaceRect); |
| 52 | |
| 53 | Rect sourceCrop = args.renderArea.getSourceCrop(); |
| 54 | output->setDisplaySize({sourceCrop.getWidth(), sourceCrop.getHeight()}); |
| 55 | |
| 56 | return output; |
| 57 | } |
| 58 | |
Patrick Williams | 01c3116 | 2022-11-14 17:45:19 +0000 | [diff] [blame^] | 59 | ScreenCaptureOutput::ScreenCaptureOutput( |
| 60 | const RenderArea& renderArea, |
| 61 | std::unordered_set<compositionengine::LayerFE*> filterForScreenshot, |
| 62 | const compositionengine::Output::ColorProfile& colorProfile, bool regionSampling) |
| 63 | : mRenderArea(renderArea), |
| 64 | mFilterForScreenshot(std::move(filterForScreenshot)), |
| 65 | mColorProfile(colorProfile), |
| 66 | mRegionSampling(regionSampling) {} |
Patrick Williams | 7584c6a | 2022-10-29 02:10:58 +0000 | [diff] [blame] | 67 | |
| 68 | void ScreenCaptureOutput::updateColorProfile(const compositionengine::CompositionRefreshArgs&) { |
| 69 | auto& outputState = editState(); |
| 70 | outputState.dataspace = mColorProfile.dataspace; |
| 71 | outputState.renderIntent = mColorProfile.renderIntent; |
| 72 | } |
| 73 | |
| 74 | renderengine::DisplaySettings ScreenCaptureOutput::generateClientCompositionDisplaySettings() |
| 75 | const { |
| 76 | auto clientCompositionDisplay = |
| 77 | compositionengine::impl::Output::generateClientCompositionDisplaySettings(); |
| 78 | clientCompositionDisplay.clip = mRenderArea.getSourceCrop(); |
| 79 | clientCompositionDisplay.targetLuminanceNits = -1; |
| 80 | return clientCompositionDisplay; |
| 81 | } |
| 82 | |
| 83 | std::vector<compositionengine::LayerFE::LayerSettings> |
| 84 | ScreenCaptureOutput::generateClientCompositionRequests( |
| 85 | bool supportsProtectedContent, ui::Dataspace outputDataspace, |
| 86 | std::vector<compositionengine::LayerFE*>& outLayerFEs) { |
| 87 | auto clientCompositionLayers = compositionengine::impl::Output:: |
| 88 | generateClientCompositionRequests(supportsProtectedContent, outputDataspace, |
| 89 | outLayerFEs); |
| 90 | |
| 91 | if (mRegionSampling) { |
| 92 | for (auto& layer : clientCompositionLayers) { |
| 93 | layer.backgroundBlurRadius = 0; |
| 94 | layer.blurRegions.clear(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | Rect sourceCrop = mRenderArea.getSourceCrop(); |
| 99 | compositionengine::LayerFE::LayerSettings fillLayer; |
| 100 | fillLayer.source.buffer.buffer = nullptr; |
| 101 | fillLayer.source.solidColor = half3(0.0f, 0.0f, 0.0f); |
| 102 | fillLayer.geometry.boundaries = |
| 103 | FloatRect(static_cast<float>(sourceCrop.left), static_cast<float>(sourceCrop.top), |
| 104 | static_cast<float>(sourceCrop.right), static_cast<float>(sourceCrop.bottom)); |
| 105 | fillLayer.alpha = half(RenderArea::getCaptureFillValue(mRenderArea.getCaptureFill())); |
| 106 | clientCompositionLayers.insert(clientCompositionLayers.begin(), fillLayer); |
| 107 | |
| 108 | return clientCompositionLayers; |
| 109 | } |
| 110 | |
| 111 | bool ScreenCaptureOutput::layerNeedsFiltering(const compositionengine::OutputLayer* layer) const { |
| 112 | return mRenderArea.needsFiltering() || |
| 113 | mFilterForScreenshot.find(&layer->getLayerFE()) != mFilterForScreenshot.end(); |
| 114 | } |
| 115 | |
| 116 | } // namespace android |