Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 1 | /* |
| 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 Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 16 | #include "GrMtlCaps.h" |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 17 | #include "GrMtlCopyManager.h" |
| 18 | #include "GrMtlResourceProvider.h" |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 19 | #include "GrMtlStencilAttachment.h" |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 20 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 21 | #import <Metal/Metal.h> |
| 22 | |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 23 | class GrMtlGpuRTCommandBuffer; |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 24 | class GrMtlTexture; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 25 | class GrSemaphore; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 26 | struct GrMtlBackendContext; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 27 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 28 | namespace SkSL { |
| 29 | class Compiler; |
| 30 | } |
| 31 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 32 | class GrMtlGpu : public GrGpu { |
| 33 | public: |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 34 | static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options, |
| 35 | id<MTLDevice> device, id<MTLCommandQueue> queue); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 36 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 37 | ~GrMtlGpu() override = default; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 38 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 39 | const GrMtlCaps& mtlCaps() const { return *fMtlCaps.get(); } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 40 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 41 | id<MTLDevice> device() const { return fDevice; } |
| 42 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 43 | id<MTLCommandBuffer> commandBuffer() const { return fCmdBuffer; } |
| 44 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 45 | GrMtlResourceProvider& resourceProvider() { return fResourceProvider; } |
| 46 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 47 | enum SyncQueue { |
| 48 | kForce_SyncQueue, |
| 49 | kSkip_SyncQueue |
| 50 | }; |
| 51 | |
Timothy Liang | 49528b6 | 2018-08-02 14:18:37 -0400 | [diff] [blame] | 52 | // Commits the current command buffer to the queue and then creates a new command buffer. If |
| 53 | // sync is set to kForce_SyncQueue, the function will wait for all work in the committed |
| 54 | // command buffer to finish before creating a new buffer and returning. |
| 55 | void submitCommandBuffer(SyncQueue sync); |
| 56 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 57 | #ifdef GR_TEST_UTILS |
| 58 | GrBackendTexture createTestingOnlyBackendTexture(const void* pixels, int w, int h, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 59 | GrColorType colorType, bool isRT, |
| 60 | GrMipMapped, size_t rowBytes = 0) override; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 61 | |
| 62 | bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override; |
| 63 | |
| 64 | void deleteTestingOnlyBackendTexture(const GrBackendTexture&) override; |
| 65 | |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 66 | GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 67 | |
| 68 | void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override; |
| 69 | |
| 70 | void testingOnly_flushGpuAndSync() override; |
| 71 | #endif |
| 72 | |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 73 | bool copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 74 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 75 | const SkIRect& srcRect, const SkIPoint& dstPoint); |
| 76 | |
Timothy Liang | 70c787a | 2018-08-01 09:56:25 -0400 | [diff] [blame] | 77 | // This function is needed when we want to copy between two surfaces with different origins and |
| 78 | // the destination surface is not a render target. We will first draw to a temporary render |
| 79 | // target to adjust for the different origins and then blit from there to the destination. |
| 80 | bool copySurfaceAsDrawThenBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 81 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 82 | const SkIRect& srcRect, const SkIPoint& dstPoint); |
| 83 | |
Robert Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 84 | bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 85 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 86 | const SkIRect& srcRect, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 87 | const SkIPoint& dstPoint, |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 88 | bool canDiscardOutsideDstRect) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 89 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 90 | GrGpuRTCommandBuffer* getCommandBuffer( |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 91 | GrRenderTarget*, GrSurfaceOrigin, const SkRect& bounds, |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 92 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 93 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override; |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 94 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 95 | GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 96 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 97 | SkSL::Compiler* shaderCompiler() const { return fCompiler.get(); } |
| 98 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 99 | void submit(GrGpuCommandBuffer* buffer) override; |
| 100 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 101 | GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; } |
| 102 | bool waitFence(GrFence, uint64_t) override { return true; } |
| 103 | void deleteFence(GrFence) const override {} |
| 104 | |
| 105 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override { |
| 106 | return nullptr; |
| 107 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 108 | sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 109 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 110 | GrWrapOwnership ownership) override { return nullptr; } |
Greg Daniel | 858e12c | 2018-12-06 11:11:37 -0500 | [diff] [blame] | 111 | void insertSemaphore(sk_sp<GrSemaphore> semaphore) override {} |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 112 | void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {} |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 113 | sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; } |
| 114 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 115 | // When the Metal backend actually uses indirect command buffers, this function will actually do |
| 116 | // what it says. For now, every command is encoded directly into the primary command buffer, so |
| 117 | // this function is pretty useless, except for indicating that a render target has been drawn |
| 118 | // to. |
| 119 | void submitIndirectCommandBuffer(GrSurface* surface, GrSurfaceOrigin origin, |
| 120 | const SkIRect* bounds) { |
| 121 | this->didWriteToSurface(surface, origin, bounds); |
| 122 | } |
| 123 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 124 | private: |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 125 | GrMtlGpu(GrContext* context, const GrContextOptions& options, |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 126 | id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 127 | |
| 128 | void onResetContext(uint32_t resetBits) override {} |
| 129 | |
| 130 | void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} |
| 131 | |
| 132 | sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 133 | const GrMipLevel texels[], int mipLevelCount) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 134 | |
Brian Salomon | c67c31c | 2018-12-06 10:00:03 -0500 | [diff] [blame] | 135 | sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership, GrIOType, |
Greg Daniel | 2268ad2 | 2018-11-15 09:27:38 -0500 | [diff] [blame] | 136 | bool purgeImmediately) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 137 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 138 | sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 139 | int sampleCnt, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 140 | GrWrapOwnership) override; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 141 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 142 | sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 143 | |
| 144 | sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 145 | int sampleCnt) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 146 | |
Timothy Liang | 49528b6 | 2018-08-02 14:18:37 -0400 | [diff] [blame] | 147 | GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 148 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 149 | bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType, |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 150 | void* buffer, size_t rowBytes) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 151 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 152 | bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType, |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 153 | const GrMipLevel[], int mipLevelCount) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 154 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 155 | bool onTransferPixels(GrTexture*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 156 | int left, int top, int width, int height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 157 | GrColorType, GrBuffer*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 158 | size_t offset, size_t rowBytes) override { |
| 159 | return false; |
| 160 | } |
| 161 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 162 | bool onRegenerateMipMapLevels(GrTexture*) override { return false; } |
| 163 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 164 | void onResolveRenderTarget(GrRenderTarget* target) override { return; } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 165 | |
Timothy Liang | 5422f9a | 2018-08-10 10:57:55 -0400 | [diff] [blame] | 166 | void onFinishFlush(bool insertedSemaphores) override { |
| 167 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 168 | } |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 169 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 170 | // Function that uploads data onto textures with private storage mode (GPU access only). |
| 171 | bool uploadToTexture(GrMtlTexture* tex, int left, int top, int width, int height, |
| 172 | GrColorType dataColorType, const GrMipLevel texels[], int mipLevels); |
| 173 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 174 | GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*, |
| 175 | int width, |
Ethan Nicholas | 0106351 | 2018-10-08 16:58:25 -0400 | [diff] [blame] | 176 | int height) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 177 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 178 | #if GR_TEST_UTILS |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 179 | bool createTestingOnlyMtlTextureInfo(GrColorType colorType, int w, int h, bool texturable, |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 180 | bool renderable, GrMipMapped mipMapped, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 181 | const void* srcData, size_t rowBytes, |
| 182 | GrMtlTextureInfo* info); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 183 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 184 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 185 | sk_sp<GrMtlCaps> fMtlCaps; |
| 186 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 187 | id<MTLDevice> fDevice; |
| 188 | id<MTLCommandQueue> fQueue; |
| 189 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 190 | id<MTLCommandBuffer> fCmdBuffer; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 191 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 192 | std::unique_ptr<SkSL::Compiler> fCompiler; |
| 193 | GrMtlCopyManager fCopyManager; |
| 194 | GrMtlResourceProvider fResourceProvider; |
| 195 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 196 | typedef GrGpu INHERITED; |
| 197 | }; |
| 198 | |
| 199 | #endif |
| 200 | |