blob: a00c95fdf3f826c45ced637ef708adacf28feccb [file] [log] [blame]
Sean Paule0c4c3d2015-01-20 16:56:04 -05001/*
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
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Sean Paule0c4c3d2015-01-20 16:56:04 -050018#define LOG_TAG "hwcomposer-drm"
19
Sean Paulef8f1f92015-04-29 16:05:23 -040020#include "drm_hwcomposer.h"
Sean Paul6a55e9f2015-04-30 15:31:06 -040021#include "drmresources.h"
Sean Paulda6270d2015-06-01 14:11:52 -040022#include "importer.h"
Sean Paul4057be32015-05-13 06:23:09 -070023#include "vsyncworker.h"
Sean Paulef8f1f92015-04-29 16:05:23 -040024
Zach Reizner09807052015-08-13 14:53:41 -070025#include <stdlib.h>
26
27#include <map>
28#include <vector>
29
Sean Paule0c4c3d2015-01-20 16:56:04 -050030#include <errno.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040031#include <fcntl.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040032#include <pthread.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050033#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050034#include <sys/resource.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050035#include <xf86drm.h>
36#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050037
Sean Paulef8f1f92015-04-29 16:05:23 -040038#include <cutils/log.h>
39#include <cutils/properties.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050040#include <hardware/hardware.h>
41#include <hardware/hwcomposer.h>
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070042#include <utils/Trace.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050043
Sean Paule0c4c3d2015-01-20 16:56:04 -050044#define UM_PER_INCH 25400
Stéphane Marchesincb3f9842015-06-19 14:50:45 -070045#define HWC_FB_BUFFERS 3
Sean Paule0c4c3d2015-01-20 16:56:04 -050046
Sean Paul6a55e9f2015-04-30 15:31:06 -040047namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050048
Sean Paule42febf2015-05-07 11:35:29 -070049typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -040050 struct hwc_context_t *ctx;
51 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -050052
Sean Paul6a55e9f2015-04-30 15:31:06 -040053 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -050054
Sean Paul4057be32015-05-13 06:23:09 -070055 VSyncWorker vsync_worker;
Sean Paule42febf2015-05-07 11:35:29 -070056} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -050057
58struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -070059 // map of display:hwc_drm_display_t
60 typedef std::map<int, hwc_drm_display_t> DisplayMap;
61 typedef DisplayMap::iterator DisplayMapIter;
Sean Paule0c4c3d2015-01-20 16:56:04 -050062
Sean Paulda6270d2015-06-01 14:11:52 -040063 hwc_context_t() : procs(NULL), importer(NULL) {
64 }
65
66 ~hwc_context_t() {
67 delete importer;
68 }
69
Sean Paule42febf2015-05-07 11:35:29 -070070 hwc_composer_device_1_t device;
Sean Paulef8f1f92015-04-29 16:05:23 -040071 hwc_procs_t const *procs;
Sean Paule0c4c3d2015-01-20 16:56:04 -050072
Sean Paule42febf2015-05-07 11:35:29 -070073 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -040074 DrmResources drm;
Sean Paulda6270d2015-06-01 14:11:52 -040075 Importer *importer;
Sean Paule0c4c3d2015-01-20 16:56:04 -050076};
77
Zach Reiznerc6520e42015-08-13 14:32:09 -070078static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -040079 int buff_len) {
80 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
81 std::ostringstream out;
82
83 ctx->drm.compositor()->Dump(&out);
84 std::string out_str = out.str();
85 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
86}
87
Sean Paulb386f1b2015-05-13 06:33:23 -070088static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -040089 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -070090 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -070091 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -040092 if (!display_contents[i])
93 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -050094
Sean Paulb386f1b2015-05-13 06:33:23 -070095 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
96 if (!crtc) {
97 ALOGE("No crtc for display %d", i);
Sean Paulb386f1b2015-05-13 06:33:23 -070098 return -ENODEV;
99 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700100
Zach Reizner45624d32015-06-10 16:03:01 -0700101 int num_layers = display_contents[i]->numHwLayers;
102 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700103 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700104
Sean Paul877be972015-06-03 14:08:27 -0400105 if (layer->compositionType == HWC_FRAMEBUFFER)
106 layer->compositionType = HWC_OVERLAY;
Sean Paulef8f1f92015-04-29 16:05:23 -0400107 }
108 }
Sean Pauldffca952015-02-04 10:19:55 -0800109
Sean Paulef8f1f92015-04-29 16:05:23 -0400110 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500111}
112
Sean Paulb386f1b2015-05-13 06:33:23 -0700113static void hwc_set_cleanup(size_t num_displays,
Zach Reizner09807052015-08-13 14:53:41 -0700114 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700115 for (int i = 0; i < (int)num_displays; ++i) {
116 if (!display_contents[i])
117 continue;
118
119 hwc_display_contents_1_t *dc = display_contents[i];
120 for (size_t j = 0; j < dc->numHwLayers; ++j) {
121 hwc_layer_1_t *layer = &dc->hwLayers[j];
122 if (layer->acquireFenceFd >= 0) {
123 close(layer->acquireFenceFd);
124 layer->acquireFenceFd = -1;
125 }
126 }
127 if (dc->outbufAcquireFenceFd >= 0) {
128 close(dc->outbufAcquireFenceFd);
129 dc->outbufAcquireFenceFd = -1;
130 }
131 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700132}
133
Zach Reizner09807052015-08-13 14:53:41 -0700134static void hwc_add_layer_to_retire_fence(
135 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400136 if (layer->releaseFenceFd < 0)
137 return;
138
139 if (display_contents->retireFenceFd >= 0) {
140 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700141 display_contents->retireFenceFd =
142 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400143 close(old_retire_fence);
144 } else {
145 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
146 }
147}
148
Sean Paule0c4c3d2015-01-20 16:56:04 -0500149static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400150 hwc_display_contents_1_t **display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700151 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400152 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner09807052015-08-13 14:53:41 -0700153 int ret;
154 std::unique_ptr<DrmComposition> composition(
155 ctx->drm.compositor()->CreateComposition(ctx->importer));
Sean Paulb386f1b2015-05-13 06:33:23 -0700156 if (!composition) {
157 ALOGE("Drm composition init failed");
Zach Reizner09807052015-08-13 14:53:41 -0700158 hwc_set_cleanup(num_displays, display_contents);
Sean Paulb386f1b2015-05-13 06:33:23 -0700159 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400160 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500161
Zach Reizner09807052015-08-13 14:53:41 -0700162 std::vector<DrmCompositionDisplayLayersMap> layers_map;
163 std::vector<std::vector<size_t>> layers_indices;
164 layers_map.reserve(num_displays);
165 layers_indices.reserve(num_displays);
166
Sean Paulb386f1b2015-05-13 06:33:23 -0700167 for (int i = 0; i < (int)num_displays; ++i) {
168 if (!display_contents[i])
169 continue;
Zach Reizner45624d32015-06-10 16:03:01 -0700170 hwc_display_contents_1_t *dc = display_contents[i];
Zach Reizner09807052015-08-13 14:53:41 -0700171
172 layers_map.emplace_back();
173 DrmCompositionDisplayLayersMap &map = layers_map[i];
174 map.display = i;
175 map.layers = dc->hwLayers;
176
177 std::vector<size_t> indices_to_composite;
Zach Reizner45624d32015-06-10 16:03:01 -0700178 unsigned num_dc_layers = dc->numHwLayers;
Zach Reizner09807052015-08-13 14:53:41 -0700179 for (int j = 0; j < (int)num_dc_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700180 hwc_layer_1_t *layer = &dc->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700181 if (layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700182 continue;
Sean Paul877be972015-06-03 14:08:27 -0400183 if (layer->compositionType == HWC_OVERLAY)
Zach Reizner09807052015-08-13 14:53:41 -0700184 indices_to_composite.push_back(j);
Zach Reizner45624d32015-06-10 16:03:01 -0700185 }
186
Zach Reizner09807052015-08-13 14:53:41 -0700187 map.num_layers = indices_to_composite.size();
188 layers_indices.emplace_back(std::move(indices_to_composite));
189 map.layer_indices = layers_indices.back().data();
190 }
Zach Reizner45624d32015-06-10 16:03:01 -0700191
Zach Reizner09807052015-08-13 14:53:41 -0700192 ret = composition->SetLayers(layers_map.size(), layers_map.data());
193 if (ret) {
194 hwc_set_cleanup(num_displays, display_contents);
195 return -EINVAL;
196 }
Zach Reizner45624d32015-06-10 16:03:01 -0700197
Zach Reizner09807052015-08-13 14:53:41 -0700198 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
199 if (ret) {
200 hwc_set_cleanup(num_displays, display_contents);
201 return -EINVAL;
202 }
203
204 composition.reset(NULL);
205
206 for (int i = 0; i < (int)num_displays; ++i) {
207 if (!display_contents[i])
208 continue;
209 hwc_display_contents_1_t *dc = display_contents[i];
210 unsigned num_dc_layers = dc->numHwLayers;
211 for (int j = 0; j < (int)num_dc_layers; ++j) {
Zach Reizner45624d32015-06-10 16:03:01 -0700212 hwc_layer_1_t *layer = &dc->hwLayers[j];
213 if (layer->flags & HWC_SKIP_LAYER)
214 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700215 if (layer->compositionType == HWC_OVERLAY)
216 hwc_add_layer_to_retire_fence(layer, dc);
Sean Paulb386f1b2015-05-13 06:33:23 -0700217 }
218 }
219
Sean Paulb386f1b2015-05-13 06:33:23 -0700220 if (ret) {
221 ALOGE("Failed to queue the composition");
Sean Paulb386f1b2015-05-13 06:33:23 -0700222 }
Zach Reizner09807052015-08-13 14:53:41 -0700223 hwc_set_cleanup(num_displays, display_contents);
Sean Paulef8f1f92015-04-29 16:05:23 -0400224 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500225}
226
Sean Paulef8f1f92015-04-29 16:05:23 -0400227static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
228 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400229 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
230 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500231
Sean Paul4057be32015-05-13 06:23:09 -0700232 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
233 hwc_drm_display_t *hd = &ctx->displays[display];
234 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500235}
236
Sean Paulef8f1f92015-04-29 16:05:23 -0400237static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
238 int mode) {
239 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500240
Sean Paul6a55e9f2015-04-30 15:31:06 -0400241 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400242 switch (mode) {
243 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400244 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400245 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500246
Sean Paulef8f1f92015-04-29 16:05:23 -0400247 /* We can't support dozing right now, so go full on */
248 case HWC_POWER_MODE_DOZE:
249 case HWC_POWER_MODE_DOZE_SUSPEND:
250 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400251 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400252 break;
253 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400254 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500255}
256
Sean Paulef8f1f92015-04-29 16:05:23 -0400257static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
258 int *value) {
259 switch (what) {
260 case HWC_BACKGROUND_LAYER_SUPPORTED:
261 *value = 0; /* TODO: We should do this */
262 break;
263 case HWC_VSYNC_PERIOD:
264 ALOGW("Query for deprecated vsync value, returning 60Hz");
265 *value = 1000 * 1000 * 1000 / 60;
266 break;
267 case HWC_DISPLAY_TYPES_SUPPORTED:
268 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
269 break;
270 }
271 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500272}
273
Sean Paulef8f1f92015-04-29 16:05:23 -0400274static void hwc_register_procs(struct hwc_composer_device_1 *dev,
275 hwc_procs_t const *procs) {
276 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500277
Sean Paulef8f1f92015-04-29 16:05:23 -0400278 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700279
280 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
281 iter != ctx->displays.end(); ++iter) {
282 iter->second.vsync_worker.SetProcs(procs);
283 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500284}
285
Sean Paulef8f1f92015-04-29 16:05:23 -0400286static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
287 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400288 size_t *num_configs) {
289 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400290 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500291
Sean Paulef8f1f92015-04-29 16:05:23 -0400292 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700293 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400294 hd->config_ids.clear();
295
296 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
297 if (!connector) {
298 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400299 return -ENODEV;
300 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500301
Sean Paule42febf2015-05-07 11:35:29 -0700302 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400303 if (ret) {
304 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400305 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400306 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500307
Sean Paul6a55e9f2015-04-30 15:31:06 -0400308 for (DrmConnector::ModeIter iter = connector->begin_modes();
309 iter != connector->end_modes(); ++iter) {
310 size_t idx = hd->config_ids.size();
311 if (idx == *num_configs)
312 break;
313 hd->config_ids.push_back(iter->id());
314 configs[idx] = iter->id();
315 }
316 *num_configs = hd->config_ids.size();
317 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500318}
319
Sean Paulef8f1f92015-04-29 16:05:23 -0400320static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
321 int display, uint32_t config,
322 const uint32_t *attributes,
323 int32_t *values) {
324 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400325 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400326 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400327 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400328 return -ENODEV;
329 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400330 DrmMode mode;
331 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
332 ++iter) {
333 if (iter->id() == config) {
334 mode = *iter;
335 break;
336 }
337 }
338 if (mode.id() == 0) {
339 ALOGE("Failed to find active mode for display %d", display);
340 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400341 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500342
Sean Paul6a55e9f2015-04-30 15:31:06 -0400343 uint32_t mm_width = c->mm_width();
344 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400345 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
346 switch (attributes[i]) {
347 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400348 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400349 break;
350 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400351 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400352 break;
353 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400354 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400355 break;
356 case HWC_DISPLAY_DPI_X:
357 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400358 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400359 break;
360 case HWC_DISPLAY_DPI_Y:
361 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400362 values[i] =
363 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400364 break;
365 }
366 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400367 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500368}
369
Sean Paulef8f1f92015-04-29 16:05:23 -0400370static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
371 int display) {
372 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400373 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
374 if (!c) {
375 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400376 return -ENODEV;
377 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500378
Sean Paul6a55e9f2015-04-30 15:31:06 -0400379 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700380 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400381 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
382 if (hd->config_ids[i] == mode.id())
383 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400384 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400385 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500386}
387
Sean Paulef8f1f92015-04-29 16:05:23 -0400388static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
389 int index) {
390 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700391 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400392 if (index >= (int)hd->config_ids.size()) {
393 ALOGE("Invalid config index %d passed in", index);
394 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400395 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500396
Sean Paul877be972015-06-03 14:08:27 -0400397 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
398 if (!c) {
399 ALOGE("Failed to get connector for display %d", display);
400 return -ENODEV;
401 }
402 DrmMode mode;
403 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
404 ++iter) {
405 if (iter->id() == hd->config_ids[index]) {
406 mode = *iter;
407 break;
408 }
409 }
410 if (mode.id() != hd->config_ids[index]) {
411 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
412 return -ENOENT;
413 }
414 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400415 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400416 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400417 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400418 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400419 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500420}
421
Sean Paulef8f1f92015-04-29 16:05:23 -0400422static int hwc_device_close(struct hw_device_t *dev) {
423 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400424 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400425 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500426}
427
Sean Paul24a26e32015-02-04 10:34:47 -0800428/*
429 * TODO: This function sets the active config to the first one in the list. This
430 * should be fixed such that it selects the preferred mode for the display, or
431 * some other, saner, method of choosing the config.
432 */
Sean Paule42febf2015-05-07 11:35:29 -0700433static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400434 uint32_t config;
435 size_t num_configs = 1;
436 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
437 &num_configs);
438 if (ret || !num_configs)
439 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800440
Sean Paulef8f1f92015-04-29 16:05:23 -0400441 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
442 if (ret) {
443 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
444 return ret;
445 }
Sean Paul24a26e32015-02-04 10:34:47 -0800446
Sean Paulef8f1f92015-04-29 16:05:23 -0400447 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800448}
449
Sean Paul6a55e9f2015-04-30 15:31:06 -0400450static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700451 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400452 hd->ctx = ctx;
453 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500454
Sean Paulb386f1b2015-05-13 06:33:23 -0700455 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400456 if (ret) {
457 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400458 return ret;
459 }
Sean Paul24a26e32015-02-04 10:34:47 -0800460
Sean Paul4057be32015-05-13 06:23:09 -0700461 ret = hd->vsync_worker.Init(&ctx->drm, display);
462 if (ret) {
463 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
464 return ret;
465 }
466
Sean Paulef8f1f92015-04-29 16:05:23 -0400467 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500468}
469
Sean Paulef8f1f92015-04-29 16:05:23 -0400470static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400471 int ret;
472 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
473 c != ctx->drm.end_connectors(); ++c) {
474 ret = hwc_initialize_display(ctx, (*c)->display());
475 if (ret) {
476 ALOGE("Failed to initialize display %d", (*c)->display());
477 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400478 }
479 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400480
481 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500482}
483
Sean Paulef8f1f92015-04-29 16:05:23 -0400484static int hwc_device_open(const struct hw_module_t *module, const char *name,
485 struct hw_device_t **dev) {
486 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
487 ALOGE("Invalid module name- %s", name);
488 return -EINVAL;
489 }
490
491 struct hwc_context_t *ctx = new hwc_context_t();
492 if (!ctx) {
493 ALOGE("Failed to allocate hwc context");
494 return -ENOMEM;
495 }
496
Sean Paul6a55e9f2015-04-30 15:31:06 -0400497 int ret = ctx->drm.Init();
498 if (ret) {
499 ALOGE("Can't initialize Drm object %d", ret);
500 delete ctx;
501 return ret;
502 }
503
Sean Paulda6270d2015-06-01 14:11:52 -0400504 ctx->importer = Importer::CreateInstance(&ctx->drm);
505 if (!ctx->importer) {
506 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400507 delete ctx;
508 return ret;
509 }
510
Sean Paulef8f1f92015-04-29 16:05:23 -0400511 ret = hwc_enumerate_displays(ctx);
512 if (ret) {
513 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400514 delete ctx;
515 return ret;
516 }
517
Sean Paulef8f1f92015-04-29 16:05:23 -0400518 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
519 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
520 ctx->device.common.module = const_cast<hw_module_t *>(module);
521 ctx->device.common.close = hwc_device_close;
522
Sean Paul9046c642015-06-10 17:27:47 -0400523 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400524 ctx->device.prepare = hwc_prepare;
525 ctx->device.set = hwc_set;
526 ctx->device.eventControl = hwc_event_control;
527 ctx->device.setPowerMode = hwc_set_power_mode;
528 ctx->device.query = hwc_query;
529 ctx->device.registerProcs = hwc_register_procs;
530 ctx->device.getDisplayConfigs = hwc_get_display_configs;
531 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
532 ctx->device.getActiveConfig = hwc_get_active_config;
533 ctx->device.setActiveConfig = hwc_set_active_config;
534 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
535
536 *dev = &ctx->device.common;
537
538 return 0;
539}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400540}
Sean Paulef8f1f92015-04-29 16:05:23 -0400541
Sean Paul6a55e9f2015-04-30 15:31:06 -0400542static struct hw_module_methods_t hwc_module_methods = {
543 open : android::hwc_device_open
544};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500545
546hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400547 common : {
548 tag : HARDWARE_MODULE_TAG,
549 version_major : 1,
550 version_minor : 0,
551 id : HWC_HARDWARE_MODULE_ID,
552 name : "DRM hwcomposer module",
553 author : "The Android Open Source Project",
554 methods : &hwc_module_methods,
555 dso : NULL,
556 reserved : {0},
557 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500558};