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" |
| 17 | |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 18 | #import <Metal/Metal.h> |
| 19 | |
Timothy Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 20 | class GrMtlTexture; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 21 | class GrSemaphore; |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 22 | struct GrMtlBackendContext; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 23 | |
| 24 | class GrMtlGpu : public GrGpu { |
| 25 | public: |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 26 | static sk_sp<GrGpu> Make(GrContext* context, const GrContextOptions& options, |
| 27 | id<MTLDevice> device, id<MTLCommandQueue> queue); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 28 | |
| 29 | ~GrMtlGpu() override {} |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 30 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 31 | const GrMtlCaps& mtlCaps() const { return *fMtlCaps.get(); } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 32 | |
Greg Daniel | 4a081e2 | 2017-08-04 09:34:44 -0400 | [diff] [blame] | 33 | id<MTLDevice> device() const { return fDevice; } |
| 34 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 35 | id<MTLCommandBuffer> commandBuffer() const { return fCmdBuffer; } |
| 36 | |
| 37 | enum SyncQueue { |
| 38 | kForce_SyncQueue, |
| 39 | kSkip_SyncQueue |
| 40 | }; |
| 41 | |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 42 | #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 Phillips | b0e93a2 | 2017-08-29 08:26:54 -0400 | [diff] [blame] | 59 | bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, |
| 60 | GrSurface* src, GrSurfaceOrigin srcOrigin, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 61 | const SkIRect& srcRect, |
Greg Daniel | 55fa647 | 2018-03-16 16:13:10 -0400 | [diff] [blame] | 62 | const SkIPoint& dstPoint, |
| 63 | bool canDiscardOutsideDstRect) override { return false; } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 64 | |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 65 | GrGpuRTCommandBuffer* createCommandBuffer( |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 66 | GrRenderTarget*, GrSurfaceOrigin, |
Greg Daniel | 6682ff2 | 2017-08-24 17:10:47 -0400 | [diff] [blame] | 67 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
| 68 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override { |
| 69 | return nullptr; |
| 70 | } |
| 71 | |
| 72 | GrGpuTextureCommandBuffer* createCommandBuffer(GrTexture*, GrSurfaceOrigin) override { |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 73 | 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 Daniel | 48661b8 | 2018-01-22 16:11:35 -0500 | [diff] [blame] | 83 | 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 Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 88 | sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; } |
| 89 | |
| 90 | private: |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 91 | GrMtlGpu(GrContext* context, const GrContextOptions& options, |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 92 | id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet); |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 93 | |
| 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 Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 99 | const GrMipLevel texels[], int mipLevelCount) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 100 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 101 | sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 102 | |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 103 | sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 104 | int sampleCnt, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 105 | GrWrapOwnership) override; |
Brian Salomon | d17f658 | 2017-07-19 18:28:58 -0400 | [diff] [blame] | 106 | |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 107 | sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 108 | |
| 109 | sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&, |
Timothy Liang | e886e80 | 2018-07-02 16:03:28 -0400 | [diff] [blame] | 110 | int sampleCnt) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 111 | |
| 112 | GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override { |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
Brian Salomon | a694870 | 2018-06-01 15:33:20 -0400 | [diff] [blame] | 116 | 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] | 117 | void* buffer, size_t rowBytes) override; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 118 | |
Brian Salomon | a9b04b9 | 2018-06-01 15:04:28 -0400 | [diff] [blame] | 119 | bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType, |
| 120 | const GrMipLevel[], int) override { |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 124 | bool onTransferPixels(GrTexture*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 125 | int left, int top, int width, int height, |
Brian Salomon | c320b15 | 2018-02-20 14:05:36 -0500 | [diff] [blame] | 126 | GrColorType, GrBuffer*, |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 127 | size_t offset, size_t rowBytes) override { |
| 128 | return false; |
| 129 | } |
| 130 | |
Brian Salomon | 930f939 | 2018-06-20 16:25:26 -0400 | [diff] [blame] | 131 | bool onRegenerateMipMapLevels(GrTexture*) override { return false; } |
| 132 | |
Brian Salomon | 1fabd51 | 2018-02-09 09:54:25 -0500 | [diff] [blame] | 133 | void onResolveRenderTarget(GrRenderTarget* target) override { return; } |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 134 | |
Greg Daniel | 5131678 | 2017-08-02 15:10:09 +0000 | [diff] [blame] | 135 | void onFinishFlush(bool insertedSemaphores) override {} |
| 136 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 137 | // 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 Liang | ff19c8f | 2018-07-11 13:27:21 -0400 | [diff] [blame] | 142 | // 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 Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 146 | GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*, |
| 147 | int width, |
| 148 | int height) override { |
| 149 | return nullptr; |
| 150 | } |
| 151 | |
Robert Phillips | 9521447 | 2017-08-08 18:00:03 -0400 | [diff] [blame] | 152 | void clearStencil(GrRenderTarget* target, int clearValue) override {} |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 153 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 154 | #if GR_TEST_UTILS |
Timothy Liang | 760dbc4 | 2018-07-17 13:28:20 -0400 | [diff] [blame] | 155 | bool createTestingOnlyMtlTextureInfo(GrPixelConfig config, int w, int h, bool texturable, |
| 156 | bool renderable, GrMipMapped mipMapped, |
| 157 | const void* srcData, GrMtlTextureInfo* info); |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 158 | #endif |
Greg Daniel | 26b50a4 | 2018-03-08 09:49:58 -0500 | [diff] [blame] | 159 | |
Greg Daniel | cebcb84 | 2017-07-31 10:45:52 -0400 | [diff] [blame] | 160 | sk_sp<GrMtlCaps> fMtlCaps; |
| 161 | |
Greg Daniel | b76a72a | 2017-07-13 15:07:54 -0400 | [diff] [blame] | 162 | id<MTLDevice> fDevice; |
| 163 | id<MTLCommandQueue> fQueue; |
| 164 | |
Timothy Liang | ef21d7e | 2018-07-02 17:03:30 -0400 | [diff] [blame] | 165 | id<MTLCommandBuffer> fCmdBuffer; |
Greg Daniel | e5ddff5 | 2017-07-05 16:49:36 -0400 | [diff] [blame] | 166 | |
| 167 | typedef GrGpu INHERITED; |
| 168 | }; |
| 169 | |
| 170 | #endif |
| 171 | |