Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 1 | /* |
| 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 Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 18 | #define LOG_TAG "hwcomposer-drm" |
| 19 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 20 | #include "drm_hwcomposer.h" |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 21 | #include "drmresources.h" |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 22 | #include "importer.h" |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 23 | #include "vsyncworker.h" |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 24 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include <map> |
| 28 | #include <vector> |
| 29 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 30 | #include <errno.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 31 | #include <fcntl.h> |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 32 | #include <pthread.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 33 | #include <sys/param.h> |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 34 | #include <sys/resource.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 35 | #include <xf86drm.h> |
| 36 | #include <xf86drmMode.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 37 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 38 | #include <cutils/log.h> |
| 39 | #include <cutils/properties.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 40 | #include <hardware/hardware.h> |
| 41 | #include <hardware/hwcomposer.h> |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 42 | #include <utils/Trace.h> |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 43 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 44 | #define UM_PER_INCH 25400 |
Stéphane Marchesin | cb3f984 | 2015-06-19 14:50:45 -0700 | [diff] [blame] | 45 | #define HWC_FB_BUFFERS 3 |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 46 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 47 | namespace android { |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 48 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 49 | typedef struct hwc_drm_display { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 50 | struct hwc_context_t *ctx; |
| 51 | int display; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 52 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 53 | std::vector<uint32_t> config_ids; |
Sean Paul | 9aa5ad3 | 2015-01-22 15:47:54 -0500 | [diff] [blame] | 54 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 55 | VSyncWorker vsync_worker; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 56 | } hwc_drm_display_t; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 57 | |
| 58 | struct hwc_context_t { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 59 | // map of display:hwc_drm_display_t |
| 60 | typedef std::map<int, hwc_drm_display_t> DisplayMap; |
| 61 | typedef DisplayMap::iterator DisplayMapIter; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 62 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 63 | hwc_context_t() : procs(NULL), importer(NULL) { |
| 64 | } |
| 65 | |
| 66 | ~hwc_context_t() { |
| 67 | delete importer; |
| 68 | } |
| 69 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 70 | hwc_composer_device_1_t device; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 71 | hwc_procs_t const *procs; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 72 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 73 | DisplayMap displays; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 74 | DrmResources drm; |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 75 | Importer *importer; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 76 | }; |
| 77 | |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 78 | static void hwc_dump(struct hwc_composer_device_1 *dev, char *buff, |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 79 | int buff_len) { |
| 80 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 81 | std::ostringstream out; |
| 82 | |
| 83 | ctx->drm.compositor()->Dump(&out); |
| 84 | std::string out_str = out.str(); |
| 85 | strncpy(buff, out_str.c_str(), std::min((size_t)buff_len, out_str.length())); |
| 86 | } |
| 87 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 88 | static int hwc_prepare(hwc_composer_device_1_t *dev, size_t num_displays, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 89 | hwc_display_contents_1_t **display_contents) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 90 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 91 | for (int i = 0; i < (int)num_displays; ++i) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 92 | if (!display_contents[i]) |
| 93 | continue; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 94 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 95 | DrmCrtc *crtc = ctx->drm.GetCrtcForDisplay(i); |
| 96 | if (!crtc) { |
| 97 | ALOGE("No crtc for display %d", i); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 98 | return -ENODEV; |
| 99 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 100 | |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 101 | int num_layers = display_contents[i]->numHwLayers; |
| 102 | for (int j = 0; j < num_layers; j++) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 103 | hwc_layer_1_t *layer = &display_contents[i]->hwLayers[j]; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 104 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 105 | if (layer->compositionType == HWC_FRAMEBUFFER) |
| 106 | layer->compositionType = HWC_OVERLAY; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 107 | } |
| 108 | } |
Sean Paul | dffca95 | 2015-02-04 10:19:55 -0800 | [diff] [blame] | 109 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 110 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 113 | static void hwc_set_cleanup(size_t num_displays, |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 114 | hwc_display_contents_1_t **display_contents) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 115 | for (int i = 0; i < (int)num_displays; ++i) { |
| 116 | if (!display_contents[i]) |
| 117 | continue; |
| 118 | |
| 119 | hwc_display_contents_1_t *dc = display_contents[i]; |
| 120 | for (size_t j = 0; j < dc->numHwLayers; ++j) { |
| 121 | hwc_layer_1_t *layer = &dc->hwLayers[j]; |
| 122 | if (layer->acquireFenceFd >= 0) { |
| 123 | close(layer->acquireFenceFd); |
| 124 | layer->acquireFenceFd = -1; |
| 125 | } |
| 126 | } |
| 127 | if (dc->outbufAcquireFenceFd >= 0) { |
| 128 | close(dc->outbufAcquireFenceFd); |
| 129 | dc->outbufAcquireFenceFd = -1; |
| 130 | } |
| 131 | } |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 134 | static void hwc_add_layer_to_retire_fence( |
| 135 | hwc_layer_1_t *layer, hwc_display_contents_1_t *display_contents) { |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 136 | if (layer->releaseFenceFd < 0) |
| 137 | return; |
| 138 | |
| 139 | if (display_contents->retireFenceFd >= 0) { |
| 140 | int old_retire_fence = display_contents->retireFenceFd; |
Zach Reizner | c6520e4 | 2015-08-13 14:32:09 -0700 | [diff] [blame] | 141 | display_contents->retireFenceFd = |
| 142 | sync_merge("dc_retire", old_retire_fence, layer->releaseFenceFd); |
Sean Paul | 0420612 | 2015-07-16 15:59:24 -0400 | [diff] [blame] | 143 | close(old_retire_fence); |
| 144 | } else { |
| 145 | display_contents->retireFenceFd = dup(layer->releaseFenceFd); |
| 146 | } |
| 147 | } |
| 148 | |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 149 | static int hwc_set(hwc_composer_device_1_t *dev, size_t num_displays, |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 150 | hwc_display_contents_1_t **display_contents) { |
Stéphane Marchesin | be98c8c | 2015-06-23 16:18:10 -0700 | [diff] [blame] | 151 | ATRACE_CALL(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 152 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 153 | int ret; |
| 154 | std::unique_ptr<DrmComposition> composition( |
| 155 | ctx->drm.compositor()->CreateComposition(ctx->importer)); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 156 | if (!composition) { |
| 157 | ALOGE("Drm composition init failed"); |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 158 | hwc_set_cleanup(num_displays, display_contents); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 159 | return -EINVAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 160 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 161 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 162 | std::vector<DrmCompositionDisplayLayersMap> layers_map; |
| 163 | std::vector<std::vector<size_t>> layers_indices; |
| 164 | layers_map.reserve(num_displays); |
| 165 | layers_indices.reserve(num_displays); |
| 166 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 167 | for (int i = 0; i < (int)num_displays; ++i) { |
| 168 | if (!display_contents[i]) |
| 169 | continue; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 170 | hwc_display_contents_1_t *dc = display_contents[i]; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 171 | |
| 172 | layers_map.emplace_back(); |
| 173 | DrmCompositionDisplayLayersMap &map = layers_map[i]; |
| 174 | map.display = i; |
| 175 | map.layers = dc->hwLayers; |
| 176 | |
| 177 | std::vector<size_t> indices_to_composite; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 178 | unsigned num_dc_layers = dc->numHwLayers; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 179 | for (int j = 0; j < (int)num_dc_layers; ++j) { |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 180 | hwc_layer_1_t *layer = &dc->hwLayers[j]; |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 181 | if (layer->flags & HWC_SKIP_LAYER) |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 182 | continue; |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 183 | if (layer->compositionType == HWC_OVERLAY) |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 184 | indices_to_composite.push_back(j); |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 187 | map.num_layers = indices_to_composite.size(); |
| 188 | layers_indices.emplace_back(std::move(indices_to_composite)); |
| 189 | map.layer_indices = layers_indices.back().data(); |
| 190 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 191 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 192 | ret = composition->SetLayers(layers_map.size(), layers_map.data()); |
| 193 | if (ret) { |
| 194 | hwc_set_cleanup(num_displays, display_contents); |
| 195 | return -EINVAL; |
| 196 | } |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 197 | |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 198 | ret = ctx->drm.compositor()->QueueComposition(std::move(composition)); |
| 199 | if (ret) { |
| 200 | hwc_set_cleanup(num_displays, display_contents); |
| 201 | return -EINVAL; |
| 202 | } |
| 203 | |
| 204 | composition.reset(NULL); |
| 205 | |
| 206 | for (int i = 0; i < (int)num_displays; ++i) { |
| 207 | if (!display_contents[i]) |
| 208 | continue; |
| 209 | hwc_display_contents_1_t *dc = display_contents[i]; |
| 210 | unsigned num_dc_layers = dc->numHwLayers; |
| 211 | for (int j = 0; j < (int)num_dc_layers; ++j) { |
Zach Reizner | 45624d3 | 2015-06-10 16:03:01 -0700 | [diff] [blame] | 212 | hwc_layer_1_t *layer = &dc->hwLayers[j]; |
| 213 | if (layer->flags & HWC_SKIP_LAYER) |
| 214 | continue; |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 215 | if (layer->compositionType == HWC_OVERLAY) |
| 216 | hwc_add_layer_to_retire_fence(layer, dc); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 220 | if (ret) { |
| 221 | ALOGE("Failed to queue the composition"); |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 222 | } |
Zach Reizner | 0980705 | 2015-08-13 14:53:41 -0700 | [diff] [blame^] | 223 | hwc_set_cleanup(num_displays, display_contents); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 224 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 227 | static int hwc_event_control(struct hwc_composer_device_1 *dev, int display, |
| 228 | int event, int enabled) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 229 | if (event != HWC_EVENT_VSYNC || (enabled != 0 && enabled != 1)) |
| 230 | return -EINVAL; |
Sean Paul | eb9e75c | 2015-01-25 23:31:30 -0500 | [diff] [blame] | 231 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 232 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
| 233 | hwc_drm_display_t *hd = &ctx->displays[display]; |
| 234 | return hd->vsync_worker.VSyncControl(enabled); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 237 | static int hwc_set_power_mode(struct hwc_composer_device_1 *dev, int display, |
| 238 | int mode) { |
| 239 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 240 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 241 | uint64_t dpmsValue = 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 242 | switch (mode) { |
| 243 | case HWC_POWER_MODE_OFF: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 244 | dpmsValue = DRM_MODE_DPMS_OFF; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 245 | break; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 246 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 247 | /* We can't support dozing right now, so go full on */ |
| 248 | case HWC_POWER_MODE_DOZE: |
| 249 | case HWC_POWER_MODE_DOZE_SUSPEND: |
| 250 | case HWC_POWER_MODE_NORMAL: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 251 | dpmsValue = DRM_MODE_DPMS_ON; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 252 | break; |
| 253 | }; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 254 | return ctx->drm.SetDpmsMode(display, dpmsValue); |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 255 | } |
| 256 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 257 | static int hwc_query(struct hwc_composer_device_1 * /* dev */, int what, |
| 258 | int *value) { |
| 259 | switch (what) { |
| 260 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 261 | *value = 0; /* TODO: We should do this */ |
| 262 | break; |
| 263 | case HWC_VSYNC_PERIOD: |
| 264 | ALOGW("Query for deprecated vsync value, returning 60Hz"); |
| 265 | *value = 1000 * 1000 * 1000 / 60; |
| 266 | break; |
| 267 | case HWC_DISPLAY_TYPES_SUPPORTED: |
| 268 | *value = HWC_DISPLAY_PRIMARY | HWC_DISPLAY_EXTERNAL; |
| 269 | break; |
| 270 | } |
| 271 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 274 | static void hwc_register_procs(struct hwc_composer_device_1 *dev, |
| 275 | hwc_procs_t const *procs) { |
| 276 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 277 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 278 | ctx->procs = procs; |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 279 | |
| 280 | for (hwc_context_t::DisplayMapIter iter = ctx->displays.begin(); |
| 281 | iter != ctx->displays.end(); ++iter) { |
| 282 | iter->second.vsync_worker.SetProcs(procs); |
| 283 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 286 | static int hwc_get_display_configs(struct hwc_composer_device_1 *dev, |
| 287 | int display, uint32_t *configs, |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 288 | size_t *num_configs) { |
| 289 | if (!*num_configs) |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 290 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 291 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 292 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 293 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 294 | hd->config_ids.clear(); |
| 295 | |
| 296 | DrmConnector *connector = ctx->drm.GetConnectorForDisplay(display); |
| 297 | if (!connector) { |
| 298 | ALOGE("Failed to get connector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 299 | return -ENODEV; |
| 300 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 301 | |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 302 | int ret = connector->UpdateModes(); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 303 | if (ret) { |
| 304 | ALOGE("Failed to update display modes %d", ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 305 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 306 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 307 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 308 | for (DrmConnector::ModeIter iter = connector->begin_modes(); |
| 309 | iter != connector->end_modes(); ++iter) { |
| 310 | size_t idx = hd->config_ids.size(); |
| 311 | if (idx == *num_configs) |
| 312 | break; |
| 313 | hd->config_ids.push_back(iter->id()); |
| 314 | configs[idx] = iter->id(); |
| 315 | } |
| 316 | *num_configs = hd->config_ids.size(); |
| 317 | return *num_configs == 0 ? -1 : 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 320 | static int hwc_get_display_attributes(struct hwc_composer_device_1 *dev, |
| 321 | int display, uint32_t config, |
| 322 | const uint32_t *attributes, |
| 323 | int32_t *values) { |
| 324 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 325 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 326 | if (!c) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 327 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 328 | return -ENODEV; |
| 329 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 330 | DrmMode mode; |
| 331 | for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes(); |
| 332 | ++iter) { |
| 333 | if (iter->id() == config) { |
| 334 | mode = *iter; |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | if (mode.id() == 0) { |
| 339 | ALOGE("Failed to find active mode for display %d", display); |
| 340 | return -ENOENT; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 341 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 342 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 343 | uint32_t mm_width = c->mm_width(); |
| 344 | uint32_t mm_height = c->mm_height(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 345 | for (int i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; ++i) { |
| 346 | switch (attributes[i]) { |
| 347 | case HWC_DISPLAY_VSYNC_PERIOD: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 348 | values[i] = 1000 * 1000 * 1000 / mode.v_refresh(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 349 | break; |
| 350 | case HWC_DISPLAY_WIDTH: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 351 | values[i] = mode.h_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 352 | break; |
| 353 | case HWC_DISPLAY_HEIGHT: |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 354 | values[i] = mode.v_display(); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 355 | break; |
| 356 | case HWC_DISPLAY_DPI_X: |
| 357 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 358 | values[i] = mm_width ? (mode.h_display() * UM_PER_INCH) / mm_width : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 359 | break; |
| 360 | case HWC_DISPLAY_DPI_Y: |
| 361 | /* Dots per 1000 inches */ |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 362 | values[i] = |
| 363 | mm_height ? (mode.v_display() * UM_PER_INCH) / mm_height : 0; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 367 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 368 | } |
| 369 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 370 | static int hwc_get_active_config(struct hwc_composer_device_1 *dev, |
| 371 | int display) { |
| 372 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 373 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 374 | if (!c) { |
| 375 | ALOGE("Failed to get DrmConnector for display %d", display); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 376 | return -ENODEV; |
| 377 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 378 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 379 | DrmMode mode = c->active_mode(); |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 380 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 381 | for (size_t i = 0; i < hd->config_ids.size(); ++i) { |
| 382 | if (hd->config_ids[i] == mode.id()) |
| 383 | return i; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 384 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 385 | return -1; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 386 | } |
| 387 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 388 | static int hwc_set_active_config(struct hwc_composer_device_1 *dev, int display, |
| 389 | int index) { |
| 390 | struct hwc_context_t *ctx = (struct hwc_context_t *)&dev->common; |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 391 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 392 | if (index >= (int)hd->config_ids.size()) { |
| 393 | ALOGE("Invalid config index %d passed in", index); |
| 394 | return -EINVAL; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 395 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 396 | |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 397 | DrmConnector *c = ctx->drm.GetConnectorForDisplay(display); |
| 398 | if (!c) { |
| 399 | ALOGE("Failed to get connector for display %d", display); |
| 400 | return -ENODEV; |
| 401 | } |
| 402 | DrmMode mode; |
| 403 | for (DrmConnector::ModeIter iter = c->begin_modes(); iter != c->end_modes(); |
| 404 | ++iter) { |
| 405 | if (iter->id() == hd->config_ids[index]) { |
| 406 | mode = *iter; |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | if (mode.id() != hd->config_ids[index]) { |
| 411 | ALOGE("Could not find active mode for %d/%d", index, hd->config_ids[index]); |
| 412 | return -ENOENT; |
| 413 | } |
| 414 | int ret = ctx->drm.SetDisplayActiveMode(display, mode); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 415 | if (ret) { |
Sean Paul | 877be97 | 2015-06-03 14:08:27 -0400 | [diff] [blame] | 416 | ALOGE("Failed to set active config %d", ret); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 417 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 418 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 419 | return ret; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 420 | } |
| 421 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 422 | static int hwc_device_close(struct hw_device_t *dev) { |
| 423 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 424 | delete ctx; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 425 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 428 | /* |
| 429 | * TODO: This function sets the active config to the first one in the list. This |
| 430 | * should be fixed such that it selects the preferred mode for the display, or |
| 431 | * some other, saner, method of choosing the config. |
| 432 | */ |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 433 | static int hwc_set_initial_config(hwc_drm_display_t *hd) { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 434 | uint32_t config; |
| 435 | size_t num_configs = 1; |
| 436 | int ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config, |
| 437 | &num_configs); |
| 438 | if (ret || !num_configs) |
| 439 | return 0; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 440 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 441 | ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0); |
| 442 | if (ret) { |
| 443 | ALOGE("Failed to set active config d=%d ret=%d", hd->display, ret); |
| 444 | return ret; |
| 445 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 446 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 447 | return ret; |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 450 | static int hwc_initialize_display(struct hwc_context_t *ctx, int display) { |
Sean Paul | e42febf | 2015-05-07 11:35:29 -0700 | [diff] [blame] | 451 | hwc_drm_display_t *hd = &ctx->displays[display]; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 452 | hd->ctx = ctx; |
| 453 | hd->display = display; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 454 | |
Sean Paul | b386f1b | 2015-05-13 06:33:23 -0700 | [diff] [blame] | 455 | int ret = hwc_set_initial_config(hd); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 456 | if (ret) { |
| 457 | ALOGE("Failed to set initial config for d=%d ret=%d", display, ret); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 458 | return ret; |
| 459 | } |
Sean Paul | 24a26e3 | 2015-02-04 10:34:47 -0800 | [diff] [blame] | 460 | |
Sean Paul | 4057be3 | 2015-05-13 06:23:09 -0700 | [diff] [blame] | 461 | ret = hd->vsync_worker.Init(&ctx->drm, display); |
| 462 | if (ret) { |
| 463 | ALOGE("Failed to create event worker for display %d %d\n", display, ret); |
| 464 | return ret; |
| 465 | } |
| 466 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 467 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 468 | } |
| 469 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 470 | static int hwc_enumerate_displays(struct hwc_context_t *ctx) { |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 471 | int ret; |
| 472 | for (DrmResources::ConnectorIter c = ctx->drm.begin_connectors(); |
| 473 | c != ctx->drm.end_connectors(); ++c) { |
| 474 | ret = hwc_initialize_display(ctx, (*c)->display()); |
| 475 | if (ret) { |
| 476 | ALOGE("Failed to initialize display %d", (*c)->display()); |
| 477 | return ret; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 478 | } |
| 479 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 480 | |
| 481 | return 0; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 482 | } |
| 483 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 484 | static int hwc_device_open(const struct hw_module_t *module, const char *name, |
| 485 | struct hw_device_t **dev) { |
| 486 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) { |
| 487 | ALOGE("Invalid module name- %s", name); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | struct hwc_context_t *ctx = new hwc_context_t(); |
| 492 | if (!ctx) { |
| 493 | ALOGE("Failed to allocate hwc context"); |
| 494 | return -ENOMEM; |
| 495 | } |
| 496 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 497 | int ret = ctx->drm.Init(); |
| 498 | if (ret) { |
| 499 | ALOGE("Can't initialize Drm object %d", ret); |
| 500 | delete ctx; |
| 501 | return ret; |
| 502 | } |
| 503 | |
Sean Paul | da6270d | 2015-06-01 14:11:52 -0400 | [diff] [blame] | 504 | ctx->importer = Importer::CreateInstance(&ctx->drm); |
| 505 | if (!ctx->importer) { |
| 506 | ALOGE("Failed to create importer instance"); |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 507 | delete ctx; |
| 508 | return ret; |
| 509 | } |
| 510 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 511 | ret = hwc_enumerate_displays(ctx); |
| 512 | if (ret) { |
| 513 | ALOGE("Failed to enumerate displays: %s", strerror(ret)); |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 514 | delete ctx; |
| 515 | return ret; |
| 516 | } |
| 517 | |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 518 | ctx->device.common.tag = HARDWARE_DEVICE_TAG; |
| 519 | ctx->device.common.version = HWC_DEVICE_API_VERSION_1_4; |
| 520 | ctx->device.common.module = const_cast<hw_module_t *>(module); |
| 521 | ctx->device.common.close = hwc_device_close; |
| 522 | |
Sean Paul | 9046c64 | 2015-06-10 17:27:47 -0400 | [diff] [blame] | 523 | ctx->device.dump = hwc_dump; |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 524 | ctx->device.prepare = hwc_prepare; |
| 525 | ctx->device.set = hwc_set; |
| 526 | ctx->device.eventControl = hwc_event_control; |
| 527 | ctx->device.setPowerMode = hwc_set_power_mode; |
| 528 | ctx->device.query = hwc_query; |
| 529 | ctx->device.registerProcs = hwc_register_procs; |
| 530 | ctx->device.getDisplayConfigs = hwc_get_display_configs; |
| 531 | ctx->device.getDisplayAttributes = hwc_get_display_attributes; |
| 532 | ctx->device.getActiveConfig = hwc_get_active_config; |
| 533 | ctx->device.setActiveConfig = hwc_set_active_config; |
| 534 | ctx->device.setCursorPositionAsync = NULL; /* TODO: Add cursor */ |
| 535 | |
| 536 | *dev = &ctx->device.common; |
| 537 | |
| 538 | return 0; |
| 539 | } |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 540 | } |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 541 | |
Sean Paul | 6a55e9f | 2015-04-30 15:31:06 -0400 | [diff] [blame] | 542 | static struct hw_module_methods_t hwc_module_methods = { |
| 543 | open : android::hwc_device_open |
| 544 | }; |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 545 | |
| 546 | hwc_module_t HAL_MODULE_INFO_SYM = { |
Sean Paul | ef8f1f9 | 2015-04-29 16:05:23 -0400 | [diff] [blame] | 547 | common : { |
| 548 | tag : HARDWARE_MODULE_TAG, |
| 549 | version_major : 1, |
| 550 | version_minor : 0, |
| 551 | id : HWC_HARDWARE_MODULE_ID, |
| 552 | name : "DRM hwcomposer module", |
| 553 | author : "The Android Open Source Project", |
| 554 | methods : &hwc_module_methods, |
| 555 | dso : NULL, |
| 556 | reserved : {0}, |
| 557 | } |
Sean Paul | e0c4c3d | 2015-01-20 16:56:04 -0500 | [diff] [blame] | 558 | }; |