blob: 4058ae5164bda465810e0f3decbade043933e2ac [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrContext_Base.h"
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050012
Adlai Holler43b15792020-06-01 10:11:49 -040013#include <atomic>
14
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050015class GrBackendFormat;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050016class GrContextThreadSafeProxyPriv;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050017struct SkImageInfo;
18class SkSurfaceCharacterization;
Brian Osman01e6d172020-03-30 15:57:14 -040019class SkSurfaceProps;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050020
21/**
22 * Can be used to perform actions related to the generating GrContext in a thread safe manner. The
23 * proxy does not access the 3D API (e.g. OpenGL) that backs the generating GrContext.
Adlai Holler43b15792020-06-01 10:11:49 -040024 *
25 * TODO: Once the guts of GrContext_Base are moved in here, this probably shouldn't derive
26 * from GrContext_Base since it isn't actually a context.
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050027 */
Robert Phillipsc1541ae2019-02-04 12:05:37 -050028class SK_API GrContextThreadSafeProxy : public GrContext_Base {
Robert Phillipsa0bc39d2019-01-29 13:14:47 -050029public:
30 ~GrContextThreadSafeProxy() override;
31
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 */
81 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const {
82 return INHERITED::defaultBackendFormat(ct, renderable);
83 }
84
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.
Robert Phillipsc1541ae2019-02-04 12:05:37 -050087 SkASSERT((this == &that) == (this->contextID() == that.contextID()));
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
Adlai Holler43b15792020-06-01 10:11:49 -040097protected:
98 // TODO: remove this once this class isn't derived from GrContext_Base
99 GrContextThreadSafeProxy* asThreadSafeProxy() override { return this; }
100
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500101private:
Robert Phillipsbb606772019-02-04 17:50:57 -0500102 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
103
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500104 // DDL TODO: need to add unit tests for backend & maybe options
Robert Phillipsbb606772019-02-04 17:50:57 -0500105 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&, uint32_t contextID);
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500106
Brian Osman7b1678a2019-12-16 09:17:25 -0500107 bool init(sk_sp<const GrCaps>) override;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500108
Adlai Holler43b15792020-06-01 10:11:49 -0400109 void abandonContext();
110 bool abandoned() const;
111
112 std::atomic<bool> fAbandoned{false};
113
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500114 typedef GrContext_Base INHERITED;
Robert Phillipsa0bc39d2019-01-29 13:14:47 -0500115};
116
117#endif