blob: 00b111ee0f5d1ffbcba521b35d9f0a5d23742e55 [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 *
39 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect when the
40 * DDL created with this characterization is replayed.
41 * Note: the contract here is that the DDL will be created as
42 * if it had a full 'cacheMaxResourceBytes' to use. If replayed
43 * into a GrContext that already has locked GPU memory, the
44 * replay can exceed the budget. To rephrase, all resource
45 * allocation decisions are made at record time and at playback
46 * time the budget limits will be ignored.
47 * @param ii The image info specifying properties of the SkSurface that
48 * the DDL created with this characterization will be replayed
49 * into.
50 * Note: Ganesh doesn't make use of the SkImageInfo's alphaType
51 * @param backendFormat Information about the format of the GPU surface that will
52 * back the SkSurface upon replay
53 * @param sampleCount The sample count of the SkSurface that the DDL created with
54 * this characterization will be replayed into
55 * @param origin The origin of the SkSurface that the DDL created with this
56 * characterization will be replayed into
57 * @param surfaceProps The surface properties of the SkSurface that the DDL created
58 * with this characterization will be replayed into
59 * @param isMipMapped Will the surface the DDL will be replayed into have space
60 * allocated for mipmaps?
61 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be backed by GL
62 * FBO 0. This flag is only valid if using an GL backend.
Robert Phillipsb45f47d2019-02-03 17:17:54 -050063 * @param isTextureable Will the surface be able to act as a texture?
Robert Phillips3cd54322019-07-10 09:28:59 -040064 * @param isProtected Will the (Vulkan) surface be DRM protected?
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050065 */
66 SkSurfaceCharacterization createCharacterization(
67 size_t cacheMaxResourceBytes,
Robert Phillipsb25e0652020-07-22 09:35:49 -040068 const SkImageInfo& ii,
69 const GrBackendFormat& backendFormat,
70 int sampleCount,
71 GrSurfaceOrigin origin,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050072 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050073 bool isMipMapped,
74 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040075 bool isTextureable = true,
Greg Daniel638b2e82020-08-27 14:29:00 -040076 GrProtected isProtected = GrProtected::kNo,
77 bool vkRTSupportsInputAttachment = false);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050078
Robert Phillipsf7599642019-08-13 16:16:42 -040079 /*
80 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
81 * It is guaranteed that this backend format will be the one used by the following
82 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
83 *
84 * The caller should check that the returned format is valid.
85 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040086 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
87
88 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -040089
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050090 bool operator==(const GrContextThreadSafeProxy& that) const {
91 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -040092 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050093 return this == &that;
94 }
95
96 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
97
98 // Provides access to functions that aren't part of the public API.
99 GrContextThreadSafeProxyPriv priv();
John Stilesec9b4aa2020-08-07 13:05:14 -0400100 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500101
102private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500103 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
104
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500105 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400106 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500107
Adlai Holler43b15792020-06-01 10:11:49 -0400108 void abandonContext();
109 bool abandoned() const;
110
Adlai Hollere219d1c2020-06-02 11:23:16 -0400111 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
112 // with GrContext where we get the caps by creating a GPU which requires a context (see the
113 // `init` method on GrContext_Base).
114 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400115
Robert Phillipsd464feb2020-10-08 11:00:02 -0400116 const GrBackendApi fBackend;
117 const GrContextOptions fOptions;
118 const uint32_t fContextID;
119 sk_sp<const GrCaps> fCaps;
120 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
121 std::unique_ptr<GrThreadSafeCache> fThreadSafeCache;
122 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500123};
124
125#endif