Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | #include "GrRecordingContext.h" |
| 9 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 10 | #include "GrCaps.h" |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 11 | #include "GrDrawingManager.h" |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 12 | #include "GrMemoryPool.h" |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 14 | #include "GrRecordingContextPriv.h" |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 15 | #include "GrRenderTargetContext.h" |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 16 | #include "GrSkSLFPFactoryCache.h" |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame^] | 17 | #include "SkGr.h" |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 18 | |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 19 | GrRecordingContext::GrRecordingContext(GrBackendApi backend, |
| 20 | const GrContextOptions& options, |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 21 | uint32_t contextID) |
| 22 | : INHERITED(backend, options, contextID) { |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | GrRecordingContext::~GrRecordingContext() { } |
| 26 | |
Robert Phillips | a9162df | 2019-02-11 14:12:03 -0500 | [diff] [blame] | 27 | void GrRecordingContext::abandonContext() { |
| 28 | INHERITED::abandonContext(); |
| 29 | } |
| 30 | |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 31 | sk_sp<GrOpMemoryPool> GrRecordingContext::refOpMemoryPool() { |
| 32 | if (!fOpMemoryPool) { |
| 33 | // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory |
| 34 | // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted |
| 35 | // memory. |
| 36 | fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384)); |
| 37 | } |
| 38 | |
| 39 | SkASSERT(fOpMemoryPool); |
| 40 | return fOpMemoryPool; |
| 41 | } |
| 42 | |
| 43 | GrOpMemoryPool* GrRecordingContext::opMemoryPool() { |
| 44 | return this->refOpMemoryPool().get(); |
| 45 | } |
| 46 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 47 | sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext( |
| 48 | const GrBackendFormat& format, |
| 49 | SkBackingFit fit, |
| 50 | int width, int height, |
| 51 | GrPixelConfig config, |
| 52 | sk_sp<SkColorSpace> colorSpace, |
| 53 | int sampleCnt, |
| 54 | GrMipMapped mipMapped, |
| 55 | GrSurfaceOrigin origin, |
| 56 | const SkSurfaceProps* surfaceProps, |
| 57 | SkBudgeted budgeted) { |
| 58 | SkASSERT(sampleCnt > 0); |
| 59 | if (this->abandoned()) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | GrSurfaceDesc desc; |
| 64 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 65 | desc.fWidth = width; |
| 66 | desc.fHeight = height; |
| 67 | desc.fConfig = config; |
| 68 | desc.fSampleCnt = sampleCnt; |
| 69 | |
| 70 | sk_sp<GrTextureProxy> rtp; |
| 71 | if (GrMipMapped::kNo == mipMapped) { |
| 72 | rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted); |
| 73 | } else { |
| 74 | rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted); |
| 75 | } |
| 76 | if (!rtp) { |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real |
| 81 | auto drawingManager = this->drawingManager(); |
| 82 | |
| 83 | sk_sp<GrRenderTargetContext> renderTargetContext = |
| 84 | drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace), |
| 85 | surfaceProps); |
| 86 | if (!renderTargetContext) { |
| 87 | return nullptr; |
| 88 | } |
| 89 | |
| 90 | renderTargetContext->discard(); |
| 91 | |
| 92 | return renderTargetContext; |
| 93 | } |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 94 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame^] | 95 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
| 96 | switch (config) { |
| 97 | case kAlpha_8_GrPixelConfig: |
| 98 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 99 | case kAlpha_8_as_Red_GrPixelConfig: |
| 100 | case kRGB_565_GrPixelConfig: |
| 101 | case kRGBA_4444_GrPixelConfig: |
| 102 | case kBGRA_8888_GrPixelConfig: |
| 103 | case kRGBA_1010102_GrPixelConfig: |
| 104 | case kRGBA_half_GrPixelConfig: |
| 105 | return kRGBA_8888_GrPixelConfig; |
| 106 | case kSBGRA_8888_GrPixelConfig: |
| 107 | return kSRGBA_8888_GrPixelConfig; |
| 108 | case kAlpha_half_GrPixelConfig: |
| 109 | case kAlpha_half_as_Red_GrPixelConfig: |
| 110 | return kRGBA_half_GrPixelConfig; |
| 111 | case kGray_8_GrPixelConfig: |
| 112 | case kGray_8_as_Lum_GrPixelConfig: |
| 113 | case kGray_8_as_Red_GrPixelConfig: |
| 114 | return kRGB_888_GrPixelConfig; |
| 115 | default: |
| 116 | return kUnknown_GrPixelConfig; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback( |
| 121 | const GrBackendFormat& format, |
| 122 | SkBackingFit fit, |
| 123 | int width, int height, |
| 124 | GrPixelConfig config, |
| 125 | sk_sp<SkColorSpace> colorSpace, |
| 126 | int sampleCnt, |
| 127 | GrMipMapped mipMapped, |
| 128 | GrSurfaceOrigin origin, |
| 129 | const SkSurfaceProps* surfaceProps, |
| 130 | SkBudgeted budgeted) { |
| 131 | GrBackendFormat localFormat = format; |
| 132 | SkASSERT(sampleCnt > 0); |
| 133 | if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
| 134 | config = GrPixelConfigFallback(config); |
| 135 | // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat |
| 136 | // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but |
| 137 | // for now we just convert the fallback pixel config to an SkColorType and then get the |
| 138 | // GrBackendFormat from that. |
| 139 | SkColorType colorType; |
| 140 | if (!GrPixelConfigToColorType(config, &colorType)) { |
| 141 | return nullptr; |
| 142 | } |
| 143 | localFormat = this->caps()->getBackendFormatFromColorType(colorType); |
| 144 | } |
| 145 | |
| 146 | return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config, |
| 147 | std::move(colorSpace), sampleCnt, mipMapped, |
| 148 | origin, surfaceProps, budgeted); |
| 149 | } |
| 150 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 151 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 152 | sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const { |
| 153 | return fContext->refCaps(); |
| 154 | } |
| 155 | |
| 156 | sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() { |
| 157 | return fContext->fpFactoryCache(); |
| 158 | } |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 159 | |
| 160 | sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() { |
| 161 | return fContext->refOpMemoryPool(); |
| 162 | } |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 163 | |
| 164 | sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext( |
| 165 | const GrBackendFormat& format, |
| 166 | SkBackingFit fit, |
| 167 | int width, int height, |
| 168 | GrPixelConfig config, |
| 169 | sk_sp<SkColorSpace> colorSpace, |
| 170 | int sampleCnt, |
| 171 | GrMipMapped mipMapped, |
| 172 | GrSurfaceOrigin origin, |
| 173 | const SkSurfaceProps* surfaceProps, |
| 174 | SkBudgeted budgeted) { |
| 175 | return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config, |
| 176 | std::move(colorSpace), sampleCnt, mipMapped, |
| 177 | origin, surfaceProps, budgeted); |
| 178 | } |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame^] | 179 | |
| 180 | sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback( |
| 181 | const GrBackendFormat& format, |
| 182 | SkBackingFit fit, |
| 183 | int width, int height, |
| 184 | GrPixelConfig config, |
| 185 | sk_sp<SkColorSpace> colorSpace, |
| 186 | int sampleCnt, |
| 187 | GrMipMapped mipMapped, |
| 188 | GrSurfaceOrigin origin, |
| 189 | const SkSurfaceProps* surfaceProps, |
| 190 | SkBudgeted budgeted) { |
| 191 | return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config, |
| 192 | std::move(colorSpace), sampleCnt, |
| 193 | mipMapped, origin, surfaceProps, |
| 194 | budgeted); |
| 195 | } |