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 GrContext_Base_DEFINED
9#define GrContext_Base_DEFINED
10
11#include "include/core/SkRefCnt.h"
12#include "include/gpu/GrBackendSurface.h"
13#include "include/gpu/GrContextOptions.h"
14#include "include/gpu/GrTypes.h"
15
16class GrBaseContextPriv;
17class GrCaps;
18class GrContextThreadSafeProxy;
19class GrDirectContext;
20class GrImageContext;
21class GrRecordingContext;
22
23class SK_API GrContext_Base : public SkRefCnt {
24public:
25    ~GrContext_Base() override;
26
27    /*
28     * Safely downcast to a GrDirectContext.
29     */
30    virtual GrDirectContext* asDirectContext() { return nullptr; }
31
32    /*
33     * The 3D API backing this context
34     */
35    SK_API GrBackendApi backend() const;
36
37    /*
38     * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
39     * It is guaranteed that this backend format will be the one used by the GrContext
40     * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
41     *
42     * The caller should check that the returned format is valid.
43     */
44    SK_API GrBackendFormat defaultBackendFormat(SkColorType, GrRenderable) const;
45
46    SK_API GrBackendFormat compressedBackendFormat(SkImage::CompressionType) const;
47
48    // TODO: When the public version is gone, rename to refThreadSafeProxy and add raw ptr ver.
49    sk_sp<GrContextThreadSafeProxy> threadSafeProxy();
50
51    // Provides access to functions that aren't part of the public API.
52    GrBaseContextPriv priv();
53    const GrBaseContextPriv priv() const;  // NOLINT(readability-const-return-type)
54
55    // Advanced Filter: Record process name, so that we can get it in other places easily.
56    const std::string& getProcessName() const;
57
58protected:
59    friend class GrBaseContextPriv; // for hidden functions
60
61    GrContext_Base(sk_sp<GrContextThreadSafeProxy>);
62
63    virtual bool init();
64
65    /**
66     * An identifier for this context. The id is used by all compatible contexts. For example,
67     * if SkImages are created on one thread using an image creation context, then fed into a
68     * DDL Recorder on second thread (which has a recording context) and finally replayed on
69     * a third thread with a direct context, then all three contexts will report the same id.
70     * It is an error for an image to be used with contexts that report different ids.
71     */
72    uint32_t contextID() const;
73
74    bool matches(GrContext_Base* candidate) const {
75        return candidate && candidate->contextID() == this->contextID();
76    }
77
78    /*
79     * The options in effect for this context
80     */
81    const GrContextOptions& options() const;
82
83    const GrCaps* caps() const;
84    sk_sp<const GrCaps> refCaps() const;
85
86    virtual GrImageContext* asImageContext() { return nullptr; }
87    virtual GrRecordingContext* asRecordingContext() { return nullptr; }
88
89    sk_sp<GrContextThreadSafeProxy>         fThreadSafeProxy;
90
91private:
92    using INHERITED = SkRefCnt;
93};
94
95#endif
96