blob: b9f425576eaf3c05f78f01606508abe250bf8ad3 [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 Phillipsc994a932018-06-19 13:09:54 -040012#include "GrMemoryPool.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040013#include "GrSurfaceProxy.h"
Brian Osman099fa0f2017-10-02 16:38:32 -040014#include "GrTextureProxyPriv.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040015
Robert Phillipsc589b0b2017-04-17 07:53:07 -040016#include "SkAtomics.h"
17
Robert Phillipsc0138922017-03-08 11:50:55 -050018uint32_t GrOpList::CreateUniqueID() {
19 static int32_t gUniqueID = SK_InvalidUniqueID;
20 uint32_t id;
21 // Loop in case our global wraps around, as we never want to return a 0.
22 do {
23 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
24 } while (id == SK_InvalidUniqueID);
25 return id;
26}
27
Robert Phillipsc994a932018-06-19 13:09:54 -040028GrOpList::GrOpList(GrResourceProvider* resourceProvider, sk_sp<GrOpMemoryPool> opMemoryPool,
Robert Phillips5efd5ea2017-05-30 13:47:32 -040029 GrSurfaceProxy* surfaceProxy, GrAuditTrail* auditTrail)
Robert Phillipsc994a932018-06-19 13:09:54 -040030 : fOpMemoryPool(std::move(opMemoryPool))
31 , fAuditTrail(auditTrail)
32 , fUniqueID(CreateUniqueID())
33 , fFlags(0) {
34 SkASSERT(fOpMemoryPool);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040035 fTarget.setProxy(sk_ref_sp(surfaceProxy), kWrite_GrIOType);
Robert Phillips6cdc22c2017-05-11 16:29:14 -040036 fTarget.get()->setLastOpList(this);
Robert Phillips5efd5ea2017-05-30 13:47:32 -040037
Robert Phillips4150eea2018-02-07 17:08:21 -050038 if (resourceProvider && !resourceProvider->explicitlyAllocateGPUResources()) {
39 // MDB TODO: remove this! We are currently moving to having all the ops that target
40 // the RT as a dest (e.g., clear, etc.) rely on the opList's 'fTarget' pointer
41 // for the IO Ref. This works well but until they are all swapped over (and none
42 // are pre-emptively instantiating proxies themselves) we need to instantiate
43 // here so that the GrSurfaces are created in an order that preserves the GrSurface
44 // re-use assumptions.
45 fTarget.get()->instantiate(resourceProvider);
46 }
47
Robert Phillips5f567c72017-09-14 08:27:37 -040048 fTarget.markPendingIO();
Robert Phillipsf2361d22016-10-25 14:20:06 -040049}
50
51GrOpList::~GrOpList() {
Chris Daltona84cacf2017-10-04 10:30:29 -060052 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
53 // Ensure the target proxy doesn't keep hold of a dangling back pointer.
54 fTarget.get()->setLastOpList(nullptr);
55 }
Robert Phillipsf2361d22016-10-25 14:20:06 -040056}
57
Robert Phillips318c4192017-05-17 09:36:38 -040058bool GrOpList::instantiate(GrResourceProvider* resourceProvider) {
59 return SkToBool(fTarget.get()->instantiate(resourceProvider));
60}
61
Chris Daltona84cacf2017-10-04 10:30:29 -060062void GrOpList::endFlush() {
Robert Phillips6cdc22c2017-05-11 16:29:14 -040063 if (fTarget.get() && this == fTarget.get()->getLastOpList()) {
64 fTarget.get()->setLastOpList(nullptr);
65 }
66
Robert Phillips5efd5ea2017-05-30 13:47:32 -040067 fTarget.reset();
Brian Osman099fa0f2017-10-02 16:38:32 -040068 fDeferredProxies.reset();
Robert Phillips6cdc22c2017-05-11 16:29:14 -040069 fAuditTrail = nullptr;
70}
71
Brian Osman099fa0f2017-10-02 16:38:32 -040072void GrOpList::instantiateDeferredProxies(GrResourceProvider* resourceProvider) {
73 for (int i = 0; i < fDeferredProxies.count(); ++i) {
Robert Phillips4150eea2018-02-07 17:08:21 -050074 if (resourceProvider->explicitlyAllocateGPUResources()) {
75 SkASSERT(fDeferredProxies[i]->priv().isInstantiated());
76 } else {
77 fDeferredProxies[i]->instantiate(resourceProvider);
78 }
Brian Osman099fa0f2017-10-02 16:38:32 -040079 }
Brian Osman5d034742017-09-11 13:38:55 -040080}
81
Brian Osman407b3422017-08-22 15:01:32 -040082void GrOpList::prepare(GrOpFlushState* flushState) {
Brian Osman099fa0f2017-10-02 16:38:32 -040083 for (int i = 0; i < fDeferredProxies.count(); ++i) {
84 fDeferredProxies[i]->texPriv().scheduleUpload(flushState);
Brian Osman407b3422017-08-22 15:01:32 -040085 }
86
87 this->onPrepare(flushState);
88}
89
Robert Phillipsf2361d22016-10-25 14:20:06 -040090// Add a GrOpList-based dependency
91void GrOpList::addDependency(GrOpList* dependedOn) {
92 SkASSERT(!dependedOn->dependsOn(this)); // loops are bad
93
94 if (this->dependsOn(dependedOn)) {
95 return; // don't add duplicate dependencies
96 }
97
Robert Phillips73fd0d62017-09-15 11:48:08 +000098 fDependencies.push_back(dependedOn);
Robert Phillipsf2361d22016-10-25 14:20:06 -040099}
100
101// Convert from a GrSurface-based dependency to a GrOpList one
Robert Phillipsee683652017-04-26 11:53:10 -0400102void GrOpList::addDependency(GrSurfaceProxy* dependedOn, const GrCaps& caps) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103 if (dependedOn->getLastOpList()) {
104 // If it is still receiving dependencies, this GrOpList shouldn't be closed
105 SkASSERT(!this->isClosed());
106
107 GrOpList* opList = dependedOn->getLastOpList();
108 if (opList == this) {
109 // self-read - presumably for dst reads
110 } else {
111 this->addDependency(opList);
112
113 // Can't make it closed in the self-read case
Robert Phillipsee683652017-04-26 11:53:10 -0400114 opList->makeClosed(caps);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400115 }
116 }
Brian Osman099fa0f2017-10-02 16:38:32 -0400117
118 if (GrTextureProxy* textureProxy = dependedOn->asTextureProxy()) {
119 if (textureProxy->texPriv().isDeferred()) {
120 fDeferredProxies.push_back(textureProxy);
121 }
122 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400123}
124
Robert Phillips09dfc472017-09-13 15:25:47 -0400125bool GrOpList::isInstantiated() const {
126 return fTarget.get()->priv().isInstantiated();
127}
128
Robert Phillips4150eea2018-02-07 17:08:21 -0500129#ifdef SK_DEBUG
Robert Phillips27483912018-04-20 12:43:18 -0400130void GrOpList::dump(bool printDependencies) const {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400131 SkDebugf("--------------------------------------------------------------\n");
Robert Phillips27483912018-04-20 12:43:18 -0400132 SkDebugf("opList: %d -> RT: %d\n", fUniqueID, fTarget.get() ? fTarget.get()->uniqueID().asUInt()
133 : -1);
134
135 if (printDependencies) {
136 SkDebugf("relies On (%d): ", fDependencies.count());
137 for (int i = 0; i < fDependencies.count(); ++i) {
138 SkDebugf("%d, ", fDependencies[i]->fUniqueID);
139 }
140 SkDebugf("\n");
Robert Phillipsf2361d22016-10-25 14:20:06 -0400141 }
Robert Phillipsf2361d22016-10-25 14:20:06 -0400142}
143#endif