blob: f645b8793b50477740dd50c404440d48cdadcec7 [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 Phillipsae67c522021-03-03 11:03:38 -050026class GrThreadSafePipelineBuilder;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050027class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040028class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050029
30/**
31 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
32 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
33 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040034class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050035public:
Adlai Hollere219d1c2020-06-02 11:23:16 -040036 ~GrContextThreadSafeProxy();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050037
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050038 /**
39 * Create a surface characterization for a DDL that will be replayed into the GrContext
40 * that created this proxy. On failure the resulting characterization will be invalid (i.e.,
41 * "!c.isValid()").
42 *
Greg Daniel007d97d2020-11-04 10:50:39 -050043 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect
44 * when the DDL created with this characterization is
45 * replayed.
46 * Note: the contract here is that the DDL will be
47 * created as if it had a full 'cacheMaxResourceBytes'
48 * to use. If replayed into a GrContext that already has
49 * locked GPU memory, the replay can exceed the budget.
50 * To rephrase, all resource allocation decisions are
51 * made at record time and at playback time the budget
52 * limits will be ignored.
53 * @param ii The image info specifying properties of the SkSurface
54 * that the DDL created with this characterization will
55 * be replayed into.
56 * Note: Ganesh doesn't make use of the SkImageInfo's
57 * alphaType
58 * @param backendFormat Information about the format of the GPU surface that
59 * will back the SkSurface upon replay
60 * @param sampleCount The sample count of the SkSurface that the DDL
61 * created with this characterization will be replayed
62 * into
63 * @param origin The origin of the SkSurface that the DDL created with
64 * this characterization will be replayed into
65 * @param surfaceProps The surface properties of the SkSurface that the DDL
Greg Daniel1448eb92020-11-06 10:29:57 -050066 * created with this characterization will be replayed
67 * into
Greg Daniel007d97d2020-11-04 10:50:39 -050068 * @param isMipMapped Will the surface the DDL will be replayed into have
69 * space allocated for mipmaps?
70 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be
71 * backed by GL FBO 0. This flag is only valid if using
72 * an GL backend.
73 * @param isTextureable Will the surface be able to act as a texture?
74 * @param isProtected Will the (Vulkan) surface be DRM protected?
75 * @param vkRTSupportsInputAttachment Can the vulkan surface be used as in input
76 attachment?
77 * @param forVulkanSecondaryCommandBuffer Will the surface be wrapping a vulkan secondary
78 * command buffer via a GrVkSecondaryCBDrawContext? If
79 * this is true then the following is required:
80 * isTexureable = false
81 * isMipMapped = false
82 * willUseGLFBO0 = false
83 * vkRTSupportsInputAttachment = false
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050084 */
85 SkSurfaceCharacterization createCharacterization(
86 size_t cacheMaxResourceBytes,
Robert Phillipsb25e0652020-07-22 09:35:49 -040087 const SkImageInfo& ii,
88 const GrBackendFormat& backendFormat,
89 int sampleCount,
90 GrSurfaceOrigin origin,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050091 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050092 bool isMipMapped,
93 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040094 bool isTextureable = true,
Greg Daniel638b2e82020-08-27 14:29:00 -040095 GrProtected isProtected = GrProtected::kNo,
Greg Daniel007d97d2020-11-04 10:50:39 -050096 bool vkRTSupportsInputAttachment = false,
97 bool forVulkanSecondaryCommandBuffer = false);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050098
Robert Phillipsf7599642019-08-13 16:16:42 -040099 /*
100 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
101 * It is guaranteed that this backend format will be the one used by the following
102 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
103 *
104 * The caller should check that the returned format is valid.
105 */
Adlai Hollere219d1c2020-06-02 11:23:16 -0400106 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
107
108 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -0400109
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500110 bool operator==(const GrContextThreadSafeProxy& that) const {
111 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -0400112 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500113 return this == &that;
114 }
115
116 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
117
118 // Provides access to functions that aren't part of the public API.
119 GrContextThreadSafeProxyPriv priv();
John Stilesec9b4aa2020-08-07 13:05:14 -0400120 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500121
122private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500123 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
124
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500125 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400126 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500127
Adlai Holler43b15792020-06-01 10:11:49 -0400128 void abandonContext();
129 bool abandoned() const;
130
Adlai Hollere219d1c2020-06-02 11:23:16 -0400131 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
132 // with GrContext where we get the caps by creating a GPU which requires a context (see the
133 // `init` method on GrContext_Base).
Robert Phillipsae67c522021-03-03 11:03:38 -0500134 void init(sk_sp<const GrCaps>, sk_sp<GrThreadSafePipelineBuilder>);
Adlai Holler43b15792020-06-01 10:11:49 -0400135
Robert Phillipsae67c522021-03-03 11:03:38 -0500136 const GrBackendApi fBackend;
137 const GrContextOptions fOptions;
138 const uint32_t fContextID;
139 sk_sp<const GrCaps> fCaps;
140 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
141 std::unique_ptr<GrThreadSafeCache> fThreadSafeCache;
142 sk_sp<GrThreadSafePipelineBuilder> fPipelineBuilder;
143 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500144};
145
Adlai Hollerc44e21f2021-02-22 14:08:07 -0500146#else // !SK_SUPPORT_GPU
147class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {};
148#endif
149
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500150#endif