blob: c4f8666de52348b00a119e721f14c4f3d56b7dbb [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
29struct ReparentForDrawing {
30 const sp<Layer>& oldParent;
31
32 ReparentForDrawing(const sp<Layer>& oldParent, const sp<Layer>& newParent,
33 const Rect& drawingBounds)
34 : oldParent(oldParent) {
35 // Compute and cache the bounds for the new parent layer.
36 newParent->computeBounds(drawingBounds.toFloatRect(), ui::Transform(),
37 0.f /* shadowRadius */);
38 oldParent->setChildrenDrawingParent(newParent);
39 }
40 ~ReparentForDrawing() { oldParent->setChildrenDrawingParent(oldParent); }
41};
42
43} // namespace
44
45LayerRenderArea::LayerRenderArea(SurfaceFlinger& flinger, sp<Layer> layer, const Rect& crop,
46 ui::Size reqSize, ui::Dataspace reqDataSpace, bool childrenOnly,
47 const Rect& displayViewport)
48 : RenderArea(reqSize, CaptureFill::CLEAR, reqDataSpace, displayViewport),
49 mLayer(std::move(layer)),
50 mCrop(crop),
51 mFlinger(flinger),
52 mChildrenOnly(childrenOnly) {}
53
54const ui::Transform& LayerRenderArea::getTransform() const {
55 return mTransform;
56}
57
58Rect LayerRenderArea::getBounds() const {
59 return mLayer->getBufferSize(mLayer->getDrawingState());
60}
61
62int LayerRenderArea::getHeight() const {
63 return mLayer->getBufferSize(mLayer->getDrawingState()).getHeight();
64}
65
66int LayerRenderArea::getWidth() const {
67 return mLayer->getBufferSize(mLayer->getDrawingState()).getWidth();
68}
69
70bool LayerRenderArea::isSecure() const {
71 return false;
72}
73
74bool LayerRenderArea::needsFiltering() const {
75 return mNeedsFiltering;
76}
77
78sp<const DisplayDevice> LayerRenderArea::getDisplayDevice() const {
79 return nullptr;
80}
81
82Rect LayerRenderArea::getSourceCrop() const {
83 if (mCrop.isEmpty()) {
84 return getBounds();
85 } else {
86 return mCrop;
87 }
88}
89
90void LayerRenderArea::render(std::function<void()> drawLayers) {
91 using namespace std::string_literals;
92
93 const Rect sourceCrop = getSourceCrop();
94 // no need to check rotation because there is none
95 mNeedsFiltering = sourceCrop.width() != getReqWidth() || sourceCrop.height() != getReqHeight();
96
97 if (!mChildrenOnly) {
98 mTransform = mLayer->getTransform().inverse();
99 drawLayers();
100 } else {
101 uint32_t w = static_cast<uint32_t>(getWidth());
102 uint32_t h = static_cast<uint32_t>(getHeight());
103 // In the "childrenOnly" case we reparent the children to a screenshot
104 // layer which has no properties set and which does not draw.
105 sp<ContainerLayer> screenshotParentLayer = mFlinger.getFactory().createContainerLayer(
106 {&mFlinger, nullptr, "Screenshot Parent"s, w, h, 0, LayerMetadata()});
107
108 ReparentForDrawing reparent(mLayer, screenshotParentLayer, sourceCrop);
109 drawLayers();
110 }
111}
112
113} // namespace android