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