1 /*
2 * Copyright (C) 2007 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 // #define LOG_NDEBUG 0
22 #undef LOG_TAG
23 #define LOG_TAG "EffectLayer"
24
25 #include "EffectLayer.h"
26
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30
31 #include <compositionengine/CompositionEngine.h>
32 #include <compositionengine/LayerFECompositionState.h>
33 #include <renderengine/RenderEngine.h>
34 #include <ui/GraphicBuffer.h>
35 #include <utils/Errors.h>
36 #include <utils/Log.h>
37
38 #include "DisplayDevice.h"
39 #include "SurfaceFlinger.h"
40
41 namespace android {
42 // ---------------------------------------------------------------------------
43
EffectLayer(const LayerCreationArgs & args)44 EffectLayer::EffectLayer(const LayerCreationArgs& args)
45 : Layer(args),
46 mCompositionState{mFlinger->getCompositionEngine().createLayerFECompositionState()} {}
47
48 EffectLayer::~EffectLayer() = default;
49
prepareClientCompositionList(compositionengine::LayerFE::ClientCompositionTargetSettings & targetSettings)50 std::vector<compositionengine::LayerFE::LayerSettings> EffectLayer::prepareClientCompositionList(
51 compositionengine::LayerFE::ClientCompositionTargetSettings& targetSettings) {
52 std::vector<compositionengine::LayerFE::LayerSettings> results;
53 std::optional<compositionengine::LayerFE::LayerSettings> layerSettings =
54 prepareClientComposition(targetSettings);
55 // Nothing to render.
56 if (!layerSettings) {
57 return {};
58 }
59
60 // set the shadow for the layer if needed
61 prepareShadowClientComposition(*layerSettings, targetSettings.viewport);
62
63 // If fill bounds are occluded or the fill color is invalid skip the fill settings.
64 if (targetSettings.realContentIsVisible && fillsColor()) {
65 // Set color for color fill settings.
66 layerSettings->source.solidColor = getColor().rgb;
67 results.push_back(*layerSettings);
68 } else if (hasBlur() || drawShadows()) {
69 layerSettings->skipContentDraw = true;
70 results.push_back(*layerSettings);
71 }
72
73 return results;
74 }
75
isVisible() const76 bool EffectLayer::isVisible() const {
77 return !isHiddenByPolicy() && (getAlpha() > 0.0_hf || hasBlur()) && hasSomethingToDraw();
78 }
79
setColor(const half3 & color)80 bool EffectLayer::setColor(const half3& color) {
81 if (mDrawingState.color.r == color.r && mDrawingState.color.g == color.g &&
82 mDrawingState.color.b == color.b) {
83 return false;
84 }
85
86 mDrawingState.sequence++;
87 mDrawingState.color.r = color.r;
88 mDrawingState.color.g = color.g;
89 mDrawingState.color.b = color.b;
90 mDrawingState.modified = true;
91 setTransactionFlags(eTransactionNeeded);
92 return true;
93 }
94
setDataspace(ui::Dataspace dataspace)95 bool EffectLayer::setDataspace(ui::Dataspace dataspace) {
96 if (mDrawingState.dataspace == dataspace) {
97 return false;
98 }
99
100 mDrawingState.sequence++;
101 mDrawingState.dataspace = dataspace;
102 mDrawingState.modified = true;
103 setTransactionFlags(eTransactionNeeded);
104 return true;
105 }
106
preparePerFrameCompositionState()107 void EffectLayer::preparePerFrameCompositionState() {
108 Layer::preparePerFrameCompositionState();
109
110 auto* compositionState = editCompositionState();
111 compositionState->color = getColor();
112 compositionState->compositionType =
113 aidl::android::hardware::graphics::composer3::Composition::SOLID_COLOR;
114 }
115
getCompositionEngineLayerFE() const116 sp<compositionengine::LayerFE> EffectLayer::getCompositionEngineLayerFE() const {
117 // There's no need to get a CE Layer if the EffectLayer isn't going to draw anything. In that
118 // case, it acts more like a ContainerLayer so returning a null CE Layer makes more sense
119 if (hasSomethingToDraw()) {
120 return asLayerFE();
121 } else {
122 return nullptr;
123 }
124 }
125
editCompositionState()126 compositionengine::LayerFECompositionState* EffectLayer::editCompositionState() {
127 return mCompositionState.get();
128 }
129
getCompositionState() const130 const compositionengine::LayerFECompositionState* EffectLayer::getCompositionState() const {
131 return mCompositionState.get();
132 }
133
isOpaque(const Layer::State & s) const134 bool EffectLayer::isOpaque(const Layer::State& s) const {
135 // Consider the layer to be opaque if its opaque flag is set or its effective
136 // alpha (considering the alpha of its parents as well) is 1.0;
137 return (s.flags & layer_state_t::eLayerOpaque) != 0 || (fillsColor() && getAlpha() == 1.0_hf);
138 }
139
getDataSpace() const140 ui::Dataspace EffectLayer::getDataSpace() const {
141 return mDrawingState.dataspace;
142 }
143
createClone()144 sp<Layer> EffectLayer::createClone() {
145 sp<EffectLayer> layer = mFlinger->getFactory().createEffectLayer(
146 LayerCreationArgs(mFlinger.get(), nullptr, mName + " (Mirror)", 0, LayerMetadata()));
147 layer->setInitialValuesForClone(this);
148 return layer;
149 }
150
fillsColor() const151 bool EffectLayer::fillsColor() const {
152 return mDrawingState.color.r >= 0.0_hf && mDrawingState.color.g >= 0.0_hf &&
153 mDrawingState.color.b >= 0.0_hf;
154 }
155
hasBlur() const156 bool EffectLayer::hasBlur() const {
157 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
158 }
159
160 } // namespace android
161
162 // TODO(b/129481165): remove the #pragma below and fix conversion issues
163 #pragma clang diagnostic pop // ignored "-Wconversion"
164