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
56 // TODO: we only need this entry point as long as we have to pre-allocate the atlas.
57 // Remove it ASAP.
makeRenderTargetContext(sk_sp<GrSurfaceProxy> proxy,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps * props)58 sk_sp<GrRenderTargetContext> GrOnFlushResourceProvider::makeRenderTargetContext(
59 sk_sp<GrSurfaceProxy> proxy,
60 sk_sp<SkColorSpace> colorSpace,
61 const SkSurfaceProps* props) {
62 sk_sp<GrRenderTargetContext> renderTargetContext(
63 fDrawingMgr->makeRenderTargetContext(std::move(proxy),
64 std::move(colorSpace),
65 props, false));
66
67 if (!renderTargetContext) {
68 return nullptr;
69 }
70
71 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
72
73 // Since this is at flush time and these won't be allocated for us by the GrResourceAllocator
74 // we have to manually ensure it is allocated here. The proxy had best have been created
75 // with the kNoPendingIO flag!
76 if (!renderTargetContext->asSurfaceProxy()->instantiate(resourceProvider)) {
77 return nullptr;
78 }
79
80 renderTargetContext->discard();
81
82 return renderTargetContext;
83 }
84
makeBuffer(GrBufferType intendedType,size_t size,const void * data)85 sk_sp<GrBuffer> GrOnFlushResourceProvider::makeBuffer(GrBufferType intendedType, size_t size,
86 const void* data) {
87 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
88 return sk_sp<GrBuffer>(resourceProvider->createBuffer(size, intendedType,
89 kDynamic_GrAccessPattern,
90 GrResourceProvider::kNoPendingIO_Flag,
91 data));
92 }
93
findOrMakeStaticBuffer(GrBufferType intendedType,size_t size,const void * data,const GrUniqueKey & key)94 sk_sp<const GrBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(GrBufferType intendedType,
95 size_t size,
96 const void* data,
97 const GrUniqueKey& key) {
98 auto resourceProvider = fDrawingMgr->getContext()->contextPriv().resourceProvider();
99 sk_sp<const GrBuffer> buffer = resourceProvider->findOrMakeStaticBuffer(intendedType, size,
100 data, key);
101 // Static buffers should never have pending IO.
102 SkASSERT(!buffer->resourcePriv().hasPendingIO_debugOnly());
103 return buffer;
104 }
105
caps() const106 const GrCaps* GrOnFlushResourceProvider::caps() const {
107 return fDrawingMgr->getContext()->caps();
108 }
109