blob: 896f25404ddce8dea6d9a2e018bc18aa404e2a88 [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
20#include "ContainerLayer.h"
21#include "DisplayDevice.h"
22#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 */);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020034 oldParent->setChildrenDrawingParent(newParent);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020035};
36
37} // namespace
38
39LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop,
40 ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly,
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020041 const Rect& layerStackRect, bool allowSecureLayers)
42 : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, layerStackRect, allowSecureLayers),
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020043 mLayer(std::move(layer)),
44 mCrop(crop),
45 mFlinger(flinger),
46 mChildrenOnly(childrenOnly) {}
47
48const ui::Transform& LayerRenderArea::getTransform() const {
49 return mTransform;
50}
51
52Rect LayerRenderArea::getBounds() const {
53 return mLayer->getBufferSize(mLayer->getDrawingState());
54}
55
56int LayerRenderArea::getHeight() const {
57 return mLayer->getBufferSize(mLayer->getDrawingState()).getHeight();
58}
59
60int LayerRenderArea::getWidth() const {
61 return mLayer->getBufferSize(mLayer->getDrawingState()).getWidth();
62}
63
64bool LayerRenderArea::isSecure() const {
chaviw70cb6a42020-07-30 13:57:36 -070065 return mAllowSecureLayers;
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020066}
67
68bool LayerRenderArea::needsFiltering() const {
69 return mNeedsFiltering;
70}
71
72sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const {
73 return nullptr;
74}
75
76Rect LayerRenderArea::getSourceCrop() const {
77 if (mCrop.isEmpty()) {
78 return getBounds();
79 } else {
80 return mCrop;
81 }
82}
83
84void LayerRenderArea::render(std::function<void()> drawLayers) {
85 using namespace std::string_literals;
86
87 const Rect sourceCrop = getSourceCrop();
88 // no need to check rotation because there is none
89 mNeedsFiltering = sourceCrop.width() != getReqWidth() || sourceCrop.height() != getReqHeight();
90
chaviw79468ab2021-10-27 11:11:24 -050091 // If layer is offscreen, update mirroring info if it exists
92 if (mLayer->isRemovedFromCurrentState()) {
93 mLayer->traverse(LayerVector::StateSet::Drawing,
94 [&](Layer* layer) { layer->updateMirrorInfo(); });
95 mLayer->traverse(LayerVector::StateSet::Drawing,
96 [&](Layer* layer) { layer->updateCloneBufferInfo(); });
97 }
98
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020099 if (!mChildrenOnly) {
100 mTransform = mLayer->getTransform().inverse();
chaviw79468ab2021-10-27 11:11:24 -0500101 // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen
102 // layers in a regular cycles.
103 if (mLayer->isRemovedFromCurrentState()) {
104 FloatRect maxBounds = mFlinger.getMaxDisplayBounds();
105 mLayer->computeBounds(maxBounds, ui::Transform(), 0.f /* shadowRadius */);
106 }
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200107 drawLayers();
108 } else {
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200109 // In the "childrenOnly" case we reparent the children to a screenshot
110 // layer which has no properties set and which does not draw.
Robert Carr22ceeaa2022-03-08 13:13:22 -0800111 // We hold the statelock as the reparent-for-drawing operation modifies the
112 // hierarchy and there could be readers on Binder threads, like dump.
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200113 sp<ContainerLayer> screenshotParentLayer = mFlinger.getFactory().createContainerLayer(
Robert Carr22ceeaa2022-03-08 13:13:22 -0800114 {&mFlinger, nullptr, "Screenshot Parent"s, 0, LayerMetadata()});
115 {
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