blob: 1866d43ac7e5db8fb201b576e3a5f0dc02854d26 [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
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
Zach Reizner7642c922015-10-29 10:11:16 -070020#include "drmhwcomposer.h"
Sean Pauled45a8e2017-02-28 13:17:34 -050021#include "drmdisplaycomposition.h"
Zach Reizner713a6782015-07-31 15:12:44 -070022#include "drmframebuffer.h"
Sean Paul98e73c82015-06-24 14:38:49 -070023
24#include <pthread.h>
Zach Reizner92f8e632015-10-12 17:47:13 -070025#include <memory>
Sean Paul98e73c82015-06-24 14:38:49 -070026#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070027#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070028
29#include <hardware/hardware.h>
30#include <hardware/hwcomposer.h>
31
Sean Paul647beb22015-11-19 13:55:48 -050032// One for the front, one for the back, and one for cases where we need to
33// squash a frame that the hw can't display with hw overlays.
34#define DRM_DISPLAY_BUFFERS 3
Zach Reizner713a6782015-07-31 15:12:44 -070035
Sean Paul98e73c82015-06-24 14:38:49 -070036namespace android {
37
38class DrmDisplayCompositor {
39 public:
40 DrmDisplayCompositor();
41 ~DrmDisplayCompositor();
42
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010043 int Init(DrmDevice *drm, int display);
Sean Paul98e73c82015-06-24 14:38:49 -070044
Zach Reizner92f8e632015-10-12 17:47:13 -070045 std::unique_ptr<DrmDisplayComposition> CreateComposition() const;
Sean Pauled45a8e2017-02-28 13:17:34 -050046 int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition);
Rob Herring4f6c62e2018-05-17 14:33:02 -050047 int TestComposition(DrmDisplayComposition *composition);
Sean Paul98e73c82015-06-24 14:38:49 -070048 int Composite();
49 void Dump(std::ostringstream *out) const;
50
Zach Reizner92f8e632015-10-12 17:47:13 -070051 std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution();
52
Sean Paul98e73c82015-06-24 14:38:49 -070053 private:
Sean Paul35301f42015-11-17 14:46:56 -050054 struct ModeState {
55 bool needs_modeset = false;
56 DrmMode mode;
57 uint32_t blob_id = 0;
58 uint32_t old_blob_id = 0;
59 };
60
Sean Paul98e73c82015-06-24 14:38:49 -070061 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
62
Sean Paul971be152015-10-13 15:44:45 -040063 // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs,
64 // kAcquireWaitTries times, logging a warning in between.
65 static const int kAcquireWaitTries = 5;
66 static const int kAcquireWaitTimeoutMs = 100;
Sean Pauld106b912015-09-29 00:56:00 -040067
Zach Reizner92f8e632015-10-12 17:47:13 -070068 int PrepareFramebuffer(DrmFramebuffer &fb,
69 DrmDisplayComposition *display_comp);
Haixia Shidda2fab2015-10-22 18:12:49 -070070 int PrepareFrame(DrmDisplayComposition *display_comp);
Sean Paulc07b2112015-11-17 16:38:10 -050071 int CommitFrame(DrmDisplayComposition *display_comp, bool test_only);
Sean Pauldb7a17d2015-06-24 18:46:05 -070072 int ApplyDpms(DrmDisplayComposition *display_comp);
Sean Paul7b1e4bc2015-10-13 15:47:22 -040073 int DisablePlanes(DrmDisplayComposition *display_comp);
Sean Paul98e73c82015-06-24 14:38:49 -070074
Sean Paul137a6a82016-06-22 22:48:22 -040075 void ClearDisplay();
Haixia Shidda2fab2015-10-22 18:12:49 -070076 void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition,
77 int status);
78
Sean Paul35301f42015-11-17 14:46:56 -050079 std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode);
80
Alexandru Gheorghe0f5abd72018-05-01 14:37:10 +010081 DrmDevice *drm_;
Sean Paul98e73c82015-06-24 14:38:49 -070082 int display_;
83
Sean Paul98e73c82015-06-24 14:38:49 -070084 std::unique_ptr<DrmDisplayComposition> active_composition_;
85
Sean Paul98e73c82015-06-24 14:38:49 -070086 bool initialized_;
Sean Pauldb7a17d2015-06-24 18:46:05 -070087 bool active_;
Sean Paul6c18b3b2015-11-25 11:04:25 -050088 bool use_hw_overlays_;
Sean Paul98e73c82015-06-24 14:38:49 -070089
Sean Paul35301f42015-11-17 14:46:56 -050090 ModeState mode_;
Sean Paul57355412015-09-19 09:14:34 -040091
Zach Reizner713a6782015-07-31 15:12:44 -070092 int framebuffer_index_;
93 DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS];
Zach Reizner5757e822015-10-16 19:06:31 -070094
Sean Pauled45a8e2017-02-28 13:17:34 -050095 // mutable since we need to acquire in Dump()
Sean Paul98e73c82015-06-24 14:38:49 -070096 mutable pthread_mutex_t lock_;
97
98 // State tracking progress since our last Dump(). These are mutable since
99 // we need to reset them on every Dump() call.
100 mutable uint64_t dump_frames_composited_;
101 mutable uint64_t dump_last_timestamp_ns_;
102};
103}
104
105#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_