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();
59 
60   std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
61 
62  private:
63   struct ModeState {
64     bool needs_modeset = false;
65     DrmMode mode;
66     uint32_t blob_id = 0;
67     uint32_t old_blob_id = 0;
68   };
69 
70   DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
71 
72   // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
73   // kAcquireWaitTries times, logging a warning in between.
74   static const int kAcquireWaitTries = 5;
75   static const int kAcquireWaitTimeoutMs = 100;
76 
77   int CommitFrame(DrmDisplayComposition *display_comp, bool test_only,
78                   DrmConnector *writeback_conn = NULL,
79                   DrmHwcBuffer *writeback_buffer = NULL);
80   int SetupWritebackCommit(drmModeAtomicReqPtr pset, uint32_t crtc_id,
81                            DrmConnector *writeback_conn,
82                            DrmHwcBuffer *writeback_buffer);
83   int ApplyDpms(DrmDisplayComposition *display_comp);
84   int DisablePlanes(DrmDisplayComposition *display_comp);
85 
86   void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
87                   int status, bool writeback = false);
88   int FlattenActiveComposition();
89   int FlattenSerial(DrmConnector *writeback_conn);
90   int FlattenConcurrent(DrmConnector *writeback_conn);
91   int FlattenOnDisplay(std::unique_ptr<DrmDisplayComposition> &src,
92                        DrmConnector *writeback_conn, DrmMode &src_mode,
93                        DrmHwcLayer *writeback_layer);
94 
95   bool CountdownExpired() const;
96 
97   std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
98 
99   ResourceManager *resource_manager_;
100   int display_;
101 
102   std::unique_ptr<DrmDisplayComposition> active_composition_;
103 
104   bool initialized_;
105   bool active_;
106   bool use_hw_overlays_;
107 
108   ModeState mode_;
109 
110   int framebuffer_index_;
111   DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
112 
113   // mutable since we need to acquire in Dump()
114   mutable pthread_mutex_t lock_;
115 
116   // State tracking progress since our last Dump(). These are mutable since
117   // we need to reset them on every Dump() call.
118   mutable uint64_t dump_frames_composited_;
119   mutable uint64_t dump_last_timestamp_ns_;
120   VSyncWorker vsync_worker_;
121   int64_t flatten_countdown_;
122   std::unique_ptr<Planner> planner_;
123   int writeback_fence_;
124 };
125 }  // namespace android
126 
127 #endif  // ANDROID_DRM_DISPLAY_COMPOSITOR_H_
128