blob: e1032b5e56b2119da6b8ff3dc71fc9ea304994ea [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 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 "GrVkGpu.h"
9
Ethan Nicholas8e265a72018-12-12 16:22:40 -050010#include "GrContextPriv.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040011#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrContextOptions.h"
14#include "GrGeometryProcessor.h"
15#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070016#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrPipeline.h"
18#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040020#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkCommandBuffer.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050022#include "GrVkCommandPool.h"
egdaniel066df7c2016-06-08 14:02:27 -070023#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050024#include "GrVkImage.h"
25#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040026#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050027#include "GrVkMemory.h"
28#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070029#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050030#include "GrVkRenderPass.h"
31#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050032#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050033#include "GrVkTexture.h"
34#include "GrVkTextureRenderTarget.h"
35#include "GrVkTransferBuffer.h"
36#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050037#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070038#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "SkSLCompiler.h"
40#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040041
Greg Daniela31f4e52018-08-01 16:48:52 -040042#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070043#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050044
Ben Wagnerf08d1d02018-06-18 15:11:00 -040045#include <utility>
46
Forrest Reiling44f85712017-03-27 23:22:20 -070047#if !defined(SK_BUILD_FOR_WIN)
48#include <unistd.h>
49#endif // !defined(SK_BUILD_FOR_WIN)
50
Greg Danieldef55462018-08-01 13:40:14 -040051#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
52#include "SkLeanWindows.h"
53#endif
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
56#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
57#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
58
Greg Danielf730c182018-07-02 20:15:37 +000059sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050060 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000061 if (backendContext.fInstance == VK_NULL_HANDLE ||
62 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
63 backendContext.fDevice == VK_NULL_HANDLE ||
64 backendContext.fQueue == VK_NULL_HANDLE) {
65 return nullptr;
66 }
Greg Danield3e65aa2018-08-01 09:19:45 -040067 if (!backendContext.fGetProc) {
68 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040069 }
Greg Danield3e65aa2018-08-01 09:19:45 -040070
Greg Daniel41f0e282019-01-28 13:15:05 -050071 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
72 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
73 backendContext.fGetProc("vkEnumerateInstanceVersion",
74 VK_NULL_HANDLE, VK_NULL_HANDLE));
75 uint32_t instanceVersion = 0;
76 if (!localEnumerateInstanceVersion) {
77 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
78 } else {
79 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
80 if (err) {
81 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
82 return nullptr;
83 }
84 }
85
Greg Danielc0b03d82018-08-03 14:41:15 -040086 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
87 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
88 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
89 backendContext.fInstance,
90 VK_NULL_HANDLE));
91
92 if (!localGetPhysicalDeviceProperties) {
93 return nullptr;
94 }
95 VkPhysicalDeviceProperties physDeviceProperties;
96 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
97 uint32_t physDevVersion = physDeviceProperties.apiVersion;
98
Greg Daniel41f0e282019-01-28 13:15:05 -050099 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
100 : instanceVersion;
101
102 instanceVersion = SkTMin(instanceVersion, apiVersion);
103 physDevVersion = SkTMin(physDevVersion, apiVersion);
104
Greg Daniel98bffae2018-08-01 13:25:41 -0400105 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400106
Greg Daniel98bffae2018-08-01 13:25:41 -0400107 if (backendContext.fVkExtensions) {
108 interface.reset(new GrVkInterface(backendContext.fGetProc,
109 backendContext.fInstance,
110 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500111 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400112 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500114 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400115 return nullptr;
116 }
117 } else {
118 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
119 // make an empty GrVkExtensions and pass that to the GrVkInterface.
120 GrVkExtensions extensions;
121 interface.reset(new GrVkInterface(backendContext.fGetProc,
122 backendContext.fInstance,
123 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500124 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400125 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400126 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500127 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400128 return nullptr;
129 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 }
131
Greg Daniel41f0e282019-01-28 13:15:05 -0500132 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
133 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136////////////////////////////////////////////////////////////////////////////////
137
halcanary9d524f22016-03-29 09:03:52 -0700138GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
140 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500141 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400142 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000143 , fMemoryAllocator(backendContext.fMemoryAllocator)
144 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400145 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000146 , fDevice(backendContext.fDevice)
147 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400148 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500149 , fResourceProvider(this)
150 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000151 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700152
Greg Daniel81df0412018-05-31 13:13:33 -0400153 if (!fMemoryAllocator) {
154 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000155 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400156 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400157 }
158
ethannicholasb3058bd2016-07-01 08:22:01 -0700159 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700160
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400162 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400163 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500164 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400165 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400166 } else if (backendContext.fDeviceFeatures) {
167 VkPhysicalDeviceFeatures2 features2;
168 features2.pNext = nullptr;
169 features2.features = *backendContext.fDeviceFeatures;
170 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500171 features2, instanceVersion, physicalDeviceVersion,
172 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400173 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400174 VkPhysicalDeviceFeatures2 features;
175 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
176 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400177 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400178 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400179 }
180 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400182 }
183 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400185 }
186 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500187 features, instanceVersion, physicalDeviceVersion,
188 GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400189 }
jvanverth633b3562016-03-23 11:01:22 -0700190 fCaps.reset(SkRef(fVkCaps.get()));
191
Greg Danielf730c182018-07-02 20:15:37 +0000192 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
193 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700194
Greg Daniela870b462019-01-08 15:49:46 -0500195 fResourceProvider.init();
196
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500197 fCmdPool = fResourceProvider.findOrCreateCommandPool();
198 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000199 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700200 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500201}
202
Greg Daniel8606cf82017-05-08 16:17:53 -0400203void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500204 if (fCmdPool) {
205 fCmdPool->getPrimaryCommandBuffer()->end(this);
206 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400207 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
209 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500210 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700211
212 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
213 // on the command buffers even though they have completed. This causes an assert to fire when
214 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500215 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700216#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500217 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700218#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500219 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700220#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500221 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700222#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500223 }
egdanielf8c2be32016-06-24 13:18:27 -0700224#endif
225
egdanielbe9d8212016-09-20 08:54:23 -0700226#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400227 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700228#endif
halcanary9d524f22016-03-29 09:03:52 -0700229
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500230 if (fCmdPool) {
231 fCmdPool->unref(this);
232 fCmdPool = nullptr;
233 }
234
Greg Daniel6be35232017-03-01 17:01:09 -0500235 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
236 fSemaphoresToWaitOn[i]->unref(this);
237 }
238 fSemaphoresToWaitOn.reset();
239
Greg Daniela5cb7812017-06-16 09:45:32 -0400240 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
241 fSemaphoresToSignal[i]->unref(this);
242 }
243 fSemaphoresToSignal.reset();
244
245
egdanielbc9b2962016-09-27 08:00:53 -0700246 fCopyManager.destroyResources(this);
247
Jim Van Verth09557d72016-11-07 11:10:21 -0500248 // must call this just before we destroy the command pool and VkDevice
249 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500250
Greg Danielf730c182018-07-02 20:15:37 +0000251 fMemoryAllocator.reset();
252
253 fQueue = VK_NULL_HANDLE;
254 fDevice = VK_NULL_HANDLE;
255 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400256}
257
258GrVkGpu::~GrVkGpu() {
259 if (!fDisconnected) {
260 this->destroyResources();
261 }
262 delete fCompiler;
263}
264
265
266void GrVkGpu::disconnect(DisconnectType type) {
267 INHERITED::disconnect(type);
268 if (!fDisconnected) {
269 if (DisconnectType::kCleanup == type) {
270 this->destroyResources();
271 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500272 if (fCmdPool) {
273 fCmdPool->unrefAndAbandon();
274 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400275 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400276 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
277 fSemaphoresToWaitOn[i]->unrefAndAbandon();
278 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400279 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
280 fSemaphoresToSignal[i]->unrefAndAbandon();
281 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400282 fCopyManager.abandonResources();
283
284 // must call this just before we destroy the command pool and VkDevice
285 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400286
287 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400288 }
289 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400290 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400291 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400292 fDisconnected = true;
293 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500294}
295
296///////////////////////////////////////////////////////////////////////////////
297
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400298GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400299 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400300 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
301 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400302 if (!fCachedRTCommandBuffer) {
303 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
304 }
305
Greg Daniela41a74a2018-10-09 12:59:23 +0000306 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400307 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400308}
309
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400310GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
311 if (!fCachedTexCommandBuffer) {
312 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
313 }
314
315 fCachedTexCommandBuffer->set(texture, origin);
316 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700317}
318
Greg Daniela5cb7812017-06-16 09:45:32 -0400319void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400321
322 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
323 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
324 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400325 fResourceProvider.checkCommandBuffers();
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400326 return;
327 }
328
Greg Daniel164a9f02016-02-22 09:56:40 -0500329 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500330 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400331 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500332
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400333 // We must delete and drawables that have been waitint till submit for us to destroy.
334 fDrawables.reset();
335
Greg Daniel6be35232017-03-01 17:01:09 -0500336 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
337 fSemaphoresToWaitOn[i]->unref(this);
338 }
339 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400340 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
341 fSemaphoresToSignal[i]->unref(this);
342 }
343 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500344
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500345 // Release old command pool and create a new one
346 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500347 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500348 fCmdPool = fResourceProvider.findOrCreateCommandPool();
349 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500350 fCurrentCmdBuffer->begin(this);
351}
352
353///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500354sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
355 GrAccessPattern accessPattern, const void* data) {
356 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700357 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500358 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700359 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
360 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500361 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700362 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500363 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700364 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
365 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500366 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700367 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500368 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400369 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
370 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500371 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700372 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500373 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400374 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
375 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500376 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700377 break;
cdalton397536c2016-03-25 12:15:03 -0700378 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400379 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700380 return nullptr;
381 }
cdalton1bf3e712016-04-19 10:00:02 -0700382 if (data && buff) {
383 buff->updateData(data, size);
384 }
385 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500386}
387
Brian Salomona9b04b92018-06-01 15:04:28 -0400388bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
389 GrColorType srcColorType, const GrMipLevel texels[],
390 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500391 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
392 if (!vkTex) {
393 return false;
394 }
395
jvanverth900bd4a2016-04-29 13:53:12 -0700396 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400397 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800398 return false;
399 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800400
Jim Van Verth1676cb92019-01-15 13:24:45 -0500401 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500402 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400403 bool linearTiling = vkTex->isLinearTiled();
404 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400405 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400406 SkDebugf("Can't upload mipmap data to linear tiled texture");
407 return false;
408 }
409 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
410 // Need to change the layout to general in order to perform a host write
411 vkTex->setImageLayout(this,
412 VK_IMAGE_LAYOUT_GENERAL,
413 VK_ACCESS_HOST_WRITE_BIT,
414 VK_PIPELINE_STAGE_HOST_BIT,
415 false);
416 this->submitCommandBuffer(kForce_SyncQueue);
417 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400418 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400419 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500420 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400421 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400422 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
423 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500424 }
egdaniel4583ec52016-06-27 12:57:00 -0700425
jvanverth900bd4a2016-04-29 13:53:12 -0700426 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500427}
428
Brian Salomone05ba5a2019-04-08 11:59:07 -0400429bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
430 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
431 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500432 // Can't transfer compressed data
433 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
434
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400435 // Vulkan only supports 4-byte aligned offsets
436 if (SkToBool(bufferOffset & 0x2)) {
437 return false;
438 }
439 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
440 if (!vkTex) {
441 return false;
442 }
443 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
444 if (!vkBuffer) {
445 return false;
446 }
447
Greg Daniel660cc992017-06-26 14:55:05 -0400448 SkDEBUGCODE(
449 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
450 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
451 SkASSERT(bounds.contains(subRect));
452 )
Brian Salomonc320b152018-02-20 14:05:36 -0500453 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400454 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500455 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400456 }
457
458 // Set up copy region
459 VkBufferImageCopy region;
460 memset(&region, 0, sizeof(VkBufferImageCopy));
461 region.bufferOffset = bufferOffset;
462 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
463 region.bufferImageHeight = 0;
464 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
465 region.imageOffset = { left, top, 0 };
466 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
467
468 // Change layout of our target so it can be copied to
469 vkTex->setImageLayout(this,
470 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
471 VK_ACCESS_TRANSFER_WRITE_BIT,
472 VK_PIPELINE_STAGE_TRANSFER_BIT,
473 false);
474
475 // Copy the buffer to the image
476 fCurrentCmdBuffer->copyBufferToImage(this,
477 vkBuffer,
478 vkTex,
479 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
480 1,
481 &region);
482
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400483 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400484 return true;
485}
486
Brian Salomona585fe92019-04-09 14:57:00 -0400487size_t GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
488 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
489 size_t offset) {
490 SkASSERT(surface);
491 SkASSERT(transferBuffer);
492
493 size_t rowBytes;
494 size_t offsetAlignment;
495 if (!this->vkCaps().transferFromBufferRequirements(bufferColorType, width, &rowBytes,
496 &offsetAlignment)) {
497 return 0;
498 }
499
500 if (offset % offsetAlignment || offset + height * rowBytes > transferBuffer->size()) {
501 return 0;
502 }
503
504 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
505
506 GrVkImage* srcImage;
507 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
508 // Reading from render targets that wrap a secondary command buffer is not allowed since
509 // it would require us to know the VkImage, which we don't have, as well as need us to
510 // stop and start the VkRenderPass which we don't have access to.
511 if (rt->wrapsSecondaryCommandBuffer()) {
512 return false;
513 }
514 // resolve the render target if necessary
515 switch (rt->getResolveType()) {
516 case GrVkRenderTarget::kCantResolve_ResolveType:
517 return false;
518 case GrVkRenderTarget::kAutoResolves_ResolveType:
519 break;
520 case GrVkRenderTarget::kCanResolve_ResolveType:
521 this->resolveRenderTargetNoFlush(rt);
522 break;
523 default:
524 SK_ABORT("Unknown resolve type");
525 }
526 srcImage = rt;
527 } else {
528 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
529 }
530
531 // Set up copy region
532 VkBufferImageCopy region;
533 memset(&region, 0, sizeof(VkBufferImageCopy));
534 region.bufferOffset = offset;
535 // We're assuming that GrVkCaps made the row bytes tight.
536 region.bufferRowLength = 0;
537#ifdef SK_DEBUG
538 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
539 SkASSERT(rowBytes == width * (size_t)bpp);
540#endif
541 region.bufferImageHeight = 0;
542 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
543 region.imageOffset = { left, top, 0 };
544 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
545
546 srcImage->setImageLayout(this,
547 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
548 VK_ACCESS_TRANSFER_READ_BIT,
549 VK_PIPELINE_STAGE_TRANSFER_BIT,
550 false);
551
552 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
553 vkBuffer, 1, &region);
554
555 // Make sure the copy to buffer has finished.
556 vkBuffer->addMemoryBarrier(this,
557 VK_ACCESS_TRANSFER_WRITE_BIT,
558 VK_ACCESS_HOST_READ_BIT,
559 VK_PIPELINE_STAGE_TRANSFER_BIT,
560 VK_PIPELINE_STAGE_HOST_BIT,
561 false);
562
563 // The caller is responsible for syncing.
564 this->submitCommandBuffer(kSkip_SyncQueue);
565
566 return rowBytes;
567}
568
Brian Salomon1fabd512018-02-09 09:54:25 -0500569void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
570 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700571 SkASSERT(dst);
572 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
573
egdaniel4bcd62e2016-08-31 07:37:31 -0700574 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500575 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
576 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
577 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
578 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
579 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700580
Greg Danielbc26c392017-04-18 13:32:10 -0400581 GrVkImage* dstImage;
582 GrRenderTarget* dstRT = dst->asRenderTarget();
583 if (dstRT) {
584 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400585 dstImage = vkRT;
586 } else {
587 SkASSERT(dst->asTexture());
588 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
589 }
590 dstImage->setImageLayout(this,
591 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
592 VK_ACCESS_TRANSFER_WRITE_BIT,
593 VK_PIPELINE_STAGE_TRANSFER_BIT,
594 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700595
596 src->msaaImage()->setImageLayout(this,
597 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
598 VK_ACCESS_TRANSFER_READ_BIT,
599 VK_PIPELINE_STAGE_TRANSFER_BIT,
600 false);
601
Greg Danielbc26c392017-04-18 13:32:10 -0400602 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700603}
604
Brian Salomon1fabd512018-02-09 09:54:25 -0500605void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700606 if (target->needsResolve()) {
607 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700608 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
609 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500610
egdaniel4bcd62e2016-08-31 07:37:31 -0700611 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700612
Brian Salomon1fabd512018-02-09 09:54:25 -0500613 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700614
615 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500616
617 if (requiresSubmit) {
618 this->submitCommandBuffer(kSkip_SyncQueue);
619 }
egdaniel52ad2512016-08-04 12:50:01 -0700620 }
621}
622
Brian Salomona9b04b92018-06-01 15:04:28 -0400623bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
624 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500625 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700626 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500627
Jim Van Verth1676cb92019-01-15 13:24:45 -0500628 // If we're uploading compressed data then we should be using uploadCompressedTexData
629 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
630 GrSRGBEncoded::kNo)));
631
Greg Daniel660cc992017-06-26 14:55:05 -0400632 SkDEBUGCODE(
633 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
634 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
635 SkASSERT(bounds.contains(subRect));
636 )
Brian Salomonc320b152018-02-20 14:05:36 -0500637 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500638 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400639 if (!rowBytes) {
640 rowBytes = trimRowBytes;
641 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500642
jvanverth900bd4a2016-04-29 13:53:12 -0700643 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
644 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
645 const VkImageSubresource subres = {
646 VK_IMAGE_ASPECT_COLOR_BIT,
647 0, // mipLevel
648 0, // arraySlice
649 };
650 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500651
jvanverth900bd4a2016-04-29 13:53:12 -0700652 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500653
jvanverth900bd4a2016-04-29 13:53:12 -0700654 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700655 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700656 &subres,
657 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500658
jvanverth1e305ba2016-06-01 09:39:15 -0700659 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400660 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700661 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400662 SkASSERT(size + offset <= alloc.fSize);
663 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
664 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700665 return false;
666 }
Greg Daniel81df0412018-05-31 13:13:33 -0400667 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700668
Brian Salomona9b04b92018-06-01 15:04:28 -0400669 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
670 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700671
Greg Daniele35a99e2018-03-02 11:44:22 -0500672 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400673 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700674
675 return true;
676}
677
Brian Salomona9b04b92018-06-01 15:04:28 -0400678bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
679 GrColorType dataColorType, const GrMipLevel texels[],
680 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700681 SkASSERT(!tex->isLinearTiled());
682 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400683 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700684 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
685
Greg Danieldd20e912017-04-07 14:42:23 -0400686 // We assume that if the texture has mip levels, we either upload to all the levels or just the
687 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400688 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400689
Jim Van Verth1676cb92019-01-15 13:24:45 -0500690 // If we're uploading compressed data then we should be using uploadCompressedTexData
691 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
692 GrSRGBEncoded::kNo)));
693
jvanverth900bd4a2016-04-29 13:53:12 -0700694 if (width == 0 || height == 0) {
695 return false;
696 }
697
Greg Daniel475eb702018-09-28 14:16:50 -0400698 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
699 return false;
700 }
701
702 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
703 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500704 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
705 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400706 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
707 // blit or draw.
708 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
709 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
710 return false;
711 }
712 mipLevelCount = 1;
713 }
714
Brian Salomond1eaf492017-05-18 10:02:08 -0400715 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500716 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700717
718 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700719 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
720 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400721 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
722
Greg Daniel475eb702018-09-28 14:16:50 -0400723 texelsShallowCopy.reset(mipLevelCount);
724 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700725
Robert Phillips590533f2017-07-11 14:22:35 -0400726 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700727 individualMipOffsets.push_back(0);
728 size_t combinedBufferSize = width * bpp * height;
729 int currentWidth = width;
730 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400731 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400732 combinedBufferSize = 0;
733 }
734
Greg Daniel468fd632017-03-22 17:03:45 -0400735 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
736 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
737 SkASSERT((bpp & (bpp - 1)) == 0);
738 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400739 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700740 currentWidth = SkTMax(1, currentWidth/2);
741 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400742
Greg Daniel55afd6d2017-09-29 09:32:44 -0400743 if (texelsShallowCopy[currentMipLevel].fPixels) {
744 const size_t trimmedSize = currentWidth * bpp * currentHeight;
745 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
746 if (alignmentDiff != 0) {
747 combinedBufferSize += alignmentMask - alignmentDiff + 1;
748 }
749 individualMipOffsets.push_back(combinedBufferSize);
750 combinedBufferSize += trimmedSize;
751 } else {
752 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400753 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400754 }
755 if (0 == combinedBufferSize) {
756 // We don't actually have any data to upload so just return success
757 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700758 }
759
760 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500761 sk_sp<GrVkTransferBuffer> transferBuffer =
762 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400763 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700764 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400765 }
jvanverth900bd4a2016-04-29 13:53:12 -0700766
Greg Daniel475eb702018-09-28 14:16:50 -0400767 int uploadLeft = left;
768 int uploadTop = top;
769 GrVkTexture* uploadTexture = tex;
770 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
771 // R8G8B8A8_UNORM image and then copy it.
772 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500773 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400774 GrSurfaceDesc surfDesc;
775 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
776 surfDesc.fWidth = width;
777 surfDesc.fHeight = height;
778 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
779 surfDesc.fSampleCnt = 1;
780
781 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
782 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
783 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
784
785 GrVkImage::ImageDesc imageDesc;
786 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
787 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
788 imageDesc.fWidth = width;
789 imageDesc.fHeight = height;
790 imageDesc.fLevels = 1;
791 imageDesc.fSamples = 1;
792 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
793 imageDesc.fUsageFlags = usageFlags;
794 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
795
796 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
797 GrMipMapsStatus::kNotAllocated);
798 if (!copyTexture) {
799 return false;
800 }
801 uploadTexture = copyTexture.get();
802 uploadLeft = 0;
803 uploadTop = 0;
804 }
805
jvanverth900bd4a2016-04-29 13:53:12 -0700806 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400807 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700808
jvanverthc578b0632016-05-02 10:58:12 -0700809 currentWidth = width;
810 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400811 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400812 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400813 if (texelsShallowCopy[currentMipLevel].fPixels) {
814 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
815 const size_t trimRowBytes = currentWidth * bpp;
816 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
817 ? texelsShallowCopy[currentMipLevel].fRowBytes
818 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700819
Greg Daniel55afd6d2017-09-29 09:32:44 -0400820 // copy data into the buffer, skipping the trailing bytes
821 char* dst = buffer + individualMipOffsets[currentMipLevel];
822 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400823 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400824
825 VkBufferImageCopy& region = regions.push_back();
826 memset(&region, 0, sizeof(VkBufferImageCopy));
827 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
828 region.bufferRowLength = currentWidth;
829 region.bufferImageHeight = currentHeight;
830 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400831 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400832 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700833 }
jvanverthc578b0632016-05-02 10:58:12 -0700834 currentWidth = SkTMax(1, currentWidth/2);
835 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400836 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700837 }
838
jvanverth9d54afc2016-09-20 09:20:03 -0700839 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700840 transferBuffer->unmap();
841
jvanverth900bd4a2016-04-29 13:53:12 -0700842 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400843 uploadTexture->setImageLayout(this,
844 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
845 VK_ACCESS_TRANSFER_WRITE_BIT,
846 VK_PIPELINE_STAGE_TRANSFER_BIT,
847 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700848
849 // Copy the buffer to the image
850 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500851 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400852 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700853 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
854 regions.count(),
855 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400856
857 // If we copied the data into a temporary image first, copy that image into our main texture
858 // now.
859 if (copyTexture.get()) {
860 SkASSERT(dataColorType == GrColorType::kRGB_888x);
861 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
862 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
863 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
864 false));
865 }
Robert Phillips590533f2017-07-11 14:22:35 -0400866 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400867 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400868 }
jvanverth900bd4a2016-04-29 13:53:12 -0700869
Greg Daniel164a9f02016-02-22 09:56:40 -0500870 return true;
871}
872
Jim Van Verth1676cb92019-01-15 13:24:45 -0500873// It's probably possible to roll this into uploadTexDataOptimal,
874// but for now it's easier to maintain as a separate entity.
875bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
876 GrColorType dataColorType, const GrMipLevel texels[],
877 int mipLevelCount) {
878 SkASSERT(!tex->isLinearTiled());
879 // For now the assumption is that our rect is the entire texture.
880 // Compressed textures are read-only so this should be a reasonable assumption.
881 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
882
883 // We assume that if the texture has mip levels, we either upload to all the levels or just the
884 // first.
885 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
886
887 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
888 GrSRGBEncoded::kNo)));
889
890 if (width == 0 || height == 0) {
891 return false;
892 }
893
894 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
895 return false;
896 }
897
898 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
899
900 SkTArray<size_t> individualMipOffsets(mipLevelCount);
901 individualMipOffsets.push_back(0);
902 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
903 int currentWidth = width;
904 int currentHeight = height;
905 if (!texels[0].fPixels) {
906 return false;
907 }
908
909 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
910 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
911 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
912 currentWidth = SkTMax(1, currentWidth / 2);
913 currentHeight = SkTMax(1, currentHeight / 2);
914
915 if (texels[currentMipLevel].fPixels) {
916 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
917 currentHeight);
918 individualMipOffsets.push_back(combinedBufferSize);
919 combinedBufferSize += dataSize;
920 } else {
921 return false;
922 }
923 }
924 if (0 == combinedBufferSize) {
925 // We don't have any data to upload so fail (compressed textures are read-only).
926 return false;
927 }
928
929 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500930 sk_sp<GrVkTransferBuffer> transferBuffer =
931 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500932 if (!transferBuffer) {
933 return false;
934 }
935
936 int uploadLeft = left;
937 int uploadTop = top;
938 GrVkTexture* uploadTexture = tex;
939
940 char* buffer = (char*)transferBuffer->map();
941 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
942
943 currentWidth = width;
944 currentHeight = height;
945 int layerHeight = uploadTexture->height();
946 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
947 if (texels[currentMipLevel].fPixels) {
948 // Again, we're assuming that our rect is the entire texture
949 SkASSERT(currentHeight == layerHeight);
950 SkASSERT(0 == uploadLeft && 0 == uploadTop);
951
952 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
953 currentHeight);
954
955 // copy data into the buffer, skipping the trailing bytes
956 char* dst = buffer + individualMipOffsets[currentMipLevel];
957 const char* src = (const char*)texels[currentMipLevel].fPixels;
958 memcpy(dst, src, dataSize);
959
960 VkBufferImageCopy& region = regions.push_back();
961 memset(&region, 0, sizeof(VkBufferImageCopy));
962 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
963 region.bufferRowLength = currentWidth;
964 region.bufferImageHeight = currentHeight;
965 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
966 region.imageOffset = { uploadLeft, uploadTop, 0 };
967 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
968 }
969 currentWidth = SkTMax(1, currentWidth / 2);
970 currentHeight = SkTMax(1, currentHeight / 2);
971 layerHeight = currentHeight;
972 }
973
974 // no need to flush non-coherent memory, unmap will do that for us
975 transferBuffer->unmap();
976
977 // Change layout of our target so it can be copied to
978 uploadTexture->setImageLayout(this,
979 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
980 VK_ACCESS_TRANSFER_WRITE_BIT,
981 VK_PIPELINE_STAGE_TRANSFER_BIT,
982 false);
983
984 // Copy the buffer to the image
985 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500986 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500987 uploadTexture,
988 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
989 regions.count(),
990 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500991
992 if (1 == mipLevelCount) {
993 tex->texturePriv().markMipMapsDirty();
994 }
995
996 return true;
997}
998
Greg Daniel164a9f02016-02-22 09:56:40 -0500999////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001000sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001001 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001002 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1003
1004 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001005 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001006
Greg Daniel164a9f02016-02-22 09:56:40 -05001007 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1008 if (renderTarget) {
1009 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1010 }
1011
1012 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1013 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1014 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001015 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001016 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1017 // texture.
1018 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1019
Greg Daniel164a9f02016-02-22 09:56:40 -05001020 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001021 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001022 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001023 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001024 GrVkImage::ImageDesc imageDesc;
1025 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1026 imageDesc.fFormat = pixelFormat;
1027 imageDesc.fWidth = desc.fWidth;
1028 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001029 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001030 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001031 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001032 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001033 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001034
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001035 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1036 if (mipLevels > 1) {
1037 mipMapsStatus = GrMipMapsStatus::kValid;
1038 for (int i = 0; i < mipLevels; ++i) {
1039 if (!texels[i].fPixels) {
1040 mipMapsStatus = GrMipMapsStatus::kDirty;
1041 break;
1042 }
Greg Daniel834f1202017-10-09 15:06:20 -04001043 }
1044 }
1045
Robert Phillips67d52cf2017-06-05 13:38:13 -04001046 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001047 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001048 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1049 imageDesc,
1050 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001051 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001052 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001053 }
1054
1055 if (!tex) {
1056 return nullptr;
1057 }
1058
Jim Van Verth1676cb92019-01-15 13:24:45 -05001059 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001060 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001061 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001062 bool success;
1063 if (isCompressed) {
1064 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1065 colorType, texels, mipLevelCount);
1066 } else {
1067 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1068 colorType, texels, mipLevelCount);
1069 }
1070 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001071 tex->unref();
1072 return nullptr;
1073 }
1074 }
1075
Jim Van Verth1676cb92019-01-15 13:24:45 -05001076 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001077 VkClearColorValue zeroClearColor;
1078 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1079 VkImageSubresourceRange range;
1080 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1081 range.baseArrayLayer = 0;
1082 range.baseMipLevel = 0;
1083 range.layerCount = 1;
1084 range.levelCount = 1;
1085 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1086 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001087 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001088 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001089 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001090}
1091
1092////////////////////////////////////////////////////////////////////////////////
1093
Greg Daniel6888c0d2017-08-25 11:55:50 -04001094void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1095 VkDeviceSize dstOffset, VkDeviceSize size) {
1096 VkBufferCopy copyRegion;
1097 copyRegion.srcOffset = srcOffset;
1098 copyRegion.dstOffset = dstOffset;
1099 copyRegion.size = size;
1100 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1101}
1102
jvanverthdb379092016-07-07 11:18:46 -07001103bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1104 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001105 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001106 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001107
1108 return true;
1109}
1110
1111////////////////////////////////////////////////////////////////////////////////
1112
Greg Daniel7e000222018-12-03 10:08:21 -05001113static bool check_image_info(const GrVkCaps& caps,
1114 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001115 GrPixelConfig config,
1116 bool isWrappedRT) {
1117 if (VK_NULL_HANDLE == info.fImage) {
1118 return false;
1119 }
1120
1121 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001122 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001123 }
1124
Greg Daniel7e000222018-12-03 10:08:21 -05001125 if (info.fYcbcrConversionInfo.isValid()) {
1126 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1127 return false;
1128 }
jvanverthfd359ca2016-03-18 11:57:24 -07001129 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001130
Greg Danielcb324152019-02-25 11:36:53 -05001131 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1132 return false;
1133 }
1134
Greg Daniel52e16d92018-04-10 09:34:07 -04001135 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001136 return true;
1137}
1138
1139sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001140 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1141 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001142 GrVkImageInfo imageInfo;
1143 if (!backendTex.getVkImageInfo(&imageInfo)) {
1144 return nullptr;
1145 }
1146
Greg Danielcb324152019-02-25 11:36:53 -05001147 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001148 return nullptr;
1149 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001150
Greg Daniel164a9f02016-02-22 09:56:40 -05001151 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001152 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001153 surfDesc.fWidth = backendTex.width();
1154 surfDesc.fHeight = backendTex.height();
1155 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001156 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001157
Greg Daniel52e16d92018-04-10 09:34:07 -04001158 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1159 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001160 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1161 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001162}
1163
1164sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001165 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001166 GrWrapOwnership ownership,
1167 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001168 GrVkImageInfo imageInfo;
1169 if (!backendTex.getVkImageInfo(&imageInfo)) {
1170 return nullptr;
1171 }
1172
Greg Danielcb324152019-02-25 11:36:53 -05001173 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001174 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001175 }
Brian Salomond17f6582017-07-19 18:28:58 -04001176
1177 GrSurfaceDesc surfDesc;
1178 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1179 surfDesc.fWidth = backendTex.width();
1180 surfDesc.fHeight = backendTex.height();
1181 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001182 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001183
Greg Daniel52e16d92018-04-10 09:34:07 -04001184 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1185 SkASSERT(layout);
1186
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001187 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1188 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001189}
1190
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001191sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001192 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1193 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1194 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1195 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001196 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001197 return nullptr;
1198 }
halcanary9d524f22016-03-29 09:03:52 -07001199
Greg Daniel323fbcf2018-04-10 13:46:30 -04001200 GrVkImageInfo info;
1201 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001202 return nullptr;
1203 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001204
Greg Danielcb324152019-02-25 11:36:53 -05001205 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001206 return nullptr;
1207 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001208
Greg Daniel164a9f02016-02-22 09:56:40 -05001209 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001210 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001211 desc.fWidth = backendRT.width();
1212 desc.fHeight = backendRT.height();
1213 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001214 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001215
Greg Daniel323fbcf2018-04-10 13:46:30 -04001216 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001217
Greg Daniel323fbcf2018-04-10 13:46:30 -04001218 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001219 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001220
1221 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1222 SkASSERT(!backendRT.stencilBits());
1223 if (tgt) {
1224 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001225 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001226
Ben Wagnerff134f22018-04-24 16:29:16 -04001227 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001228}
1229
Greg Daniel7ef28f32017-04-20 16:41:55 +00001230sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001231 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001232
Greg Daniel52e16d92018-04-10 09:34:07 -04001233 GrVkImageInfo imageInfo;
1234 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001235 return nullptr;
1236 }
Greg Danielcb324152019-02-25 11:36:53 -05001237 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001238 return nullptr;
1239 }
1240
Greg Danielcb324152019-02-25 11:36:53 -05001241
Brian Osman33910292017-04-18 14:38:53 -04001242 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001243 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001244 desc.fWidth = tex.width();
1245 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001246 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001247 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1248 if (!desc.fSampleCnt) {
1249 return nullptr;
1250 }
Brian Osman33910292017-04-18 14:38:53 -04001251
Greg Daniel52e16d92018-04-10 09:34:07 -04001252 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1253 SkASSERT(layout);
1254
Ben Wagnerff134f22018-04-24 16:29:16 -04001255 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001256}
1257
Greg Danielb46add82019-01-02 14:51:29 -05001258sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1259 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1260 int maxSize = this->caps()->maxTextureSize();
1261 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1262 return nullptr;
1263 }
1264
1265 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1266 if (!backendFormat.isValid()) {
1267 return nullptr;
1268 }
1269 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1270 imageInfo.colorType());
1271 if (config == kUnknown_GrPixelConfig) {
1272 return nullptr;
1273 }
1274
1275 GrSurfaceDesc desc;
1276 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1277 desc.fWidth = imageInfo.width();
1278 desc.fHeight = imageInfo.height();
1279 desc.fConfig = config;
1280 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1281 if (!desc.fSampleCnt) {
1282 return nullptr;
1283 }
1284
1285 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1286}
1287
Brian Salomon930f9392018-06-20 16:25:26 -04001288bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1289 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001290 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001291 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001292 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001293 return false;
jvanverth62340062016-04-26 08:01:44 -07001294 }
1295
jvanverth62340062016-04-26 08:01:44 -07001296 // determine if we can blit to and from this format
1297 const GrVkCaps& caps = this->vkCaps();
1298 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001299 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1300 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001301 return false;
jvanverth62340062016-04-26 08:01:44 -07001302 }
1303
egdaniel7ac5da82016-07-15 13:41:42 -07001304 int width = tex->width();
1305 int height = tex->height();
1306 VkImageBlit blitRegion;
1307 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001308
jvanverth82c05582016-05-03 11:19:01 -07001309 // SkMipMap doesn't include the base level in the level count so we have to add 1
1310 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001311 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001312
Greg Danielda86e282018-06-13 09:41:19 -04001313 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001314 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1315 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001316
jvanverth50c46c72016-05-06 12:31:28 -07001317 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001318 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001319 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1320 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001321 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1322 nullptr, // pNext
1323 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1324 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1325 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1326 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1327 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1328 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1329 vkTex->image(), // image
1330 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001331 };
1332
jvanverth62340062016-04-26 08:01:44 -07001333 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001334 uint32_t mipLevel = 1;
1335 while (mipLevel < levelCount) {
1336 int prevWidth = width;
1337 int prevHeight = height;
1338 width = SkTMax(1, width / 2);
1339 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001340
jvanverth50c46c72016-05-06 12:31:28 -07001341 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001342 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1343 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001344
1345 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001346 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001347 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001348 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1349 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001350 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001351 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001352 vkTex->resource(),
1353 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001354 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001355 vkTex->resource(),
1356 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001357 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001358 1,
1359 &blitRegion,
1360 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001361 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001362 }
Greg Danielee54f232019-04-03 14:58:40 -04001363 if (levelCount > 1) {
1364 // This barrier logically is not needed, but it changes the final level to the same layout
1365 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1366 // layouts and future layout changes easier. The alternative here would be to track layout
1367 // and memory accesses per layer which doesn't seem work it.
1368 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1369 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1370 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1371 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1372 }
Brian Salomon930f9392018-06-20 16:25:26 -04001373 return true;
jvanverth62340062016-04-26 08:01:44 -07001374}
1375
Greg Daniel164a9f02016-02-22 09:56:40 -05001376////////////////////////////////////////////////////////////////////////////////
1377
1378GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1379 int width,
1380 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001381 SkASSERT(width >= rt->width());
1382 SkASSERT(height >= rt->height());
1383
1384 int samples = rt->numStencilSamples();
1385
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001386 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001387
1388 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001389 width,
1390 height,
1391 samples,
1392 sFmt));
1393 fStats.incStencilAttachmentCreates();
1394 return stencil;
1395}
1396
1397////////////////////////////////////////////////////////////////////////////////
1398
Brian Salomon52e943a2018-03-13 09:32:39 -04001399bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001400 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1401 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001402 VkDeviceSize size = dstRowBytes * h;
1403 VkDeviceSize offset = bufferOffset;
1404 SkASSERT(size + offset <= alloc.fSize);
1405 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1406 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001407 return false;
1408 }
Greg Daniel81df0412018-05-31 13:13:33 -04001409 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001410
Greg Daniel20ece3a2017-03-28 10:24:43 -04001411 if (srcData) {
1412 // If there is no padding on dst we can do a single memcopy.
1413 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001414 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001415 } else {
1416 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1417 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001418 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001419 }
Greg Daniel81df0412018-05-31 13:13:33 -04001420 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1421 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001422 return true;
1423}
1424
Brian Salomonf865b052018-03-09 09:01:53 -05001425#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001426bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1427 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001428 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001429 SkASSERT(texturable || renderable);
1430 if (!texturable) {
1431 SkASSERT(GrMipMapped::kNo == mipMapped);
1432 SkASSERT(!srcData);
1433 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001434 VkFormat pixelFormat;
1435 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001436 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001437 }
1438
Brian Salomon52e943a2018-03-13 09:32:39 -04001439 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1440 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001441 }
1442
Brian Salomon52e943a2018-03-13 09:32:39 -04001443 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1444 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001445 }
1446
1447 // Currently we don't support uploading pixel data when mipped.
1448 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001449 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001450 }
1451
Brian Salomon52e943a2018-03-13 09:32:39 -04001452 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001453 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1454 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001455 if (texturable) {
1456 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1457 }
1458 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001459 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1460 }
1461
1462 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001463 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001464 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001465
1466 // Create Image
1467 VkSampleCountFlagBits vkSamples;
1468 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001469 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001470 }
1471
1472 // Figure out the number of mip levels.
1473 uint32_t mipLevels = 1;
1474 if (GrMipMapped::kYes == mipMapped) {
1475 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1476 }
1477
1478 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001479 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1480 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001481 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001482 VK_IMAGE_TYPE_2D, // VkImageType
1483 pixelFormat, // VkFormat
1484 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1485 mipLevels, // mipLevels
1486 1, // arrayLayers
1487 vkSamples, // samples
1488 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1489 usageFlags, // VkImageUsageFlags
1490 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1491 0, // queueFamilyCount
1492 0, // pQueueFamilyIndices
1493 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001494 };
1495
Brian Salomon52e943a2018-03-13 09:32:39 -04001496 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1497 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001498
Brian Salomonde9f5462018-03-07 14:23:58 -05001499 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001500 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001501 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001502 }
1503
1504 // We need to declare these early so that we can delete them at the end outside of the if block.
Greg Daniel8385a8a2018-02-26 13:29:37 -05001505 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001506 VkBuffer buffer = VK_NULL_HANDLE;
1507
1508 VkResult err;
1509 const VkCommandBufferAllocateInfo cmdInfo = {
1510 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1511 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001512 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001513 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1514 1 // bufferCount
1515 };
1516
1517 VkCommandBuffer cmdBuffer;
1518 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1519 if (err) {
1520 GrVkMemory::FreeImageMemory(this, false, alloc);
1521 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001522 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001523 }
1524
1525 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1526 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1527 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1528 cmdBufferBeginInfo.pNext = nullptr;
1529 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1530 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1531
1532 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1533 SkASSERT(!err);
1534
1535 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001536 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001537
Robert Phillips646f6372018-09-25 09:31:10 -04001538 const size_t trimRowBytes = w * bpp;
1539 if (!srcRowBytes) {
1540 srcRowBytes = trimRowBytes;
1541 }
1542
Brian Salomonde9f5462018-03-07 14:23:58 -05001543 SkTArray<size_t> individualMipOffsets(mipLevels);
1544 individualMipOffsets.push_back(0);
1545 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001546 if (GrPixelConfigIsCompressed(config)) {
1547 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1548 bpp = 4; // we have at least this alignment, which will pass the code below
1549 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001550 int currentWidth = w;
1551 int currentHeight = h;
1552 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1553 // config. This works with the assumption that the bytes in pixel config is always a power
1554 // of 2.
1555 SkASSERT((bpp & (bpp - 1)) == 0);
1556 const size_t alignmentMask = 0x3 | (bpp - 1);
1557 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1558 currentWidth = SkTMax(1, currentWidth / 2);
1559 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001560
Jim Van Verth1676cb92019-01-15 13:24:45 -05001561 size_t trimmedSize;
1562 if (GrPixelConfigIsCompressed(config)) {
1563 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1564 } else {
1565 trimmedSize = currentWidth * bpp * currentHeight;
1566 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001567 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1568 if (alignmentDiff != 0) {
1569 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001570 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001571 individualMipOffsets.push_back(combinedBufferSize);
1572 combinedBufferSize += trimmedSize;
1573 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001574
Brian Salomonde9f5462018-03-07 14:23:58 -05001575 VkBufferCreateInfo bufInfo;
1576 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1577 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1578 bufInfo.flags = 0;
1579 bufInfo.size = combinedBufferSize;
1580 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1581 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1582 bufInfo.queueFamilyIndexCount = 0;
1583 bufInfo.pQueueFamilyIndices = nullptr;
1584 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001585
Brian Salomonde9f5462018-03-07 14:23:58 -05001586 if (err) {
1587 GrVkMemory::FreeImageMemory(this, false, alloc);
1588 VK_CALL(DestroyImage(fDevice, image, nullptr));
1589 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001590 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001591 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001592 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001593
Brian Salomonde9f5462018-03-07 14:23:58 -05001594 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1595 &bufferAlloc)) {
1596 GrVkMemory::FreeImageMemory(this, false, alloc);
1597 VK_CALL(DestroyImage(fDevice, image, nullptr));
1598 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1599 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001600 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001601 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001602 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001603
Brian Salomonde9f5462018-03-07 14:23:58 -05001604 currentWidth = w;
1605 currentHeight = h;
1606 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1607 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001608 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001609 bool result;
1610 if (GrPixelConfigIsCompressed(config)) {
1611 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1612 size_t currentRowBytes = levelSize / currentHeight;
1613 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1614 currentRowBytes, currentRowBytes, currentHeight);
1615 } else {
1616 size_t currentRowBytes = bpp * currentWidth;
1617 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1618 currentRowBytes, trimRowBytes, currentHeight);
1619 }
1620 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001621 GrVkMemory::FreeImageMemory(this, false, alloc);
1622 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001623 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001624 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1625 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001626 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001627 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001628 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001629 currentWidth = SkTMax(1, currentWidth / 2);
1630 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001631 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001632
1633 // Set image layout and add barrier
1634 VkImageMemoryBarrier barrier;
1635 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1636 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1637 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001638 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001639 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1640 barrier.oldLayout = initialLayout;
1641 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1642 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1643 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1644 barrier.image = image;
1645 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1646
Greg Danielf7828d02018-10-09 12:01:32 -04001647 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001648 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1649 &barrier));
1650 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1651
1652 SkTArray<VkBufferImageCopy> regions(mipLevels);
1653
1654 currentWidth = w;
1655 currentHeight = h;
1656 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1657 // Submit copy command
1658 VkBufferImageCopy& region = regions.push_back();
1659 memset(&region, 0, sizeof(VkBufferImageCopy));
1660 region.bufferOffset = individualMipOffsets[currentMipLevel];
1661 region.bufferRowLength = currentWidth;
1662 region.bufferImageHeight = currentHeight;
1663 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1664 region.imageOffset = {0, 0, 0};
1665 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1666 currentWidth = SkTMax(1, currentWidth / 2);
1667 currentHeight = SkTMax(1, currentHeight / 2);
1668 }
1669
1670 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1671 regions.begin()));
1672
Brian Salomon52e943a2018-03-13 09:32:39 -04001673 if (texturable) {
1674 // Change Image layout to shader read since if we use this texture as a borrowed textures
1675 // within Ganesh we require that its layout be set to that
1676 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1677 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1678 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001679 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001680 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1681 barrier.oldLayout = initialLayout;
1682 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1683 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1684 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1685 barrier.image = image;
1686 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001687 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001688 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1689 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001690 0,
1691 0, nullptr,
1692 0, nullptr,
1693 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001694 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001695 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001696
1697 // End CommandBuffer
1698 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1699 SkASSERT(!err);
1700
1701 // Create Fence for queue
1702 VkFence fence;
1703 VkFenceCreateInfo fenceInfo;
1704 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1705 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1706
1707 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1708 SkASSERT(!err);
1709
1710 VkSubmitInfo submitInfo;
1711 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1712 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1713 submitInfo.pNext = nullptr;
1714 submitInfo.waitSemaphoreCount = 0;
1715 submitInfo.pWaitSemaphores = nullptr;
1716 submitInfo.pWaitDstStageMask = 0;
1717 submitInfo.commandBufferCount = 1;
1718 submitInfo.pCommandBuffers = &cmdBuffer;
1719 submitInfo.signalSemaphoreCount = 0;
1720 submitInfo.pSignalSemaphores = nullptr;
1721 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1722 SkASSERT(!err);
1723
1724 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1725 if (VK_TIMEOUT == err) {
1726 GrVkMemory::FreeImageMemory(this, false, alloc);
1727 VK_CALL(DestroyImage(fDevice, image, nullptr));
1728 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1729 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001730 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001731 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1732 SkDebugf("Fence failed to signal: %d\n", err);
1733 SK_ABORT("failing");
1734 }
1735 SkASSERT(!err);
1736
1737 // Clean up transfer resources
1738 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1739 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1740 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1741 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001742 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001743 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1744
Brian Salomon52e943a2018-03-13 09:32:39 -04001745 info->fImage = image;
1746 info->fAlloc = alloc;
1747 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1748 info->fImageLayout = initialLayout;
1749 info->fFormat = pixelFormat;
1750 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001751
Brian Salomon52e943a2018-03-13 09:32:39 -04001752 return true;
1753}
1754
1755GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001756 GrColorType colorType,
1757 bool isRenderTarget,
1758 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001759 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001760
1761 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1762 return GrBackendTexture();
1763 }
1764
Robert Phillips646f6372018-09-25 09:31:10 -04001765 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1766 if (!this->caps()->isConfigTexturable(config)) {
1767 return GrBackendTexture();
1768 }
1769
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001770 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001771 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001772 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001773 return {};
1774 }
Greg Daniel108bb232018-07-03 16:18:29 -04001775 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1776 // Lots of tests don't go through Skia's public interface which will set the config so for
1777 // testing we make sure we set a config here.
1778 beTex.setPixelConfig(config);
1779 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001780}
1781
1782bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001783 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001784
Greg Daniel52e16d92018-04-10 09:34:07 -04001785 GrVkImageInfo backend;
1786 if (!tex.getVkImageInfo(&backend)) {
1787 return false;
1788 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001789
Greg Daniel52e16d92018-04-10 09:34:07 -04001790 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001791 VkMemoryRequirements req;
1792 memset(&req, 0, sizeof(req));
1793 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001794 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001795 &req));
1796 // TODO: find a better check
1797 // This will probably fail with a different driver
1798 return (req.size > 0) && (req.size <= 8192 * 8192);
1799 }
1800
1801 return false;
1802}
1803
Brian Salomon26102cb2018-03-09 09:33:19 -05001804void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001805 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001806
Greg Daniel52e16d92018-04-10 09:34:07 -04001807 GrVkImageInfo info;
1808 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001809 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001810 }
1811}
1812
Brian Osman2d010b62018-08-09 10:55:09 -04001813GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001814 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1815 return GrBackendRenderTarget();
1816 }
1817
Brian Salomon8a375832018-03-14 10:21:40 -04001818 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001819 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001820 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001821 if (kUnknown_GrPixelConfig == config) {
1822 return {};
1823 }
Robert Phillips646f6372018-09-25 09:31:10 -04001824 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001825 &info)) {
1826 return {};
1827 }
Greg Daniel108bb232018-07-03 16:18:29 -04001828 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1829 // Lots of tests don't go through Skia's public interface which will set the config so for
1830 // testing we make sure we set a config here.
1831 beRT.setPixelConfig(config);
1832 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001833}
1834
Brian Salomon52e943a2018-03-13 09:32:39 -04001835void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001836 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001837
Greg Daniel323fbcf2018-04-10 13:46:30 -04001838 GrVkImageInfo info;
1839 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001840 // something in the command buffer may still be using this, so force submit
1841 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001842 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001843 }
1844}
Brian Salomonf865b052018-03-09 09:01:53 -05001845
Greg Daniel26b50a42018-03-08 09:49:58 -05001846void GrVkGpu::testingOnly_flushGpuAndSync() {
1847 this->submitCommandBuffer(kForce_SyncQueue);
1848}
Brian Salomonf865b052018-03-09 09:01:53 -05001849#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001850
Greg Daniel164a9f02016-02-22 09:56:40 -05001851////////////////////////////////////////////////////////////////////////////////
1852
Greg Daniel59dc1482019-02-22 10:46:38 -05001853void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1854 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001855 VkPipelineStageFlags dstStageMask,
1856 bool byRegion,
1857 VkBufferMemoryBarrier* barrier) const {
1858 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001859 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001860 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001861 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001862 srcStageMask,
1863 dstStageMask,
1864 byRegion,
1865 GrVkCommandBuffer::kBufferMemory_BarrierType,
1866 barrier);
1867}
1868
Greg Daniel59dc1482019-02-22 10:46:38 -05001869void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1870 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001871 VkPipelineStageFlags dstStageMask,
1872 bool byRegion,
1873 VkImageMemoryBarrier* barrier) const {
1874 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001875 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001876 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001877 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001878 srcStageMask,
1879 dstStageMask,
1880 byRegion,
1881 GrVkCommandBuffer::kImageMemory_BarrierType,
1882 barrier);
1883}
1884
Greg Danielbae71212019-03-01 15:24:35 -05001885void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
Greg Danielb9990e42019-04-10 16:28:52 -04001886 GrFlushFlags flags, bool insertedSemaphore) {
Greg Daniel51316782017-08-02 15:10:09 +00001887 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1888 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001889 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1890 GrVkImage* image;
1891 SkASSERT(proxy->isInstantiated());
1892 if (GrTexture* tex = proxy->peekTexture()) {
1893 image = static_cast<GrVkTexture*>(tex);
1894 } else {
1895 GrRenderTarget* rt = proxy->peekRenderTarget();
1896 SkASSERT(rt);
1897 image = static_cast<GrVkRenderTarget*>(rt);
1898 }
1899 image->prepareForPresent(this);
1900 }
Greg Danielb9990e42019-04-10 16:28:52 -04001901 if (flags & kSyncCpu_GrFlushFlag) {
Greg Danielbae71212019-03-01 15:24:35 -05001902 this->submitCommandBuffer(kForce_SyncQueue);
1903 } else {
1904 this->submitCommandBuffer(kSkip_SyncQueue);
1905 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001906}
1907
Greg Daniel25af6712018-04-25 10:44:38 -04001908static int get_surface_sample_cnt(GrSurface* surf) {
1909 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1910 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001911 }
Greg Daniel25af6712018-04-25 10:44:38 -04001912 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001913}
1914
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001915void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1916 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001917 GrVkImage* dstImage,
1918 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001919 const SkIRect& srcRect,
1920 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001921#ifdef SK_DEBUG
1922 int dstSampleCnt = get_surface_sample_cnt(dst);
1923 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001924 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1925 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1926 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1927 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001928
1929#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001930
Greg Daniel164a9f02016-02-22 09:56:40 -05001931 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1932 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001933 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001934 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1935 VK_ACCESS_TRANSFER_WRITE_BIT,
1936 VK_PIPELINE_STAGE_TRANSFER_BIT,
1937 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001938
egdaniel17b89252016-04-05 07:23:38 -07001939 srcImage->setImageLayout(this,
1940 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001941 VK_ACCESS_TRANSFER_READ_BIT,
1942 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001943 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001944
1945 // Flip rect if necessary
1946 SkIRect srcVkRect = srcRect;
1947 int32_t dstY = dstPoint.fY;
1948
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001949 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1950 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001951 srcVkRect.fTop = src->height() - srcRect.fBottom;
1952 srcVkRect.fBottom = src->height() - srcRect.fTop;
1953 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1954 }
1955
1956 VkImageCopy copyRegion;
1957 memset(&copyRegion, 0, sizeof(VkImageCopy));
1958 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1959 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1960 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1961 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001962 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001963
1964 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001965 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001966 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001967 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001968 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1969 1,
1970 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001971
1972 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1973 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001974 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001975}
1976
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001977void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1978 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001979 GrVkImage* dstImage,
1980 GrVkImage* srcImage,
1981 const SkIRect& srcRect,
1982 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001983#ifdef SK_DEBUG
1984 int dstSampleCnt = get_surface_sample_cnt(dst);
1985 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001986 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1987 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04001988 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04001989 dstHasYcbcr, src->config(), srcSampleCnt,
1990 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07001991
Greg Daniel25af6712018-04-25 10:44:38 -04001992#endif
egdaniel17b89252016-04-05 07:23:38 -07001993 dstImage->setImageLayout(this,
1994 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001995 VK_ACCESS_TRANSFER_WRITE_BIT,
1996 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001997 false);
1998
egdaniel17b89252016-04-05 07:23:38 -07001999 srcImage->setImageLayout(this,
2000 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002001 VK_ACCESS_TRANSFER_READ_BIT,
2002 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002003 false);
2004
2005 // Flip rect if necessary
2006 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002007 srcVkRect.fLeft = srcRect.fLeft;
2008 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002009 SkIRect dstRect;
2010 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002011 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002012
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002013 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002014 srcVkRect.fTop = src->height() - srcRect.fBottom;
2015 srcVkRect.fBottom = src->height() - srcRect.fTop;
2016 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002017 srcVkRect.fTop = srcRect.fTop;
2018 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002019 }
2020
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002021 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002022 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2023 } else {
2024 dstRect.fTop = dstPoint.fY;
2025 }
2026 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2027
2028 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2029 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002030 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002031 using std::swap;
2032 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002033 }
2034
2035 VkImageBlit blitRegion;
2036 memset(&blitRegion, 0, sizeof(VkImageBlit));
2037 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2038 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002039 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002040 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2041 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002042 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002043
2044 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002045 *srcImage,
2046 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002047 1,
2048 &blitRegion,
2049 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002050
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002051 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002052 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002053}
2054
Brian Salomon1fabd512018-02-09 09:54:25 -05002055void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2056 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2057 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002058 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002059 SkIRect srcRect = origSrcRect;
2060 SkIPoint dstPoint = origDstPoint;
2061 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2062 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2063 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2064 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2065 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2066 }
2067 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002068 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2069 srcRect.width(), srcRect.height());
2070 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002071}
2072
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002073bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2074 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002075 const SkIRect& srcRect, const SkIPoint& dstPoint,
2076 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002077#ifdef SK_DEBUG
2078 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2079 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2080 }
2081 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2082 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2083 }
2084#endif
2085
Greg Daniel25af6712018-04-25 10:44:38 -04002086 GrPixelConfig dstConfig = dst->config();
2087 GrPixelConfig srcConfig = src->config();
2088
2089 int dstSampleCnt = get_surface_sample_cnt(dst);
2090 int srcSampleCnt = get_surface_sample_cnt(src);
2091
egdaniel17b89252016-04-05 07:23:38 -07002092 GrVkImage* dstImage;
2093 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002094 GrRenderTarget* dstRT = dst->asRenderTarget();
2095 if (dstRT) {
2096 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002097 if (vkRT->wrapsSecondaryCommandBuffer()) {
2098 return false;
2099 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002100 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2101 } else {
2102 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002103 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002104 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002105 GrRenderTarget* srcRT = src->asRenderTarget();
2106 if (srcRT) {
2107 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2108 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002109 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002110 SkASSERT(src->asTexture());
2111 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002112 }
2113
Greg Daniela51e93c2019-03-25 12:30:45 -04002114 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2115 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2116
2117 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2118 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2119 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2120 return true;
2121 }
2122
2123 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2124 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2125 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2126 dstPoint, canDiscardOutsideDstRect));
2127 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2128 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2129 return true;
2130 }
2131
2132 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2133 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002134 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2135 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002136 return true;
2137 }
2138
Greg Daniel25af6712018-04-25 10:44:38 -04002139 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002140 dstHasYcbcr, srcConfig, srcSampleCnt,
2141 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002142 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2143 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002144 return true;
2145 }
2146
Greg Daniel164a9f02016-02-22 09:56:40 -05002147 return false;
2148}
2149
Brian Salomona6948702018-06-01 15:33:20 -04002150bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2151 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002152 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002153 return false;
2154 }
2155
egdaniel66933552016-08-24 07:22:19 -07002156 GrVkImage* image = nullptr;
2157 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2158 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002159 // Reading from render targets that wrap a secondary command buffer is not allowed since
2160 // it would require us to know the VkImage, which we don't have, as well as need us to
2161 // stop and start the VkRenderPass which we don't have access to.
2162 if (rt->wrapsSecondaryCommandBuffer()) {
2163 return false;
2164 }
egdaniel66933552016-08-24 07:22:19 -07002165 // resolve the render target if necessary
2166 switch (rt->getResolveType()) {
2167 case GrVkRenderTarget::kCantResolve_ResolveType:
2168 return false;
2169 case GrVkRenderTarget::kAutoResolves_ResolveType:
2170 break;
2171 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002172 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002173 break;
2174 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002175 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002176 }
2177 image = rt;
2178 } else {
2179 image = static_cast<GrVkTexture*>(surface->asTexture());
2180 }
2181
2182 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002183 return false;
2184 }
2185
Greg Daniel475eb702018-09-28 14:16:50 -04002186 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2187 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2188 // image and then do the read pixels from that.
2189 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002190 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2191 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002192
2193 // Make a new surface that is RGBA to copy the RGB surface into.
2194 GrSurfaceDesc surfDesc;
2195 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2196 surfDesc.fWidth = width;
2197 surfDesc.fHeight = height;
2198 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2199 surfDesc.fSampleCnt = 1;
2200
2201 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2202 VK_IMAGE_USAGE_SAMPLED_BIT |
2203 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2204 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2205
2206 GrVkImage::ImageDesc imageDesc;
2207 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2208 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2209 imageDesc.fWidth = width;
2210 imageDesc.fHeight = height;
2211 imageDesc.fLevels = 1;
2212 imageDesc.fSamples = 1;
2213 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2214 imageDesc.fUsageFlags = usageFlags;
2215 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2216
2217 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2218 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2219 if (!copySurface) {
2220 return false;
2221 }
2222
2223 int srcSampleCount = 0;
2224 if (rt) {
2225 srcSampleCount = rt->numColorSamples();
2226 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002227 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002228 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002229 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2230 surface->config(), srcSampleCount, kOrigin,
2231 srcHasYcbcr) &&
2232 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2233 surface->config(), SkToBool(surface->asTexture()),
2234 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002235 return false;
2236 }
2237 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2238 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2239 srcRect, SkIPoint::Make(0,0))) {
2240 return false;
2241 }
2242 top = 0;
2243 left = 0;
2244 dstColorType = GrColorType::kRGBA_8888;
2245 image = copySurface.get();
2246 }
2247
Greg Daniel164a9f02016-02-22 09:56:40 -05002248 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002249 image->setImageLayout(this,
2250 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2251 VK_ACCESS_TRANSFER_READ_BIT,
2252 VK_PIPELINE_STAGE_TRANSFER_BIT,
2253 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002254
Brian Salomonc320b152018-02-20 14:05:36 -05002255 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002256 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002257
Greg Daniel164a9f02016-02-22 09:56:40 -05002258 VkBufferImageCopy region;
2259 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002260
2261 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2262 if (copyFromOrigin) {
2263 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002264 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002265 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002266 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002267 region.imageOffset = offset;
2268 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2269 }
2270
2271 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002272 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002273 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002274 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002275 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002276 kStream_GrAccessPattern)
2277 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002278
2279 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002280 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002281 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002282 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2283 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002284
2285 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002286 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002287 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002288 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002289 1,
2290 &region);
2291
2292 // make sure the copy to buffer has finished
2293 transferBuffer->addMemoryBarrier(this,
2294 VK_ACCESS_TRANSFER_WRITE_BIT,
2295 VK_ACCESS_HOST_READ_BIT,
2296 VK_PIPELINE_STAGE_TRANSFER_BIT,
2297 VK_PIPELINE_STAGE_HOST_BIT,
2298 false);
2299
2300 // We need to submit the current command buffer to the Queue and make sure it finishes before
2301 // we can copy the data out of the buffer.
2302 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002303 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002304 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002305 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002306
egdaniel6fa0a912016-09-12 11:51:29 -07002307 if (copyFromOrigin) {
2308 uint32_t skipRows = region.imageExtent.height - height;
2309 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2310 }
2311
Brian Salomona6948702018-06-01 15:33:20 -04002312 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002313
2314 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002315 return true;
2316}
egdaniel066df7c2016-06-08 14:02:27 -07002317
egdaniel27bb2842016-07-07 11:58:35 -07002318// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2319// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2320// the the entire attachment. Similar requirements for the y and height components.
2321void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2322 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2323 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002324 if ((0 != granularity.width && 1 != granularity.width)) {
2325 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2326 int rightAdj = srcBounds.fRight % granularity.width;
2327 if (rightAdj != 0) {
2328 rightAdj = granularity.width - rightAdj;
2329 }
2330 dstBounds->fRight = srcBounds.fRight + rightAdj;
2331 if (dstBounds->fRight > maxWidth) {
2332 dstBounds->fRight = maxWidth;
2333 dstBounds->fLeft = 0;
2334 } else {
2335 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2336 }
egdaniel27bb2842016-07-07 11:58:35 -07002337 } else {
egdanield5797b32016-09-20 12:57:45 -07002338 dstBounds->fLeft = srcBounds.fLeft;
2339 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002340 }
2341
2342 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002343 if ((0 != granularity.height && 1 != granularity.height)) {
2344 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2345 int bottomAdj = srcBounds.fBottom % granularity.height;
2346 if (bottomAdj != 0) {
2347 bottomAdj = granularity.height - bottomAdj;
2348 }
2349 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2350 if (dstBounds->fBottom > maxHeight) {
2351 dstBounds->fBottom = maxHeight;
2352 dstBounds->fTop = 0;
2353 } else {
2354 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2355 }
egdaniel27bb2842016-07-07 11:58:35 -07002356 } else {
egdanield5797b32016-09-20 12:57:45 -07002357 dstBounds->fTop = srcBounds.fTop;
2358 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002359 }
2360}
2361
Greg Daniel22bc8652017-03-22 15:45:43 -04002362void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002363 const GrVkRenderPass* renderPass,
2364 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002365 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002366 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002367 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002368 const SkIRect* pBounds = &bounds;
2369 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002370 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002371 flippedBounds = bounds;
2372 flippedBounds.fTop = target->height() - bounds.fBottom;
2373 flippedBounds.fBottom = target->height() - bounds.fTop;
2374 pBounds = &flippedBounds;
2375 }
2376
egdaniel27bb2842016-07-07 11:58:35 -07002377 // The bounds we use for the render pass should be of the granularity supported
2378 // by the device.
2379 const VkExtent2D& granularity = renderPass->granularity();
2380 SkIRect adjustedBounds;
2381 if ((0 != granularity.width && 1 != granularity.width) ||
2382 (0 != granularity.height && 1 != granularity.height)) {
2383 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2384 target->width(), target->height());
2385 pBounds = &adjustedBounds;
2386 }
2387
Robert Phillips95214472017-08-08 18:00:03 -04002388#ifdef SK_DEBUG
2389 uint32_t index;
2390 bool result = renderPass->colorAttachmentIndex(&index);
2391 SkASSERT(result && 0 == index);
2392 result = renderPass->stencilAttachmentIndex(&index);
2393 if (result) {
2394 SkASSERT(1 == index);
2395 }
2396#endif
2397 VkClearValue clears[2];
2398 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002399 clears[1].depthStencil.depth = 0.0f;
2400 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002401
2402 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002403 for (int i = 0; i < buffers.count(); ++i) {
2404 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2405 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002406 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002407
Brian Salomon1fabd512018-02-09 09:54:25 -05002408 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002409}
egdaniel9cb63402016-06-23 08:37:05 -07002410
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002411void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2412 if (buffer->asRTCommandBuffer()) {
2413 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2414
2415 fCachedRTCommandBuffer->submit();
2416 fCachedRTCommandBuffer->reset();
2417 } else {
2418 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2419
2420 fCachedTexCommandBuffer->submit();
2421 fCachedTexCommandBuffer->reset();
2422 }
2423}
2424
Greg Daniel6be35232017-03-01 17:01:09 -05002425GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002426 VkFenceCreateInfo createInfo;
2427 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2428 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2429 createInfo.pNext = nullptr;
2430 createInfo.flags = 0;
2431 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002432
2433 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2434 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2435
2436 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002437 return (GrFence)fence;
2438}
2439
Greg Daniel6be35232017-03-01 17:01:09 -05002440bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2441 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2442
2443 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002444 return (VK_SUCCESS == result);
2445}
2446
2447void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002448 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2449}
2450
Greg Daniela5cb7812017-06-16 09:45:32 -04002451sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2452 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002453}
2454
Greg Daniel48661b82018-01-22 16:11:35 -05002455sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2456 GrResourceProvider::SemaphoreWrapType wrapType,
2457 GrWrapOwnership ownership) {
2458 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002459}
2460
Greg Daniel858e12c2018-12-06 11:11:37 -05002461void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002462 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2463
Greg Daniel48661b82018-01-22 16:11:35 -05002464 GrVkSemaphore::Resource* resource = vkSem->getResource();
2465 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002466 resource->ref();
2467 fSemaphoresToSignal.push_back(resource);
2468 }
Greg Daniel6be35232017-03-01 17:01:09 -05002469}
2470
Greg Daniel48661b82018-01-22 16:11:35 -05002471void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002472 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2473
Greg Daniel48661b82018-01-22 16:11:35 -05002474 GrVkSemaphore::Resource* resource = vkSem->getResource();
2475 if (resource->shouldWait()) {
2476 resource->ref();
2477 fSemaphoresToWaitOn.push_back(resource);
2478 }
jvanverth84741b32016-09-30 08:39:02 -07002479}
Brian Osman13dddce2017-05-09 13:19:50 -04002480
2481sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2482 SkASSERT(texture);
2483 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2484 vkTexture->setImageLayout(this,
2485 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2486 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002487 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002488 false);
2489 this->submitCommandBuffer(kSkip_SyncQueue);
2490
2491 // The image layout change serves as a barrier, so no semaphore is needed
2492 return nullptr;
2493}
Greg Danielf5d87582017-12-18 14:48:15 -05002494
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002495void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2496 fDrawables.emplace_back(std::move(drawable));
2497}
2498
Greg Daniel7a82edf2018-12-04 10:54:34 -05002499uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2500 const GrBackendFormat& format) {
2501 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2502 SkASSERT(ycbcrInfo);
2503 if (!ycbcrInfo->isValid()) {
2504 return 0;
2505 }
2506
2507 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2508 samplerState, *ycbcrInfo);
2509
2510 return sampler->uniqueID();
2511}
2512
Greg Daniela870b462019-01-08 15:49:46 -05002513void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002514 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002515 this->resourceProvider().storePipelineCacheData();
2516 }
2517}