OSDN Git Service

Merge "drm_hwcomposer: ground work for squashing" into mnc-dr-dev
[android-x86/external-drm_hwcomposer.git] / drmcomposition.cpp
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 #define LOG_TAG "hwc-drm-composition"
18
19 #include "drmcomposition.h"
20 #include "drmcrtc.h"
21 #include "drmplane.h"
22 #include "drmresources.h"
23
24 #include <stdlib.h>
25
26 #include <cutils/log.h>
27 #include <cutils/properties.h>
28 #include <sw_sync.h>
29 #include <sync/sync.h>
30
31 namespace android {
32
33 DrmComposition::DrmComposition(DrmResources *drm, Importer *importer)
34     : drm_(drm), importer_(importer) {
35   char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
36   property_get("hwc.drm.use_overlay_planes", use_overlay_planes_prop, "1");
37   bool use_overlay_planes = atoi(use_overlay_planes_prop);
38
39   for (DrmResources::PlaneIter iter = drm_->begin_planes();
40        iter != drm_->end_planes(); ++iter) {
41     if ((*iter)->type() == DRM_PLANE_TYPE_PRIMARY)
42       primary_planes_.push_back(*iter);
43     else if (use_overlay_planes && (*iter)->type() == DRM_PLANE_TYPE_OVERLAY)
44       overlay_planes_.push_back(*iter);
45   }
46 }
47
48 int DrmComposition::Init(uint64_t frame_no) {
49   for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
50        iter != drm_->end_connectors(); ++iter) {
51     int display = (*iter)->display();
52     composition_map_[display].reset(new DrmDisplayComposition());
53     if (!composition_map_[display]) {
54       ALOGE("Failed to allocate new display composition\n");
55       return -ENOMEM;
56     }
57
58     // If the display hasn't been modeset yet, this will be NULL
59     DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
60
61     int ret = composition_map_[display]->Init(drm_, crtc, importer_, frame_no);
62     if (ret) {
63       ALOGE("Failed to init display composition for %d", display);
64       return ret;
65     }
66   }
67   return 0;
68 }
69
70 int DrmComposition::SetLayers(size_t num_displays,
71                               DrmCompositionDisplayLayersMap *maps) {
72   int ret = 0;
73   for (size_t display_index = 0; display_index < num_displays;
74        display_index++) {
75     DrmCompositionDisplayLayersMap &map = maps[display_index];
76     int display = map.display;
77
78     ret = composition_map_[display]->SetLayers(map.layers.data(),
79                                                map.layers.size());
80     if (ret)
81       return ret;
82   }
83
84   return 0;
85 }
86
87 int DrmComposition::SetDpmsMode(int display, uint32_t dpms_mode) {
88   return composition_map_[display]->SetDpmsMode(dpms_mode);
89 }
90
91 int DrmComposition::SetDisplayMode(int display, const DrmMode &display_mode) {
92   return composition_map_[display]->SetDisplayMode(display_mode);
93 }
94
95 std::unique_ptr<DrmDisplayComposition> DrmComposition::TakeDisplayComposition(
96     int display) {
97   return std::move(composition_map_[display]);
98 }
99
100 int DrmComposition::Plan(std::map<int, DrmDisplayCompositor> &compositor_map) {
101   int ret = 0;
102   for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
103        iter != drm_->end_connectors(); ++iter) {
104     int display = (*iter)->display();
105     DrmDisplayComposition *comp = GetDisplayComposition(display);
106     ret = comp->Plan(compositor_map[display].squash_state(), &primary_planes_,
107                      &overlay_planes_);
108     if (ret) {
109       ALOGE("Failed to plan composition for dislay %d", display);
110       return ret;
111     }
112   }
113
114   return 0;
115 }
116
117 int DrmComposition::DisableUnusedPlanes() {
118   for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
119        iter != drm_->end_connectors(); ++iter) {
120     int display = (*iter)->display();
121     DrmDisplayComposition *comp = GetDisplayComposition(display);
122
123     /*
124      * Leave empty compositions alone
125      * TODO: re-visit this and potentially disable leftover planes after the
126      *       active compositions have gobbled up all they can
127      */
128     if (comp->type() == DRM_COMPOSITION_TYPE_EMPTY ||
129         comp->type() == DRM_COMPOSITION_TYPE_MODESET)
130       continue;
131
132     DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
133     if (!crtc) {
134       ALOGE("Failed to find crtc for display %d", display);
135       continue;
136     }
137
138     for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin();
139          iter != primary_planes_.end(); ++iter) {
140       if ((*iter)->GetCrtcSupported(*crtc)) {
141         comp->AddPlaneDisable(*iter);
142         primary_planes_.erase(iter);
143         break;
144       }
145     }
146     for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin();
147          iter != overlay_planes_.end();) {
148       if ((*iter)->GetCrtcSupported(*crtc)) {
149         comp->AddPlaneDisable(*iter);
150         iter = overlay_planes_.erase(iter);
151       } else {
152         iter++;
153       }
154     }
155   }
156   return 0;
157 }
158
159 DrmDisplayComposition *DrmComposition::GetDisplayComposition(int display) {
160   return composition_map_[display].get();
161 }
162 }