summaryrefslogtreecommitdiff
path: root/LayerManagerPlugins/Renderers/Graphic/src/WindowSystems/WaylandDrmWindowSystem.cpp
blob: b8929707cd8b5d0b7e9a824f84640a5b1b8e552b (plain)
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
/***************************************************************************
*
* Copyright 2010, 2011 BMW Car IT GmbH
* Copyright (C) 2011 DENSO CORPORATION and Robert Bosch Car Multimedia Gmbh
*
*
* 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.
*
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
* SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
* SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
****************************************************************************/

#include "WindowSystems/WaylandDrmWindowSystem.h"
#include "Log.h"
#include <linux/fb.h>
#include <fcntl.h>
#include <iomanip>

#include <gbm.h>
#include <libudev.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include "WindowSystems/WaylandEvdevInputEvent.h"

static const char default_seat[] = "seat0";

WaylandDrmWindowSystem::WaylandDrmWindowSystem(const char* displayname, int width, int height, Scene* pScene, InputManager* pInputManager)
: WaylandBaseWindowSystem(displayname, width, height, pScene, pInputManager)
, m_fdDev(-1), m_gbm(NULL)
{
    LOG_DEBUG("WaylandDrmWindowSystem", "creating WaylandDrmWindowSystem width:" << width << " height:" << height);

    // Because of using DrmMode, it's not necessary to use frame timer.
    m_bUseFrameTimer = false;
}

WaylandDrmWindowSystem::~WaylandDrmWindowSystem()
{
    if (m_gbm != NULL)
        gbm_device_destroy(m_gbm);
    if (m_fdDev >= 0)
        drmDropMaster(m_fdDev);
}

bool WaylandDrmWindowSystem::initGraphicSystem()
{
    graphicSystem->setBaseWindowSystem(this);
    bool ans = graphicSystem->init((void*)m_gbm, (void*)NULL);
    if (true != ans)
    {
        LOG_ERROR("WaylandDrmWindowSystem", "Failed to init graphic system");
        return false;
    }

    graphicSystem->updateScreenList(m_pScene->getScreenList());

    return true;
}

bool WaylandDrmWindowSystem::createNativeContext()
{
    struct udev* udev;
    struct udev_device *device, *drm_device;
    struct udev_enumerate* e;
    struct udev_list_entry* entry;
    const char *path, *device_seat;
    const char *seat = default_seat;

    udev = udev_new();
    if (udev == NULL)
    {
        LOG_ERROR("WaylandDrmWindowSystem", "failed to initialize udev context.");
        return false;
    }

    e = udev_enumerate_new(udev);
    udev_enumerate_add_match_subsystem(e, "drm");
    udev_enumerate_add_match_sysname(e, "card[0-9]*");

    udev_enumerate_scan_devices(e);
    drm_device = NULL;
    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e))
    {
        path = udev_list_entry_get_name(entry);
        device = udev_device_new_from_syspath(udev, path);
        device_seat = udev_device_get_property_value(device, "ID_SEAT");
        if (!device_seat)
            device_seat = default_seat;
        if (strcmp(device_seat, seat) == 0)
        {
            drm_device = device;
            break;
        }
        udev_device_unref(device);
    }

    if (drm_device == NULL)
    {
        LOG_ERROR("WaylandDrmWindowSystem", "no drm device found");
        return false;
    }

    const char* filename;

    filename = udev_device_get_devnode(drm_device);
    m_fdDev = open(filename, O_RDWR | O_CLOEXEC);
    if (m_fdDev < 0)
    {
        LOG_ERROR("WaylandDrmWindowSystem", "failed to open device");
        return false;
    }

    LOG_DEBUG("WaylandDrmWindowSystem", "Device name: " << filename << " fd: " << m_fdDev);

    m_gbm = gbm_create_device(m_fdDev);
    if (m_gbm == NULL)
    {
        LOG_ERROR("WaylandDrmWindowSystem", "failed to create gbm device");
        return false;
    }

    udev_device_unref(drm_device);

    LOG_DEBUG("WaylandDrmWindowSystem", "SUCCESS:create gbm device");
    return true;
}

bool WaylandDrmWindowSystem::createInputEvent()
{
    m_inputEvent = new WaylandEvdevInputEvent(this);
    m_inputEvent->setupInputEvent();
    return true;
}

void WaylandDrmWindowSystem::checkForNewSurfaceNativeContent()
{
    m_pScene->lockScene();
    LmScreenList screenList = m_pScene->getScreenList();
    LmScreenListIterator iter = screenList.begin();
    LmScreenListIterator iterEnd = screenList.end();
    for (; iter != iterEnd; ++iter)
    {
        LayerList layers = m_pScene->getCurrentRenderOrder((*iter)->getID());
        for (LayerListConstIterator current = layers.begin(); current != layers.end(); current++)
        {
            SurfaceList surfaces = (*current)->getAllSurfaces();
            for (SurfaceListConstIterator currentS = surfaces.begin(); currentS != surfaces.end(); currentS++)
            {
                if ((*currentS)->hasNativeContent())
                {
                    graphicSystem->switchScreen((*iter)->getID());
                    allocatePlatformSurface(*currentS);
                }
                else // While we are at it, also cleanup any stale native content
                {
                    deallocatePlatformSurface(*currentS);
                }
            }
        }
    }
    m_pScene->unlockScene();
}

void WaylandDrmWindowSystem::RedrawAllLayers(bool clear, bool swap)
{
    LmScreenList screenList = m_pScene->getScreenList();
    LmScreenListIterator iter = screenList.begin();
    LmScreenListIterator iterEnd = screenList.end();
    for (; iter != iterEnd; ++iter)
    {
        LayerList layers = m_pScene->getCurrentRenderOrder((*iter)->getID());
        LayerList swLayers;
        // TODO: bRedraw is overly conservative if layers includes a hardware layer
        bool bRedraw = m_forceComposition || graphicSystem->needsRedraw(layers) || (m_systemState == REDRAW_STATE);

        if (bRedraw)
        {
            graphicSystem->switchScreen((*iter)->getID());
            graphicSystem->activateGraphicContext();
            if (clear)
            {
                graphicSystem->clearBackground();
            }
        }
        for (std::list<Layer*>::const_iterator current = layers.begin(); current != layers.end(); ++current)
        {
            if ((*current)->getLayerType() == Hardware)
            {
                if (m_forceComposition || graphicSystem->needsRedraw(*current))
                {
                    renderHWLayer(*current);
                }
            }
            else if (bRedraw)
            {
                swLayers.push_back(*current);
            }
        }
        if (bRedraw)
        {
            graphicSystem->renderSWLayers(swLayers, false); // Already cleared
            if (swap)
            {
                graphicSystem->swapBuffers();
            }
            graphicSystem->releaseGraphicContext();

            if (m_debugMode)
            {
                printDebug();
            }

            calculateFps();

            m_systemState = IDLE_STATE;
        }
    }
}