blob: 0d0ffa53e3aeabe9c7ebcd52aa909f22236207dd [file] [log] [blame]
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001/*
2 * Copyright 2017 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrOnFlushResourceProvider.h"
Robert Phillips69893702019-02-22 11:16:30 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrContextPriv.h"
12#include "src/gpu/GrDrawingManager.h"
13#include "src/gpu/GrProxyProvider.h"
14#include "src/gpu/GrRecordingContextPriv.h"
15#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040016#include "src/gpu/GrSurfaceProxy.h"
Chris Dalton4ece96d2019-08-30 11:26:39 -060017#include "src/gpu/GrTextureResolveRenderTask.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040018
Brian Salomonbf6b9792019-08-21 09:38:10 -040019std::unique_ptr<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Greg Danield11ae2e2020-02-10 16:36:07 -050020 sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrColorType colorType,
21 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) {
Chris Dalton4c458b12018-06-16 17:22:59 -060022 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
Robert Phillipsaee18c92019-09-06 11:48:27 -040023 // we have to manually ensure it is allocated here.
Chris Dalton4c458b12018-06-16 17:22:59 -060024 if (!this->instatiateProxy(proxy.get())) {
25 return nullptr;
26 }
27
Greg Danielba0ff782020-01-07 15:42:57 -050028 auto context = fDrawingMgr->getContext();
29
30 if (!proxy->asRenderTargetProxy()) {
31 return nullptr;
32 }
33
Greg Danielba0ff782020-01-07 15:42:57 -050034 auto renderTargetContext = GrRenderTargetContext::Make(
35 context, colorType, std::move(colorSpace), std::move(proxy),
36 origin, props, false);
Robert Phillips1119dc32017-04-11 12:54:57 -040037
38 if (!renderTargetContext) {
39 return nullptr;
40 }
41
42 renderTargetContext->discard();
43
Chris Daltonc4b47352019-08-23 10:10:36 -060044 // FIXME: http://skbug.com/9357: This breaks if the renderTargetContext splits its opsTask.
45 fDrawingMgr->fOnFlushRenderTasks.push_back(sk_ref_sp(renderTargetContext->getOpsTask()));
46
Robert Phillips1119dc32017-04-11 12:54:57 -040047 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040048}
49
Chris Dalton4ece96d2019-08-30 11:26:39 -060050void GrOnFlushResourceProvider::addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,
51 GrSurfaceProxy::ResolveFlags resolveFlags) {
52 // Since we are bypassing normal DAG operation, we need to ensure the textureProxy's last render
53 // task gets closed before making a texture resolve task. makeClosed is what will mark msaa and
54 // mipmaps dirty.
55 if (GrRenderTask* renderTask = textureProxy->getLastRenderTask()) {
56 renderTask->makeClosed(*this->caps());
57 }
58 auto task = static_cast<GrTextureResolveRenderTask*>(fDrawingMgr->fOnFlushRenderTasks.push_back(
Chris Daltone2a903e2019-09-18 13:41:50 -060059 sk_make_sp<GrTextureResolveRenderTask>()).get());
Greg Daniel242536f2020-02-13 14:12:46 -050060 task->addProxy(std::move(textureProxy), resolveFlags, *this->caps());
Chris Daltone2a903e2019-09-18 13:41:50 -060061 task->makeClosed(*this->caps());
Chris Dalton4ece96d2019-08-30 11:26:39 -060062}
63
Chris Dalton50edafa2018-05-17 21:45:25 -060064bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
65 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050066 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060067 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
68}
69
Chris Dalton2de13dd2019-01-03 15:11:59 -070070void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050071 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070072 proxyProvider->removeUniqueKeyFromProxy(proxy);
73}
74
75void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050076 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070077 proxyProvider->processInvalidUniqueKey(key, nullptr,
78 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060079}
80
81sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
Brian Salomonbeb7f522019-08-30 16:19:42 -040082 const GrUniqueKey& key,
83 GrColorType colorType,
84 GrSurfaceOrigin origin,
85 UseAllocator useAllocator) {
Robert Phillips9da87e02019-02-04 13:26:26 -050086 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Brian Salomonbeb7f522019-08-30 16:19:42 -040087 return proxyProvider->findOrCreateProxyByUniqueKey(key, colorType, origin, useAllocator);
Chris Dalton50edafa2018-05-17 21:45:25 -060088}
89
Robert Phillipscd5099c2018-02-09 09:56:56 -050090bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040091 SkASSERT(proxy->canSkipResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040092
Robert Phillips6a6de562019-02-15 15:19:15 -050093 // TODO: this class should probably just get a GrDirectContext
94 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
95 if (!direct) {
96 return false;
97 }
98
99 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -0500100
Brian Salomonbeb7f522019-08-30 16:19:42 -0400101 if (proxy->isLazy()) {
Robert Phillips7d79e7b2018-02-14 11:09:57 -0500102 return proxy->priv().doLazyInstantiation(resourceProvider);
103 }
104
Robert Phillipscd5099c2018-02-09 09:56:56 -0500105 return proxy->instantiate(resourceProvider);
106}
107
Brian Salomondbf70722019-02-07 11:31:24 -0500108sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
109 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500110 // TODO: this class should probably just get a GrDirectContext
111 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
112 if (!direct) {
113 return nullptr;
114 }
115
116 auto resourceProvider = direct->priv().resourceProvider();
117
Brian Salomondbf70722019-02-07 11:31:24 -0500118 return sk_sp<GrGpuBuffer>(
119 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -0700120}
121
Brian Salomondbf70722019-02-07 11:31:24 -0500122sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500123 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500124 // TODO: class should probably just get a GrDirectContext
125 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
126 if (!direct) {
127 return nullptr;
128 }
129
130 auto resourceProvider = direct->priv().resourceProvider();
131
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400132 return resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Daltone9e91dd2017-07-14 08:48:57 -0600133}
134
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500135uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500136 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400137}
138
Chris Dalton6081ebb2017-06-20 11:35:59 -0700139const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500140 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700141}
Chris Daltona378b452019-12-11 13:24:11 -0500142
Chris Dalton4e998532020-02-10 11:06:42 -0700143GrOpMemoryPool* GrOnFlushResourceProvider::opMemoryPool() const {
144 return fDrawingMgr->getContext()->priv().opMemoryPool();
145}
146
Chris Daltona378b452019-12-11 13:24:11 -0500147#if GR_TEST_UTILS
148bool GrOnFlushResourceProvider::testingOnly_getSuppressAllocationWarnings() const {
149 return fDrawingMgr->getContext()->testingOnly_getSuppressAllocationWarnings();
150}
151#endif