blob: 6bc7dc1dd741eaac5151da00798719ad8a4b97d5 [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"
21#include "Layer.h"
22#include "LayerRenderArea.h"
23#include "SurfaceFlinger.h"
24
25namespace android {
26namespace {
27
Robert Carr22ceeaa2022-03-08 13:13:22 -080028void reparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
29 const Rect& drawingBounds) {
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020030 // Compute and cache the bounds for the new parent layer.
31 newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform(),
Robert Carr22ceeaa2022-03-08 13:13:22 -080032 0.f /* shadowRadius */);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020033 oldParent->setChildrenDrawingParent(newParent);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020034};
35
36} // namespace
37
38LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop,
39 ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly,
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020040 const Rect& layerStackRect, bool allowSecureLayers)
41 : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, layerStackRect, allowSecureLayers),
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020042 mLayer(std::move(layer)),
43 mCrop(crop),
44 mFlinger(flinger),
45 mChildrenOnly(childrenOnly) {}
46
47const ui::Transform& LayerRenderArea::getTransform() const {
48 return mTransform;
49}
50
51Rect LayerRenderArea::getBounds() const {
52 return mLayer->getBufferSize(mLayer->getDrawingState());
53}
54
55int LayerRenderArea::getHeight() const {
56 return mLayer->getBufferSize(mLayer->getDrawingState()).getHeight();
57}
58
59int LayerRenderArea::getWidth() const {
60 return mLayer->getBufferSize(mLayer->getDrawingState()).getWidth();
61}
62
63bool LayerRenderArea::isSecure() const {
chaviw70cb6a42020-07-30 13:57:36 -070064 return mAllowSecureLayers;
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020065}
66
67bool LayerRenderArea::needsFiltering() const {
68 return mNeedsFiltering;
69}
70
71sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const {
72 return nullptr;
73}
74
75Rect LayerRenderArea::getSourceCrop() const {
76 if (mCrop.isEmpty()) {
77 return getBounds();
78 } else {
79 return mCrop;
80 }
81}
82
83void LayerRenderArea::render(std::function<void()> drawLayers) {
84 using namespace std::string_literals;
85
86 const Rect sourceCrop = getSourceCrop();
87 // no need to check rotation because there is none
88 mNeedsFiltering = sourceCrop.width() != getReqWidth() || sourceCrop.height() != getReqHeight();
89
chaviw79468ab2021-10-27 11:11:24 -050090 // If layer is offscreen, update mirroring info if it exists
91 if (mLayer->isRemovedFromCurrentState()) {
92 mLayer->traverse(LayerVector::StateSet::Drawing,
93 [&](Layer* layer) { layer->updateMirrorInfo(); });
94 mLayer->traverse(LayerVector::StateSet::Drawing,
95 [&](Layer* layer) { layer->updateCloneBufferInfo(); });
96 }
97
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020098 if (!mChildrenOnly) {
99 mTransform = mLayer->getTransform().inverse();
chaviw79468ab2021-10-27 11:11:24 -0500100 // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen
101 // layers in a regular cycles.
102 if (mLayer->isRemovedFromCurrentState()) {
103 FloatRect maxBounds = mFlinger.getMaxDisplayBounds();
104 mLayer->computeBounds(maxBounds, ui::Transform(), 0.f /* shadowRadius */);
105 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200106 drawLayers();
107 } else {
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200108 // In the "childrenOnly" case we reparent the children to a screenshot
109 // layer which has no properties set and which does not draw.
Robert Carr22ceeaa2022-03-08 13:13:22 -0800110 // We hold the statelock as the reparent-for-drawing operation modifies the
111 // hierarchy and there could be readers on Binder threads, like dump.
Patrick Williams46b61b92022-09-01 17:25:49 +0000112 auto screenshotParentLayer = mFlinger.getFactory().createEffectLayer(
Vishnu Nairb21272e2022-07-01 22:47:33 +0000113 {&mFlinger, nullptr, "Screenshot Parent"s, ISurfaceComposerClient::eNoColorFill,
114 LayerMetadata()});
Robert Carr22ceeaa2022-03-08 13:13:22 -0800115 {
116 Mutex::Autolock _l(mFlinger.mStateLock);
117 reparentForDrawing(mLayer, screenshotParentLayer, sourceCrop);
118 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200119 drawLayers();
Robert Carr22ceeaa2022-03-08 13:13:22 -0800120 {
121 Mutex::Autolock _l(mFlinger.mStateLock);
122 mLayer->setChildrenDrawingParent(mLayer);
123 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200124 }
125}
126
127} // namespace android