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