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 = Hwc2::IComposerClient::Composition::SOLID_COLOR;
113 }
114
getCompositionEngineLayerFE() const115 sp<compositionengine::LayerFE> EffectLayer::getCompositionEngineLayerFE() const {
116 return asLayerFE();
117 }
118
editCompositionState()119 compositionengine::LayerFECompositionState* EffectLayer::editCompositionState() {
120 return mCompositionState.get();
121 }
122
getCompositionState() const123 const compositionengine::LayerFECompositionState* EffectLayer::getCompositionState() const {
124 return mCompositionState.get();
125 }
126
isOpaque(const Layer::State & s) const127 bool EffectLayer::isOpaque(const Layer::State& s) const {
128 // Consider the layer to be opaque if its opaque flag is set or its effective
129 // alpha (considering the alpha of its parents as well) is 1.0;
130 return (s.flags & layer_state_t::eLayerOpaque) != 0 || (fillsColor() && getAlpha() == 1.0_hf);
131 }
132
getDataSpace() const133 ui::Dataspace EffectLayer::getDataSpace() const {
134 return mDrawingState.dataspace;
135 }
136
createClone()137 sp<Layer> EffectLayer::createClone() {
138 sp<EffectLayer> layer = mFlinger->getFactory().createEffectLayer(
139 LayerCreationArgs(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0,
140 LayerMetadata()));
141 layer->setInitialValuesForClone(this);
142 return layer;
143 }
144
fillsColor() const145 bool EffectLayer::fillsColor() const {
146 return mDrawingState.color.r >= 0.0_hf && mDrawingState.color.g >= 0.0_hf &&
147 mDrawingState.color.b >= 0.0_hf;
148 }
149
hasBlur() const150 bool EffectLayer::hasBlur() const {
151 return getBackgroundBlurRadius() > 0 || getDrawingState().blurRegions.size() > 0;
152 }
153
154 } // namespace android
155
156 // TODO(b/129481165): remove the #pragma below and fix conversion issues
157 #pragma clang diagnostic pop // ignored "-Wconversion"
158