blob: b1ee3fbf5d316a8622c644b65958ddea02a48384 [file] [log] [blame]
Vishnu Nair9b079a22020-01-21 14:36:08 -08001/*
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
23namespace android::compositionengine::impl {
24
25namespace {
26LayerFE::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
33inline 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 &&
Lucas Dupind011f502020-03-22 18:34:51 -070038 lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow &&
Nader Jawad2dfc98b2021-04-08 20:35:39 -070039 lhs.backgroundBlurRadius == rhs.backgroundBlurRadius &&
40 lhs.stretchEffect == rhs.stretchEffect;
Vishnu Nair9b079a22020-01-21 14:36:08 -080041}
42
43inline bool equalIgnoringBuffer(const renderengine::Buffer& lhs, const renderengine::Buffer& rhs) {
44 return lhs.textureName == rhs.textureName &&
45 lhs.useTextureFiltering == rhs.useTextureFiltering &&
46 lhs.textureTransform == rhs.textureTransform &&
47 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
48 lhs.isOpaque == rhs.isOpaque && lhs.isY410BT2020 == rhs.isY410BT2020 &&
49 lhs.maxMasteringLuminance == rhs.maxMasteringLuminance &&
50 lhs.maxContentLuminance == rhs.maxContentLuminance;
51}
52
53inline bool equalIgnoringBuffer(const renderengine::LayerSettings& lhs,
54 const renderengine::LayerSettings& rhs) {
55 // compare LayerSettings without LayerSettings.PixelSource
56 return equalIgnoringSource(lhs, rhs) &&
57
58 // compare LayerSettings.PixelSource without buffer
59 lhs.source.solidColor == rhs.source.solidColor &&
60
61 // compare LayerSettings.PixelSource.Buffer without buffer & fence
62 equalIgnoringBuffer(lhs.source.buffer, rhs.source.buffer);
63}
64
65bool layerSettingsAreEqual(const LayerFE::LayerSettings& lhs, const LayerFE::LayerSettings& rhs) {
66 return lhs.bufferId == rhs.bufferId && lhs.frameNumber == rhs.frameNumber &&
67 equalIgnoringBuffer(lhs, rhs);
68}
69
70} // namespace
71
72ClientCompositionRequestCache::ClientCompositionRequest::ClientCompositionRequest(
73 const renderengine::DisplaySettings& initDisplay,
74 const std::vector<LayerFE::LayerSettings>& initLayerSettings)
75 : display(initDisplay) {
76 layerSettings.reserve(initLayerSettings.size());
77 for (const LayerFE::LayerSettings& settings : initLayerSettings) {
78 layerSettings.push_back(getLayerSettingsSnapshot(settings));
79 }
80}
81
82bool ClientCompositionRequestCache::ClientCompositionRequest::equals(
83 const renderengine::DisplaySettings& newDisplay,
84 const std::vector<LayerFE::LayerSettings>& newLayerSettings) const {
85 return newDisplay == display &&
86 std::equal(layerSettings.begin(), layerSettings.end(), newLayerSettings.begin(),
87 newLayerSettings.end(), layerSettingsAreEqual);
88}
89
90bool ClientCompositionRequestCache::exists(
91 uint64_t bufferId, const renderengine::DisplaySettings& display,
92 const std::vector<LayerFE::LayerSettings>& layerSettings) const {
93 for (const auto& [cachedBufferId, cachedRequest] : mCache) {
94 if (cachedBufferId == bufferId) {
95 return cachedRequest.equals(display, layerSettings);
96 }
97 }
98 return false;
99}
100
101void ClientCompositionRequestCache::add(uint64_t bufferId,
102 const renderengine::DisplaySettings& display,
103 const std::vector<LayerFE::LayerSettings>& layerSettings) {
104 const ClientCompositionRequest request(display, layerSettings);
105 for (auto& [cachedBufferId, cachedRequest] : mCache) {
106 if (cachedBufferId == bufferId) {
107 cachedRequest = std::move(request);
108 return;
109 }
110 }
111
112 if (mCache.size() >= mMaxCacheSize) {
113 mCache.pop_front();
114 }
115
116 mCache.emplace_back(bufferId, std::move(request));
117}
118
119void ClientCompositionRequestCache::remove(uint64_t bufferId) {
120 for (auto it = mCache.begin(); it != mCache.end(); it++) {
121 if (it->first == bufferId) {
122 mCache.erase(it);
123 return;
124 }
125 }
126}
127
128} // namespace android::compositionengine::impl