blob: ca4cb313dcd4be0e5d2d82a5002f309e41904954 [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;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050021class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040022class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050023
24/**
25 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
26 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
27 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040028class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050029public:
Adlai Hollere219d1c2020-06-02 11:23:16 -040030 ~GrContextThreadSafeProxy();
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050031
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050032 /**
33 * Create a surface characterization for a DDL that will be replayed into the GrContext
34 * that created this proxy. On failure the resulting characterization will be invalid (i.e.,
35 * "!c.isValid()").
36 *
37 * @param cacheMaxResourceBytes The max resource bytes limit that will be in effect when the
38 * DDL created with this characterization is replayed.
39 * Note: the contract here is that the DDL will be created as
40 * if it had a full 'cacheMaxResourceBytes' to use. If replayed
41 * into a GrContext that already has locked GPU memory, the
42 * replay can exceed the budget. To rephrase, all resource
43 * allocation decisions are made at record time and at playback
44 * time the budget limits will be ignored.
45 * @param ii The image info specifying properties of the SkSurface that
46 * the DDL created with this characterization will be replayed
47 * into.
48 * Note: Ganesh doesn't make use of the SkImageInfo's alphaType
49 * @param backendFormat Information about the format of the GPU surface that will
50 * back the SkSurface upon replay
51 * @param sampleCount The sample count of the SkSurface that the DDL created with
52 * this characterization will be replayed into
53 * @param origin The origin of the SkSurface that the DDL created with this
54 * characterization will be replayed into
55 * @param surfaceProps The surface properties of the SkSurface that the DDL created
56 * with this characterization will be replayed into
57 * @param isMipMapped Will the surface the DDL will be replayed into have space
58 * allocated for mipmaps?
59 * @param willUseGLFBO0 Will the surface the DDL will be replayed into be backed by GL
60 * FBO 0. This flag is only valid if using an GL backend.
Robert Phillipsb45f47d2019-02-03 17:17:54 -050061 * @param isTextureable Will the surface be able to act as a texture?
Robert Phillips3cd54322019-07-10 09:28:59 -040062 * @param isProtected Will the (Vulkan) surface be DRM protected?
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050063 */
64 SkSurfaceCharacterization createCharacterization(
65 size_t cacheMaxResourceBytes,
66 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
67 int sampleCount, GrSurfaceOrigin origin,
68 const SkSurfaceProps& surfaceProps,
Robert Phillipsb45f47d2019-02-03 17:17:54 -050069 bool isMipMapped,
70 bool willUseGLFBO0 = false,
Robert Phillips3cd54322019-07-10 09:28:59 -040071 bool isTextureable = true,
72 GrProtected isProtected = GrProtected::kNo);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050073
Robert Phillipsf7599642019-08-13 16:16:42 -040074 /*
75 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
76 * It is guaranteed that this backend format will be the one used by the following
77 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
78 *
79 * The caller should check that the returned format is valid.
80 */
Adlai Hollere219d1c2020-06-02 11:23:16 -040081 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
82
83 bool isValid() const { return nullptr != fCaps; }
Robert Phillipsf7599642019-08-13 16:16:42 -040084
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050085 bool operator==(const GrContextThreadSafeProxy& that) const {
86 // Each GrContext should only ever have a single thread-safe proxy.
Adlai Hollere219d1c2020-06-02 11:23:16 -040087 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050088 return this == &that;
89 }
90
91 bool operator!=(const GrContextThreadSafeProxy& that) const { return !(*this == that); }
92
93 // Provides access to functions that aren't part of the public API.
94 GrContextThreadSafeProxyPriv priv();
95 const GrContextThreadSafeProxyPriv priv() const;
96
97private:
Robert Phillipsbb606772019-02-04 17:50:57 -050098 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
99
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500100 // DDL TODO: need to add unit tests for backend & maybe options
Adlai Hollere219d1c2020-06-02 11:23:16 -0400101 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500102
Adlai Holler43b15792020-06-01 10:11:49 -0400103 void abandonContext();
104 bool abandoned() const;
105
Adlai Hollere219d1c2020-06-02 11:23:16 -0400106 // TODO: This should be part of the constructor but right now we have a chicken-and-egg problem
107 // with GrContext where we get the caps by creating a GPU which requires a context (see the
108 // `init` method on GrContext_Base).
109 void init(sk_sp<const GrCaps>);
Adlai Holler43b15792020-06-01 10:11:49 -0400110
Adlai Hollere219d1c2020-06-02 11:23:16 -0400111 const GrBackendApi fBackend;
112 const GrContextOptions fOptions;
113 const uint32_t fContextID;
114 sk_sp<const GrCaps> fCaps;
115 std::atomic<bool> fAbandoned{false};
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500116};
117
118#endif