blob: 88f13c37b6a62895883f8b43b1093c9e284fb67f [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
Zach Reizner1946fa72015-08-14 11:14:38 -070063 hwc_context_t() : procs(NULL), importer(NULL), use_framebuffer_target(false) {
Sean Paulda6270d2015-06-01 14:11:52 -040064 }
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;
Zach Reizner1946fa72015-08-14 11:14:38 -070076 bool use_framebuffer_target;
Sean Paule0c4c3d2015-01-20 16:56:04 -050077};
78
Zach Reiznerc6520e42015-08-13 14:32:09 -070079static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff,
Sean Paul9046c642015-06-10 17:27:47 -040080 int buff_len) {
81 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
82 std::ostringstream out;
83
84 ctx->drm.compositor()->Dump(&out);
85 std::string out_str = out.str();
86 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
87}
88
Sean Paulb386f1b2015-05-13 06:33:23 -070089static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -040090 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -070091 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Zach Reizner1946fa72015-08-14 11:14:38 -070092
93 char use_framebuffer_target[PROPERTY_VALUE_MAX];
94 property_get("hwc.drm.use_framebuffer_target", use_framebuffer_target, "0");
95 bool new_use_framebuffer_target = atoi(use_framebuffer_target);
96 if (ctx->use_framebuffer_target != new_use_framebuffer_target)
97 ALOGW("Starting to %s HWC_FRAMEBUFFER_TARGET",
98 new_use_framebuffer_target ? "use" : "not use");
99 ctx->use_framebuffer_target = new_use_framebuffer_target;
100
Sean Paule42febf2015-05-07 11:35:29 -0700101 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400102 if (!display_contents[i])
103 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500104
Sean Paulb386f1b2015-05-13 06:33:23 -0700105 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
106 if (!crtc) {
107 ALOGE("No crtc for display %d", i);
Sean Paulb386f1b2015-05-13 06:33:23 -0700108 return -ENODEV;
109 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700110
Zach Reizner45624d32015-06-10 16:03:01 -0700111 int num_layers = display_contents[i]->numHwLayers;
112 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700113 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700114
Zach Reizner1946fa72015-08-14 11:14:38 -0700115 if (!ctx->use_framebuffer_target) {
116 if (layer->compositionType == HWC_FRAMEBUFFER)
117 layer->compositionType = HWC_OVERLAY;
118 } else {
119 switch (layer->compositionType) {
120 case HWC_OVERLAY:
121 case HWC_BACKGROUND:
122 case HWC_SIDEBAND:
123 case HWC_CURSOR_OVERLAY:
124 layer->compositionType = HWC_FRAMEBUFFER;
125 break;
126 }
127 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400128 }
129 }
Sean Pauldffca952015-02-04 10:19:55 -0800130
Sean Paulef8f1f92015-04-29 16:05:23 -0400131 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500132}
133
Sean Paulb386f1b2015-05-13 06:33:23 -0700134static void hwc_set_cleanup(size_t num_displays,
Zach Reizner09807052015-08-13 14:53:41 -0700135 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700136 for (int i = 0; i < (int)num_displays; ++i) {
137 if (!display_contents[i])
138 continue;
139
140 hwc_display_contents_1_t *dc = display_contents[i];
141 for (size_t j = 0; j < dc->numHwLayers; ++j) {
142 hwc_layer_1_t *layer = &dc->hwLayers[j];
143 if (layer->acquireFenceFd >= 0) {
144 close(layer->acquireFenceFd);
145 layer->acquireFenceFd = -1;
146 }
147 }
148 if (dc->outbufAcquireFenceFd >= 0) {
149 close(dc->outbufAcquireFenceFd);
150 dc->outbufAcquireFenceFd = -1;
151 }
152 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700153}
154
Zach Reizner09807052015-08-13 14:53:41 -0700155static void hwc_add_layer_to_retire_fence(
156 hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) {
Sean Paul04206122015-07-16 15:59:24 -0400157 if (layer->releaseFenceFd < 0)
158 return;
159
160 if (display_contents->retireFenceFd >= 0) {
161 int old_retire_fence = display_contents->retireFenceFd;
Zach Reiznerc6520e42015-08-13 14:32:09 -0700162 display_contents->retireFenceFd =
163 sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd);
Sean Paul04206122015-07-16 15:59:24 -0400164 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;
Zach Reizner09807052015-08-13 14:53:41 -0700174 int ret;
175 std::unique_ptr<DrmComposition> composition(
176 ctx->drm.compositor()->CreateComposition(ctx->importer));
Sean Paulb386f1b2015-05-13 06:33:23 -0700177 if (!composition) {
178 ALOGE("Drm composition init failed");
Zach Reizner09807052015-08-13 14:53:41 -0700179 hwc_set_cleanup(num_displays, display_contents);
Sean Paulb386f1b2015-05-13 06:33:23 -0700180 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400181 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500182
Zach Reizner09807052015-08-13 14:53:41 -0700183 std::vector<DrmCompositionDisplayLayersMap> layers_map;
184 std::vector<std::vector<size_t>> layers_indices;
185 layers_map.reserve(num_displays);
186 layers_indices.reserve(num_displays);
187
Sean Paulb386f1b2015-05-13 06:33:23 -0700188 for (int i = 0; i < (int)num_displays; ++i) {
189 if (!display_contents[i])
190 continue;
Zach Reizner45624d32015-06-10 16:03:01 -0700191 hwc_display_contents_1_t *dc = display_contents[i];
Zach Reizner09807052015-08-13 14:53:41 -0700192
193 layers_map.emplace_back();
194 DrmCompositionDisplayLayersMap &map = layers_map[i];
195 map.display = i;
196 map.layers = dc->hwLayers;
197
198 std::vector<size_t> indices_to_composite;
Zach Reizner45624d32015-06-10 16:03:01 -0700199 unsigned num_dc_layers = dc->numHwLayers;
Zach Reizner09807052015-08-13 14:53:41 -0700200 for (int j = 0; j < (int)num_dc_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700201 hwc_layer_1_t *layer = &dc->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700202 if (layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700203 continue;
Zach Reizner1946fa72015-08-14 11:14:38 -0700204 if (!ctx->use_framebuffer_target) {
205 if (layer->compositionType == HWC_OVERLAY)
206 indices_to_composite.push_back(j);
207 } else {
208 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
209 indices_to_composite.push_back(j);
210 }
211 }
212 if (ctx->use_framebuffer_target) {
213 if (indices_to_composite.size() != 1) {
214 ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET",
215 indices_to_composite.size());
216 hwc_set_cleanup(num_displays, display_contents);
217 return -EINVAL;
218 }
Zach Reizner45624d32015-06-10 16:03:01 -0700219 }
220
Zach Reizner09807052015-08-13 14:53:41 -0700221 map.num_layers = indices_to_composite.size();
222 layers_indices.emplace_back(std::move(indices_to_composite));
223 map.layer_indices = layers_indices.back().data();
224 }
Zach Reizner45624d32015-06-10 16:03:01 -0700225
Zach Reizner09807052015-08-13 14:53:41 -0700226 ret = composition->SetLayers(layers_map.size(), layers_map.data());
227 if (ret) {
228 hwc_set_cleanup(num_displays, display_contents);
229 return -EINVAL;
230 }
Zach Reizner45624d32015-06-10 16:03:01 -0700231
Zach Reizner09807052015-08-13 14:53:41 -0700232 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
233 if (ret) {
234 hwc_set_cleanup(num_displays, display_contents);
235 return -EINVAL;
236 }
237
238 composition.reset(NULL);
239
240 for (int i = 0; i < (int)num_displays; ++i) {
241 if (!display_contents[i])
242 continue;
243 hwc_display_contents_1_t *dc = display_contents[i];
244 unsigned num_dc_layers = dc->numHwLayers;
245 for (int j = 0; j < (int)num_dc_layers; ++j) {
Zach Reizner45624d32015-06-10 16:03:01 -0700246 hwc_layer_1_t *layer = &dc->hwLayers[j];
247 if (layer->flags & HWC_SKIP_LAYER)
248 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700249 if (layer->compositionType == HWC_OVERLAY)
250 hwc_add_layer_to_retire_fence(layer, dc);
Sean Paulb386f1b2015-05-13 06:33:23 -0700251 }
252 }
253
Sean Paulb386f1b2015-05-13 06:33:23 -0700254 if (ret) {
255 ALOGE("Failed to queue the composition");
Sean Paulb386f1b2015-05-13 06:33:23 -0700256 }
Zach Reizner09807052015-08-13 14:53:41 -0700257 hwc_set_cleanup(num_displays, display_contents);
Sean Paulef8f1f92015-04-29 16:05:23 -0400258 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500259}
260
Sean Paulef8f1f92015-04-29 16:05:23 -0400261static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
262 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400263 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
264 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500265
Sean Paul4057be32015-05-13 06:23:09 -0700266 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
267 hwc_drm_display_t *hd = &ctx->displays[display];
268 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500269}
270
Sean Paulef8f1f92015-04-29 16:05:23 -0400271static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
272 int mode) {
273 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500274
Sean Paul6a55e9f2015-04-30 15:31:06 -0400275 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400276 switch (mode) {
277 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400278 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400279 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500280
Sean Paulef8f1f92015-04-29 16:05:23 -0400281 /* We can't support dozing right now, so go full on */
282 case HWC_POWER_MODE_DOZE:
283 case HWC_POWER_MODE_DOZE_SUSPEND:
284 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400285 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400286 break;
287 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400288 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500289}
290
Sean Paulef8f1f92015-04-29 16:05:23 -0400291static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
292 int *value) {
293 switch (what) {
294 case HWC_BACKGROUND_LAYER_SUPPORTED:
295 *value = 0; /* TODO: We should do this */
296 break;
297 case HWC_VSYNC_PERIOD:
298 ALOGW("Query for deprecated vsync value, returning 60Hz");
299 *value = 1000 * 1000 * 1000 / 60;
300 break;
301 case HWC_DISPLAY_TYPES_SUPPORTED:
302 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
303 break;
304 }
305 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500306}
307
Sean Paulef8f1f92015-04-29 16:05:23 -0400308static void hwc_register_procs(struct hwc_composer_device_1 *dev,
309 hwc_procs_t const *procs) {
310 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500311
Sean Paulef8f1f92015-04-29 16:05:23 -0400312 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700313
314 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
315 iter != ctx->displays.end(); ++iter) {
316 iter->second.vsync_worker.SetProcs(procs);
317 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500318}
319
Sean Paulef8f1f92015-04-29 16:05:23 -0400320static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
321 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400322 size_t *num_configs) {
323 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400324 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500325
Sean Paulef8f1f92015-04-29 16:05:23 -0400326 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700327 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400328 hd->config_ids.clear();
329
330 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
331 if (!connector) {
332 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400333 return -ENODEV;
334 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500335
Sean Paule42febf2015-05-07 11:35:29 -0700336 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400337 if (ret) {
338 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400339 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400340 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500341
Sean Paul6a55e9f2015-04-30 15:31:06 -0400342 for (DrmConnector::ModeIter iter = connector->begin_modes();
343 iter != connector->end_modes(); ++iter) {
344 size_t idx = hd->config_ids.size();
345 if (idx == *num_configs)
346 break;
347 hd->config_ids.push_back(iter->id());
348 configs[idx] = iter->id();
349 }
350 *num_configs = hd->config_ids.size();
351 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500352}
353
Sean Paulef8f1f92015-04-29 16:05:23 -0400354static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
355 int display, uint32_t config,
356 const uint32_t *attributes,
357 int32_t *values) {
358 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400359 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400360 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400361 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400362 return -ENODEV;
363 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400364 DrmMode mode;
365 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
366 ++iter) {
367 if (iter->id() == config) {
368 mode = *iter;
369 break;
370 }
371 }
372 if (mode.id() == 0) {
373 ALOGE("Failed to find active mode for display %d", display);
374 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400375 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500376
Sean Paul6a55e9f2015-04-30 15:31:06 -0400377 uint32_t mm_width = c->mm_width();
378 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400379 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
380 switch (attributes[i]) {
381 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400382 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400383 break;
384 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400385 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400386 break;
387 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400388 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400389 break;
390 case HWC_DISPLAY_DPI_X:
391 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400392 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400393 break;
394 case HWC_DISPLAY_DPI_Y:
395 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400396 values[i] =
397 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400398 break;
399 }
400 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400401 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500402}
403
Sean Paulef8f1f92015-04-29 16:05:23 -0400404static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
405 int display) {
406 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400407 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
408 if (!c) {
409 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400410 return -ENODEV;
411 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500412
Sean Paul6a55e9f2015-04-30 15:31:06 -0400413 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700414 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400415 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
416 if (hd->config_ids[i] == mode.id())
417 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400418 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400419 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500420}
421
Sean Paulef8f1f92015-04-29 16:05:23 -0400422static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
423 int index) {
424 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700425 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400426 if (index >= (int)hd->config_ids.size()) {
427 ALOGE("Invalid config index %d passed in", index);
428 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400429 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500430
Sean Paul877be972015-06-03 14:08:27 -0400431 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
432 if (!c) {
433 ALOGE("Failed to get connector for display %d", display);
434 return -ENODEV;
435 }
436 DrmMode mode;
437 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
438 ++iter) {
439 if (iter->id() == hd->config_ids[index]) {
440 mode = *iter;
441 break;
442 }
443 }
444 if (mode.id() != hd->config_ids[index]) {
445 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
446 return -ENOENT;
447 }
448 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400449 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400450 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400451 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400452 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400453 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500454}
455
Sean Paulef8f1f92015-04-29 16:05:23 -0400456static int hwc_device_close(struct hw_device_t *dev) {
457 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400458 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400459 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500460}
461
Sean Paul24a26e32015-02-04 10:34:47 -0800462/*
463 * TODO: This function sets the active config to the first one in the list. This
464 * should be fixed such that it selects the preferred mode for the display, or
465 * some other, saner, method of choosing the config.
466 */
Sean Paule42febf2015-05-07 11:35:29 -0700467static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400468 uint32_t config;
469 size_t num_configs = 1;
470 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
471 &num_configs);
472 if (ret || !num_configs)
473 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800474
Sean Paulef8f1f92015-04-29 16:05:23 -0400475 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
476 if (ret) {
477 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
478 return ret;
479 }
Sean Paul24a26e32015-02-04 10:34:47 -0800480
Sean Paulef8f1f92015-04-29 16:05:23 -0400481 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800482}
483
Sean Paul6a55e9f2015-04-30 15:31:06 -0400484static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700485 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400486 hd->ctx = ctx;
487 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500488
Sean Paulb386f1b2015-05-13 06:33:23 -0700489 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400490 if (ret) {
491 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400492 return ret;
493 }
Sean Paul24a26e32015-02-04 10:34:47 -0800494
Sean Paul4057be32015-05-13 06:23:09 -0700495 ret = hd->vsync_worker.Init(&ctx->drm, display);
496 if (ret) {
497 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
498 return ret;
499 }
500
Sean Paulef8f1f92015-04-29 16:05:23 -0400501 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500502}
503
Sean Paulef8f1f92015-04-29 16:05:23 -0400504static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400505 int ret;
506 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
507 c != ctx->drm.end_connectors(); ++c) {
508 ret = hwc_initialize_display(ctx, (*c)->display());
509 if (ret) {
510 ALOGE("Failed to initialize display %d", (*c)->display());
511 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400512 }
513 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400514
515 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500516}
517
Sean Paulef8f1f92015-04-29 16:05:23 -0400518static int hwc_device_open(const struct hw_module_t *module, const char *name,
519 struct hw_device_t **dev) {
520 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
521 ALOGE("Invalid module name- %s", name);
522 return -EINVAL;
523 }
524
525 struct hwc_context_t *ctx = new hwc_context_t();
526 if (!ctx) {
527 ALOGE("Failed to allocate hwc context");
528 return -ENOMEM;
529 }
530
Sean Paul6a55e9f2015-04-30 15:31:06 -0400531 int ret = ctx->drm.Init();
532 if (ret) {
533 ALOGE("Can't initialize Drm object %d", ret);
534 delete ctx;
535 return ret;
536 }
537
Sean Paulda6270d2015-06-01 14:11:52 -0400538 ctx->importer = Importer::CreateInstance(&ctx->drm);
539 if (!ctx->importer) {
540 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400541 delete ctx;
542 return ret;
543 }
544
Sean Paulef8f1f92015-04-29 16:05:23 -0400545 ret = hwc_enumerate_displays(ctx);
546 if (ret) {
547 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400548 delete ctx;
549 return ret;
550 }
551
Sean Paulef8f1f92015-04-29 16:05:23 -0400552 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
553 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
554 ctx->device.common.module = const_cast<hw_module_t *>(module);
555 ctx->device.common.close = hwc_device_close;
556
Sean Paul9046c642015-06-10 17:27:47 -0400557 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400558 ctx->device.prepare = hwc_prepare;
559 ctx->device.set = hwc_set;
560 ctx->device.eventControl = hwc_event_control;
561 ctx->device.setPowerMode = hwc_set_power_mode;
562 ctx->device.query = hwc_query;
563 ctx->device.registerProcs = hwc_register_procs;
564 ctx->device.getDisplayConfigs = hwc_get_display_configs;
565 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
566 ctx->device.getActiveConfig = hwc_get_active_config;
567 ctx->device.setActiveConfig = hwc_set_active_config;
568 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
569
570 *dev = &ctx->device.common;
571
572 return 0;
573}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400574}
Sean Paulef8f1f92015-04-29 16:05:23 -0400575
Sean Paul6a55e9f2015-04-30 15:31:06 -0400576static struct hw_module_methods_t hwc_module_methods = {
577 open : android::hwc_device_open
578};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500579
580hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400581 common : {
582 tag : HARDWARE_MODULE_TAG,
583 version_major : 1,
584 version_minor : 0,
585 id : HWC_HARDWARE_MODULE_ID,
586 name : "DRM hwcomposer module",
587 author : "The Android Open Source Project",
588 methods : &hwc_module_methods,
589 dso : NULL,
590 reserved : {0},
591 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500592};