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 GrContext;
19class GrImageContext;
20class GrRecordingContext;
21
22class GrContext_Base : public SkRefCnt {
23public:
24 virtual ~GrContext_Base();
25
26 /*
27 * The 3D API backing this context
28 */
29 SK_API GrBackendApi backend() const { return fBackend; }
30
31 /*
32 * Retrieve the default GrBackendFormat for a given SkColorType and renderability.
33 * It is guaranteed that this backend format will be the one used by the GrContext
34 * SkColorType and SkSurfaceCharacterization-based createBackendTexture methods.
35 *
36 * The caller should check that the returned format is valid.
37 */
38 SK_API GrBackendFormat defaultBackendFormat(SkColorType, GrRenderable) const;
39
40 SK_API GrBackendFormat compressedBackendFormat(SkImage::CompressionType) const;
41
42 // Provides access to functions that aren't part of the public API.
43 GrBaseContextPriv priv();
44 const GrBaseContextPriv priv() const;
45
46protected:
47 friend class GrBaseContextPriv; // for hidden functions
48
49 GrContext_Base(GrBackendApi backend, const GrContextOptions& options, uint32_t contextID);
50
51 virtual bool init(sk_sp<const GrCaps>);
52
53 /**
54 * An identifier for this context. The id is used by all compatible contexts. For example,
55 * if SkImages are created on one thread using an image creation context, then fed into a
56 * DDL Recorder on second thread (which has a recording context) and finally replayed on
57 * a third thread with a direct context, then all three contexts will report the same id.
58 * It is an error for an image to be used with contexts that report different ids.
59 */
60 uint32_t contextID() const { return fContextID; }
61
62 bool matches(GrContext_Base* candidate) const {
63 return candidate->contextID() == this->contextID();
64 }
65
66 /*
67 * The options in effect for this context
68 */
69 const GrContextOptions& options() const { return fOptions; }
70
71 const GrCaps* caps() const;
72 sk_sp<const GrCaps> refCaps() const;
73
74 virtual GrImageContext* asImageContext() { return nullptr; }
75 virtual GrRecordingContext* asRecordingContext() { return nullptr; }
76 virtual GrContext* asDirectContext() { return nullptr; }
77
78private:
79 const GrBackendApi fBackend;
80 const GrContextOptions fOptions;
81 const uint32_t fContextID;
82 sk_sp<const GrCaps> fCaps;
83
84 typedef SkRefCnt INHERITED;
85};
86
87#endif
88