1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2017 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrMockCaps_DEFINED
9cb93a386Sopenharmony_ci#define GrMockCaps_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/gpu/mock/GrMockTypes.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrCaps.h"
13cb93a386Sopenharmony_ci#include "src/gpu/SkGr.h"
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ciclass GrMockCaps : public GrCaps {
16cb93a386Sopenharmony_cipublic:
17cb93a386Sopenharmony_ci    GrMockCaps(const GrContextOptions& contextOptions, const GrMockOptions& options)
18cb93a386Sopenharmony_ci            : INHERITED(contextOptions), fOptions(options) {
19cb93a386Sopenharmony_ci        fMipmapSupport = options.fMipmapSupport;
20cb93a386Sopenharmony_ci        fDrawInstancedSupport = options.fDrawInstancedSupport;
21cb93a386Sopenharmony_ci        fHalfFloatVertexAttributeSupport = options.fHalfFloatVertexAttributeSupport;
22cb93a386Sopenharmony_ci        fMapBufferFlags = options.fMapBufferFlags;
23cb93a386Sopenharmony_ci        fBufferMapThreshold = SK_MaxS32; // Overridable in GrContextOptions.
24cb93a386Sopenharmony_ci        fMaxTextureSize = options.fMaxTextureSize;
25cb93a386Sopenharmony_ci        fMaxWindowRectangles = options.fMaxWindowRectangles;
26cb93a386Sopenharmony_ci        fMaxRenderTargetSize = std::min(options.fMaxRenderTargetSize, fMaxTextureSize);
27cb93a386Sopenharmony_ci        fMaxPreferredRenderTargetSize = fMaxRenderTargetSize;
28cb93a386Sopenharmony_ci        fMaxVertexAttributes = options.fMaxVertexAttributes;
29cb93a386Sopenharmony_ci        fSampleLocationsSupport = true;
30cb93a386Sopenharmony_ci
31cb93a386Sopenharmony_ci        fShaderCaps = std::make_unique<GrShaderCaps>();
32cb93a386Sopenharmony_ci        fShaderCaps->fIntegerSupport = options.fIntegerSupport;
33cb93a386Sopenharmony_ci        fShaderCaps->fFlatInterpolationSupport = options.fFlatInterpolationSupport;
34cb93a386Sopenharmony_ci        fShaderCaps->fMaxFragmentSamplers = options.fMaxFragmentSamplers;
35cb93a386Sopenharmony_ci        fShaderCaps->fShaderDerivativeSupport = options.fShaderDerivativeSupport;
36cb93a386Sopenharmony_ci        fShaderCaps->fDualSourceBlendingSupport = options.fDualSourceBlendingSupport;
37cb93a386Sopenharmony_ci        fShaderCaps->fSampleMaskSupport = true;
38cb93a386Sopenharmony_ci        fShaderCaps->fMaxTessellationSegments = options.fMaxTessellationSegments;
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci        this->finishInitialization(contextOptions);
41cb93a386Sopenharmony_ci    }
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ci    bool isFormatSRGB(const GrBackendFormat& format) const override {
44cb93a386Sopenharmony_ci        SkImage::CompressionType compression = format.asMockCompressionType();
45cb93a386Sopenharmony_ci        if (compression != SkImage::CompressionType::kNone) {
46cb93a386Sopenharmony_ci            return false;
47cb93a386Sopenharmony_ci        }
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci        auto ct = format.asMockColorType();
50cb93a386Sopenharmony_ci        return GrGetColorTypeDesc(ct).encoding() == GrColorTypeEncoding::kSRGBUnorm;
51cb93a386Sopenharmony_ci    }
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    bool isFormatTexturable(const GrBackendFormat& format, GrTextureType) const override {
54cb93a386Sopenharmony_ci        SkImage::CompressionType compression = format.asMockCompressionType();
55cb93a386Sopenharmony_ci        if (compression != SkImage::CompressionType::kNone) {
56cb93a386Sopenharmony_ci            return fOptions.fCompressedOptions[(int)compression].fTexturable;
57cb93a386Sopenharmony_ci        }
58cb93a386Sopenharmony_ci
59cb93a386Sopenharmony_ci        auto index = static_cast<int>(format.asMockColorType());
60cb93a386Sopenharmony_ci        return fOptions.fConfigOptions[index].fTexturable;
61cb93a386Sopenharmony_ci    }
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci    bool isFormatCopyable(const GrBackendFormat& format) const override {
64cb93a386Sopenharmony_ci        return false;
65cb93a386Sopenharmony_ci    }
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    bool isFormatAsColorTypeRenderable(GrColorType ct, const GrBackendFormat& format,
68cb93a386Sopenharmony_ci                                       int sampleCount = 1) const override {
69cb93a386Sopenharmony_ci        // Currently we don't allow RGB_888X to be renderable because we don't have a way to
70cb93a386Sopenharmony_ci        // handle blends that reference dst alpha when the values in the dst alpha channel are
71cb93a386Sopenharmony_ci        // uninitialized.
72cb93a386Sopenharmony_ci        if (ct == GrColorType::kRGB_888x) {
73cb93a386Sopenharmony_ci            return false;
74cb93a386Sopenharmony_ci        }
75cb93a386Sopenharmony_ci        return this->isFormatRenderable(format, sampleCount);
76cb93a386Sopenharmony_ci    }
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci    bool isFormatRenderable(const GrBackendFormat& format, int sampleCount) const override {
79cb93a386Sopenharmony_ci        if (format.asMockCompressionType() != SkImage::CompressionType::kNone) {
80cb93a386Sopenharmony_ci            return false;  // compressed formats are never renderable
81cb93a386Sopenharmony_ci        }
82cb93a386Sopenharmony_ci
83cb93a386Sopenharmony_ci        return sampleCount <= this->maxRenderTargetSampleCount(format.asMockColorType());
84cb93a386Sopenharmony_ci    }
85cb93a386Sopenharmony_ci
86cb93a386Sopenharmony_ci    int getRenderTargetSampleCount(int requestCount, GrColorType) const;
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    int getRenderTargetSampleCount(int requestCount,
89cb93a386Sopenharmony_ci                                   const GrBackendFormat& format) const override {
90cb93a386Sopenharmony_ci        SkImage::CompressionType compression = format.asMockCompressionType();
91cb93a386Sopenharmony_ci        if (compression != SkImage::CompressionType::kNone) {
92cb93a386Sopenharmony_ci            return 0; // no compressed format is renderable
93cb93a386Sopenharmony_ci        }
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci        return this->getRenderTargetSampleCount(requestCount, format.asMockColorType());
96cb93a386Sopenharmony_ci    }
97cb93a386Sopenharmony_ci
98cb93a386Sopenharmony_ci    int maxRenderTargetSampleCount(GrColorType ct) const {
99cb93a386Sopenharmony_ci        switch (fOptions.fConfigOptions[(int)ct].fRenderability) {
100cb93a386Sopenharmony_ci            case GrMockOptions::ConfigOptions::Renderability::kNo:
101cb93a386Sopenharmony_ci                return 0;
102cb93a386Sopenharmony_ci            case GrMockOptions::ConfigOptions::Renderability::kNonMSAA:
103cb93a386Sopenharmony_ci                return 1;
104cb93a386Sopenharmony_ci            case GrMockOptions::ConfigOptions::Renderability::kMSAA:
105cb93a386Sopenharmony_ci                return kMaxSampleCnt;
106cb93a386Sopenharmony_ci        }
107cb93a386Sopenharmony_ci        return 0;
108cb93a386Sopenharmony_ci    }
109cb93a386Sopenharmony_ci
110cb93a386Sopenharmony_ci    int maxRenderTargetSampleCount(const GrBackendFormat& format) const override {
111cb93a386Sopenharmony_ci        SkImage::CompressionType compression = format.asMockCompressionType();
112cb93a386Sopenharmony_ci        if (compression != SkImage::CompressionType::kNone) {
113cb93a386Sopenharmony_ci            return 0; // no compressed format is renderable
114cb93a386Sopenharmony_ci        }
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ci        return this->maxRenderTargetSampleCount(format.asMockColorType());
117cb93a386Sopenharmony_ci    }
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    SupportedWrite supportedWritePixelsColorType(GrColorType surfaceColorType,
120cb93a386Sopenharmony_ci                                                 const GrBackendFormat& surfaceFormat,
121cb93a386Sopenharmony_ci                                                 GrColorType srcColorType) const override {
122cb93a386Sopenharmony_ci        return {surfaceColorType, 1};
123cb93a386Sopenharmony_ci    }
124cb93a386Sopenharmony_ci
125cb93a386Sopenharmony_ci    SurfaceReadPixelsSupport surfaceSupportsReadPixels(const GrSurface*) const override {
126cb93a386Sopenharmony_ci        return SurfaceReadPixelsSupport::kSupported;
127cb93a386Sopenharmony_ci    }
128cb93a386Sopenharmony_ci
129cb93a386Sopenharmony_ci    GrBackendFormat getBackendFormatFromCompressionType(SkImage::CompressionType) const override {
130cb93a386Sopenharmony_ci        return {};
131cb93a386Sopenharmony_ci    }
132cb93a386Sopenharmony_ci
133cb93a386Sopenharmony_ci    GrSwizzle getWriteSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
134cb93a386Sopenharmony_ci        SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
135cb93a386Sopenharmony_ci        return GrSwizzle("rgba");
136cb93a386Sopenharmony_ci    }
137cb93a386Sopenharmony_ci
138cb93a386Sopenharmony_ci    uint64_t computeFormatKey(const GrBackendFormat&) const override;
139cb93a386Sopenharmony_ci
140cb93a386Sopenharmony_ci    GrProgramDesc makeDesc(GrRenderTarget*,
141cb93a386Sopenharmony_ci                           const GrProgramInfo&,
142cb93a386Sopenharmony_ci                           ProgramDescOverrideFlags) const override;
143cb93a386Sopenharmony_ci
144cb93a386Sopenharmony_ci#if GR_TEST_UTILS
145cb93a386Sopenharmony_ci    std::vector<GrCaps::TestFormatColorTypeCombination> getTestingCombinations() const override;
146cb93a386Sopenharmony_ci#endif
147cb93a386Sopenharmony_ci
148cb93a386Sopenharmony_ciprivate:
149cb93a386Sopenharmony_ci    bool onSurfaceSupportsWritePixels(const GrSurface*) const override { return true; }
150cb93a386Sopenharmony_ci    bool onCanCopySurface(const GrSurfaceProxy* dst, const GrSurfaceProxy* src,
151cb93a386Sopenharmony_ci                          const SkIRect& srcRect, const SkIPoint& dstPoint) const override {
152cb93a386Sopenharmony_ci        return true;
153cb93a386Sopenharmony_ci    }
154cb93a386Sopenharmony_ci    GrBackendFormat onGetDefaultBackendFormat(GrColorType ct) const override {
155cb93a386Sopenharmony_ci        return GrBackendFormat::MakeMock(ct, SkImage::CompressionType::kNone);
156cb93a386Sopenharmony_ci    }
157cb93a386Sopenharmony_ci
158cb93a386Sopenharmony_ci    bool onAreColorTypeAndFormatCompatible(GrColorType ct,
159cb93a386Sopenharmony_ci                                           const GrBackendFormat& format) const override {
160cb93a386Sopenharmony_ci        if (ct == GrColorType::kUnknown) {
161cb93a386Sopenharmony_ci            return false;
162cb93a386Sopenharmony_ci        }
163cb93a386Sopenharmony_ci
164cb93a386Sopenharmony_ci        SkImage::CompressionType compression = format.asMockCompressionType();
165cb93a386Sopenharmony_ci        if (compression == SkImage::CompressionType::kETC2_RGB8_UNORM ||
166cb93a386Sopenharmony_ci            compression == SkImage::CompressionType::kBC1_RGB8_UNORM) {
167cb93a386Sopenharmony_ci            return ct == GrColorType::kRGB_888x; // TODO: this may be too restrictive
168cb93a386Sopenharmony_ci        }
169cb93a386Sopenharmony_ci        if (compression == SkImage::CompressionType::kBC1_RGBA8_UNORM) {
170cb93a386Sopenharmony_ci            return ct == GrColorType::kRGBA_8888;
171cb93a386Sopenharmony_ci        }
172cb93a386Sopenharmony_ci
173cb93a386Sopenharmony_ci        return ct == format.asMockColorType();
174cb93a386Sopenharmony_ci    }
175cb93a386Sopenharmony_ci
176cb93a386Sopenharmony_ci    SupportedRead onSupportedReadPixelsColorType(GrColorType srcColorType, const GrBackendFormat&,
177cb93a386Sopenharmony_ci                                                 GrColorType) const override {
178cb93a386Sopenharmony_ci        return SupportedRead{srcColorType, 1};
179cb93a386Sopenharmony_ci    }
180cb93a386Sopenharmony_ci
181cb93a386Sopenharmony_ci    GrSwizzle onGetReadSwizzle(const GrBackendFormat& format, GrColorType ct) const override {
182cb93a386Sopenharmony_ci        SkASSERT(this->areColorTypeAndFormatCompatible(ct, format));
183cb93a386Sopenharmony_ci        return GrSwizzle("rgba");
184cb93a386Sopenharmony_ci    }
185cb93a386Sopenharmony_ci
186cb93a386Sopenharmony_ci    static const int kMaxSampleCnt = 16;
187cb93a386Sopenharmony_ci
188cb93a386Sopenharmony_ci    GrMockOptions fOptions;
189cb93a386Sopenharmony_ci    using INHERITED = GrCaps;
190cb93a386Sopenharmony_ci};
191cb93a386Sopenharmony_ci
192cb93a386Sopenharmony_ci#endif
193