blob: 02c13afb1f1e2476e56a3d360eb38cafac0180b7 [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"
Zach Reizner45624d32015-06-10 16:03:01 -070022#include "gl_compositor.h"
Sean Paulda6270d2015-06-01 14:11:52 -040023#include "importer.h"
Sean Paul4057be32015-05-13 06:23:09 -070024#include "vsyncworker.h"
Sean Paulef8f1f92015-04-29 16:05:23 -040025
Sean Paule0c4c3d2015-01-20 16:56:04 -050026#include <errno.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040027#include <fcntl.h>
Sean Paul5ad302c2015-05-11 10:43:31 -070028#include <list>
Sean Paule42febf2015-05-07 11:35:29 -070029#include <map>
Sean Paulef8f1f92015-04-29 16:05:23 -040030#include <pthread.h>
Dan Albertc5255b32015-05-07 23:42:54 -070031#include <stdlib.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050032#include <sys/param.h>
Sean Paul9aa5ad32015-01-22 15:47:54 -050033#include <sys/resource.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050034#include <xf86drm.h>
35#include <xf86drmMode.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050036
Sean Paulef8f1f92015-04-29 16:05:23 -040037#include <cutils/log.h>
38#include <cutils/properties.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050039#include <hardware/hardware.h>
40#include <hardware/hwcomposer.h>
Sean Paulf1dc1912015-01-24 01:34:31 -050041#include <sw_sync.h>
Sean Paulef8f1f92015-04-29 16:05:23 -040042#include <sync/sync.h>
Zach Reizner45624d32015-06-10 16:03:01 -070043#include <ui/GraphicBuffer.h>
44#include <ui/PixelFormat.h>
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -070045#include <utils/Trace.h>
Sean Paule0c4c3d2015-01-20 16:56:04 -050046
Sean Paule0c4c3d2015-01-20 16:56:04 -050047#define UM_PER_INCH 25400
Stéphane Marchesincb3f9842015-06-19 14:50:45 -070048#define HWC_FB_BUFFERS 3
Sean Paule0c4c3d2015-01-20 16:56:04 -050049
Sean Paul6a55e9f2015-04-30 15:31:06 -040050namespace android {
Sean Paule0c4c3d2015-01-20 16:56:04 -050051
Zach Reizner45624d32015-06-10 16:03:01 -070052struct hwc_drm_display_framebuffer {
53 hwc_drm_display_framebuffer() : release_fence_fd_(-1) {
54 }
55
56 ~hwc_drm_display_framebuffer() {
57 if (release_fence_fd() >= 0)
58 close(release_fence_fd());
59 }
60
61 bool is_valid() {
62 return buffer_ != NULL;
63 }
64
65 sp<GraphicBuffer> buffer() {
66 return buffer_;
67 }
68
69 int release_fence_fd() {
70 return release_fence_fd_;
71 }
72
73 void set_release_fence_fd(int fd) {
74 if (release_fence_fd_ >= 0)
75 close(release_fence_fd_);
76 release_fence_fd_ = fd;
77 }
78
79 bool Allocate(uint32_t w, uint32_t h) {
80 if (is_valid()) {
81 if (buffer_->getWidth() == w && buffer_->getHeight() == h)
82 return true;
83
84 if (release_fence_fd_ >= 0) {
85 if (sync_wait(release_fence_fd_, -1) != 0) {
86 return false;
87 }
88 }
89 Clear();
90 }
91 buffer_ = new GraphicBuffer(w, h, android::PIXEL_FORMAT_RGBA_8888,
92 GRALLOC_USAGE_HW_FB | GRALLOC_USAGE_HW_RENDER |
93 GRALLOC_USAGE_HW_COMPOSER);
94 release_fence_fd_ = -1;
95 return is_valid();
96 }
97
98 void Clear() {
99 if (!is_valid())
100 return;
101
102 if (release_fence_fd_ >= 0) {
103 close(release_fence_fd_);
104 release_fence_fd_ = -1;
105 }
106
107 buffer_.clear();
108 }
109
110 int WaitReleased(int timeout_milliseconds) {
111 if (!is_valid())
112 return 0;
113 if (release_fence_fd_ < 0)
114 return 0;
115
116 int ret = sync_wait(release_fence_fd_, timeout_milliseconds);
117 return ret;
118 }
119
120 private:
121 sp<GraphicBuffer> buffer_;
122 int release_fence_fd_;
123};
124
125
Sean Paule42febf2015-05-07 11:35:29 -0700126typedef struct hwc_drm_display {
Sean Paulef8f1f92015-04-29 16:05:23 -0400127 struct hwc_context_t *ctx;
128 int display;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500129
Sean Paul6a55e9f2015-04-30 15:31:06 -0400130 std::vector<uint32_t> config_ids;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500131
Sean Paul4057be32015-05-13 06:23:09 -0700132 VSyncWorker vsync_worker;
Zach Reizner45624d32015-06-10 16:03:01 -0700133
134 hwc_drm_display_framebuffer fb_chain[HWC_FB_BUFFERS];
135 int fb_idx;
Sean Paule42febf2015-05-07 11:35:29 -0700136} hwc_drm_display_t;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500137
138struct hwc_context_t {
Sean Paule42febf2015-05-07 11:35:29 -0700139 // map of display:hwc_drm_display_t
140 typedef std::map<int, hwc_drm_display_t> DisplayMap;
141 typedef DisplayMap::iterator DisplayMapIter;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500142
Sean Paulda6270d2015-06-01 14:11:52 -0400143 hwc_context_t() : procs(NULL), importer(NULL) {
144 }
145
146 ~hwc_context_t() {
147 delete importer;
148 }
149
Sean Paule42febf2015-05-07 11:35:29 -0700150 hwc_composer_device_1_t device;
Sean Paulef8f1f92015-04-29 16:05:23 -0400151 hwc_procs_t const *procs;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500152
Sean Paule42febf2015-05-07 11:35:29 -0700153 DisplayMap displays;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400154 DrmResources drm;
Sean Paulda6270d2015-06-01 14:11:52 -0400155 Importer *importer;
Zach Reizner45624d32015-06-10 16:03:01 -0700156 GLCompositor pre_compositor;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500157};
158
Sean Paul9046c642015-06-10 17:27:47 -0400159static void hwc_dump(struct hwc_composer_device_1* dev, char *buff,
160 int buff_len) {
161 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
162 std::ostringstream out;
163
164 ctx->drm.compositor()->Dump(&out);
165 std::string out_str = out.str();
166 strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length()));
167}
168
Sean Paulb386f1b2015-05-13 06:33:23 -0700169static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400170 hwc_display_contents_1_t **display_contents) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700171 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700172 for (int i = 0; i < (int)num_displays; ++i) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400173 if (!display_contents[i])
174 continue;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500175
Sean Paulb386f1b2015-05-13 06:33:23 -0700176 DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i);
177 if (!crtc) {
178 ALOGE("No crtc for display %d", i);
Sean Paulb386f1b2015-05-13 06:33:23 -0700179 return -ENODEV;
180 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700181
Zach Reizner45624d32015-06-10 16:03:01 -0700182 int num_layers = display_contents[i]->numHwLayers;
183 for (int j = 0; j < num_layers; j++) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700184 hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700185
Sean Paul877be972015-06-03 14:08:27 -0400186 if (layer->compositionType == HWC_FRAMEBUFFER)
187 layer->compositionType = HWC_OVERLAY;
Sean Paulef8f1f92015-04-29 16:05:23 -0400188 }
189 }
Sean Pauldffca952015-02-04 10:19:55 -0800190
Sean Paulef8f1f92015-04-29 16:05:23 -0400191 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500192}
193
Sean Paulb386f1b2015-05-13 06:33:23 -0700194static void hwc_set_cleanup(size_t num_displays,
195 hwc_display_contents_1_t **display_contents,
196 Composition *composition) {
197 for (int i = 0; i < (int)num_displays; ++i) {
198 if (!display_contents[i])
199 continue;
200
201 hwc_display_contents_1_t *dc = display_contents[i];
202 for (size_t j = 0; j < dc->numHwLayers; ++j) {
203 hwc_layer_1_t *layer = &dc->hwLayers[j];
204 if (layer->acquireFenceFd >= 0) {
205 close(layer->acquireFenceFd);
206 layer->acquireFenceFd = -1;
207 }
208 }
209 if (dc->outbufAcquireFenceFd >= 0) {
210 close(dc->outbufAcquireFenceFd);
211 dc->outbufAcquireFenceFd = -1;
212 }
213 }
214
215 delete composition;
216}
217
Sean Paulb386f1b2015-05-13 06:33:23 -0700218static int hwc_add_layer(int display, hwc_context_t *ctx, hwc_layer_1_t *layer,
219 Composition *composition) {
220 hwc_drm_bo_t bo;
221 int ret = ctx->importer->ImportBuffer(layer->handle, &bo);
222 if (ret) {
223 ALOGE("Failed to import handle to bo %d", ret);
224 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400225 }
Sean Paulefb20cb2015-02-04 09:29:15 -0800226
Sean Paulb386f1b2015-05-13 06:33:23 -0700227 ret = composition->AddLayer(display, layer, &bo);
228 if (!ret)
Sean Paulef8f1f92015-04-29 16:05:23 -0400229 return 0;
Sean Paul9aa5ad32015-01-22 15:47:54 -0500230
Sean Paulb386f1b2015-05-13 06:33:23 -0700231 int destroy_ret = ctx->importer->ReleaseBuffer(&bo);
232 if (destroy_ret)
233 ALOGE("Failed to destroy buffer %d", destroy_ret);
Sean Paul9aa5ad32015-01-22 15:47:54 -0500234
Sean Paulef8f1f92015-04-29 16:05:23 -0400235 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500236}
237
238static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays,
Sean Paulef8f1f92015-04-29 16:05:23 -0400239 hwc_display_contents_1_t **display_contents) {
Stéphane Marchesinbe98c8c2015-06-23 16:18:10 -0700240 ATRACE_CALL();
Sean Paulef8f1f92015-04-29 16:05:23 -0400241 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paulb386f1b2015-05-13 06:33:23 -0700242 Composition *composition =
243 ctx->drm.compositor()->CreateComposition(ctx->importer);
244 if (!composition) {
245 ALOGE("Drm composition init failed");
246 hwc_set_cleanup(num_displays, display_contents, NULL);
247 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400248 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500249
Sean Paulb386f1b2015-05-13 06:33:23 -0700250 int ret;
251 for (int i = 0; i < (int)num_displays; ++i) {
252 if (!display_contents[i])
253 continue;
254
Zach Reizner45624d32015-06-10 16:03:01 -0700255 hwc_display_contents_1_t *dc = display_contents[i];
256 int j;
257 unsigned num_layers = 0;
258 unsigned num_dc_layers = dc->numHwLayers;
259 for (j = 0; j < (int)num_dc_layers; ++j) {
Sean Paulb386f1b2015-05-13 06:33:23 -0700260 hwc_layer_1_t *layer = &dc->hwLayers[j];
Zach Reizner45624d32015-06-10 16:03:01 -0700261 if (layer->flags & HWC_SKIP_LAYER)
Sean Paulb386f1b2015-05-13 06:33:23 -0700262 continue;
Sean Paul877be972015-06-03 14:08:27 -0400263 if (layer->compositionType == HWC_OVERLAY)
Zach Reizner45624d32015-06-10 16:03:01 -0700264 num_layers++;
Zach Reizner45624d32015-06-10 16:03:01 -0700265 }
266
267 unsigned num_planes = composition->GetRemainingLayers(i, num_layers);
268 bool use_pre_compositor = false;
269
Sean Paul877be972015-06-03 14:08:27 -0400270 if (num_layers > num_planes) {
Zach Reizner45624d32015-06-10 16:03:01 -0700271 use_pre_compositor = true;
272 // Reserve one of the planes for the result of the pre compositor.
273 num_planes--;
274 }
275
276 for (j = 0; num_planes && j < (int)num_dc_layers; ++j) {
277 hwc_layer_1_t *layer = &dc->hwLayers[j];
278 if (layer->flags & HWC_SKIP_LAYER)
279 continue;
Sean Paul877be972015-06-03 14:08:27 -0400280 if (layer->compositionType != HWC_OVERLAY)
281 continue;
Sean Paulb386f1b2015-05-13 06:33:23 -0700282
283 ret = hwc_add_layer(i, ctx, layer, composition);
284 if (ret) {
285 ALOGE("Add layer failed %d", ret);
286 hwc_set_cleanup(num_displays, display_contents, composition);
287 return ret;
288 }
289 --num_planes;
290 }
Zach Reizner45624d32015-06-10 16:03:01 -0700291
292 int last_comp_layer = j;
293
294 if (use_pre_compositor) {
295 hwc_drm_display_t *hd = &ctx->displays[i];
296 struct hwc_drm_display_framebuffer *fb = &hd->fb_chain[hd->fb_idx];
297 ret = fb->WaitReleased(-1);
298 if (ret) {
299 ALOGE("Failed to wait for framebuffer %d", ret);
300 hwc_set_cleanup(num_displays, display_contents, composition);
301 return ret;
302 }
303
304 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(i);
305 if (!connector) {
306 ALOGE("No connector for display %d", i);
307 hwc_set_cleanup(num_displays, display_contents, composition);
308 return -ENODEV;
309 }
310
311 const DrmMode &mode = connector->active_mode();
312 if (!fb->Allocate(mode.h_display(), mode.v_display())) {
313 ALOGE("Failed to allocate framebuffer with size %dx%d",
314 mode.h_display(), mode.v_display());
315 hwc_set_cleanup(num_displays, display_contents, composition);
316 return -EINVAL;
317 }
318
319 sp<GraphicBuffer> fb_buffer = fb->buffer();
320 if (fb_buffer == NULL) {
321 ALOGE("Framebuffer is NULL");
322 hwc_set_cleanup(num_displays, display_contents, composition);
323 return -EINVAL;
324 }
325
326 Targeting *targeting = ctx->pre_compositor.targeting();
327 if (targeting == NULL) {
328 ALOGE("Pre-compositor does not support targeting");
329 hwc_set_cleanup(num_displays, display_contents, composition);
330 return -EINVAL;
331 }
332
333 int target = targeting->CreateTarget(fb_buffer);
334 targeting->SetTarget(target);
335
336 Composition *pre_composition = ctx->pre_compositor.CreateComposition(ctx->importer);
337 if (pre_composition == NULL) {
338 ALOGE("Failed to create pre-composition");
339 targeting->ForgetTarget(target);
340 hwc_set_cleanup(num_displays, display_contents, composition);
341 return -EINVAL;
342 }
343
344 for (j = last_comp_layer; j < (int)num_dc_layers; ++j) {
345 hwc_layer_1_t *layer = &dc->hwLayers[j];
346 if (layer->flags & HWC_SKIP_LAYER)
347 continue;
348 if (layer->compositionType != HWC_OVERLAY)
349 continue;
350 ret = hwc_add_layer(i, ctx, layer, pre_composition);
351 if (ret) {
352 ALOGE("Add layer failed %d", ret);
353 delete pre_composition;
354 targeting->ForgetTarget(target);
355 hwc_set_cleanup(num_displays, display_contents, composition);
356 return ret;
357 }
358 }
359
360 ret = ctx->pre_compositor.QueueComposition(pre_composition);
361 pre_composition = NULL;
362
363 targeting->ForgetTarget(target);
364 if (ret < 0 && ret != -EALREADY) {
365 ALOGE("Pre-composition failed %d", ret);
366 hwc_set_cleanup(num_displays, display_contents, composition);
367 return ret;
368 }
369
370 for (j = last_comp_layer; j < (int)num_dc_layers; ++j) {
371 hwc_layer_1_t *layer = &dc->hwLayers[j];
372 if (layer->flags & HWC_SKIP_LAYER)
373 continue;
374 if (layer->compositionType != HWC_OVERLAY)
375 continue;
376 layer->acquireFenceFd = -1;
377 }
378
379 hwc_layer_1_t composite_layer;
380 hwc_rect_t visible_rect;
381 memset(&composite_layer, 0, sizeof(composite_layer));
382 memset(&visible_rect, 0, sizeof(visible_rect));
383
384 composite_layer.compositionType = HWC_OVERLAY;
385 composite_layer.handle = fb_buffer->getNativeBuffer()->handle;
386 composite_layer.sourceCropf.right = composite_layer.displayFrame.right =
387 visible_rect.right = fb_buffer->getWidth();
388 composite_layer.sourceCropf.bottom = composite_layer.displayFrame.bottom =
389 visible_rect.bottom = fb_buffer->getHeight();
390 composite_layer.visibleRegionScreen.numRects = 1;
391 composite_layer.visibleRegionScreen.rects = &visible_rect;
392 composite_layer.acquireFenceFd = ret == -EALREADY ? -1 : ret;
393 // A known invalid fd in case AddLayer does not modify this field.
394 composite_layer.releaseFenceFd = -1;
395 composite_layer.planeAlpha = 0xff;
396
397 ret = hwc_add_layer(i, ctx, &composite_layer, composition);
398 if (ret) {
399 ALOGE("Add layer failed %d", ret);
400 hwc_set_cleanup(num_displays, display_contents, composition);
401 return ret;
402 }
403
404 fb->set_release_fence_fd(composite_layer.releaseFenceFd);
405 hd->fb_idx = (hd->fb_idx + 1) % HWC_FB_BUFFERS;
406 }
Sean Paulb386f1b2015-05-13 06:33:23 -0700407 }
408
409 ret = ctx->drm.compositor()->QueueComposition(composition);
410 if (ret) {
411 ALOGE("Failed to queue the composition");
412 hwc_set_cleanup(num_displays, display_contents, composition);
413 return ret;
414 }
415 hwc_set_cleanup(num_displays, display_contents, NULL);
Sean Paulef8f1f92015-04-29 16:05:23 -0400416 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500417}
418
Sean Paulef8f1f92015-04-29 16:05:23 -0400419static int hwc_event_control(struct hwc_composer_device_1 *dev, int display,
420 int event, int enabled) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400421 if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1))
422 return -EINVAL;
Sean Pauleb9e75c2015-01-25 23:31:30 -0500423
Sean Paul4057be32015-05-13 06:23:09 -0700424 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
425 hwc_drm_display_t *hd = &ctx->displays[display];
426 return hd->vsync_worker.VSyncControl(enabled);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500427}
428
Sean Paulef8f1f92015-04-29 16:05:23 -0400429static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display,
430 int mode) {
431 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500432
Sean Paul6a55e9f2015-04-30 15:31:06 -0400433 uint64_t dpmsValue = 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400434 switch (mode) {
435 case HWC_POWER_MODE_OFF:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400436 dpmsValue = DRM_MODE_DPMS_OFF;
Sean Paulef8f1f92015-04-29 16:05:23 -0400437 break;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500438
Sean Paulef8f1f92015-04-29 16:05:23 -0400439 /* We can't support dozing right now, so go full on */
440 case HWC_POWER_MODE_DOZE:
441 case HWC_POWER_MODE_DOZE_SUSPEND:
442 case HWC_POWER_MODE_NORMAL:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400443 dpmsValue = DRM_MODE_DPMS_ON;
Sean Paulef8f1f92015-04-29 16:05:23 -0400444 break;
445 };
Sean Paul6a55e9f2015-04-30 15:31:06 -0400446 return ctx->drm.SetDpmsMode(display, dpmsValue);
Sean Paule0c4c3d2015-01-20 16:56:04 -0500447}
448
Sean Paulef8f1f92015-04-29 16:05:23 -0400449static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what,
450 int *value) {
451 switch (what) {
452 case HWC_BACKGROUND_LAYER_SUPPORTED:
453 *value = 0; /* TODO: We should do this */
454 break;
455 case HWC_VSYNC_PERIOD:
456 ALOGW("Query for deprecated vsync value, returning 60Hz");
457 *value = 1000 * 1000 * 1000 / 60;
458 break;
459 case HWC_DISPLAY_TYPES_SUPPORTED:
460 *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL;
461 break;
462 }
463 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500464}
465
Sean Paulef8f1f92015-04-29 16:05:23 -0400466static void hwc_register_procs(struct hwc_composer_device_1 *dev,
467 hwc_procs_t const *procs) {
468 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500469
Sean Paulef8f1f92015-04-29 16:05:23 -0400470 ctx->procs = procs;
Sean Paul4057be32015-05-13 06:23:09 -0700471
472 for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin();
473 iter != ctx->displays.end(); ++iter) {
474 iter->second.vsync_worker.SetProcs(procs);
475 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500476}
477
Sean Paulef8f1f92015-04-29 16:05:23 -0400478static int hwc_get_display_configs(struct hwc_composer_device_1 *dev,
479 int display, uint32_t *configs,
Sean Paul6a55e9f2015-04-30 15:31:06 -0400480 size_t *num_configs) {
481 if (!*num_configs)
Sean Paulef8f1f92015-04-29 16:05:23 -0400482 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500483
Sean Paulef8f1f92015-04-29 16:05:23 -0400484 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700485 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400486 hd->config_ids.clear();
487
488 DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display);
489 if (!connector) {
490 ALOGE("Failed to get connector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400491 return -ENODEV;
492 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500493
Sean Paule42febf2015-05-07 11:35:29 -0700494 int ret = connector->UpdateModes();
Sean Paul6a55e9f2015-04-30 15:31:06 -0400495 if (ret) {
496 ALOGE("Failed to update display modes %d", ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400497 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400498 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500499
Sean Paul6a55e9f2015-04-30 15:31:06 -0400500 for (DrmConnector::ModeIter iter = connector->begin_modes();
501 iter != connector->end_modes(); ++iter) {
502 size_t idx = hd->config_ids.size();
503 if (idx == *num_configs)
504 break;
505 hd->config_ids.push_back(iter->id());
506 configs[idx] = iter->id();
507 }
508 *num_configs = hd->config_ids.size();
509 return *num_configs == 0 ? -1 : 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500510}
511
Sean Paulef8f1f92015-04-29 16:05:23 -0400512static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev,
513 int display, uint32_t config,
514 const uint32_t *attributes,
515 int32_t *values) {
516 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400517 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400518 if (!c) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400519 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400520 return -ENODEV;
521 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400522 DrmMode mode;
523 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
524 ++iter) {
525 if (iter->id() == config) {
526 mode = *iter;
527 break;
528 }
529 }
530 if (mode.id() == 0) {
531 ALOGE("Failed to find active mode for display %d", display);
532 return -ENOENT;
Sean Paulef8f1f92015-04-29 16:05:23 -0400533 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500534
Sean Paul6a55e9f2015-04-30 15:31:06 -0400535 uint32_t mm_width = c->mm_width();
536 uint32_t mm_height = c->mm_height();
Sean Paulef8f1f92015-04-29 16:05:23 -0400537 for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) {
538 switch (attributes[i]) {
539 case HWC_DISPLAY_VSYNC_PERIOD:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400540 values[i] = 1000 * 1000 * 1000 / mode.v_refresh();
Sean Paulef8f1f92015-04-29 16:05:23 -0400541 break;
542 case HWC_DISPLAY_WIDTH:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400543 values[i] = mode.h_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400544 break;
545 case HWC_DISPLAY_HEIGHT:
Sean Paul6a55e9f2015-04-30 15:31:06 -0400546 values[i] = mode.v_display();
Sean Paulef8f1f92015-04-29 16:05:23 -0400547 break;
548 case HWC_DISPLAY_DPI_X:
549 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400550 values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400551 break;
552 case HWC_DISPLAY_DPI_Y:
553 /* Dots per 1000 inches */
Sean Paul6a55e9f2015-04-30 15:31:06 -0400554 values[i] =
555 mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0;
Sean Paulef8f1f92015-04-29 16:05:23 -0400556 break;
557 }
558 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400559 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500560}
561
Sean Paulef8f1f92015-04-29 16:05:23 -0400562static int hwc_get_active_config(struct hwc_composer_device_1 *dev,
563 int display) {
564 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paul6a55e9f2015-04-30 15:31:06 -0400565 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
566 if (!c) {
567 ALOGE("Failed to get DrmConnector for display %d", display);
Sean Paulef8f1f92015-04-29 16:05:23 -0400568 return -ENODEV;
569 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500570
Sean Paul6a55e9f2015-04-30 15:31:06 -0400571 DrmMode mode = c->active_mode();
Sean Paule42febf2015-05-07 11:35:29 -0700572 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400573 for (size_t i = 0; i < hd->config_ids.size(); ++i) {
574 if (hd->config_ids[i] == mode.id())
575 return i;
Sean Paulef8f1f92015-04-29 16:05:23 -0400576 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400577 return -1;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500578}
579
Sean Paulef8f1f92015-04-29 16:05:23 -0400580static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display,
581 int index) {
582 struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common;
Sean Paule42febf2015-05-07 11:35:29 -0700583 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paul6a55e9f2015-04-30 15:31:06 -0400584 if (index >= (int)hd->config_ids.size()) {
585 ALOGE("Invalid config index %d passed in", index);
586 return -EINVAL;
Sean Paulef8f1f92015-04-29 16:05:23 -0400587 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500588
Sean Paul877be972015-06-03 14:08:27 -0400589 DrmConnector *c = ctx->drm.GetConnectorForDisplay(display);
590 if (!c) {
591 ALOGE("Failed to get connector for display %d", display);
592 return -ENODEV;
593 }
594 DrmMode mode;
595 for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes();
596 ++iter) {
597 if (iter->id() == hd->config_ids[index]) {
598 mode = *iter;
599 break;
600 }
601 }
602 if (mode.id() != hd->config_ids[index]) {
603 ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]);
604 return -ENOENT;
605 }
606 int ret = ctx->drm.SetDisplayActiveMode(display, mode);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400607 if (ret) {
Sean Paul877be972015-06-03 14:08:27 -0400608 ALOGE("Failed to set active config %d", ret);
Sean Paul6a55e9f2015-04-30 15:31:06 -0400609 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400610 }
Sean Paul6a55e9f2015-04-30 15:31:06 -0400611 return ret;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500612}
613
Sean Paulef8f1f92015-04-29 16:05:23 -0400614static int hwc_device_close(struct hw_device_t *dev) {
615 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
Sean Paulef8f1f92015-04-29 16:05:23 -0400616 delete ctx;
Sean Paulef8f1f92015-04-29 16:05:23 -0400617 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500618}
619
Sean Paul24a26e32015-02-04 10:34:47 -0800620/*
621 * TODO: This function sets the active config to the first one in the list. This
622 * should be fixed such that it selects the preferred mode for the display, or
623 * some other, saner, method of choosing the config.
624 */
Sean Paule42febf2015-05-07 11:35:29 -0700625static int hwc_set_initial_config(hwc_drm_display_t *hd) {
Sean Paulef8f1f92015-04-29 16:05:23 -0400626 uint32_t config;
627 size_t num_configs = 1;
628 int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
629 &num_configs);
630 if (ret || !num_configs)
631 return 0;
Sean Paul24a26e32015-02-04 10:34:47 -0800632
Sean Paulef8f1f92015-04-29 16:05:23 -0400633 ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
634 if (ret) {
635 ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret);
636 return ret;
637 }
Sean Paul24a26e32015-02-04 10:34:47 -0800638
Sean Paulef8f1f92015-04-29 16:05:23 -0400639 return ret;
Sean Paul24a26e32015-02-04 10:34:47 -0800640}
641
Sean Paul6a55e9f2015-04-30 15:31:06 -0400642static int hwc_initialize_display(struct hwc_context_t *ctx, int display) {
Sean Paule42febf2015-05-07 11:35:29 -0700643 hwc_drm_display_t *hd = &ctx->displays[display];
Sean Paulef8f1f92015-04-29 16:05:23 -0400644 hd->ctx = ctx;
645 hd->display = display;
Zach Reizner45624d32015-06-10 16:03:01 -0700646 hd->fb_idx = 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500647
Sean Paulb386f1b2015-05-13 06:33:23 -0700648 int ret = hwc_set_initial_config(hd);
Sean Paulef8f1f92015-04-29 16:05:23 -0400649 if (ret) {
650 ALOGE("Failed to set initial config for d=%d ret=%d", display, ret);
Sean Paulef8f1f92015-04-29 16:05:23 -0400651 return ret;
652 }
Sean Paul24a26e32015-02-04 10:34:47 -0800653
Sean Paul4057be32015-05-13 06:23:09 -0700654 ret = hd->vsync_worker.Init(&ctx->drm, display);
655 if (ret) {
656 ALOGE("Failed to create event worker for display %d %d\n", display, ret);
657 return ret;
658 }
659
Sean Paulef8f1f92015-04-29 16:05:23 -0400660 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500661}
662
Sean Paulef8f1f92015-04-29 16:05:23 -0400663static int hwc_enumerate_displays(struct hwc_context_t *ctx) {
Sean Paul6a55e9f2015-04-30 15:31:06 -0400664 int ret;
665 for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors();
666 c != ctx->drm.end_connectors(); ++c) {
667 ret = hwc_initialize_display(ctx, (*c)->display());
668 if (ret) {
669 ALOGE("Failed to initialize display %d", (*c)->display());
670 return ret;
Sean Paulef8f1f92015-04-29 16:05:23 -0400671 }
672 }
Sean Paulef8f1f92015-04-29 16:05:23 -0400673
674 return 0;
Sean Paule0c4c3d2015-01-20 16:56:04 -0500675}
676
Sean Paulef8f1f92015-04-29 16:05:23 -0400677static int hwc_device_open(const struct hw_module_t *module, const char *name,
678 struct hw_device_t **dev) {
679 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
680 ALOGE("Invalid module name- %s", name);
681 return -EINVAL;
682 }
683
684 struct hwc_context_t *ctx = new hwc_context_t();
685 if (!ctx) {
686 ALOGE("Failed to allocate hwc context");
687 return -ENOMEM;
688 }
689
Sean Paul6a55e9f2015-04-30 15:31:06 -0400690 int ret = ctx->drm.Init();
691 if (ret) {
692 ALOGE("Can't initialize Drm object %d", ret);
693 delete ctx;
694 return ret;
695 }
696
Zach Reizner45624d32015-06-10 16:03:01 -0700697 ret = ctx->pre_compositor.Init();
698 if (ret) {
699 ALOGE("Can't initialize OpenGL Compositor object %d", ret);
700 delete ctx;
701 return ret;
702 }
703
Sean Paulda6270d2015-06-01 14:11:52 -0400704 ctx->importer = Importer::CreateInstance(&ctx->drm);
705 if (!ctx->importer) {
706 ALOGE("Failed to create importer instance");
Sean Paulef8f1f92015-04-29 16:05:23 -0400707 delete ctx;
708 return ret;
709 }
710
Sean Paulef8f1f92015-04-29 16:05:23 -0400711 ret = hwc_enumerate_displays(ctx);
712 if (ret) {
713 ALOGE("Failed to enumerate displays: %s", strerror(ret));
Sean Paul6a55e9f2015-04-30 15:31:06 -0400714 delete ctx;
715 return ret;
716 }
717
Sean Paulef8f1f92015-04-29 16:05:23 -0400718 ctx->device.common.tag = HARDWARE_DEVICE_TAG;
719 ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4;
720 ctx->device.common.module = const_cast<hw_module_t *>(module);
721 ctx->device.common.close = hwc_device_close;
722
Sean Paul9046c642015-06-10 17:27:47 -0400723 ctx->device.dump = hwc_dump;
Sean Paulef8f1f92015-04-29 16:05:23 -0400724 ctx->device.prepare = hwc_prepare;
725 ctx->device.set = hwc_set;
726 ctx->device.eventControl = hwc_event_control;
727 ctx->device.setPowerMode = hwc_set_power_mode;
728 ctx->device.query = hwc_query;
729 ctx->device.registerProcs = hwc_register_procs;
730 ctx->device.getDisplayConfigs = hwc_get_display_configs;
731 ctx->device.getDisplayAttributes = hwc_get_display_attributes;
732 ctx->device.getActiveConfig = hwc_get_active_config;
733 ctx->device.setActiveConfig = hwc_set_active_config;
734 ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */
735
736 *dev = &ctx->device.common;
737
738 return 0;
739}
Sean Paul6a55e9f2015-04-30 15:31:06 -0400740}
Sean Paulef8f1f92015-04-29 16:05:23 -0400741
Sean Paul6a55e9f2015-04-30 15:31:06 -0400742static struct hw_module_methods_t hwc_module_methods = {
743 open : android::hwc_device_open
744};
Sean Paule0c4c3d2015-01-20 16:56:04 -0500745
746hwc_module_t HAL_MODULE_INFO_SYM = {
Sean Paulef8f1f92015-04-29 16:05:23 -0400747 common : {
748 tag : HARDWARE_MODULE_TAG,
749 version_major : 1,
750 version_minor : 0,
751 id : HWC_HARDWARE_MODULE_ID,
752 name : "DRM hwcomposer module",
753 author : "The Android Open Source Project",
754 methods : &hwc_module_methods,
755 dso : NULL,
756 reserved : {0},
757 }
Sean Paule0c4c3d2015-01-20 16:56:04 -0500758};