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