summaryrefslogtreecommitdiffstats
path: root/src/core/compositor/display_skia_output_device.cpp
blob: 7f42d61dec57b8f9bf6af3cba4f006a849ca1244 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (C) 2021 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_skia_output_device.h"

#include "type_conversion.h"

#include "gpu/command_buffer/service/skia_utils.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"

#if QT_CONFIG(webengine_vulkan)
#if defined(USE_OZONE)
#include "ui/ozone/buildflags.h"
#if BUILDFLAG(OZONE_PLATFORM_X11)
// We need to define USE_VULKAN_XCB for proper vulkan function pointers.
// Avoiding it may lead to call wrong vulkan functions.
// This is originally defined in chromium/gpu/vulkan/BUILD.gn.
#define USE_VULKAN_XCB
#endif // BUILDFLAG(OZONE_PLATFORM_X11)
#endif // defined(USE_OZONE)
#include "gpu/vulkan/vulkan_function_pointers.h"

#include "components/viz/common/gpu/vulkan_context_provider.h"
#include "compositor/display_skia_output_device.h"
#include "gpu/vulkan/vma_wrapper.h"
#include "gpu/vulkan/vulkan_device_queue.h"
#include "third_party/vulkan_memory_allocator/include/vk_mem_alloc.h"

#include <QQuickWindow>
#include <QVulkanInstance>
#include <QVulkanFunctions>
#include <QVulkanDeviceFunctions>
#endif // QT_CONFIG(webengine_vulkan)

namespace QtWebEngineCore {

class DisplaySkiaOutputDevice::Buffer
{
public:
    Buffer(DisplaySkiaOutputDevice *parent)
        : m_parent(parent)
        , m_shape(m_parent->m_shape)
    {
        const auto &colorType = m_shape.characterization.colorType();
        DCHECK(colorType != kUnknown_SkColorType);

        m_texture = m_parent->m_contextState->gr_context()->createBackendTexture(
                m_shape.characterization.width(), m_shape.characterization.height(), colorType,
                GrMipMapped::kNo, GrRenderable::kYes);
        DCHECK(m_texture.isValid());

        if (m_texture.backend() == GrBackendApi::kVulkan) {
#if QT_CONFIG(webengine_vulkan)
            initVulkan();
#else
            NOTREACHED();
#endif
        } else {
            auto info = SkImageInfo::Make(m_shape.characterization.width(), m_shape.characterization.height(),
                                          colorType, kUnpremul_SkAlphaType);
            m_estimatedSize = info.computeMinByteSize();
        }
        m_parent->memory_type_tracker_->TrackMemAlloc(m_estimatedSize);

        SkSurfaceProps surfaceProps = SkSurfaceProps{0, kUnknown_SkPixelGeometry};
        m_surface = SkSurface::MakeFromBackendTexture(
                m_parent->m_contextState->gr_context(), m_texture,
                m_parent->capabilities_.output_surface_origin == gfx::SurfaceOrigin::kTopLeft
                ? kTopLeft_GrSurfaceOrigin
                : kBottomLeft_GrSurfaceOrigin,
                0 /* sampleCount */, colorType, m_shape.colorSpace.ToSkColorSpace(),
                &surfaceProps);
    }

    ~Buffer()
    {
#if QT_CONFIG(webengine_vulkan) && defined(Q_OS_WIN)
        CloseHandle(m_win32Handle);
#endif
        DeleteGrBackendTexture(m_parent->m_contextState.get(), &m_texture);
        m_parent->memory_type_tracker_->TrackMemFree(m_estimatedSize);
    }

    void createFence()
    {
        m_fence = CompositorResourceFence::create();
    }

    void consumeFence()
    {
        if (m_fence) {
            m_fence->wait();
            m_fence.reset();
        }
    }

    const Shape &shape() const { return m_shape; }
    const GrBackendTexture &texture() const { return m_texture; }
    SkSurface *surface() const { return m_surface.get(); }

#if QT_CONFIG(webengine_vulkan)
    const VkImageCreateInfo *imageCreateInfo() const { return &m_imageCreateInfo; }
    uint64_t allocationSize() const { return m_estimatedSize; }
    VkImageLayout imageLayout() const { return m_imageLayout; }
    uint32_t memoryTypeIndex() const { return m_memoryTypeIndex.value(); }

#if defined(Q_OS_WIN)
    const VkExternalMemoryHandleTypeFlagBits externalMemoryHandleType() const
    {
        return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_BIT_KHR;
    }

    HANDLE externalMemoryHandle() const { return m_win32Handle; }
#else
    const VkExternalMemoryHandleTypeFlagBits externalMemoryHandleType() const
    {
        return VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR;
    }

    int externalMemoryHandle() const
    {
        VkMemoryGetFdInfoKHR exportInfo = { VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR };
        exportInfo.pNext = nullptr;
        exportInfo.memory = m_imageInfo.fAlloc.fMemory;
        exportInfo.handleType = externalMemoryHandleType();

        // Importing Vulkan memory object closes the file descriptor.
        int fd = -1;
        if (m_vfp->vkGetMemoryFdKHR(m_vulkanDevice, &exportInfo, &fd) != VK_SUCCESS)
            qFatal("VULKAN: Unable to extract file descriptor out of external VkImage!");

        return fd;
    }
#endif // defined(Q_OS_WIN)
#endif // QT_CONFIG(webengine_vulkan)

private:
    DisplaySkiaOutputDevice *m_parent;
    Shape m_shape;
    GrBackendTexture m_texture;
    sk_sp<SkSurface> m_surface;
    uint64_t m_estimatedSize = 0;
    scoped_refptr<CompositorResourceFence> m_fence;

#if QT_CONFIG(webengine_vulkan)
    static VkSampleCountFlagBits vkSampleCount(uint32_t sampleCount)
    {
        Q_ASSERT(sampleCount >= 1);
        switch (sampleCount) {
        case 1:
            return VK_SAMPLE_COUNT_1_BIT;
        case 2:
            return VK_SAMPLE_COUNT_2_BIT;
        case 4:
            return VK_SAMPLE_COUNT_4_BIT;
        case 8:
            return VK_SAMPLE_COUNT_8_BIT;
        case 16:
            return VK_SAMPLE_COUNT_16_BIT;
        default:
            Q_UNREACHABLE();
        }

        return VK_SAMPLE_COUNT_1_BIT;
    }

    void initVulkan()
    {
        bool success = m_texture.getVkImageInfo(&m_imageInfo);
        if (!success)
            qFatal("VULKAN: Failed to get external Vulkan resources from Skia!");

        m_vfp = gpu::GetVulkanFunctionPointers();
        gpu::VulkanDeviceQueue *vulkanDeviceQueue =
                m_parent->m_contextState->vk_context_provider()->GetDeviceQueue();
        m_vulkanDevice = vulkanDeviceQueue->GetVulkanDevice();

        // Store allocation size for the external VkImage.
        m_estimatedSize = m_imageInfo.fAlloc.fSize;

        // Store initial layout for the imported image.
        if (vulkanDeviceQueue->vk_physical_device_properties().vendorID == 0x10DE) {
            // FIXME: This is a workaround for Nvidia driver.
            // The imported image is empty if the initialLayout is not PREINITIALIZED.
            m_imageLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
        } else {
            // The initialLayout should be undefined for the external image.
            // https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VkImageCreateInfo.html#VUID-VkImageCreateInfo-pNext-01443
            m_imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
        }

        // Specify VkCreateImageInfo for the imported VkImage.
        // The specification should match with the texture's VkImage.
        m_externalMemoryImageCreateInfo.sType =
                VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO_KHR;
        m_externalMemoryImageCreateInfo.pNext = nullptr;
        m_externalMemoryImageCreateInfo.handleTypes = externalMemoryHandleType();

        m_imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
        m_imageCreateInfo.pNext = &m_externalMemoryImageCreateInfo;
        m_imageCreateInfo.flags =
                m_imageInfo.fProtected == GrProtected::kYes ? VK_IMAGE_CREATE_PROTECTED_BIT : 0;
        m_imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
        m_imageCreateInfo.format = m_imageInfo.fFormat;
        m_imageCreateInfo.extent.width = static_cast<uint32_t>(m_shape.characterization.width());
        m_imageCreateInfo.extent.height = static_cast<uint32_t>(m_shape.characterization.height());
        m_imageCreateInfo.extent.depth = 1;
        m_imageCreateInfo.mipLevels = m_imageInfo.fLevelCount;
        m_imageCreateInfo.arrayLayers = 1;
        m_imageCreateInfo.samples = vkSampleCount(m_imageInfo.fSampleCount);
        m_imageCreateInfo.tiling = m_imageInfo.fImageTiling;
        m_imageCreateInfo.usage = m_imageInfo.fImageUsageFlags;
        m_imageCreateInfo.sharingMode = m_imageInfo.fSharingMode;
        m_imageCreateInfo.queueFamilyIndexCount = 0;
        m_imageCreateInfo.pQueueFamilyIndices = nullptr;
        m_imageCreateInfo.initialLayout = m_imageLayout;

#if defined(Q_OS_WIN)
        // Extract Windows handle for the memory of the external VkImage.
        // Ownership should be released at the destruction of the Buffer object.
        // Multiple handles for the same memory cause issues on Windows even if one is
        // released before extracting the other.
        VkMemoryGetWin32HandleInfoKHR exportInfo = {
            VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR
        };
        exportInfo.pNext = nullptr;
        exportInfo.memory = m_imageInfo.fAlloc.fMemory;
        exportInfo.handleType = externalMemoryHandleType();

        if (m_vfp->vkGetMemoryWin32HandleKHR(m_vulkanDevice, &exportInfo, &m_win32Handle)
            != VK_SUCCESS) {
            qFatal("VULKAN: Unable to extract handle out of external VkImage!");
        }
#endif // defined(Q_OS_WIN)

        VmaAllocator vmaAllocator = vulkanDeviceQueue->vma_allocator();
        const VmaAllocation vmaAllocation =
                reinterpret_cast<const VmaAllocation>(m_imageInfo.fAlloc.fBackendMemory);
        VmaAllocationInfo vmaInfo;
        gpu::vma::GetAllocationInfo(vmaAllocator, vmaAllocation, &vmaInfo);
        m_memoryTypeIndex = vmaInfo.memoryType;
    }

    gpu::VulkanFunctionPointers *m_vfp = nullptr;
    VkDevice m_vulkanDevice = VK_NULL_HANDLE;
    VkImageLayout m_imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
    GrVkImageInfo m_imageInfo;
    VkExternalMemoryImageCreateInfoKHR m_externalMemoryImageCreateInfo = {};
    VkImageCreateInfo m_imageCreateInfo = {};
#if defined(Q_OS_WIN)
    HANDLE m_win32Handle = nullptr;
#endif
    absl::optional<uint32_t> m_memoryTypeIndex;
#endif // QT_CONFIG(webengine_vulkan)
};

DisplaySkiaOutputDevice::DisplaySkiaOutputDevice(
        scoped_refptr<gpu::SharedContextState> contextState, gpu::MemoryTracker *memoryTracker,
        DidSwapBufferCompleteCallback didSwapBufferCompleteCallback)
    : SkiaOutputDevice(contextState->gr_context(), memoryTracker, didSwapBufferCompleteCallback)
    , Compositor(contextState->GrContextIsVulkan() ? Compositor::Type::Vulkan
                                                   : Compositor::Type::OpenGL)
    , m_contextState(contextState)
{
    capabilities_.uses_default_gl_framebuffer = false;
    capabilities_.supports_surfaceless = true;

    capabilities_.sk_color_types[static_cast<int>(gfx::BufferFormat::RGBA_8888)] =
            kRGBA_8888_SkColorType;
    capabilities_.sk_color_types[static_cast<int>(gfx::BufferFormat::RGBX_8888)] =
            kRGBA_8888_SkColorType;
    capabilities_.sk_color_types[static_cast<int>(gfx::BufferFormat::BGRA_8888)] =
            kRGBA_8888_SkColorType;
    capabilities_.sk_color_types[static_cast<int>(gfx::BufferFormat::BGRX_8888)] =
            kRGBA_8888_SkColorType;
}

DisplaySkiaOutputDevice::~DisplaySkiaOutputDevice()
{
}

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

bool DisplaySkiaOutputDevice::Reshape(const SkSurfaceCharacterization &characterization,
                                      const gfx::ColorSpace &colorSpace,
                                      float device_scale_factor,
                                      gfx::OverlayTransform transform)
{
    m_shape = Shape{characterization, device_scale_factor, colorSpace};
    DCHECK_EQ(transform, gfx::OVERLAY_TRANSFORM_NONE);
    return true;
}

void DisplaySkiaOutputDevice::SwapBuffers(BufferPresentedCallback feedback,
                                          viz::OutputSurfaceFrame frame)
{
    DCHECK(m_backBuffer);

    StartSwapBuffers(std::move(feedback));
    m_frame = std::move(frame);
    m_backBuffer->createFence();

    {
        QMutexLocker locker(&m_mutex);
        m_taskRunner = base::ThreadTaskRunnerHandle::Get();
        m_middleBuffer = std::move(m_backBuffer);
        m_readyToUpdate = true;
    }

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

void DisplaySkiaOutputDevice::EnsureBackbuffer()
{
}

void DisplaySkiaOutputDevice::DiscardBackbuffer()
{
}

SkSurface *DisplaySkiaOutputDevice::BeginPaint(std::vector<GrBackendSemaphore> *)
{
    if (!m_backBuffer || m_backBuffer->shape() != m_shape)
        m_backBuffer = std::make_unique<Buffer>(this);
    return m_backBuffer->surface();
}

void DisplaySkiaOutputDevice::EndPaint()
{
}

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

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

int DisplaySkiaOutputDevice::textureId()
{
    if (!m_frontBuffer)
        return 0;

    GrGLTextureInfo info;
    if (!m_frontBuffer->texture().getGLTextureInfo(&info))
        return 0;

    return info.fID;
}

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

bool DisplaySkiaOutputDevice::hasAlphaChannel()
{
    return true;
}

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

#if QT_CONFIG(webengine_vulkan)
VkImage DisplaySkiaOutputDevice::vkImage(QQuickWindow *win)
{
    if (!m_frontBuffer)
        return VK_NULL_HANDLE;

    QSGRendererInterface *ri = win->rendererInterface();
    VkDevice qtVulkanDevice =
            *static_cast<VkDevice *>(ri->getResource(win, QSGRendererInterface::DeviceResource));
    QVulkanDeviceFunctions *df = win->vulkanInstance()->deviceFunctions(qtVulkanDevice);

    df->vkDestroyImage(qtVulkanDevice, m_importedImage, nullptr);
    df->vkFreeMemory(qtVulkanDevice, m_importedImageMemory, nullptr);

    if (df->vkCreateImage(qtVulkanDevice, m_frontBuffer->imageCreateInfo(), nullptr,
                          &m_importedImage)
        != VK_SUCCESS) {
        qFatal("VULKAN: Failed to create imported image!");
    }

    VkMemoryDedicatedAllocateInfoKHR dedicatedAllocateInfo = {
        VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO_KHR
    };
    dedicatedAllocateInfo.pNext = nullptr;
    dedicatedAllocateInfo.image = m_importedImage;
    dedicatedAllocateInfo.buffer = VK_NULL_HANDLE;

#if defined(Q_OS_WIN)
    VkImportMemoryWin32HandleInfoKHR importInfo = {
        VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR
    };
    importInfo.handle = m_frontBuffer->externalMemoryHandle();
#else
    VkImportMemoryFdInfoKHR importInfo = { VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR };
    importInfo.fd = m_frontBuffer->externalMemoryHandle();
#endif // defined(Q_OS_WIN)
    importInfo.pNext = &dedicatedAllocateInfo;
    importInfo.handleType = m_frontBuffer->externalMemoryHandleType();

    VkMemoryAllocateInfo allocateInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO };
    allocateInfo.pNext = &importInfo;
    allocateInfo.allocationSize = m_frontBuffer->allocationSize();
    allocateInfo.memoryTypeIndex = m_frontBuffer->memoryTypeIndex();

    if (df->vkAllocateMemory(qtVulkanDevice, &allocateInfo, nullptr, &m_importedImageMemory)
        != VK_SUCCESS) {
        qFatal("VULKAN: Failed to allocate memory for imported VkImage!");
    }

    df->vkBindImageMemory(qtVulkanDevice, m_importedImage, m_importedImageMemory, 0);

    return m_importedImage;
}

VkImageLayout DisplaySkiaOutputDevice::vkImageLayout()
{
    if (!m_frontBuffer)
        return VK_IMAGE_LAYOUT_UNDEFINED;

    return m_frontBuffer->imageLayout();
}

void DisplaySkiaOutputDevice::releaseVulkanResources(QQuickWindow *win)
{
    VkDevice *vkDevicePtr = static_cast<VkDevice *>(
            win->rendererInterface()->getResource(win, QSGRendererInterface::DeviceResource));

    if (!vkDevicePtr) {
        Q_ASSERT(m_importedImage == VK_NULL_HANDLE && m_importedImageMemory == VK_NULL_HANDLE);
        return;
    }

    QVulkanDeviceFunctions *df = win->vulkanInstance()->deviceFunctions(*vkDevicePtr);

    if (m_importedImage != VK_NULL_HANDLE) {
        df->vkDestroyImage(*vkDevicePtr, m_importedImage, nullptr);
        m_importedImage = VK_NULL_HANDLE;
    }

    if (m_importedImageMemory != VK_NULL_HANDLE) {
        df->vkFreeMemory(*vkDevicePtr, m_importedImageMemory, nullptr);
        m_importedImageMemory = VK_NULL_HANDLE;
    }
}
#endif // QT_CONFIG(webengine_vulkan)

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

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

} // namespace QtWebEngineCore