blob: 060a955494048585a62f8af0703b1fa8c077488e [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;
Haixia Shi1034bb72015-09-09 12:08:20 -0700200 int framebuffer_target_index = -1;
Zach Reizner09807052015-08-13 14:53:41 -0700201 for (int j = 0; j < (int)num_dc_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700202 hwc_layer_1_t *layer = &dc->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700203 if (layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700204 continue;
Zach Reizner1946fa72015-08-14 11:14:38 -0700205 if (!ctx->use_framebuffer_target) {
206 if (layer->compositionType == HWC_OVERLAY)
207 indices_to_composite.push_back(j);
Haixia Shi1034bb72015-09-09 12:08:20 -0700208 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
209 framebuffer_target_index = j;
Zach Reizner1946fa72015-08-14 11:14:38 -0700210 } else {
211 if (layer->compositionType == HWC_FRAMEBUFFER_TARGET)
212 indices_to_composite.push_back(j);
213 }
214 }
215 if (ctx->use_framebuffer_target) {
216 if (indices_to_composite.size() != 1) {
217 ALOGE("Expected 1 (got %d) layer with HWC_FRAMEBUFFER_TARGET",
218 indices_to_composite.size());
219 hwc_set_cleanup(num_displays, display_contents);
220 return -EINVAL;
221 }
Haixia Shi1034bb72015-09-09 12:08:20 -0700222 } else {
223 if (indices_to_composite.empty() && framebuffer_target_index >= 0) {
224 // Fall back to use HWC_FRAMEBUFFER_TARGET if all HWC_OVERLAY layers
225 // are skipped.
226 hwc_layer_1_t *layer = &dc->hwLayers[framebuffer_target_index];
227 if (!layer->handle || (layer->flags & HWC_SKIP_LAYER)) {
228 ALOGE("Expected valid layer with HWC_FRAMEBUFFER_TARGET when all "
229 "HWC_OVERLAY layers are skipped.");
230 hwc_set_cleanup(num_displays, display_contents);
231 return -EINVAL;
232 }
233 indices_to_composite.push_back(framebuffer_target_index);
234 }
Zach Reizner45624d32015-06-10 16:03:01 -0700235 }
236
Zach Reizner09807052015-08-13 14:53:41 -0700237 map.num_layers = indices_to_composite.size();
238 layers_indices.emplace_back(std::move(indices_to_composite));
239 map.layer_indices = layers_indices.back().data();
240 }
Zach Reizner45624d32015-06-10 16:03:01 -0700241
Zach Reizner09807052015-08-13 14:53:41 -0700242 ret = composition->SetLayers(layers_map.size(), layers_map.data());
243 if (ret) {
244 hwc_set_cleanup(num_displays, display_contents);
245 return -EINVAL;
246 }
Zach Reizner45624d32015-06-10 16:03:01 -0700247
Zach Reizner09807052015-08-13 14:53:41 -0700248 ret = ctx->drm.compositor()->QueueComposition(std::move(composition));
249 if (ret) {
250 hwc_set_cleanup(num_displays, display_contents);
251 return -EINVAL;
252 }
253
254 composition.reset(NULL);
255
256 for (int i = 0; i < (int)num_displays; ++i) {
257 if (!display_contents[i])
258 continue;
259 hwc_display_contents_1_t *dc = display_contents[i];
260 unsigned num_dc_layers = dc->numHwLayers;
261 for (int j = 0; j < (int)num_dc_layers; ++j) {
Zach Reizner45624d32015-06-10 16:03:01 -0700262 hwc_layer_1_t *layer = &dc->hwLayers[j];
263 if (layer->flags & HWC_SKIP_LAYER)
264 continue;
Zach Reizner09807052015-08-13 14:53:41 -0700265 if (layer->compositionType == HWC_OVERLAY)
266 hwc_add_layer_to_retire_fence(layer, dc);
Sean Paulb386f1b2015-05-13 06:33:23 -0700267 }
268 }
269
Sean Paulb386f1b2015-05-13 06:33:23 -0700270 if (ret) {
271 ALOGE("Failed to queue the composition");
Sean Paulb386f1b2015-05-13 06:33:23 -0700272 }
Zach Reizner09807052015-08-13 14:53:41 -0700273 hwc_set_cleanup(num_displays, display_contents);
Sean Paulef8f1f92015-04-29 16:05:23 -0400274 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500275}
276
Sean Paulef8f1f92015-04-29 16:05:23 -0400277static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
278 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400279 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
280 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500281
Sean Paul4057be32015-05-13 06:23:09 -0700282 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
283 hwc_drm_display_t *hd = &ctx->displays[display];
284 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500285}
286
Sean Paulef8f1f92015-04-29 16:05:23 -0400287static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
288 int mode) {
289 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500290
Sean Paul6a55e9f2015-04-30 15:31:06 -0400291 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400292 switch (mode) {
293 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400294 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400295 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500296
Sean Paulef8f1f92015-04-29 16:05:23 -0400297 /* We can't support dozing right now, so go full on */
298 case HWC_POWER_MODE_DOZE:
299 case HWC_POWER_MODE_DOZE_SUSPEND:
300 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400301 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400302 break;
303 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400304 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500305}
306
Sean Paulef8f1f92015-04-29 16:05:23 -0400307static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
308 int *value) {
309 switch (what) {
310 case HWC_BACKGROUND_LAYER_SUPPORTED:
311 *value = 0; /* TODO: We should do this */
312 break;
313 case HWC_VSYNC_PERIOD:
314 ALOGW("Query for deprecated vsync value, returning 60Hz");
315 *value = 1000 * 1000 * 1000 / 60;
316 break;
317 case HWC_DISPLAY_TYPES_SUPPORTED:
318 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
319 break;
320 }
321 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500322}
323
Sean Paulef8f1f92015-04-29 16:05:23 -0400324static void hwc_register_procs(struct hwc_composer_device_1 *dev,
325 hwc_procs_t const *procs) {
326 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500327
Sean Paulef8f1f92015-04-29 16:05:23 -0400328 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700329
330 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
331 iter != ctx->displays.end(); ++iter) {
332 iter->second.vsync_worker.SetProcs(procs);
333 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500334}
335
Sean Paulef8f1f92015-04-29 16:05:23 -0400336static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
337 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400338 size_t *num_configs) {
339 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400340 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500341
Sean Paulef8f1f92015-04-29 16:05:23 -0400342 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700343 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400344 hd->config_ids.clear();
345
346 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
347 if (!connector) {
348 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400349 return -ENODEV;
350 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500351
Sean Paule42febf2015-05-07 11:35:29 -0700352 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400353 if (ret) {
354 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400355 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400356 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500357
Sean Paul6a55e9f2015-04-30 15:31:06 -0400358 for (DrmConnector::ModeIter iter = connector->begin_modes();
359 iter != connector->end_modes(); ++iter) {
360 size_t idx = hd->config_ids.size();
361 if (idx == *num_configs)
362 break;
363 hd->config_ids.push_back(iter->id());
364 configs[idx] = iter->id();
365 }
366 *num_configs = hd->config_ids.size();
367 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500368}
369
Sean Paulef8f1f92015-04-29 16:05:23 -0400370static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
371 int display, uint32_t config,
372 const uint32_t *attributes,
373 int32_t *values) {
374 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400375 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400376 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400377 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400378 return -ENODEV;
379 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400380 DrmMode mode;
381 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
382 ++iter) {
383 if (iter->id() == config) {
384 mode = *iter;
385 break;
386 }
387 }
388 if (mode.id() == 0) {
389 ALOGE("Failed to find active mode for display %d", display);
390 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400391 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500392
Sean Paul6a55e9f2015-04-30 15:31:06 -0400393 uint32_t mm_width = c->mm_width();
394 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400395 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
396 switch (attributes[i]) {
397 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400398 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400399 break;
400 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400401 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400402 break;
403 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400404 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400405 break;
406 case HWC_DISPLAY_DPI_X:
407 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400408 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400409 break;
410 case HWC_DISPLAY_DPI_Y:
411 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400412 values[i] =
413 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400414 break;
415 }
416 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400417 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500418}
419
Sean Paulef8f1f92015-04-29 16:05:23 -0400420static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
421 int display) {
422 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400423 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
424 if (!c) {
425 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400426 return -ENODEV;
427 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500428
Sean Paul6a55e9f2015-04-30 15:31:06 -0400429 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700430 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400431 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
432 if (hd->config_ids[i] == mode.id())
433 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400434 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400435 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500436}
437
Sean Paulef8f1f92015-04-29 16:05:23 -0400438static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
439 int index) {
440 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700441 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400442 if (index >= (int)hd->config_ids.size()) {
443 ALOGE("Invalid config index %d passed in", index);
444 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400445 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500446
Sean Paul877be972015-06-03 14:08:27 -0400447 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
448 if (!c) {
449 ALOGE("Failed to get connector for display %d", display);
450 return -ENODEV;
451 }
452 DrmMode mode;
453 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
454 ++iter) {
455 if (iter->id() == hd->config_ids[index]) {
456 mode = *iter;
457 break;
458 }
459 }
460 if (mode.id() != hd->config_ids[index]) {
461 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
462 return -ENOENT;
463 }
464 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400465 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400466 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400467 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400468 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400469 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500470}
471
Sean Paulef8f1f92015-04-29 16:05:23 -0400472static int hwc_device_close(struct hw_device_t *dev) {
473 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400474 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400475 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500476}
477
Sean Paul24a26e32015-02-04 10:34:47 -0800478/*
479 * TODO: This function sets the active config to the first one in the list. This
480 * should be fixed such that it selects the preferred mode for the display, or
481 * some other, saner, method of choosing the config.
482 */
Sean Paule42febf2015-05-07 11:35:29 -0700483static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400484 uint32_t config;
485 size_t num_configs = 1;
486 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
487 &num_configs);
488 if (ret || !num_configs)
489 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800490
Sean Paulef8f1f92015-04-29 16:05:23 -0400491 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
492 if (ret) {
493 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
494 return ret;
495 }
Sean Paul24a26e32015-02-04 10:34:47 -0800496
Sean Paulef8f1f92015-04-29 16:05:23 -0400497 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800498}
499
Sean Paul6a55e9f2015-04-30 15:31:06 -0400500static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700501 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400502 hd->ctx = ctx;
503 hd->display = display;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500504
Sean Paulb386f1b2015-05-13 06:33:23 -0700505 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400506 if (ret) {
507 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400508 return ret;
509 }
Sean Paul24a26e32015-02-04 10:34:47 -0800510
Sean Paul4057be32015-05-13 06:23:09 -0700511 ret = hd->vsync_worker.Init(&ctx->drm, display);
512 if (ret) {
513 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
514 return ret;
515 }
516
Sean Paulef8f1f92015-04-29 16:05:23 -0400517 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500518}
519
Sean Paulef8f1f92015-04-29 16:05:23 -0400520static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400521 int ret;
522 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
523 c != ctx->drm.end_connectors(); ++c) {
524 ret = hwc_initialize_display(ctx, (*c)->display());
525 if (ret) {
526 ALOGE("Failed to initialize display %d", (*c)->display());
527 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400528 }
529 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400530
531 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500532}
533
Sean Paulef8f1f92015-04-29 16:05:23 -0400534static int hwc_device_open(const struct hw_module_t *module, const char *name,
535 struct hw_device_t **dev) {
536 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
537 ALOGE("Invalid module name- %s", name);
538 return -EINVAL;
539 }
540
541 struct hwc_context_t *ctx = new hwc_context_t();
542 if (!ctx) {
543 ALOGE("Failed to allocate hwc context");
544 return -ENOMEM;
545 }
546
Sean Paul6a55e9f2015-04-30 15:31:06 -0400547 int ret = ctx->drm.Init();
548 if (ret) {
549 ALOGE("Can't initialize Drm object %d", ret);
550 delete ctx;
551 return ret;
552 }
553
Sean Paulda6270d2015-06-01 14:11:52 -0400554 ctx->importer = Importer::CreateInstance(&ctx->drm);
555 if (!ctx->importer) {
556 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400557 delete ctx;
558 return ret;
559 }
560
Sean Paulef8f1f92015-04-29 16:05:23 -0400561 ret = hwc_enumerate_displays(ctx);
562 if (ret) {
563 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400564 delete ctx;
565 return ret;
566 }
567
Sean Paulef8f1f92015-04-29 16:05:23 -0400568 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
569 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
570 ctx->device.common.module = const_cast<hw_module_t *>(module);
571 ctx->device.common.close = hwc_device_close;
572
Sean Paul9046c642015-06-10 17:27:47 -0400573 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400574 ctx->device.prepare = hwc_prepare;
575 ctx->device.set = hwc_set;
576 ctx->device.eventControl = hwc_event_control;
577 ctx->device.setPowerMode = hwc_set_power_mode;
578 ctx->device.query = hwc_query;
579 ctx->device.registerProcs = hwc_register_procs;
580 ctx->device.getDisplayConfigs = hwc_get_display_configs;
581 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
582 ctx->device.getActiveConfig = hwc_get_active_config;
583 ctx->device.setActiveConfig = hwc_set_active_config;
584 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
585
586 *dev = &ctx->device.common;
587
588 return 0;
589}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400590}
Sean Paulef8f1f92015-04-29 16:05:23 -0400591
Sean Paul6a55e9f2015-04-30 15:31:06 -0400592static struct hw_module_methods_t hwc_module_methods = {
593 open : android::hwc_device_open
594};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500595
596hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400597 common : {
598 tag : HARDWARE_MODULE_TAG,
599 version_major : 1,
600 version_minor : 0,
601 id : HWC_HARDWARE_MODULE_ID,
602 name : "DRM hwcomposer module",
603 author : "The Android Open Source Project",
604 methods : &hwc_module_methods,
605 dso : NULL,
606 reserved : {0},
607 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500608};