blob: 8a68b763ebbaf864d19197fe81fad90678a56117 [file] [log] [blame]
Greg Daniele5ddff52017-07-05 16:49:36 -04001/*
2 * Copyright 2017 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 GrMtlGpu_DEFINED
9#define GrMtlGpu_DEFINED
10
11#include "GrGpu.h"
12#include "GrRenderTarget.h"
13#include "GrSemaphore.h"
14#include "GrTexture.h"
15
Greg Danielcebcb842017-07-31 10:45:52 -040016#include "GrMtlCaps.h"
17
Greg Daniele5ddff52017-07-05 16:49:36 -040018#import <Metal/Metal.h>
19
Timothy Liangff19c8f2018-07-11 13:27:21 -040020class GrMtlTexture;
Greg Daniele5ddff52017-07-05 16:49:36 -040021class GrSemaphore;
Greg Danielb76a72a2017-07-13 15:07:54 -040022struct GrMtlBackendContext;
Greg Daniele5ddff52017-07-05 16:49:36 -040023
24class GrMtlGpu : public GrGpu {
25public:
Brian Salomon384fab42017-12-07 12:33:05 -050026 static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options,
27 id<MTLDevice> device, id<MTLCommandQueue> queue);
Greg Daniele5ddff52017-07-05 16:49:36 -040028
29 ~GrMtlGpu() override {}
Greg Daniel4a081e22017-08-04 09:34:44 -040030
Greg Danielcebcb842017-07-31 10:45:52 -040031 const GrMtlCaps& mtlCaps() const { return *fMtlCaps.get(); }
Greg Daniele5ddff52017-07-05 16:49:36 -040032
Greg Daniel4a081e22017-08-04 09:34:44 -040033 id<MTLDevice> device() const { return fDevice; }
34
Timothy Liangef21d7e2018-07-02 17:03:30 -040035 id<MTLCommandBuffer> commandBuffer() const { return fCmdBuffer; }
36
37 enum SyncQueue {
38 kForce_SyncQueue,
39 kSkip_SyncQueue
40 };
41
Timothy Liang760dbc42018-07-17 13:28:20 -040042#ifdef GR_TEST_UTILS
43 GrBackendTexture createTestingOnlyBackendTexture(const void* pixels, int w, int h,
44 GrPixelConfig config, bool isRT,
45 GrMipMapped) override;
46
47 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
48
49 void deleteTestingOnlyBackendTexture(const GrBackendTexture&) override;
50
51 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType,
52 GrSRGBEncoded) override;
53
54 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
55
56 void testingOnly_flushGpuAndSync() override;
57#endif
58
Robert Phillipsb0e93a22017-08-29 08:26:54 -040059 bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
60 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniele5ddff52017-07-05 16:49:36 -040061 const SkIRect& srcRect,
Greg Daniel55fa6472018-03-16 16:13:10 -040062 const SkIPoint& dstPoint,
63 bool canDiscardOutsideDstRect) override { return false; }
Greg Daniele5ddff52017-07-05 16:49:36 -040064
Greg Daniel6682ff22017-08-24 17:10:47 -040065 GrGpuRTCommandBuffer* createCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -040066 GrRenderTarget*, GrSurfaceOrigin,
Greg Daniel6682ff22017-08-24 17:10:47 -040067 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
68 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override {
69 return nullptr;
70 }
71
72 GrGpuTextureCommandBuffer* createCommandBuffer(GrTexture*, GrSurfaceOrigin) override {
Greg Daniele5ddff52017-07-05 16:49:36 -040073 return nullptr;
74 }
75
76 GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; }
77 bool waitFence(GrFence, uint64_t) override { return true; }
78 void deleteFence(GrFence) const override {}
79
80 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override {
81 return nullptr;
82 }
Greg Daniel48661b82018-01-22 16:11:35 -050083 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
84 GrResourceProvider::SemaphoreWrapType wrapType,
85 GrWrapOwnership ownership) override { return nullptr; }
86 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override {}
87 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {}
Greg Daniele5ddff52017-07-05 16:49:36 -040088 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
89
90private:
Greg Danielb76a72a2017-07-13 15:07:54 -040091 GrMtlGpu(GrContext* context, const GrContextOptions& options,
Greg Danielcebcb842017-07-31 10:45:52 -040092 id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet);
Greg Daniele5ddff52017-07-05 16:49:36 -040093
94 void onResetContext(uint32_t resetBits) override {}
95
96 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
97
98 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -050099 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400100
Timothy Liange886e802018-07-02 16:03:28 -0400101 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400102
Brian Salomond17f6582017-07-19 18:28:58 -0400103 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400104 int sampleCnt,
Timothy Liange886e802018-07-02 16:03:28 -0400105 GrWrapOwnership) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400106
Timothy Liange886e802018-07-02 16:03:28 -0400107 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400108
109 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Timothy Liange886e802018-07-02 16:03:28 -0400110 int sampleCnt) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400111
112 GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override {
113 return nullptr;
114 }
115
Brian Salomona6948702018-06-01 15:33:20 -0400116 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
Timothy Liangef21d7e2018-07-02 17:03:30 -0400117 void* buffer, size_t rowBytes) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400118
Brian Salomona9b04b92018-06-01 15:04:28 -0400119 bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
120 const GrMipLevel[], int) override {
Greg Daniele5ddff52017-07-05 16:49:36 -0400121 return false;
122 }
123
Brian Salomonc320b152018-02-20 14:05:36 -0500124 bool onTransferPixels(GrTexture*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400125 int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500126 GrColorType, GrBuffer*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400127 size_t offset, size_t rowBytes) override {
128 return false;
129 }
130
Brian Salomon930f9392018-06-20 16:25:26 -0400131 bool onRegenerateMipMapLevels(GrTexture*) override { return false; }
132
Brian Salomon1fabd512018-02-09 09:54:25 -0500133 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
Greg Daniele5ddff52017-07-05 16:49:36 -0400134
Greg Daniel51316782017-08-02 15:10:09 +0000135 void onFinishFlush(bool insertedSemaphores) override {}
136
Timothy Liangef21d7e2018-07-02 17:03:30 -0400137 // Commits the current command buffer to the queue and then creates a new command buffer. If
138 // sync is set to kForce_SyncQueue, the function will wait for all work in the committed
139 // command buffer to finish before creating a new buffer and returning.
140 void submitCommandBuffer(SyncQueue sync);
141
Timothy Liangff19c8f2018-07-11 13:27:21 -0400142 // Function that uploads data onto textures with private storage mode (GPU access only).
143 bool uploadToTexture(GrMtlTexture* tex, int left, int top, int width, int height,
144 GrColorType dataColorType, const GrMipLevel texels[], int mipLevels);
145
Greg Daniele5ddff52017-07-05 16:49:36 -0400146 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
147 int width,
148 int height) override {
149 return nullptr;
150 }
151
Robert Phillips95214472017-08-08 18:00:03 -0400152 void clearStencil(GrRenderTarget* target, int clearValue) override {}
Greg Daniele5ddff52017-07-05 16:49:36 -0400153
Brian Salomonf865b052018-03-09 09:01:53 -0500154#if GR_TEST_UTILS
Timothy Liang760dbc42018-07-17 13:28:20 -0400155 bool createTestingOnlyMtlTextureInfo(GrPixelConfig config, int w, int h, bool texturable,
156 bool renderable, GrMipMapped mipMapped,
157 const void* srcData, GrMtlTextureInfo* info);
Brian Salomonf865b052018-03-09 09:01:53 -0500158#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500159
Greg Danielcebcb842017-07-31 10:45:52 -0400160 sk_sp<GrMtlCaps> fMtlCaps;
161
Greg Danielb76a72a2017-07-13 15:07:54 -0400162 id<MTLDevice> fDevice;
163 id<MTLCommandQueue> fQueue;
164
Timothy Liangef21d7e2018-07-02 17:03:30 -0400165 id<MTLCommandBuffer> fCmdBuffer;
Greg Daniele5ddff52017-07-05 16:49:36 -0400166
167 typedef GrGpu INHERITED;
168};
169
170#endif
171