1 /*
2  * Copyright 2017 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 GrOnFlushResourceProvider_DEFINED
9 #define GrOnFlushResourceProvider_DEFINED
10 
11 #include "GrTypes.h"
12 #include "GrDeferredUpload.h"
13 #include "GrOpFlushState.h"
14 #include "GrResourceProvider.h"
15 #include "SkRefCnt.h"
16 #include "SkTArray.h"
17 
18 class GrDrawingManager;
19 class GrOpList;
20 class GrOnFlushResourceProvider;
21 class GrRenderTargetOpList;
22 class GrRenderTargetContext;
23 class GrSurfaceProxy;
24 
25 class SkColorSpace;
26 class SkSurfaceProps;
27 
28 /*
29  * This is the base class from which all pre-flush callback objects must be derived. It
30  * provides the "preFlush" / "postFlush" interface.
31  */
32 class GrOnFlushCallbackObject {
33 public:
34     virtual ~GrOnFlushCallbackObject() { }
35 
36     /*
37      * The onFlush callback allows subsystems (e.g., text, path renderers) to create atlases
38      * for a specific flush. All the GrOpList IDs required for the flush are passed into the
39      * callback. The callback should return the render target contexts used to render the atlases
40      * in 'results'.
41      */
42     virtual void preFlush(GrOnFlushResourceProvider*,
43                           const uint32_t* opListIDs, int numOpListIDs,
44                           SkTArray<sk_sp<GrRenderTargetContext>>* results) = 0;
45 
46     /**
47      * Called once flushing is complete and all ops indicated by preFlush have been executed and
48      * released. startTokenForNextFlush can be used to track resources used in the current flush.
49      */
50     virtual void postFlush(GrDeferredUploadToken startTokenForNextFlush,
51                            const uint32_t* opListIDs, int numOpListIDs) {}
52 
53     /**
54      * Tells the callback owner to hold onto this object when freeing GPU resources
55      *
56      * In particular, GrDrawingManager::freeGPUResources() deletes all the path renderers.
57      * Any OnFlushCallbackObject associated with a path renderer will need to be deleted.
58      */
59     virtual bool retainOnFreeGpuResources() { return false; }
60 };
61 
62 /*
63  * This class is a shallow wrapper around the drawing manager. It is passed into the
64  * onFlush callbacks and is intended to limit the functionality available to them.
65  * It should never have additional data members or virtual methods.
66  */
67 class GrOnFlushResourceProvider {
68 public:
69     explicit GrOnFlushResourceProvider(GrDrawingManager* drawingMgr) : fDrawingMgr(drawingMgr) {}
70 
71     sk_sp<GrRenderTargetContext> makeRenderTargetContext(const GrSurfaceDesc&,
72                                                          sk_sp<SkColorSpace>,
73                                                          const SkSurfaceProps*);
74 
75     sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
76                                                          sk_sp<SkColorSpace>,
77                                                          const SkSurfaceProps*);
78 
79     bool instatiateProxy(GrSurfaceProxy*);
80 
81     // Creates a GPU buffer with a "dynamic" access pattern.
82     sk_sp<GrBuffer> makeBuffer(GrBufferType, size_t, const void* data = nullptr);
83 
84     // Either finds and refs, or creates a static GPU buffer with the given data.
85     sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType, size_t, const void* data,
86                                                  const GrUniqueKey&);
87 
88     const GrCaps* caps() const;
89 
90 private:
91     GrOnFlushResourceProvider(const GrOnFlushResourceProvider&) = delete;
92     GrOnFlushResourceProvider& operator=(const GrOnFlushResourceProvider&) = delete;
93 
94     GrDrawingManager* fDrawingMgr;
95 };
96 
97 #endif
98