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
20 #include "DisplayDevice.h"
21 #include "FrontEnd/LayerCreationArgs.h"
22 #include "Layer.h"
23 #include "LayerRenderArea.h"
24 #include "SurfaceFlinger.h"
25
26 namespace android {
27 namespace {
28
reparentForDrawing(const sp<Layer> & oldParent,const sp<Layer> & newParent,const Rect & drawingBounds)29 void reparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
30 const Rect& drawingBounds) {
31 // Compute and cache the bounds for the new parent layer.
32 newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform(),
33 0.f /* shadowRadius */);
34 newParent->updateSnapshot(true /* updateGeometry */);
35 oldParent->setChildrenDrawingParent(newParent);
36 };
37
38 } // namespace
39
LayerRenderArea(SurfaceFlinger & flinger,sp<Layer> layer,const Rect & crop,ui::Size reqSize,ui::Dataspace reqDataSpace,bool childrenOnly,bool allowSecureLayers,const ui::Transform & layerTransform,const Rect & layerBufferSize,bool hintForSeamlessTransition)40 LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop,
41 ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly,
42 bool allowSecureLayers, const ui::Transform& layerTransform,
43 const Rect& layerBufferSize, bool hintForSeamlessTransition)
44 : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, hintForSeamlessTransition,
45 allowSecureLayers),
46 mLayer(std::move(layer)),
47 mLayerTransform(layerTransform),
48 mLayerBufferSize(layerBufferSize),
49 mCrop(crop),
50 mFlinger(flinger),
51 mChildrenOnly(childrenOnly) {}
52
getTransform() const53 const ui::Transform& LayerRenderArea::getTransform() const {
54 return mTransform;
55 }
56
isSecure() const57 bool LayerRenderArea::isSecure() const {
58 return mAllowSecureLayers;
59 }
60
getDisplayDevice() const61 sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const {
62 return nullptr;
63 }
64
getSourceCrop() const65 Rect LayerRenderArea::getSourceCrop() const {
66 if (mCrop.isEmpty()) {
67 // TODO this should probably be mBounds instead of just buffer bounds
68 return mLayerBufferSize;
69 } else {
70 return mCrop;
71 }
72 }
73
render(std::function<void ()> drawLayers)74 void LayerRenderArea::render(std::function<void()> drawLayers) {
75 using namespace std::string_literals;
76
77 if (!mChildrenOnly) {
78 mTransform = mLayerTransform.inverse();
79 }
80
81 if (mFlinger.mLayerLifecycleManagerEnabled) {
82 drawLayers();
83 return;
84 }
85 // If layer is offscreen, update mirroring info if it exists
86 if (mLayer->isRemovedFromCurrentState()) {
87 mLayer->traverse(LayerVector::StateSet::Drawing,
88 [&](Layer* layer) { layer->updateMirrorInfo({}); });
89 mLayer->traverse(LayerVector::StateSet::Drawing,
90 [&](Layer* layer) { layer->updateCloneBufferInfo(); });
91 }
92
93 if (!mChildrenOnly) {
94 // 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 }
100 drawLayers();
101 } else {
102 // In the "childrenOnly" case we reparent the children to a screenshot
103 // layer which has no properties set and which does not draw.
104 // We hold the statelock as the reparent-for-drawing operation modifies the
105 // hierarchy and there could be readers on Binder threads, like dump.
106 auto screenshotParentLayer = mFlinger.getFactory().createEffectLayer(
107 {&mFlinger, nullptr, "Screenshot Parent"s, ISurfaceComposerClient::eNoColorFill,
108 LayerMetadata()});
109 {
110 Mutex::Autolock _l(mFlinger.mStateLock);
111 reparentForDrawing(mLayer, screenshotParentLayer, getSourceCrop());
112 }
113 drawLayers();
114 {
115 Mutex::Autolock _l(mFlinger.mStateLock);
116 mLayer->setChildrenDrawingParent(mLayer);
117 }
118 }
119 mLayer->updateSnapshot(/*updateGeometry=*/true);
120 mLayer->updateChildrenSnapshots(/*updateGeometry=*/true);
121 }
122
123 } // namespace android
124