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:
~GrOnFlushCallbackObject()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      */
postFlush(GrDeferredUploadToken startTokenForNextFlush,const uint32_t * opListIDs,int numOpListIDs)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      */
retainOnFreeGpuResources()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     sk_sp<GrRenderTargetContext> makeRenderTargetContext(const GrSurfaceDesc&,
70                                                          sk_sp<SkColorSpace>,
71                                                          const SkSurfaceProps*);
72 
73     sk_sp<GrRenderTargetContext> makeRenderTargetContext(sk_sp<GrSurfaceProxy>,
74                                                          sk_sp<SkColorSpace>,
75                                                          const SkSurfaceProps*);
76 
77     bool instatiateProxy(GrSurfaceProxy*);
78 
79     // Creates a GPU buffer with a "dynamic" access pattern.
80     sk_sp<GrBuffer> makeBuffer(GrBufferType, size_t, const void* data = nullptr);
81 
82     // Either finds and refs, or creates a static GPU buffer with the given data.
83     sk_sp<const GrBuffer> findOrMakeStaticBuffer(GrBufferType, size_t, const void* data,
84                                                  const GrUniqueKey&);
85 
86     const GrCaps* caps() const;
87 
88 private:
GrOnFlushResourceProvider(GrDrawingManager * drawingMgr)89     explicit GrOnFlushResourceProvider(GrDrawingManager* drawingMgr) : fDrawingMgr(drawingMgr) {}
90     GrOnFlushResourceProvider(const GrOnFlushResourceProvider&) = delete;
91     GrOnFlushResourceProvider& operator=(const GrOnFlushResourceProvider&) = delete;
92 
93     GrDrawingManager* fDrawingMgr;
94 
95     friend class GrDrawingManager; // to construct this type.
96 };
97 
98 #endif
99