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 "DisplayRenderArea.h"
18 #include "DisplayDevice.h"
19
20 namespace android {
21 namespace {
22
applyDeviceOrientation(bool useIdentityTransform,const DisplayDevice & display)23 RenderArea::RotationFlags applyDeviceOrientation(bool useIdentityTransform,
24 const DisplayDevice& display) {
25 if (!useIdentityTransform) {
26 return RenderArea::RotationFlags::ROT_0;
27 }
28
29 return ui::Transform::toRotationFlags(display.getOrientation());
30 }
31
32 } // namespace
33
create(wp<const DisplayDevice> displayWeak,const Rect & sourceCrop,ui::Size reqSize,ui::Dataspace reqDataSpace,bool useIdentityTransform,bool hintForSeamlessTransition,bool allowSecureLayers)34 std::unique_ptr<RenderArea> DisplayRenderArea::create(wp<const DisplayDevice> displayWeak,
35 const Rect& sourceCrop, ui::Size reqSize,
36 ui::Dataspace reqDataSpace,
37 bool useIdentityTransform,
38 bool hintForSeamlessTransition,
39 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,
44 useIdentityTransform, hintForSeamlessTransition,
45 allowSecureLayers));
46 }
47 return nullptr;
48 }
49
DisplayRenderArea(sp<const DisplayDevice> display,const Rect & sourceCrop,ui::Size reqSize,ui::Dataspace reqDataSpace,bool useIdentityTransform,bool hintForSeamlessTransition,bool allowSecureLayers)50 DisplayRenderArea::DisplayRenderArea(sp<const DisplayDevice> display, const Rect& sourceCrop,
51 ui::Size reqSize, ui::Dataspace reqDataSpace,
52 bool useIdentityTransform, bool hintForSeamlessTransition,
53 bool allowSecureLayers)
54 : RenderArea(reqSize, CaptureFill::OPAQUE, reqDataSpace, hintForSeamlessTransition,
55 allowSecureLayers, applyDeviceOrientation(useIdentityTransform, *display)),
56 mDisplay(std::move(display)),
57 mSourceCrop(sourceCrop) {}
58
getTransform() const59 const ui::Transform& DisplayRenderArea::getTransform() const {
60 return mTransform;
61 }
62
isSecure() const63 bool DisplayRenderArea::isSecure() const {
64 return mAllowSecureLayers && mDisplay->isSecure();
65 }
66
getDisplayDevice() const67 sp<const DisplayDevice> DisplayRenderArea::getDisplayDevice() const {
68 return mDisplay;
69 }
70
getSourceCrop() const71 Rect DisplayRenderArea::getSourceCrop() const {
72 // use the projected display viewport by default.
73 if (mSourceCrop.isEmpty()) {
74 return mDisplay->getLayerStackSpaceRect();
75 }
76
77 // Correct for the orientation when the screen capture request contained
78 // useIdentityTransform. This will cause the rotation flag to be non 0 since
79 // it needs to rotate based on the screen orientation to allow the screenshot
80 // to be taken in the ROT_0 orientation
81 const auto flags = getRotationFlags();
82 int width = mDisplay->getLayerStackSpaceRect().getWidth();
83 int height = mDisplay->getLayerStackSpaceRect().getHeight();
84 ui::Transform rotation;
85 rotation.set(flags, width, height);
86 return rotation.transform(mSourceCrop);
87 }
88
89 } // namespace android
90