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