blob: bf9b1f58239373451950b5eed6a0c04bad3f8327 [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"
Timothy Liange30739a2018-07-31 10:51:17 -040017#include "GrMtlCopyManager.h"
18#include "GrMtlResourceProvider.h"
Greg Danielcebcb842017-07-31 10:45:52 -040019
Greg Daniele5ddff52017-07-05 16:49:36 -040020#import <Metal/Metal.h>
21
Timothy Liangff19c8f2018-07-11 13:27:21 -040022class GrMtlTexture;
Greg Daniele5ddff52017-07-05 16:49:36 -040023class GrSemaphore;
Greg Danielb76a72a2017-07-13 15:07:54 -040024struct GrMtlBackendContext;
Greg Daniele5ddff52017-07-05 16:49:36 -040025
Timothy Liange30739a2018-07-31 10:51:17 -040026namespace SkSL {
27 class Compiler;
28}
29
Greg Daniele5ddff52017-07-05 16:49:36 -040030class GrMtlGpu : public GrGpu {
31public:
Brian Salomon384fab42017-12-07 12:33:05 -050032 static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options,
33 id<MTLDevice> device, id<MTLCommandQueue> queue);
Greg Daniele5ddff52017-07-05 16:49:36 -040034
Timothy Liange30739a2018-07-31 10:51:17 -040035 ~GrMtlGpu() override = default;
Greg Daniel4a081e22017-08-04 09:34:44 -040036
Greg Danielcebcb842017-07-31 10:45:52 -040037 const GrMtlCaps& mtlCaps() const { return *fMtlCaps.get(); }
Greg Daniele5ddff52017-07-05 16:49:36 -040038
Greg Daniel4a081e22017-08-04 09:34:44 -040039 id<MTLDevice> device() const { return fDevice; }
40
Timothy Liangef21d7e2018-07-02 17:03:30 -040041 id<MTLCommandBuffer> commandBuffer() const { return fCmdBuffer; }
42
Timothy Liange30739a2018-07-31 10:51:17 -040043 GrMtlResourceProvider& resourceProvider() { return fResourceProvider; }
44
Timothy Liangef21d7e2018-07-02 17:03:30 -040045 enum SyncQueue {
46 kForce_SyncQueue,
47 kSkip_SyncQueue
48 };
49
Timothy Liang49528b62018-08-02 14:18:37 -040050 // Commits the current command buffer to the queue and then creates a new command buffer. If
51 // sync is set to kForce_SyncQueue, the function will wait for all work in the committed
52 // command buffer to finish before creating a new buffer and returning.
53 void submitCommandBuffer(SyncQueue sync);
54
Timothy Liang760dbc42018-07-17 13:28:20 -040055#ifdef GR_TEST_UTILS
56 GrBackendTexture createTestingOnlyBackendTexture(const void* pixels, int w, int h,
57 GrPixelConfig config, bool isRT,
58 GrMipMapped) override;
59
60 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
61
62 void deleteTestingOnlyBackendTexture(const GrBackendTexture&) override;
63
Brian Osman2d010b62018-08-09 10:55:09 -040064 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override;
Timothy Liang760dbc42018-07-17 13:28:20 -040065
66 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
67
68 void testingOnly_flushGpuAndSync() override;
69#endif
70
Timothy Liange35055f2018-07-20 16:53:00 -040071 bool copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
72 GrSurface* src, GrSurfaceOrigin srcOrigin,
73 const SkIRect& srcRect, const SkIPoint& dstPoint);
74
Timothy Liang70c787a2018-08-01 09:56:25 -040075 // This function is needed when we want to copy between two surfaces with different origins and
76 // the destination surface is not a render target. We will first draw to a temporary render
77 // target to adjust for the different origins and then blit from there to the destination.
78 bool copySurfaceAsDrawThenBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
79 GrSurface* src, GrSurfaceOrigin srcOrigin,
80 const SkIRect& srcRect, const SkIPoint& dstPoint);
81
Robert Phillipsb0e93a22017-08-29 08:26:54 -040082 bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
83 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniele5ddff52017-07-05 16:49:36 -040084 const SkIRect& srcRect,
Greg Daniel55fa6472018-03-16 16:13:10 -040085 const SkIPoint& dstPoint,
Timothy Liange35055f2018-07-20 16:53:00 -040086 bool canDiscardOutsideDstRect) override;
Greg Daniele5ddff52017-07-05 16:49:36 -040087
Robert Phillips5b5d84c2018-08-09 15:12:18 -040088 GrGpuRTCommandBuffer* getCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -040089 GrRenderTarget*, GrSurfaceOrigin,
Greg Daniel6682ff22017-08-24 17:10:47 -040090 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
Timothy Liange70604e2018-07-19 09:49:46 -040091 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
Greg Daniel6682ff22017-08-24 17:10:47 -040092
Robert Phillips5b5d84c2018-08-09 15:12:18 -040093 GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
Greg Daniele5ddff52017-07-05 16:49:36 -040094
Timothy Liange30739a2018-07-31 10:51:17 -040095 SkSL::Compiler* shaderCompiler() const { return fCompiler.get(); }
96
Robert Phillips5b5d84c2018-08-09 15:12:18 -040097 void submit(GrGpuCommandBuffer* buffer) override;
98
Greg Daniele5ddff52017-07-05 16:49:36 -040099 GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; }
100 bool waitFence(GrFence, uint64_t) override { return true; }
101 void deleteFence(GrFence) const override {}
102
103 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override {
104 return nullptr;
105 }
Greg Daniel48661b82018-01-22 16:11:35 -0500106 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
107 GrResourceProvider::SemaphoreWrapType wrapType,
108 GrWrapOwnership ownership) override { return nullptr; }
109 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override {}
110 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {}
Greg Daniele5ddff52017-07-05 16:49:36 -0400111 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
112
Timothy Liang5422f9a2018-08-10 10:57:55 -0400113 // When the Metal backend actually uses indirect command buffers, this function will actually do
114 // what it says. For now, every command is encoded directly into the primary command buffer, so
115 // this function is pretty useless, except for indicating that a render target has been drawn
116 // to.
117 void submitIndirectCommandBuffer(GrSurface* surface, GrSurfaceOrigin origin,
118 const SkIRect* bounds) {
119 this->didWriteToSurface(surface, origin, bounds);
120 }
121
Greg Daniele5ddff52017-07-05 16:49:36 -0400122private:
Greg Danielb76a72a2017-07-13 15:07:54 -0400123 GrMtlGpu(GrContext* context, const GrContextOptions& options,
Greg Danielcebcb842017-07-31 10:45:52 -0400124 id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet);
Greg Daniele5ddff52017-07-05 16:49:36 -0400125
126 void onResetContext(uint32_t resetBits) override {}
127
128 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
129
130 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500131 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400132
Timothy Liange886e802018-07-02 16:03:28 -0400133 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400134
Brian Salomond17f6582017-07-19 18:28:58 -0400135 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400136 int sampleCnt,
Timothy Liange886e802018-07-02 16:03:28 -0400137 GrWrapOwnership) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400138
Timothy Liange886e802018-07-02 16:03:28 -0400139 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400140
141 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Timothy Liange886e802018-07-02 16:03:28 -0400142 int sampleCnt) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400143
Timothy Liang49528b62018-08-02 14:18:37 -0400144 GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400145
Brian Salomona6948702018-06-01 15:33:20 -0400146 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
Timothy Liangef21d7e2018-07-02 17:03:30 -0400147 void* buffer, size_t rowBytes) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400148
Brian Salomona9b04b92018-06-01 15:04:28 -0400149 bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
Timothy Lianga8046af2018-07-19 09:58:00 -0400150 const GrMipLevel[], int mipLevelCount) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400151
Brian Salomonc320b152018-02-20 14:05:36 -0500152 bool onTransferPixels(GrTexture*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400153 int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500154 GrColorType, GrBuffer*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400155 size_t offset, size_t rowBytes) override {
156 return false;
157 }
158
Brian Salomon930f9392018-06-20 16:25:26 -0400159 bool onRegenerateMipMapLevels(GrTexture*) override { return false; }
160
Brian Salomon1fabd512018-02-09 09:54:25 -0500161 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
Greg Daniele5ddff52017-07-05 16:49:36 -0400162
Timothy Liang5422f9a2018-08-10 10:57:55 -0400163 void onFinishFlush(bool insertedSemaphores) override {
164 this->submitCommandBuffer(kSkip_SyncQueue);
165 }
Greg Daniel51316782017-08-02 15:10:09 +0000166
Timothy Liangff19c8f2018-07-11 13:27:21 -0400167 // Function that uploads data onto textures with private storage mode (GPU access only).
168 bool uploadToTexture(GrMtlTexture* tex, int left, int top, int width, int height,
169 GrColorType dataColorType, const GrMipLevel texels[], int mipLevels);
170
Greg Daniele5ddff52017-07-05 16:49:36 -0400171 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
172 int width,
173 int height) override {
174 return nullptr;
175 }
176
Robert Phillips95214472017-08-08 18:00:03 -0400177 void clearStencil(GrRenderTarget* target, int clearValue) override {}
Greg Daniele5ddff52017-07-05 16:49:36 -0400178
Brian Salomonf865b052018-03-09 09:01:53 -0500179#if GR_TEST_UTILS
Timothy Liang760dbc42018-07-17 13:28:20 -0400180 bool createTestingOnlyMtlTextureInfo(GrPixelConfig config, int w, int h, bool texturable,
181 bool renderable, GrMipMapped mipMapped,
182 const void* srcData, GrMtlTextureInfo* info);
Brian Salomonf865b052018-03-09 09:01:53 -0500183#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500184
Greg Danielcebcb842017-07-31 10:45:52 -0400185 sk_sp<GrMtlCaps> fMtlCaps;
186
Greg Danielb76a72a2017-07-13 15:07:54 -0400187 id<MTLDevice> fDevice;
188 id<MTLCommandQueue> fQueue;
189
Timothy Liangef21d7e2018-07-02 17:03:30 -0400190 id<MTLCommandBuffer> fCmdBuffer;
Greg Daniele5ddff52017-07-05 16:49:36 -0400191
Timothy Liange30739a2018-07-31 10:51:17 -0400192 std::unique_ptr<SkSL::Compiler> fCompiler;
193 GrMtlCopyManager fCopyManager;
194 GrMtlResourceProvider fResourceProvider;
195
Greg Daniele5ddff52017-07-05 16:49:36 -0400196 typedef GrGpu INHERITED;
197};
198
199#endif
200