blob: 8a92f8dedb87e535a1a0af6a811ec1699899aa54 [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 Phillipsa0bc39d2019-01-29 13:14:47 -050022class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040023class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050024
25/**
26 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
27 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
28 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040029class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050030public:
Adlai Hollere219d1c2020-06-02 11:23:16 -040031 ~GrContextThreadSafeProxy();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050032
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050033 /**
34 * Create a surface characterization for a DDL that will be replayed into the GrContext
35 * that created this proxy. On failure the resulting characterization will be invalid (i.e.,
36 * "!c.isValid()").
37 *
38 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect when the
39 * DDL created with this characterization is replayed.
40 * Note: the contract here is that the DDL will be created as
41 * if it had a full 'cacheMaxResourceBytes' to use. If replayed
42 * into a GrContext that already has locked GPU memory, the
43 * replay can exceed the budget. To rephrase, all resource
44 * allocation decisions are made at record time and at playback
45 * time the budget limits will be ignored.
46 * @param ii The image info specifying properties of the SkSurface that
47 * the DDL created with this characterization will be replayed
48 * into.
49 * Note: Ganesh doesn't make use of the SkImageInfo's alphaType
50 * @param backendFormat Information about the format of the GPU surface that will
51 * back the SkSurface upon replay
52 * @param sampleCount The sample count of the SkSurface that the DDL created with
53 * this characterization will be replayed into
54 * @param origin The origin of the SkSurface that the DDL created with this
55 * characterization will be replayed into
56 * @param surfaceProps The surface properties of the SkSurface that the DDL created
57 * with this characterization will be replayed into
58 * @param isMipMapped Will the surface the DDL will be replayed into have space
59 * allocated for mipmaps?
60 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be backed by GL
61 * FBO 0. This flag is only valid if using an GL backend.
Robert Phillipsb45f47d2019-02-03 17:17:54 -050062 * @param isTextureable Will the surface be able to act as a texture?
Robert Phillips3cd54322019-07-10 09:28:59 -040063 * @param isProtected Will the (Vulkan) surface be DRM protected?
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050064 */
65 SkSurfaceCharacterization createCharacterization(
66 size_t cacheMaxResourceBytes,
67 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
68 int sampleCount, GrSurfaceOrigin origin,
69 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050070 bool isMipMapped,
71 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040072 bool isTextureable = true,
73 GrProtected isProtected = GrProtected::kNo);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050074
Robert Phillipsf7599642019-08-13 16:16:42 -040075 /*
76 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
77 * It is guaranteed that this backend format will be the one used by the following
78 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
79 *
80 * The caller should check that the returned format is valid.
81 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040082 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
83
84 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -040085
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050086 bool operator==(const GrContextThreadSafeProxy& that) const {
87 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -040088 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050089 return this == &that;
90 }
91
92 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
93
94 // Provides access to functions that aren't part of the public API.
95 GrContextThreadSafeProxyPriv priv();
96 const GrContextThreadSafeProxyPriv priv() const;
97
98private:
Robert Phillipsbb606772019-02-04 17:50:57 -050099 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
100
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500101 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400102 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500103
Adlai Holler43b15792020-06-01 10:11:49 -0400104 void abandonContext();
105 bool abandoned() const;
106
Adlai Hollere219d1c2020-06-02 11:23:16 -0400107 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
108 // with GrContext where we get the caps by creating a GPU which requires a context (see the
109 // `init` method on GrContext_Base).
110 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400111
Herb Derbybc4d13a2020-06-25 15:35:23 -0400112 const GrBackendApi fBackend;
113 const GrContextOptions fOptions;
114 const uint32_t fContextID;
115 sk_sp<const GrCaps> fCaps;
116 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
117 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500118};
119
120#endif