OSDN Git Service

drm_hwcomposer: Mark tests as vendor, fix build
[android-x86/external-drm_hwcomposer.git] / drmdisplaycompositor.h
1 /*
2  * Copyright (C) 2015 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 #ifndef ANDROID_DRM_DISPLAY_COMPOSITOR_H_
18 #define ANDROID_DRM_DISPLAY_COMPOSITOR_H_
19
20 #include "drmhwcomposer.h"
21 #include "drmdisplaycomposition.h"
22 #include "drmframebuffer.h"
23 #include "separate_rects.h"
24
25 #include <pthread.h>
26 #include <memory>
27 #include <sstream>
28 #include <tuple>
29
30 #include <hardware/hardware.h>
31 #include <hardware/hwcomposer.h>
32
33 // One for the front, one for the back, and one for cases where we need to
34 // squash a frame that the hw can't display with hw overlays.
35 #define DRM_DISPLAY_BUFFERS 3
36
37 namespace android {
38
39 class GLWorkerCompositor;
40
41 class SquashState {
42  public:
43   static const unsigned kHistoryLength = 6;  // TODO: make this number not magic
44   static const unsigned kMaxLayers = 64;
45
46   struct Region {
47     DrmHwcRect<int> rect;
48     std::bitset<kMaxLayers> layer_refs;
49     std::bitset<kHistoryLength> change_history;
50     bool squashed = false;
51   };
52
53   bool is_stable(int region_index) const {
54     return valid_history_ >= kHistoryLength &&
55            regions_[region_index].change_history.none();
56   }
57
58   const std::vector<Region> &regions() const {
59     return regions_;
60   }
61
62   void Init(DrmHwcLayer *layers, size_t num_layers);
63   void GenerateHistory(DrmHwcLayer *layers, size_t num_layers,
64                        std::vector<bool> &changed_regions) const;
65   void StableRegionsWithMarginalHistory(
66       const std::vector<bool> &changed_regions,
67       std::vector<bool> &stable_regions) const;
68   void RecordHistory(DrmHwcLayer *layers, size_t num_layers,
69                      const std::vector<bool> &changed_regions);
70   bool RecordAndCompareSquashed(const std::vector<bool> &squashed_regions);
71
72   void Dump(std::ostringstream *out) const;
73
74  private:
75   size_t generation_number_ = 0;
76   unsigned valid_history_ = 0;
77   std::vector<buffer_handle_t> last_handles_;
78
79   std::vector<Region> regions_;
80 };
81
82 class DrmDisplayCompositor {
83  public:
84   DrmDisplayCompositor();
85   ~DrmDisplayCompositor();
86
87   int Init(DrmResources *drm, int display);
88
89   std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
90   int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
91   int Composite();
92   int SquashAll();
93   void Dump(std::ostringstream *out) const;
94
95   std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
96
97   SquashState *squash_state() {
98     return &squash_state_;
99   }
100
101   bool uses_GL() {
102     return !!pre_compositor_;
103   }
104  private:
105   struct ModeState {
106     bool needs_modeset = false;
107     DrmMode mode;
108     uint32_t blob_id = 0;
109     uint32_t old_blob_id = 0;
110   };
111
112   DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
113
114   // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
115   // kAcquireWaitTries times, logging a warning in between.
116   static const int kAcquireWaitTries = 5;
117   static const int kAcquireWaitTimeoutMs = 100;
118
119   int PrepareFramebuffer(DrmFramebuffer &fb,
120                          DrmDisplayComposition *display_comp);
121   int ApplySquash(DrmDisplayComposition *display_comp);
122   int ApplyPreComposite(DrmDisplayComposition *display_comp);
123   int PrepareFrame(DrmDisplayComposition *display_comp);
124   int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
125   int SquashFrame(DrmDisplayComposition *src, DrmDisplayComposition *dst);
126   int ApplyDpms(DrmDisplayComposition *display_comp);
127   int DisablePlanes(DrmDisplayComposition *display_comp);
128
129   void ClearDisplay();
130   void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
131                   int status);
132
133   std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
134
135   DrmResources *drm_;
136   int display_;
137
138   std::unique_ptr<DrmDisplayComposition> active_composition_;
139
140   bool initialized_;
141   bool active_;
142   bool use_hw_overlays_;
143
144   ModeState mode_;
145
146   int framebuffer_index_;
147   DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
148   std::unique_ptr<GLWorkerCompositor> pre_compositor_;
149
150   SquashState squash_state_;
151   int squash_framebuffer_index_;
152   DrmFramebuffer squash_framebuffers_[2];
153
154   // mutable since we need to acquire in Dump()
155   mutable pthread_mutex_t lock_;
156
157   // State tracking progress since our last Dump(). These are mutable since
158   // we need to reset them on every Dump() call.
159   mutable uint64_t dump_frames_composited_;
160   mutable uint64_t dump_last_timestamp_ns_;
161 };
162 }
163
164 #endif  // ANDROID_DRM_DISPLAY_COMPOSITOR_H_