blob: f84f2fe53978a6de35ec66aa9c459b00d8c19a80 [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"
11#include "include/private/GrSurfaceProxy.h"
12#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"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040017
Chris Daltonfe199b72017-05-05 11:26:15 -040018sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Robert Phillipseb35f4d2017-03-21 07:56:47 -040019 sk_sp<GrSurfaceProxy> proxy,
20 sk_sp<SkColorSpace> colorSpace,
21 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
23 // we have to manually ensure it is allocated here. The proxy had best have been created
24 // with the kNoPendingIO flag!
25 if (!this->instatiateProxy(proxy.get())) {
26 return nullptr;
27 }
28
Robert Phillips1119dc32017-04-11 12:54:57 -040029 sk_sp<GrRenderTargetContext> renderTargetContext(
30 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
31 std::move(colorSpace),
Robert Phillips941d1442017-06-14 16:37:02 -040032 props, false));
Robert Phillips1119dc32017-04-11 12:54:57 -040033
34 if (!renderTargetContext) {
35 return nullptr;
36 }
37
38 renderTargetContext->discard();
39
40 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040041}
42
Chris Dalton50edafa2018-05-17 21:45:25 -060043bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
44 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050045 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060046 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
47}
48
Chris Dalton2de13dd2019-01-03 15:11:59 -070049void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050050 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070051 proxyProvider->removeUniqueKeyFromProxy(proxy);
52}
53
54void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050055 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070056 proxyProvider->processInvalidUniqueKey(key, nullptr,
57 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060058}
59
60sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
61 const GrUniqueKey& key, GrSurfaceOrigin origin) {
Robert Phillips9da87e02019-02-04 13:26:26 -050062 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060063 return proxyProvider->findOrCreateProxyByUniqueKey(key, origin);
64}
65
Robert Phillipscd5099c2018-02-09 09:56:56 -050066bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040067 SkASSERT(proxy->priv().ignoredByResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040068
Robert Phillips6a6de562019-02-15 15:19:15 -050069 // TODO: this class should probably just get a GrDirectContext
70 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
71 if (!direct) {
72 return false;
73 }
74
75 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050076
Robert Phillips7d79e7b2018-02-14 11:09:57 -050077 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
Brian Salomon967df202018-12-07 11:15:53 -050078 // DDL TODO: Decide if we ever plan to have these proxies use the GrDeinstantiateTracker
Greg Daniel4684f822018-03-08 15:27:36 -050079 // to support unistantiating them at the end of a flush.
Robert Phillips7d79e7b2018-02-14 11:09:57 -050080 return proxy->priv().doLazyInstantiation(resourceProvider);
81 }
82
Robert Phillipscd5099c2018-02-09 09:56:56 -050083 return proxy->instantiate(resourceProvider);
84}
85
Brian Salomondbf70722019-02-07 11:31:24 -050086sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
87 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -050088 // TODO: this class should probably just get a GrDirectContext
89 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
90 if (!direct) {
91 return nullptr;
92 }
93
94 auto resourceProvider = direct->priv().resourceProvider();
95
Brian Salomondbf70722019-02-07 11:31:24 -050096 return sk_sp<GrGpuBuffer>(
97 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -070098}
99
Brian Salomondbf70722019-02-07 11:31:24 -0500100sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500101 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500102 // TODO: 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 sk_sp<const GrGpuBuffer> buffer =
111 resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700112 // Static buffers should never have pending IO.
Chris Dalton133944a2018-11-16 23:30:29 -0500113 SkASSERT(!buffer || !buffer->resourcePriv().hasPendingIO_debugOnly());
Chris Daltone9e91dd2017-07-14 08:48:57 -0600114 return buffer;
115}
116
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500117uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500118 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400119}
120
Chris Dalton6081ebb2017-06-20 11:35:59 -0700121const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500122 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700123}