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     std::optional<compositionengine::LayerFE::LayerSettings> shadowSettings =
61             prepareShadowClientComposition(*layerSettings, targetSettings.viewport,
62                                            targetSettings.dataspace);
63     if (shadowSettings) {
64         results.push_back(*shadowSettings);
65     }
66 
67     // If fill bounds are occluded or the fill color is invalid skip the fill settings.
68     if (targetSettings.realContentIsVisible && fillsColor()) {
69         // Set color for color fill settings.
70         layerSettings->source.solidColor = getColor().rgb;
71         results.push_back(*layerSettings);
72     } else if (hasBlur()) {
73         results.push_back(*layerSettings);
74     }
75 
76     return results;
77 }
78 
isVisible() const79 bool EffectLayer::isVisible() const {
80     return !isHiddenByPolicy() && getAlpha() > 0.0_hf && hasSomethingToDraw();
81 }
82 
setColor(const half3 & color)83 bool EffectLayer::setColor(const half3& color) {
84     if (mCurrentState.color.r == color.r && mCurrentState.color.g == color.g &&
85         mCurrentState.color.b == color.b) {
86         return false;
87     }
88 
89     mCurrentState.sequence++;
90     mCurrentState.color.r = color.r;
91     mCurrentState.color.g = color.g;
92     mCurrentState.color.b = color.b;
93     mCurrentState.modified = true;
94     setTransactionFlags(eTransactionNeeded);
95     return true;
96 }
97 
setDataspace(ui::Dataspace dataspace)98 bool EffectLayer::setDataspace(ui::Dataspace dataspace) {
99     if (mCurrentState.dataspace == dataspace) {
100         return false;
101     }
102 
103     mCurrentState.sequence++;
104     mCurrentState.dataspace = dataspace;
105     mCurrentState.modified = true;
106     setTransactionFlags(eTransactionNeeded);
107     return true;
108 }
109 
preparePerFrameCompositionState()110 void EffectLayer::preparePerFrameCompositionState() {
111     Layer::preparePerFrameCompositionState();
112 
113     auto* compositionState = editCompositionState();
114     compositionState->color = getColor();
115     compositionState->compositionType = Hwc2::IComposerClient::Composition::SOLID_COLOR;
116 }
117 
getCompositionEngineLayerFE() const118 sp<compositionengine::LayerFE> EffectLayer::getCompositionEngineLayerFE() const {
119     return asLayerFE();
120 }
121 
editCompositionState()122 compositionengine::LayerFECompositionState* EffectLayer::editCompositionState() {
123     return mCompositionState.get();
124 }
125 
getCompositionState() const126 const compositionengine::LayerFECompositionState* EffectLayer::getCompositionState() const {
127     return mCompositionState.get();
128 }
129 
isOpaque(const Layer::State & s) const130 bool EffectLayer::isOpaque(const Layer::State& s) const {
131     // Consider the layer to be opaque if its opaque flag is set or its effective
132     // alpha (considering the alpha of its parents as well) is 1.0;
133     return (s.flags & layer_state_t::eLayerOpaque) != 0 || getAlpha() == 1.0_hf;
134 }
135 
getDataSpace() const136 ui::Dataspace EffectLayer::getDataSpace() const {
137     return mDrawingState.dataspace;
138 }
139 
createClone()140 sp<Layer> EffectLayer::createClone() {
141     sp<EffectLayer> layer = mFlinger->getFactory().createEffectLayer(
142             LayerCreationArgs(mFlinger.get(), nullptr, mName + " (Mirror)", 0, 0, 0,
143                               LayerMetadata()));
144     layer->setInitialValuesForClone(this);
145     return layer;
146 }
147 
fillsColor() const148 bool EffectLayer::fillsColor() const {
149     return mDrawingState.color.r >= 0.0_hf && mDrawingState.color.g >= 0.0_hf &&
150             mDrawingState.color.b >= 0.0_hf;
151 }
152 
hasBlur() const153 bool EffectLayer::hasBlur() const {
154     return getBackgroundBlurRadius() > 0;
155 }
156 
157 } // namespace android
158 
159 // TODO(b/129481165): remove the #pragma below and fix conversion issues
160 #pragma clang diagnostic pop // ignored "-Wconversion"
161