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