2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_DRM_DISPLAY_COMPOSITOR_H_
18 #define ANDROID_DRM_DISPLAY_COMPOSITOR_H_
20 #include "drm_hwcomposer.h"
21 #include "drmcomposition.h"
22 #include "drmcompositorworker.h"
23 #include "drmframebuffer.h"
24 #include "seperate_rects.h"
32 #include <hardware/hardware.h>
33 #include <hardware/hwcomposer.h>
35 #define DRM_DISPLAY_BUFFERS 2
39 class GLWorkerCompositor;
43 static const unsigned kHistoryLength = 6;
44 static const unsigned kMaxLayers = 64;
48 std::bitset<kMaxLayers> layer_refs;
49 std::bitset<kHistoryLength> change_history;
50 bool squashed = false;
53 bool is_stable(int region_index) const {
54 return valid_history_ >= kHistoryLength &&
55 regions_[region_index].change_history.none();
58 const std::vector<Region> ®ions() const {
62 void Init(DrmHwcLayer *layers, size_t num_layers);
63 void GenerateHistory(DrmHwcLayer *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,
69 const std::vector<bool> &changed_regions);
70 void RecordSquashed(const std::vector<bool> &squashed_regions);
72 void Dump(std::ostringstream *out) const;
75 size_t generation_number_ = 0;
76 unsigned valid_history_ = 0;
77 std::vector<buffer_handle_t> last_handles_;
79 std::vector<Region> regions_;
82 class DrmDisplayCompositor {
84 DrmDisplayCompositor();
85 ~DrmDisplayCompositor();
87 int Init(DrmResources *drm, int display);
89 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
90 int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition);
92 void Dump(std::ostringstream *out) const;
94 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
96 bool HaveQueuedComposites() const;
98 SquashState *squash_state() {
103 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
105 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
106 // kAcquireWaitTries times, logging a warning in between.
107 static const int kAcquireWaitTries = 5;
108 static const int kAcquireWaitTimeoutMs = 100;
110 int PrepareFramebuffer(DrmFramebuffer &fb,
111 DrmDisplayComposition *display_comp);
112 int ApplyPreComposite(DrmDisplayComposition *display_comp);
113 int ApplyFrame(DrmDisplayComposition *display_comp);
114 int ApplyDpms(DrmDisplayComposition *display_comp);
115 int DisablePlanes(DrmDisplayComposition *display_comp);
120 DrmCompositorWorker worker_;
122 std::queue<std::unique_ptr<DrmDisplayComposition>> composite_queue_;
123 std::unique_ptr<DrmDisplayComposition> active_composition_;
131 int framebuffer_index_;
132 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
133 std::unique_ptr<GLWorkerCompositor> pre_compositor_;
135 // mutable since we need to acquire in HaveQueuedComposites
136 mutable pthread_mutex_t lock_;
138 // State tracking progress since our last Dump(). These are mutable since
139 // we need to reset them on every Dump() call.
140 mutable uint64_t dump_frames_composited_;
141 mutable uint64_t dump_last_timestamp_ns_;
145 #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_