1 /*
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
8 #include "src/gpu/GrOnFlushResourceProvider.h"
9
10 #include "include/private/GrRecordingContext.h"
11 #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"
16 #include "src/gpu/GrSurfaceProxy.h"
17
makeRenderTargetContext(sk_sp<GrSurfaceProxy> proxy,GrColorType colorType,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)18 sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
19 sk_sp<GrSurfaceProxy> proxy,
20 GrColorType colorType,
21 sk_sp<SkColorSpace> colorSpace,
22 const SkSurfaceProps* props) {
23 // 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
30 sk_sp<GrRenderTargetContext> renderTargetContext(fDrawingMgr->makeRenderTargetContext(
31 std::move(proxy), colorType, std::move(colorSpace), props, false));
32
33 if (!renderTargetContext) {
34 return nullptr;
35 }
36
37 renderTargetContext->discard();
38
39 return renderTargetContext;
40 }
41
assignUniqueKeyToProxy(const GrUniqueKey & key,GrTextureProxy * proxy)42 bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
43 GrTextureProxy* proxy) {
44 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
45 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
46 }
47
removeUniqueKeyFromProxy(GrTextureProxy * proxy)48 void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
49 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
50 proxyProvider->removeUniqueKeyFromProxy(proxy);
51 }
52
processInvalidUniqueKey(const GrUniqueKey & key)53 void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
54 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
55 proxyProvider->processInvalidUniqueKey(key, nullptr,
56 GrProxyProvider::InvalidateGPUResource::kYes);
57 }
58
findOrCreateProxyByUniqueKey(const GrUniqueKey & key,GrColorType colorType,GrSurfaceOrigin origin)59 sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
60 const GrUniqueKey& key, GrColorType colorType, GrSurfaceOrigin origin) {
61 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
62 return proxyProvider->findOrCreateProxyByUniqueKey(key, colorType, origin);
63 }
64
instatiateProxy(GrSurfaceProxy * proxy)65 bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
66 SkASSERT(proxy->priv().ignoredByResourceAllocator());
67
68 // 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();
75
76 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
77 // DDL TODO: Decide if we ever plan to have these proxies use the GrDeinstantiateTracker
78 // to support unistantiating them at the end of a flush.
79 return proxy->priv().doLazyInstantiation(resourceProvider);
80 }
81
82 return proxy->instantiate(resourceProvider);
83 }
84
makeBuffer(GrGpuBufferType intendedType,size_t size,const void * data)85 sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
86 const void* data) {
87 // 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
95 return sk_sp<GrGpuBuffer>(
96 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
97 }
98
findOrMakeStaticBuffer(GrGpuBufferType intendedType,size_t size,const void * data,const GrUniqueKey & key)99 sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
100 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
101 // 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
109 sk_sp<const GrGpuBuffer> buffer =
110 resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
111 // Static buffers should never have pending IO.
112 SkASSERT(!buffer || !buffer->resourcePriv().hasPendingIO_debugOnly());
113 return buffer;
114 }
115
contextID() const116 uint32_t GrOnFlushResourceProvider::contextID() const {
117 return fDrawingMgr->getContext()->priv().contextID();
118 }
119
caps() const120 const GrCaps* GrOnFlushResourceProvider::caps() const {
121 return fDrawingMgr->getContext()->priv().caps();
122 }
123