blob: 538b0f4aa664c92a76269a92a90c88074c2b7731 [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
Robert Phillips44333c52020-06-30 13:28:00 -040010#include "include/private/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/private/GrRecordingContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrContextPriv.h"
13#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
16#include "src/gpu/GrRenderTargetContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrSurfaceProxy.h"
Chris Dalton4ece96d2019-08-30 11:26:39 -060018#include "src/gpu/GrTextureResolveRenderTask.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040019
Brian Salomonbf6b9792019-08-21 09:38:10 -040020std::unique_ptr<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Greg Danield11ae2e2020-02-10 16:36:07 -050021 sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrColorType colorType,
22 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps* props) {
Chris Dalton4c458b12018-06-16 17:22:59 -060023 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
Robert Phillipsaee18c92019-09-06 11:48:27 -040024 // we have to manually ensure it is allocated here.
Chris Dalton4c458b12018-06-16 17:22:59 -060025 if (!this->instatiateProxy(proxy.get())) {
26 return nullptr;
27 }
28
Greg Danielba0ff782020-01-07 15:42:57 -050029 auto context = fDrawingMgr->getContext();
30
31 if (!proxy->asRenderTargetProxy()) {
32 return nullptr;
33 }
34
Greg Danielba0ff782020-01-07 15:42:57 -050035 auto renderTargetContext = GrRenderTargetContext::Make(
36 context, colorType, std::move(colorSpace), std::move(proxy),
37 origin, props, false);
Robert Phillips1119dc32017-04-11 12:54:57 -040038
39 if (!renderTargetContext) {
40 return nullptr;
41 }
42
43 renderTargetContext->discard();
44
Chris Daltonc4b47352019-08-23 10:10:36 -060045 // FIXME: http://skbug.com/9357: This breaks if the renderTargetContext splits its opsTask.
46 fDrawingMgr->fOnFlushRenderTasks.push_back(sk_ref_sp(renderTargetContext->getOpsTask()));
47
Robert Phillips1119dc32017-04-11 12:54:57 -040048 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040049}
50
Chris Dalton4ece96d2019-08-30 11:26:39 -060051void GrOnFlushResourceProvider::addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,
52 GrSurfaceProxy::ResolveFlags resolveFlags) {
53 // Since we are bypassing normal DAG operation, we need to ensure the textureProxy's last render
54 // task gets closed before making a texture resolve task. makeClosed is what will mark msaa and
55 // mipmaps dirty.
Adlai Hollerd71b7b02020-06-08 15:55:00 -040056 if (GrRenderTask* renderTask = fDrawingMgr->getLastRenderTask(textureProxy.get())) {
Chris Dalton4ece96d2019-08-30 11:26:39 -060057 renderTask->makeClosed(*this->caps());
58 }
59 auto task = static_cast<GrTextureResolveRenderTask*>(fDrawingMgr->fOnFlushRenderTasks.push_back(
Chris Daltone2a903e2019-09-18 13:41:50 -060060 sk_make_sp<GrTextureResolveRenderTask>()).get());
Adlai Hollerd71b7b02020-06-08 15:55:00 -040061 task->addProxy(fDrawingMgr, std::move(textureProxy), resolveFlags, *this->caps());
Chris Daltone2a903e2019-09-18 13:41:50 -060062 task->makeClosed(*this->caps());
Chris Dalton4ece96d2019-08-30 11:26:39 -060063}
64
Chris Dalton50edafa2018-05-17 21:45:25 -060065bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
66 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050067 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060068 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
69}
70
Chris Dalton2de13dd2019-01-03 15:11:59 -070071void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050072 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070073 proxyProvider->removeUniqueKeyFromProxy(proxy);
74}
75
76void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050077 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070078 proxyProvider->processInvalidUniqueKey(key, nullptr,
79 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060080}
81
82sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
Brian Salomonbeb7f522019-08-30 16:19:42 -040083 const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -040084 UseAllocator useAllocator) {
Robert Phillips9da87e02019-02-04 13:26:26 -050085 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Brian Salomondf1bd6d2020-03-26 20:37:01 -040086 return proxyProvider->findOrCreateProxyByUniqueKey(key, useAllocator);
Chris Dalton50edafa2018-05-17 21:45:25 -060087}
88
Robert Phillipscd5099c2018-02-09 09:56:56 -050089bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040090 SkASSERT(proxy->canSkipResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040091
Robert Phillips6a6de562019-02-15 15:19:15 -050092 // TODO: this class should probably just get a GrDirectContext
93 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
94 if (!direct) {
95 return false;
96 }
97
98 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050099
Brian Salomonbeb7f522019-08-30 16:19:42 -0400100 if (proxy->isLazy()) {
Robert Phillips7d79e7b2018-02-14 11:09:57 -0500101 return proxy->priv().doLazyInstantiation(resourceProvider);
102 }
103
Robert Phillipscd5099c2018-02-09 09:56:56 -0500104 return proxy->instantiate(resourceProvider);
105}
106
Brian Salomondbf70722019-02-07 11:31:24 -0500107sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
108 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500109 // TODO: this class should probably just get a GrDirectContext
110 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
111 if (!direct) {
112 return nullptr;
113 }
114
115 auto resourceProvider = direct->priv().resourceProvider();
116
Brian Salomondbf70722019-02-07 11:31:24 -0500117 return sk_sp<GrGpuBuffer>(
118 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -0700119}
120
Brian Salomondbf70722019-02-07 11:31:24 -0500121sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500122 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500123 // TODO: class should probably just get a GrDirectContext
124 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
125 if (!direct) {
126 return nullptr;
127 }
128
129 auto resourceProvider = direct->priv().resourceProvider();
130
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400131 return resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Daltone9e91dd2017-07-14 08:48:57 -0600132}
133
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500134uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500135 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400136}
137
Chris Dalton6081ebb2017-06-20 11:35:59 -0700138const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500139 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700140}
Chris Daltona378b452019-12-11 13:24:11 -0500141
Chris Dalton4e998532020-02-10 11:06:42 -0700142GrOpMemoryPool* GrOnFlushResourceProvider::opMemoryPool() const {
143 return fDrawingMgr->getContext()->priv().opMemoryPool();
144}
145
Chris Dalton5a5fe792020-02-15 11:41:30 -0700146void GrOnFlushResourceProvider::printWarningMessage(const char* msg) const {
147 fDrawingMgr->getContext()->priv().printWarningMessage(msg);
Chris Daltona378b452019-12-11 13:24:11 -0500148}