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 #pragma once
18 
19 #include <cstdint>
20 #include <deque>
21 
22 #include <compositionengine/LayerFE.h>
23 #include <renderengine/DisplaySettings.h>
24 #include <renderengine/LayerSettings.h>
25 
26 namespace android {
27 
28 namespace compositionengine::impl {
29 
30 // The cache is used to skip duplicate client composition requests. We do so by keeping track
31 // of every composition request and the buffer that the request is rendered into. During the
32 // next composition request, if the request matches what was rendered into the buffer, then
33 // we can skip of the request, pass back an empty fence, and let HWC use the previous render
34 // result.
35 //
36 // The cache is a mapping of the RenderSurface buffer id (unique per process) and a snapshot of
37 // the composition request. We need to make sure the request, including the order of the
38 // layers, do not change from call to call. The snapshot removes strong references to the
39 // client buffer id so we don't extend the lifetime of the buffer by storing it in the cache.
40 class ClientCompositionRequestCache {
41 public:
ClientCompositionRequestCache(uint32_t cacheSize)42     explicit ClientCompositionRequestCache(uint32_t cacheSize) : mMaxCacheSize(cacheSize){};
43     ~ClientCompositionRequestCache() = default;
44     bool exists(uint64_t bufferId, const renderengine::DisplaySettings& display,
45                 const std::vector<LayerFE::LayerSettings>& layerSettings) const;
46     void add(uint64_t bufferId, const renderengine::DisplaySettings& display,
47              const std::vector<LayerFE::LayerSettings>& layerSettings);
48     void remove(uint64_t bufferId);
49 
50 private:
51     uint32_t mMaxCacheSize;
52     struct ClientCompositionRequest {
53         renderengine::DisplaySettings display;
54         std::vector<LayerFE::LayerSettings> layerSettings;
55         ClientCompositionRequest(const renderengine::DisplaySettings& _display,
56                                  const std::vector<LayerFE::LayerSettings>& _layerSettings);
57         bool equals(const renderengine::DisplaySettings& _display,
58                     const std::vector<LayerFE::LayerSettings>& _layerSettings) const;
59     };
60 
61     // Cache of requests, keyed by corresponding GraphicBuffer ID.
62     std::deque<std::pair<uint64_t /* bufferId */, ClientCompositionRequest>> mCache;
63 };
64 
65 } // namespace compositionengine::impl
66 } // namespace android
67