Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 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 GrContextThreadSafeProxy_DEFINED |
| 9 | #define GrContextThreadSafeProxy_DEFINED |
| 10 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/gpu/GrContextOptions.h" |
| 14 | #include "include/gpu/GrTypes.h" |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 15 | |
Adlai Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 16 | #include <atomic> |
| 17 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 18 | class GrBackendFormat; |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 19 | class GrCaps; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 20 | class GrContextThreadSafeProxyPriv; |
Herb Derby | bc4d13a | 2020-06-25 15:35:23 -0400 | [diff] [blame] | 21 | class GrTextBlobCache; |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 22 | class GrThreadSafeCache; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 23 | class SkSurfaceCharacterization; |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 24 | class SkSurfaceProps; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 25 | |
| 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 Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 30 | class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> { |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 31 | public: |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 32 | ~GrContextThreadSafeProxy(); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 33 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 34 | /** |
| 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 Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 39 | * @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 |
Greg Daniel | 1448eb9 | 2020-11-06 10:29:57 -0500 | [diff] [blame^] | 62 | * created with this characterization will be replayed |
| 63 | * into |
Greg Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 64 | * @param isMipMapped Will the surface the DDL will be replayed into have |
| 65 | * space allocated for mipmaps? |
| 66 | * @param willUseGLFBO0 Will the surface the DDL will be replayed into be |
| 67 | * backed by GL FBO 0. This flag is only valid if using |
| 68 | * an GL backend. |
| 69 | * @param isTextureable Will the surface be able to act as a texture? |
| 70 | * @param isProtected Will the (Vulkan) surface be DRM protected? |
| 71 | * @param vkRTSupportsInputAttachment Can the vulkan surface be used as in input |
| 72 | attachment? |
| 73 | * @param forVulkanSecondaryCommandBuffer Will the surface be wrapping a vulkan secondary |
| 74 | * command buffer via a GrVkSecondaryCBDrawContext? If |
| 75 | * this is true then the following is required: |
| 76 | * isTexureable = false |
| 77 | * isMipMapped = false |
| 78 | * willUseGLFBO0 = false |
| 79 | * vkRTSupportsInputAttachment = false |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 80 | */ |
| 81 | SkSurfaceCharacterization createCharacterization( |
| 82 | size_t cacheMaxResourceBytes, |
Robert Phillips | b25e065 | 2020-07-22 09:35:49 -0400 | [diff] [blame] | 83 | const SkImageInfo& ii, |
| 84 | const GrBackendFormat& backendFormat, |
| 85 | int sampleCount, |
| 86 | GrSurfaceOrigin origin, |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 87 | const SkSurfaceProps& surfaceProps, |
Robert Phillips | b45f47d | 2019-02-03 17:17:54 -0500 | [diff] [blame] | 88 | bool isMipMapped, |
| 89 | bool willUseGLFBO0 = false, |
Robert Phillips | 3cd5432 | 2019-07-10 09:28:59 -0400 | [diff] [blame] | 90 | bool isTextureable = true, |
Greg Daniel | 638b2e8 | 2020-08-27 14:29:00 -0400 | [diff] [blame] | 91 | GrProtected isProtected = GrProtected::kNo, |
Greg Daniel | 007d97d | 2020-11-04 10:50:39 -0500 | [diff] [blame] | 92 | bool vkRTSupportsInputAttachment = false, |
| 93 | bool forVulkanSecondaryCommandBuffer = false); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 94 | |
Robert Phillips | f759964 | 2019-08-13 16:16:42 -0400 | [diff] [blame] | 95 | /* |
| 96 | * Retrieve the default GrBackendFormat for a given SkColorType and renderability. |
| 97 | * It is guaranteed that this backend format will be the one used by the following |
| 98 | * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods. |
| 99 | * |
| 100 | * The caller should check that the returned format is valid. |
| 101 | */ |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 102 | GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const; |
| 103 | |
| 104 | bool isValid() const { return nullptr != fCaps; } |
Robert Phillips | f759964 | 2019-08-13 16:16:42 -0400 | [diff] [blame] | 105 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 106 | bool operator==(const GrContextThreadSafeProxy& that) const { |
| 107 | // Each GrContext should only ever have a single thread-safe proxy. |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 108 | SkASSERT((this == &that) == (this->fContextID == that.fContextID)); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 109 | return this == &that; |
| 110 | } |
| 111 | |
| 112 | bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); } |
| 113 | |
| 114 | // Provides access to functions that aren't part of the public API. |
| 115 | GrContextThreadSafeProxyPriv priv(); |
John Stiles | ec9b4aa | 2020-08-07 13:05:14 -0400 | [diff] [blame] | 116 | const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type) |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 117 | |
| 118 | private: |
Robert Phillips | bb60677 | 2019-02-04 17:50:57 -0500 | [diff] [blame] | 119 | friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods |
| 120 | |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 121 | // DDL TODO: need to add unit tests for backend & maybe options |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 122 | GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&); |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 123 | |
Adlai Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 124 | void abandonContext(); |
| 125 | bool abandoned() const; |
| 126 | |
Adlai Holler | e219d1c | 2020-06-02 11:23:16 -0400 | [diff] [blame] | 127 | // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem |
| 128 | // with GrContext where we get the caps by creating a GPU which requires a context (see the |
| 129 | // `init` method on GrContext_Base). |
| 130 | void init(sk_sp<const GrCaps>); |
Adlai Holler | 43b1579 | 2020-06-01 10:11:49 -0400 | [diff] [blame] | 131 | |
Robert Phillips | d464feb | 2020-10-08 11:00:02 -0400 | [diff] [blame] | 132 | const GrBackendApi fBackend; |
| 133 | const GrContextOptions fOptions; |
| 134 | const uint32_t fContextID; |
| 135 | sk_sp<const GrCaps> fCaps; |
| 136 | std::unique_ptr<GrTextBlobCache> fTextBlobCache; |
| 137 | std::unique_ptr<GrThreadSafeCache> fThreadSafeCache; |
| 138 | std::atomic<bool> fAbandoned{false}; |
Robert Phillips | a0bc39d | 2019-01-29 13:14:47 -0500 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #endif |