blob: ebbb68c199e352c217556f108e454285d5f2c146 [file] [log] [blame]
Robert Phillips4217ea72019-01-30 13:08:28 -05001/*
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 Phillipsa41c6852019-02-07 10:44:10 -050010#include "GrCaps.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050011#include "GrDrawingManager.h"
Robert Phillipsd6841482019-02-08 10:29:20 -050012#include "GrMemoryPool.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050013#include "GrProxyProvider.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050014#include "GrRecordingContextPriv.h"
Robert Phillipsb97da532019-02-12 15:24:12 -050015#include "GrRenderTargetContext.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050016#include "GrSkSLFPFactoryCache.h"
Robert Phillips6f0e02f2019-02-13 11:02:28 -050017#include "SkGr.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050018
Robert Phillipsc1541ae2019-02-04 12:05:37 -050019GrRecordingContext::GrRecordingContext(GrBackendApi backend,
20 const GrContextOptions& options,
Robert Phillipsa41c6852019-02-07 10:44:10 -050021 uint32_t contextID)
22 : INHERITED(backend, options, contextID) {
Robert Phillips4217ea72019-01-30 13:08:28 -050023}
24
25GrRecordingContext::~GrRecordingContext() { }
26
Robert Phillipsa9162df2019-02-11 14:12:03 -050027void GrRecordingContext::abandonContext() {
28 INHERITED::abandonContext();
29}
30
Robert Phillipsd6841482019-02-08 10:29:20 -050031sk_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
43GrOpMemoryPool* GrRecordingContext::opMemoryPool() {
44 return this->refOpMemoryPool().get();
45}
46
Robert Phillipsb97da532019-02-12 15:24:12 -050047sk_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 Phillipsd6841482019-02-08 10:29:20 -050094
Robert Phillips6f0e02f2019-02-13 11:02:28 -050095static 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
120sk_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 Phillipsa41c6852019-02-07 10:44:10 -0500151///////////////////////////////////////////////////////////////////////////////////////////////////
152sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
153 return fContext->refCaps();
154}
155
156sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
157 return fContext->fpFactoryCache();
158}
Robert Phillipsd6841482019-02-08 10:29:20 -0500159
160sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
161 return fContext->refOpMemoryPool();
162}
Robert Phillipsb97da532019-02-12 15:24:12 -0500163
164sk_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 Phillips6f0e02f2019-02-13 11:02:28 -0500179
180sk_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}