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 GrRecordingContext_DEFINED
9#define GrRecordingContext_DEFINED
10
11#include "include/core/SkRefCnt.h"
12#include "include/private/GrImageContext.h"
13#include "include/private/SkTArray.h"
14
15class GrAuditTrail;
16class GrBackendFormat;
17class GrDrawingManager;
18class GrOnFlushCallbackObject;
19class GrOpMemoryPool;
20class GrProgramDesc;
21class GrProgramInfo;
22class GrRecordingContextPriv;
23class GrSurfaceContext;
24class GrSurfaceProxy;
25class GrTextBlobCache;
26class SkArenaAlloc;
27
28class GrRecordingContext : public GrImageContext {
29public:
30 ~GrRecordingContext() override;
31
32 SK_API GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const {
33 return INHERITED::defaultBackendFormat(ct, renderable);
34 }
35
36 // Provides access to functions that aren't part of the public API.
37 GrRecordingContextPriv priv();
38 const GrRecordingContextPriv priv() const;
39
40 // The collection of specialized memory arenas for different types of data recorded by a
41 // GrRecordingContext. Arenas does not maintain ownership of the pools it groups together.
42 class Arenas {
43 public:
44 Arenas(GrOpMemoryPool*, SkArenaAlloc*);
45
46 // For storing GrOp-derived classes recorded by a GrRecordingContext
47 GrOpMemoryPool* opMemoryPool() { return fOpMemoryPool; }
48
49 // For storing pipelines and other complex data as-needed by ops
50 SkArenaAlloc* recordTimeAllocator() { return fRecordTimeAllocator; }
51
52 private:
53 GrOpMemoryPool* fOpMemoryPool;
54 SkArenaAlloc* fRecordTimeAllocator;
55 };
56
57protected:
58 friend class GrRecordingContextPriv; // for hidden functions
59 friend class SkDeferredDisplayList; // for OwnedArenas
60 friend class SkDeferredDisplayListPriv; // for ProgramData
61
62 // Like Arenas, but preserves ownership of the underlying pools.
63 class OwnedArenas {
64 public:
65 OwnedArenas();
66 ~OwnedArenas();
67
68 Arenas get();
69
70 OwnedArenas& operator=(OwnedArenas&&);
71
72 private:
73 std::unique_ptr<GrOpMemoryPool> fOpMemoryPool;
74 std::unique_ptr<SkArenaAlloc> fRecordTimeAllocator;
75 };
76
77 GrRecordingContext(GrBackendApi, const GrContextOptions&, uint32_t contextID);
78 bool init(sk_sp<const GrCaps>) override;
79 void setupDrawingManager(bool sortOpsTasks, bool reduceOpsTaskSplitting);
80
81 void abandonContext() override;
82
83 GrDrawingManager* drawingManager();
84
85 Arenas arenas() { return fArenas.get(); }
86 // This entry point should only be used for DDL creation where we want the ops' lifetime to
87 // match that of the DDL.
88 OwnedArenas&& detachArenas();
89
90 struct ProgramData {
91 ProgramData(std::unique_ptr<const GrProgramDesc>, const GrProgramInfo*);
92 ProgramData(ProgramData&&); // for SkTArray
93 ProgramData(const ProgramData&) = delete;
94 ~ProgramData();
95
96 const GrProgramDesc& desc() const { return *fDesc; }
97 const GrProgramInfo& info() const { return *fInfo; }
98
99 private:
100 // TODO: store the GrProgramDescs in the 'fRecordTimeData' arena
101 std::unique_ptr<const GrProgramDesc> fDesc;
102 // The program infos should be stored in 'fRecordTimeData' so do not need to be ref
103 // counted or deleted in the destructor.
104 const GrProgramInfo* fInfo = nullptr;
105 };
106
107 // This entry point gives the recording context a chance to cache the provided
108 // programInfo. The DDL context takes this opportunity to store programInfos as a sidecar
109 // to the DDL.
110 virtual void recordProgramInfo(const GrProgramInfo*) {}
111 // This asks the recording context to return any programInfos it may have collected
112 // via the 'recordProgramInfo' call. It is up to the caller to ensure that the lifetime
113 // of the programInfos matches the intended use. For example, in DDL-record mode it
114 // is known that all the programInfos will have been allocated in an arena with the
115 // same lifetime at the DDL itself.
116 virtual void detachProgramData(SkTArray<ProgramData>*) {}
117
118 GrTextBlobCache* getTextBlobCache();
119 const GrTextBlobCache* getTextBlobCache() const;
120
121 /**
122 * Registers an object for flush-related callbacks. (See GrOnFlushCallbackObject.)
123 *
124 * NOTE: the drawing manager tracks this object as a raw pointer; it is up to the caller to
125 * ensure its lifetime is tied to that of the context.
126 */
127 void addOnFlushCallbackObject(GrOnFlushCallbackObject*);
128
129 GrAuditTrail* auditTrail() { return fAuditTrail.get(); }
130
131 GrRecordingContext* asRecordingContext() override { return this; }
132
133private:
134 OwnedArenas fArenas;
135
136 std::unique_ptr<GrDrawingManager> fDrawingManager;
137
138 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
139
140 std::unique_ptr<GrAuditTrail> fAuditTrail;
141
142#ifdef GR_TEST_UTILS
143 int fSuppressWarningMessages = 0;
144#endif
145
146 typedef GrImageContext INHERITED;
147};
148
149#endif
150