blob: 51d4ff854f0dcbb773bc14915b828ad0a384e079 [file] [log] [blame]
Marin Shalamanovf6b5d182020-06-12 02:08:51 +02001/*
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 Shalamanovf6b5d182020-06-12 02:08:51 +020020#include "DisplayDevice.h"
Vishnu Naircb8be502022-10-12 19:03:23 +000021#include "FrontEnd/LayerCreationArgs.h"
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020022#include "Layer.h"
23#include "LayerRenderArea.h"
24#include "SurfaceFlinger.h"
25
26namespace android {
27namespace {
28
Robert Carr22ceeaa2022-03-08 13:13:22 -080029void reparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
30 const Rect& drawingBounds) {
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020031 // Compute and cache the bounds for the new parent layer.
32 newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform(),
Robert Carr22ceeaa2022-03-08 13:13:22 -080033 0.f /* shadowRadius */);
Vishnu Naire14c6b32022-08-06 04:20:15 +000034 newParent->updateSnapshot(true /* updateGeometry */);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020035 oldParent->setChildrenDrawingParent(newParent);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020036};
37
38} // namespace
39
40LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop,
41 ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly,
Vishnu Nair36d5f8e2023-03-19 13:31:35 -070042 bool allowSecureLayers, const ui::Transform& layerTransform,
Alec Mouri3e5965f2023-04-07 18:00:58 +000043 const Rect& layerBufferSize, bool hintForSeamlessTransition)
44 : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, hintForSeamlessTransition,
45 allowSecureLayers),
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020046 mLayer(std::move(layer)),
Vishnu Nair36d5f8e2023-03-19 13:31:35 -070047 mLayerTransform(layerTransform),
48 mLayerBufferSize(layerBufferSize),
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020049 mCrop(crop),
50 mFlinger(flinger),
51 mChildrenOnly(childrenOnly) {}
52
53const ui::Transform& LayerRenderArea::getTransform() const {
54 return mTransform;
55}
56
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020057bool LayerRenderArea::isSecure() const {
chaviw70cb6a42020-07-30 13:57:36 -070058 return mAllowSecureLayers;
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020059}
60
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020061sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const {
62 return nullptr;
63}
64
65Rect LayerRenderArea::getSourceCrop() const {
66 if (mCrop.isEmpty()) {
Vishnu Nair36d5f8e2023-03-19 13:31:35 -070067 // TODO this should probably be mBounds instead of just buffer bounds
68 return mLayerBufferSize;
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020069 } else {
70 return mCrop;
71 }
72}
73
74void LayerRenderArea::render(std::function<void()> drawLayers) {
75 using namespace std::string_literals;
76
Vishnu Nair3af0ec02023-02-10 04:13:48 +000077 if (!mChildrenOnly) {
Vishnu Nair36d5f8e2023-03-19 13:31:35 -070078 mTransform = mLayerTransform.inverse();
Vishnu Nair3af0ec02023-02-10 04:13:48 +000079 }
80
81 if (mFlinger.mLayerLifecycleManagerEnabled) {
82 drawLayers();
83 return;
84 }
chaviw79468ab2021-10-27 11:11:24 -050085 // If layer is offscreen, update mirroring info if it exists
86 if (mLayer->isRemovedFromCurrentState()) {
87 mLayer->traverse(LayerVector::StateSet::Drawing,
Vishnu Nair3be61902023-04-26 19:47:29 -070088 [&](Layer* layer) { layer->updateMirrorInfo({}); });
chaviw79468ab2021-10-27 11:11:24 -050089 mLayer->traverse(LayerVector::StateSet::Drawing,
90 [&](Layer* layer) { layer->updateCloneBufferInfo(); });
91 }
92
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020093 if (!mChildrenOnly) {
chaviw79468ab2021-10-27 11:11:24 -050094 // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen
95 // layers in a regular cycles.
96 if (mLayer->isRemovedFromCurrentState()) {
97 FloatRect maxBounds = mFlinger.getMaxDisplayBounds();
98 mLayer->computeBounds(maxBounds, ui::Transform(), 0.f /* shadowRadius */);
99 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200100 drawLayers();
101 } else {
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200102 // In the "childrenOnly" case we reparent the children to a screenshot
103 // layer which has no properties set and which does not draw.
Robert Carr22ceeaa2022-03-08 13:13:22 -0800104 // We hold the statelock as the reparent-for-drawing operation modifies the
105 // hierarchy and there could be readers on Binder threads, like dump.
Patrick Williams46b61b92022-09-01 17:25:49 +0000106 auto screenshotParentLayer = mFlinger.getFactory().createEffectLayer(
Vishnu Nairb21272e2022-07-01 22:47:33 +0000107 {&mFlinger, nullptr, "Screenshot Parent"s, ISurfaceComposerClient::eNoColorFill,
108 LayerMetadata()});
Robert Carr22ceeaa2022-03-08 13:13:22 -0800109 {
110 Mutex::Autolock _l(mFlinger.mStateLock);
Patrick Williams278a88f2023-01-27 16:52:40 -0600111 reparentForDrawing(mLayer, screenshotParentLayer, getSourceCrop());
Robert Carr22ceeaa2022-03-08 13:13:22 -0800112 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200113 drawLayers();
Robert Carr22ceeaa2022-03-08 13:13:22 -0800114 {
115 Mutex::Autolock _l(mFlinger.mStateLock);
116 mLayer->setChildrenDrawingParent(mLayer);
117 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200118 }
Vishnu Nairfa9e2ef2023-05-22 15:10:20 -0700119 mLayer->updateSnapshot(/*updateGeometry=*/true);
120 mLayer->updateChildrenSnapshots(/*updateGeometry=*/true);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200121}
122
123} // namespace android