summaryrefslogtreecommitdiffstats
path: root/src/core/compositor/native_skia_output_device.cpp
blob: 708692df7d51c43a564075a911ee4f6162f6e3e7 (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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (C) 2023 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 "native_skia_output_device.h"

#include "type_conversion.h"

#include "components/viz/common/resources/shared_image_format.h"
#include "components/viz/common/resources/shared_image_format_utils.h"
#include "components/viz/service/display_embedder/skia_output_surface_dependency.h"
#include "gpu/command_buffer/common/mailbox.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "gpu/command_buffer/service/shared_image/shared_image_factory.h"
#include "gpu/command_buffer/service/shared_image/shared_image_representation.h"
#include "gpu/command_buffer/service/skia_utils.h"
#include "third_party/skia/include/gpu/GrDirectContext.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
#include "ui/gfx/native_pixmap.h"
#include "ui/gfx/gpu_fence.h"
#include "ui/gl/gl_fence.h"

#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif

namespace QtWebEngineCore {

namespace {

// Helper function for moving a GpuFence from a fence handle to a unique_ptr.
std::unique_ptr<gfx::GpuFence> TakeGpuFence(gfx::GpuFenceHandle fence)
{
    return fence.is_null() ? nullptr : std::make_unique<gfx::GpuFence>(std::move(fence));
}

} // namespace

NativeSkiaOutputDevice::NativeSkiaOutputDevice(
        scoped_refptr<gpu::SharedContextState> contextState, bool requiresAlpha,
        gpu::MemoryTracker *memoryTracker, viz::SkiaOutputSurfaceDependency *dependency,
        gpu::SharedImageFactory *shared_image_factory,
        gpu::SharedImageRepresentationFactory *shared_image_representation_factory,
        DidSwapBufferCompleteCallback didSwapBufferCompleteCallback)
    : SkiaOutputDevice(contextState->gr_context(), contextState->graphite_context(), memoryTracker,
                       didSwapBufferCompleteCallback)
    , Compositor(Type::Native)
    , m_contextState(contextState)
    , m_requiresAlpha(requiresAlpha)
    , m_factory(shared_image_factory)
    , m_representationFactory(shared_image_representation_factory)
    , m_deps(dependency)
{
    capabilities_.uses_default_gl_framebuffer = false;
    capabilities_.supports_surfaceless = true;
    capabilities_.output_surface_origin = gfx::SurfaceOrigin::kTopLeft;
    capabilities_.preserve_buffer_content = true;
    capabilities_.only_invalidates_damage_rect = false;

#if defined(USE_OZONE)
    m_isNativeBufferSupported = ui::OzonePlatform::GetInstance()
                                        ->GetPlatformRuntimeProperties()
                                        .supports_native_pixmaps;
#endif
}

NativeSkiaOutputDevice::~NativeSkiaOutputDevice()
{
}

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

bool NativeSkiaOutputDevice::Reshape(const SkImageInfo &image_info,
                                     const gfx::ColorSpace &colorSpace,
                                     int sample_count,
                                     float device_scale_factor,
                                     gfx::OverlayTransform transform)
{
    m_shape = Shape{image_info, device_scale_factor, colorSpace, sample_count};
    DCHECK_EQ(transform, gfx::OVERLAY_TRANSFORM_NONE);
    return true;
}

void NativeSkiaOutputDevice::Present(const absl::optional<gfx::Rect> &update_rect,
                                     BufferPresentedCallback feedback,
                                     viz::OutputSurfaceFrame frame)
{
    DCHECK(m_backBuffer);

    StartSwapBuffers(std::move(feedback));
    m_frame = std::move(frame);
    {
        QMutexLocker locker(&m_mutex);
        m_backBuffer->createFence();
        m_gpuTaskRunner = base::SingleThreadTaskRunner::GetCurrentDefault();
        std::swap(m_middleBuffer, m_backBuffer);
        m_readyToUpdate = true;
    }

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

void NativeSkiaOutputDevice::EnsureBackbuffer()
{
}

void NativeSkiaOutputDevice::DiscardBackbuffer()
{
}

SkSurface *NativeSkiaOutputDevice::BeginPaint(std::vector<GrBackendSemaphore> *end_semaphores)
{
    {
        QMutexLocker locker(&m_mutex);
        if (!m_backBuffer || m_backBuffer->shape() != m_shape) {
            m_backBuffer = std::make_unique<Buffer>(this);
            if (!m_backBuffer->initialize())
                return nullptr;
        }
    }
    auto surface = m_backBuffer->beginWriteSkia();
    *end_semaphores = m_backBuffer->takeEndWriteSkiaSemaphores();
    return surface;
}

void NativeSkiaOutputDevice::EndPaint()
{
    m_backBuffer->endWriteSkia(true);
}

void NativeSkiaOutputDevice::swapFrame()
{
    QMutexLocker locker(&m_mutex);
    if (m_readyToUpdate) {
        std::swap(m_frontBuffer, m_middleBuffer);
        m_gpuTaskRunner->PostTask(FROM_HERE,
                                  base::BindOnce(&NativeSkiaOutputDevice::SwapBuffersFinished,
                                                 base::Unretained(this)));
        m_readyToUpdate = false;
        if (m_frontBuffer) {
            m_readyWithTexture = true;
            m_frontBuffer->beginPresent();
        }
        if (m_middleBuffer)
            m_middleBuffer->freeTexture();
        m_gpuTaskRunner.reset();
    }
}

void NativeSkiaOutputDevice::waitForTexture()
{
    if (m_readyWithTexture)
        m_frontBuffer->consumeFence();
}

void NativeSkiaOutputDevice::releaseTexture()
{
    if (m_readyWithTexture) {
        m_frontBuffer->endPresent();
        m_readyWithTexture = false;
    }
}

void NativeSkiaOutputDevice::releaseResources()
{
    if (m_frontBuffer)
        m_frontBuffer->freeTexture();
}

bool NativeSkiaOutputDevice::textureIsFlipped()
{
    return false;
}

QSize NativeSkiaOutputDevice::size()
{
    return m_frontBuffer ? toQt(m_frontBuffer->shape().imageInfo.dimensions()) : QSize();
}

bool NativeSkiaOutputDevice::requiresAlphaChannel()
{
    return m_requiresAlpha;
}

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

void NativeSkiaOutputDevice::SwapBuffersFinished()
{
    {
        QMutexLocker locker(&m_mutex);
        std::swap(m_backBuffer, m_middleBuffer);
    }

    FinishSwapBuffers(gfx::SwapCompletionResult(gfx::SwapResult::SWAP_ACK),
                      gfx::Size(m_shape.imageInfo.width(), m_shape.imageInfo.height()),
                      std::move(m_frame));
}

NativeSkiaOutputDevice::Buffer::Buffer(NativeSkiaOutputDevice *parent)
    : m_parent(parent), m_shape(m_parent->m_shape)
{
}

NativeSkiaOutputDevice::Buffer::~Buffer()
{
    if (m_scopedSkiaWriteAccess)
        endWriteSkia(false);

    if (!m_mailbox.IsZero())
        m_parent->m_factory->DestroySharedImage(m_mailbox);
}

// The following Buffer methods are based on
// components/viz/service/display_embedder/output_presenter.cc: Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
bool NativeSkiaOutputDevice::Buffer::initialize()
{
    static const uint32_t kDefaultSharedImageUsage = gpu::SHARED_IMAGE_USAGE_SCANOUT
            | gpu::SHARED_IMAGE_USAGE_DISPLAY_READ | gpu::SHARED_IMAGE_USAGE_DISPLAY_WRITE
            | gpu::SHARED_IMAGE_USAGE_GLES2_FRAMEBUFFER_HINT;
    auto mailbox = gpu::Mailbox::GenerateForSharedImage();

    SkColorType skColorType = m_shape.imageInfo.colorType();
    if (!m_parent->m_factory->CreateSharedImage(
                mailbox, viz::SkColorTypeToSinglePlaneSharedImageFormat(skColorType),
                { m_shape.imageInfo.width(), m_shape.imageInfo.height() }, m_shape.colorSpace,
                kTopLeft_GrSurfaceOrigin, kPremul_SkAlphaType, m_parent->m_deps->GetSurfaceHandle(),
                kDefaultSharedImageUsage, "QWE_SharedImageBuffer")) {
        LOG(ERROR) << "CreateSharedImage failed.";
        return false;
    }
    m_mailbox = mailbox;

    m_skiaRepresentation =
            m_parent->m_representationFactory->ProduceSkia(m_mailbox, m_parent->m_contextState);
    if (!m_skiaRepresentation) {
        LOG(ERROR) << "ProduceSkia() failed.";
        return false;
    }

    if (m_parent->m_isNativeBufferSupported) {
        m_overlayRepresentation = m_parent->m_representationFactory->ProduceOverlay(m_mailbox);
        if (!m_overlayRepresentation) {
            LOG(ERROR) << "ProduceOverlay() failed";
            return false;
        }
    }

    return true;
}

SkSurface *NativeSkiaOutputDevice::Buffer::beginWriteSkia()
{
    DCHECK(!m_scopedSkiaWriteAccess);
    DCHECK(!m_presentCount);
    DCHECK(m_endSemaphores.empty());

    std::vector<GrBackendSemaphore> beginSemaphores;
    SkSurfaceProps surface_props{ 0, kUnknown_SkPixelGeometry };

    // Buffer queue is internal to GPU proc and handles texture initialization,
    // so allow uncleared access.
    m_scopedSkiaWriteAccess = m_skiaRepresentation->BeginScopedWriteAccess(
            m_shape.sampleCount, surface_props, &beginSemaphores, &m_endSemaphores,
            gpu::SharedImageRepresentation::AllowUnclearedAccess::kYes);
    DCHECK(m_scopedSkiaWriteAccess);
    if (!beginSemaphores.empty()) {
        m_scopedSkiaWriteAccess->surface()->wait(beginSemaphores.size(), beginSemaphores.data(),
                                                 /*deleteSemaphoresAfterWait=*/false);
    }
    return m_scopedSkiaWriteAccess->surface();
}

void NativeSkiaOutputDevice::Buffer::endWriteSkia(bool force_flush)
{
    // The Flush now takes place in finishPaintCurrentBuffer on the CPU side.
    // check if end_semaphores is not empty then flush here
    DCHECK(m_scopedSkiaWriteAccess);
    if (!m_endSemaphores.empty() || force_flush) {
        GrFlushInfo flush_info = {};
        flush_info.fNumSemaphores = m_endSemaphores.size();
        flush_info.fSignalSemaphores = m_endSemaphores.data();
        auto *direct_context =
                m_scopedSkiaWriteAccess->surface()->recordingContext()->asDirectContext();
        DCHECK(direct_context);
        direct_context->flush(m_scopedSkiaWriteAccess->surface(), {});
        m_scopedSkiaWriteAccess->ApplyBackendSurfaceEndState();
        direct_context->flush(m_scopedSkiaWriteAccess->surface(), flush_info, nullptr);
        direct_context->submit();
    }
    m_scopedSkiaWriteAccess.reset();
    m_endSemaphores.clear();

    // SkiaRenderer always draws the full frame.
    m_skiaRepresentation->SetCleared();
}

std::vector<GrBackendSemaphore> NativeSkiaOutputDevice::Buffer::takeEndWriteSkiaSemaphores()
{
    return std::exchange(m_endSemaphores, {});
}

void NativeSkiaOutputDevice::Buffer::createSkImageOnGPUThread()
{
    if (!m_scopedSkiaReadAccess) {
        m_skImageMutex.unlock();
        return;
    }

    m_cachedSkImage = m_scopedSkiaReadAccess->CreateSkImage(m_parent->m_contextState.get());
    m_skImageMutex.unlock();
    if (!m_cachedSkImage)
        qWarning("SKIA: Failed to create SkImage.");
}

void NativeSkiaOutputDevice::Buffer::beginPresent()
{
    if (++m_presentCount != 1) {
        DCHECK(m_scopedOverlayReadAccess || m_scopedSkiaReadAccess);
        return;
    }

    DCHECK(!m_scopedSkiaWriteAccess);
    DCHECK(!m_scopedOverlayReadAccess && !m_scopedSkiaReadAccess);

    if (m_overlayRepresentation) {
        m_scopedOverlayReadAccess = m_overlayRepresentation->BeginScopedReadAccess();
        DCHECK(m_scopedOverlayReadAccess);
        m_acquireFence = TakeGpuFence(m_scopedOverlayReadAccess->TakeAcquireFence());
    } else {
        DCHECK(m_skiaRepresentation);
        std::vector<GrBackendSemaphore> beginSemaphores;
        m_scopedSkiaReadAccess =
                m_skiaRepresentation->BeginScopedReadAccess(&beginSemaphores, nullptr);
        DCHECK(m_scopedSkiaReadAccess);
        if (!beginSemaphores.empty())
            qWarning("SKIA: Unexpected semaphores while reading texture, wait is not implemented.");

        m_skImageMutex.tryLock();
        m_parent->m_gpuTaskRunner->PostTask(FROM_HERE,
                                            base::BindOnce(&NativeSkiaOutputDevice::Buffer::createSkImageOnGPUThread,
                                                           base::Unretained(this)));
    }
}

void NativeSkiaOutputDevice::Buffer::endPresent()
{
    if (!m_presentCount)
        return;
    DCHECK(m_scopedOverlayReadAccess || m_scopedSkiaReadAccess);
    if (--m_presentCount)
        return;

    if (m_scopedOverlayReadAccess) {
        DCHECK(!m_scopedSkiaReadAccess);
        m_scopedOverlayReadAccess.reset();
    } else if (m_scopedSkiaReadAccess) {
        DCHECK(!m_scopedOverlayReadAccess);
        QMutexLocker locker(&m_skImageMutex);
        m_scopedSkiaReadAccess.reset();
    }
}

void NativeSkiaOutputDevice::Buffer::freeTexture()
{
    if (textureCleanupCallback) {
        textureCleanupCallback();
        textureCleanupCallback = nullptr;
    }
}

void NativeSkiaOutputDevice::Buffer::createFence()
{
    // For some reason we still need to create this, but we do not need to wait on it.
    if (m_parent->m_contextState->gr_context_type() == gpu::GrContextType::kGL)
        m_fence = gl::GLFence::Create();
}

void NativeSkiaOutputDevice::Buffer::consumeFence()
{
    if (m_acquireFence) {
        m_acquireFence->Wait();
        m_acquireFence.reset();
    }
}

sk_sp<SkImage> NativeSkiaOutputDevice::Buffer::skImage()
{
    QMutexLocker locker(&m_skImageMutex);
    return m_cachedSkImage;
}
#if defined(USE_OZONE)
scoped_refptr<gfx::NativePixmap> NativeSkiaOutputDevice::Buffer::nativePixmap()
{
    DCHECK(m_presentCount);
    if (!m_scopedOverlayReadAccess)
        return nullptr;

    return m_scopedOverlayReadAccess->GetNativePixmap();
}
#elif defined(Q_OS_WIN)
absl::optional<gl::DCLayerOverlayImage> NativeSkiaOutputDevice::Buffer::overlayImage() const
{
    DCHECK(m_presentCount);
    return m_scopedOverlayReadAccess->GetDCLayerOverlayImage();
}
#elif defined(Q_OS_MACOS)
gfx::ScopedIOSurface NativeSkiaOutputDevice::Buffer::ioSurface() const
{
    DCHECK(m_presentCount);
    return m_scopedOverlayReadAccess->GetIOSurface();
}
#endif

} // namespace QtWebEngineCore