blob: b4b1a50705d12769b66d30a34feee1b5beb5dfac [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
Sean Paule0c4c3d2015-01-20 16:56:04 -050025#include <errno.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040026#include <fcntl.h>
Sean Paul5ad302c2015-05-11 10:43:31 -070027#include <list>
Sean Paule42febf2015-05-07 11:35:29 -070028#include <map>
Sean Paulef8f1f92015-04-29 16:05:23 -040029#include <pthread.h>
Dan Albertc5255b32015-05-07 23:42:54 -070030#include <stdlib.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050031#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050032#include <sys/resource.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050033#include <xf86drm.h>
34#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050035
Sean Paulef8f1f92015-04-29 16:05:23 -040036#include <cutils/log.h>
37#include <cutils/properties.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050038#include <hardware/hardware.h>
39#include <hardware/hwcomposer.h>
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070040#include <utils/Trace.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050041
Sean Paule0c4c3d2015-01-20 16:56:04 -050042#define UM_PER_INCH 25400
Stéphane Marchesincb3f9842015-06-19 14:50:45 -070043#define HWC_FB_BUFFERS 3
Sean Paule0c4c3d2015-01-20 16:56:04 -050044
Sean Paul6a55e9f2015-04-30 15:31:06 -040045namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050046
Sean Paule42febf2015-05-07 11:35:29 -070047typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -040048 struct hwc_context_t *ctx;
49 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -050050
Sean Paul6a55e9f2015-04-30 15:31:06 -040051 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -050052
Sean Paul4057be32015-05-13 06:23:09 -070053 VSyncWorker vsync_worker;
Sean Paule42febf2015-05-07 11:35:29 -070054} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -050055
56struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -070057 // map of display:hwc_drm_display_t
58 typedef std::map<int, hwc_drm_display_t> DisplayMap;
59 typedef DisplayMap::iterator DisplayMapIter;
Sean Paule0c4c3d2015-01-20 16:56:04 -050060
Sean Paulda6270d2015-06-01 14:11:52 -040061 hwc_context_t() : procs(NULL), importer(NULL) {
62 }
63
64 ~hwc_context_t() {
65 delete importer;
66 }
67
Sean Paule42febf2015-05-07 11:35:29 -070068 hwc_composer_device_1_t device;
Sean Paulef8f1f92015-04-29 16:05:23 -040069 hwc_procs_t const *procs;
Sean Paule0c4c3d2015-01-20 16:56:04 -050070
Sean Paule42febf2015-05-07 11:35:29 -070071 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -040072 DrmResources drm;
Sean Paulda6270d2015-06-01 14:11:52 -040073 Importer *importer;
Sean Paule0c4c3d2015-01-20 16:56:04 -050074};
75
Sean Paul9046c642015-06-10 17:27:47 -040076static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
77 int buff_len) {
78 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
79 std::ostringstream out;
80
81 ctx->drm.compositor()->Dump(&out);
82 std::string out_str = out.str();
83 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
84}
85
Sean Paulb386f1b2015-05-13 06:33:23 -070086static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -040087 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -070088 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -070089 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -040090 if (!display_contents[i])
91 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -050092
Sean Paulb386f1b2015-05-13 06:33:23 -070093 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
94 if (!crtc) {
95 ALOGE("No crtc for display %d", i);
Sean Paulb386f1b2015-05-13 06:33:23 -070096 return -ENODEV;
97 }
Sean Paulb386f1b2015-05-13 06:33:23 -070098
Zach Reizner45624d32015-06-10 16:03:01 -070099 int num_layers = display_contents[i]->numHwLayers;
100 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700101 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700102
Sean Paul877be972015-06-03 14:08:27 -0400103 if (layer->compositionType == HWC_FRAMEBUFFER)
104 layer->compositionType = HWC_OVERLAY;
Sean Paulef8f1f92015-04-29 16:05:23 -0400105 }
106 }
Sean Pauldffca952015-02-04 10:19:55 -0800107
Sean Paulef8f1f92015-04-29 16:05:23 -0400108 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500109}
110
Sean Paulb386f1b2015-05-13 06:33:23 -0700111static void hwc_set_cleanup(size_t num_displays,
112 hwc_display_contents_1_t **display_contents,
113 Composition *composition) {
114 for (int i = 0; i < (int)num_displays; ++i) {
115 if (!display_contents[i])
116 continue;
117
118 hwc_display_contents_1_t *dc = display_contents[i];
119 for (size_t j = 0; j < dc->numHwLayers; ++j) {
120 hwc_layer_1_t *layer = &dc->hwLayers[j];
121 if (layer->acquireFenceFd >= 0) {
122 close(layer->acquireFenceFd);
123 layer->acquireFenceFd = -1;
124 }
125 }
126 if (dc->outbufAcquireFenceFd >= 0) {
127 close(dc->outbufAcquireFenceFd);
128 dc->outbufAcquireFenceFd = -1;
129 }
130 }
131
132 delete composition;
133}
134
Sean Paulb386f1b2015-05-13 06:33:23 -0700135static int hwc_add_layer(int display, hwc_context_t *ctx, hwc_layer_1_t *layer,
136 Composition *composition) {
137 hwc_drm_bo_t bo;
138 int ret = ctx->importer->ImportBuffer(layer->handle, &bo);
139 if (ret) {
140 ALOGE("Failed to import handle to bo %d", ret);
141 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400142 }
Sean Paulefb20cb2015-02-04 09:29:15 -0800143
Sean Paulb386f1b2015-05-13 06:33:23 -0700144 ret = composition->AddLayer(display, layer, &bo);
145 if (!ret)
Sean Paulef8f1f92015-04-29 16:05:23 -0400146 return 0;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500147
Sean Paulb386f1b2015-05-13 06:33:23 -0700148 int destroy_ret = ctx->importer->ReleaseBuffer(&bo);
149 if (destroy_ret)
150 ALOGE("Failed to destroy buffer %d", destroy_ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500151
Sean Paulef8f1f92015-04-29 16:05:23 -0400152 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500153}
154
Sean Paul04206122015-07-16 15:59:24 -0400155static void hwc_add_layer_to_retire_fence(hwc_layer_1_t *layer,
156 hwc_display_contents_1_t *display_contents) {
157 if (layer->releaseFenceFd < 0)
158 return;
159
160 if (display_contents->retireFenceFd >= 0) {
161 int old_retire_fence = display_contents->retireFenceFd;
162 display_contents->retireFenceFd = sync_merge("dc_retire", old_retire_fence,
163 layer->releaseFenceFd);
164 close(old_retire_fence);
165 } else {
166 display_contents->retireFenceFd = dup(layer->releaseFenceFd);
167 }
168}
169
Sean Paule0c4c3d2015-01-20 16:56:04 -0500170static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400171 hwc_display_contents_1_t **display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700172 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400173 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paulb386f1b2015-05-13 06:33:23 -0700174 Composition *composition =
175 ctx->drm.compositor()->CreateComposition(ctx->importer);
176 if (!composition) {
177 ALOGE("Drm composition init failed");
178 hwc_set_cleanup(num_displays, display_contents, NULL);
179 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400180 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500181
Sean Paulb386f1b2015-05-13 06:33:23 -0700182 int ret;
183 for (int i = 0; i < (int)num_displays; ++i) {
184 if (!display_contents[i])
185 continue;
186
Zach Reizner45624d32015-06-10 16:03:01 -0700187 hwc_display_contents_1_t *dc = display_contents[i];
188 int j;
189 unsigned num_layers = 0;
190 unsigned num_dc_layers = dc->numHwLayers;
191 for (j = 0; j < (int)num_dc_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700192 hwc_layer_1_t *layer = &dc->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700193 if (layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700194 continue;
Sean Paul877be972015-06-03 14:08:27 -0400195 if (layer->compositionType == HWC_OVERLAY)
Zach Reizner45624d32015-06-10 16:03:01 -0700196 num_layers++;
Zach Reizner45624d32015-06-10 16:03:01 -0700197 }
198
199 unsigned num_planes = composition->GetRemainingLayers(i, num_layers);
Zach Reizner45624d32015-06-10 16:03:01 -0700200
Sean Paul877be972015-06-03 14:08:27 -0400201 if (num_layers > num_planes) {
Zach Reizner810ecc62015-07-31 15:12:47 -0700202 ALOGE("Can not composite %u with only %u planes", num_layers, num_planes);
Zach Reizner45624d32015-06-10 16:03:01 -0700203 }
204
205 for (j = 0; num_planes && j < (int)num_dc_layers; ++j) {
206 hwc_layer_1_t *layer = &dc->hwLayers[j];
207 if (layer->flags & HWC_SKIP_LAYER)
208 continue;
Sean Paul877be972015-06-03 14:08:27 -0400209 if (layer->compositionType != HWC_OVERLAY)
210 continue;
Sean Paulb386f1b2015-05-13 06:33:23 -0700211
212 ret = hwc_add_layer(i, ctx, layer, composition);
213 if (ret) {
214 ALOGE("Add layer failed %d", ret);
215 hwc_set_cleanup(num_displays, display_contents, composition);
216 return ret;
217 }
Sean Paul04206122015-07-16 15:59:24 -0400218 hwc_add_layer_to_retire_fence(layer, dc);
219
Sean Paulb386f1b2015-05-13 06:33:23 -0700220 --num_planes;
221 }
222 }
223
224 ret = ctx->drm.compositor()->QueueComposition(composition);
Sean Paulfd138282015-07-10 16:43:06 -0400225 composition = NULL;
Sean Paulb386f1b2015-05-13 06:33:23 -0700226 if (ret) {
227 ALOGE("Failed to queue the composition");
Sean Paulfd138282015-07-10 16:43:06 -0400228 hwc_set_cleanup(num_displays, display_contents, NULL);
Sean Paulb386f1b2015-05-13 06:33:23 -0700229 return ret;
230 }
231 hwc_set_cleanup(num_displays, display_contents, NULL);
Sean Paulef8f1f92015-04-29 16:05:23 -0400232 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500233}
234
Sean Paulef8f1f92015-04-29 16:05:23 -0400235static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
236 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400237 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
238 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500239
Sean Paul4057be32015-05-13 06:23:09 -0700240 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
241 hwc_drm_display_t *hd = &ctx->displays[display];
242 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500243}
244
Sean Paulef8f1f92015-04-29 16:05:23 -0400245static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
246 int mode) {
247 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500248
Sean Paul6a55e9f2015-04-30 15:31:06 -0400249 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400250 switch (mode) {
251 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400252 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400253 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500254
Sean Paulef8f1f92015-04-29 16:05:23 -0400255 /* We can't support dozing right now, so go full on */
256 case HWC_POWER_MODE_DOZE:
257 case HWC_POWER_MODE_DOZE_SUSPEND:
258 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400259 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400260 break;
261 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400262 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500263}
264
Sean Paulef8f1f92015-04-29 16:05:23 -0400265static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
266 int *value) {
267 switch (what) {
268 case HWC_BACKGROUND_LAYER_SUPPORTED:
269 *value = 0; /* TODO: We should do this */
270 break;
271 case HWC_VSYNC_PERIOD:
272 ALOGW("Query for deprecated vsync value, returning 60Hz");
273 *value = 1000 * 1000 * 1000 / 60;
274 break;
275 case HWC_DISPLAY_TYPES_SUPPORTED:
276 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
277 break;
278 }
279 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500280}
281
Sean Paulef8f1f92015-04-29 16:05:23 -0400282static void hwc_register_procs(struct hwc_composer_device_1 *dev,
283 hwc_procs_t const *procs) {
284 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500285
Sean Paulef8f1f92015-04-29 16:05:23 -0400286 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700287
288 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
289 iter != ctx->displays.end(); ++iter) {
290 iter->second.vsync_worker.SetProcs(procs);
291 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500292}
293
Sean Paulef8f1f92015-04-29 16:05:23 -0400294static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
295 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400296 size_t *num_configs) {
297 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400298 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500299
Sean Paulef8f1f92015-04-29 16:05:23 -0400300 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700301 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400302 hd->config_ids.clear();
303
304 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
305 if (!connector) {
306 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400307 return -ENODEV;
308 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500309
Sean Paule42febf2015-05-07 11:35:29 -0700310 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400311 if (ret) {
312 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400313 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400314 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500315
Sean Paul6a55e9f2015-04-30 15:31:06 -0400316 for (DrmConnector::ModeIter iter = connector->begin_modes();
317 iter != connector->end_modes(); ++iter) {
318 size_t idx = hd->config_ids.size();
319 if (idx == *num_configs)
320 break;
321 hd->config_ids.push_back(iter->id());
322 configs[idx] = iter->id();
323 }
324 *num_configs = hd->config_ids.size();
325 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500326}
327
Sean Paulef8f1f92015-04-29 16:05:23 -0400328static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
329 int display, uint32_t config,
330 const uint32_t *attributes,
331 int32_t *values) {
332 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400333 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400334 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400335 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400336 return -ENODEV;
337 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400338 DrmMode mode;
339 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
340 ++iter) {
341 if (iter->id() == config) {
342 mode = *iter;
343 break;
344 }
345 }
346 if (mode.id() == 0) {
347 ALOGE("Failed to find active mode for display %d", display);
348 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400349 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500350
Sean Paul6a55e9f2015-04-30 15:31:06 -0400351 uint32_t mm_width = c->mm_width();
352 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400353 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
354 switch (attributes[i]) {
355 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400356 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400357 break;
358 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400359 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400360 break;
361 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400362 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400363 break;
364 case HWC_DISPLAY_DPI_X:
365 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400366 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400367 break;
368 case HWC_DISPLAY_DPI_Y:
369 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400370 values[i] =
371 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400372 break;
373 }
374 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400375 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500376}
377
Sean Paulef8f1f92015-04-29 16:05:23 -0400378static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
379 int display) {
380 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400381 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
382 if (!c) {
383 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400384 return -ENODEV;
385 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500386
Sean Paul6a55e9f2015-04-30 15:31:06 -0400387 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700388 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400389 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
390 if (hd->config_ids[i] == mode.id())
391 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400392 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400393 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500394}
395
Sean Paulef8f1f92015-04-29 16:05:23 -0400396static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
397 int index) {
398 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700399 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400400 if (index >= (int)hd->config_ids.size()) {
401 ALOGE("Invalid config index %d passed in", index);
402 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400403 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500404
Sean Paul877be972015-06-03 14:08:27 -0400405 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
406 if (!c) {
407 ALOGE("Failed to get connector for display %d", display);
408 return -ENODEV;
409 }
410 DrmMode mode;
411 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
412 ++iter) {
413 if (iter->id() == hd->config_ids[index]) {
414 mode = *iter;
415 break;
416 }
417 }
418 if (mode.id() != hd->config_ids[index]) {
419 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
420 return -ENOENT;
421 }
422 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400424 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400425 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400426 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400427 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500428}
429
Sean Paulef8f1f92015-04-29 16:05:23 -0400430static int hwc_device_close(struct hw_device_t *dev) {
431 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400432 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400433 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500434}
435
Sean Paul24a26e32015-02-04 10:34:47 -0800436/*
437 * TODO: This function sets the active config to the first one in the list. This
438 * should be fixed such that it selects the preferred mode for the display, or
439 * some other, saner, method of choosing the config.
440 */
Sean Paule42febf2015-05-07 11:35:29 -0700441static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400442 uint32_t config;
443 size_t num_configs = 1;
444 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
445 &num_configs);
446 if (ret || !num_configs)
447 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800448
Sean Paulef8f1f92015-04-29 16:05:23 -0400449 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
450 if (ret) {
451 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
452 return ret;
453 }
Sean Paul24a26e32015-02-04 10:34:47 -0800454
Sean Paulef8f1f92015-04-29 16:05:23 -0400455 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800456}
457
Sean Paul6a55e9f2015-04-30 15:31:06 -0400458static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700459 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400460 hd->ctx = ctx;
461 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500462
Sean Paulb386f1b2015-05-13 06:33:23 -0700463 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400464 if (ret) {
465 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400466 return ret;
467 }
Sean Paul24a26e32015-02-04 10:34:47 -0800468
Sean Paul4057be32015-05-13 06:23:09 -0700469 ret = hd->vsync_worker.Init(&ctx->drm, display);
470 if (ret) {
471 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
472 return ret;
473 }
474
Sean Paulef8f1f92015-04-29 16:05:23 -0400475 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500476}
477
Sean Paulef8f1f92015-04-29 16:05:23 -0400478static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400479 int ret;
480 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
481 c != ctx->drm.end_connectors(); ++c) {
482 ret = hwc_initialize_display(ctx, (*c)->display());
483 if (ret) {
484 ALOGE("Failed to initialize display %d", (*c)->display());
485 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400486 }
487 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400488
489 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500490}
491
Sean Paulef8f1f92015-04-29 16:05:23 -0400492static int hwc_device_open(const struct hw_module_t *module, const char *name,
493 struct hw_device_t **dev) {
494 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
495 ALOGE("Invalid module name- %s", name);
496 return -EINVAL;
497 }
498
499 struct hwc_context_t *ctx = new hwc_context_t();
500 if (!ctx) {
501 ALOGE("Failed to allocate hwc context");
502 return -ENOMEM;
503 }
504
Sean Paul6a55e9f2015-04-30 15:31:06 -0400505 int ret = ctx->drm.Init();
506 if (ret) {
507 ALOGE("Can't initialize Drm object %d", ret);
508 delete ctx;
509 return ret;
510 }
511
Sean Paulda6270d2015-06-01 14:11:52 -0400512 ctx->importer = Importer::CreateInstance(&ctx->drm);
513 if (!ctx->importer) {
514 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400515 delete ctx;
516 return ret;
517 }
518
Sean Paulef8f1f92015-04-29 16:05:23 -0400519 ret = hwc_enumerate_displays(ctx);
520 if (ret) {
521 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400522 delete ctx;
523 return ret;
524 }
525
Sean Paulef8f1f92015-04-29 16:05:23 -0400526 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
527 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
528 ctx->device.common.module = const_cast<hw_module_t *>(module);
529 ctx->device.common.close = hwc_device_close;
530
Sean Paul9046c642015-06-10 17:27:47 -0400531 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400532 ctx->device.prepare = hwc_prepare;
533 ctx->device.set = hwc_set;
534 ctx->device.eventControl = hwc_event_control;
535 ctx->device.setPowerMode = hwc_set_power_mode;
536 ctx->device.query = hwc_query;
537 ctx->device.registerProcs = hwc_register_procs;
538 ctx->device.getDisplayConfigs = hwc_get_display_configs;
539 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
540 ctx->device.getActiveConfig = hwc_get_active_config;
541 ctx->device.setActiveConfig = hwc_set_active_config;
542 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
543
544 *dev = &ctx->device.common;
545
546 return 0;
547}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400548}
Sean Paulef8f1f92015-04-29 16:05:23 -0400549
Sean Paul6a55e9f2015-04-30 15:31:06 -0400550static struct hw_module_methods_t hwc_module_methods = {
551 open : android::hwc_device_open
552};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500553
554hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400555 common : {
556 tag : HARDWARE_MODULE_TAG,
557 version_major : 1,
558 version_minor : 0,
559 id : HWC_HARDWARE_MODULE_ID,
560 name : "DRM hwcomposer module",
561 author : "The Android Open Source Project",
562 methods : &hwc_module_methods,
563 dso : NULL,
564 reserved : {0},
565 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500566};