Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 <ui/GraphicTypes.h> |
| 18 | #include <ui/Transform.h> |
| 19 | |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 20 | #include "DisplayDevice.h" |
Vishnu Nair | cb8be50 | 2022-10-12 19:03:23 +0000 | [diff] [blame^] | 21 | #include "FrontEnd/LayerCreationArgs.h" |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 22 | #include "Layer.h" |
| 23 | #include "LayerRenderArea.h" |
| 24 | #include "SurfaceFlinger.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace { |
| 28 | |
Robert Carr | 22ceeaa | 2022-03-08 13:13:22 -0800 | [diff] [blame] | 29 | void reparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent, |
| 30 | const Rect& drawingBounds) { |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 31 | // Compute and cache the bounds for the new parent layer. |
| 32 | newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform(), |
Robert Carr | 22ceeaa | 2022-03-08 13:13:22 -0800 | [diff] [blame] | 33 | 0.f /* shadowRadius */); |
Vishnu Nair | e14c6b3 | 2022-08-06 04:20:15 +0000 | [diff] [blame] | 34 | newParent->updateSnapshot(true /* updateGeometry */); |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 35 | oldParent->setChildrenDrawingParent(newParent); |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop, |
| 41 | ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly, |
Marin Shalamanov | 6ad317c | 2020-07-29 23:34:07 +0200 | [diff] [blame] | 42 | const Rect& layerStackRect, bool allowSecureLayers) |
| 43 | : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, layerStackRect, allowSecureLayers), |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 44 | mLayer(std::move(layer)), |
| 45 | mCrop(crop), |
| 46 | mFlinger(flinger), |
| 47 | mChildrenOnly(childrenOnly) {} |
| 48 | |
| 49 | const ui::Transform& LayerRenderArea::getTransform() const { |
| 50 | return mTransform; |
| 51 | } |
| 52 | |
| 53 | Rect LayerRenderArea::getBounds() const { |
| 54 | return mLayer->getBufferSize(mLayer->getDrawingState()); |
| 55 | } |
| 56 | |
| 57 | int LayerRenderArea::getHeight() const { |
| 58 | return mLayer->getBufferSize(mLayer->getDrawingState()).getHeight(); |
| 59 | } |
| 60 | |
| 61 | int LayerRenderArea::getWidth() const { |
| 62 | return mLayer->getBufferSize(mLayer->getDrawingState()).getWidth(); |
| 63 | } |
| 64 | |
| 65 | bool LayerRenderArea::isSecure() const { |
chaviw | 70cb6a4 | 2020-07-30 13:57:36 -0700 | [diff] [blame] | 66 | return mAllowSecureLayers; |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool LayerRenderArea::needsFiltering() const { |
| 70 | return mNeedsFiltering; |
| 71 | } |
| 72 | |
| 73 | sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const { |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | Rect LayerRenderArea::getSourceCrop() const { |
| 78 | if (mCrop.isEmpty()) { |
| 79 | return getBounds(); |
| 80 | } else { |
| 81 | return mCrop; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void LayerRenderArea::render(std::function<void()> drawLayers) { |
| 86 | using namespace std::string_literals; |
| 87 | |
| 88 | const Rect sourceCrop = getSourceCrop(); |
| 89 | // no need to check rotation because there is none |
| 90 | mNeedsFiltering = sourceCrop.width() != getReqWidth() || sourceCrop.height() != getReqHeight(); |
| 91 | |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 92 | // If layer is offscreen, update mirroring info if it exists |
| 93 | if (mLayer->isRemovedFromCurrentState()) { |
| 94 | mLayer->traverse(LayerVector::StateSet::Drawing, |
| 95 | [&](Layer* layer) { layer->updateMirrorInfo(); }); |
| 96 | mLayer->traverse(LayerVector::StateSet::Drawing, |
| 97 | [&](Layer* layer) { layer->updateCloneBufferInfo(); }); |
| 98 | } |
| 99 | |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 100 | if (!mChildrenOnly) { |
| 101 | mTransform = mLayer->getTransform().inverse(); |
chaviw | 79468ab | 2021-10-27 11:11:24 -0500 | [diff] [blame] | 102 | // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen |
| 103 | // layers in a regular cycles. |
| 104 | if (mLayer->isRemovedFromCurrentState()) { |
| 105 | FloatRect maxBounds = mFlinger.getMaxDisplayBounds(); |
| 106 | mLayer->computeBounds(maxBounds, ui::Transform(), 0.f /* shadowRadius */); |
| 107 | } |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 108 | drawLayers(); |
| 109 | } else { |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 110 | // In the "childrenOnly" case we reparent the children to a screenshot |
| 111 | // layer which has no properties set and which does not draw. |
Robert Carr | 22ceeaa | 2022-03-08 13:13:22 -0800 | [diff] [blame] | 112 | // We hold the statelock as the reparent-for-drawing operation modifies the |
| 113 | // hierarchy and there could be readers on Binder threads, like dump. |
Patrick Williams | 46b61b9 | 2022-09-01 17:25:49 +0000 | [diff] [blame] | 114 | auto screenshotParentLayer = mFlinger.getFactory().createEffectLayer( |
Vishnu Nair | b21272e | 2022-07-01 22:47:33 +0000 | [diff] [blame] | 115 | {&mFlinger, nullptr, "Screenshot Parent"s, ISurfaceComposerClient::eNoColorFill, |
| 116 | LayerMetadata()}); |
Robert Carr | 22ceeaa | 2022-03-08 13:13:22 -0800 | [diff] [blame] | 117 | { |
| 118 | Mutex::Autolock _l(mFlinger.mStateLock); |
| 119 | reparentForDrawing(mLayer, screenshotParentLayer, sourceCrop); |
| 120 | } |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 121 | drawLayers(); |
Robert Carr | 22ceeaa | 2022-03-08 13:13:22 -0800 | [diff] [blame] | 122 | { |
| 123 | Mutex::Autolock _l(mFlinger.mStateLock); |
| 124 | mLayer->setChildrenDrawingParent(mLayer); |
| 125 | } |
Marin Shalamanov | f6b5d18 | 2020-06-12 02:08:51 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | } // namespace android |