blob: c00d9d867a30e969fd22f497857945b631ac8c05 [file] [log] [blame]
Robert Phillipsa0bc39d2019-01-29 13:14:47 -05001/*
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 GrContextThreadSafeProxy_DEFINED
9#define GrContextThreadSafeProxy_DEFINED
10
Adlai Hollere219d1c2020-06-02 11:23:16 -040011#include "include/core/SkImageInfo.h"
12#include "include/core/SkRefCnt.h"
13#include "include/gpu/GrContextOptions.h"
14#include "include/gpu/GrTypes.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050015
Adlai Holler43b15792020-06-01 10:11:49 -040016#include <atomic>
17
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050018class GrBackendFormat;
Adlai Hollere219d1c2020-06-02 11:23:16 -040019class GrCaps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050020class GrContextThreadSafeProxyPriv;
Herb Derbybc4d13a2020-06-25 15:35:23 -040021class GrTextBlobCache;
Robert Phillipsd464feb2020-10-08 11:00:02 -040022class GrThreadSafeCache;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050023class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040024class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050025
26/**
27 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
28 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
29 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040030class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050031public:
Adlai Hollere219d1c2020-06-02 11:23:16 -040032 ~GrContextThreadSafeProxy();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050033
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050034 /**
35 * Create a surface characterization for a DDL that will be replayed into the GrContext
36 * that created this proxy. On failure the resulting characterization will be invalid (i.e.,
37 * "!c.isValid()").
38 *
Greg Daniel007d97d2020-11-04 10:50:39 -050039 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect
40 * when the DDL created with this characterization is
41 * replayed.
42 * Note: the contract here is that the DDL will be
43 * created as if it had a full 'cacheMaxResourceBytes'
44 * to use. If replayed into a GrContext that already has
45 * locked GPU memory, the replay can exceed the budget.
46 * To rephrase, all resource allocation decisions are
47 * made at record time and at playback time the budget
48 * limits will be ignored.
49 * @param ii The image info specifying properties of the SkSurface
50 * that the DDL created with this characterization will
51 * be replayed into.
52 * Note: Ganesh doesn't make use of the SkImageInfo's
53 * alphaType
54 * @param backendFormat Information about the format of the GPU surface that
55 * will back the SkSurface upon replay
56 * @param sampleCount The sample count of the SkSurface that the DDL
57 * created with this characterization will be replayed
58 * into
59 * @param origin The origin of the SkSurface that the DDL created with
60 * this characterization will be replayed into
61 * @param surfaceProps The surface properties of the SkSurface that the DDL
62 * created with this characterization will be replayed into
63 * @param isMipMapped Will the surface the DDL will be replayed into have
64 * space allocated for mipmaps?
65 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be
66 * backed by GL FBO 0. This flag is only valid if using
67 * an GL backend.
68 * @param isTextureable Will the surface be able to act as a texture?
69 * @param isProtected Will the (Vulkan) surface be DRM protected?
70 * @param vkRTSupportsInputAttachment Can the vulkan surface be used as in input
71 attachment?
72 * @param forVulkanSecondaryCommandBuffer Will the surface be wrapping a vulkan secondary
73 * command buffer via a GrVkSecondaryCBDrawContext? If
74 * this is true then the following is required:
75 * isTexureable = false
76 * isMipMapped = false
77 * willUseGLFBO0 = false
78 * vkRTSupportsInputAttachment = false
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050079 */
80 SkSurfaceCharacterization createCharacterization(
81 size_t cacheMaxResourceBytes,
Robert Phillipsb25e0652020-07-22 09:35:49 -040082 const SkImageInfo& ii,
83 const GrBackendFormat& backendFormat,
84 int sampleCount,
85 GrSurfaceOrigin origin,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050086 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050087 bool isMipMapped,
88 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040089 bool isTextureable = true,
Greg Daniel638b2e82020-08-27 14:29:00 -040090 GrProtected isProtected = GrProtected::kNo,
Greg Daniel007d97d2020-11-04 10:50:39 -050091 bool vkRTSupportsInputAttachment = false,
92 bool forVulkanSecondaryCommandBuffer = false);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050093
Robert Phillipsf7599642019-08-13 16:16:42 -040094 /*
95 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
96 * It is guaranteed that this backend format will be the one used by the following
97 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
98 *
99 * The caller should check that the returned format is valid.
100 */
Adlai Hollere219d1c2020-06-02 11:23:16 -0400101 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
102
103 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -0400104
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500105 bool operator==(const GrContextThreadSafeProxy& that) const {
106 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -0400107 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500108 return this == &that;
109 }
110
111 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
112
113 // Provides access to functions that aren't part of the public API.
114 GrContextThreadSafeProxyPriv priv();
John Stilesec9b4aa2020-08-07 13:05:14 -0400115 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500116
117private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500118 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
119
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500120 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400121 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500122
Adlai Holler43b15792020-06-01 10:11:49 -0400123 void abandonContext();
124 bool abandoned() const;
125
Adlai Hollere219d1c2020-06-02 11:23:16 -0400126 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
127 // with GrContext where we get the caps by creating a GPU which requires a context (see the
128 // `init` method on GrContext_Base).
129 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400130
Robert Phillipsd464feb2020-10-08 11:00:02 -0400131 const GrBackendApi fBackend;
132 const GrContextOptions fOptions;
133 const uint32_t fContextID;
134 sk_sp<const GrCaps> fCaps;
135 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
136 std::unique_ptr<GrThreadSafeCache> fThreadSafeCache;
137 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500138};
139
140#endif