blob: 615e0d7c1ce45dc50bcc10ed098879f7b129ac07 [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 Phillips292a6b22019-02-14 14:49:02 -050060sk_sp<GrSurfaceContext> GrRecordingContext::makeWrappedSurfaceContext(
61 sk_sp<GrSurfaceProxy> proxy,
62 sk_sp<SkColorSpace> colorSpace,
63 const SkSurfaceProps* props) {
64 ASSERT_SINGLE_OWNER_PRIV
65
66 if (proxy->asRenderTargetProxy()) {
67 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
68 std::move(colorSpace), props);
69 } else {
70 SkASSERT(proxy->asTextureProxy());
71 SkASSERT(!props);
72 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
73 }
74}
75
76sk_sp<GrSurfaceContext> GrRecordingContext::makeDeferredSurfaceContext(
77 const GrBackendFormat& format,
78 const GrSurfaceDesc& dstDesc,
79 GrSurfaceOrigin origin,
80 GrMipMapped mipMapped,
81 SkBackingFit fit,
82 SkBudgeted isDstBudgeted,
83 sk_sp<SkColorSpace> colorSpace,
84 const SkSurfaceProps* props) {
85 sk_sp<GrTextureProxy> proxy;
86 if (GrMipMapped::kNo == mipMapped) {
87 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
88 } else {
89 SkASSERT(SkBackingFit::kExact == fit);
90 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
91 }
92 if (!proxy) {
93 return nullptr;
94 }
95
96 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
97 std::move(colorSpace),
98 props);
99 if (sContext && sContext->asRenderTargetContext()) {
100 sContext->asRenderTargetContext()->discard();
101 }
102
103 return sContext;
104}
105
Robert Phillipsb97da532019-02-12 15:24:12 -0500106sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContext(
107 const GrBackendFormat& format,
108 SkBackingFit fit,
109 int width, int height,
110 GrPixelConfig config,
111 sk_sp<SkColorSpace> colorSpace,
112 int sampleCnt,
113 GrMipMapped mipMapped,
114 GrSurfaceOrigin origin,
115 const SkSurfaceProps* surfaceProps,
116 SkBudgeted budgeted) {
117 SkASSERT(sampleCnt > 0);
118 if (this->abandoned()) {
119 return nullptr;
120 }
121
122 GrSurfaceDesc desc;
123 desc.fFlags = kRenderTarget_GrSurfaceFlag;
124 desc.fWidth = width;
125 desc.fHeight = height;
126 desc.fConfig = config;
127 desc.fSampleCnt = sampleCnt;
128
129 sk_sp<GrTextureProxy> rtp;
130 if (GrMipMapped::kNo == mipMapped) {
131 rtp = this->proxyProvider()->createProxy(format, desc, origin, fit, budgeted);
132 } else {
133 rtp = this->proxyProvider()->createMipMapProxy(format, desc, origin, budgeted);
134 }
135 if (!rtp) {
136 return nullptr;
137 }
138
139 // CONTEXT TODO: move GrDrawingManager to GrRecordingContext for real
140 auto drawingManager = this->drawingManager();
141
142 sk_sp<GrRenderTargetContext> renderTargetContext =
143 drawingManager->makeRenderTargetContext(std::move(rtp), std::move(colorSpace),
144 surfaceProps);
145 if (!renderTargetContext) {
146 return nullptr;
147 }
148
149 renderTargetContext->discard();
150
151 return renderTargetContext;
152}
Robert Phillipsd6841482019-02-08 10:29:20 -0500153
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500154static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
155 switch (config) {
156 case kAlpha_8_GrPixelConfig:
157 case kAlpha_8_as_Alpha_GrPixelConfig:
158 case kAlpha_8_as_Red_GrPixelConfig:
159 case kRGB_565_GrPixelConfig:
160 case kRGBA_4444_GrPixelConfig:
161 case kBGRA_8888_GrPixelConfig:
162 case kRGBA_1010102_GrPixelConfig:
163 case kRGBA_half_GrPixelConfig:
164 return kRGBA_8888_GrPixelConfig;
165 case kSBGRA_8888_GrPixelConfig:
166 return kSRGBA_8888_GrPixelConfig;
167 case kAlpha_half_GrPixelConfig:
168 case kAlpha_half_as_Red_GrPixelConfig:
169 return kRGBA_half_GrPixelConfig;
170 case kGray_8_GrPixelConfig:
171 case kGray_8_as_Lum_GrPixelConfig:
172 case kGray_8_as_Red_GrPixelConfig:
173 return kRGB_888_GrPixelConfig;
174 default:
175 return kUnknown_GrPixelConfig;
176 }
177}
178
179sk_sp<GrRenderTargetContext> GrRecordingContext::makeDeferredRenderTargetContextWithFallback(
180 const GrBackendFormat& format,
181 SkBackingFit fit,
182 int width, int height,
183 GrPixelConfig config,
184 sk_sp<SkColorSpace> colorSpace,
185 int sampleCnt,
186 GrMipMapped mipMapped,
187 GrSurfaceOrigin origin,
188 const SkSurfaceProps* surfaceProps,
189 SkBudgeted budgeted) {
190 GrBackendFormat localFormat = format;
191 SkASSERT(sampleCnt > 0);
192 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
193 config = GrPixelConfigFallback(config);
194 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
195 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
196 // for now we just convert the fallback pixel config to an SkColorType and then get the
197 // GrBackendFormat from that.
198 SkColorType colorType;
199 if (!GrPixelConfigToColorType(config, &colorType)) {
200 return nullptr;
201 }
202 localFormat = this->caps()->getBackendFormatFromColorType(colorType);
203 }
204
205 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
206 std::move(colorSpace), sampleCnt, mipMapped,
207 origin, surfaceProps, budgeted);
208}
209
Robert Phillipsa41c6852019-02-07 10:44:10 -0500210///////////////////////////////////////////////////////////////////////////////////////////////////
211sk_sp<const GrCaps> GrRecordingContextPriv::refCaps() const {
212 return fContext->refCaps();
213}
214
215sk_sp<GrSkSLFPFactoryCache> GrRecordingContextPriv::fpFactoryCache() {
216 return fContext->fpFactoryCache();
217}
Robert Phillipsd6841482019-02-08 10:29:20 -0500218
219sk_sp<GrOpMemoryPool> GrRecordingContextPriv::refOpMemoryPool() {
220 return fContext->refOpMemoryPool();
221}
Robert Phillipsb97da532019-02-12 15:24:12 -0500222
Robert Phillips292a6b22019-02-14 14:49:02 -0500223sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeWrappedSurfaceContext(
224 sk_sp<GrSurfaceProxy> proxy,
225 sk_sp<SkColorSpace> colorSpace,
226 const SkSurfaceProps* props) {
227 return fContext->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
228}
229
230sk_sp<GrSurfaceContext> GrRecordingContextPriv::makeDeferredSurfaceContext(
231 const GrBackendFormat& format,
232 const GrSurfaceDesc& dstDesc,
233 GrSurfaceOrigin origin,
234 GrMipMapped mipMapped,
235 SkBackingFit fit,
236 SkBudgeted isDstBudgeted,
237 sk_sp<SkColorSpace> colorSpace,
238 const SkSurfaceProps* props) {
239 return fContext->makeDeferredSurfaceContext(format, dstDesc, origin, mipMapped, fit,
240 isDstBudgeted, std::move(colorSpace), props);
241}
242
Robert Phillipsb97da532019-02-12 15:24:12 -0500243sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContext(
244 const GrBackendFormat& format,
245 SkBackingFit fit,
246 int width, int height,
247 GrPixelConfig config,
248 sk_sp<SkColorSpace> colorSpace,
249 int sampleCnt,
250 GrMipMapped mipMapped,
251 GrSurfaceOrigin origin,
252 const SkSurfaceProps* surfaceProps,
253 SkBudgeted budgeted) {
254 return fContext->makeDeferredRenderTargetContext(format, fit, width, height, config,
255 std::move(colorSpace), sampleCnt, mipMapped,
256 origin, surfaceProps, budgeted);
257}
Robert Phillips6f0e02f2019-02-13 11:02:28 -0500258
259sk_sp<GrRenderTargetContext> GrRecordingContextPriv::makeDeferredRenderTargetContextWithFallback(
260 const GrBackendFormat& format,
261 SkBackingFit fit,
262 int width, int height,
263 GrPixelConfig config,
264 sk_sp<SkColorSpace> colorSpace,
265 int sampleCnt,
266 GrMipMapped mipMapped,
267 GrSurfaceOrigin origin,
268 const SkSurfaceProps* surfaceProps,
269 SkBudgeted budgeted) {
270 return fContext->makeDeferredRenderTargetContextWithFallback(format, fit, width, height, config,
271 std::move(colorSpace), sampleCnt,
272 mipMapped, origin, surfaceProps,
273 budgeted);
274}