blob: bdaa1d0ae10c7bea6174e099c338ad56e6898867 [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) {
Alec Mouric3a17822023-08-16 14:56:30 +000044 return lhs.useTextureFiltering == rhs.useTextureFiltering &&
Vishnu Nair9b079a22020-01-21 14:36:08 -080045 lhs.textureTransform == rhs.textureTransform &&
46 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
Alec Mouri994761f2023-08-04 21:50:55 +000047 lhs.isOpaque == rhs.isOpaque && lhs.maxLuminanceNits == rhs.maxLuminanceNits;
Vishnu Nair9b079a22020-01-21 14:36:08 -080048}
49
50inline 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
62bool 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
69ClientCompositionRequestCache::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
79bool 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
87bool 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
98void 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
116void 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