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