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 GrMockGpu_DEFINED 9 #define GrMockGpu_DEFINED 10 11 #include "include/core/SkRect.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkSize.h" 14 #include "include/core/SkTextureCompressionType.h" 15 #include "include/gpu/GrBackendSurface.h" 16 #include "include/gpu/GrTypes.h" 17 #include "include/gpu/mock/GrMockTypes.h" 18 #include "include/private/base/SkAssert.h" 19 #include "include/private/base/SkTArray.h" 20 #include "include/private/gpu/ganesh/GrTypesPriv.h" 21 #include "src/core/SkTHash.h" 22 #include "src/gpu/ganesh/GrAttachment.h" 23 #include "src/gpu/ganesh/GrGpu.h" 24 #include "src/gpu/ganesh/GrOpsRenderPass.h" 25 #include "src/gpu/ganesh/GrSamplerState.h" 26 #include "src/gpu/ganesh/GrSemaphore.h" // IWYU pragma: keep 27 #include "src/gpu/ganesh/GrXferProcessor.h" 28 29 #include <array> 30 #include <cstddef> 31 #include <cstdint> 32 #include <memory> 33 #include <string_view> 34 35 class GrBackendSemaphore; 36 class GrDirectContext; 37 class GrGpuBuffer; 38 class GrProgramDesc; 39 class GrProgramInfo; 40 class GrRenderTarget; 41 class GrSurface; 42 class GrSurfaceProxy; 43 class GrTexture; 44 class GrThreadSafePipelineBuilder; 45 struct GrContextOptions; 46 47 namespace skgpu { 48 class RefCntedCallback; 49 enum class Budgeted : bool; 50 enum class Mipmapped : bool; 51 } 52 53 class GrMockGpu : public GrGpu { 54 public: 55 static std::unique_ptr<GrGpu> Make(const GrMockOptions*, 56 const GrContextOptions&, 57 GrDirectContext*); 58 59 ~GrMockGpu() override; 60 61 GrThreadSafePipelineBuilder* pipelineBuilder() override; 62 sk_sp<GrThreadSafePipelineBuilder> refPipelineBuilder() override; 63 makeSemaphore(bool isOwned)64 [[nodiscard]] std::unique_ptr<GrSemaphore> makeSemaphore(bool isOwned) override { 65 return nullptr; 66 } wrapBackendSemaphore(const GrBackendSemaphore &,GrSemaphoreWrapType,GrWrapOwnership)67 std::unique_ptr<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& /* semaphore */, 68 GrSemaphoreWrapType /* wraptype */, 69 GrWrapOwnership /* ownership */) override { 70 return nullptr; 71 } insertSemaphore(GrSemaphore * semaphore)72 void insertSemaphore(GrSemaphore* semaphore) override {} waitSemaphore(GrSemaphore * semaphore)73 void waitSemaphore(GrSemaphore* semaphore) override {} prepareTextureForCrossContextUsage(GrTexture *)74 std::unique_ptr<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { 75 return nullptr; 76 } 77 78 void submit(GrOpsRenderPass* renderPass) override; 79 checkFinishProcs()80 void checkFinishProcs() override {} finishOutstandingGpuWork()81 void finishOutstandingGpuWork() override {} 82 83 private: 84 GrMockGpu(GrDirectContext*, const GrMockOptions&, const GrContextOptions&); 85 xferBarrier(GrRenderTarget *,GrXferBarrierType)86 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} 87 88 sk_sp<GrTexture> onCreateTexture(SkISize, 89 const GrBackendFormat&, 90 GrRenderable, 91 int renderTargetSampleCnt, 92 skgpu::Budgeted, 93 GrProtected, 94 int mipLevelCount, 95 uint32_t levelClearMask, 96 std::string_view label) override; 97 98 sk_sp<GrTexture> onCreateCompressedTexture(SkISize dimensions, 99 const GrBackendFormat&, 100 skgpu::Budgeted, 101 skgpu::Mipmapped, 102 GrProtected, 103 const void* data, 104 size_t dataSize) override; 105 106 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, 107 GrWrapOwnership, 108 GrWrapCacheable, 109 GrIOType) override; 110 sk_sp<GrTexture> onWrapCompressedBackendTexture(const GrBackendTexture&, 111 GrWrapOwnership, 112 GrWrapCacheable) override; 113 114 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&, 115 int sampleCnt, 116 GrWrapOwnership, 117 GrWrapCacheable) override; 118 119 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override; 120 121 sk_sp<GrGpuBuffer> onCreateBuffer(size_t sizeInBytes, 122 GrGpuBufferType, 123 GrAccessPattern) override; 124 onReadPixels(GrSurface *,SkIRect,GrColorType surfaceColorType,GrColorType dstColorType,void *,size_t rowBytes)125 bool onReadPixels(GrSurface*, 126 SkIRect, 127 GrColorType surfaceColorType, 128 GrColorType dstColorType, 129 void*, 130 size_t rowBytes) override { 131 return true; 132 } 133 onWritePixels(GrSurface *,SkIRect,GrColorType surfaceColorType,GrColorType srcColorType,const GrMipLevel[],int mipLevelCount,bool prepForTexSampling)134 bool onWritePixels(GrSurface*, 135 SkIRect, 136 GrColorType surfaceColorType, 137 GrColorType srcColorType, 138 const GrMipLevel[], 139 int mipLevelCount, 140 bool prepForTexSampling) override { 141 return true; 142 } 143 onTransferFromBufferToBuffer(sk_sp<GrGpuBuffer> src,size_t srcOffset,sk_sp<GrGpuBuffer> dst,size_t dstOffset,size_t size)144 bool onTransferFromBufferToBuffer(sk_sp<GrGpuBuffer> src, 145 size_t srcOffset, 146 sk_sp<GrGpuBuffer> dst, 147 size_t dstOffset, 148 size_t size) override { 149 return true; 150 } 151 onTransferPixelsTo(GrTexture *,SkIRect,GrColorType surfaceColorType,GrColorType bufferColorType,sk_sp<GrGpuBuffer>,size_t offset,size_t rowBytes)152 bool onTransferPixelsTo(GrTexture*, 153 SkIRect, 154 GrColorType surfaceColorType, 155 GrColorType bufferColorType, 156 sk_sp<GrGpuBuffer>, 157 size_t offset, 158 size_t rowBytes) override { 159 return true; 160 } 161 onTransferPixelsFrom(GrSurface *,SkIRect,GrColorType surfaceColorType,GrColorType bufferColorType,sk_sp<GrGpuBuffer>,size_t offset)162 bool onTransferPixelsFrom(GrSurface*, 163 SkIRect, 164 GrColorType surfaceColorType, 165 GrColorType bufferColorType, 166 sk_sp<GrGpuBuffer>, 167 size_t offset) override { 168 return true; 169 } 170 onCopySurface(GrSurface * dst,const SkIRect & dstRect,GrSurface * src,const SkIRect & srcRect,GrSamplerState::Filter)171 bool onCopySurface(GrSurface* dst, const SkIRect& dstRect, 172 GrSurface* src, const SkIRect& srcRect, 173 GrSamplerState::Filter)override { 174 return true; 175 } 176 onRegenerateMipMapLevels(GrTexture *)177 bool onRegenerateMipMapLevels(GrTexture*) override { return true; } 178 onResolveRenderTarget(GrRenderTarget * target,const SkIRect &)179 void onResolveRenderTarget(GrRenderTarget* target, const SkIRect&) override {} 180 addFinishedProc(GrGpuFinishedProc finishedProc,GrGpuFinishedContext finishedContext)181 void addFinishedProc(GrGpuFinishedProc finishedProc, 182 GrGpuFinishedContext finishedContext) override { 183 SkASSERT(finishedProc); 184 finishedProc(finishedContext); 185 } 186 187 GrOpsRenderPass* onGetOpsRenderPass( 188 GrRenderTarget*, 189 bool useMSAASurface, 190 GrAttachment*, 191 GrSurfaceOrigin, 192 const SkIRect&, 193 const GrOpsRenderPass::LoadAndStoreInfo&, 194 const GrOpsRenderPass::StencilLoadAndStoreInfo&, 195 const skia_private::TArray<GrSurfaceProxy*, true>& sampledProxies, 196 GrXferBarrierFlags renderPassXferBarriers) override; 197 onSubmitToGpu(GrSyncCpu)198 bool onSubmitToGpu(GrSyncCpu) override { return true; } 199 200 sk_sp<GrAttachment> makeStencilAttachment(const GrBackendFormat& /*colorFormat*/, 201 SkISize dimensions, int numStencilSamples) override; 202 getPreferredStencilFormat(const GrBackendFormat &)203 GrBackendFormat getPreferredStencilFormat(const GrBackendFormat&) override { 204 return GrBackendFormat::MakeMock(GrColorType::kUnknown, SkTextureCompressionType::kNone, 205 true); 206 } 207 makeMSAAAttachment(SkISize dimensions,const GrBackendFormat & format,int numSamples,GrProtected isProtected,GrMemoryless isMemoryless)208 sk_sp<GrAttachment> makeMSAAAttachment(SkISize dimensions, 209 const GrBackendFormat& format, 210 int numSamples, 211 GrProtected isProtected, 212 GrMemoryless isMemoryless) override { 213 return nullptr; 214 } 215 216 GrBackendTexture onCreateBackendTexture(SkISize dimensions, 217 const GrBackendFormat&, 218 GrRenderable, 219 skgpu::Mipmapped, 220 GrProtected, 221 std::string_view label) override; 222 onClearBackendTexture(const GrBackendTexture &,sk_sp<skgpu::RefCntedCallback> finishedCallback,std::array<float,4> color)223 bool onClearBackendTexture(const GrBackendTexture&, 224 sk_sp<skgpu::RefCntedCallback> finishedCallback, 225 std::array<float, 4> color) override { 226 return true; 227 } 228 229 GrBackendTexture onCreateCompressedBackendTexture(SkISize dimensions, 230 const GrBackendFormat&, 231 skgpu::Mipmapped, 232 GrProtected) override; 233 onUpdateCompressedBackendTexture(const GrBackendTexture &,sk_sp<skgpu::RefCntedCallback> finishedCallback,const void *,size_t)234 bool onUpdateCompressedBackendTexture(const GrBackendTexture&, 235 sk_sp<skgpu::RefCntedCallback> finishedCallback, 236 const void*, 237 size_t) override { 238 return true; 239 } 240 241 void deleteBackendTexture(const GrBackendTexture&) override; 242 compile(const GrProgramDesc &,const GrProgramInfo &)243 bool compile(const GrProgramDesc&, const GrProgramInfo&) override { return false; } 244 245 #if defined(GR_TEST_UTILS) 246 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override; 247 248 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(SkISize dimensions, 249 GrColorType, 250 int sampleCnt, 251 GrProtected) override; 252 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override; 253 #endif 254 255 const GrMockOptions fMockOptions; 256 257 static int NextInternalTextureID(); 258 static int NextExternalTextureID(); 259 static int NextInternalRenderTargetID(); 260 static int NextExternalRenderTargetID(); 261 262 skia_private::THashSet<int> fOutstandingTestingOnlyTextureIDs; 263 264 using INHERITED = GrGpu; 265 }; 266 267 #endif 268