| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 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 { | 
|  | 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 |  | 
|  | 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 && | 
| Lucas Dupin | d011f50 | 2020-03-22 18:34:51 -0700 | [diff] [blame] | 38 | lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow && | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 39 | lhs.backgroundBlurRadius == rhs.backgroundBlurRadius && | 
|  | 40 | lhs.stretchEffect == rhs.stretchEffect; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 | inline 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 && | 
| Alec Mouri | 994761f | 2023-08-04 21:50:55 +0000 | [diff] [blame] | 48 | lhs.isOpaque == rhs.isOpaque && lhs.maxLuminanceNits == rhs.maxLuminanceNits; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 49 | } | 
|  | 50 |  | 
|  | 51 | inline 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 |  | 
|  | 63 | bool 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 |  | 
|  | 70 | ClientCompositionRequestCache::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 |  | 
|  | 80 | bool 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 |  | 
|  | 88 | bool 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 |  | 
|  | 99 | void 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 |  | 
|  | 117 | void 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 |