summaryrefslogtreecommitdiffstats
path: root/src/core/compositor/display_gl_output_surface.cpp
blob: a13dc2f3562ba39f4f3326704775d819c630ca74 (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
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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "display_gl_output_surface.h"

#include "type_conversion.h"

#include "base/threading/thread_task_runner_handle.h"
#include "components/viz/service/display/display.h"
#include "components/viz/service/display/output_surface_frame.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/command_buffer/service/mailbox_manager.h"
#include "gpu/command_buffer/service/texture_base.h"
#include "gpu/ipc/in_process_command_buffer.h"
#include "ui/gfx/buffer_format_util.h"

namespace QtWebEngineCore {

DisplayGLOutputSurface::DisplayGLOutputSurface(
        scoped_refptr<viz::VizProcessContextProvider> contextProvider)
    : OutputSurface(contextProvider)
    , Compositor(Compositor::Type::OpenGL)
    , m_commandBuffer(contextProvider->command_buffer())
    , m_gl(contextProvider->ContextGL())
    , m_vizContextProvider(contextProvider)
{
    capabilities_.uses_default_gl_framebuffer = false;
    m_gl->GenFramebuffers(1, &m_fboId);
}

DisplayGLOutputSurface::~DisplayGLOutputSurface()
{
    m_vizContextProvider->SetUpdateVSyncParametersCallback(viz::UpdateVSyncParametersCallback());
    m_gl->DeleteFramebuffers(1, &m_fboId);
}

// Called from viz::Display::Initialize.
void DisplayGLOutputSurface::BindToClient(viz::OutputSurfaceClient *client)
{
    m_client = client;
}

void DisplayGLOutputSurface::SetFrameSinkId(const viz::FrameSinkId &id)
{
    bind(id);
}

// Triggered by ui::Compositor::SetVisible(true).
void DisplayGLOutputSurface::EnsureBackbuffer()
{
}

// Triggered by ui::Compositor::SetVisible(false). Framebuffer must be cleared.
void DisplayGLOutputSurface::DiscardBackbuffer()
{
    NOTIMPLEMENTED();
    // m_gl->DiscardBackbufferCHROMIUM();
}

// Called from viz::DirectRenderer::DrawFrame before rendering starts, but only
// if the parameters differ from the previous Reshape call.
//
// Parameters:
//
//   - sizeInPixels comes from ui::Compositor::SetScaleAndSize via
//     viz::HostContextFactoryPrivate::ResizeDisplay.
//
//   - devicePixelRatio comes from viz::CompositorFrame::device_scale_factor()
//     via viz::RootCompositorFrameSinkImpl::SubmitCompositorFrame and
//     viz::Display::SetLocalSurfaceId.
//
//   - colorSpace and hasAlpha correspond to the color_space and
//     has_transparent_background properties of the root viz::RenderPass.
//
//   - useStencil should create a stencil buffer, but this is only needed for
//     overdraw feedback (--show-overdraw-feedback), so it's safe to ignore.
//     Accordingly, capabilities_.supports_stencil should be set to false.
//
void DisplayGLOutputSurface::Reshape(const gfx::Size &sizeInPixels,
                                     float devicePixelRatio,
                                     const gfx::ColorSpace &colorSpace,
                                     gfx::BufferFormat format,
                                     bool /*useStencil*/)
{
    bool hasAlpha = gfx::AlphaBitsForBufferFormat(format) > 0;
    m_currentShape = Shape{sizeInPixels, devicePixelRatio, colorSpace, hasAlpha};
    m_gl->ResizeCHROMIUM(sizeInPixels.width(), sizeInPixels.height(), devicePixelRatio,
                         colorSpace.AsGLColorSpace(), hasAlpha);
}

std::unique_ptr<DisplayGLOutputSurface::Buffer> DisplayGLOutputSurface::makeBuffer(const Shape &shape)
{
    std::unique_ptr<Buffer> buffer = std::make_unique<Buffer>(this);
    buffer->shape = shape;
    m_gl->GenTextures(1, &buffer->clientId);
    m_gl->BindTexture(GL_TEXTURE_2D, buffer->clientId);
    m_gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    m_gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    m_gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    m_gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    uint32_t width = shape.sizeInPixels.width();
    uint32_t height = shape.sizeInPixels.height();
    uint32_t format = shape.hasAlpha ? GL_RGBA : GL_RGB;
    m_gl->TexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, nullptr);
    return buffer;
}

void DisplayGLOutputSurface::deleteBufferResources(Buffer *buffer)
{
    m_gl->DeleteTextures(1, &buffer->clientId);
}

// Called by viz::GLRenderer during rendering whenever it switches to the root
// render pass.
void DisplayGLOutputSurface::BindFramebuffer()
{
    if (!m_backBuffer || m_backBuffer->shape != m_currentShape)
        m_backBuffer = makeBuffer(m_currentShape);

    m_gl->BindFramebuffer(GL_FRAMEBUFFER, m_fboId);
    m_gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_backBuffer->clientId, 0);
}

// Called from viz::Display::DrawAndSwap after rendering.
//
// Parameters:
//
//   - frame.size is the same as the size given to Reshape.
//
//   - frame.sub_buffer_rect and frame.content_bounds are never used since these
//     are only enabled if gl::GLSurface::SupportsPostSubBuffer() or
//     gl::GLSurface::SupportsSwapBuffersWithBounds() are true, respectively,
//     but this not the case for any offscreen gl::GLSurface.
//
//   - frame.latency_info is viz::CompositorFrame::metadata.latency_info.
void DisplayGLOutputSurface::SwapBuffers(viz::OutputSurfaceFrame frame)
{
    DCHECK(frame.size == m_currentShape.sizeInPixels);
    DCHECK(!frame.sub_buffer_rect.has_value());
    DCHECK(frame.content_bounds.empty());
    DCHECK(m_backBuffer);

    m_gl->BindFramebuffer(GL_FRAMEBUFFER, m_fboId);
    m_gl->FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
    gpu::SyncToken syncToken;
    m_gl->GenSyncTokenCHROMIUM(syncToken.GetData());

    unsigned int clientId = m_backBuffer->clientId;

    // Now some thread-hopping:
    //
    //   - We start here on the viz thread (client side of command buffer).
    //
    //   - Then we'll jump to the gpu thread (service side of command buffer) to
    //     get the real OpenGL texture id.
    //
    //   - Then we'll get a call from the Qt Quick Scene Graph thread (could be
    //     a separate thread or the main thread).
    //
    //   - Finally we'll return to the viz thread to acknowledge the swap.

    {
        QMutexLocker locker(&m_mutex);
        m_taskRunner = base::ThreadTaskRunnerHandle::Get();
        m_middleBuffer = std::move(m_backBuffer);
        m_middleBuffer->serviceId = 0;
    }

    m_commandBuffer->GetTextureQt(
            clientId,
            base::BindOnce(&DisplayGLOutputSurface::swapBuffersOnGpuThread, base::Unretained(this)),
            std::vector<gpu::SyncToken>{syncToken});
}

void DisplayGLOutputSurface::swapBuffersOnGpuThread(unsigned int id, std::unique_ptr<gl::GLFence> fence)
{
    {
        QMutexLocker locker(&m_mutex);
        m_middleBuffer->serviceId = id;
        m_middleBuffer->fence = CompositorResourceFence::create(std::move(fence));
        m_readyToUpdate = true;
    }

    if (auto obs = observer())
        obs->readyToSwap();
}

void DisplayGLOutputSurface::swapBuffersOnVizThread()
{
    {
        QMutexLocker locker(&m_mutex);
        m_backBuffer = std::move(m_middleBuffer);
    }

    const auto now = base::TimeTicks::Now();
    m_client->DidReceiveSwapBuffersAck(gfx::SwapTimings{now, now, {}, {}, {}}, gfx::GpuFenceHandle());
    m_client->DidReceivePresentationFeedback(
            gfx::PresentationFeedback(now, base::TimeDelta(),
                                      gfx::PresentationFeedback::Flags::kVSync));
}

void DisplayGLOutputSurface::SetDrawRectangle(const gfx::Rect &)
{
}

// Returning true here will cause viz::GLRenderer to try to render the output
// surface as an overlay plane (see viz::DirectRenderer::DrawFrame and
// viz::GLRenderer::ScheduleOverlays).
bool DisplayGLOutputSurface::IsDisplayedAsOverlayPlane() const
{
    return false;
}

// Only used if IsDisplayedAsOverlayPlane was true (called from
// viz::GLRenderer::ScheduleOverlays).
unsigned DisplayGLOutputSurface::GetOverlayTextureId() const
{
    return 0;
}

// Called by viz::GLRenderer but always false in all implementations except for
// android_webview::ParentOutputSurface.
bool DisplayGLOutputSurface::HasExternalStencilTest() const
{
    return false;
}

// Only called if HasExternalStencilTest was true. Dead code?
void DisplayGLOutputSurface::ApplyExternalStencil()
{
    NOTREACHED();
}

// Called from GLRenderer::GetFramebufferCopyTextureFormat when using
// glCopyTexSubImage2D on our framebuffer.
uint32_t DisplayGLOutputSurface::GetFramebufferCopyTextureFormat()
{
    return m_currentShape.hasAlpha ? GL_RGBA : GL_RGB;
}

// Called from viz::DirectRenderer::DrawFrame, only used for overlays.
unsigned DisplayGLOutputSurface::UpdateGpuFence()
{
    NOTREACHED();
    return 0;
}

void DisplayGLOutputSurface::SetUpdateVSyncParametersCallback(viz::UpdateVSyncParametersCallback callback)
{
    m_vizContextProvider->SetUpdateVSyncParametersCallback(std::move(callback));
}

void DisplayGLOutputSurface::SetDisplayTransformHint(gfx::OverlayTransform)
{
}

gfx::OverlayTransform DisplayGLOutputSurface::GetDisplayTransform()
{
    return gfx::OVERLAY_TRANSFORM_NONE;
}

void DisplayGLOutputSurface::swapFrame()
{
    QMutexLocker locker(&m_mutex);
    if (m_readyToUpdate) {
        std::swap(m_middleBuffer, m_frontBuffer);
        m_taskRunner->PostTask(FROM_HERE,
                               base::BindOnce(&DisplayGLOutputSurface::swapBuffersOnVizThread,
                                              base::Unretained(this)));
        m_taskRunner.reset();
        m_readyToUpdate = false;
    }
}

void DisplayGLOutputSurface::waitForTexture()
{
    if (m_frontBuffer && m_frontBuffer->fence) {
        m_frontBuffer->fence->wait();
        m_frontBuffer->fence.reset();
    }
}

int DisplayGLOutputSurface::textureId()
{
    return m_frontBuffer ? m_frontBuffer->serviceId : 0;
}

QSize DisplayGLOutputSurface::size()
{
    return m_frontBuffer ? toQt(m_frontBuffer->shape.sizeInPixels) : QSize();
}

bool DisplayGLOutputSurface::hasAlphaChannel()
{
    return m_frontBuffer ? m_frontBuffer->shape.hasAlpha : false;
}

float DisplayGLOutputSurface::devicePixelRatio()
{
    return m_frontBuffer ? m_frontBuffer->shape.devicePixelRatio : 1;
}

} // namespace QtWebEngineCore