blob: 34232d61182413e6c783a7a8d913c3a207be4f95 [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 Phillipsb7bfbc22020-07-01 12:55:01 -040010#include "include/gpu/GrDirectContext.h"
11#include "include/gpu/GrRecordingContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrDrawingManager.h"
14#include "src/gpu/GrProxyProvider.h"
15#include "src/gpu/GrRecordingContextPriv.h"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040016#include "src/gpu/GrResourceProvider.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050017#include "src/gpu/GrSurfaceDrawContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040018#include "src/gpu/GrSurfaceProxy.h"
Chris Dalton4ece96d2019-08-30 11:26:39 -060019#include "src/gpu/GrTextureResolveRenderTask.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040020
John Stiles0fbc6a32021-06-04 14:40:57 -040021std::unique_ptr<GrSurfaceDrawContext> GrOnFlushResourceProvider::makeSurfaceDrawContext(
Greg Danield11ae2e2020-02-10 16:36:07 -050022 sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrColorType colorType,
Chris Daltonf5b87f92021-04-19 17:27:09 -060023 sk_sp<SkColorSpace> colorSpace, const SkSurfaceProps& props) {
Chris Dalton4c458b12018-06-16 17:22:59 -060024 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
Robert Phillipsaee18c92019-09-06 11:48:27 -040025 // we have to manually ensure it is allocated here.
Chris Dalton4c458b12018-06-16 17:22:59 -060026 if (!this->instatiateProxy(proxy.get())) {
27 return nullptr;
28 }
29
Greg Danielba0ff782020-01-07 15:42:57 -050030 auto context = fDrawingMgr->getContext();
31
32 if (!proxy->asRenderTargetProxy()) {
33 return nullptr;
34 }
35
Robert Phillips779125d2021-05-24 13:14:39 -040036 auto sdc = GrSurfaceDrawContext::Make(context, colorType, std::move(proxy),
37 std::move(colorSpace), origin, props, true);
Robert Phillips1119dc32017-04-11 12:54:57 -040038
Robert Phillips779125d2021-05-24 13:14:39 -040039 if (!sdc) {
Robert Phillips1119dc32017-04-11 12:54:57 -040040 return nullptr;
41 }
42
Robert Phillips779125d2021-05-24 13:14:39 -040043 sdc->discard();
Robert Phillips1119dc32017-04-11 12:54:57 -040044
Robert Phillips779125d2021-05-24 13:14:39 -040045 return sdc;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040046}
47
Chris Dalton4ece96d2019-08-30 11:26:39 -060048void GrOnFlushResourceProvider::addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,
49 GrSurfaceProxy::ResolveFlags resolveFlags) {
50 // Since we are bypassing normal DAG operation, we need to ensure the textureProxy's last render
51 // task gets closed before making a texture resolve task. makeClosed is what will mark msaa and
52 // mipmaps dirty.
Adlai Hollerd71b7b02020-06-08 15:55:00 -040053 if (GrRenderTask* renderTask = fDrawingMgr->getLastRenderTask(textureProxy.get())) {
Chris Daltonaa938ce2021-06-23 18:13:59 -060054 renderTask->makeClosed(fDrawingMgr->getContext());
Chris Dalton4ece96d2019-08-30 11:26:39 -060055 }
56 auto task = static_cast<GrTextureResolveRenderTask*>(fDrawingMgr->fOnFlushRenderTasks.push_back(
Chris Daltone2a903e2019-09-18 13:41:50 -060057 sk_make_sp<GrTextureResolveRenderTask>()).get());
Adlai Hollerd71b7b02020-06-08 15:55:00 -040058 task->addProxy(fDrawingMgr, std::move(textureProxy), resolveFlags, *this->caps());
Chris Daltonaa938ce2021-06-23 18:13:59 -060059 task->makeClosed(fDrawingMgr->getContext());
Chris Dalton4ece96d2019-08-30 11:26:39 -060060}
61
Chris Dalton50edafa2018-05-17 21:45:25 -060062bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
63 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050064 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060065 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
66}
67
Chris Dalton2de13dd2019-01-03 15:11:59 -070068void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050069 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070070 proxyProvider->removeUniqueKeyFromProxy(proxy);
71}
72
73void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050074 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070075 proxyProvider->processInvalidUniqueKey(key, nullptr,
76 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060077}
78
79sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
Brian Salomonbeb7f522019-08-30 16:19:42 -040080 const GrUniqueKey& key,
Brian Salomonbeb7f522019-08-30 16:19:42 -040081 UseAllocator useAllocator) {
Robert Phillips9da87e02019-02-04 13:26:26 -050082 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Brian Salomondf1bd6d2020-03-26 20:37:01 -040083 return proxyProvider->findOrCreateProxyByUniqueKey(key, useAllocator);
Chris Dalton50edafa2018-05-17 21:45:25 -060084}
85
Robert Phillipscd5099c2018-02-09 09:56:56 -050086bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040087 SkASSERT(proxy->canSkipResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040088
Robert Phillips6a6de562019-02-15 15:19:15 -050089 // TODO: this class should probably just get a GrDirectContext
Robert Phillipsf8f45d92020-07-01 11:11:18 -040090 auto direct = fDrawingMgr->getContext()->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -050091 if (!direct) {
92 return false;
93 }
94
95 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050096
Brian Salomonbeb7f522019-08-30 16:19:42 -040097 if (proxy->isLazy()) {
Robert Phillips7d79e7b2018-02-14 11:09:57 -050098 return proxy->priv().doLazyInstantiation(resourceProvider);
99 }
100
Robert Phillipscd5099c2018-02-09 09:56:56 -0500101 return proxy->instantiate(resourceProvider);
102}
103
Brian Salomondbf70722019-02-07 11:31:24 -0500104sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
105 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500106 // TODO: this class should probably just get a GrDirectContext
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400107 auto direct = fDrawingMgr->getContext()->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500108 if (!direct) {
109 return nullptr;
110 }
111
112 auto resourceProvider = direct->priv().resourceProvider();
113
Brian Salomondbf70722019-02-07 11:31:24 -0500114 return sk_sp<GrGpuBuffer>(
115 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -0700116}
117
Brian Salomondbf70722019-02-07 11:31:24 -0500118sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500119 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500120 // TODO: class should probably just get a GrDirectContext
Robert Phillipsf8f45d92020-07-01 11:11:18 -0400121 auto direct = fDrawingMgr->getContext()->asDirectContext();
Robert Phillips6a6de562019-02-15 15:19:15 -0500122 if (!direct) {
123 return nullptr;
124 }
125
126 auto resourceProvider = direct->priv().resourceProvider();
127
Robert Phillipsbf8bf832019-08-30 13:13:44 -0400128 return resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Daltone9e91dd2017-07-14 08:48:57 -0600129}
130
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500131uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500132 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400133}
134
Chris Dalton6081ebb2017-06-20 11:35:59 -0700135const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500136 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700137}
Chris Daltona378b452019-12-11 13:24:11 -0500138
Herb Derbyc76d4092020-10-07 16:46:15 -0400139GrRecordingContext* GrOnFlushResourceProvider::recordingContext() const {
140 return fDrawingMgr->getContext();
Chris Dalton4e998532020-02-10 11:06:42 -0700141}
142
Chris Dalton5a5fe792020-02-15 11:41:30 -0700143void GrOnFlushResourceProvider::printWarningMessage(const char* msg) const {
144 fDrawingMgr->getContext()->priv().printWarningMessage(msg);
Chris Daltona378b452019-12-11 13:24:11 -0500145}