blob: ccfbefd05d8b0b8b86be03965472648b4d51c694 [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/SkRefCnt.h"
Adlai Hollerc44e21f2021-02-22 14:08:07 -050012
13#if SK_SUPPORT_GPU
14
15#include "include/core/SkImageInfo.h"
Adlai Hollere219d1c2020-06-02 11:23:16 -040016#include "include/gpu/GrContextOptions.h"
17#include "include/gpu/GrTypes.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050018
Adlai Holler43b15792020-06-01 10:11:49 -040019#include <atomic>
20
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050021class GrBackendFormat;
Adlai Hollere219d1c2020-06-02 11:23:16 -040022class GrCaps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050023class GrContextThreadSafeProxyPriv;
Herb Derbybc4d13a2020-06-25 15:35:23 -040024class GrTextBlobCache;
Robert Phillipsd464feb2020-10-08 11:00:02 -040025class GrThreadSafeCache;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050026class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040027class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050028
29/**
30 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
31 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
32 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040033class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050034public:
Adlai Hollere219d1c2020-06-02 11:23:16 -040035 ~GrContextThreadSafeProxy();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050036
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050037 /**
38 * Create a surface characterization for a DDL that will be replayed into the GrContext
39 * that created this proxy. On failure the resulting characterization will be invalid (i.e.,
40 * "!c.isValid()").
41 *
Greg Daniel007d97d2020-11-04 10:50:39 -050042 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect
43 * when the DDL created with this characterization is
44 * replayed.
45 * Note: the contract here is that the DDL will be
46 * created as if it had a full 'cacheMaxResourceBytes'
47 * to use. If replayed into a GrContext that already has
48 * locked GPU memory, the replay can exceed the budget.
49 * To rephrase, all resource allocation decisions are
50 * made at record time and at playback time the budget
51 * limits will be ignored.
52 * @param ii The image info specifying properties of the SkSurface
53 * that the DDL created with this characterization will
54 * be replayed into.
55 * Note: Ganesh doesn't make use of the SkImageInfo's
56 * alphaType
57 * @param backendFormat Information about the format of the GPU surface that
58 * will back the SkSurface upon replay
59 * @param sampleCount The sample count of the SkSurface that the DDL
60 * created with this characterization will be replayed
61 * into
62 * @param origin The origin of the SkSurface that the DDL created with
63 * this characterization will be replayed into
64 * @param surfaceProps The surface properties of the SkSurface that the DDL
Greg Daniel1448eb92020-11-06 10:29:57 -050065 * created with this characterization will be replayed
66 * into
Greg Daniel007d97d2020-11-04 10:50:39 -050067 * @param isMipMapped Will the surface the DDL will be replayed into have
68 * space allocated for mipmaps?
69 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be
70 * backed by GL FBO 0. This flag is only valid if using
71 * an GL backend.
72 * @param isTextureable Will the surface be able to act as a texture?
73 * @param isProtected Will the (Vulkan) surface be DRM protected?
74 * @param vkRTSupportsInputAttachment Can the vulkan surface be used as in input
75 attachment?
76 * @param forVulkanSecondaryCommandBuffer Will the surface be wrapping a vulkan secondary
77 * command buffer via a GrVkSecondaryCBDrawContext? If
78 * this is true then the following is required:
79 * isTexureable = false
80 * isMipMapped = false
81 * willUseGLFBO0 = false
82 * vkRTSupportsInputAttachment = false
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050083 */
84 SkSurfaceCharacterization createCharacterization(
85 size_t cacheMaxResourceBytes,
Robert Phillipsb25e0652020-07-22 09:35:49 -040086 const SkImageInfo& ii,
87 const GrBackendFormat& backendFormat,
88 int sampleCount,
89 GrSurfaceOrigin origin,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050090 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050091 bool isMipMapped,
92 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040093 bool isTextureable = true,
Greg Daniel638b2e82020-08-27 14:29:00 -040094 GrProtected isProtected = GrProtected::kNo,
Greg Daniel007d97d2020-11-04 10:50:39 -050095 bool vkRTSupportsInputAttachment = false,
96 bool forVulkanSecondaryCommandBuffer = false);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050097
Robert Phillipsf7599642019-08-13 16:16:42 -040098 /*
99 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
100 * It is guaranteed that this backend format will be the one used by the following
101 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
102 *
103 * The caller should check that the returned format is valid.
104 */
Adlai Hollere219d1c2020-06-02 11:23:16 -0400105 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
106
107 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -0400108
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500109 bool operator==(const GrContextThreadSafeProxy& that) const {
110 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -0400111 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500112 return this == &that;
113 }
114
115 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
116
117 // Provides access to functions that aren't part of the public API.
118 GrContextThreadSafeProxyPriv priv();
John Stilesec9b4aa2020-08-07 13:05:14 -0400119 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500120
121private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500122 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
123
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500124 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400125 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500126
Adlai Holler43b15792020-06-01 10:11:49 -0400127 void abandonContext();
128 bool abandoned() const;
129
Adlai Hollere219d1c2020-06-02 11:23:16 -0400130 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
131 // with GrContext where we get the caps by creating a GPU which requires a context (see the
132 // `init` method on GrContext_Base).
133 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400134
Robert Phillipsd464feb2020-10-08 11:00:02 -0400135 const GrBackendApi fBackend;
136 const GrContextOptions fOptions;
137 const uint32_t fContextID;
138 sk_sp<const GrCaps> fCaps;
139 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
140 std::unique_ptr<GrThreadSafeCache> fThreadSafeCache;
141 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500142};
143
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500144#else // !SK_SUPPORT_GPU
145class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {};
146#endif
147
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500148#endif