blob: ab5dcc90e73e495300eefb210306633964a8c88f [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"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040017
Chris Daltonfe199b72017-05-05 11:26:15 -040018sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Brian Salomond6287472019-06-24 15:50:07 -040019 sk_sp<GrSurfaceProxy> proxy,
20 GrColorType colorType,
21 sk_sp<SkColorSpace> colorSpace,
22 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
24 // we have to manually ensure it is allocated here. The proxy had best have been created
25 // with the kNoPendingIO flag!
26 if (!this->instatiateProxy(proxy.get())) {
27 return nullptr;
28 }
29
Brian Salomond6287472019-06-24 15:50:07 -040030 sk_sp<GrRenderTargetContext> renderTargetContext(fDrawingMgr->makeRenderTargetContext(
31 std::move(proxy), colorType, std::move(colorSpace), props, false));
Robert Phillips1119dc32017-04-11 12:54:57 -040032
33 if (!renderTargetContext) {
34 return nullptr;
35 }
36
37 renderTargetContext->discard();
38
39 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040040}
41
Chris Dalton50edafa2018-05-17 21:45:25 -060042bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
43 GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050044 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060045 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
46}
47
Chris Dalton2de13dd2019-01-03 15:11:59 -070048void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
Robert Phillips9da87e02019-02-04 13:26:26 -050049 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070050 proxyProvider->removeUniqueKeyFromProxy(proxy);
51}
52
53void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
Robert Phillips9da87e02019-02-04 13:26:26 -050054 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton2de13dd2019-01-03 15:11:59 -070055 proxyProvider->processInvalidUniqueKey(key, nullptr,
56 GrProxyProvider::InvalidateGPUResource::kYes);
Chris Dalton50edafa2018-05-17 21:45:25 -060057}
58
59sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
60 const GrUniqueKey& key, GrSurfaceOrigin origin) {
Robert Phillips9da87e02019-02-04 13:26:26 -050061 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
Chris Dalton50edafa2018-05-17 21:45:25 -060062 return proxyProvider->findOrCreateProxyByUniqueKey(key, origin);
63}
64
Robert Phillipscd5099c2018-02-09 09:56:56 -050065bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
Robert Phillips5f78adf2019-04-22 12:41:39 -040066 SkASSERT(proxy->priv().ignoredByResourceAllocator());
Robert Phillips7eeb74f2019-03-29 07:26:46 -040067
Robert Phillips6a6de562019-02-15 15:19:15 -050068 // TODO: this class should probably just get a GrDirectContext
69 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
70 if (!direct) {
71 return false;
72 }
73
74 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050075
Robert Phillips7d79e7b2018-02-14 11:09:57 -050076 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
Brian Salomon967df202018-12-07 11:15:53 -050077 // DDL TODO: Decide if we ever plan to have these proxies use the GrDeinstantiateTracker
Greg Daniel4684f822018-03-08 15:27:36 -050078 // to support unistantiating them at the end of a flush.
Robert Phillips7d79e7b2018-02-14 11:09:57 -050079 return proxy->priv().doLazyInstantiation(resourceProvider);
80 }
81
Robert Phillipscd5099c2018-02-09 09:56:56 -050082 return proxy->instantiate(resourceProvider);
83}
84
Brian Salomondbf70722019-02-07 11:31:24 -050085sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
86 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -050087 // TODO: this class should probably just get a GrDirectContext
88 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
89 if (!direct) {
90 return nullptr;
91 }
92
93 auto resourceProvider = direct->priv().resourceProvider();
94
Brian Salomondbf70722019-02-07 11:31:24 -050095 return sk_sp<GrGpuBuffer>(
96 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -070097}
98
Brian Salomondbf70722019-02-07 11:31:24 -050099sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -0500100 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500101 // TODO: class should probably just get a GrDirectContext
102 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
103 if (!direct) {
104 return nullptr;
105 }
106
107 auto resourceProvider = direct->priv().resourceProvider();
108
Brian Salomondbf70722019-02-07 11:31:24 -0500109 sk_sp<const GrGpuBuffer> buffer =
110 resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700111 // Static buffers should never have pending IO.
Chris Dalton133944a2018-11-16 23:30:29 -0500112 SkASSERT(!buffer || !buffer->resourcePriv().hasPendingIO_debugOnly());
Chris Daltone9e91dd2017-07-14 08:48:57 -0600113 return buffer;
114}
115
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500116uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500117 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400118}
119
Chris Dalton6081ebb2017-06-20 11:35:59 -0700120const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500121 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700122}