blob: 631c9517805fe5a032d7b84981e7f943e14e62a4 [file] [log] [blame]
Robert Phillipsad248452020-06-30 09:27:52 -04001/*
2 * Copyright 2020 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 GrDirectContext_DEFINED
9#define GrDirectContext_DEFINED
10
11#include "include/gpu/GrContext.h"
12
13class GrAtlasManager;
14
Robert Phillipsc7228c62020-07-14 12:57:39 -040015class SK_API GrDirectContext : public GrContext {
Robert Phillipsad248452020-06-30 09:27:52 -040016public:
Robert Phillipsf4f80112020-07-13 16:13:31 -040017#ifdef SK_GL
18 /**
19 * Creates a GrDirectContext for a backend context. If no GrGLInterface is provided then the
20 * result of GrGLMakeNativeInterface() is used if it succeeds.
21 */
22 static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>, const GrContextOptions&);
23 static sk_sp<GrDirectContext> MakeGL(sk_sp<const GrGLInterface>);
24 static sk_sp<GrDirectContext> MakeGL(const GrContextOptions&);
25 static sk_sp<GrDirectContext> MakeGL();
26#endif
27
28#ifdef SK_VULKAN
29 /**
30 * The Vulkan context (VkQueue, VkDevice, VkInstance) must be kept alive until the returned
31 * GrDirectContext is destroyed. This also means that any objects created with this
32 * GrDirectContext (e.g. SkSurfaces, SkImages, etc.) must also be released as they may hold
33 * refs on the GrDirectContext. Once all these objects and the GrDirectContext are released,
34 * then it is safe to delete the vulkan objects.
35 */
36 static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&, const GrContextOptions&);
37 static sk_sp<GrDirectContext> MakeVulkan(const GrVkBackendContext&);
38#endif
39
40#ifdef SK_METAL
41 /**
42 * Makes a GrDirectContext which uses Metal as the backend. The device parameter is an
43 * MTLDevice and queue is an MTLCommandQueue which should be used by the backend. These objects
44 * must have a ref on them which can be transferred to Ganesh which will release the ref
45 * when the GrDirectContext is destroyed.
46 */
47 static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue, const GrContextOptions&);
48 static sk_sp<GrDirectContext> MakeMetal(void* device, void* queue);
49#endif
50
51#ifdef SK_DIRECT3D
52 /**
53 * Makes a GrDirectContext which uses Direct3D as the backend. The Direct3D context
54 * must be kept alive until the returned GrDirectContext is first destroyed or abandoned.
55 */
56 static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&, const GrContextOptions&);
57 static sk_sp<GrDirectContext> MakeDirect3D(const GrD3DBackendContext&);
58#endif
59
60#ifdef SK_DAWN
61 static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&,
62 const GrContextOptions&);
63 static sk_sp<GrDirectContext> MakeDawn(const wgpu::Device&);
64#endif
65
66 static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*, const GrContextOptions&);
67 static sk_sp<GrDirectContext> MakeMock(const GrMockOptions*);
Robert Phillipsad248452020-06-30 09:27:52 -040068
69 ~GrDirectContext() override;
70
71 void abandonContext() override;
72
73 void releaseResourcesAndAbandonContext() override;
74
75 void freeGpuResources() override;
76
77protected:
Robert Phillipsf4f80112020-07-13 16:13:31 -040078 GrDirectContext(GrBackendApi backend, const GrContextOptions& options);
79
Robert Phillipsad248452020-06-30 09:27:52 -040080 bool init() override;
81
Robert Phillips3262bc82020-08-10 12:11:58 -040082 GrAtlasManager* onGetAtlasManager() override { return fAtlasManager.get(); }
Robert Phillipsad248452020-06-30 09:27:52 -040083
Robert Phillips44333c52020-06-30 13:28:00 -040084 GrDirectContext* asDirectContext() override { return this; }
85
Robert Phillipsad248452020-06-30 09:27:52 -040086private:
Robert Phillips3262bc82020-08-10 12:11:58 -040087 std::unique_ptr<GrAtlasManager> fAtlasManager;
Robert Phillipsad248452020-06-30 09:27:52 -040088
89 typedef GrContext INHERITED;
90};
91
92
93#endif