blob: e6e3c05e7ed4c91526fbc0698a4115916a147d2f [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
Greg Daniel6682ff22017-08-24 17:10:47 -040088 GrGpuRTCommandBuffer* createCommandBuffer(
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
Timothy Liange70604e2018-07-19 09:49:46 -040093 GrGpuTextureCommandBuffer* createCommandBuffer(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
Greg Daniele5ddff52017-07-05 16:49:36 -040097 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 Daniel48661b82018-01-22 16:11:35 -0500104 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 Daniele5ddff52017-07-05 16:49:36 -0400109 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
110
111private:
Greg Danielb76a72a2017-07-13 15:07:54 -0400112 GrMtlGpu(GrContext* context, const GrContextOptions& options,
Greg Danielcebcb842017-07-31 10:45:52 -0400113 id<MTLDevice> device, id<MTLCommandQueue> queue, MTLFeatureSet featureSet);
Greg Daniele5ddff52017-07-05 16:49:36 -0400114
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 Salomon58389b92018-03-07 13:01:25 -0500120 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400121
Timothy Liange886e802018-07-02 16:03:28 -0400122 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400123
Brian Salomond17f6582017-07-19 18:28:58 -0400124 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400125 int sampleCnt,
Timothy Liange886e802018-07-02 16:03:28 -0400126 GrWrapOwnership) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400127
Timothy Liange886e802018-07-02 16:03:28 -0400128 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400129
130 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Timothy Liange886e802018-07-02 16:03:28 -0400131 int sampleCnt) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400132
Timothy Liang49528b62018-08-02 14:18:37 -0400133 GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400134
Brian Salomona6948702018-06-01 15:33:20 -0400135 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
Timothy Liangef21d7e2018-07-02 17:03:30 -0400136 void* buffer, size_t rowBytes) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400137
Brian Salomona9b04b92018-06-01 15:04:28 -0400138 bool onWritePixels(GrSurface*, int left, int top, int width, int height, GrColorType,
Timothy Lianga8046af2018-07-19 09:58:00 -0400139 const GrMipLevel[], int mipLevelCount) override;
Greg Daniele5ddff52017-07-05 16:49:36 -0400140
Brian Salomonc320b152018-02-20 14:05:36 -0500141 bool onTransferPixels(GrTexture*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400142 int left, int top, int width, int height,
Brian Salomonc320b152018-02-20 14:05:36 -0500143 GrColorType, GrBuffer*,
Greg Daniele5ddff52017-07-05 16:49:36 -0400144 size_t offset, size_t rowBytes) override {
145 return false;
146 }
147
Brian Salomon930f9392018-06-20 16:25:26 -0400148 bool onRegenerateMipMapLevels(GrTexture*) override { return false; }
149
Brian Salomon1fabd512018-02-09 09:54:25 -0500150 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
Greg Daniele5ddff52017-07-05 16:49:36 -0400151
Greg Daniel51316782017-08-02 15:10:09 +0000152 void onFinishFlush(bool insertedSemaphores) override {}
153
Timothy Liangff19c8f2018-07-11 13:27:21 -0400154 // 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 Daniele5ddff52017-07-05 16:49:36 -0400158 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
159 int width,
160 int height) override {
161 return nullptr;
162 }
163
Robert Phillips95214472017-08-08 18:00:03 -0400164 void clearStencil(GrRenderTarget* target, int clearValue) override {}
Greg Daniele5ddff52017-07-05 16:49:36 -0400165
Brian Salomonf865b052018-03-09 09:01:53 -0500166#if GR_TEST_UTILS
Timothy Liang760dbc42018-07-17 13:28:20 -0400167 bool createTestingOnlyMtlTextureInfo(GrPixelConfig config, int w, int h, bool texturable,
168 bool renderable, GrMipMapped mipMapped,
169 const void* srcData, GrMtlTextureInfo* info);
Brian Salomonf865b052018-03-09 09:01:53 -0500170#endif
Greg Daniel26b50a42018-03-08 09:49:58 -0500171
Greg Danielcebcb842017-07-31 10:45:52 -0400172 sk_sp<GrMtlCaps> fMtlCaps;
173
Greg Danielb76a72a2017-07-13 15:07:54 -0400174 id<MTLDevice> fDevice;
175 id<MTLCommandQueue> fQueue;
176
Timothy Liangef21d7e2018-07-02 17:03:30 -0400177 id<MTLCommandBuffer> fCmdBuffer;
Greg Daniele5ddff52017-07-05 16:49:36 -0400178
Timothy Liange30739a2018-07-31 10:51:17 -0400179 std::unique_ptr<SkSL::Compiler> fCompiler;
180 GrMtlCopyManager fCopyManager;
181 GrMtlResourceProvider fResourceProvider;
182
Greg Daniele5ddff52017-07-05 16:49:36 -0400183 typedef GrGpu INHERITED;
184};
185
186#endif
187