1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2019 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrVkSecondaryCBDrawContext_DEFINED
9cb93a386Sopenharmony_ci#define GrVkSecondaryCBDrawContext_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/core/SkRefCnt.h"
12cb93a386Sopenharmony_ci#include "include/core/SkSurfaceProps.h"
13cb93a386Sopenharmony_ci#include "include/core/SkTypes.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ciclass GrBackendSemaphore;
16cb93a386Sopenharmony_ciclass GrRecordingContext;
17cb93a386Sopenharmony_cistruct GrVkDrawableInfo;
18cb93a386Sopenharmony_cinamespace skgpu { class BaseDevice; }
19cb93a386Sopenharmony_ciclass SkCanvas;
20cb93a386Sopenharmony_ciclass SkDeferredDisplayList;
21cb93a386Sopenharmony_cistruct SkImageInfo;
22cb93a386Sopenharmony_ciclass SkSurfaceCharacterization;
23cb93a386Sopenharmony_ciclass SkSurfaceProps;
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci/**
26cb93a386Sopenharmony_ci * This class is a private header that is intended to only be used inside of Chromium. This requires
27cb93a386Sopenharmony_ci * Chromium to burrow in and include this specifically since it is not part of skia's public include
28cb93a386Sopenharmony_ci * directory.
29cb93a386Sopenharmony_ci */
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci/**
32cb93a386Sopenharmony_ci * This class is used to draw into an external Vulkan secondary command buffer that is imported
33cb93a386Sopenharmony_ci * by the client. The secondary command buffer that gets imported must already have had begin called
34cb93a386Sopenharmony_ci * on it with VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT. Thus any draws to the imported
35cb93a386Sopenharmony_ci * command buffer cannot require changing the render pass. This requirement means that certain types
36cb93a386Sopenharmony_ci * of draws will not be supported when using a GrVkSecondaryCBDrawContext. This includes:
37cb93a386Sopenharmony_ci *     Draws that require a dst copy for blending will be dropped
38cb93a386Sopenharmony_ci *     Text draws will be dropped (these may require intermediate uploads of text data)
39cb93a386Sopenharmony_ci *     Read and Write pixels will not work
40cb93a386Sopenharmony_ci *     Any other draw that requires a copy will fail (this includes using backdrop filter with save
41cb93a386Sopenharmony_ci *         layer).
42cb93a386Sopenharmony_ci *     Stenciling is also disabled, but that should not restrict any actual draws from working.
43cb93a386Sopenharmony_ci *
44cb93a386Sopenharmony_ci * While using a GrVkSecondaryCBDrawContext, the client can also draw into normal SkSurfaces and
45cb93a386Sopenharmony_ci * then draw those SkSufaces (as SkImages) into the GrVkSecondaryCBDrawContext. If any of the
46cb93a386Sopenharmony_ci * previously mentioned unsupported draws are needed by the client, they can draw them into an
47cb93a386Sopenharmony_ci * offscreen surface, and then draw that into the GrVkSecondaryCBDrawContext.
48cb93a386Sopenharmony_ci *
49cb93a386Sopenharmony_ci * After all drawing to the GrVkSecondaryCBDrawContext has been done, the client must call flush()
50cb93a386Sopenharmony_ci * on the GrVkSecondaryCBDrawContext to actually fill in the secondary VkCommandBuffer with the
51cb93a386Sopenharmony_ci * draws.
52cb93a386Sopenharmony_ci *
53cb93a386Sopenharmony_ci * Additionally, the client must keep the GrVkSecondaryCBDrawContext alive until the secondary
54cb93a386Sopenharmony_ci * VkCommandBuffer has been submitted and all work finished on the GPU. Before deleting the
55cb93a386Sopenharmony_ci * GrVkSecondaryCBDrawContext, the client must call releaseResources() so that Skia can cleanup
56cb93a386Sopenharmony_ci * any internal objects that were created for the draws into the secondary command buffer.
57cb93a386Sopenharmony_ci */
58cb93a386Sopenharmony_ciclass SK_SPI GrVkSecondaryCBDrawContext : public SkRefCnt {
59cb93a386Sopenharmony_cipublic:
60cb93a386Sopenharmony_ci    static sk_sp<GrVkSecondaryCBDrawContext> Make(GrRecordingContext*,
61cb93a386Sopenharmony_ci                                                  const SkImageInfo&,
62cb93a386Sopenharmony_ci                                                  const GrVkDrawableInfo&,
63cb93a386Sopenharmony_ci                                                  const SkSurfaceProps* props);
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci    ~GrVkSecondaryCBDrawContext() override;
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    SkCanvas* getCanvas();
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci    // Records all the draws to the imported secondary command buffer and sets any dependent
70cb93a386Sopenharmony_ci    // offscreen draws to the GPU.
71cb93a386Sopenharmony_ci    void flush();
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    /** Inserts a list of GPU semaphores that Skia will have the driver wait on before executing
74cb93a386Sopenharmony_ci        commands for this secondary CB. The wait semaphores will get added to the VkCommandBuffer
75cb93a386Sopenharmony_ci        owned by this GrContext when flush() is called, and not the command buffer which the
76cb93a386Sopenharmony_ci        Secondary CB is from. This will guarantee that the driver waits on the semaphores before
77cb93a386Sopenharmony_ci        the secondary command buffer gets executed. If this call returns false, then the GPU
78cb93a386Sopenharmony_ci        back end will not wait on any passed in semaphores, and the client will still own the
79cb93a386Sopenharmony_ci        semaphores, regardless of the value of deleteSemaphoresAfterWait.
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci        If deleteSemaphoresAfterWait is false then Skia will not delete the semaphores. In this case
82cb93a386Sopenharmony_ci        it is the client's responsibility to not destroy or attempt to reuse the semaphores until it
83cb93a386Sopenharmony_ci        knows that Skia has finished waiting on them. This can be done by using finishedProcs
84cb93a386Sopenharmony_ci        on flush calls.
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci        @param numSemaphores               size of waitSemaphores array
87cb93a386Sopenharmony_ci        @param waitSemaphores              array of semaphore containers
88cb93a386Sopenharmony_ci        @paramm deleteSemaphoresAfterWait  who owns and should delete the semaphores
89cb93a386Sopenharmony_ci        @return                            true if GPU is waiting on semaphores
90cb93a386Sopenharmony_ci    */
91cb93a386Sopenharmony_ci    bool wait(int numSemaphores,
92cb93a386Sopenharmony_ci              const GrBackendSemaphore waitSemaphores[],
93cb93a386Sopenharmony_ci              bool deleteSemaphoresAfterWait = true);
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci    // This call will release all resources held by the draw context. The client must call
96cb93a386Sopenharmony_ci    // releaseResources() before deleting the drawing context. However, the resources also include
97cb93a386Sopenharmony_ci    // any Vulkan resources that were created and used for draws. Therefore the client must only
98cb93a386Sopenharmony_ci    // call releaseResources() after submitting the secondary command buffer, and waiting for it to
99cb93a386Sopenharmony_ci    // finish on the GPU. If it is called earlier then some vulkan objects may be deleted while they
100cb93a386Sopenharmony_ci    // are still in use by the GPU.
101cb93a386Sopenharmony_ci    void releaseResources();
102cb93a386Sopenharmony_ci
103cb93a386Sopenharmony_ci    const SkSurfaceProps& props() const { return fProps; }
104cb93a386Sopenharmony_ci
105cb93a386Sopenharmony_ci    // TODO: Fill out these calls to support DDL
106cb93a386Sopenharmony_ci    bool characterize(SkSurfaceCharacterization* characterization) const;
107cb93a386Sopenharmony_ci
108cb93a386Sopenharmony_ci#ifndef SK_DDL_IS_UNIQUE_POINTER
109cb93a386Sopenharmony_ci    bool draw(sk_sp<const SkDeferredDisplayList> deferredDisplayList);
110cb93a386Sopenharmony_ci#else
111cb93a386Sopenharmony_ci    bool draw(const SkDeferredDisplayList* deferredDisplayList);
112cb93a386Sopenharmony_ci#endif
113cb93a386Sopenharmony_ci
114cb93a386Sopenharmony_ci    bool isCompatible(const SkSurfaceCharacterization& characterization) const;
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ciprivate:
117cb93a386Sopenharmony_ci    explicit GrVkSecondaryCBDrawContext(sk_sp<skgpu::BaseDevice>, const SkSurfaceProps*);
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    sk_sp<skgpu::BaseDevice>  fDevice;
120cb93a386Sopenharmony_ci    std::unique_ptr<SkCanvas> fCachedCanvas;
121cb93a386Sopenharmony_ci    const SkSurfaceProps      fProps;
122cb93a386Sopenharmony_ci
123cb93a386Sopenharmony_ci    using INHERITED = SkRefCnt;
124cb93a386Sopenharmony_ci};
125cb93a386Sopenharmony_ci
126cb93a386Sopenharmony_ci#endif
127