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 "GrSurfaceProxy.h"
14
makeRenderTargetContext(const GrSurfaceDesc & desc,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)15 sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
16 const GrSurfaceDesc& desc,
17 sk_sp<SkColorSpace> colorSpace,
18 const SkSurfaceProps* props) {
19 GrSurfaceDesc tmpDesc = desc;
20 tmpDesc.fFlags |= kRenderTarget_GrSurfaceFlag;
21
22 auto proxyProvider = fDrawingMgr->getContext()->contextPriv().proxyProvider();
23 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
24
25 // Because this is being allocated at the start of a flush we must ensure the proxy
26 // will, when instantiated, have no pending IO.
27 // TODO: fold the kNoPendingIO_Flag into GrSurfaceFlags?
28 sk_sp<GrSurfaceProxy> proxy = proxyProvider->createProxy(tmpDesc, SkBackingFit::kExact,
29 SkBudgeted::kYes,
30 GrResourceProvider::kNoPendingIO_Flag);
31 if (!proxy->asRenderTargetProxy()) {
32 return nullptr;
33 }
34
35 sk_sp<GrRenderTargetContext> renderTargetContext(
36 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
37 std::move(colorSpace),
38 props, false));
39
40 if (!renderTargetContext) {
41 return nullptr;
42 }
43
44 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
45 // we have to manually ensure it is allocated here. The proxy had best have been created
46 // with the kNoPendingIO flag!
47 if (!renderTargetContext->asSurfaceProxy()->instantiate(resourceProvider)) {
48 return nullptr;
49 }
50
51 renderTargetContext->discard();
52
53 return renderTargetContext;
54 }
55
makeRenderTargetContext(sk_sp<GrSurfaceProxy> proxy,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)56 sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
57 sk_sp<GrSurfaceProxy> proxy,
58 sk_sp<SkColorSpace> colorSpace,
59 const SkSurfaceProps* props) {
60 sk_sp<GrRenderTargetContext> renderTargetContext(
61 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
62 std::move(colorSpace),
63 props, false));
64
65 if (!renderTargetContext) {
66 return nullptr;
67 }
68
69 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
70
71 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
72 // we have to manually ensure it is allocated here. The proxy had best have been created
73 // with the kNoPendingIO flag!
74 if (!renderTargetContext->asSurfaceProxy()->instantiate(resourceProvider)) {
75 return nullptr;
76 }
77
78 renderTargetContext->discard();
79
80 return renderTargetContext;
81 }
82
instatiateProxy(GrSurfaceProxy * proxy)83 bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
84 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
85
86 if (GrSurfaceProxy::LazyState::kNot != proxy->lazyInstantiationState()) {
87 return proxy->priv().doLazyInstantiation(resourceProvider);
88 }
89
90 return proxy->instantiate(resourceProvider);
91 }
92
makeBuffer(GrBufferType intendedType,size_t size,const void * data)93 sk_sp<GrBuffer> GrOnFlushResourceProvider::makeBuffer(GrBufferType intendedType, size_t size,
94 const void* data) {
95 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
96 return sk_sp<GrBuffer>(resourceProvider->createBuffer(size, intendedType,
97 kDynamic_GrAccessPattern,
98 GrResourceProvider::kNoPendingIO_Flag,
99 data));
100 }
101
findOrMakeStaticBuffer(GrBufferType intendedType,size_t size,const void * data,const GrUniqueKey & key)102 sk_sp<const GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
103 size_t size,
104 const void* data,
105 const GrUniqueKey& key) {
106 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
107 sk_sp<const GrBuffer> buffer = resourceProvider->findOrMakeStaticBuffer(intendedType, size,
108 data, key);
109 // Static buffers should never have pending IO.
110 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
111 return buffer;
112 }
113
caps() const114 const GrCaps* GrOnFlushResourceProvider::caps() const {
115 return fDrawingMgr->getContext()->caps();
116 }
117