blob: dc4bfe82e9f57790c47d3495d40147fe6c96d00b [file] [log] [blame]
Sean Paulb386f1b2015-05-13 06:33:23 -07001/*
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
17#define LOG_TAG "hwc-drm-compositor"
18
19#include "drmcompositor.h"
20#include "drmcrtc.h"
21#include "drmplane.h"
22#include "drmresources.h"
23
24#include <pthread.h>
Sean Paulc8dcfe02015-06-10 17:29:05 -040025#include <sstream>
Sean Paulb386f1b2015-05-13 06:33:23 -070026#include <stdlib.h>
Sean Paulc8dcfe02015-06-10 17:29:05 -040027#include <time.h>
Sean Paulb386f1b2015-05-13 06:33:23 -070028
29#include <cutils/log.h>
30#include <sync/sync.h>
31
32namespace android {
33
34DrmCompositor::DrmCompositor(DrmResources *drm)
35 : drm_(drm),
36 worker_(this),
37 active_composition_(NULL),
38 frame_no_(0),
Sean Paulc8dcfe02015-06-10 17:29:05 -040039 initialized_(false),
40 dump_frames_composited_(0),
41 dump_last_timestamp_ns_(0) {
42 struct timespec ts;
43 if (clock_gettime(CLOCK_MONOTONIC, &ts))
44 return;
45 dump_last_timestamp_ns_ = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
Sean Paulb386f1b2015-05-13 06:33:23 -070046}
47
48DrmCompositor::~DrmCompositor() {
49 if (initialized_)
50 pthread_mutex_destroy(&lock_);
51}
52
53int DrmCompositor::Init() {
54 int ret = pthread_mutex_init(&lock_, NULL);
55 if (ret) {
56 ALOGE("Failed to initialize drm compositor lock %d\n", ret);
57 return ret;
58 }
59 ret = worker_.Init();
60 if (ret) {
61 pthread_mutex_destroy(&lock_);
62 ALOGE("Failed to initialize compositor worker %d\n", ret);
63 return ret;
64 }
65
66 initialized_ = true;
67 return 0;
68}
69
70Composition *DrmCompositor::CreateComposition(Importer *importer) {
71 DrmComposition *composition = new DrmComposition(drm_, importer, frame_no_++);
72 if (!composition) {
73 ALOGE("Failed to allocate drm composition");
74 return NULL;
75 }
76 int ret = composition->Init();
77 if (ret) {
78 ALOGE("Failed to initialize drm composition %d", ret);
79 delete composition;
80 return NULL;
81 }
82 return composition;
83}
84
85int DrmCompositor::QueueComposition(Composition *composition) {
86 int ret = pthread_mutex_lock(&lock_);
87 if (ret) {
88 ALOGE("Failed to acquire compositor lock %d", ret);
89 return ret;
90 }
91
92 composite_queue_.push((DrmComposition *)composition);
93
94 ret = pthread_mutex_unlock(&lock_);
95 if (ret) {
96 ALOGE("Failed to release compositor lock %d", ret);
97 return ret;
98 }
99
100 worker_.Signal();
101 return 0;
102}
103
Sean Paulb386f1b2015-05-13 06:33:23 -0700104int DrmCompositor::CompositeDisplay(DrmCompositionLayerMap_t::iterator begin,
105 DrmCompositionLayerMap_t::iterator end) {
106 int ret = 0;
107 // Wait for all acquire fences to signal
108 for (DrmCompositionLayerMap_t::iterator iter = begin; iter != end; ++iter) {
109 hwc_layer_1_t *layer = &iter->second.layer;
110
111 if (layer->acquireFenceFd < 0)
112 continue;
113
114 ret = sync_wait(layer->acquireFenceFd, -1);
115 if (ret) {
116 ALOGE("Failed to wait for acquire %d/%d", layer->acquireFenceFd, ret);
117 return ret;
118 }
119 close(layer->acquireFenceFd);
120 layer->acquireFenceFd = -1;
121 }
122
Sean Paulb386f1b2015-05-13 06:33:23 -0700123 drmModePropertySetPtr pset = drmModePropertySetAlloc();
124 if (!pset) {
125 ALOGE("Failed to allocate property set");
126 return -ENOMEM;
127 }
128
129 for (DrmCompositionLayerMap_t::iterator iter = begin; iter != end; ++iter) {
130 DrmCompositionLayer_t *comp = &iter->second;
131 hwc_layer_1_t *layer = &comp->layer;
132 DrmPlane *plane = comp->plane;
133
134 ret =
135 drmModePropertySetAdd(pset, plane->id(), plane->crtc_property().id(),
Sean Paul877be972015-06-03 14:08:27 -0400136 begin->second.crtc->id()) ||
Sean Paulb386f1b2015-05-13 06:33:23 -0700137 drmModePropertySetAdd(pset, plane->id(), plane->fb_property().id(),
138 comp->bo.fb_id) ||
139 drmModePropertySetAdd(pset, plane->id(), plane->crtc_x_property().id(),
140 layer->displayFrame.left) ||
141 drmModePropertySetAdd(pset, plane->id(), plane->crtc_y_property().id(),
142 layer->displayFrame.top) ||
143 drmModePropertySetAdd(
144 pset, plane->id(), plane->crtc_w_property().id(),
145 layer->displayFrame.right - layer->displayFrame.left) ||
146 drmModePropertySetAdd(
147 pset, plane->id(), plane->crtc_h_property().id(),
148 layer->displayFrame.bottom - layer->displayFrame.top) ||
149 drmModePropertySetAdd(pset, plane->id(), plane->src_x_property().id(),
150 layer->sourceCropf.left) ||
151 drmModePropertySetAdd(pset, plane->id(), plane->src_y_property().id(),
152 layer->sourceCropf.top) ||
153 drmModePropertySetAdd(
154 pset, plane->id(), plane->src_w_property().id(),
155 (int)(layer->sourceCropf.right - layer->sourceCropf.left) << 16) ||
156 drmModePropertySetAdd(
157 pset, plane->id(), plane->src_h_property().id(),
158 (int)(layer->sourceCropf.bottom - layer->sourceCropf.top) << 16);
159 if (ret) {
160 ALOGE("Failed to add plane %d to set", plane->id());
161 break;
162 }
163 }
164
165 if (!ret) {
166 ret = drmModePropertySetCommit(drm_->fd(), 0, drm_, pset);
167 if (ret)
168 ALOGE("Failed to commit pset ret=%d\n", ret);
169 }
170 if (pset)
171 drmModePropertySetFree(pset);
172
173 return ret;
174}
175
176int DrmCompositor::Composite() {
177 int ret = pthread_mutex_lock(&lock_);
178 if (ret) {
179 ALOGE("Failed to acquire compositor lock %d", ret);
180 return ret;
181 }
182 if (composite_queue_.empty()) {
183 ret = pthread_mutex_unlock(&lock_);
184 if (ret)
185 ALOGE("Failed to release compositor lock %d", ret);
186 return ret;
187 }
188
189 DrmComposition *composition = composite_queue_.front();
190 composite_queue_.pop();
Sean Paulc8dcfe02015-06-10 17:29:05 -0400191 ++dump_frames_composited_;
Sean Paulb386f1b2015-05-13 06:33:23 -0700192
193 ret = pthread_mutex_unlock(&lock_);
194 if (ret) {
195 ALOGE("Failed to release compositor lock %d", ret);
196 return ret;
197 }
198
199 DrmCompositionLayerMap_t *map = composition->GetCompositionMap();
200 for (DrmResources::ConnectorIter iter = drm_->begin_connectors();
201 iter != drm_->end_connectors(); ++iter) {
202 int display = (*iter)->display();
203 std::pair<DrmCompositionLayerMap_t::iterator,
204 DrmCompositionLayerMap_t::iterator> layer_iters =
205 map->equal_range(display);
206
207 if (layer_iters.first != layer_iters.second) {
208 ret = CompositeDisplay(layer_iters.first, layer_iters.second);
209 if (ret) {
210 ALOGE("Composite failed for display %d:", display);
211 break;
212 }
213 }
214 }
215
216 if (active_composition_) {
217 active_composition_->FinishComposition();
218 delete active_composition_;
219 }
220 active_composition_ = composition;
221 return ret;
222}
223
224bool DrmCompositor::HaveQueuedComposites() const {
225 int ret = pthread_mutex_lock(&lock_);
226 if (ret) {
227 ALOGE("Failed to acquire compositor lock %d", ret);
228 return false;
229 }
230
231 bool empty_ret = !composite_queue_.empty();
232
233 ret = pthread_mutex_unlock(&lock_);
234 if (ret) {
235 ALOGE("Failed to release compositor lock %d", ret);
236 return false;
237 }
238
239 return empty_ret;
240}
Sean Paulc8dcfe02015-06-10 17:29:05 -0400241
242void DrmCompositor::Dump(std::ostringstream *out) const {
243 uint64_t cur_ts;
244
245 int ret = pthread_mutex_lock(&lock_);
246 if (ret)
247 return;
248
249 uint64_t num_frames = dump_frames_composited_;
250 dump_frames_composited_ = 0;
251
252 struct timespec ts;
253 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
254
255 ret |= pthread_mutex_unlock(&lock_);
256 if (ret)
257 return;
258
259 cur_ts = ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
260 uint64_t num_ms = (cur_ts - dump_last_timestamp_ns_) / (1000 * 1000);
261 unsigned fps = num_ms ? (num_frames * 1000) / (num_ms) : 0;
262
263 *out << "DrmCompositor: num_frames=" << num_frames << " num_ms=" << num_ms <<
264 " fps=" << fps << "\n";
265
266 dump_last_timestamp_ns_ = cur_ts;
267}
Sean Paulb386f1b2015-05-13 06:33:23 -0700268}