1/*
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
11#include "include/core/SkImageInfo.h"
12#include "include/core/SkRefCnt.h"
13#include "include/gpu/GrContextOptions.h"
14#include "include/gpu/GrTypes.h"
15
16#include <atomic>
17
18class GrBackendFormat;
19class GrCaps;
20class GrContextThreadSafeProxyPriv;
21class GrTextBlobCache;
22class SkSurfaceCharacterization;
23class SkSurfaceProps;
24
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 */
29class SK_API GrContextThreadSafeProxy final : public SkNVRefCnt<GrContextThreadSafeProxy> {
30public:
31 ~GrContextThreadSafeProxy();
32
33 /**
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.
62 * @param isTextureable Will the surface be able to act as a texture?
63 * @param isProtected Will the (Vulkan) surface be DRM protected?
64 */
65 SkSurfaceCharacterization createCharacterization(
66 size_t cacheMaxResourceBytes,
67 const SkImageInfo& ii,
68 const GrBackendFormat& backendFormat,
69 int sampleCount,
70 GrSurfaceOrigin origin,
71 const SkSurfaceProps& surfaceProps,
72 bool isMipMapped,
73 bool willUseGLFBO0 = false,
74 bool isTextureable = true,
75 GrProtected isProtected = GrProtected::kNo);
76
77 /*
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 */
84 GrBackendFormat defaultBackendFormat(SkColorType ct, GrRenderable renderable) const;
85
86 bool isValid() const { return nullptr != fCaps; }
87
88 bool operator==(const GrContextThreadSafeProxy& that) const {
89 // Each GrContext should only ever have a single thread-safe proxy.
90 SkASSERT((this == &that) == (this->fContextID == that.fContextID));
91 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();
98 const GrContextThreadSafeProxyPriv priv() const; // NOLINT(readability-const-return-type)
99
100private:
101 friend class GrContextThreadSafeProxyPriv; // for ctor and hidden methods
102
103 // DDL TODO: need to add unit tests for backend & maybe options
104 GrContextThreadSafeProxy(GrBackendApi, const GrContextOptions&);
105
106 void abandonContext();
107 bool abandoned() const;
108
109 // 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>);
113
114 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};
120};
121
122#endif
123