1 /*
2  * Copyright 2024 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 #pragma once
18 
19 #include "DisplayDevice.h"
20 #include "DisplayRenderArea.h"
21 #include "LayerRenderArea.h"
22 #include "ui/Size.h"
23 #include "ui/Transform.h"
24 
25 namespace android {
26 /**
27  * A parameter object for creating a render area
28  */
29 struct RenderAreaBuilder {
30     // Source crop of the render area
31     Rect crop;
32 
33     // Size of the physical render area
34     ui::Size reqSize;
35 
36     // Composition data space of the render area
37     ui::Dataspace reqDataSpace;
38 
39     ftl::Flags<RenderArea::Options> options;
40     virtual std::unique_ptr<RenderArea> build() const = 0;
41 
RenderAreaBuilderRenderAreaBuilder42     RenderAreaBuilder(Rect crop, ui::Size reqSize, ui::Dataspace reqDataSpace,
43                       ftl::Flags<RenderArea::Options> options)
44           : crop(crop), reqSize(reqSize), reqDataSpace(reqDataSpace), options(options) {}
45 
46     virtual ~RenderAreaBuilder() = default;
47 };
48 
49 struct DisplayRenderAreaBuilder : RenderAreaBuilder {
DisplayRenderAreaBuilderDisplayRenderAreaBuilder50     DisplayRenderAreaBuilder(Rect crop, ui::Size reqSize, ui::Dataspace reqDataSpace,
51                              wp<const DisplayDevice> displayWeak,
52                              ftl::Flags<RenderArea::Options> options)
53           : RenderAreaBuilder(crop, reqSize, reqDataSpace, options), displayWeak(displayWeak) {}
54 
55     // Display that render area will be on
56     wp<const DisplayDevice> displayWeak;
57 
buildDisplayRenderAreaBuilder58     std::unique_ptr<RenderArea> build() const override {
59         return DisplayRenderArea::create(displayWeak, crop, reqSize, reqDataSpace, options);
60     }
61 };
62 
63 struct LayerRenderAreaBuilder : RenderAreaBuilder {
LayerRenderAreaBuilderLayerRenderAreaBuilder64     LayerRenderAreaBuilder(Rect crop, ui::Size reqSize, ui::Dataspace reqDataSpace, sp<Layer> layer,
65                            bool childrenOnly, ftl::Flags<RenderArea::Options> options)
66           : RenderAreaBuilder(crop, reqSize, reqDataSpace, options),
67             layer(layer),
68             childrenOnly(childrenOnly) {}
69 
70     // Root layer of the render area
71     sp<Layer> layer;
72 
73     // Layer snapshot of the root layer
74     frontend::LayerSnapshot layerSnapshot;
75 
76     // Transform to be applied on the layers to transform them
77     // into the logical render area
78     ui::Transform layerTransform{ui::Transform()};
79 
80     // Buffer bounds
81     Rect layerBufferSize{Rect()};
82 
83     // If false, transform is inverted from the parent snapshot
84     bool childrenOnly;
85 
86     // Uses parent snapshot to determine layer transform and buffer size
setLayerSnapshotLayerRenderAreaBuilder87     void setLayerSnapshot(const frontend::LayerSnapshot& parentSnapshot) {
88         layerSnapshot = parentSnapshot;
89         if (!childrenOnly) {
90             layerTransform = parentSnapshot.localTransform.inverse();
91         }
92         layerBufferSize = parentSnapshot.bufferSize;
93     }
94 
buildLayerRenderAreaBuilder95     std::unique_ptr<RenderArea> build() const override {
96         return std::make_unique<LayerRenderArea>(layer, std::move(layerSnapshot), crop, reqSize,
97                                                  reqDataSpace, layerTransform, layerBufferSize,
98                                                  options);
99     }
100 };
101 
102 } // namespace android