blob: 97e72566901ad2fdb97ee1a74fbf1e3cbbd66546 [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,
Robert Phillipsb25e0652020-07-22 09:35:49 -040067 const SkImageInfo& ii,
68 const GrBackendFormat& backendFormat,
69 int sampleCount,
70 GrSurfaceOrigin origin,
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050071 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050072 bool isMipMapped,
73 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040074 bool isTextureable = true,
75 GrProtected isProtected = GrProtected::kNo);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050076
Robert Phillipsf7599642019-08-13 16:16:42 -040077 /*
78 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
79 * It is guaranteed that this backend format will be the one used by the following
80 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
81 *
82 * The caller should check that the returned format is valid.
83 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040084 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
85
86 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -040087
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050088 bool operator==(const GrContextThreadSafeProxy& that) const {
89 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -040090 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050091 return this == &that;
92 }
93
94 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
95
96 // Provides access to functions that aren't part of the public API.
97 GrContextThreadSafeProxyPriv priv();
John Stilesec9b4aa2020-08-07 13:05:14 -040098 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050099
100private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500101 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
102
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500103 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400104 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500105
Adlai Holler43b15792020-06-01 10:11:49 -0400106 void abandonContext();
107 bool abandoned() const;
108
Adlai Hollere219d1c2020-06-02 11:23:16 -0400109 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
110 // with GrContext where we get the caps by creating a GPU which requires a context (see the
111 // `init` method on GrContext_Base).
112 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400113
Herb Derbybc4d13a2020-06-25 15:35:23 -0400114 const GrBackendApi fBackend;
115 const GrContextOptions fOptions;
116 const uint32_t fContextID;
117 sk_sp<const GrCaps> fCaps;
118 std::unique_ptr<GrTextBlobCache> fTextBlobCache;
119 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500120};
121
122#endif