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/gpu/GrDirectContext.h"
11 #include "include/gpu/GrRecordingContext.h"
12 #include "src/gpu/GrDirectContextPriv.h"
13 #include "src/gpu/GrDrawingManager.h"
14 #include "src/gpu/GrProxyProvider.h"
15 #include "src/gpu/GrRecordingContextPriv.h"
16 #include "src/gpu/GrSurfaceDrawContext.h"
17 #include "src/gpu/GrSurfaceProxy.h"
18 #include "src/gpu/GrTextureResolveRenderTask.h"
19
makeRenderTargetContext(sk_sp<GrSurfaceProxy> proxy,GrSurfaceOrigin origin,GrColorType colorType,sk_sp<SkColorSpace> colorSpace,const SkSurfaceProps & props)20 std::unique_ptr<GrSurfaceDrawContext> GrOnFlushResourceProvider::makeRenderTargetContext(
21 sk_sp<GrSurfaceProxy> proxy, GrSurfaceOrigin origin, GrColorType colorType,
22 sk_sp<SkColorSpace> colorSpace, 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.
25 if (!this->instatiateProxy(proxy.get())) {
26 return nullptr;
27 }
28
29 auto context = fDrawingMgr->getContext();
30
31 if (!proxy->asRenderTargetProxy()) {
32 return nullptr;
33 }
34
35 auto surfaceDrawContext = GrSurfaceDrawContext::Make(context, colorType, std::move(colorSpace),
36 std::move(proxy), origin, props, true);
37
38 if (!surfaceDrawContext) {
39 return nullptr;
40 }
41
42 surfaceDrawContext->discard();
43
44 return surfaceDrawContext;
45 }
46
addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,GrSurfaceProxy::ResolveFlags resolveFlags)47 void GrOnFlushResourceProvider::addTextureResolveTask(sk_sp<GrTextureProxy> textureProxy,
48 GrSurfaceProxy::ResolveFlags resolveFlags) {
49 // Since we are bypassing normal DAG operation, we need to ensure the textureProxy's last render
50 // task gets closed before making a texture resolve task. makeClosed is what will mark msaa and
51 // mipmaps dirty.
52 if (GrRenderTask* renderTask = fDrawingMgr->getLastRenderTask(textureProxy.get())) {
53 renderTask->makeClosed(*this->caps());
54 }
55 auto task = static_cast<GrTextureResolveRenderTask*>(fDrawingMgr->fOnFlushRenderTasks.push_back(
56 sk_make_sp<GrTextureResolveRenderTask>()).get());
57 task->addProxy(fDrawingMgr, std::move(textureProxy), resolveFlags, *this->caps());
58 task->makeClosed(*this->caps());
59 }
60
assignUniqueKeyToProxy(const GrUniqueKey & key,GrTextureProxy * proxy)61 bool GrOnFlushResourceProvider::assignUniqueKeyToProxy(const GrUniqueKey& key,
62 GrTextureProxy* proxy) {
63 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
64 return proxyProvider->assignUniqueKeyToProxy(key, proxy);
65 }
66
removeUniqueKeyFromProxy(GrTextureProxy * proxy)67 void GrOnFlushResourceProvider::removeUniqueKeyFromProxy(GrTextureProxy* proxy) {
68 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
69 proxyProvider->removeUniqueKeyFromProxy(proxy);
70 }
71
processInvalidUniqueKey(const GrUniqueKey & key)72 void GrOnFlushResourceProvider::processInvalidUniqueKey(const GrUniqueKey& key) {
73 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
74 proxyProvider->processInvalidUniqueKey(key, nullptr,
75 GrProxyProvider::InvalidateGPUResource::kYes);
76 }
77
findOrCreateProxyByUniqueKey(const GrUniqueKey & key,UseAllocator useAllocator)78 sk_sp<GrTextureProxy> GrOnFlushResourceProvider::findOrCreateProxyByUniqueKey(
79 const GrUniqueKey& key,
80 UseAllocator useAllocator) {
81 auto proxyProvider = fDrawingMgr->getContext()->priv().proxyProvider();
82 return proxyProvider->findOrCreateProxyByUniqueKey(key, useAllocator);
83 }
84
instatiateProxy(GrSurfaceProxy * proxy)85 bool GrOnFlushResourceProvider::instatiateProxy(GrSurfaceProxy* proxy) {
86 SkASSERT(proxy->canSkipResourceAllocator());
87
88 // TODO: this class should probably just get a GrDirectContext
89 auto direct = fDrawingMgr->getContext()->asDirectContext();
90 if (!direct) {
91 return false;
92 }
93
94 auto resourceProvider = direct->priv().resourceProvider();
95
96 if (proxy->isLazy()) {
97 return proxy->priv().doLazyInstantiation(resourceProvider);
98 }
99
100 return proxy->instantiate(resourceProvider);
101 }
102
makeBuffer(GrGpuBufferType intendedType,size_t size,const void * data)103 sk_sp<GrGpuBuffer> GrOnFlushResourceProvider::makeBuffer(GrGpuBufferType intendedType, size_t size,
104 const void* data) {
105 // TODO: this class should probably just get a GrDirectContext
106 auto direct = fDrawingMgr->getContext()->asDirectContext();
107 if (!direct) {
108 return nullptr;
109 }
110
111 auto resourceProvider = direct->priv().resourceProvider();
112
113 return sk_sp<GrGpuBuffer>(
114 resourceProvider->createBuffer(size, intendedType, kDynamic_GrAccessPattern, data));
115 }
116
findOrMakeStaticBuffer(GrGpuBufferType intendedType,size_t size,const void * data,const GrUniqueKey & key)117 sk_sp<const GrGpuBuffer> GrOnFlushResourceProvider::findOrMakeStaticBuffer(
118 GrGpuBufferType intendedType, size_t size, const void* data, const GrUniqueKey& key) {
119 // TODO: class should probably just get a GrDirectContext
120 auto direct = fDrawingMgr->getContext()->asDirectContext();
121 if (!direct) {
122 return nullptr;
123 }
124
125 auto resourceProvider = direct->priv().resourceProvider();
126
127 return resourceProvider->findOrMakeStaticBuffer(intendedType, size, data, key);
128 }
129
contextID() const130 uint32_t GrOnFlushResourceProvider::contextID() const {
131 return fDrawingMgr->getContext()->priv().contextID();
132 }
133
caps() const134 const GrCaps* GrOnFlushResourceProvider::caps() const {
135 return fDrawingMgr->getContext()->priv().caps();
136 }
137
recordingContext() const138 GrRecordingContext* GrOnFlushResourceProvider::recordingContext() const {
139 return fDrawingMgr->getContext();
140 }
141
printWarningMessage(const char * msg) const142 void GrOnFlushResourceProvider::printWarningMessage(const char* msg) const {
143 fDrawingMgr->getContext()->priv().printWarningMessage(msg);
144 }
145