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" |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 19 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 20 | #import <Metal/Metal.h> |
| 21 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 22 | class GrMtlTexture; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 23 | class GrSemaphore; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 24 | struct GrMtlBackendContext; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 25 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 26 | namespace SkSL { |
| 27 | class Compiler; |
| 28 | } |
| 29 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 30 | class GrMtlGpu : public GrGpu { |
| 31 | public: |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 32 | static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options, |
| 33 | id<MTLDevice> device, id<MTLCommandQueue> queue); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 34 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 35 | ~GrMtlGpu() override = default; |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 36 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 37 | const GrMtlCaps& mtlCaps() const { return *fMtlCaps.get(); } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 38 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 39 | id<MTLDevice> device() const { return fDevice; } |
| 40 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 41 | id<MTLCommandBuffer> commandBuffer() const { return fCmdBuffer; } |
| 42 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 43 | GrMtlResourceProvider& resourceProvider() { return fResourceProvider; } |
| 44 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 45 | enum SyncQueue { |
| 46 | kForce_SyncQueue, |
| 47 | kSkip_SyncQueue |
| 48 | }; |
| 49 | |
Timothy Liang | 49528b6 | 2018-08-02 14:18:37 -0400 | [diff] [blame] | 50 | // 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 Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 55 | #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 Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame^] | 64 | GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override; |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 65 | |
| 66 | void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override; |
| 67 | |
| 68 | void testingOnly_flushGpuAndSync() override; |
| 69 | #endif |
| 70 | |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 71 | bool copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 72 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
| 73 | const SkIRect& srcRect, const SkIPoint& dstPoint); |
| 74 | |
Timothy Liang | 70c787a | 2018-08-01 09:56:25 -0400 | [diff] [blame] | 75 | // 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 Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 82 | bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 83 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 84 | const SkIRect& srcRect, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 85 | const SkIPoint& dstPoint, |
Timothy Liang | e35055f | 2018-07-20 16:53:00 -0400 | [diff] [blame] | 86 | bool canDiscardOutsideDstRect) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 87 | |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 88 | GrGpuRTCommandBuffer* createCommandBuffer( |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 89 | GrRenderTarget*, GrSurfaceOrigin, |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 90 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 91 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override; |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 92 | |
Timothy Liang | e70604e | 2018-07-19 09:49:46 -0400 | [diff] [blame] | 93 | GrGpuTextureCommandBuffer* createCommandBuffer(GrTexture*, GrSurfaceOrigin) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 94 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 95 | SkSL::Compiler* shaderCompiler() const { return fCompiler.get(); } |
| 96 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 97 | GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; } |
| 98 | bool waitFence(GrFence, uint64_t) override { return true; } |
| 99 | void deleteFence(GrFence) const override {} |
| 100 | |
| 101 | sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override { |
| 102 | return nullptr; |
| 103 | } |
Greg Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 104 | sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore, |
| 105 | GrResourceProvider::SemaphoreWrapType wrapType, |
| 106 | GrWrapOwnership ownership) override { return nullptr; } |
| 107 | void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override {} |
| 108 | void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {} |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 109 | sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; } |
| 110 | |
| 111 | private: |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 112 | GrMtlGpu(GrContext* context, const GrContextOptions& options, |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 113 | id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 114 | |
| 115 | void onResetContext(uint32_t resetBits) override {} |
| 116 | |
| 117 | void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} |
| 118 | |
| 119 | sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 120 | const GrMipLevel texels[], int mipLevelCount) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 121 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 122 | sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 123 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 124 | sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 125 | int sampleCnt, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 126 | GrWrapOwnership) override; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 127 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 128 | sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 129 | |
| 130 | sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 131 | int sampleCnt) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 132 | |
Timothy Liang | 49528b6 | 2018-08-02 14:18:37 -0400 | [diff] [blame] | 133 | GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 134 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 135 | 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] | 136 | void* buffer, size_t rowBytes) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 137 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 138 | bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType, |
Timothy Liang | a8046af | 2018-07-19 09:58:00 -0400 | [diff] [blame] | 139 | const GrMipLevel[], int mipLevelCount) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 140 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 141 | bool onTransferPixels(GrTexture*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 142 | int left, int top, int width, int height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 143 | GrColorType, GrBuffer*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 144 | size_t offset, size_t rowBytes) override { |
| 145 | return false; |
| 146 | } |
| 147 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 148 | bool onRegenerateMipMapLevels(GrTexture*) override { return false; } |
| 149 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 150 | void onResolveRenderTarget(GrRenderTarget* target) override { return; } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 151 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 152 | void onFinishFlush(bool insertedSemaphores) override {} |
| 153 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 154 | // Function that uploads data onto textures with private storage mode (GPU access only). |
| 155 | bool uploadToTexture(GrMtlTexture* tex, int left, int top, int width, int height, |
| 156 | GrColorType dataColorType, const GrMipLevel texels[], int mipLevels); |
| 157 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 158 | GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*, |
| 159 | int width, |
| 160 | int height) override { |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 164 | void clearStencil(GrRenderTarget* target, int clearValue) override {} |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 165 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 166 | #if GR_TEST_UTILS |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 167 | bool createTestingOnlyMtlTextureInfo(GrPixelConfig config, int w, int h, bool texturable, |
| 168 | bool renderable, GrMipMapped mipMapped, |
| 169 | const void* srcData, GrMtlTextureInfo* info); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 170 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 171 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 172 | sk_sp<GrMtlCaps> fMtlCaps; |
| 173 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 174 | id<MTLDevice> fDevice; |
| 175 | id<MTLCommandQueue> fQueue; |
| 176 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 177 | id<MTLCommandBuffer> fCmdBuffer; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 178 | |
Timothy Liang | e30739a | 2018-07-31 10:51:17 -0400 | [diff] [blame] | 179 | std::unique_ptr<SkSL::Compiler> fCompiler; |
| 180 | GrMtlCopyManager fCopyManager; |
| 181 | GrMtlResourceProvider fResourceProvider; |
| 182 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 183 | typedef GrGpu INHERITED; |
| 184 | }; |
| 185 | |
| 186 | #endif |
| 187 | |