OSDN Git Service

drm_hwcomposer: cache the framebuffers given to the GLWorker
[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() {
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     DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
58     if (!crtc) {
59       ALOGE("Failed to find crtc for display %d", display);
60       return -ENODEV;
61     }
62     int ret = composition_map_[(*iter)->display()]->Init(drm_, crtc, importer_);
63     if (ret) {
64       ALOGE("Failed to init display composition for %d", (*iter)->display());
65       return ret;
66     }
67   }
68   return 0;
69 }
70
71 int DrmComposition::SetLayers(size_t num_displays,
72                               const DrmCompositionDisplayLayersMap *maps) {
73   int ret = 0;
74   for (size_t display_index = 0; display_index < num_displays;
75        display_index++) {
76     const DrmCompositionDisplayLayersMap &map = maps[display_index];
77     int display = map.display;
78
79     ret = composition_map_[display]->SetLayers(
80         map.layers, map.num_layers, map.layer_indices, &primary_planes_,
81         &overlay_planes_);
82     if (ret)
83       return ret;
84   }
85
86   return DisableUnusedPlanes();
87 }
88
89 int DrmComposition::SetDpmsMode(int display, uint32_t dpms_mode) {
90   return composition_map_[display]->SetDpmsMode(dpms_mode);
91 }
92
93 std::unique_ptr<DrmDisplayComposition> DrmComposition::TakeDisplayComposition(
94     int display) {
95   return std::move(composition_map_[display]);
96 }
97
98 int DrmComposition::DisableUnusedPlanes() {
99   for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
100        iter != drm_->end_connectors(); ++iter) {
101     int display = (*iter)->display();
102     DrmDisplayComposition *comp = GetDisplayComposition(display);
103
104     /*
105      * Leave empty compositions alone
106      * TODO: re-visit this and potentially disable leftover planes after the
107      *       active compositions have gobbled up all they can
108      */
109     if (comp->type() == DRM_COMPOSITION_TYPE_EMPTY)
110       continue;
111
112     DrmCrtc *crtc = drm_->GetCrtcForDisplay(display);
113     if (!crtc) {
114       ALOGE("Failed to find crtc for display %d", display);
115       continue;
116     }
117
118     for (std::vector<DrmPlane *>::iterator iter = primary_planes_.begin();
119          iter != primary_planes_.end(); ++iter) {
120       if ((*iter)->GetCrtcSupported(*crtc)) {
121         comp->AddPlaneDisable(*iter);
122         primary_planes_.erase(iter);
123         break;
124       }
125     }
126     for (std::vector<DrmPlane *>::iterator iter = overlay_planes_.begin();
127          iter != overlay_planes_.end();) {
128       if ((*iter)->GetCrtcSupported(*crtc)) {
129         comp->AddPlaneDisable(*iter);
130         iter = overlay_planes_.erase(iter);
131       } else {
132         iter++;
133       }
134     }
135   }
136   return 0;
137 }
138
139 DrmDisplayComposition *DrmComposition::GetDisplayComposition(int display) {
140   return composition_map_[display].get();
141 }
142 }