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 // #define LOG_NDEBUG 0
18 #undef LOG_TAG
19 #define LOG_TAG "ColorLayer"
20 
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 
25 #include <utils/Errors.h>
26 #include <utils/Log.h>
27 
28 #include <ui/GraphicBuffer.h>
29 
30 #include "ColorLayer.h"
31 #include "DisplayDevice.h"
32 #include "RenderEngine/RenderEngine.h"
33 #include "SurfaceFlinger.h"
34 
35 namespace android {
36 // ---------------------------------------------------------------------------
37 
ColorLayer(SurfaceFlinger * flinger,const sp<Client> & client,const String8 & name,uint32_t w,uint32_t h,uint32_t flags)38 ColorLayer::ColorLayer(SurfaceFlinger* flinger, const sp<Client>& client, const String8& name,
39                        uint32_t w, uint32_t h, uint32_t flags)
40       : Layer(flinger, client, name, w, h, flags) {
41     // drawing state & current state are identical
42     mDrawingState = mCurrentState;
43 }
44 
onDraw(const RenderArea & renderArea,const Region &,bool useIdentityTransform) const45 void ColorLayer::onDraw(const RenderArea& renderArea, const Region& /* clip */,
46                         bool useIdentityTransform) const {
47     half4 color = getColor();
48     if (color.a > 0) {
49         Mesh mesh(Mesh::TRIANGLE_FAN, 4, 2);
50         computeGeometry(renderArea, mesh, useIdentityTransform);
51         auto& engine(mFlinger->getRenderEngine());
52         engine.setupLayerBlending(getPremultipledAlpha(), false /* opaque */,
53                                   true /* disableTexture */, color);
54         engine.drawMesh(mesh);
55         engine.disableBlending();
56     }
57 }
58 
isVisible() const59 bool ColorLayer::isVisible() const {
60     const Layer::State& s(getDrawingState());
61     return !isHiddenByPolicy() && s.color.a;
62 }
63 
setPerFrameData(const sp<const DisplayDevice> & displayDevice)64 void ColorLayer::setPerFrameData(const sp<const DisplayDevice>& displayDevice) {
65     const Transform& tr = displayDevice->getTransform();
66     const auto& viewport = displayDevice->getViewport();
67     Region visible = tr.transform(visibleRegion.intersect(viewport));
68     auto hwcId = displayDevice->getHwcDisplayId();
69     auto& hwcInfo = getBE().mHwcLayers[hwcId];
70     auto& hwcLayer = hwcInfo.layer;
71     auto error = hwcLayer->setVisibleRegion(visible);
72     if (error != HWC2::Error::None) {
73         ALOGE("[%s] Failed to set visible region: %s (%d)", mName.string(),
74               to_string(error).c_str(), static_cast<int32_t>(error));
75         visible.dump(LOG_TAG);
76     }
77 
78     setCompositionType(hwcId, HWC2::Composition::SolidColor);
79 
80     error = hwcLayer->setDataspace(mCurrentDataSpace);
81     if (error != HWC2::Error::None) {
82         ALOGE("[%s] Failed to set dataspace %d: %s (%d)", mName.string(), mCurrentDataSpace,
83               to_string(error).c_str(), static_cast<int32_t>(error));
84     }
85 
86     half4 color = getColor();
87     error = hwcLayer->setColor({static_cast<uint8_t>(std::round(255.0f * color.r)),
88                                 static_cast<uint8_t>(std::round(255.0f * color.g)),
89                                 static_cast<uint8_t>(std::round(255.0f * color.b)), 255});
90     if (error != HWC2::Error::None) {
91         ALOGE("[%s] Failed to set color: %s (%d)", mName.string(), to_string(error).c_str(),
92               static_cast<int32_t>(error));
93     }
94 
95     // Clear out the transform, because it doesn't make sense absent a source buffer
96     error = hwcLayer->setTransform(HWC2::Transform::None);
97     if (error != HWC2::Error::None) {
98         ALOGE("[%s] Failed to clear transform: %s (%d)", mName.string(), to_string(error).c_str(),
99               static_cast<int32_t>(error));
100     }
101 }
102 
103 // ---------------------------------------------------------------------------
104 
105 }; // namespace android
106