summaryrefslogtreecommitdiffstats
blob: 0a3ddaeab4b87ead8c142c8d87920ce5ca8588aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (C) Texas Instruments Incorporated - http://www.ti.com/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>

#include <cerrno>
#include <cstdint>
#include <cstdbool>
#include <cstdlib>
#include <cstring>
#include <sys/ioctl.h>

#include <log/log.h>
#include <cutils/properties.h>

#include "hwc_dev.h"
#include "color_fmt.h"
#include "display.h"
#include "layer.h"
#include "utils.h"

HWCDisplay::HWCDisplay(enum disp_role role) :
    active_config(0),
    role(role),
    cb_procs(NULL),
    is_dummy(false),
    vsync_on(false),
    blanked(true),
    retire_timeline(new Timeline),
    is_flip_pending(false)
{
}

HWCDisplay::~HWCDisplay()
{
    std::unique_lock<std::mutex> lock(this->mutex);

    if (this->retire_timeline) {
        /* increment timeline to signal any pending fences */
        this->retire_timeline->inc();
        this->retire_timeline->inc();

        this->cond_flip.wait(lock, [this]{return !this->is_flip_pending;});

        delete this->retire_timeline;
    }
}

void HWCDisplay::setup_composition_pipes(int disp)
{
    std::unique_lock<std::mutex> lock(this->mutex);

    KMSDisplay *kdisp = &this->disp_link;
    composition_t *comp = &this->composition;

    int count = 0;
    for (auto plane : kdisp->card->get_planes()) {
        auto possible_crtcs = plane->get_possible_crtcs();
        if (std::find(possible_crtcs.begin(), possible_crtcs.end(), kdisp->crtc) != possible_crtcs.end()) {
            comp->plane_info[count].plane = plane;
            ALOGI("Display %d - Overlay %d: plane_id: %d", disp, count,
                    comp->plane_info[count].plane->id());
            count++;
        }
    }
}

/* Page flip handler callback */
void HWCDisplay::page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
{
    HWCDisplay *display = (HWCDisplay *)data;

    std::unique_lock<std::mutex> lock(display->mutex);

    if (display->is_flip_pending == false) {
        ALOGD("Spurious page flip?");
        return;
    }

//    ALOGD("Page flip on display %d", display->role);

    /* release the old buffers */
    for (auto current_fb_info : display->current_fb_infos)
        delete current_fb_info;
    display->current_fb_infos.clear();

    /* pending are now current */
    for (auto pending_fb_info : display->pending_fb_infos)
        display->current_fb_infos.push_back(pending_fb_info);
    display->pending_fb_infos.clear();

    display->retire_timeline->inc();

    display->is_flip_pending = false;
    lock.unlock();
    display->cond_flip.notify_one();
}

static int vblank_kick(HWCDisplay *display)
{
    drmVBlank vblank;
    memset(&vblank, 0, sizeof(vblank));
    vblank.request.type = (drmVBlankSeqType)(DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT |
                display->role == DISP_ROLE_SECONDARY ? DRM_VBLANK_SECONDARY : 0);
    vblank.request.sequence = 1;
    vblank.request.signal = (unsigned long)display;
    int err = drmWaitVBlank(display->disp_link.card->fd(), &vblank);
    if (err < 0) {
        /* error in drmWaitVBlank() use SW vsync instead? */
        ALOGE("drmWaitVBlank error %d (%s)", err, strerror(errno));
        return -1;
    }

    return 0;
}

/* Callback function that gets triggered on vsync */
void HWCDisplay::vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data)
{
    HWCDisplay *display = (HWCDisplay *)data;

    std::unique_lock<std::mutex> lock(display->mutex);

    if (display->vsync_on) {
        int64_t ts = sec * (int64_t)1000000000 + usec * (int64_t)1000;

//        ALOGD("Sending VBLANK at %lld for display %d", ts, display->role);
        display->cb_procs->vsync(display->cb_procs, display->role, ts);

        vblank_kick(display);
    }
}

int HWCDisplay::set_vsync_state(bool state)
{
    std::unique_lock<std::mutex> lock(this->mutex);

    if (this->is_dummy)
        return 0;

    this->vsync_on = state;

    if (this->vsync_on)
        return vblank_kick(this);

    return 0;
}

static void set_plane_properties(kms::AtomicReq &req, drm_plane_props_t *plane_props)
{
    uint32_t fb_id = plane_props->fb_info->fb_id;
    kms::Plane* plane = plane_props->plane;

    req.add(plane, "FB_ID", fb_id);

    if (!fb_id) {
        /* disable plane */
        ALOGI("Disabling plane %d", plane->id());
        req.add(plane, "ACTIVE", 0);
        return;
    }

    req.add(plane, "IN_FENCE_FD", plane_props->fb_info->acquirefence);

    req.add(plane, {
            { "CRTC_ID", plane_props->crtc_id },
            { "SRC_X", (plane_props->src_x) << 16 },
            { "SRC_Y", (plane_props->src_y) << 16 },
            { "SRC_W", (plane_props->src_w) << 16 },
            { "SRC_H", (plane_props->src_h) << 16 },
            { "CRTC_X", plane_props->crtc_x },
            { "CRTC_Y", plane_props->crtc_y },
            { "CRTC_W", plane_props->crtc_w },
            { "CRTC_H", plane_props->crtc_h },
    });

    /* optional props */
    req.add(plane, {
            { "zorder", plane_props->zorder },
            { "pre_mult_alpha", plane_props->pre_mult_alpha },
    });
}

int HWCDisplay::update_display(hwc_display_contents_1_t *content)
{
    std::unique_lock<std::mutex> lock(this->mutex);

    this->cond_flip.wait(lock, [this]{return !this->is_flip_pending;});

    KMSDisplay *kdisp = &this->disp_link;

    kms::AtomicReq req(*kdisp->card);

    composition_t *comp = &this->composition;
    for (size_t i = 0; i <= comp->used_ovls; i++) {
        drm_plane_props_t *plane_info = &comp->plane_info[comp->prop_indices[i]];
        if (plane_info->layer && plane_info->layer->handle) {

            hwc_layer_1_t *layer = plane_info->layer;
            buffer_handle_t handle = layer->handle;

            if (comp->use_blitter)
                handle = comp->blitter_handle;

            DRM_FB *drm_fb = new DRM_FB(kdisp->card->fd(), layer->acquireFenceFd, handle, i != 0);
            layer->acquireFenceFd = -1;

            plane_info->fb_info = drm_fb;
            this->pending_fb_infos.push_back(drm_fb);

            plane_info->crtc_id = kdisp->crtc->id();

            set_plane_properties(req, plane_info);
        }
    }

    int ret = req.commit(this, true);
    if (ret) {
        ALOGE("cannot do atomic commit/page flip: %d (%s)", ret, strerror(errno));
        for (auto pending_fb_info : this->pending_fb_infos)
            delete pending_fb_info;
        this->pending_fb_infos.clear();
        return ret;
    }

//    ALOGD("Scheduled page flip on display %d", this->role);
    this->is_flip_pending = true;

    /*
     * we scheduled flip, assign release and retire fences, currently we make
     * no distinction between release and retire fences and they both get
     * signaled at the same time.
     *   - Display retire fence
     *   - FB target release fence
     */
//    int fence_fd = this->retire_timeline->create_fence("hwc_retire_fence", TWO_FLIP_EVENTS);
//
//    content->retireFenceFd = dup(fence_fd);
//
//    for (size_t i = 0; i < content->numHwLayers; i++) {
//        if (content->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)
//            content->hwLayers[i].releaseFenceFd = dup(fence_fd);
//    }
//
//    close(fence_fd);

    return 0;
}

void HWCDisplay::blank(int blank)
{
    std::unique_lock<std::mutex> lock(this->mutex);

    if (this->is_dummy)
        return;

    KMSDisplay *kdisp = &this->disp_link;

    ALOGI("Connecting connector %d to crtc %d", kdisp->con->id(), kdisp->crtc->id());

    int ret = kdisp->crtc->set_mode(kdisp->con, kdisp->mode);
    if (ret) {
           ALOGE("Failed to connect encoder (%s)", strerror(-ret));
           return;
    }

    // Fixme: This should actually blank the screen
    this->blanked = blank;
}

int HWCDisplay::get_display_configs(uint32_t *configs, size_t *numConfigs)
{
    if (!configs || !numConfigs)
        return -EINVAL;

    if (*numConfigs == 0)
        return 0;

    std::unique_lock<std::mutex> lock(this->mutex);

    size_t num = this->configs.size();
    if (num > *numConfigs)
        num = *numConfigs;

    for (size_t i = 0; i < num; i++)
        configs[i] = i;

    *numConfigs = num;

    return 0;
}

int HWCDisplay::get_display_attributes(uint32_t cfg, const uint32_t *attributes, int32_t *values)
{
    if (!attributes || !values)
        return 0;

    std::unique_lock<std::mutex> lock(this->mutex);

    if (cfg >= this->configs.size())
        return -EINVAL;

    display_config_t *config = &this->configs[cfg];

    for (size_t i = 0; attributes[i] != HWC_DISPLAY_NO_ATTRIBUTE; i++)
    {
        switch (attributes[i]) {
        case HWC_DISPLAY_VSYNC_PERIOD:
            values[i] = 1000000000 / config->fps;
            break;
        case HWC_DISPLAY_WIDTH:
            values[i] = config->xres;
            break;
        case HWC_DISPLAY_HEIGHT:
            values[i] = config->yres;
            break;
        case HWC_DISPLAY_DPI_X:
            values[i] = 1000 * config->xdpi;
            break;
        case HWC_DISPLAY_DPI_Y:
            values[i] = 1000 * config->ydpi;
            break;
        }
    }

    return 0;
}