blob: 781df12824c4eeda91ef633d5527482461411816 [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(
Chris Daltonc4b47352019-08-23 10:10:36 -060020 sk_sp<GrSurfaceProxy> proxy, GrColorType colorType, sk_sp<SkColorSpace> colorSpace,
Brian Salomond6287472019-06-24 15:50:07 -040021 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
Brian Salomonbf6b9792019-08-21 09:38:10 -040028 auto renderTargetContext = fDrawingMgr->makeRenderTargetContext(
29 std::move(proxy), colorType, std::move(colorSpace), props, false);
Robert Phillips1119dc32017-04-11 12:54:57 -040030
31 if (!renderTargetContext) {
32 return nullptr;
33 }
34
35 renderTargetContext->discard();
36
Chris Daltonc4b47352019-08-23 10:10:36 -060037 // FIXME: http://skbug.com/9357: This breaks if the renderTargetContext splits its opsTask.
38 fDrawingMgr->fOnFlushRenderTasks.push_back(sk_ref_sp(renderTargetContext->getOpsTask()));
39
Robert Phillips1119dc32017-04-11 12:54:57 -040040 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040041}
42
Chris Dalton4ece96d2019-08-30 11:26:39 -060043void GrOnFlushResourceProvider::addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,
44 GrSurfaceProxy::ResolveFlags resolveFlags) {
45 // Since we are bypassing normal DAG operation, we need to ensure the textureProxy's last render
46 // task gets closed before making a texture resolve task. makeClosed is what will mark msaa and
47 // mipmaps dirty.
48 if (GrRenderTask* renderTask = textureProxy->getLastRenderTask()) {
49 renderTask->makeClosed(*this->caps());
50 }
51 auto task = static_cast<GrTextureResolveRenderTask*>(fDrawingMgr->fOnFlushRenderTasks.push_back(
52 sk_make_sp<GrTextureResolveRenderTask>(std::move(textureProxy), resolveFlags)).get());
53 task->init(*this->caps());
54}
55
Chris Dalton50edafa2018-05-17 21:45:25 -060056bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
57 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050058 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060059 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
60}
61
Chris Dalton2de13dd2019-01-03 15:11:59 -070062void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050063 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070064 proxyProvider->removeUniqueKeyFromProxy(proxy);
65}
66
67void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050068 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070069 proxyProvider->processInvalidUniqueKey(key, nullptr,
70 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060071}
72
73sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
Brian Salomonbeb7f522019-08-30 16:19:42 -040074 const GrUniqueKey& key,
75 GrColorType colorType,
76 GrSurfaceOrigin origin,
77 UseAllocator useAllocator) {
Robert Phillips9da87e02019-02-04 13:26:26 -050078 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Brian Salomonbeb7f522019-08-30 16:19:42 -040079 return proxyProvider->findOrCreateProxyByUniqueKey(key, colorType, origin, useAllocator);
Chris Dalton50edafa2018-05-17 21:45:25 -060080}
81
Robert Phillipscd5099c2018-02-09 09:56:56 -050082bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040083 SkASSERT(proxy->canSkipResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040084
Robert Phillips6a6de562019-02-15 15:19:15 -050085 // TODO: this class should probably just get a GrDirectContext
86 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
87 if (!direct) {
88 return false;
89 }
90
91 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050092
Brian Salomonbeb7f522019-08-30 16:19:42 -040093 if (proxy->isLazy()) {
Robert Phillips7d79e7b2018-02-14 11:09:57 -050094 return proxy->priv().doLazyInstantiation(resourceProvider);
95 }
96
Robert Phillipscd5099c2018-02-09 09:56:56 -050097 return proxy->instantiate(resourceProvider);
98}
99
Brian Salomondbf70722019-02-07 11:31:24 -0500100sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
101 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500102 // TODO: this class should probably just get a GrDirectContext
103 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
104 if (!direct) {
105 return nullptr;
106 }
107
108 auto resourceProvider = direct->priv().resourceProvider();
109
Brian Salomondbf70722019-02-07 11:31:24 -0500110 return sk_sp<GrGpuBuffer>(
111 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -0700112}
113
Brian Salomondbf70722019-02-07 11:31:24 -0500114sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500115 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500116 // TODO: class should probably just get a GrDirectContext
117 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
118 if (!direct) {
119 return nullptr;
120 }
121
122 auto resourceProvider = direct->priv().resourceProvider();
123
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400124 return resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Daltone9e91dd2017-07-14 08:48:57 -0600125}
126
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500127uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500128 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400129}
130
Chris Dalton6081ebb2017-06-20 11:35:59 -0700131const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500132 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700133}