1 /*
2 * Copyright (C) 2020 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 #include <algorithm>
18
19 #include <compositionengine/impl/ClientCompositionRequestCache.h>
20 #include <renderengine/DisplaySettings.h>
21 #include <renderengine/LayerSettings.h>
22
23 namespace android::compositionengine::impl {
24
25 namespace {
getLayerSettingsSnapshot(const LayerFE::LayerSettings & settings)26 LayerFE::LayerSettings getLayerSettingsSnapshot(const LayerFE::LayerSettings& settings) {
27 LayerFE::LayerSettings snapshot = settings;
28 snapshot.source.buffer.buffer = nullptr;
29 snapshot.source.buffer.fence = nullptr;
30 return snapshot;
31 }
32
equalIgnoringSource(const renderengine::LayerSettings & lhs,const renderengine::LayerSettings & rhs)33 inline bool equalIgnoringSource(const renderengine::LayerSettings& lhs,
34 const renderengine::LayerSettings& rhs) {
35 return lhs.geometry == rhs.geometry && lhs.alpha == rhs.alpha &&
36 lhs.sourceDataspace == rhs.sourceDataspace &&
37 lhs.colorTransform == rhs.colorTransform &&
38 lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow &&
39 lhs.backgroundBlurRadius == rhs.backgroundBlurRadius &&
40 lhs.stretchEffect == rhs.stretchEffect;
41 }
42
equalIgnoringBuffer(const renderengine::Buffer & lhs,const renderengine::Buffer & rhs)43 inline bool equalIgnoringBuffer(const renderengine::Buffer& lhs, const renderengine::Buffer& rhs) {
44 return lhs.useTextureFiltering == rhs.useTextureFiltering &&
45 lhs.textureTransform == rhs.textureTransform &&
46 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
47 lhs.isOpaque == rhs.isOpaque && lhs.maxLuminanceNits == rhs.maxLuminanceNits;
48 }
49
equalIgnoringBuffer(const renderengine::LayerSettings & lhs,const renderengine::LayerSettings & rhs)50 inline bool equalIgnoringBuffer(const renderengine::LayerSettings& lhs,
51 const renderengine::LayerSettings& rhs) {
52 // compare LayerSettings without LayerSettings.PixelSource
53 return equalIgnoringSource(lhs, rhs) &&
54
55 // compare LayerSettings.PixelSource without buffer
56 lhs.source.solidColor == rhs.source.solidColor &&
57
58 // compare LayerSettings.PixelSource.Buffer without buffer & fence
59 equalIgnoringBuffer(lhs.source.buffer, rhs.source.buffer);
60 }
61
layerSettingsAreEqual(const LayerFE::LayerSettings & lhs,const LayerFE::LayerSettings & rhs)62 bool layerSettingsAreEqual(const LayerFE::LayerSettings& lhs, const LayerFE::LayerSettings& rhs) {
63 return lhs.bufferId == rhs.bufferId && lhs.frameNumber == rhs.frameNumber &&
64 equalIgnoringBuffer(lhs, rhs);
65 }
66
67 } // namespace
68
ClientCompositionRequest(const renderengine::DisplaySettings & initDisplay,const std::vector<LayerFE::LayerSettings> & initLayerSettings)69 ClientCompositionRequestCache::ClientCompositionRequest::ClientCompositionRequest(
70 const renderengine::DisplaySettings& initDisplay,
71 const std::vector<LayerFE::LayerSettings>& initLayerSettings)
72 : display(initDisplay) {
73 layerSettings.reserve(initLayerSettings.size());
74 for (const LayerFE::LayerSettings& settings : initLayerSettings) {
75 layerSettings.push_back(getLayerSettingsSnapshot(settings));
76 }
77 }
78
equals(const renderengine::DisplaySettings & newDisplay,const std::vector<LayerFE::LayerSettings> & newLayerSettings) const79 bool ClientCompositionRequestCache::ClientCompositionRequest::equals(
80 const renderengine::DisplaySettings& newDisplay,
81 const std::vector<LayerFE::LayerSettings>& newLayerSettings) const {
82 return newDisplay == display &&
83 std::equal(layerSettings.begin(), layerSettings.end(), newLayerSettings.begin(),
84 newLayerSettings.end(), layerSettingsAreEqual);
85 }
86
exists(uint64_t bufferId,const renderengine::DisplaySettings & display,const std::vector<LayerFE::LayerSettings> & layerSettings) const87 bool ClientCompositionRequestCache::exists(
88 uint64_t bufferId, const renderengine::DisplaySettings& display,
89 const std::vector<LayerFE::LayerSettings>& layerSettings) const {
90 for (const auto& [cachedBufferId, cachedRequest] : mCache) {
91 if (cachedBufferId == bufferId) {
92 return cachedRequest.equals(display, layerSettings);
93 }
94 }
95 return false;
96 }
97
add(uint64_t bufferId,const renderengine::DisplaySettings & display,const std::vector<LayerFE::LayerSettings> & layerSettings)98 void ClientCompositionRequestCache::add(uint64_t bufferId,
99 const renderengine::DisplaySettings& display,
100 const std::vector<LayerFE::LayerSettings>& layerSettings) {
101 const ClientCompositionRequest request(display, layerSettings);
102 for (auto& [cachedBufferId, cachedRequest] : mCache) {
103 if (cachedBufferId == bufferId) {
104 cachedRequest = std::move(request);
105 return;
106 }
107 }
108
109 if (mCache.size() >= mMaxCacheSize) {
110 mCache.pop_front();
111 }
112
113 mCache.emplace_back(bufferId, std::move(request));
114 }
115
remove(uint64_t bufferId)116 void ClientCompositionRequestCache::remove(uint64_t bufferId) {
117 for (auto it = mCache.begin(); it != mCache.end(); it++) {
118 if (it->first == bufferId) {
119 mCache.erase(it);
120 return;
121 }
122 }
123 }
124
125 } // namespace android::compositionengine::impl
126