1 /*
2  * Copyright 2021 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 <compositionengine/Output.h>
20 #include <compositionengine/impl/planner/Flattener.h>
21 #include <compositionengine/impl/planner/LayerState.h>
22 #include <compositionengine/impl/planner/Predictor.h>
23 #include <utils/String16.h>
24 #include <utils/Vector.h>
25 
26 #include <optional>
27 #include <string>
28 #include <unordered_map>
29 
30 namespace android {
31 
32 namespace renderengine {
33 class RenderEngine;
34 } // namespace renderengine
35 
36 namespace compositionengine::impl::planner {
37 
38 // This is the top level class for layer caching. It is responsible for
39 // heuristically determining the composition strategy of the current layer stack,
40 // and flattens inactive layers into an override buffer so it can be used
41 // as a more efficient representation of parts of the layer stack.
42 // Implicitly, layer caching must also be enabled for the Planner to have any effect
43 // E.g., setprop debug.sf.enable_layer_caching 1, or
44 // adb shell service call SurfaceFlinger 1040 i32 1 [i64 <display ID>]
45 class Planner {
46 public:
47     Planner(renderengine::RenderEngine& renderengine);
48 
49     void setDisplaySize(ui::Size);
50 
51     // Updates the Planner with the current set of layers before a composition strategy is
52     // determined.
53     // The Planner will call to the Flattener to determine to:
54     // 1. Replace any cached sets with a newly available flattened cached set
55     // 2. Create a new cached set if possible
56     void plan(
57             compositionengine::Output::OutputLayersEnumerator<compositionengine::Output>&& layers);
58 
59     // Updates the Planner with the current set of layers after a composition strategy is
60     // determined.
61     void reportFinalPlan(
62             compositionengine::Output::OutputLayersEnumerator<compositionengine::Output>&& layers);
63 
64     // The planner will call to the Flattener to render any pending cached set.
65     // Rendering a pending cached set is optional: if the renderDeadline is not far enough in the
66     // future then the planner may opt to skip rendering the cached set.
67     void renderCachedSets(const OutputCompositionState& outputState,
68                           std::optional<std::chrono::steady_clock::time_point> renderDeadline,
69                           bool deviceHandlesColorTransform);
70 
setTexturePoolEnabled(bool enabled)71     void setTexturePoolEnabled(bool enabled) { mFlattener.setTexturePoolEnabled(enabled); }
72 
73     void dump(const Vector<String16>& args, std::string&);
74 
75 private:
76     void dumpUsage(std::string&) const;
77 
78     std::unordered_map<LayerId, LayerState> mPreviousLayers;
79 
80     std::vector<const LayerState*> mCurrentLayers;
81 
82     Predictor mPredictor;
83     Flattener mFlattener;
84 
85     std::optional<Predictor::PredictedPlan> mPredictedPlan;
86     NonBufferHash mFlattenedHash = 0;
87 
88     bool mPredictorEnabled = false;
89 };
90 
91 } // namespace compositionengine::impl::planner
92 } // namespace android
93