• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrRecordingContext;
17 struct GrVkDrawableInfo;
18 namespace skgpu { class BaseDevice; }
19 class SkCanvas;
20 class SkDeferredDisplayList;
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_SPI GrVkSecondaryCBDrawContext : public SkRefCnt {
59 public:
60     static sk_sp<GrVkSecondaryCBDrawContext> Make(GrRecordingContext*,
61                                                   const SkImageInfo&,
62                                                   const GrVkDrawableInfo&,
63                                                   const SkSurfaceProps* props);
64 
65     ~GrVkSecondaryCBDrawContext() override;
66 
67     SkCanvas* getCanvas();
68 
69     // Records all the draws to the imported secondary command buffer and sets any dependent
70     // offscreen draws to the GPU.
71     void flush();
72 
73     /** Inserts a list of GPU semaphores that Skia will have the driver wait on before executing
74         commands for this secondary CB. The wait semaphores will get added to the VkCommandBuffer
75         owned by this GrContext when flush() is called, and not the command buffer which the
76         Secondary CB is from. This will guarantee that the driver waits on the semaphores before
77         the secondary command buffer gets executed. If this call returns false, then the GPU
78         back end will not wait on any passed in semaphores, and the client will still own the
79         semaphores, regardless of the value of deleteSemaphoresAfterWait.
80 
81         If deleteSemaphoresAfterWait is false then Skia will not delete the semaphores. In this case
82         it is the client's responsibility to not destroy or attempt to reuse the semaphores until it
83         knows that Skia has finished waiting on them. This can be done by using finishedProcs
84         on flush calls.
85 
86         @param numSemaphores               size of waitSemaphores array
87         @param waitSemaphores              array of semaphore containers
88         @paramm deleteSemaphoresAfterWait  who owns and should delete the semaphores
89         @return                            true if GPU is waiting on semaphores
90     */
91     bool wait(int numSemaphores,
92               const GrBackendSemaphore waitSemaphores[],
93               bool deleteSemaphoresAfterWait = true);
94 
95     // This call will release all resources held by the draw context. The client must call
96     // releaseResources() before deleting the drawing context. However, the resources also include
97     // any Vulkan resources that were created and used for draws. Therefore the client must only
98     // call releaseResources() after submitting the secondary command buffer, and waiting for it to
99     // finish on the GPU. If it is called earlier then some vulkan objects may be deleted while they
100     // are still in use by the GPU.
101     void releaseResources();
102 
props()103     const SkSurfaceProps& props() const { return fProps; }
104 
105     // TODO: Fill out these calls to support DDL
106     bool characterize(SkSurfaceCharacterization* characterization) const;
107 
108 #ifndef SK_DDL_IS_UNIQUE_POINTER
109     bool draw(sk_sp<const SkDeferredDisplayList> deferredDisplayList);
110 #else
111     bool draw(const SkDeferredDisplayList* deferredDisplayList);
112 #endif
113 
114     bool isCompatible(const SkSurfaceCharacterization& characterization) const;
115 
116 private:
117     explicit GrVkSecondaryCBDrawContext(sk_sp<skgpu::BaseDevice>, const SkSurfaceProps*);
118 
119     sk_sp<skgpu::BaseDevice>  fDevice;
120     std::unique_ptr<SkCanvas> fCachedCanvas;
121     const SkSurfaceProps      fProps;
122 
123     using INHERITED = SkRefCnt;
124 };
125 
126 #endif
127