• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "drmdisplaycomposition.h"
21 #include "drmframebuffer.h"
22 #include "drmhwcomposer.h"
23 #include "resourcemanager.h"
24 #include "vsyncworker.h"
25 
26 #include <pthread.h>
27 #include <memory>
28 #include <sstream>
29 #include <tuple>
30 
31 #include <hardware/hardware.h>
32 #include <hardware/hwcomposer.h>
33 
34 // One for the front, one for the back, and one for cases where we need to
35 // squash a frame that the hw can't display with hw overlays.
36 #define DRM_DISPLAY_BUFFERS 3
37 
38 // If a scene is still for this number of vblanks flatten it to reduce power
39 // consumption.
40 #define FLATTEN_COUNTDOWN_INIT 60
41 
42 namespace android {
43 
44 class DrmDisplayCompositor {
45  public:
46   DrmDisplayCompositor();
47   ~DrmDisplayCompositor();
48 
49   int Init(ResourceManager *resource_manager, int display);
50 
51   std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
52   std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
53   int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
54   int TestComposition(DrmDisplayComposition *composition);
55   int Composite();
56   void Dump(std::ostringstream *out) const;
57   void Vsync(int display, int64_t timestamp);
58   void ClearDisplay();
TakeOutFence()59   int TakeOutFence() {
60     if (!active_composition_)
61       return -1;
62     return active_composition_->take_out_fence();
63   }
64 
65   std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
66 
67  private:
68   struct ModeState {
69     bool needs_modeset = false;
70     DrmMode mode;
71     uint32_t blob_id = 0;
72     uint32_t old_blob_id = 0;
73   };
74 
75   DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
76 
77   // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
78   // kAcquireWaitTries times, logging a warning in between.
79   static const int kAcquireWaitTries = 5;
80   static const int kAcquireWaitTimeoutMs = 100;
81 
82   int CommitFrame(DrmDisplayComposition *display_comp, bool test_only,
83                   DrmConnector *writeback_conn = NULL,
84                   DrmHwcBuffer *writeback_buffer = NULL);
85   int SetupWritebackCommit(drmModeAtomicReqPtr pset, uint32_t crtc_id,
86                            DrmConnector *writeback_conn,
87                            DrmHwcBuffer *writeback_buffer);
88   int ApplyDpms(DrmDisplayComposition *display_comp);
89   int DisablePlanes(DrmDisplayComposition *display_comp);
90 
91   void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
92                   int status, bool writeback = false);
93   int FlattenActiveComposition();
94   int FlattenSerial(DrmConnector *writeback_conn);
95   int FlattenConcurrent(DrmConnector *writeback_conn);
96   int FlattenOnDisplay(std::unique_ptr<DrmDisplayComposition> &src,
97                        DrmConnector *writeback_conn, DrmMode &src_mode,
98                        DrmHwcLayer *writeback_layer);
99 
100   bool CountdownExpired() const;
101 
102   std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
103 
104   ResourceManager *resource_manager_;
105   int display_;
106 
107   std::unique_ptr<DrmDisplayComposition> active_composition_;
108 
109   bool initialized_;
110   bool active_;
111   bool use_hw_overlays_;
112 
113   ModeState mode_;
114 
115   int framebuffer_index_;
116   DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
117 
118   // mutable since we need to acquire in Dump()
119   mutable pthread_mutex_t lock_;
120 
121   // State tracking progress since our last Dump(). These are mutable since
122   // we need to reset them on every Dump() call.
123   mutable uint64_t dump_frames_composited_;
124   mutable uint64_t dump_last_timestamp_ns_;
125   VSyncWorker vsync_worker_;
126   int64_t flatten_countdown_;
127   std::unique_ptr<Planner> planner_;
128   int writeback_fence_;
129 };
130 }  // namespace android
131 
132 #endif  // ANDROID_DRM_DISPLAY_COMPOSITOR_H_
133