blob: 40dc68a582240d1049e6e8694f43dd7a50a70923 [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"
Robert Phillips69893702019-02-22 11:16:30 -05009
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"
Robert Phillips69893702019-02-22 11:16:30 -050013#include "GrRecordingContext.h"
14#include "GrRecordingContextPriv.h"
Brian Salomon653f42f2018-07-10 10:07:31 -040015#include "GrRenderTargetContext.h"
Robert Phillipseb35f4d2017-03-21 07:56:47 -040016#include "GrSurfaceProxy.h"
17
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 Phillips6a6de562019-02-15 15:19:15 -050067 // TODO: this class should probably just get a GrDirectContext
68 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
69 if (!direct) {
70 return false;
71 }
72
73 auto resourceProvider = direct->priv().resourceProvider();
Robert Phillipscd5099c2018-02-09 09:56:56 -050074
Robert Phillips7d79e7b2018-02-14 11:09:57 -050075 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
Brian Salomon967df202018-12-07 11:15:53 -050076 // DDL TODO: Decide if we ever plan to have these proxies use the GrDeinstantiateTracker
Greg Daniel4684f822018-03-08 15:27:36 -050077 // to support unistantiating them at the end of a flush.
Robert Phillips7d79e7b2018-02-14 11:09:57 -050078 return proxy->priv().doLazyInstantiation(resourceProvider);
79 }
80
Robert Phillipscd5099c2018-02-09 09:56:56 -050081 return proxy->instantiate(resourceProvider);
82}
83
Brian Salomondbf70722019-02-07 11:31:24 -050084sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
85 const void* data) {
Robert Phillips6a6de562019-02-15 15:19:15 -050086 // TODO: this class should probably just get a GrDirectContext
87 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
88 if (!direct) {
89 return nullptr;
90 }
91
92 auto resourceProvider = direct->priv().resourceProvider();
93
Brian Salomondbf70722019-02-07 11:31:24 -050094 return sk_sp<GrGpuBuffer>(
95 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
Chris Dalton6081ebb2017-06-20 11:35:59 -070096}
97
Brian Salomondbf70722019-02-07 11:31:24 -050098sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
Brian Salomonae64c192019-02-05 09:41:37 -050099 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
Robert Phillips6a6de562019-02-15 15:19:15 -0500100 // TODO: class should probably just get a GrDirectContext
101 auto direct = fDrawingMgr->getContext()->priv().asDirectContext();
102 if (!direct) {
103 return nullptr;
104 }
105
106 auto resourceProvider = direct->priv().resourceProvider();
107
Brian Salomondbf70722019-02-07 11:31:24 -0500108 sk_sp<const GrGpuBuffer> buffer =
109 resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
Chris Dalton5d2de082017-12-19 10:40:23 -0700110 // Static buffers should never have pending IO.
Chris Dalton133944a2018-11-16 23:30:29 -0500111 SkASSERT(!buffer || !buffer->resourcePriv().hasPendingIO_debugOnly());
Chris Daltone9e91dd2017-07-14 08:48:57 -0600112 return buffer;
113}
114
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500115uint32_t GrOnFlushResourceProvider::contextID() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500116 return fDrawingMgr->getContext()->priv().contextID();
Brian Salomon238069b2018-07-11 15:58:57 -0400117}
118
Chris Dalton6081ebb2017-06-20 11:35:59 -0700119const GrCaps* GrOnFlushResourceProvider::caps() const {
Robert Phillips9da87e02019-02-04 13:26:26 -0500120 return fDrawingMgr->getContext()->priv().caps();
Chris Dalton6081ebb2017-06-20 11:35:59 -0700121}