blob: 6fed34a38ced9681f1e76cfbc78c0d25c378c18c [file] [log] [blame]
Robert Phillipsf2361d22016-10-25 14:20:06 -04001/*
2 * Copyright 2016 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 "GrOpList.h"
Robert Phillips5efd5ea2017-05-30 13:47:32 -04009
10#include "GrContext.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040011#include "GrDeferredProxyUploader.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040012#include "GrSurfaceProxy.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040013#include "GrTextureProxyPriv.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040014
Robert Phillipsc589b0b2017-04-17 07:53:07 -040015#include "SkAtomics.h"
16
Robert Phillipsc0138922017-03-08 11:50:55 -050017uint32_t GrOpList::CreateUniqueID() {
18 static int32_t gUniqueID = SK_InvalidUniqueID;
19 uint32_t id;
20 // Loop in case our global wraps around, as we never want to return a 0.
21 do {
22 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
23 } while (id == SK_InvalidUniqueID);
24 return id;
25}
26
Robert Phillips5efd5ea2017-05-30 13:47:32 -040027GrOpList::GrOpList(GrResourceProvider* resourceProvider,
28 GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail)
Robert Phillips6cdc22c2017-05-11 16:29:14 -040029 : fAuditTrail(auditTrail)
Robert Phillipsdc83b892017-04-13 12:23:54 -040030 , fUniqueID(CreateUniqueID())
31 , fFlags(0) {
Robert Phillips5efd5ea2017-05-30 13:47:32 -040032 fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType);
Robert Phillips6cdc22c2017-05-11 16:29:14 -040033 fTarget.get()->setLastOpList(this);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040034
Robert Phillips4150eea2018-02-07 17:08:21 -050035 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
36 // MDB TODO: remove this! We are currently moving to having all the ops that target
37 // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer
38 // for the IO Ref. This works well but until they are all swapped over (and none
39 // are pre-emptively instantiating proxies themselves) we need to instantiate
40 // here so that the GrSurfaces are created in an order that preserves the GrSurface
41 // re-use assumptions.
42 fTarget.get()->instantiate(resourceProvider);
43 }
44
Robert Phillips5f567c72017-09-14 08:27:37 -040045 fTarget.markPendingIO();
Robert Phillipsf2361d22016-10-25 14:20:06 -040046}
47
48GrOpList::~GrOpList() {
Chris Daltona84cacf2017-10-04 10:30:29 -060049 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
50 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
51 fTarget.get()->setLastOpList(nullptr);
52 }
Robert Phillipsf2361d22016-10-25 14:20:06 -040053}
54
Robert Phillips318c4192017-05-17 09:36:38 -040055bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
56 return SkToBool(fTarget.get()->instantiate(resourceProvider));
57}
58
Chris Daltona84cacf2017-10-04 10:30:29 -060059void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040060 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
61 fTarget.get()->setLastOpList(nullptr);
62 }
63
Robert Phillips5efd5ea2017-05-30 13:47:32 -040064 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040065 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040066 fAuditTrail = nullptr;
67}
68
Brian Osman099fa0f2017-10-02 16:38:32 -040069void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
70 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -050071 if (resourceProvider->explicitlyAllocateGPUResources()) {
72 SkASSERT(fDeferredProxies[i]->priv().isInstantiated());
73 } else {
74 fDeferredProxies[i]->instantiate(resourceProvider);
75 }
Brian Osman099fa0f2017-10-02 16:38:32 -040076 }
Brian Osman5d034742017-09-11 13:38:55 -040077}
78
Brian Osman407b3422017-08-22 15:01:32 -040079void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040080 for (int i = 0; i < fDeferredProxies.count(); ++i) {
81 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040082 }
83
84 this->onPrepare(flushState);
85}
86
Robert Phillipsf2361d22016-10-25 14:20:06 -040087// Add a GrOpList-based dependency
88void GrOpList::addDependency(GrOpList* dependedOn) {
89 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
90
91 if (this->dependsOn(dependedOn)) {
92 return; // don't add duplicate dependencies
93 }
94
Robert Phillips73fd0d62017-09-15 11:48:08 +000095 fDependencies.push_back(dependedOn);
Robert Phillipsf2361d22016-10-25 14:20:06 -040096}
97
98// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -040099void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400100 if (dependedOn->getLastOpList()) {
101 // If it is still receiving dependencies, this GrOpList shouldn't be closed
102 SkASSERT(!this->isClosed());
103
104 GrOpList* opList = dependedOn->getLastOpList();
105 if (opList == this) {
106 // self-read - presumably for dst reads
107 } else {
108 this->addDependency(opList);
109
110 // Can't make it closed in the self-read case
Robert Phillipsee683652017-04-26 11:53:10 -0400111 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400112 }
113 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400114
115 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
116 if (textureProxy->texPriv().isDeferred()) {
117 fDeferredProxies.push_back(textureProxy);
118 }
119 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400120}
121
Robert Phillips09dfc472017-09-13 15:25:47 -0400122bool GrOpList::isInstantiated() const {
123 return fTarget.get()->priv().isInstantiated();
124}
125
Robert Phillips4150eea2018-02-07 17:08:21 -0500126#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400127void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400128 SkDebugf("--------------------------------------------------------------\n");
Robert Phillips27483912018-04-20 12:43:18 -0400129 SkDebugf("opList: %d -> RT: %d\n", fUniqueID, fTarget.get() ? fTarget.get()->uniqueID().asUInt()
130 : -1);
131
132 if (printDependencies) {
133 SkDebugf("relies On (%d): ", fDependencies.count());
134 for (int i = 0; i < fDependencies.count(); ++i) {
135 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
136 }
137 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400138 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400139}
140#endif