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 
16 class GrBaseContextPriv;
17 class GrCaps;
18 class GrContextThreadSafeProxy;
19 class GrDirectContext;
20 class GrImageContext;
21 class GrRecordingContext;
22 
23 class GrContext_Base : public SkRefCnt {
24 public:
25     ~GrContext_Base() override;
26 
27     /*
28      * Safely downcast to a GrDirectContext.
29      */
asDirectContext()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 protected:
56     friend class GrBaseContextPriv; // for hidden functions
57 
58     GrContext_Base(sk_sp<GrContextThreadSafeProxy>);
59 
60     virtual bool init();
61 
62     /**
63      * An identifier for this context. The id is used by all compatible contexts. For example,
64      * if SkImages are created on one thread using an image creation context, then fed into a
65      * DDL Recorder on second thread (which has a recording context) and finally replayed on
66      * a third thread with a direct context, then all three contexts will report the same id.
67      * It is an error for an image to be used with contexts that report different ids.
68      */
69     uint32_t contextID() const;
70 
matches(GrContext_Base * candidate)71     bool matches(GrContext_Base* candidate) const {
72         return candidate && candidate->contextID() == this->contextID();
73     }
74 
75     /*
76      * The options in effect for this context
77      */
78     const GrContextOptions& options() const;
79 
80     const GrCaps* caps() const;
81     sk_sp<const GrCaps> refCaps() const;
82 
asImageContext()83     virtual GrImageContext* asImageContext() { return nullptr; }
asRecordingContext()84     virtual GrRecordingContext* asRecordingContext() { return nullptr; }
85 
86     sk_sp<GrContextThreadSafeProxy>         fThreadSafeProxy;
87 
88 private:
89     using INHERITED = SkRefCnt;
90 };
91 
92 #endif
93