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 | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 17 | #include "GrTextureContext.h" |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 18 | #include "SkGr.h" |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 19 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 20 | #define ASSERT_SINGLE_OWNER_PRIV \ |
| 21 | SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());) |
| 22 | |
Robert Phillips | c1541ae | 2019-02-04 12:05:37 -0500 | [diff] [blame] | 23 | GrRecordingContext::GrRecordingContext(GrBackendApi backend, |
| 24 | const GrContextOptions& options, |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 25 | uint32_t contextID) |
| 26 | : INHERITED(backend, options, contextID) { |
Robert Phillips | 4217ea7 | 2019-01-30 13:08:28 -0500 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | GrRecordingContext::~GrRecordingContext() { } |
| 30 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 31 | bool GrRecordingContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> cache) { |
| 32 | |
| 33 | if (!INHERITED::init(std::move(caps), std::move(cache))) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | return true; |
| 38 | } |
| 39 | |
Robert Phillips | a9162df | 2019-02-11 14:12:03 -0500 | [diff] [blame] | 40 | void GrRecordingContext::abandonContext() { |
| 41 | INHERITED::abandonContext(); |
| 42 | } |
| 43 | |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 44 | sk_sp<GrOpMemoryPool> GrRecordingContext::refOpMemoryPool() { |
| 45 | if (!fOpMemoryPool) { |
| 46 | // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory |
| 47 | // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted |
| 48 | // memory. |
| 49 | fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384)); |
| 50 | } |
| 51 | |
| 52 | SkASSERT(fOpMemoryPool); |
| 53 | return fOpMemoryPool; |
| 54 | } |
| 55 | |
| 56 | GrOpMemoryPool* GrRecordingContext::opMemoryPool() { |
| 57 | return this->refOpMemoryPool().get(); |
| 58 | } |
| 59 | |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame^] | 60 | void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 61 | this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject); |
| 62 | } |
| 63 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 64 | sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext( |
| 65 | sk_sp<GrSurfaceProxy> proxy, |
| 66 | sk_sp<SkColorSpace> colorSpace, |
| 67 | const SkSurfaceProps* props) { |
| 68 | ASSERT_SINGLE_OWNER_PRIV |
| 69 | |
| 70 | if (proxy->asRenderTargetProxy()) { |
| 71 | return this->drawingManager()->makeRenderTargetContext(std::move(proxy), |
| 72 | std::move(colorSpace), props); |
| 73 | } else { |
| 74 | SkASSERT(proxy->asTextureProxy()); |
| 75 | SkASSERT(!props); |
| 76 | return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace)); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext( |
| 81 | const GrBackendFormat& format, |
| 82 | const GrSurfaceDesc& dstDesc, |
| 83 | GrSurfaceOrigin origin, |
| 84 | GrMipMapped mipMapped, |
| 85 | SkBackingFit fit, |
| 86 | SkBudgeted isDstBudgeted, |
| 87 | sk_sp<SkColorSpace> colorSpace, |
| 88 | const SkSurfaceProps* props) { |
| 89 | sk_sp<GrTextureProxy> proxy; |
| 90 | if (GrMipMapped::kNo == mipMapped) { |
| 91 | proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted); |
| 92 | } else { |
| 93 | SkASSERT(SkBackingFit::kExact == fit); |
| 94 | proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted); |
| 95 | } |
| 96 | if (!proxy) { |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy), |
| 101 | std::move(colorSpace), |
| 102 | props); |
| 103 | if (sContext && sContext->asRenderTargetContext()) { |
| 104 | sContext->asRenderTargetContext()->discard(); |
| 105 | } |
| 106 | |
| 107 | return sContext; |
| 108 | } |
| 109 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 110 | sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext( |
| 111 | const GrBackendFormat& format, |
| 112 | SkBackingFit fit, |
| 113 | int width, int height, |
| 114 | GrPixelConfig config, |
| 115 | sk_sp<SkColorSpace> colorSpace, |
| 116 | int sampleCnt, |
| 117 | GrMipMapped mipMapped, |
| 118 | GrSurfaceOrigin origin, |
| 119 | const SkSurfaceProps* surfaceProps, |
| 120 | SkBudgeted budgeted) { |
| 121 | SkASSERT(sampleCnt > 0); |
| 122 | if (this->abandoned()) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | GrSurfaceDesc desc; |
| 127 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 128 | desc.fWidth = width; |
| 129 | desc.fHeight = height; |
| 130 | desc.fConfig = config; |
| 131 | desc.fSampleCnt = sampleCnt; |
| 132 | |
| 133 | sk_sp<GrTextureProxy> rtp; |
| 134 | if (GrMipMapped::kNo == mipMapped) { |
| 135 | rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted); |
| 136 | } else { |
| 137 | rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted); |
| 138 | } |
| 139 | if (!rtp) { |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real |
| 144 | auto drawingManager = this->drawingManager(); |
| 145 | |
| 146 | sk_sp<GrRenderTargetContext> renderTargetContext = |
| 147 | drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace), |
| 148 | surfaceProps); |
| 149 | if (!renderTargetContext) { |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | renderTargetContext->discard(); |
| 154 | |
| 155 | return renderTargetContext; |
| 156 | } |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 157 | |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 158 | static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) { |
| 159 | switch (config) { |
| 160 | case kAlpha_8_GrPixelConfig: |
| 161 | case kAlpha_8_as_Alpha_GrPixelConfig: |
| 162 | case kAlpha_8_as_Red_GrPixelConfig: |
| 163 | case kRGB_565_GrPixelConfig: |
| 164 | case kRGBA_4444_GrPixelConfig: |
| 165 | case kBGRA_8888_GrPixelConfig: |
| 166 | case kRGBA_1010102_GrPixelConfig: |
| 167 | case kRGBA_half_GrPixelConfig: |
| 168 | return kRGBA_8888_GrPixelConfig; |
| 169 | case kSBGRA_8888_GrPixelConfig: |
| 170 | return kSRGBA_8888_GrPixelConfig; |
| 171 | case kAlpha_half_GrPixelConfig: |
| 172 | case kAlpha_half_as_Red_GrPixelConfig: |
| 173 | return kRGBA_half_GrPixelConfig; |
| 174 | case kGray_8_GrPixelConfig: |
| 175 | case kGray_8_as_Lum_GrPixelConfig: |
| 176 | case kGray_8_as_Red_GrPixelConfig: |
| 177 | return kRGB_888_GrPixelConfig; |
| 178 | default: |
| 179 | return kUnknown_GrPixelConfig; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback( |
| 184 | const GrBackendFormat& format, |
| 185 | SkBackingFit fit, |
| 186 | int width, int height, |
| 187 | GrPixelConfig config, |
| 188 | sk_sp<SkColorSpace> colorSpace, |
| 189 | int sampleCnt, |
| 190 | GrMipMapped mipMapped, |
| 191 | GrSurfaceOrigin origin, |
| 192 | const SkSurfaceProps* surfaceProps, |
| 193 | SkBudgeted budgeted) { |
| 194 | GrBackendFormat localFormat = format; |
| 195 | SkASSERT(sampleCnt > 0); |
| 196 | if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) { |
| 197 | config = GrPixelConfigFallback(config); |
| 198 | // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat |
| 199 | // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but |
| 200 | // for now we just convert the fallback pixel config to an SkColorType and then get the |
| 201 | // GrBackendFormat from that. |
| 202 | SkColorType colorType; |
| 203 | if (!GrPixelConfigToColorType(config, &colorType)) { |
| 204 | return nullptr; |
| 205 | } |
| 206 | localFormat = this->caps()->getBackendFormatFromColorType(colorType); |
| 207 | } |
| 208 | |
| 209 | return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config, |
| 210 | std::move(colorSpace), sampleCnt, mipMapped, |
| 211 | origin, surfaceProps, budgeted); |
| 212 | } |
| 213 | |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 214 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 215 | sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const { |
| 216 | return fContext->refCaps(); |
| 217 | } |
| 218 | |
| 219 | sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() { |
| 220 | return fContext->fpFactoryCache(); |
| 221 | } |
Robert Phillips | d684148 | 2019-02-08 10:29:20 -0500 | [diff] [blame] | 222 | |
| 223 | sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() { |
| 224 | return fContext->refOpMemoryPool(); |
| 225 | } |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 226 | |
Robert Phillips | c5058a6 | 2019-02-15 12:52:59 -0500 | [diff] [blame^] | 227 | void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) { |
| 228 | fContext->addOnFlushCallbackObject(onFlushCBObject); |
| 229 | } |
| 230 | |
Robert Phillips | 292a6b2 | 2019-02-14 14:49:02 -0500 | [diff] [blame] | 231 | sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext( |
| 232 | sk_sp<GrSurfaceProxy> proxy, |
| 233 | sk_sp<SkColorSpace> colorSpace, |
| 234 | const SkSurfaceProps* props) { |
| 235 | return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props); |
| 236 | } |
| 237 | |
| 238 | sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext( |
| 239 | const GrBackendFormat& format, |
| 240 | const GrSurfaceDesc& dstDesc, |
| 241 | GrSurfaceOrigin origin, |
| 242 | GrMipMapped mipMapped, |
| 243 | SkBackingFit fit, |
| 244 | SkBudgeted isDstBudgeted, |
| 245 | sk_sp<SkColorSpace> colorSpace, |
| 246 | const SkSurfaceProps* props) { |
| 247 | return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit, |
| 248 | isDstBudgeted, std::move(colorSpace), props); |
| 249 | } |
| 250 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 251 | sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext( |
| 252 | const GrBackendFormat& format, |
| 253 | SkBackingFit fit, |
| 254 | int width, int height, |
| 255 | GrPixelConfig config, |
| 256 | sk_sp<SkColorSpace> colorSpace, |
| 257 | int sampleCnt, |
| 258 | GrMipMapped mipMapped, |
| 259 | GrSurfaceOrigin origin, |
| 260 | const SkSurfaceProps* surfaceProps, |
| 261 | SkBudgeted budgeted) { |
| 262 | return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config, |
| 263 | std::move(colorSpace), sampleCnt, mipMapped, |
| 264 | origin, surfaceProps, budgeted); |
| 265 | } |
Robert Phillips | 6f0e02f | 2019-02-13 11:02:28 -0500 | [diff] [blame] | 266 | |
| 267 | sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback( |
| 268 | const GrBackendFormat& format, |
| 269 | SkBackingFit fit, |
| 270 | int width, int height, |
| 271 | GrPixelConfig config, |
| 272 | sk_sp<SkColorSpace> colorSpace, |
| 273 | int sampleCnt, |
| 274 | GrMipMapped mipMapped, |
| 275 | GrSurfaceOrigin origin, |
| 276 | const SkSurfaceProps* surfaceProps, |
| 277 | SkBudgeted budgeted) { |
| 278 | return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config, |
| 279 | std::move(colorSpace), sampleCnt, |
| 280 | mipMapped, origin, surfaceProps, |
| 281 | budgeted); |
| 282 | } |