blob: eeb107118cd4e858ffbb01803f0ac8d73f4d458d [file] [log] [blame]
robertphillips3dc6ae52015-10-20 09:54:32 -07001/*
2 * Copyright 2015 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
bsalomon5eb41fd2016-09-06 13:49:32 -07008#include "GrDrawingManager.h"
bsalomonb77a9072016-09-07 10:02:04 -07009
10#include "GrContext.h"
Robert Phillips646e4292017-06-13 12:44:56 -040011#include "GrGpu.h"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040012#include "GrOnFlushResourceProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
14#include "GrPathRenderingRenderTargetContext.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040015#include "GrRenderTargetProxy.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070016#include "GrResourceProvider.h"
robertphillips68737822015-10-29 12:12:21 -070017#include "GrSoftwarePathRenderer.h"
bsalomon6a2b1942016-09-08 11:28:59 -070018#include "GrSurfacePriv.h"
Robert Phillips7ee385e2017-03-30 08:02:11 -040019#include "GrSurfaceProxyPriv.h"
Brian Osman45580d32016-11-23 09:37:01 -050020#include "GrTextureContext.h"
21#include "GrTextureOpList.h"
brianosman0e22eb82016-08-30 07:07:59 -070022#include "SkSurface_Gpu.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070023#include "SkTTopoSort.h"
24
Brian Salomondcbb9d92017-07-19 10:53:20 -040025#include "GrTracing.h"
joshualitte8042922015-12-11 06:11:21 -080026#include "text/GrAtlasTextContext.h"
27#include "text/GrStencilAndCoverTextContext.h"
robertphillips498d7ac2015-10-30 10:11:30 -070028
robertphillips3dc6ae52015-10-20 09:54:32 -070029void GrDrawingManager::cleanup() {
Robert Phillipsf2361d22016-10-25 14:20:06 -040030 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillipsee683652017-04-26 11:53:10 -040031 // no opList should receive a new command after this
32 fOpLists[i]->makeClosed(*fContext->caps());
robertphillips0dfa62c2015-11-16 06:23:31 -080033
Robert Phillipsf2361d22016-10-25 14:20:06 -040034 // We shouldn't need to do this, but it turns out some clients still hold onto opLists
Robert Phillips6cdc22c2017-05-11 16:29:14 -040035 // after a cleanup.
36 // MDB TODO: is this still true?
Robert Phillipsf2361d22016-10-25 14:20:06 -040037 fOpLists[i]->reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070038 }
39
Robert Phillipsf2361d22016-10-25 14:20:06 -040040 fOpLists.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070041
robertphillips13391dd2015-10-30 05:15:11 -070042 delete fPathRendererChain;
43 fPathRendererChain = nullptr;
robertphillips68737822015-10-29 12:12:21 -070044 SkSafeSetNull(fSoftwarePathRenderer);
robertphillips3dc6ae52015-10-20 09:54:32 -070045}
46
47GrDrawingManager::~GrDrawingManager() {
48 this->cleanup();
49}
50
51void GrDrawingManager::abandon() {
52 fAbandoned = true;
Robert Phillipsf2361d22016-10-25 14:20:06 -040053 for (int i = 0; i < fOpLists.count(); ++i) {
54 fOpLists[i]->abandonGpuResources();
csmartdaltona7f29642016-07-07 08:49:11 -070055 }
robertphillips3dc6ae52015-10-20 09:54:32 -070056 this->cleanup();
57}
58
robertphillips68737822015-10-29 12:12:21 -070059void GrDrawingManager::freeGpuResources() {
60 // a path renderer may be holding onto resources
robertphillips13391dd2015-10-30 05:15:11 -070061 delete fPathRendererChain;
62 fPathRendererChain = nullptr;
robertphillips68737822015-10-29 12:12:21 -070063 SkSafeSetNull(fSoftwarePathRenderer);
Robert Phillipsf2361d22016-10-25 14:20:06 -040064 for (int i = 0; i < fOpLists.count(); ++i) {
65 fOpLists[i]->freeGpuResources();
csmartdaltona7f29642016-07-07 08:49:11 -070066 }
robertphillips68737822015-10-29 12:12:21 -070067}
68
robertphillips3dc6ae52015-10-20 09:54:32 -070069void GrDrawingManager::reset() {
Robert Phillipsf2361d22016-10-25 14:20:06 -040070 for (int i = 0; i < fOpLists.count(); ++i) {
71 fOpLists[i]->reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070072 }
robertphillipsa13e2022015-11-11 12:01:09 -080073 fFlushState.reset();
robertphillips3dc6ae52015-10-20 09:54:32 -070074}
75
Robert Phillipse3302df2017-04-24 07:31:02 -040076gr_instanced::OpAllocator* GrDrawingManager::instancingAllocator() {
77 if (fInstancingAllocator) {
78 return fInstancingAllocator.get();
79 }
80
81 fInstancingAllocator = fContext->getGpu()->createInstancedRenderingAllocator();
82 return fInstancingAllocator.get();
83}
84
Robert Phillips7ee385e2017-03-30 08:02:11 -040085// MDB TODO: make use of the 'proxy' parameter.
Mike Reed8724b462017-07-22 17:33:48 +000086void GrDrawingManager::internalFlush(GrSurfaceProxy*, GrResourceCache::FlushType type) {
Brian Salomondcbb9d92017-07-19 10:53:20 -040087 GR_CREATE_TRACE_MARKER_CONTEXT("GrDrawingManager", "internalFlush", fContext);
88
robertphillips7761d612016-05-16 09:14:53 -070089 if (fFlushing || this->wasAbandoned()) {
Mike Reed8724b462017-07-22 17:33:48 +000090 return;
joshualittb8918c42015-12-18 09:59:46 -080091 }
92 fFlushing = true;
bsalomondc438982016-08-31 11:53:49 -070093 bool flushed = false;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040094
95 for (int i = 0; i < fOpLists.count(); ++i) {
96 // Semi-usually the GrOpLists are already closed at this point, but sometimes Ganesh
97 // needs to flush mid-draw. In that case, the SkGpuDevice's GrOpLists won't be closed
98 // but need to be flushed anyway. Closing such GrOpLists here will mean new
99 // GrOpLists will be created to replace them if the SkGpuDevice(s) write to them again.
Robert Phillipsee683652017-04-26 11:53:10 -0400100 fOpLists[i]->makeClosed(*fContext->caps());
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400101 }
102
Robert Phillipsa4c93ac2017-05-18 11:40:04 -0400103#ifdef SK_DEBUG
104 // This block checks for any unnecessary splits in the opLists. If two sequential opLists
105 // share the same backing GrSurfaceProxy it means the opList was artificially split.
106 if (fOpLists.count()) {
107 GrRenderTargetOpList* prevOpList = fOpLists[0]->asRenderTargetOpList();
108 for (int i = 1; i < fOpLists.count(); ++i) {
109 GrRenderTargetOpList* curOpList = fOpLists[i]->asRenderTargetOpList();
110
111 if (prevOpList && curOpList) {
112 SkASSERT(prevOpList->fTarget.get() != curOpList->fTarget.get());
113 }
114
115 prevOpList = curOpList;
116 }
117 }
118#endif
119
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400120#ifdef ENABLE_MDB
halcanary9d524f22016-03-29 09:03:52 -0700121 SkDEBUGCODE(bool result =)
Robert Phillipsf2361d22016-10-25 14:20:06 -0400122 SkTTopoSort<GrOpList, GrOpList::TopoSortTraits>(&fOpLists);
robertphillips3dc6ae52015-10-20 09:54:32 -0700123 SkASSERT(result);
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400124#endif
robertphillips3dc6ae52015-10-20 09:54:32 -0700125
Chris Daltonfe199b72017-05-05 11:26:15 -0400126 GrOnFlushResourceProvider onFlushProvider(this);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400127
Chris Daltonfe199b72017-05-05 11:26:15 -0400128 if (!fOnFlushCBObjects.empty()) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400129 // MDB TODO: pre-MDB '1' is the correct pre-allocated size. Post-MDB it will need
130 // to be larger.
131 SkAutoSTArray<1, uint32_t> opListIds(fOpLists.count());
132 for (int i = 0; i < fOpLists.count(); ++i) {
133 opListIds[i] = fOpLists[i]->uniqueID();
134 }
135
136 SkSTArray<1, sk_sp<GrRenderTargetContext>> renderTargetContexts;
Chris Daltonfe199b72017-05-05 11:26:15 -0400137 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
138 onFlushCBObject->preFlush(&onFlushProvider,
139 opListIds.get(), opListIds.count(),
140 &renderTargetContexts);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400141 if (!renderTargetContexts.count()) {
142 continue; // This is fine. No atlases of this type are required for this flush
143 }
144
145 for (int j = 0; j < renderTargetContexts.count(); ++j) {
Robert Phillips2de8cfa2017-06-28 10:33:41 -0400146 GrOpList* opList = renderTargetContexts[j]->getOpList();
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400147 if (!opList) {
148 continue; // Odd - but not a big deal
149 }
Robert Phillipsb6deea82017-05-11 14:14:30 -0400150 opList->makeClosed(*fContext->caps());
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400151 opList->prepareOps(&fFlushState);
152 if (!opList->executeOps(&fFlushState)) {
153 continue; // This is bad
154 }
155 }
156 renderTargetContexts.reset();
157 }
158 }
159
robertphillipsa13e2022015-11-11 12:01:09 -0800160#if 0
Brian Salomon09d994e2016-12-21 11:14:46 -0500161 // Enable this to print out verbose GrOp information
Robert Phillipsf2361d22016-10-25 14:20:06 -0400162 for (int i = 0; i < fOpLists.count(); ++i) {
163 SkDEBUGCODE(fOpLists[i]->dump();)
robertphillips3dc6ae52015-10-20 09:54:32 -0700164 }
robertphillipsa13e2022015-11-11 12:01:09 -0800165#endif
166
Robert Phillips318c4192017-05-17 09:36:38 -0400167 for (int i = 0; i < fOpLists.count(); ++i) {
168 if (!fOpLists[i]->instantiate(fContext->resourceProvider())) {
169 fOpLists[i] = nullptr;
170 continue;
171 }
172
173 fOpLists[i]->prepareOps(&fFlushState);
174 }
175
robertphillipsa13e2022015-11-11 12:01:09 -0800176 // Upload all data to the GPU
177 fFlushState.preIssueDraws();
178
Robert Phillipsf2361d22016-10-25 14:20:06 -0400179 for (int i = 0; i < fOpLists.count(); ++i) {
Robert Phillips318c4192017-05-17 09:36:38 -0400180 if (!fOpLists[i]) {
181 continue;
182 }
183
Brian Salomon1e41f4a2016-12-07 15:05:04 -0500184 if (fOpLists[i]->executeOps(&fFlushState)) {
bsalomondc438982016-08-31 11:53:49 -0700185 flushed = true;
186 }
Robert Phillips18e94842017-05-15 13:06:44 -0400187 fOpLists[i]->reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800188 }
Robert Phillips18e94842017-05-15 13:06:44 -0400189 fOpLists.reset();
robertphillipsa13e2022015-11-11 12:01:09 -0800190
bsalomon342bfc22016-04-01 06:06:20 -0700191 SkASSERT(fFlushState.nextDrawToken() == fFlushState.nextTokenToFlush());
robertphillips3dc6ae52015-10-20 09:54:32 -0700192
Mike Reed8724b462017-07-22 17:33:48 +0000193 fContext->getGpu()->finishFlush();
robertphillipsa13e2022015-11-11 12:01:09 -0800194
robertphillipsa13e2022015-11-11 12:01:09 -0800195 fFlushState.reset();
robertphillipsee843b22016-10-04 05:30:20 -0700196 // We always have to notify the cache when it requested a flush so it can reset its state.
197 if (flushed || type == GrResourceCache::FlushType::kCacheRequested) {
bsalomonb77a9072016-09-07 10:02:04 -0700198 fContext->getResourceCache()->notifyFlushOccurred(type);
199 }
Chris Daltonfe199b72017-05-05 11:26:15 -0400200 for (GrOnFlushCallbackObject* onFlushCBObject : fOnFlushCBObjects) {
201 onFlushCBObject->postFlush();
202 }
joshualittb8918c42015-12-18 09:59:46 -0800203 fFlushing = false;
robertphillips3dc6ae52015-10-20 09:54:32 -0700204}
205
Mike Reed8724b462017-07-22 17:33:48 +0000206void GrDrawingManager::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
bsalomon6a2b1942016-09-08 11:28:59 -0700207 if (this->wasAbandoned()) {
Mike Reed8724b462017-07-22 17:33:48 +0000208 return;
bsalomon6a2b1942016-09-08 11:28:59 -0700209 }
Robert Phillips7ee385e2017-03-30 08:02:11 -0400210 SkASSERT(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700211
Mike Reed8724b462017-07-22 17:33:48 +0000212 if (proxy->priv().hasPendingIO()) {
213 this->flush(proxy);
bsalomon6a2b1942016-09-08 11:28:59 -0700214 }
215
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400216 if (!proxy->instantiate(fContext->resourceProvider())) {
Mike Reed8724b462017-07-22 17:33:48 +0000217 return;
Robert Phillips7ee385e2017-03-30 08:02:11 -0400218 }
219
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400220 GrSurface* surface = proxy->priv().peekSurface();
221
Robert Phillips7ee385e2017-03-30 08:02:11 -0400222 if (fContext->getGpu() && surface->asRenderTarget()) {
223 fContext->getGpu()->resolveRenderTarget(surface->asRenderTarget());
bsalomon6a2b1942016-09-08 11:28:59 -0700224 }
225}
226
Chris Daltonfe199b72017-05-05 11:26:15 -0400227void GrDrawingManager::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
228 fOnFlushCBObjects.push_back(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400229}
230
Robert Phillips941d1442017-06-14 16:37:02 -0400231sk_sp<GrRenderTargetOpList> GrDrawingManager::newRTOpList(GrRenderTargetProxy* rtp,
232 bool managedOpList) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700233 SkASSERT(fContext);
234
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400235 // This is a temporary fix for the partial-MDB world. In that world we're not reordering
236 // so ops that (in the single opList world) would've just glommed onto the end of the single
237 // opList but referred to a far earlier RT need to appear in their own opList.
238 if (!fOpLists.empty()) {
239 fOpLists.back()->makeClosed(*fContext->caps());
robertphillips3dc6ae52015-10-20 09:54:32 -0700240 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700241
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400242 sk_sp<GrRenderTargetOpList> opList(new GrRenderTargetOpList(rtp,
243 fContext->getGpu(),
Robert Phillips8185f592017-04-26 08:31:08 -0400244 fContext->getAuditTrail()));
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400245 SkASSERT(rtp->getLastOpList() == opList.get());
robertphillips3dc6ae52015-10-20 09:54:32 -0700246
Robert Phillips941d1442017-06-14 16:37:02 -0400247 if (managedOpList) {
248 fOpLists.push_back() = opList;
249 }
robertphillips3dc6ae52015-10-20 09:54:32 -0700250
Robert Phillipsf5442bb2017-04-17 14:18:34 -0400251 return opList;
robertphillips3dc6ae52015-10-20 09:54:32 -0700252}
253
Robert Phillipsb6deea82017-05-11 14:14:30 -0400254sk_sp<GrTextureOpList> GrDrawingManager::newTextureOpList(GrTextureProxy* textureProxy) {
Brian Osman45580d32016-11-23 09:37:01 -0500255 SkASSERT(fContext);
256
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400257 // This is a temporary fix for the partial-MDB world. In that world we're not reordering
258 // so ops that (in the single opList world) would've just glommed onto the end of the single
259 // opList but referred to a far earlier RT need to appear in their own opList.
260 if (!fOpLists.empty()) {
261 fOpLists.back()->makeClosed(*fContext->caps());
262 }
263
Robert Phillips5efd5ea2017-05-30 13:47:32 -0400264 sk_sp<GrTextureOpList> opList(new GrTextureOpList(fContext->resourceProvider(),
265 textureProxy,
266 fContext->getAuditTrail()));
Brian Osman45580d32016-11-23 09:37:01 -0500267
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400268 SkASSERT(textureProxy->getLastOpList() == opList.get());
Robert Phillips4a395042017-04-24 16:27:17 +0000269
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400270 fOpLists.push_back() = opList;
271
Robert Phillips4a395042017-04-24 16:27:17 +0000272 return opList;
Brian Osman45580d32016-11-23 09:37:01 -0500273}
274
brianosman86e76262016-08-11 12:17:31 -0700275GrAtlasTextContext* GrDrawingManager::getAtlasTextContext() {
276 if (!fAtlasTextContext) {
277 fAtlasTextContext.reset(GrAtlasTextContext::Create());
278 }
279
280 return fAtlasTextContext.get();
281}
282
robertphillips68737822015-10-29 12:12:21 -0700283/*
284 * This method finds a path renderer that can draw the specified path on
285 * the provided target.
286 * Due to its expense, the software path renderer has split out so it can
287 * can be individually allowed/disallowed via the "allowSW" boolean.
288 */
289GrPathRenderer* GrDrawingManager::getPathRenderer(const GrPathRenderer::CanDrawPathArgs& args,
290 bool allowSW,
291 GrPathRendererChain::DrawType drawType,
292 GrPathRenderer::StencilSupport* stencilSupport) {
293
294 if (!fPathRendererChain) {
bsalomon6b2552f2016-09-15 13:50:26 -0700295 fPathRendererChain = new GrPathRendererChain(fContext, fOptionsForPathRendererChain);
robertphillips68737822015-10-29 12:12:21 -0700296 }
297
298 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(args, drawType, stencilSupport);
299 if (!pr && allowSW) {
300 if (!fSoftwarePathRenderer) {
bsalomon39ef7fb2016-09-21 11:16:05 -0700301 fSoftwarePathRenderer =
Brian Osman32342f02017-03-04 08:12:46 -0500302 new GrSoftwarePathRenderer(fContext->resourceProvider(),
bsalomon39ef7fb2016-09-21 11:16:05 -0700303 fOptionsForPathRendererChain.fAllowPathMaskCaching);
robertphillips68737822015-10-29 12:12:21 -0700304 }
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500305 if (fSoftwarePathRenderer->canDrawPath(args)) {
306 pr = fSoftwarePathRenderer;
307 }
robertphillips68737822015-10-29 12:12:21 -0700308 }
309
310 return pr;
311}
312
Brian Osman11052242016-10-27 14:47:55 -0400313sk_sp<GrRenderTargetContext> GrDrawingManager::makeRenderTargetContext(
Robert Phillips37430132016-11-09 06:50:43 -0500314 sk_sp<GrSurfaceProxy> sProxy,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400315 sk_sp<SkColorSpace> colorSpace,
Robert Phillips941d1442017-06-14 16:37:02 -0400316 const SkSurfaceProps* surfaceProps,
317 bool managedOpList) {
Robert Phillips37430132016-11-09 06:50:43 -0500318 if (this->wasAbandoned() || !sProxy->asRenderTargetProxy()) {
robertphillips3dc6ae52015-10-20 09:54:32 -0700319 return nullptr;
320 }
321
brianosman0e22eb82016-08-30 07:07:59 -0700322 // SkSurface catches bad color space usage at creation. This check handles anything that slips
323 // by, including internal usage. We allow a null color space here, for read/write pixels and
324 // other special code paths. If a color space is provided, though, enforce all other rules.
Robert Phillips2c862492017-01-18 10:08:39 -0500325 if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
brianosmana9c3c6a2016-09-29 10:08:36 -0700326 SkDEBUGFAIL("Invalid config and colorspace combination");
brianosman0e22eb82016-08-30 07:07:59 -0700327 return nullptr;
328 }
joshualitt96880d92016-02-16 10:36:53 -0800329
Robert Phillips2c862492017-01-18 10:08:39 -0500330 sk_sp<GrRenderTargetProxy> rtp(sk_ref_sp(sProxy->asRenderTargetProxy()));
331
joshualitt96880d92016-02-16 10:36:53 -0800332 bool useDIF = false;
333 if (surfaceProps) {
334 useDIF = surfaceProps->isUseDeviceIndependentFonts();
335 }
336
337 if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() &&
Brian Salomon7c8460e2017-05-12 11:36:10 -0400338 GrFSAAType::kNone != rtp->fsaaType()) {
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400339 // TODO: defer stencil buffer attachment for PathRenderingDrawContext
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400340 if (!rtp->instantiate(fContext->resourceProvider())) {
Robert Phillipse60ad622016-11-17 10:22:48 -0500341 return nullptr;
342 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400343 GrRenderTarget* rt = rtp->priv().peekRenderTarget();
344
345 GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt);
joshualitt96880d92016-02-16 10:36:53 -0800346 if (sb) {
Brian Osman11052242016-10-27 14:47:55 -0400347 return sk_sp<GrRenderTargetContext>(new GrPathRenderingRenderTargetContext(
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400348 fContext, this, std::move(rtp),
brianosmandfe4f2e2016-07-21 13:28:36 -0700349 std::move(colorSpace), surfaceProps,
robertphillips6c7e3252016-04-27 10:47:51 -0700350 fContext->getAuditTrail(), fSingleOwner));
joshualitt96880d92016-02-16 10:36:53 -0800351 }
352 }
353
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400354 return sk_sp<GrRenderTargetContext>(new GrRenderTargetContext(fContext, this, std::move(rtp),
Brian Osman11052242016-10-27 14:47:55 -0400355 std::move(colorSpace),
356 surfaceProps,
357 fContext->getAuditTrail(),
Robert Phillips941d1442017-06-14 16:37:02 -0400358 fSingleOwner, managedOpList));
robertphillips3dc6ae52015-10-20 09:54:32 -0700359}
Brian Osman45580d32016-11-23 09:37:01 -0500360
Robert Phillips2c862492017-01-18 10:08:39 -0500361sk_sp<GrTextureContext> GrDrawingManager::makeTextureContext(sk_sp<GrSurfaceProxy> sProxy,
362 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500363 if (this->wasAbandoned() || !sProxy->asTextureProxy()) {
364 return nullptr;
365 }
366
Robert Phillips2c862492017-01-18 10:08:39 -0500367 // SkSurface catches bad color space usage at creation. This check handles anything that slips
368 // by, including internal usage. We allow a null color space here, for read/write pixels and
369 // other special code paths. If a color space is provided, though, enforce all other rules.
370 if (colorSpace && !SkSurface_Gpu::Valid(fContext, sProxy->config(), colorSpace.get())) {
371 SkDEBUGFAIL("Invalid config and colorspace combination");
372 return nullptr;
373 }
374
Brian Osman45580d32016-11-23 09:37:01 -0500375 // GrTextureRenderTargets should always be using GrRenderTargetContext
376 SkASSERT(!sProxy->asRenderTargetProxy());
377
378 sk_sp<GrTextureProxy> textureProxy(sk_ref_sp(sProxy->asTextureProxy()));
379
380 return sk_sp<GrTextureContext>(new GrTextureContext(fContext, this, std::move(textureProxy),
Robert Phillips2c862492017-01-18 10:08:39 -0500381 std::move(colorSpace),
382 fContext->getAuditTrail(),
383 fSingleOwner));
Brian Osman45580d32016-11-23 09:37:01 -0500384}