blob: 20b857eb7f0b1167a93c8636f0715a923c067f57 [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 Phillips292a6b22019-02-14 14:49:02 -050017#include "GrTextureContext.h"
Robert Phillips6f0e02f2019-02-13 11:02:28 -050018#include "SkGr.h"
Robert Phillipsa41c6852019-02-07 10:44:10 -050019
Robert Phillips292a6b22019-02-14 14:49:02 -050020#define ASSERT_SINGLE_OWNER_PRIV \
21 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(this->singleOwner());)
22
Robert Phillipsc1541ae2019-02-04 12:05:37 -050023GrRecordingContext::GrRecordingContext(GrBackendApi backend,
24 const GrContextOptions& options,
Robert Phillipsa41c6852019-02-07 10:44:10 -050025 uint32_t contextID)
26 : INHERITED(backend, options, contextID) {
Robert Phillips4217ea72019-01-30 13:08:28 -050027}
28
29GrRecordingContext::~GrRecordingContext() { }
30
Robert Phillips292a6b22019-02-14 14:49:02 -050031bool 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 Phillipsa9162df2019-02-11 14:12:03 -050040void GrRecordingContext::abandonContext() {
41 INHERITED::abandonContext();
42}
43
Robert Phillipsd6841482019-02-08 10:29:20 -050044sk_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
56GrOpMemoryPool* GrRecordingContext::opMemoryPool() {
57 return this->refOpMemoryPool().get();
58}
59
Robert Phillipsc5058a62019-02-15 12:52:59 -050060void GrRecordingContext::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
61 this->drawingManager()->addOnFlushCallbackObject(onFlushCBObject);
62}
63
Robert Phillips292a6b22019-02-14 14:49:02 -050064sk_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
80sk_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 Phillipsb97da532019-02-12 15:24:12 -0500110sk_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 Phillipsd6841482019-02-08 10:29:20 -0500157
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500158static 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
183sk_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 Phillipsa41c6852019-02-07 10:44:10 -0500214///////////////////////////////////////////////////////////////////////////////////////////////////
215sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
216 return fContext->refCaps();
217}
218
219sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
220 return fContext->fpFactoryCache();
221}
Robert Phillipsd6841482019-02-08 10:29:20 -0500222
223sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
224 return fContext->refOpMemoryPool();
225}
Robert Phillipsb97da532019-02-12 15:24:12 -0500226
Robert Phillipsc5058a62019-02-15 12:52:59 -0500227void GrRecordingContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
228 fContext->addOnFlushCallbackObject(onFlushCBObject);
229}
230
Robert Phillips292a6b22019-02-14 14:49:02 -0500231sk_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
238sk_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 Phillipsb97da532019-02-12 15:24:12 -0500251sk_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 Phillips6f0e02f2019-02-13 11:02:28 -0500266
267sk_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}