blob: 26b59f93b933eb0ca7164f23913f810f7a2cce75 [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
Chris Daltonfe199b72017-05-05 11:26:15 -04008#include "GrOnFlushResourceProvider.h"
Brian Salomon238069b2018-07-11 15:58:57 -04009#include "GrContext.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050010#include "GrContextPriv.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040011#include "GrDrawingManager.h"
Robert Phillips0bd24dc2018-01-16 08:06:32 -050012#include "GrProxyProvider.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040013#include "GrRenderTargetContext.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040014#include "GrSurfaceProxy.h"
15
Chris Daltonfe199b72017-05-05 11:26:15 -040016sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -050017 const GrSurfaceDesc& desc,
18 GrSurfaceOrigin origin,
19 sk_sp<SkColorSpace> colorSpace,
20 const SkSurfaceProps* props) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -040021 GrSurfaceDesc tmpDesc = desc;
22 tmpDesc.fFlags |= kRenderTarget_GrSurfaceFlag;
23
Robert Phillips6be756b2018-01-16 15:07:54 -050024 auto proxyProvider = fDrawingMgr->getContext()->contextPriv().proxyProvider();
25 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
26
Robert Phillipseb35f4d2017-03-21 07:56:47 -040027 // Because this is being allocated at the start of a flush we must ensure the proxy
28 // will, when instantiated, have no pending IO.
29 // TODO: fold the kNoPendingIO_Flag into GrSurfaceFlags?
Brian Salomon2a4f9832018-03-03 22:43:43 -050030 sk_sp<GrSurfaceProxy> proxy =
31 proxyProvider->createProxy(tmpDesc, origin, SkBackingFit::kExact, SkBudgeted::kYes,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040032 GrInternalSurfaceFlags::kNoPendingIO);
Chris Dalton4da70192018-06-18 09:51:36 -060033 if (!proxy || !proxy->asRenderTargetProxy()) {
Robert Phillipseb35f4d2017-03-21 07:56:47 -040034 return nullptr;
35 }
36
Robert Phillips1119dc32017-04-11 12:54:57 -040037 sk_sp<GrRenderTargetContext> renderTargetContext(
38 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
39 std::move(colorSpace),
Robert Phillips941d1442017-06-14 16:37:02 -040040 props, false));
Robert Phillips1119dc32017-04-11 12:54:57 -040041
42 if (!renderTargetContext) {
43 return nullptr;
44 }
45
Robert Phillipse0070c02017-11-13 12:47:24 -050046 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
47 // we have to manually ensure it is allocated here. The proxy had best have been created
48 // with the kNoPendingIO flag!
Robert Phillips6be756b2018-01-16 15:07:54 -050049 if (!renderTargetContext->asSurfaceProxy()->instantiate(resourceProvider)) {
Robert Phillipse0070c02017-11-13 12:47:24 -050050 return nullptr;
51 }
52
Robert Phillips1119dc32017-04-11 12:54:57 -040053 renderTargetContext->discard();
54
55 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040056}
57
Chris Daltonfe199b72017-05-05 11:26:15 -040058sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
Robert Phillipseb35f4d2017-03-21 07:56:47 -040059 sk_sp<GrSurfaceProxy> proxy,
60 sk_sp<SkColorSpace> colorSpace,
61 const SkSurfaceProps* props) {
Chris Dalton4c458b12018-06-16 17:22:59 -060062 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
63 // we have to manually ensure it is allocated here. The proxy had best have been created
64 // with the kNoPendingIO flag!
65 if (!this->instatiateProxy(proxy.get())) {
66 return nullptr;
67 }
68
Robert Phillips1119dc32017-04-11 12:54:57 -040069 sk_sp<GrRenderTargetContext> renderTargetContext(
70 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
71 std::move(colorSpace),
Robert Phillips941d1442017-06-14 16:37:02 -040072 props, false));
Robert Phillips1119dc32017-04-11 12:54:57 -040073
74 if (!renderTargetContext) {
75 return nullptr;
76 }
77
78 renderTargetContext->discard();
79
80 return renderTargetContext;
Robert Phillipseb35f4d2017-03-21 07:56:47 -040081}
82
Chris Dalton50edafa2018-05-17 21:45:25 -060083bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
84 GrTextureProxy* proxy) {
85 auto proxyProvider = fDrawingMgr->getContext()->contextPriv().proxyProvider();
86 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
87}
88
89void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(const GrUniqueKey& key,
90 GrTextureProxy* proxy) {
91 auto proxyProvider = fDrawingMgr->getContext()->contextPriv().proxyProvider();
92 proxyProvider->removeUniqueKeyFromProxy(key, proxy);
93}
94
95sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
96 const GrUniqueKey& key, GrSurfaceOrigin origin) {
97 auto proxyProvider = fDrawingMgr->getContext()->contextPriv().proxyProvider();
98 return proxyProvider->findOrCreateProxyByUniqueKey(key, origin);
99}
100
Robert Phillipscd5099c2018-02-09 09:56:56 -0500101bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
102 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
103
Robert Phillips7d79e7b2018-02-14 11:09:57 -0500104 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
Greg Daniel4684f822018-03-08 15:27:36 -0500105 // DDL TODO: Decide if we ever plan to have these proxies use the GrUninstantiateTracker
106 // to support unistantiating them at the end of a flush.
Robert Phillips7d79e7b2018-02-14 11:09:57 -0500107 return proxy->priv().doLazyInstantiation(resourceProvider);
108 }
109
Robert Phillipscd5099c2018-02-09 09:56:56 -0500110 return proxy->instantiate(resourceProvider);
111}
112
Chris Dalton6081ebb2017-06-20 11:35:59 -0700113sk_sp<GrBuffer> GrOnFlushResourceProvider::makeBuffer(GrBufferType intendedType, size_t size,
114 const void* data) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500115 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
116 return sk_sp<GrBuffer>(resourceProvider->createBuffer(size, intendedType,
117 kDynamic_GrAccessPattern,
118 GrResourceProvider::kNoPendingIO_Flag,
119 data));
Chris Dalton6081ebb2017-06-20 11:35:59 -0700120}
121
Chris Dalton5d2de082017-12-19 10:40:23 -0700122sk_sp<const GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
123 size_t size,
124 const void* data,
125 const GrUniqueKey& key) {
Robert Phillips6be756b2018-01-16 15:07:54 -0500126 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
127 sk_sp<const GrBuffer> buffer = resourceProvider->findOrMakeStaticBuffer(intendedType, size,
128 data, key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700129 // Static buffers should never have pending IO.
130 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
Chris Daltone9e91dd2017-07-14 08:48:57 -0600131 return buffer;
132}
133
Brian Salomon238069b2018-07-11 15:58:57 -0400134uint32_t GrOnFlushResourceProvider::contextUniqueID() const {
135 return fDrawingMgr->getContext()->uniqueID();
136}
137
Chris Dalton6081ebb2017-06-20 11:35:59 -0700138const GrCaps* GrOnFlushResourceProvider::caps() const {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400139 return fDrawingMgr->getContext()->contextPriv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700140}