blob: 3742fa4cbb42cf1b2959a7d327c5b97b252460cc [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 Daniela3aa75a2019-04-12 14:24:55 -0400319void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
320 GrGpuFinishedContext finishedContext) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500321 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400322
323 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
324 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
325 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400326 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400327 if (finishedProc) {
328 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
329 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400330 return;
331 }
332
Greg Daniel164a9f02016-02-22 09:56:40 -0500333 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500334 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400335 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500336
Greg Daniela3aa75a2019-04-12 14:24:55 -0400337 if (finishedProc) {
338 // Make sure this is called after closing the current command pool
339 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
340 }
341
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400342 // We must delete and drawables that have been waitint till submit for us to destroy.
343 fDrawables.reset();
344
Greg Daniel6be35232017-03-01 17:01:09 -0500345 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
346 fSemaphoresToWaitOn[i]->unref(this);
347 }
348 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400349 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
350 fSemaphoresToSignal[i]->unref(this);
351 }
352 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500353
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500354 // Release old command pool and create a new one
355 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500356 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500357 fCmdPool = fResourceProvider.findOrCreateCommandPool();
358 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500359 fCurrentCmdBuffer->begin(this);
360}
361
362///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500363sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
364 GrAccessPattern accessPattern, const void* data) {
365 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700366 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500367 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700368 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
369 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500370 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700371 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500372 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700373 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
374 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500375 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700376 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500377 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400378 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
379 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500380 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700381 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500382 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400383 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
384 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500385 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700386 break;
cdalton397536c2016-03-25 12:15:03 -0700387 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400388 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700389 return nullptr;
390 }
cdalton1bf3e712016-04-19 10:00:02 -0700391 if (data && buff) {
392 buff->updateData(data, size);
393 }
394 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500395}
396
Brian Salomona9b04b92018-06-01 15:04:28 -0400397bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
398 GrColorType srcColorType, const GrMipLevel texels[],
399 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500400 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
401 if (!vkTex) {
402 return false;
403 }
404
jvanverth900bd4a2016-04-29 13:53:12 -0700405 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400406 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800407 return false;
408 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800409
Jim Van Verth1676cb92019-01-15 13:24:45 -0500410 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500411 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400412 bool linearTiling = vkTex->isLinearTiled();
413 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400414 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400415 SkDebugf("Can't upload mipmap data to linear tiled texture");
416 return false;
417 }
418 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
419 // Need to change the layout to general in order to perform a host write
420 vkTex->setImageLayout(this,
421 VK_IMAGE_LAYOUT_GENERAL,
422 VK_ACCESS_HOST_WRITE_BIT,
423 VK_PIPELINE_STAGE_HOST_BIT,
424 false);
425 this->submitCommandBuffer(kForce_SyncQueue);
426 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400427 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400428 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500429 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400430 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400431 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
432 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500433 }
egdaniel4583ec52016-06-27 12:57:00 -0700434
jvanverth900bd4a2016-04-29 13:53:12 -0700435 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500436}
437
Brian Salomone05ba5a2019-04-08 11:59:07 -0400438bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
439 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
440 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500441 // Can't transfer compressed data
442 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
443
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400444 // Vulkan only supports 4-byte aligned offsets
445 if (SkToBool(bufferOffset & 0x2)) {
446 return false;
447 }
448 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
449 if (!vkTex) {
450 return false;
451 }
452 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
453 if (!vkBuffer) {
454 return false;
455 }
456
Greg Daniel660cc992017-06-26 14:55:05 -0400457 SkDEBUGCODE(
458 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
459 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
460 SkASSERT(bounds.contains(subRect));
461 )
Brian Salomonc320b152018-02-20 14:05:36 -0500462 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400463 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500464 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400465 }
466
467 // Set up copy region
468 VkBufferImageCopy region;
469 memset(&region, 0, sizeof(VkBufferImageCopy));
470 region.bufferOffset = bufferOffset;
471 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
472 region.bufferImageHeight = 0;
473 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
474 region.imageOffset = { left, top, 0 };
475 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
476
477 // Change layout of our target so it can be copied to
478 vkTex->setImageLayout(this,
479 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
480 VK_ACCESS_TRANSFER_WRITE_BIT,
481 VK_PIPELINE_STAGE_TRANSFER_BIT,
482 false);
483
484 // Copy the buffer to the image
485 fCurrentCmdBuffer->copyBufferToImage(this,
486 vkBuffer,
487 vkTex,
488 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
489 1,
490 &region);
491
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400492 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400493 return true;
494}
495
Brian Salomon26de56e2019-04-10 12:14:26 -0400496bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
497 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
498 size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400499 SkASSERT(surface);
500 SkASSERT(transferBuffer);
501
Brian Salomona585fe92019-04-09 14:57:00 -0400502 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
503
504 GrVkImage* srcImage;
505 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
506 // Reading from render targets that wrap a secondary command buffer is not allowed since
507 // it would require us to know the VkImage, which we don't have, as well as need us to
508 // stop and start the VkRenderPass which we don't have access to.
509 if (rt->wrapsSecondaryCommandBuffer()) {
510 return false;
511 }
512 // resolve the render target if necessary
513 switch (rt->getResolveType()) {
514 case GrVkRenderTarget::kCantResolve_ResolveType:
515 return false;
516 case GrVkRenderTarget::kAutoResolves_ResolveType:
517 break;
518 case GrVkRenderTarget::kCanResolve_ResolveType:
519 this->resolveRenderTargetNoFlush(rt);
520 break;
521 default:
522 SK_ABORT("Unknown resolve type");
523 }
524 srcImage = rt;
525 } else {
526 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
527 }
528
529 // Set up copy region
530 VkBufferImageCopy region;
531 memset(&region, 0, sizeof(VkBufferImageCopy));
532 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400533 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400534 region.bufferImageHeight = 0;
535 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
536 region.imageOffset = { left, top, 0 };
537 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
538
539 srcImage->setImageLayout(this,
540 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
541 VK_ACCESS_TRANSFER_READ_BIT,
542 VK_PIPELINE_STAGE_TRANSFER_BIT,
543 false);
544
545 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
546 vkBuffer, 1, &region);
547
548 // Make sure the copy to buffer has finished.
549 vkBuffer->addMemoryBarrier(this,
550 VK_ACCESS_TRANSFER_WRITE_BIT,
551 VK_ACCESS_HOST_READ_BIT,
552 VK_PIPELINE_STAGE_TRANSFER_BIT,
553 VK_PIPELINE_STAGE_HOST_BIT,
554 false);
555
556 // The caller is responsible for syncing.
557 this->submitCommandBuffer(kSkip_SyncQueue);
558
Brian Salomon26de56e2019-04-10 12:14:26 -0400559 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400560}
561
Brian Salomon1fabd512018-02-09 09:54:25 -0500562void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
563 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700564 SkASSERT(dst);
565 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
566
egdaniel4bcd62e2016-08-31 07:37:31 -0700567 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500568 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
569 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
570 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
571 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
572 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700573
Greg Danielbc26c392017-04-18 13:32:10 -0400574 GrVkImage* dstImage;
575 GrRenderTarget* dstRT = dst->asRenderTarget();
576 if (dstRT) {
577 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400578 dstImage = vkRT;
579 } else {
580 SkASSERT(dst->asTexture());
581 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
582 }
583 dstImage->setImageLayout(this,
584 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
585 VK_ACCESS_TRANSFER_WRITE_BIT,
586 VK_PIPELINE_STAGE_TRANSFER_BIT,
587 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700588
589 src->msaaImage()->setImageLayout(this,
590 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
591 VK_ACCESS_TRANSFER_READ_BIT,
592 VK_PIPELINE_STAGE_TRANSFER_BIT,
593 false);
594
Greg Danielbc26c392017-04-18 13:32:10 -0400595 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700596}
597
Brian Salomon1fabd512018-02-09 09:54:25 -0500598void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700599 if (target->needsResolve()) {
600 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700601 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
602 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500603
egdaniel4bcd62e2016-08-31 07:37:31 -0700604 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700605
Brian Salomon1fabd512018-02-09 09:54:25 -0500606 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700607
608 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500609
610 if (requiresSubmit) {
611 this->submitCommandBuffer(kSkip_SyncQueue);
612 }
egdaniel52ad2512016-08-04 12:50:01 -0700613 }
614}
615
Brian Salomona9b04b92018-06-01 15:04:28 -0400616bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
617 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500618 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700619 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500620
Jim Van Verth1676cb92019-01-15 13:24:45 -0500621 // If we're uploading compressed data then we should be using uploadCompressedTexData
622 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
623 GrSRGBEncoded::kNo)));
624
Greg Daniel660cc992017-06-26 14:55:05 -0400625 SkDEBUGCODE(
626 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
627 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
628 SkASSERT(bounds.contains(subRect));
629 )
Brian Salomonc320b152018-02-20 14:05:36 -0500630 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500631 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400632 if (!rowBytes) {
633 rowBytes = trimRowBytes;
634 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500635
jvanverth900bd4a2016-04-29 13:53:12 -0700636 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
637 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
638 const VkImageSubresource subres = {
639 VK_IMAGE_ASPECT_COLOR_BIT,
640 0, // mipLevel
641 0, // arraySlice
642 };
643 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500644
jvanverth900bd4a2016-04-29 13:53:12 -0700645 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500646
jvanverth900bd4a2016-04-29 13:53:12 -0700647 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700648 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700649 &subres,
650 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500651
jvanverth1e305ba2016-06-01 09:39:15 -0700652 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400653 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700654 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400655 SkASSERT(size + offset <= alloc.fSize);
656 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
657 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700658 return false;
659 }
Greg Daniel81df0412018-05-31 13:13:33 -0400660 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700661
Brian Salomona9b04b92018-06-01 15:04:28 -0400662 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
663 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700664
Greg Daniele35a99e2018-03-02 11:44:22 -0500665 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400666 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700667
668 return true;
669}
670
Brian Salomona9b04b92018-06-01 15:04:28 -0400671bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
672 GrColorType dataColorType, const GrMipLevel texels[],
673 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700674 SkASSERT(!tex->isLinearTiled());
675 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400676 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700677 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
678
Greg Danieldd20e912017-04-07 14:42:23 -0400679 // We assume that if the texture has mip levels, we either upload to all the levels or just the
680 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400681 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400682
Jim Van Verth1676cb92019-01-15 13:24:45 -0500683 // If we're uploading compressed data then we should be using uploadCompressedTexData
684 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
685 GrSRGBEncoded::kNo)));
686
jvanverth900bd4a2016-04-29 13:53:12 -0700687 if (width == 0 || height == 0) {
688 return false;
689 }
690
Greg Daniel475eb702018-09-28 14:16:50 -0400691 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
692 return false;
693 }
694
695 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
696 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500697 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
698 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400699 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
700 // blit or draw.
701 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
702 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
703 return false;
704 }
705 mipLevelCount = 1;
706 }
707
Brian Salomond1eaf492017-05-18 10:02:08 -0400708 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500709 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700710
711 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700712 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
713 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400714 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
715
Greg Daniel475eb702018-09-28 14:16:50 -0400716 texelsShallowCopy.reset(mipLevelCount);
717 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700718
Robert Phillips590533f2017-07-11 14:22:35 -0400719 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700720 individualMipOffsets.push_back(0);
721 size_t combinedBufferSize = width * bpp * height;
722 int currentWidth = width;
723 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400724 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400725 combinedBufferSize = 0;
726 }
727
Greg Daniel468fd632017-03-22 17:03:45 -0400728 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
729 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
730 SkASSERT((bpp & (bpp - 1)) == 0);
731 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400732 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700733 currentWidth = SkTMax(1, currentWidth/2);
734 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400735
Greg Daniel55afd6d2017-09-29 09:32:44 -0400736 if (texelsShallowCopy[currentMipLevel].fPixels) {
737 const size_t trimmedSize = currentWidth * bpp * currentHeight;
738 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
739 if (alignmentDiff != 0) {
740 combinedBufferSize += alignmentMask - alignmentDiff + 1;
741 }
742 individualMipOffsets.push_back(combinedBufferSize);
743 combinedBufferSize += trimmedSize;
744 } else {
745 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400746 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400747 }
748 if (0 == combinedBufferSize) {
749 // We don't actually have any data to upload so just return success
750 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700751 }
752
753 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500754 sk_sp<GrVkTransferBuffer> transferBuffer =
755 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400756 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700757 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400758 }
jvanverth900bd4a2016-04-29 13:53:12 -0700759
Greg Daniel475eb702018-09-28 14:16:50 -0400760 int uploadLeft = left;
761 int uploadTop = top;
762 GrVkTexture* uploadTexture = tex;
763 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
764 // R8G8B8A8_UNORM image and then copy it.
765 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500766 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400767 GrSurfaceDesc surfDesc;
768 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
769 surfDesc.fWidth = width;
770 surfDesc.fHeight = height;
771 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
772 surfDesc.fSampleCnt = 1;
773
774 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
775 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
776 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
777
778 GrVkImage::ImageDesc imageDesc;
779 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
780 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
781 imageDesc.fWidth = width;
782 imageDesc.fHeight = height;
783 imageDesc.fLevels = 1;
784 imageDesc.fSamples = 1;
785 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
786 imageDesc.fUsageFlags = usageFlags;
787 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
788
789 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
790 GrMipMapsStatus::kNotAllocated);
791 if (!copyTexture) {
792 return false;
793 }
794 uploadTexture = copyTexture.get();
795 uploadLeft = 0;
796 uploadTop = 0;
797 }
798
jvanverth900bd4a2016-04-29 13:53:12 -0700799 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400800 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700801
jvanverthc578b0632016-05-02 10:58:12 -0700802 currentWidth = width;
803 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400804 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400805 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400806 if (texelsShallowCopy[currentMipLevel].fPixels) {
807 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
808 const size_t trimRowBytes = currentWidth * bpp;
809 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
810 ? texelsShallowCopy[currentMipLevel].fRowBytes
811 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700812
Greg Daniel55afd6d2017-09-29 09:32:44 -0400813 // copy data into the buffer, skipping the trailing bytes
814 char* dst = buffer + individualMipOffsets[currentMipLevel];
815 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400816 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400817
818 VkBufferImageCopy& region = regions.push_back();
819 memset(&region, 0, sizeof(VkBufferImageCopy));
820 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
821 region.bufferRowLength = currentWidth;
822 region.bufferImageHeight = currentHeight;
823 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400824 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400825 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700826 }
jvanverthc578b0632016-05-02 10:58:12 -0700827 currentWidth = SkTMax(1, currentWidth/2);
828 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400829 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700830 }
831
jvanverth9d54afc2016-09-20 09:20:03 -0700832 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700833 transferBuffer->unmap();
834
jvanverth900bd4a2016-04-29 13:53:12 -0700835 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400836 uploadTexture->setImageLayout(this,
837 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
838 VK_ACCESS_TRANSFER_WRITE_BIT,
839 VK_PIPELINE_STAGE_TRANSFER_BIT,
840 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700841
842 // Copy the buffer to the image
843 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500844 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400845 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700846 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
847 regions.count(),
848 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400849
850 // If we copied the data into a temporary image first, copy that image into our main texture
851 // now.
852 if (copyTexture.get()) {
853 SkASSERT(dataColorType == GrColorType::kRGB_888x);
854 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
855 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
856 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
857 false));
858 }
Robert Phillips590533f2017-07-11 14:22:35 -0400859 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400860 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400861 }
jvanverth900bd4a2016-04-29 13:53:12 -0700862
Greg Daniel164a9f02016-02-22 09:56:40 -0500863 return true;
864}
865
Jim Van Verth1676cb92019-01-15 13:24:45 -0500866// It's probably possible to roll this into uploadTexDataOptimal,
867// but for now it's easier to maintain as a separate entity.
868bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
869 GrColorType dataColorType, const GrMipLevel texels[],
870 int mipLevelCount) {
871 SkASSERT(!tex->isLinearTiled());
872 // For now the assumption is that our rect is the entire texture.
873 // Compressed textures are read-only so this should be a reasonable assumption.
874 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
875
876 // We assume that if the texture has mip levels, we either upload to all the levels or just the
877 // first.
878 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
879
880 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
881 GrSRGBEncoded::kNo)));
882
883 if (width == 0 || height == 0) {
884 return false;
885 }
886
887 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
888 return false;
889 }
890
891 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
892
893 SkTArray<size_t> individualMipOffsets(mipLevelCount);
894 individualMipOffsets.push_back(0);
895 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
896 int currentWidth = width;
897 int currentHeight = height;
898 if (!texels[0].fPixels) {
899 return false;
900 }
901
902 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
903 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
904 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
905 currentWidth = SkTMax(1, currentWidth / 2);
906 currentHeight = SkTMax(1, currentHeight / 2);
907
908 if (texels[currentMipLevel].fPixels) {
909 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
910 currentHeight);
911 individualMipOffsets.push_back(combinedBufferSize);
912 combinedBufferSize += dataSize;
913 } else {
914 return false;
915 }
916 }
917 if (0 == combinedBufferSize) {
918 // We don't have any data to upload so fail (compressed textures are read-only).
919 return false;
920 }
921
922 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500923 sk_sp<GrVkTransferBuffer> transferBuffer =
924 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500925 if (!transferBuffer) {
926 return false;
927 }
928
929 int uploadLeft = left;
930 int uploadTop = top;
931 GrVkTexture* uploadTexture = tex;
932
933 char* buffer = (char*)transferBuffer->map();
934 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
935
936 currentWidth = width;
937 currentHeight = height;
938 int layerHeight = uploadTexture->height();
939 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
940 if (texels[currentMipLevel].fPixels) {
941 // Again, we're assuming that our rect is the entire texture
942 SkASSERT(currentHeight == layerHeight);
943 SkASSERT(0 == uploadLeft && 0 == uploadTop);
944
945 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
946 currentHeight);
947
948 // copy data into the buffer, skipping the trailing bytes
949 char* dst = buffer + individualMipOffsets[currentMipLevel];
950 const char* src = (const char*)texels[currentMipLevel].fPixels;
951 memcpy(dst, src, dataSize);
952
953 VkBufferImageCopy& region = regions.push_back();
954 memset(&region, 0, sizeof(VkBufferImageCopy));
955 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
956 region.bufferRowLength = currentWidth;
957 region.bufferImageHeight = currentHeight;
958 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
959 region.imageOffset = { uploadLeft, uploadTop, 0 };
960 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
961 }
962 currentWidth = SkTMax(1, currentWidth / 2);
963 currentHeight = SkTMax(1, currentHeight / 2);
964 layerHeight = currentHeight;
965 }
966
967 // no need to flush non-coherent memory, unmap will do that for us
968 transferBuffer->unmap();
969
970 // Change layout of our target so it can be copied to
971 uploadTexture->setImageLayout(this,
972 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
973 VK_ACCESS_TRANSFER_WRITE_BIT,
974 VK_PIPELINE_STAGE_TRANSFER_BIT,
975 false);
976
977 // Copy the buffer to the image
978 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500979 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500980 uploadTexture,
981 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
982 regions.count(),
983 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500984
985 if (1 == mipLevelCount) {
986 tex->texturePriv().markMipMapsDirty();
987 }
988
989 return true;
990}
991
Greg Daniel164a9f02016-02-22 09:56:40 -0500992////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400993sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500994 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500995 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
996
997 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500998 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700999
Greg Daniel164a9f02016-02-22 09:56:40 -05001000 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1001 if (renderTarget) {
1002 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1003 }
1004
1005 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1006 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1007 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001008 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001009 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1010 // texture.
1011 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1012
Greg Daniel164a9f02016-02-22 09:56:40 -05001013 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001014 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001015 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001016 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001017 GrVkImage::ImageDesc imageDesc;
1018 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1019 imageDesc.fFormat = pixelFormat;
1020 imageDesc.fWidth = desc.fWidth;
1021 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001022 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001023 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001024 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001025 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001026 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001027
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001028 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1029 if (mipLevels > 1) {
1030 mipMapsStatus = GrMipMapsStatus::kValid;
1031 for (int i = 0; i < mipLevels; ++i) {
1032 if (!texels[i].fPixels) {
1033 mipMapsStatus = GrMipMapsStatus::kDirty;
1034 break;
1035 }
Greg Daniel834f1202017-10-09 15:06:20 -04001036 }
1037 }
1038
Robert Phillips67d52cf2017-06-05 13:38:13 -04001039 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001040 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001041 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1042 imageDesc,
1043 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001044 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001045 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001046 }
1047
1048 if (!tex) {
1049 return nullptr;
1050 }
1051
Jim Van Verth1676cb92019-01-15 13:24:45 -05001052 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001053 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001054 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001055 bool success;
1056 if (isCompressed) {
1057 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1058 colorType, texels, mipLevelCount);
1059 } else {
1060 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1061 colorType, texels, mipLevelCount);
1062 }
1063 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001064 tex->unref();
1065 return nullptr;
1066 }
1067 }
1068
Jim Van Verth1676cb92019-01-15 13:24:45 -05001069 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001070 VkClearColorValue zeroClearColor;
1071 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1072 VkImageSubresourceRange range;
1073 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1074 range.baseArrayLayer = 0;
1075 range.baseMipLevel = 0;
1076 range.layerCount = 1;
1077 range.levelCount = 1;
1078 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1079 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001080 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001081 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001082 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001083}
1084
1085////////////////////////////////////////////////////////////////////////////////
1086
Greg Daniel6888c0d2017-08-25 11:55:50 -04001087void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1088 VkDeviceSize dstOffset, VkDeviceSize size) {
1089 VkBufferCopy copyRegion;
1090 copyRegion.srcOffset = srcOffset;
1091 copyRegion.dstOffset = dstOffset;
1092 copyRegion.size = size;
1093 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1094}
1095
jvanverthdb379092016-07-07 11:18:46 -07001096bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1097 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001098 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001099 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001100
1101 return true;
1102}
1103
1104////////////////////////////////////////////////////////////////////////////////
1105
Greg Daniel7e000222018-12-03 10:08:21 -05001106static bool check_image_info(const GrVkCaps& caps,
1107 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001108 GrPixelConfig config,
1109 bool isWrappedRT) {
1110 if (VK_NULL_HANDLE == info.fImage) {
1111 return false;
1112 }
1113
1114 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001115 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001116 }
1117
Greg Daniel7e000222018-12-03 10:08:21 -05001118 if (info.fYcbcrConversionInfo.isValid()) {
1119 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1120 return false;
1121 }
jvanverthfd359ca2016-03-18 11:57:24 -07001122 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001123
Greg Danielcb324152019-02-25 11:36:53 -05001124 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1125 return false;
1126 }
1127
Greg Daniel52e16d92018-04-10 09:34:07 -04001128 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001129 return true;
1130}
1131
1132sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001133 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1134 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001135 GrVkImageInfo imageInfo;
1136 if (!backendTex.getVkImageInfo(&imageInfo)) {
1137 return nullptr;
1138 }
1139
Greg Danielcb324152019-02-25 11:36:53 -05001140 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001141 return nullptr;
1142 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001143
Greg Daniel164a9f02016-02-22 09:56:40 -05001144 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001145 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001146 surfDesc.fWidth = backendTex.width();
1147 surfDesc.fHeight = backendTex.height();
1148 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001149 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001150
Greg Daniel52e16d92018-04-10 09:34:07 -04001151 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1152 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001153 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1154 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001155}
1156
1157sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001158 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001159 GrWrapOwnership ownership,
1160 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001161 GrVkImageInfo imageInfo;
1162 if (!backendTex.getVkImageInfo(&imageInfo)) {
1163 return nullptr;
1164 }
1165
Greg Danielcb324152019-02-25 11:36:53 -05001166 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001167 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001168 }
Brian Salomond17f6582017-07-19 18:28:58 -04001169
1170 GrSurfaceDesc surfDesc;
1171 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1172 surfDesc.fWidth = backendTex.width();
1173 surfDesc.fHeight = backendTex.height();
1174 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001175 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001176
Greg Daniel52e16d92018-04-10 09:34:07 -04001177 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1178 SkASSERT(layout);
1179
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001180 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1181 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001182}
1183
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001184sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001185 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1186 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1187 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1188 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001189 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001190 return nullptr;
1191 }
halcanary9d524f22016-03-29 09:03:52 -07001192
Greg Daniel323fbcf2018-04-10 13:46:30 -04001193 GrVkImageInfo info;
1194 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001195 return nullptr;
1196 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001197
Greg Danielcb324152019-02-25 11:36:53 -05001198 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001199 return nullptr;
1200 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001201
Greg Daniel164a9f02016-02-22 09:56:40 -05001202 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001203 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001204 desc.fWidth = backendRT.width();
1205 desc.fHeight = backendRT.height();
1206 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001207 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001208
Greg Daniel323fbcf2018-04-10 13:46:30 -04001209 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001210
Greg Daniel323fbcf2018-04-10 13:46:30 -04001211 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001212 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001213
1214 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1215 SkASSERT(!backendRT.stencilBits());
1216 if (tgt) {
1217 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001218 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001219
Ben Wagnerff134f22018-04-24 16:29:16 -04001220 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001221}
1222
Greg Daniel7ef28f32017-04-20 16:41:55 +00001223sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001224 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001225
Greg Daniel52e16d92018-04-10 09:34:07 -04001226 GrVkImageInfo imageInfo;
1227 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001228 return nullptr;
1229 }
Greg Danielcb324152019-02-25 11:36:53 -05001230 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001231 return nullptr;
1232 }
1233
Greg Danielcb324152019-02-25 11:36:53 -05001234
Brian Osman33910292017-04-18 14:38:53 -04001235 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001236 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001237 desc.fWidth = tex.width();
1238 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001239 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001240 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1241 if (!desc.fSampleCnt) {
1242 return nullptr;
1243 }
Brian Osman33910292017-04-18 14:38:53 -04001244
Greg Daniel52e16d92018-04-10 09:34:07 -04001245 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1246 SkASSERT(layout);
1247
Ben Wagnerff134f22018-04-24 16:29:16 -04001248 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001249}
1250
Greg Danielb46add82019-01-02 14:51:29 -05001251sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1252 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1253 int maxSize = this->caps()->maxTextureSize();
1254 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1255 return nullptr;
1256 }
1257
1258 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1259 if (!backendFormat.isValid()) {
1260 return nullptr;
1261 }
1262 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1263 imageInfo.colorType());
1264 if (config == kUnknown_GrPixelConfig) {
1265 return nullptr;
1266 }
1267
1268 GrSurfaceDesc desc;
1269 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1270 desc.fWidth = imageInfo.width();
1271 desc.fHeight = imageInfo.height();
1272 desc.fConfig = config;
1273 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1274 if (!desc.fSampleCnt) {
1275 return nullptr;
1276 }
1277
1278 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1279}
1280
Brian Salomon930f9392018-06-20 16:25:26 -04001281bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1282 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001283 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001284 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001285 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001286 return false;
jvanverth62340062016-04-26 08:01:44 -07001287 }
1288
jvanverth62340062016-04-26 08:01:44 -07001289 // determine if we can blit to and from this format
1290 const GrVkCaps& caps = this->vkCaps();
1291 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001292 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1293 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001294 return false;
jvanverth62340062016-04-26 08:01:44 -07001295 }
1296
egdaniel7ac5da82016-07-15 13:41:42 -07001297 int width = tex->width();
1298 int height = tex->height();
1299 VkImageBlit blitRegion;
1300 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001301
jvanverth82c05582016-05-03 11:19:01 -07001302 // SkMipMap doesn't include the base level in the level count so we have to add 1
1303 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001304 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001305
Greg Danielda86e282018-06-13 09:41:19 -04001306 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001307 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1308 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001309
jvanverth50c46c72016-05-06 12:31:28 -07001310 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001311 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001312 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1313 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001314 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1315 nullptr, // pNext
1316 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1317 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1318 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1319 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1320 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1321 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1322 vkTex->image(), // image
1323 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001324 };
1325
jvanverth62340062016-04-26 08:01:44 -07001326 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001327 uint32_t mipLevel = 1;
1328 while (mipLevel < levelCount) {
1329 int prevWidth = width;
1330 int prevHeight = height;
1331 width = SkTMax(1, width / 2);
1332 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001333
jvanverth50c46c72016-05-06 12:31:28 -07001334 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001335 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1336 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001337
1338 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001339 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001340 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001341 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1342 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001343 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001344 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001345 vkTex->resource(),
1346 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001347 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001348 vkTex->resource(),
1349 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001350 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001351 1,
1352 &blitRegion,
1353 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001354 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001355 }
Greg Danielee54f232019-04-03 14:58:40 -04001356 if (levelCount > 1) {
1357 // This barrier logically is not needed, but it changes the final level to the same layout
1358 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1359 // layouts and future layout changes easier. The alternative here would be to track layout
1360 // and memory accesses per layer which doesn't seem work it.
1361 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1362 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1363 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1364 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1365 }
Brian Salomon930f9392018-06-20 16:25:26 -04001366 return true;
jvanverth62340062016-04-26 08:01:44 -07001367}
1368
Greg Daniel164a9f02016-02-22 09:56:40 -05001369////////////////////////////////////////////////////////////////////////////////
1370
1371GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1372 int width,
1373 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001374 SkASSERT(width >= rt->width());
1375 SkASSERT(height >= rt->height());
1376
1377 int samples = rt->numStencilSamples();
1378
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001379 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001380
1381 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001382 width,
1383 height,
1384 samples,
1385 sFmt));
1386 fStats.incStencilAttachmentCreates();
1387 return stencil;
1388}
1389
1390////////////////////////////////////////////////////////////////////////////////
1391
Brian Salomon52e943a2018-03-13 09:32:39 -04001392bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001393 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1394 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001395 VkDeviceSize size = dstRowBytes * h;
1396 VkDeviceSize offset = bufferOffset;
1397 SkASSERT(size + offset <= alloc.fSize);
1398 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1399 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001400 return false;
1401 }
Greg Daniel81df0412018-05-31 13:13:33 -04001402 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001403
Greg Daniel20ece3a2017-03-28 10:24:43 -04001404 if (srcData) {
1405 // If there is no padding on dst we can do a single memcopy.
1406 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001407 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001408 } else {
1409 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1410 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001411 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001412 }
Greg Daniel81df0412018-05-31 13:13:33 -04001413 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1414 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001415 return true;
1416}
1417
Brian Salomonf865b052018-03-09 09:01:53 -05001418#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001419bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1420 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001421 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001422 SkASSERT(texturable || renderable);
1423 if (!texturable) {
1424 SkASSERT(GrMipMapped::kNo == mipMapped);
1425 SkASSERT(!srcData);
1426 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001427 VkFormat pixelFormat;
1428 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001429 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001430 }
1431
Brian Salomon52e943a2018-03-13 09:32:39 -04001432 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1433 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001434 }
1435
Brian Salomon52e943a2018-03-13 09:32:39 -04001436 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1437 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001438 }
1439
1440 // Currently we don't support uploading pixel data when mipped.
1441 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001442 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001443 }
1444
Brian Salomon52e943a2018-03-13 09:32:39 -04001445 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001446 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1447 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001448 if (texturable) {
1449 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1450 }
1451 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001452 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1453 }
1454
1455 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001456 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001457 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001458
1459 // Create Image
1460 VkSampleCountFlagBits vkSamples;
1461 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001462 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001463 }
1464
1465 // Figure out the number of mip levels.
1466 uint32_t mipLevels = 1;
1467 if (GrMipMapped::kYes == mipMapped) {
1468 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1469 }
1470
1471 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001472 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1473 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001474 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001475 VK_IMAGE_TYPE_2D, // VkImageType
1476 pixelFormat, // VkFormat
1477 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1478 mipLevels, // mipLevels
1479 1, // arrayLayers
1480 vkSamples, // samples
1481 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1482 usageFlags, // VkImageUsageFlags
1483 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1484 0, // queueFamilyCount
1485 0, // pQueueFamilyIndices
1486 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001487 };
1488
Brian Salomon52e943a2018-03-13 09:32:39 -04001489 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1490 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001491
Brian Salomonde9f5462018-03-07 14:23:58 -05001492 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001493 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001494 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001495 }
1496
1497 // 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 -05001498 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001499 VkBuffer buffer = VK_NULL_HANDLE;
1500
1501 VkResult err;
1502 const VkCommandBufferAllocateInfo cmdInfo = {
1503 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1504 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001505 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001506 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1507 1 // bufferCount
1508 };
1509
1510 VkCommandBuffer cmdBuffer;
1511 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1512 if (err) {
1513 GrVkMemory::FreeImageMemory(this, false, alloc);
1514 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001515 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001516 }
1517
1518 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1519 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1520 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1521 cmdBufferBeginInfo.pNext = nullptr;
1522 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1523 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1524
1525 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1526 SkASSERT(!err);
1527
1528 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001529 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001530
Robert Phillips646f6372018-09-25 09:31:10 -04001531 const size_t trimRowBytes = w * bpp;
1532 if (!srcRowBytes) {
1533 srcRowBytes = trimRowBytes;
1534 }
1535
Brian Salomonde9f5462018-03-07 14:23:58 -05001536 SkTArray<size_t> individualMipOffsets(mipLevels);
1537 individualMipOffsets.push_back(0);
1538 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001539 if (GrPixelConfigIsCompressed(config)) {
1540 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1541 bpp = 4; // we have at least this alignment, which will pass the code below
1542 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001543 int currentWidth = w;
1544 int currentHeight = h;
1545 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1546 // config. This works with the assumption that the bytes in pixel config is always a power
1547 // of 2.
1548 SkASSERT((bpp & (bpp - 1)) == 0);
1549 const size_t alignmentMask = 0x3 | (bpp - 1);
1550 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1551 currentWidth = SkTMax(1, currentWidth / 2);
1552 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001553
Jim Van Verth1676cb92019-01-15 13:24:45 -05001554 size_t trimmedSize;
1555 if (GrPixelConfigIsCompressed(config)) {
1556 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1557 } else {
1558 trimmedSize = currentWidth * bpp * currentHeight;
1559 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001560 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1561 if (alignmentDiff != 0) {
1562 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001563 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001564 individualMipOffsets.push_back(combinedBufferSize);
1565 combinedBufferSize += trimmedSize;
1566 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001567
Brian Salomonde9f5462018-03-07 14:23:58 -05001568 VkBufferCreateInfo bufInfo;
1569 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1570 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1571 bufInfo.flags = 0;
1572 bufInfo.size = combinedBufferSize;
1573 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1574 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1575 bufInfo.queueFamilyIndexCount = 0;
1576 bufInfo.pQueueFamilyIndices = nullptr;
1577 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001578
Brian Salomonde9f5462018-03-07 14:23:58 -05001579 if (err) {
1580 GrVkMemory::FreeImageMemory(this, false, alloc);
1581 VK_CALL(DestroyImage(fDevice, image, nullptr));
1582 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001583 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001584 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001585 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001586
Brian Salomonde9f5462018-03-07 14:23:58 -05001587 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1588 &bufferAlloc)) {
1589 GrVkMemory::FreeImageMemory(this, false, alloc);
1590 VK_CALL(DestroyImage(fDevice, image, nullptr));
1591 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1592 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001593 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001594 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001595 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001596
Brian Salomonde9f5462018-03-07 14:23:58 -05001597 currentWidth = w;
1598 currentHeight = h;
1599 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1600 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001601 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001602 bool result;
1603 if (GrPixelConfigIsCompressed(config)) {
1604 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1605 size_t currentRowBytes = levelSize / currentHeight;
1606 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1607 currentRowBytes, currentRowBytes, currentHeight);
1608 } else {
1609 size_t currentRowBytes = bpp * currentWidth;
1610 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1611 currentRowBytes, trimRowBytes, currentHeight);
1612 }
1613 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001614 GrVkMemory::FreeImageMemory(this, false, alloc);
1615 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001616 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001617 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1618 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001619 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001620 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001621 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001622 currentWidth = SkTMax(1, currentWidth / 2);
1623 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001624 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001625
1626 // Set image layout and add barrier
1627 VkImageMemoryBarrier barrier;
1628 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1629 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1630 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001631 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001632 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1633 barrier.oldLayout = initialLayout;
1634 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1635 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1636 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1637 barrier.image = image;
1638 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1639
Greg Danielf7828d02018-10-09 12:01:32 -04001640 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001641 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1642 &barrier));
1643 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1644
1645 SkTArray<VkBufferImageCopy> regions(mipLevels);
1646
1647 currentWidth = w;
1648 currentHeight = h;
1649 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1650 // Submit copy command
1651 VkBufferImageCopy& region = regions.push_back();
1652 memset(&region, 0, sizeof(VkBufferImageCopy));
1653 region.bufferOffset = individualMipOffsets[currentMipLevel];
1654 region.bufferRowLength = currentWidth;
1655 region.bufferImageHeight = currentHeight;
1656 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1657 region.imageOffset = {0, 0, 0};
1658 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1659 currentWidth = SkTMax(1, currentWidth / 2);
1660 currentHeight = SkTMax(1, currentHeight / 2);
1661 }
1662
1663 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1664 regions.begin()));
1665
Brian Salomon52e943a2018-03-13 09:32:39 -04001666 if (texturable) {
1667 // Change Image layout to shader read since if we use this texture as a borrowed textures
1668 // within Ganesh we require that its layout be set to that
1669 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1670 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1671 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001672 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001673 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1674 barrier.oldLayout = initialLayout;
1675 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1676 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1677 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1678 barrier.image = image;
1679 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001680 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001681 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1682 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001683 0,
1684 0, nullptr,
1685 0, nullptr,
1686 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001687 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001688 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001689
1690 // End CommandBuffer
1691 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1692 SkASSERT(!err);
1693
1694 // Create Fence for queue
1695 VkFence fence;
1696 VkFenceCreateInfo fenceInfo;
1697 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1698 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1699
1700 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1701 SkASSERT(!err);
1702
1703 VkSubmitInfo submitInfo;
1704 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1705 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1706 submitInfo.pNext = nullptr;
1707 submitInfo.waitSemaphoreCount = 0;
1708 submitInfo.pWaitSemaphores = nullptr;
1709 submitInfo.pWaitDstStageMask = 0;
1710 submitInfo.commandBufferCount = 1;
1711 submitInfo.pCommandBuffers = &cmdBuffer;
1712 submitInfo.signalSemaphoreCount = 0;
1713 submitInfo.pSignalSemaphores = nullptr;
1714 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1715 SkASSERT(!err);
1716
1717 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1718 if (VK_TIMEOUT == err) {
1719 GrVkMemory::FreeImageMemory(this, false, alloc);
1720 VK_CALL(DestroyImage(fDevice, image, nullptr));
1721 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1722 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001723 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001724 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1725 SkDebugf("Fence failed to signal: %d\n", err);
1726 SK_ABORT("failing");
1727 }
1728 SkASSERT(!err);
1729
1730 // Clean up transfer resources
1731 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1732 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1733 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1734 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001735 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001736 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1737
Brian Salomon52e943a2018-03-13 09:32:39 -04001738 info->fImage = image;
1739 info->fAlloc = alloc;
1740 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1741 info->fImageLayout = initialLayout;
1742 info->fFormat = pixelFormat;
1743 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001744
Brian Salomon52e943a2018-03-13 09:32:39 -04001745 return true;
1746}
1747
1748GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001749 GrColorType colorType,
1750 bool isRenderTarget,
1751 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001752 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001753
1754 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1755 return GrBackendTexture();
1756 }
1757
Robert Phillips646f6372018-09-25 09:31:10 -04001758 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1759 if (!this->caps()->isConfigTexturable(config)) {
1760 return GrBackendTexture();
1761 }
1762
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001763 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001764 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001765 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001766 return {};
1767 }
Greg Daniel108bb232018-07-03 16:18:29 -04001768 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1769 // Lots of tests don't go through Skia's public interface which will set the config so for
1770 // testing we make sure we set a config here.
1771 beTex.setPixelConfig(config);
1772 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001773}
1774
1775bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001776 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001777
Greg Daniel52e16d92018-04-10 09:34:07 -04001778 GrVkImageInfo backend;
1779 if (!tex.getVkImageInfo(&backend)) {
1780 return false;
1781 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001782
Greg Daniel52e16d92018-04-10 09:34:07 -04001783 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001784 VkMemoryRequirements req;
1785 memset(&req, 0, sizeof(req));
1786 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001787 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001788 &req));
1789 // TODO: find a better check
1790 // This will probably fail with a different driver
1791 return (req.size > 0) && (req.size <= 8192 * 8192);
1792 }
1793
1794 return false;
1795}
1796
Brian Salomon26102cb2018-03-09 09:33:19 -05001797void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001798 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001799
Greg Daniel52e16d92018-04-10 09:34:07 -04001800 GrVkImageInfo info;
1801 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001802 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001803 }
1804}
1805
Brian Osman2d010b62018-08-09 10:55:09 -04001806GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001807 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1808 return GrBackendRenderTarget();
1809 }
1810
Brian Salomon8a375832018-03-14 10:21:40 -04001811 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001812 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001813 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001814 if (kUnknown_GrPixelConfig == config) {
1815 return {};
1816 }
Robert Phillips646f6372018-09-25 09:31:10 -04001817 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001818 &info)) {
1819 return {};
1820 }
Greg Daniel108bb232018-07-03 16:18:29 -04001821 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1822 // Lots of tests don't go through Skia's public interface which will set the config so for
1823 // testing we make sure we set a config here.
1824 beRT.setPixelConfig(config);
1825 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001826}
1827
Brian Salomon52e943a2018-03-13 09:32:39 -04001828void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001829 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001830
Greg Daniel323fbcf2018-04-10 13:46:30 -04001831 GrVkImageInfo info;
1832 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001833 // something in the command buffer may still be using this, so force submit
1834 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001835 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001836 }
1837}
Brian Salomonf865b052018-03-09 09:01:53 -05001838
Greg Daniel26b50a42018-03-08 09:49:58 -05001839void GrVkGpu::testingOnly_flushGpuAndSync() {
1840 this->submitCommandBuffer(kForce_SyncQueue);
1841}
Brian Salomonf865b052018-03-09 09:01:53 -05001842#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001843
Greg Daniel164a9f02016-02-22 09:56:40 -05001844////////////////////////////////////////////////////////////////////////////////
1845
Greg Daniel59dc1482019-02-22 10:46:38 -05001846void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1847 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001848 VkPipelineStageFlags dstStageMask,
1849 bool byRegion,
1850 VkBufferMemoryBarrier* barrier) const {
1851 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001852 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001853 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001854 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001855 srcStageMask,
1856 dstStageMask,
1857 byRegion,
1858 GrVkCommandBuffer::kBufferMemory_BarrierType,
1859 barrier);
1860}
1861
Greg Daniel59dc1482019-02-22 10:46:38 -05001862void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1863 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001864 VkPipelineStageFlags dstStageMask,
1865 bool byRegion,
1866 VkImageMemoryBarrier* barrier) const {
1867 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001868 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001869 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001870 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001871 srcStageMask,
1872 dstStageMask,
1873 byRegion,
1874 GrVkCommandBuffer::kImageMemory_BarrierType,
1875 barrier);
1876}
1877
Greg Danielbae71212019-03-01 15:24:35 -05001878void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001879 const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +00001880 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1881 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001882 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1883 GrVkImage* image;
1884 SkASSERT(proxy->isInstantiated());
1885 if (GrTexture* tex = proxy->peekTexture()) {
1886 image = static_cast<GrVkTexture*>(tex);
1887 } else {
1888 GrRenderTarget* rt = proxy->peekRenderTarget();
1889 SkASSERT(rt);
1890 image = static_cast<GrVkRenderTarget*>(rt);
1891 }
1892 image->prepareForPresent(this);
1893 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001894 if (info.fFlags & kSyncCpu_GrFlushFlag) {
1895 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001896 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001897 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001898 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001899}
1900
Greg Daniel25af6712018-04-25 10:44:38 -04001901static int get_surface_sample_cnt(GrSurface* surf) {
1902 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1903 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001904 }
Greg Daniel25af6712018-04-25 10:44:38 -04001905 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001906}
1907
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001908void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1909 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001910 GrVkImage* dstImage,
1911 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001912 const SkIRect& srcRect,
1913 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001914#ifdef SK_DEBUG
1915 int dstSampleCnt = get_surface_sample_cnt(dst);
1916 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001917 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1918 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1919 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1920 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001921
1922#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001923
Greg Daniel164a9f02016-02-22 09:56:40 -05001924 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1925 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001926 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001927 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1928 VK_ACCESS_TRANSFER_WRITE_BIT,
1929 VK_PIPELINE_STAGE_TRANSFER_BIT,
1930 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001931
egdaniel17b89252016-04-05 07:23:38 -07001932 srcImage->setImageLayout(this,
1933 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001934 VK_ACCESS_TRANSFER_READ_BIT,
1935 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001936 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001937
1938 // Flip rect if necessary
1939 SkIRect srcVkRect = srcRect;
1940 int32_t dstY = dstPoint.fY;
1941
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001942 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1943 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001944 srcVkRect.fTop = src->height() - srcRect.fBottom;
1945 srcVkRect.fBottom = src->height() - srcRect.fTop;
1946 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1947 }
1948
1949 VkImageCopy copyRegion;
1950 memset(&copyRegion, 0, sizeof(VkImageCopy));
1951 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1952 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1953 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1954 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001955 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001956
1957 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001958 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001959 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001960 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001961 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1962 1,
1963 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001964
1965 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1966 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001967 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001968}
1969
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001970void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1971 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001972 GrVkImage* dstImage,
1973 GrVkImage* srcImage,
1974 const SkIRect& srcRect,
1975 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001976#ifdef SK_DEBUG
1977 int dstSampleCnt = get_surface_sample_cnt(dst);
1978 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001979 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1980 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04001981 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04001982 dstHasYcbcr, src->config(), srcSampleCnt,
1983 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07001984
Greg Daniel25af6712018-04-25 10:44:38 -04001985#endif
egdaniel17b89252016-04-05 07:23:38 -07001986 dstImage->setImageLayout(this,
1987 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001988 VK_ACCESS_TRANSFER_WRITE_BIT,
1989 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001990 false);
1991
egdaniel17b89252016-04-05 07:23:38 -07001992 srcImage->setImageLayout(this,
1993 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001994 VK_ACCESS_TRANSFER_READ_BIT,
1995 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001996 false);
1997
1998 // Flip rect if necessary
1999 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002000 srcVkRect.fLeft = srcRect.fLeft;
2001 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002002 SkIRect dstRect;
2003 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002004 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002005
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002006 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002007 srcVkRect.fTop = src->height() - srcRect.fBottom;
2008 srcVkRect.fBottom = src->height() - srcRect.fTop;
2009 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002010 srcVkRect.fTop = srcRect.fTop;
2011 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002012 }
2013
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002014 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002015 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2016 } else {
2017 dstRect.fTop = dstPoint.fY;
2018 }
2019 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2020
2021 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2022 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002023 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002024 using std::swap;
2025 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002026 }
2027
2028 VkImageBlit blitRegion;
2029 memset(&blitRegion, 0, sizeof(VkImageBlit));
2030 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2031 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002032 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002033 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2034 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002035 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002036
2037 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002038 *srcImage,
2039 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002040 1,
2041 &blitRegion,
2042 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002043
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002044 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002045 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002046}
2047
Brian Salomon1fabd512018-02-09 09:54:25 -05002048void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2049 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2050 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002051 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002052 SkIRect srcRect = origSrcRect;
2053 SkIPoint dstPoint = origDstPoint;
2054 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2055 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2056 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2057 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2058 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2059 }
2060 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002061 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2062 srcRect.width(), srcRect.height());
2063 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002064}
2065
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002066bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2067 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002068 const SkIRect& srcRect, const SkIPoint& dstPoint,
2069 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002070#ifdef SK_DEBUG
2071 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2072 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2073 }
2074 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2075 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2076 }
2077#endif
2078
Greg Daniel25af6712018-04-25 10:44:38 -04002079 GrPixelConfig dstConfig = dst->config();
2080 GrPixelConfig srcConfig = src->config();
2081
2082 int dstSampleCnt = get_surface_sample_cnt(dst);
2083 int srcSampleCnt = get_surface_sample_cnt(src);
2084
egdaniel17b89252016-04-05 07:23:38 -07002085 GrVkImage* dstImage;
2086 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002087 GrRenderTarget* dstRT = dst->asRenderTarget();
2088 if (dstRT) {
2089 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002090 if (vkRT->wrapsSecondaryCommandBuffer()) {
2091 return false;
2092 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002093 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2094 } else {
2095 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002096 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002097 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002098 GrRenderTarget* srcRT = src->asRenderTarget();
2099 if (srcRT) {
2100 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2101 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002102 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002103 SkASSERT(src->asTexture());
2104 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002105 }
2106
Greg Daniela51e93c2019-03-25 12:30:45 -04002107 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2108 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2109
2110 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2111 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2112 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2113 return true;
2114 }
2115
2116 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2117 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2118 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2119 dstPoint, canDiscardOutsideDstRect));
2120 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2121 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2122 return true;
2123 }
2124
2125 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2126 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002127 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2128 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002129 return true;
2130 }
2131
Greg Daniel25af6712018-04-25 10:44:38 -04002132 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002133 dstHasYcbcr, srcConfig, srcSampleCnt,
2134 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002135 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2136 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002137 return true;
2138 }
2139
Greg Daniel164a9f02016-02-22 09:56:40 -05002140 return false;
2141}
2142
Brian Salomona6948702018-06-01 15:33:20 -04002143bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2144 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002145 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002146 return false;
2147 }
2148
egdaniel66933552016-08-24 07:22:19 -07002149 GrVkImage* image = nullptr;
2150 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2151 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002152 // Reading from render targets that wrap a secondary command buffer is not allowed since
2153 // it would require us to know the VkImage, which we don't have, as well as need us to
2154 // stop and start the VkRenderPass which we don't have access to.
2155 if (rt->wrapsSecondaryCommandBuffer()) {
2156 return false;
2157 }
egdaniel66933552016-08-24 07:22:19 -07002158 // resolve the render target if necessary
2159 switch (rt->getResolveType()) {
2160 case GrVkRenderTarget::kCantResolve_ResolveType:
2161 return false;
2162 case GrVkRenderTarget::kAutoResolves_ResolveType:
2163 break;
2164 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002165 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002166 break;
2167 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002168 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002169 }
2170 image = rt;
2171 } else {
2172 image = static_cast<GrVkTexture*>(surface->asTexture());
2173 }
2174
2175 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002176 return false;
2177 }
2178
Greg Daniel475eb702018-09-28 14:16:50 -04002179 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2180 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2181 // image and then do the read pixels from that.
2182 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002183 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2184 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002185
2186 // Make a new surface that is RGBA to copy the RGB surface into.
2187 GrSurfaceDesc surfDesc;
2188 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2189 surfDesc.fWidth = width;
2190 surfDesc.fHeight = height;
2191 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2192 surfDesc.fSampleCnt = 1;
2193
2194 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2195 VK_IMAGE_USAGE_SAMPLED_BIT |
2196 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2197 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2198
2199 GrVkImage::ImageDesc imageDesc;
2200 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2201 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2202 imageDesc.fWidth = width;
2203 imageDesc.fHeight = height;
2204 imageDesc.fLevels = 1;
2205 imageDesc.fSamples = 1;
2206 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2207 imageDesc.fUsageFlags = usageFlags;
2208 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2209
2210 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2211 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2212 if (!copySurface) {
2213 return false;
2214 }
2215
2216 int srcSampleCount = 0;
2217 if (rt) {
2218 srcSampleCount = rt->numColorSamples();
2219 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002220 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002221 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002222 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2223 surface->config(), srcSampleCount, kOrigin,
2224 srcHasYcbcr) &&
2225 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2226 surface->config(), SkToBool(surface->asTexture()),
2227 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002228 return false;
2229 }
2230 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2231 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2232 srcRect, SkIPoint::Make(0,0))) {
2233 return false;
2234 }
2235 top = 0;
2236 left = 0;
2237 dstColorType = GrColorType::kRGBA_8888;
2238 image = copySurface.get();
2239 }
2240
Greg Daniel164a9f02016-02-22 09:56:40 -05002241 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002242 image->setImageLayout(this,
2243 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2244 VK_ACCESS_TRANSFER_READ_BIT,
2245 VK_PIPELINE_STAGE_TRANSFER_BIT,
2246 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002247
Brian Salomonc320b152018-02-20 14:05:36 -05002248 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002249 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002250
Greg Daniel164a9f02016-02-22 09:56:40 -05002251 VkBufferImageCopy region;
2252 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002253
2254 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2255 if (copyFromOrigin) {
2256 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002257 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002258 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002259 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002260 region.imageOffset = offset;
2261 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2262 }
2263
2264 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002265 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002266 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002267 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002268 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002269 kStream_GrAccessPattern)
2270 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002271
2272 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002273 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002274 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002275 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2276 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002277
2278 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002279 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002280 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002281 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002282 1,
2283 &region);
2284
2285 // make sure the copy to buffer has finished
2286 transferBuffer->addMemoryBarrier(this,
2287 VK_ACCESS_TRANSFER_WRITE_BIT,
2288 VK_ACCESS_HOST_READ_BIT,
2289 VK_PIPELINE_STAGE_TRANSFER_BIT,
2290 VK_PIPELINE_STAGE_HOST_BIT,
2291 false);
2292
2293 // We need to submit the current command buffer to the Queue and make sure it finishes before
2294 // we can copy the data out of the buffer.
2295 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002296 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002297 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002298 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002299
egdaniel6fa0a912016-09-12 11:51:29 -07002300 if (copyFromOrigin) {
2301 uint32_t skipRows = region.imageExtent.height - height;
2302 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2303 }
2304
Brian Salomona6948702018-06-01 15:33:20 -04002305 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002306
2307 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002308 return true;
2309}
egdaniel066df7c2016-06-08 14:02:27 -07002310
egdaniel27bb2842016-07-07 11:58:35 -07002311// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2312// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2313// the the entire attachment. Similar requirements for the y and height components.
2314void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2315 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2316 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002317 if ((0 != granularity.width && 1 != granularity.width)) {
2318 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2319 int rightAdj = srcBounds.fRight % granularity.width;
2320 if (rightAdj != 0) {
2321 rightAdj = granularity.width - rightAdj;
2322 }
2323 dstBounds->fRight = srcBounds.fRight + rightAdj;
2324 if (dstBounds->fRight > maxWidth) {
2325 dstBounds->fRight = maxWidth;
2326 dstBounds->fLeft = 0;
2327 } else {
2328 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2329 }
egdaniel27bb2842016-07-07 11:58:35 -07002330 } else {
egdanield5797b32016-09-20 12:57:45 -07002331 dstBounds->fLeft = srcBounds.fLeft;
2332 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002333 }
2334
2335 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002336 if ((0 != granularity.height && 1 != granularity.height)) {
2337 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2338 int bottomAdj = srcBounds.fBottom % granularity.height;
2339 if (bottomAdj != 0) {
2340 bottomAdj = granularity.height - bottomAdj;
2341 }
2342 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2343 if (dstBounds->fBottom > maxHeight) {
2344 dstBounds->fBottom = maxHeight;
2345 dstBounds->fTop = 0;
2346 } else {
2347 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2348 }
egdaniel27bb2842016-07-07 11:58:35 -07002349 } else {
egdanield5797b32016-09-20 12:57:45 -07002350 dstBounds->fTop = srcBounds.fTop;
2351 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002352 }
2353}
2354
Greg Daniel22bc8652017-03-22 15:45:43 -04002355void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002356 const GrVkRenderPass* renderPass,
2357 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002358 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002359 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002360 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002361 const SkIRect* pBounds = &bounds;
2362 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002363 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002364 flippedBounds = bounds;
2365 flippedBounds.fTop = target->height() - bounds.fBottom;
2366 flippedBounds.fBottom = target->height() - bounds.fTop;
2367 pBounds = &flippedBounds;
2368 }
2369
egdaniel27bb2842016-07-07 11:58:35 -07002370 // The bounds we use for the render pass should be of the granularity supported
2371 // by the device.
2372 const VkExtent2D& granularity = renderPass->granularity();
2373 SkIRect adjustedBounds;
2374 if ((0 != granularity.width && 1 != granularity.width) ||
2375 (0 != granularity.height && 1 != granularity.height)) {
2376 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2377 target->width(), target->height());
2378 pBounds = &adjustedBounds;
2379 }
2380
Robert Phillips95214472017-08-08 18:00:03 -04002381#ifdef SK_DEBUG
2382 uint32_t index;
2383 bool result = renderPass->colorAttachmentIndex(&index);
2384 SkASSERT(result && 0 == index);
2385 result = renderPass->stencilAttachmentIndex(&index);
2386 if (result) {
2387 SkASSERT(1 == index);
2388 }
2389#endif
2390 VkClearValue clears[2];
2391 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002392 clears[1].depthStencil.depth = 0.0f;
2393 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002394
2395 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002396 for (int i = 0; i < buffers.count(); ++i) {
2397 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2398 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002399 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002400
Brian Salomon1fabd512018-02-09 09:54:25 -05002401 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002402}
egdaniel9cb63402016-06-23 08:37:05 -07002403
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002404void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2405 if (buffer->asRTCommandBuffer()) {
2406 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2407
2408 fCachedRTCommandBuffer->submit();
2409 fCachedRTCommandBuffer->reset();
2410 } else {
2411 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2412
2413 fCachedTexCommandBuffer->submit();
2414 fCachedTexCommandBuffer->reset();
2415 }
2416}
2417
Greg Daniel6be35232017-03-01 17:01:09 -05002418GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002419 VkFenceCreateInfo createInfo;
2420 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2421 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2422 createInfo.pNext = nullptr;
2423 createInfo.flags = 0;
2424 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002425
2426 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2427 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2428
2429 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002430 return (GrFence)fence;
2431}
2432
Greg Daniel6be35232017-03-01 17:01:09 -05002433bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2434 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2435
2436 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002437 return (VK_SUCCESS == result);
2438}
2439
2440void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002441 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2442}
2443
Greg Daniela5cb7812017-06-16 09:45:32 -04002444sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2445 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002446}
2447
Greg Daniel48661b82018-01-22 16:11:35 -05002448sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2449 GrResourceProvider::SemaphoreWrapType wrapType,
2450 GrWrapOwnership ownership) {
2451 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002452}
2453
Greg Daniel858e12c2018-12-06 11:11:37 -05002454void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002455 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2456
Greg Daniel48661b82018-01-22 16:11:35 -05002457 GrVkSemaphore::Resource* resource = vkSem->getResource();
2458 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002459 resource->ref();
2460 fSemaphoresToSignal.push_back(resource);
2461 }
Greg Daniel6be35232017-03-01 17:01:09 -05002462}
2463
Greg Daniel48661b82018-01-22 16:11:35 -05002464void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002465 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2466
Greg Daniel48661b82018-01-22 16:11:35 -05002467 GrVkSemaphore::Resource* resource = vkSem->getResource();
2468 if (resource->shouldWait()) {
2469 resource->ref();
2470 fSemaphoresToWaitOn.push_back(resource);
2471 }
jvanverth84741b32016-09-30 08:39:02 -07002472}
Brian Osman13dddce2017-05-09 13:19:50 -04002473
2474sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2475 SkASSERT(texture);
2476 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2477 vkTexture->setImageLayout(this,
2478 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2479 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002480 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002481 false);
2482 this->submitCommandBuffer(kSkip_SyncQueue);
2483
2484 // The image layout change serves as a barrier, so no semaphore is needed
2485 return nullptr;
2486}
Greg Danielf5d87582017-12-18 14:48:15 -05002487
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002488void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2489 fDrawables.emplace_back(std::move(drawable));
2490}
2491
Greg Daniel7a82edf2018-12-04 10:54:34 -05002492uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2493 const GrBackendFormat& format) {
2494 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2495 SkASSERT(ycbcrInfo);
2496 if (!ycbcrInfo->isValid()) {
2497 return 0;
2498 }
2499
2500 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2501 samplerState, *ycbcrInfo);
2502
2503 return sampler->uniqueID();
2504}
2505
Greg Daniela870b462019-01-08 15:49:46 -05002506void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002507 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002508 this->resourceProvider().storePipelineCacheData();
2509 }
2510}