blob: 9a6b328a1eced7bbb37e593884b87da1abd65b2f [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 "DisplayRenderArea.h"
18#include "DisplayDevice.h"
19
20namespace android {
21namespace {
22
chaviwc6ad8af2020-08-03 11:33:30 -070023RenderArea::RotationFlags applyDeviceOrientation(bool useIdentityTransform,
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020024 const DisplayDevice& display) {
chaviwc6ad8af2020-08-03 11:33:30 -070025 if (!useIdentityTransform) {
26 return RenderArea::RotationFlags::ROT_0;
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020027 }
28
chaviwc6ad8af2020-08-03 11:33:30 -070029 const ui::Rotation orientation = display.getPhysicalOrientation() + display.getOrientation();
30 return ui::Transform::toRotationFlags(orientation);
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020031}
32
33} // namespace
34
35std::unique_ptr<RenderArea> DisplayRenderArea::create(wp<const DisplayDevice> displayWeak,
36 const Rect& sourceCrop, ui::Size reqSize,
37 ui::Dataspace reqDataSpace,
chaviwc6ad8af2020-08-03 11:33:30 -070038 bool useIdentityTransform,
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020039 bool allowSecureLayers) {
40 if (auto display = displayWeak.promote()) {
41 // Using new to access a private constructor.
42 return std::unique_ptr<DisplayRenderArea>(
43 new DisplayRenderArea(std::move(display), sourceCrop, reqSize, reqDataSpace,
chaviwc6ad8af2020-08-03 11:33:30 -070044 useIdentityTransform, allowSecureLayers));
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020045 }
46 return nullptr;
47}
48
49DisplayRenderArea::DisplayRenderArea(sp<const DisplayDevice> display, const Rect& sourceCrop,
50 ui::Size reqSize, ui::Dataspace reqDataSpace,
chaviwc6ad8af2020-08-03 11:33:30 -070051 bool useIdentityTransform, bool allowSecureLayers)
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020052 : RenderArea(reqSize, CaptureFill::OPAQUE, reqDataSpace, display->getLayerStackSpaceRect(),
chaviwc6ad8af2020-08-03 11:33:30 -070053 allowSecureLayers, applyDeviceOrientation(useIdentityTransform, *display)),
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020054 mDisplay(std::move(display)),
chaviw70cb6a42020-07-30 13:57:36 -070055 mSourceCrop(sourceCrop) {}
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020056
57const ui::Transform& DisplayRenderArea::getTransform() const {
58 return mTransform;
59}
60
61Rect DisplayRenderArea::getBounds() const {
62 return mDisplay->getBounds();
63}
64
65int DisplayRenderArea::getHeight() const {
66 return mDisplay->getHeight();
67}
68
69int DisplayRenderArea::getWidth() const {
70 return mDisplay->getWidth();
71}
72
73bool DisplayRenderArea::isSecure() const {
74 return mAllowSecureLayers && mDisplay->isSecure();
75}
76
77sp<const DisplayDevice> DisplayRenderArea::getDisplayDevice() const {
78 return mDisplay;
79}
80
81bool DisplayRenderArea::needsFiltering() const {
82 // check if the projection from the logical render area
83 // to the physical render area requires filtering
84 const Rect& sourceCrop = getSourceCrop();
85 int width = sourceCrop.width();
86 int height = sourceCrop.height();
87 if (getRotationFlags() & ui::Transform::ROT_90) {
88 std::swap(width, height);
89 }
90 return width != getReqWidth() || height != getReqHeight();
91}
92
93Rect DisplayRenderArea::getSourceCrop() const {
94 // use the projected display viewport by default.
95 if (mSourceCrop.isEmpty()) {
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020096 return mDisplay->getLayerStackSpaceRect();
Marin Shalamanovf6b5d182020-06-12 02:08:51 +020097 }
98
chaviwc6ad8af2020-08-03 11:33:30 -070099 // Correct for the orientation when the screen capture request contained
100 // useIdentityTransform. This will cause the rotation flag to be non 0 since
101 // it needs to rotate based on the screen orientation to allow the screenshot
102 // to be taken in the ROT_0 orientation
103 const auto flags = getRotationFlags();
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200104 int width = mDisplay->getLayerStackSpaceRect().getWidth();
105 int height = mDisplay->getLayerStackSpaceRect().getHeight();
Marin Shalamanovf6b5d182020-06-12 02:08:51 +0200106 ui::Transform rotation;
107 rotation.set(flags, width, height);
108 return rotation.transform(mSourceCrop);
109}
110
111} // namespace android