blob: 207e5a337ff0d2973b009c79859b6b6d19ab6baf [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 Salomona585fe92019-04-09 14:57:00 -0400496size_t GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
497 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
498 size_t offset) {
499 SkASSERT(surface);
500 SkASSERT(transferBuffer);
501
502 size_t rowBytes;
503 size_t offsetAlignment;
504 if (!this->vkCaps().transferFromBufferRequirements(bufferColorType, width, &rowBytes,
505 &offsetAlignment)) {
506 return 0;
507 }
508
509 if (offset % offsetAlignment || offset + height * rowBytes > transferBuffer->size()) {
510 return 0;
511 }
512
513 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
514
515 GrVkImage* srcImage;
516 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
517 // Reading from render targets that wrap a secondary command buffer is not allowed since
518 // it would require us to know the VkImage, which we don't have, as well as need us to
519 // stop and start the VkRenderPass which we don't have access to.
520 if (rt->wrapsSecondaryCommandBuffer()) {
521 return false;
522 }
523 // resolve the render target if necessary
524 switch (rt->getResolveType()) {
525 case GrVkRenderTarget::kCantResolve_ResolveType:
526 return false;
527 case GrVkRenderTarget::kAutoResolves_ResolveType:
528 break;
529 case GrVkRenderTarget::kCanResolve_ResolveType:
530 this->resolveRenderTargetNoFlush(rt);
531 break;
532 default:
533 SK_ABORT("Unknown resolve type");
534 }
535 srcImage = rt;
536 } else {
537 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
538 }
539
540 // Set up copy region
541 VkBufferImageCopy region;
542 memset(&region, 0, sizeof(VkBufferImageCopy));
543 region.bufferOffset = offset;
544 // We're assuming that GrVkCaps made the row bytes tight.
545 region.bufferRowLength = 0;
546#ifdef SK_DEBUG
547 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
548 SkASSERT(rowBytes == width * (size_t)bpp);
549#endif
550 region.bufferImageHeight = 0;
551 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
552 region.imageOffset = { left, top, 0 };
553 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
554
555 srcImage->setImageLayout(this,
556 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
557 VK_ACCESS_TRANSFER_READ_BIT,
558 VK_PIPELINE_STAGE_TRANSFER_BIT,
559 false);
560
561 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
562 vkBuffer, 1, &region);
563
564 // Make sure the copy to buffer has finished.
565 vkBuffer->addMemoryBarrier(this,
566 VK_ACCESS_TRANSFER_WRITE_BIT,
567 VK_ACCESS_HOST_READ_BIT,
568 VK_PIPELINE_STAGE_TRANSFER_BIT,
569 VK_PIPELINE_STAGE_HOST_BIT,
570 false);
571
572 // The caller is responsible for syncing.
573 this->submitCommandBuffer(kSkip_SyncQueue);
574
575 return rowBytes;
576}
577
Brian Salomon1fabd512018-02-09 09:54:25 -0500578void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
579 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700580 SkASSERT(dst);
581 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
582
egdaniel4bcd62e2016-08-31 07:37:31 -0700583 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500584 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
585 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
586 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
587 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
588 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700589
Greg Danielbc26c392017-04-18 13:32:10 -0400590 GrVkImage* dstImage;
591 GrRenderTarget* dstRT = dst->asRenderTarget();
592 if (dstRT) {
593 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400594 dstImage = vkRT;
595 } else {
596 SkASSERT(dst->asTexture());
597 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
598 }
599 dstImage->setImageLayout(this,
600 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
601 VK_ACCESS_TRANSFER_WRITE_BIT,
602 VK_PIPELINE_STAGE_TRANSFER_BIT,
603 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700604
605 src->msaaImage()->setImageLayout(this,
606 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
607 VK_ACCESS_TRANSFER_READ_BIT,
608 VK_PIPELINE_STAGE_TRANSFER_BIT,
609 false);
610
Greg Danielbc26c392017-04-18 13:32:10 -0400611 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700612}
613
Brian Salomon1fabd512018-02-09 09:54:25 -0500614void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700615 if (target->needsResolve()) {
616 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700617 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
618 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500619
egdaniel4bcd62e2016-08-31 07:37:31 -0700620 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700621
Brian Salomon1fabd512018-02-09 09:54:25 -0500622 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700623
624 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500625
626 if (requiresSubmit) {
627 this->submitCommandBuffer(kSkip_SyncQueue);
628 }
egdaniel52ad2512016-08-04 12:50:01 -0700629 }
630}
631
Brian Salomona9b04b92018-06-01 15:04:28 -0400632bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
633 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500634 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700635 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500636
Jim Van Verth1676cb92019-01-15 13:24:45 -0500637 // If we're uploading compressed data then we should be using uploadCompressedTexData
638 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
639 GrSRGBEncoded::kNo)));
640
Greg Daniel660cc992017-06-26 14:55:05 -0400641 SkDEBUGCODE(
642 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
643 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
644 SkASSERT(bounds.contains(subRect));
645 )
Brian Salomonc320b152018-02-20 14:05:36 -0500646 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500647 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400648 if (!rowBytes) {
649 rowBytes = trimRowBytes;
650 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500651
jvanverth900bd4a2016-04-29 13:53:12 -0700652 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
653 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
654 const VkImageSubresource subres = {
655 VK_IMAGE_ASPECT_COLOR_BIT,
656 0, // mipLevel
657 0, // arraySlice
658 };
659 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500660
jvanverth900bd4a2016-04-29 13:53:12 -0700661 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500662
jvanverth900bd4a2016-04-29 13:53:12 -0700663 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700664 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700665 &subres,
666 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500667
jvanverth1e305ba2016-06-01 09:39:15 -0700668 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400669 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700670 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400671 SkASSERT(size + offset <= alloc.fSize);
672 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
673 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700674 return false;
675 }
Greg Daniel81df0412018-05-31 13:13:33 -0400676 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700677
Brian Salomona9b04b92018-06-01 15:04:28 -0400678 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
679 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700680
Greg Daniele35a99e2018-03-02 11:44:22 -0500681 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400682 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700683
684 return true;
685}
686
Brian Salomona9b04b92018-06-01 15:04:28 -0400687bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
688 GrColorType dataColorType, const GrMipLevel texels[],
689 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700690 SkASSERT(!tex->isLinearTiled());
691 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400692 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700693 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
694
Greg Danieldd20e912017-04-07 14:42:23 -0400695 // We assume that if the texture has mip levels, we either upload to all the levels or just the
696 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400697 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400698
Jim Van Verth1676cb92019-01-15 13:24:45 -0500699 // If we're uploading compressed data then we should be using uploadCompressedTexData
700 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
701 GrSRGBEncoded::kNo)));
702
jvanverth900bd4a2016-04-29 13:53:12 -0700703 if (width == 0 || height == 0) {
704 return false;
705 }
706
Greg Daniel475eb702018-09-28 14:16:50 -0400707 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
708 return false;
709 }
710
711 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
712 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500713 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
714 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400715 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
716 // blit or draw.
717 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
718 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
719 return false;
720 }
721 mipLevelCount = 1;
722 }
723
Brian Salomond1eaf492017-05-18 10:02:08 -0400724 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500725 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700726
727 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700728 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
729 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400730 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
731
Greg Daniel475eb702018-09-28 14:16:50 -0400732 texelsShallowCopy.reset(mipLevelCount);
733 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700734
Robert Phillips590533f2017-07-11 14:22:35 -0400735 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700736 individualMipOffsets.push_back(0);
737 size_t combinedBufferSize = width * bpp * height;
738 int currentWidth = width;
739 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400740 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400741 combinedBufferSize = 0;
742 }
743
Greg Daniel468fd632017-03-22 17:03:45 -0400744 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
745 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
746 SkASSERT((bpp & (bpp - 1)) == 0);
747 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400748 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700749 currentWidth = SkTMax(1, currentWidth/2);
750 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400751
Greg Daniel55afd6d2017-09-29 09:32:44 -0400752 if (texelsShallowCopy[currentMipLevel].fPixels) {
753 const size_t trimmedSize = currentWidth * bpp * currentHeight;
754 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
755 if (alignmentDiff != 0) {
756 combinedBufferSize += alignmentMask - alignmentDiff + 1;
757 }
758 individualMipOffsets.push_back(combinedBufferSize);
759 combinedBufferSize += trimmedSize;
760 } else {
761 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400762 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400763 }
764 if (0 == combinedBufferSize) {
765 // We don't actually have any data to upload so just return success
766 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700767 }
768
769 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500770 sk_sp<GrVkTransferBuffer> transferBuffer =
771 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400772 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700773 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400774 }
jvanverth900bd4a2016-04-29 13:53:12 -0700775
Greg Daniel475eb702018-09-28 14:16:50 -0400776 int uploadLeft = left;
777 int uploadTop = top;
778 GrVkTexture* uploadTexture = tex;
779 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
780 // R8G8B8A8_UNORM image and then copy it.
781 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500782 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400783 GrSurfaceDesc surfDesc;
784 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
785 surfDesc.fWidth = width;
786 surfDesc.fHeight = height;
787 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
788 surfDesc.fSampleCnt = 1;
789
790 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
791 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
792 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
793
794 GrVkImage::ImageDesc imageDesc;
795 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
796 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
797 imageDesc.fWidth = width;
798 imageDesc.fHeight = height;
799 imageDesc.fLevels = 1;
800 imageDesc.fSamples = 1;
801 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
802 imageDesc.fUsageFlags = usageFlags;
803 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
804
805 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
806 GrMipMapsStatus::kNotAllocated);
807 if (!copyTexture) {
808 return false;
809 }
810 uploadTexture = copyTexture.get();
811 uploadLeft = 0;
812 uploadTop = 0;
813 }
814
jvanverth900bd4a2016-04-29 13:53:12 -0700815 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400816 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700817
jvanverthc578b0632016-05-02 10:58:12 -0700818 currentWidth = width;
819 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400820 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400821 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400822 if (texelsShallowCopy[currentMipLevel].fPixels) {
823 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
824 const size_t trimRowBytes = currentWidth * bpp;
825 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
826 ? texelsShallowCopy[currentMipLevel].fRowBytes
827 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700828
Greg Daniel55afd6d2017-09-29 09:32:44 -0400829 // copy data into the buffer, skipping the trailing bytes
830 char* dst = buffer + individualMipOffsets[currentMipLevel];
831 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400832 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400833
834 VkBufferImageCopy& region = regions.push_back();
835 memset(&region, 0, sizeof(VkBufferImageCopy));
836 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
837 region.bufferRowLength = currentWidth;
838 region.bufferImageHeight = currentHeight;
839 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400840 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400841 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700842 }
jvanverthc578b0632016-05-02 10:58:12 -0700843 currentWidth = SkTMax(1, currentWidth/2);
844 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400845 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700846 }
847
jvanverth9d54afc2016-09-20 09:20:03 -0700848 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700849 transferBuffer->unmap();
850
jvanverth900bd4a2016-04-29 13:53:12 -0700851 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400852 uploadTexture->setImageLayout(this,
853 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
854 VK_ACCESS_TRANSFER_WRITE_BIT,
855 VK_PIPELINE_STAGE_TRANSFER_BIT,
856 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700857
858 // Copy the buffer to the image
859 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500860 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400861 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700862 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
863 regions.count(),
864 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400865
866 // If we copied the data into a temporary image first, copy that image into our main texture
867 // now.
868 if (copyTexture.get()) {
869 SkASSERT(dataColorType == GrColorType::kRGB_888x);
870 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
871 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
872 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
873 false));
874 }
Robert Phillips590533f2017-07-11 14:22:35 -0400875 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400876 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400877 }
jvanverth900bd4a2016-04-29 13:53:12 -0700878
Greg Daniel164a9f02016-02-22 09:56:40 -0500879 return true;
880}
881
Jim Van Verth1676cb92019-01-15 13:24:45 -0500882// It's probably possible to roll this into uploadTexDataOptimal,
883// but for now it's easier to maintain as a separate entity.
884bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
885 GrColorType dataColorType, const GrMipLevel texels[],
886 int mipLevelCount) {
887 SkASSERT(!tex->isLinearTiled());
888 // For now the assumption is that our rect is the entire texture.
889 // Compressed textures are read-only so this should be a reasonable assumption.
890 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
891
892 // We assume that if the texture has mip levels, we either upload to all the levels or just the
893 // first.
894 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
895
896 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
897 GrSRGBEncoded::kNo)));
898
899 if (width == 0 || height == 0) {
900 return false;
901 }
902
903 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
904 return false;
905 }
906
907 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
908
909 SkTArray<size_t> individualMipOffsets(mipLevelCount);
910 individualMipOffsets.push_back(0);
911 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
912 int currentWidth = width;
913 int currentHeight = height;
914 if (!texels[0].fPixels) {
915 return false;
916 }
917
918 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
919 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
920 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
921 currentWidth = SkTMax(1, currentWidth / 2);
922 currentHeight = SkTMax(1, currentHeight / 2);
923
924 if (texels[currentMipLevel].fPixels) {
925 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
926 currentHeight);
927 individualMipOffsets.push_back(combinedBufferSize);
928 combinedBufferSize += dataSize;
929 } else {
930 return false;
931 }
932 }
933 if (0 == combinedBufferSize) {
934 // We don't have any data to upload so fail (compressed textures are read-only).
935 return false;
936 }
937
938 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500939 sk_sp<GrVkTransferBuffer> transferBuffer =
940 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500941 if (!transferBuffer) {
942 return false;
943 }
944
945 int uploadLeft = left;
946 int uploadTop = top;
947 GrVkTexture* uploadTexture = tex;
948
949 char* buffer = (char*)transferBuffer->map();
950 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
951
952 currentWidth = width;
953 currentHeight = height;
954 int layerHeight = uploadTexture->height();
955 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
956 if (texels[currentMipLevel].fPixels) {
957 // Again, we're assuming that our rect is the entire texture
958 SkASSERT(currentHeight == layerHeight);
959 SkASSERT(0 == uploadLeft && 0 == uploadTop);
960
961 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
962 currentHeight);
963
964 // copy data into the buffer, skipping the trailing bytes
965 char* dst = buffer + individualMipOffsets[currentMipLevel];
966 const char* src = (const char*)texels[currentMipLevel].fPixels;
967 memcpy(dst, src, dataSize);
968
969 VkBufferImageCopy& region = regions.push_back();
970 memset(&region, 0, sizeof(VkBufferImageCopy));
971 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
972 region.bufferRowLength = currentWidth;
973 region.bufferImageHeight = currentHeight;
974 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
975 region.imageOffset = { uploadLeft, uploadTop, 0 };
976 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
977 }
978 currentWidth = SkTMax(1, currentWidth / 2);
979 currentHeight = SkTMax(1, currentHeight / 2);
980 layerHeight = currentHeight;
981 }
982
983 // no need to flush non-coherent memory, unmap will do that for us
984 transferBuffer->unmap();
985
986 // Change layout of our target so it can be copied to
987 uploadTexture->setImageLayout(this,
988 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
989 VK_ACCESS_TRANSFER_WRITE_BIT,
990 VK_PIPELINE_STAGE_TRANSFER_BIT,
991 false);
992
993 // Copy the buffer to the image
994 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500995 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500996 uploadTexture,
997 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
998 regions.count(),
999 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -05001000
1001 if (1 == mipLevelCount) {
1002 tex->texturePriv().markMipMapsDirty();
1003 }
1004
1005 return true;
1006}
1007
Greg Daniel164a9f02016-02-22 09:56:40 -05001008////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001009sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001010 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001011 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1012
1013 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001014 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001015
Greg Daniel164a9f02016-02-22 09:56:40 -05001016 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1017 if (renderTarget) {
1018 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1019 }
1020
1021 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1022 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1023 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001024 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001025 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1026 // texture.
1027 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1028
Greg Daniel164a9f02016-02-22 09:56:40 -05001029 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001030 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001031 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001032 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001033 GrVkImage::ImageDesc imageDesc;
1034 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1035 imageDesc.fFormat = pixelFormat;
1036 imageDesc.fWidth = desc.fWidth;
1037 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001038 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001039 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001040 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001041 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001042 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001043
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001044 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1045 if (mipLevels > 1) {
1046 mipMapsStatus = GrMipMapsStatus::kValid;
1047 for (int i = 0; i < mipLevels; ++i) {
1048 if (!texels[i].fPixels) {
1049 mipMapsStatus = GrMipMapsStatus::kDirty;
1050 break;
1051 }
Greg Daniel834f1202017-10-09 15:06:20 -04001052 }
1053 }
1054
Robert Phillips67d52cf2017-06-05 13:38:13 -04001055 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001056 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001057 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1058 imageDesc,
1059 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001060 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001061 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001062 }
1063
1064 if (!tex) {
1065 return nullptr;
1066 }
1067
Jim Van Verth1676cb92019-01-15 13:24:45 -05001068 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001069 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001070 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001071 bool success;
1072 if (isCompressed) {
1073 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1074 colorType, texels, mipLevelCount);
1075 } else {
1076 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1077 colorType, texels, mipLevelCount);
1078 }
1079 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001080 tex->unref();
1081 return nullptr;
1082 }
1083 }
1084
Jim Van Verth1676cb92019-01-15 13:24:45 -05001085 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001086 VkClearColorValue zeroClearColor;
1087 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1088 VkImageSubresourceRange range;
1089 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1090 range.baseArrayLayer = 0;
1091 range.baseMipLevel = 0;
1092 range.layerCount = 1;
1093 range.levelCount = 1;
1094 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1095 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001096 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001097 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001098 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001099}
1100
1101////////////////////////////////////////////////////////////////////////////////
1102
Greg Daniel6888c0d2017-08-25 11:55:50 -04001103void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1104 VkDeviceSize dstOffset, VkDeviceSize size) {
1105 VkBufferCopy copyRegion;
1106 copyRegion.srcOffset = srcOffset;
1107 copyRegion.dstOffset = dstOffset;
1108 copyRegion.size = size;
1109 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1110}
1111
jvanverthdb379092016-07-07 11:18:46 -07001112bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1113 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001114 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001115 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001116
1117 return true;
1118}
1119
1120////////////////////////////////////////////////////////////////////////////////
1121
Greg Daniel7e000222018-12-03 10:08:21 -05001122static bool check_image_info(const GrVkCaps& caps,
1123 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001124 GrPixelConfig config,
1125 bool isWrappedRT) {
1126 if (VK_NULL_HANDLE == info.fImage) {
1127 return false;
1128 }
1129
1130 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001131 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001132 }
1133
Greg Daniel7e000222018-12-03 10:08:21 -05001134 if (info.fYcbcrConversionInfo.isValid()) {
1135 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1136 return false;
1137 }
jvanverthfd359ca2016-03-18 11:57:24 -07001138 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001139
Greg Danielcb324152019-02-25 11:36:53 -05001140 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1141 return false;
1142 }
1143
Greg Daniel52e16d92018-04-10 09:34:07 -04001144 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001145 return true;
1146}
1147
1148sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001149 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1150 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001151 GrVkImageInfo imageInfo;
1152 if (!backendTex.getVkImageInfo(&imageInfo)) {
1153 return nullptr;
1154 }
1155
Greg Danielcb324152019-02-25 11:36:53 -05001156 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001157 return nullptr;
1158 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001159
Greg Daniel164a9f02016-02-22 09:56:40 -05001160 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001161 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001162 surfDesc.fWidth = backendTex.width();
1163 surfDesc.fHeight = backendTex.height();
1164 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001165 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001166
Greg Daniel52e16d92018-04-10 09:34:07 -04001167 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1168 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001169 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1170 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001171}
1172
1173sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001174 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001175 GrWrapOwnership ownership,
1176 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001177 GrVkImageInfo imageInfo;
1178 if (!backendTex.getVkImageInfo(&imageInfo)) {
1179 return nullptr;
1180 }
1181
Greg Danielcb324152019-02-25 11:36:53 -05001182 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001183 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001184 }
Brian Salomond17f6582017-07-19 18:28:58 -04001185
1186 GrSurfaceDesc surfDesc;
1187 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1188 surfDesc.fWidth = backendTex.width();
1189 surfDesc.fHeight = backendTex.height();
1190 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001191 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001192
Greg Daniel52e16d92018-04-10 09:34:07 -04001193 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1194 SkASSERT(layout);
1195
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001196 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1197 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001198}
1199
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001200sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001201 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1202 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1203 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1204 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001205 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001206 return nullptr;
1207 }
halcanary9d524f22016-03-29 09:03:52 -07001208
Greg Daniel323fbcf2018-04-10 13:46:30 -04001209 GrVkImageInfo info;
1210 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001211 return nullptr;
1212 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001213
Greg Danielcb324152019-02-25 11:36:53 -05001214 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001215 return nullptr;
1216 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001217
Greg Daniel164a9f02016-02-22 09:56:40 -05001218 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001219 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001220 desc.fWidth = backendRT.width();
1221 desc.fHeight = backendRT.height();
1222 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001223 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001224
Greg Daniel323fbcf2018-04-10 13:46:30 -04001225 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001226
Greg Daniel323fbcf2018-04-10 13:46:30 -04001227 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001228 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001229
1230 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1231 SkASSERT(!backendRT.stencilBits());
1232 if (tgt) {
1233 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001234 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001235
Ben Wagnerff134f22018-04-24 16:29:16 -04001236 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001237}
1238
Greg Daniel7ef28f32017-04-20 16:41:55 +00001239sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001240 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001241
Greg Daniel52e16d92018-04-10 09:34:07 -04001242 GrVkImageInfo imageInfo;
1243 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001244 return nullptr;
1245 }
Greg Danielcb324152019-02-25 11:36:53 -05001246 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001247 return nullptr;
1248 }
1249
Greg Danielcb324152019-02-25 11:36:53 -05001250
Brian Osman33910292017-04-18 14:38:53 -04001251 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001252 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001253 desc.fWidth = tex.width();
1254 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001255 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001256 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1257 if (!desc.fSampleCnt) {
1258 return nullptr;
1259 }
Brian Osman33910292017-04-18 14:38:53 -04001260
Greg Daniel52e16d92018-04-10 09:34:07 -04001261 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1262 SkASSERT(layout);
1263
Ben Wagnerff134f22018-04-24 16:29:16 -04001264 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001265}
1266
Greg Danielb46add82019-01-02 14:51:29 -05001267sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1268 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1269 int maxSize = this->caps()->maxTextureSize();
1270 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1271 return nullptr;
1272 }
1273
1274 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1275 if (!backendFormat.isValid()) {
1276 return nullptr;
1277 }
1278 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1279 imageInfo.colorType());
1280 if (config == kUnknown_GrPixelConfig) {
1281 return nullptr;
1282 }
1283
1284 GrSurfaceDesc desc;
1285 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1286 desc.fWidth = imageInfo.width();
1287 desc.fHeight = imageInfo.height();
1288 desc.fConfig = config;
1289 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1290 if (!desc.fSampleCnt) {
1291 return nullptr;
1292 }
1293
1294 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1295}
1296
Brian Salomon930f9392018-06-20 16:25:26 -04001297bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1298 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001299 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001300 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001301 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001302 return false;
jvanverth62340062016-04-26 08:01:44 -07001303 }
1304
jvanverth62340062016-04-26 08:01:44 -07001305 // determine if we can blit to and from this format
1306 const GrVkCaps& caps = this->vkCaps();
1307 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001308 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1309 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001310 return false;
jvanverth62340062016-04-26 08:01:44 -07001311 }
1312
egdaniel7ac5da82016-07-15 13:41:42 -07001313 int width = tex->width();
1314 int height = tex->height();
1315 VkImageBlit blitRegion;
1316 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001317
jvanverth82c05582016-05-03 11:19:01 -07001318 // SkMipMap doesn't include the base level in the level count so we have to add 1
1319 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001320 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001321
Greg Danielda86e282018-06-13 09:41:19 -04001322 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001323 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1324 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001325
jvanverth50c46c72016-05-06 12:31:28 -07001326 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001327 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001328 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1329 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001330 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1331 nullptr, // pNext
1332 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1333 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1334 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1335 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1336 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1337 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1338 vkTex->image(), // image
1339 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001340 };
1341
jvanverth62340062016-04-26 08:01:44 -07001342 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001343 uint32_t mipLevel = 1;
1344 while (mipLevel < levelCount) {
1345 int prevWidth = width;
1346 int prevHeight = height;
1347 width = SkTMax(1, width / 2);
1348 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001349
jvanverth50c46c72016-05-06 12:31:28 -07001350 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001351 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1352 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001353
1354 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001355 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001356 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001357 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1358 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001359 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001360 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001361 vkTex->resource(),
1362 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001363 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001364 vkTex->resource(),
1365 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001366 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001367 1,
1368 &blitRegion,
1369 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001370 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001371 }
Greg Danielee54f232019-04-03 14:58:40 -04001372 if (levelCount > 1) {
1373 // This barrier logically is not needed, but it changes the final level to the same layout
1374 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1375 // layouts and future layout changes easier. The alternative here would be to track layout
1376 // and memory accesses per layer which doesn't seem work it.
1377 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1378 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1379 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1380 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1381 }
Brian Salomon930f9392018-06-20 16:25:26 -04001382 return true;
jvanverth62340062016-04-26 08:01:44 -07001383}
1384
Greg Daniel164a9f02016-02-22 09:56:40 -05001385////////////////////////////////////////////////////////////////////////////////
1386
1387GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1388 int width,
1389 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001390 SkASSERT(width >= rt->width());
1391 SkASSERT(height >= rt->height());
1392
1393 int samples = rt->numStencilSamples();
1394
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001395 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001396
1397 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001398 width,
1399 height,
1400 samples,
1401 sFmt));
1402 fStats.incStencilAttachmentCreates();
1403 return stencil;
1404}
1405
1406////////////////////////////////////////////////////////////////////////////////
1407
Brian Salomon52e943a2018-03-13 09:32:39 -04001408bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001409 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1410 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001411 VkDeviceSize size = dstRowBytes * h;
1412 VkDeviceSize offset = bufferOffset;
1413 SkASSERT(size + offset <= alloc.fSize);
1414 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1415 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001416 return false;
1417 }
Greg Daniel81df0412018-05-31 13:13:33 -04001418 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001419
Greg Daniel20ece3a2017-03-28 10:24:43 -04001420 if (srcData) {
1421 // If there is no padding on dst we can do a single memcopy.
1422 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001423 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001424 } else {
1425 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1426 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001427 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001428 }
Greg Daniel81df0412018-05-31 13:13:33 -04001429 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1430 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001431 return true;
1432}
1433
Brian Salomonf865b052018-03-09 09:01:53 -05001434#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001435bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1436 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001437 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001438 SkASSERT(texturable || renderable);
1439 if (!texturable) {
1440 SkASSERT(GrMipMapped::kNo == mipMapped);
1441 SkASSERT(!srcData);
1442 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001443 VkFormat pixelFormat;
1444 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001445 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001446 }
1447
Brian Salomon52e943a2018-03-13 09:32:39 -04001448 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1449 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001450 }
1451
Brian Salomon52e943a2018-03-13 09:32:39 -04001452 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1453 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001454 }
1455
1456 // Currently we don't support uploading pixel data when mipped.
1457 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001458 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001459 }
1460
Brian Salomon52e943a2018-03-13 09:32:39 -04001461 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001462 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1463 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001464 if (texturable) {
1465 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1466 }
1467 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001468 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1469 }
1470
1471 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001472 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001473 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001474
1475 // Create Image
1476 VkSampleCountFlagBits vkSamples;
1477 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001478 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001479 }
1480
1481 // Figure out the number of mip levels.
1482 uint32_t mipLevels = 1;
1483 if (GrMipMapped::kYes == mipMapped) {
1484 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1485 }
1486
1487 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001488 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1489 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001490 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001491 VK_IMAGE_TYPE_2D, // VkImageType
1492 pixelFormat, // VkFormat
1493 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1494 mipLevels, // mipLevels
1495 1, // arrayLayers
1496 vkSamples, // samples
1497 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1498 usageFlags, // VkImageUsageFlags
1499 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1500 0, // queueFamilyCount
1501 0, // pQueueFamilyIndices
1502 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001503 };
1504
Brian Salomon52e943a2018-03-13 09:32:39 -04001505 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1506 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001507
Brian Salomonde9f5462018-03-07 14:23:58 -05001508 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001509 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001510 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001511 }
1512
1513 // 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 -05001514 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001515 VkBuffer buffer = VK_NULL_HANDLE;
1516
1517 VkResult err;
1518 const VkCommandBufferAllocateInfo cmdInfo = {
1519 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1520 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001521 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001522 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1523 1 // bufferCount
1524 };
1525
1526 VkCommandBuffer cmdBuffer;
1527 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1528 if (err) {
1529 GrVkMemory::FreeImageMemory(this, false, alloc);
1530 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001531 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001532 }
1533
1534 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1535 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1536 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1537 cmdBufferBeginInfo.pNext = nullptr;
1538 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1539 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1540
1541 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1542 SkASSERT(!err);
1543
1544 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001545 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001546
Robert Phillips646f6372018-09-25 09:31:10 -04001547 const size_t trimRowBytes = w * bpp;
1548 if (!srcRowBytes) {
1549 srcRowBytes = trimRowBytes;
1550 }
1551
Brian Salomonde9f5462018-03-07 14:23:58 -05001552 SkTArray<size_t> individualMipOffsets(mipLevels);
1553 individualMipOffsets.push_back(0);
1554 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001555 if (GrPixelConfigIsCompressed(config)) {
1556 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1557 bpp = 4; // we have at least this alignment, which will pass the code below
1558 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001559 int currentWidth = w;
1560 int currentHeight = h;
1561 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1562 // config. This works with the assumption that the bytes in pixel config is always a power
1563 // of 2.
1564 SkASSERT((bpp & (bpp - 1)) == 0);
1565 const size_t alignmentMask = 0x3 | (bpp - 1);
1566 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1567 currentWidth = SkTMax(1, currentWidth / 2);
1568 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001569
Jim Van Verth1676cb92019-01-15 13:24:45 -05001570 size_t trimmedSize;
1571 if (GrPixelConfigIsCompressed(config)) {
1572 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1573 } else {
1574 trimmedSize = currentWidth * bpp * currentHeight;
1575 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001576 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1577 if (alignmentDiff != 0) {
1578 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001579 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001580 individualMipOffsets.push_back(combinedBufferSize);
1581 combinedBufferSize += trimmedSize;
1582 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001583
Brian Salomonde9f5462018-03-07 14:23:58 -05001584 VkBufferCreateInfo bufInfo;
1585 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1586 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1587 bufInfo.flags = 0;
1588 bufInfo.size = combinedBufferSize;
1589 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1590 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1591 bufInfo.queueFamilyIndexCount = 0;
1592 bufInfo.pQueueFamilyIndices = nullptr;
1593 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001594
Brian Salomonde9f5462018-03-07 14:23:58 -05001595 if (err) {
1596 GrVkMemory::FreeImageMemory(this, false, alloc);
1597 VK_CALL(DestroyImage(fDevice, image, nullptr));
1598 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001599 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001600 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001601 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001602
Brian Salomonde9f5462018-03-07 14:23:58 -05001603 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1604 &bufferAlloc)) {
1605 GrVkMemory::FreeImageMemory(this, false, alloc);
1606 VK_CALL(DestroyImage(fDevice, image, nullptr));
1607 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1608 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001609 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001610 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001611 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001612
Brian Salomonde9f5462018-03-07 14:23:58 -05001613 currentWidth = w;
1614 currentHeight = h;
1615 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1616 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001617 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001618 bool result;
1619 if (GrPixelConfigIsCompressed(config)) {
1620 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1621 size_t currentRowBytes = levelSize / currentHeight;
1622 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1623 currentRowBytes, currentRowBytes, currentHeight);
1624 } else {
1625 size_t currentRowBytes = bpp * currentWidth;
1626 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1627 currentRowBytes, trimRowBytes, currentHeight);
1628 }
1629 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001630 GrVkMemory::FreeImageMemory(this, false, alloc);
1631 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001632 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001633 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1634 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001635 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001636 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001637 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001638 currentWidth = SkTMax(1, currentWidth / 2);
1639 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001640 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001641
1642 // Set image layout and add barrier
1643 VkImageMemoryBarrier barrier;
1644 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1645 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1646 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001647 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001648 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1649 barrier.oldLayout = initialLayout;
1650 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1651 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1652 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1653 barrier.image = image;
1654 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1655
Greg Danielf7828d02018-10-09 12:01:32 -04001656 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001657 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1658 &barrier));
1659 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1660
1661 SkTArray<VkBufferImageCopy> regions(mipLevels);
1662
1663 currentWidth = w;
1664 currentHeight = h;
1665 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1666 // Submit copy command
1667 VkBufferImageCopy& region = regions.push_back();
1668 memset(&region, 0, sizeof(VkBufferImageCopy));
1669 region.bufferOffset = individualMipOffsets[currentMipLevel];
1670 region.bufferRowLength = currentWidth;
1671 region.bufferImageHeight = currentHeight;
1672 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1673 region.imageOffset = {0, 0, 0};
1674 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1675 currentWidth = SkTMax(1, currentWidth / 2);
1676 currentHeight = SkTMax(1, currentHeight / 2);
1677 }
1678
1679 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1680 regions.begin()));
1681
Brian Salomon52e943a2018-03-13 09:32:39 -04001682 if (texturable) {
1683 // Change Image layout to shader read since if we use this texture as a borrowed textures
1684 // within Ganesh we require that its layout be set to that
1685 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1686 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1687 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001688 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001689 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1690 barrier.oldLayout = initialLayout;
1691 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1692 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1693 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1694 barrier.image = image;
1695 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001696 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001697 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1698 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001699 0,
1700 0, nullptr,
1701 0, nullptr,
1702 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001703 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001704 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001705
1706 // End CommandBuffer
1707 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1708 SkASSERT(!err);
1709
1710 // Create Fence for queue
1711 VkFence fence;
1712 VkFenceCreateInfo fenceInfo;
1713 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1714 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1715
1716 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1717 SkASSERT(!err);
1718
1719 VkSubmitInfo submitInfo;
1720 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1721 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1722 submitInfo.pNext = nullptr;
1723 submitInfo.waitSemaphoreCount = 0;
1724 submitInfo.pWaitSemaphores = nullptr;
1725 submitInfo.pWaitDstStageMask = 0;
1726 submitInfo.commandBufferCount = 1;
1727 submitInfo.pCommandBuffers = &cmdBuffer;
1728 submitInfo.signalSemaphoreCount = 0;
1729 submitInfo.pSignalSemaphores = nullptr;
1730 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1731 SkASSERT(!err);
1732
1733 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1734 if (VK_TIMEOUT == err) {
1735 GrVkMemory::FreeImageMemory(this, false, alloc);
1736 VK_CALL(DestroyImage(fDevice, image, nullptr));
1737 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1738 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001739 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001740 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1741 SkDebugf("Fence failed to signal: %d\n", err);
1742 SK_ABORT("failing");
1743 }
1744 SkASSERT(!err);
1745
1746 // Clean up transfer resources
1747 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1748 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1749 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1750 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001751 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001752 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1753
Brian Salomon52e943a2018-03-13 09:32:39 -04001754 info->fImage = image;
1755 info->fAlloc = alloc;
1756 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1757 info->fImageLayout = initialLayout;
1758 info->fFormat = pixelFormat;
1759 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001760
Brian Salomon52e943a2018-03-13 09:32:39 -04001761 return true;
1762}
1763
1764GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001765 GrColorType colorType,
1766 bool isRenderTarget,
1767 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001768 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001769
1770 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1771 return GrBackendTexture();
1772 }
1773
Robert Phillips646f6372018-09-25 09:31:10 -04001774 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1775 if (!this->caps()->isConfigTexturable(config)) {
1776 return GrBackendTexture();
1777 }
1778
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001779 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001780 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001781 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001782 return {};
1783 }
Greg Daniel108bb232018-07-03 16:18:29 -04001784 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1785 // Lots of tests don't go through Skia's public interface which will set the config so for
1786 // testing we make sure we set a config here.
1787 beTex.setPixelConfig(config);
1788 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001789}
1790
1791bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001792 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001793
Greg Daniel52e16d92018-04-10 09:34:07 -04001794 GrVkImageInfo backend;
1795 if (!tex.getVkImageInfo(&backend)) {
1796 return false;
1797 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001798
Greg Daniel52e16d92018-04-10 09:34:07 -04001799 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001800 VkMemoryRequirements req;
1801 memset(&req, 0, sizeof(req));
1802 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001803 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001804 &req));
1805 // TODO: find a better check
1806 // This will probably fail with a different driver
1807 return (req.size > 0) && (req.size <= 8192 * 8192);
1808 }
1809
1810 return false;
1811}
1812
Brian Salomon26102cb2018-03-09 09:33:19 -05001813void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001814 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001815
Greg Daniel52e16d92018-04-10 09:34:07 -04001816 GrVkImageInfo info;
1817 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001818 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001819 }
1820}
1821
Brian Osman2d010b62018-08-09 10:55:09 -04001822GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001823 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1824 return GrBackendRenderTarget();
1825 }
1826
Brian Salomon8a375832018-03-14 10:21:40 -04001827 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001828 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001829 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001830 if (kUnknown_GrPixelConfig == config) {
1831 return {};
1832 }
Robert Phillips646f6372018-09-25 09:31:10 -04001833 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001834 &info)) {
1835 return {};
1836 }
Greg Daniel108bb232018-07-03 16:18:29 -04001837 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1838 // Lots of tests don't go through Skia's public interface which will set the config so for
1839 // testing we make sure we set a config here.
1840 beRT.setPixelConfig(config);
1841 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001842}
1843
Brian Salomon52e943a2018-03-13 09:32:39 -04001844void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001845 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001846
Greg Daniel323fbcf2018-04-10 13:46:30 -04001847 GrVkImageInfo info;
1848 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001849 // something in the command buffer may still be using this, so force submit
1850 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001851 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001852 }
1853}
Brian Salomonf865b052018-03-09 09:01:53 -05001854
Greg Daniel26b50a42018-03-08 09:49:58 -05001855void GrVkGpu::testingOnly_flushGpuAndSync() {
1856 this->submitCommandBuffer(kForce_SyncQueue);
1857}
Brian Salomonf865b052018-03-09 09:01:53 -05001858#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001859
Greg Daniel164a9f02016-02-22 09:56:40 -05001860////////////////////////////////////////////////////////////////////////////////
1861
Greg Daniel59dc1482019-02-22 10:46:38 -05001862void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1863 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001864 VkPipelineStageFlags dstStageMask,
1865 bool byRegion,
1866 VkBufferMemoryBarrier* 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::kBufferMemory_BarrierType,
1875 barrier);
1876}
1877
Greg Daniel59dc1482019-02-22 10:46:38 -05001878void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1879 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001880 VkPipelineStageFlags dstStageMask,
1881 bool byRegion,
1882 VkImageMemoryBarrier* barrier) const {
1883 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001884 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001885 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001886 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001887 srcStageMask,
1888 dstStageMask,
1889 byRegion,
1890 GrVkCommandBuffer::kImageMemory_BarrierType,
1891 barrier);
1892}
1893
Greg Danielbae71212019-03-01 15:24:35 -05001894void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
Greg Daniela3aa75a2019-04-12 14:24:55 -04001895 GrFlushFlags flags, bool insertedSemaphore,
1896 GrGpuFinishedProc finishedProc, GrGpuFinishedContext finishedContext) {
Greg Daniel51316782017-08-02 15:10:09 +00001897 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1898 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001899 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1900 GrVkImage* image;
1901 SkASSERT(proxy->isInstantiated());
1902 if (GrTexture* tex = proxy->peekTexture()) {
1903 image = static_cast<GrVkTexture*>(tex);
1904 } else {
1905 GrRenderTarget* rt = proxy->peekRenderTarget();
1906 SkASSERT(rt);
1907 image = static_cast<GrVkRenderTarget*>(rt);
1908 }
1909 image->prepareForPresent(this);
1910 }
Greg Danielb9990e42019-04-10 16:28:52 -04001911 if (flags & kSyncCpu_GrFlushFlag) {
Greg Daniela3aa75a2019-04-12 14:24:55 -04001912 this->submitCommandBuffer(kForce_SyncQueue, finishedProc, finishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001913 } else {
Greg Daniela3aa75a2019-04-12 14:24:55 -04001914 this->submitCommandBuffer(kSkip_SyncQueue, finishedProc, finishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001915 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001916}
1917
Greg Daniel25af6712018-04-25 10:44:38 -04001918static int get_surface_sample_cnt(GrSurface* surf) {
1919 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1920 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001921 }
Greg Daniel25af6712018-04-25 10:44:38 -04001922 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001923}
1924
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001925void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1926 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001927 GrVkImage* dstImage,
1928 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001929 const SkIRect& srcRect,
1930 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001931#ifdef SK_DEBUG
1932 int dstSampleCnt = get_surface_sample_cnt(dst);
1933 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001934 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1935 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1936 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1937 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001938
1939#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001940
Greg Daniel164a9f02016-02-22 09:56:40 -05001941 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1942 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001943 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001944 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1945 VK_ACCESS_TRANSFER_WRITE_BIT,
1946 VK_PIPELINE_STAGE_TRANSFER_BIT,
1947 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001948
egdaniel17b89252016-04-05 07:23:38 -07001949 srcImage->setImageLayout(this,
1950 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001951 VK_ACCESS_TRANSFER_READ_BIT,
1952 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001953 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001954
1955 // Flip rect if necessary
1956 SkIRect srcVkRect = srcRect;
1957 int32_t dstY = dstPoint.fY;
1958
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001959 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1960 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001961 srcVkRect.fTop = src->height() - srcRect.fBottom;
1962 srcVkRect.fBottom = src->height() - srcRect.fTop;
1963 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1964 }
1965
1966 VkImageCopy copyRegion;
1967 memset(&copyRegion, 0, sizeof(VkImageCopy));
1968 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1969 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1970 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1971 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001972 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001973
1974 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001975 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001976 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001977 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001978 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1979 1,
1980 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001981
1982 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1983 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001984 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001985}
1986
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001987void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1988 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001989 GrVkImage* dstImage,
1990 GrVkImage* srcImage,
1991 const SkIRect& srcRect,
1992 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001993#ifdef SK_DEBUG
1994 int dstSampleCnt = get_surface_sample_cnt(dst);
1995 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001996 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1997 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04001998 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04001999 dstHasYcbcr, src->config(), srcSampleCnt,
2000 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002001
Greg Daniel25af6712018-04-25 10:44:38 -04002002#endif
egdaniel17b89252016-04-05 07:23:38 -07002003 dstImage->setImageLayout(this,
2004 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002005 VK_ACCESS_TRANSFER_WRITE_BIT,
2006 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002007 false);
2008
egdaniel17b89252016-04-05 07:23:38 -07002009 srcImage->setImageLayout(this,
2010 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002011 VK_ACCESS_TRANSFER_READ_BIT,
2012 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002013 false);
2014
2015 // Flip rect if necessary
2016 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002017 srcVkRect.fLeft = srcRect.fLeft;
2018 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002019 SkIRect dstRect;
2020 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002021 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002022
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002023 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002024 srcVkRect.fTop = src->height() - srcRect.fBottom;
2025 srcVkRect.fBottom = src->height() - srcRect.fTop;
2026 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002027 srcVkRect.fTop = srcRect.fTop;
2028 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002029 }
2030
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002031 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002032 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2033 } else {
2034 dstRect.fTop = dstPoint.fY;
2035 }
2036 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2037
2038 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2039 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002040 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002041 using std::swap;
2042 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002043 }
2044
2045 VkImageBlit blitRegion;
2046 memset(&blitRegion, 0, sizeof(VkImageBlit));
2047 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2048 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002049 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002050 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2051 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002052 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002053
2054 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002055 *srcImage,
2056 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002057 1,
2058 &blitRegion,
2059 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002060
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002061 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002062 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002063}
2064
Brian Salomon1fabd512018-02-09 09:54:25 -05002065void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2066 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2067 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002068 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002069 SkIRect srcRect = origSrcRect;
2070 SkIPoint dstPoint = origDstPoint;
2071 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2072 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2073 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2074 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2075 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2076 }
2077 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002078 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2079 srcRect.width(), srcRect.height());
2080 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002081}
2082
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002083bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2084 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002085 const SkIRect& srcRect, const SkIPoint& dstPoint,
2086 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002087#ifdef SK_DEBUG
2088 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2089 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2090 }
2091 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2092 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2093 }
2094#endif
2095
Greg Daniel25af6712018-04-25 10:44:38 -04002096 GrPixelConfig dstConfig = dst->config();
2097 GrPixelConfig srcConfig = src->config();
2098
2099 int dstSampleCnt = get_surface_sample_cnt(dst);
2100 int srcSampleCnt = get_surface_sample_cnt(src);
2101
egdaniel17b89252016-04-05 07:23:38 -07002102 GrVkImage* dstImage;
2103 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002104 GrRenderTarget* dstRT = dst->asRenderTarget();
2105 if (dstRT) {
2106 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002107 if (vkRT->wrapsSecondaryCommandBuffer()) {
2108 return false;
2109 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002110 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2111 } else {
2112 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002113 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002114 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002115 GrRenderTarget* srcRT = src->asRenderTarget();
2116 if (srcRT) {
2117 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2118 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002119 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002120 SkASSERT(src->asTexture());
2121 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002122 }
2123
Greg Daniela51e93c2019-03-25 12:30:45 -04002124 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2125 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2126
2127 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2128 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2129 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2130 return true;
2131 }
2132
2133 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2134 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2135 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2136 dstPoint, canDiscardOutsideDstRect));
2137 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2138 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2139 return true;
2140 }
2141
2142 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2143 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002144 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2145 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002146 return true;
2147 }
2148
Greg Daniel25af6712018-04-25 10:44:38 -04002149 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002150 dstHasYcbcr, srcConfig, srcSampleCnt,
2151 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002152 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2153 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002154 return true;
2155 }
2156
Greg Daniel164a9f02016-02-22 09:56:40 -05002157 return false;
2158}
2159
Brian Salomona6948702018-06-01 15:33:20 -04002160bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2161 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002162 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002163 return false;
2164 }
2165
egdaniel66933552016-08-24 07:22:19 -07002166 GrVkImage* image = nullptr;
2167 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2168 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002169 // Reading from render targets that wrap a secondary command buffer is not allowed since
2170 // it would require us to know the VkImage, which we don't have, as well as need us to
2171 // stop and start the VkRenderPass which we don't have access to.
2172 if (rt->wrapsSecondaryCommandBuffer()) {
2173 return false;
2174 }
egdaniel66933552016-08-24 07:22:19 -07002175 // resolve the render target if necessary
2176 switch (rt->getResolveType()) {
2177 case GrVkRenderTarget::kCantResolve_ResolveType:
2178 return false;
2179 case GrVkRenderTarget::kAutoResolves_ResolveType:
2180 break;
2181 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002182 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002183 break;
2184 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002185 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002186 }
2187 image = rt;
2188 } else {
2189 image = static_cast<GrVkTexture*>(surface->asTexture());
2190 }
2191
2192 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002193 return false;
2194 }
2195
Greg Daniel475eb702018-09-28 14:16:50 -04002196 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2197 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2198 // image and then do the read pixels from that.
2199 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002200 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2201 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002202
2203 // Make a new surface that is RGBA to copy the RGB surface into.
2204 GrSurfaceDesc surfDesc;
2205 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2206 surfDesc.fWidth = width;
2207 surfDesc.fHeight = height;
2208 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2209 surfDesc.fSampleCnt = 1;
2210
2211 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2212 VK_IMAGE_USAGE_SAMPLED_BIT |
2213 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2214 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2215
2216 GrVkImage::ImageDesc imageDesc;
2217 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2218 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2219 imageDesc.fWidth = width;
2220 imageDesc.fHeight = height;
2221 imageDesc.fLevels = 1;
2222 imageDesc.fSamples = 1;
2223 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2224 imageDesc.fUsageFlags = usageFlags;
2225 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2226
2227 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2228 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2229 if (!copySurface) {
2230 return false;
2231 }
2232
2233 int srcSampleCount = 0;
2234 if (rt) {
2235 srcSampleCount = rt->numColorSamples();
2236 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002237 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002238 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002239 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2240 surface->config(), srcSampleCount, kOrigin,
2241 srcHasYcbcr) &&
2242 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2243 surface->config(), SkToBool(surface->asTexture()),
2244 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002245 return false;
2246 }
2247 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2248 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2249 srcRect, SkIPoint::Make(0,0))) {
2250 return false;
2251 }
2252 top = 0;
2253 left = 0;
2254 dstColorType = GrColorType::kRGBA_8888;
2255 image = copySurface.get();
2256 }
2257
Greg Daniel164a9f02016-02-22 09:56:40 -05002258 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002259 image->setImageLayout(this,
2260 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2261 VK_ACCESS_TRANSFER_READ_BIT,
2262 VK_PIPELINE_STAGE_TRANSFER_BIT,
2263 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002264
Brian Salomonc320b152018-02-20 14:05:36 -05002265 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002266 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002267
Greg Daniel164a9f02016-02-22 09:56:40 -05002268 VkBufferImageCopy region;
2269 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002270
2271 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2272 if (copyFromOrigin) {
2273 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002274 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002275 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002276 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002277 region.imageOffset = offset;
2278 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2279 }
2280
2281 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002282 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002283 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002284 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002285 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002286 kStream_GrAccessPattern)
2287 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002288
2289 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002290 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002291 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002292 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2293 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002294
2295 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002296 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002297 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002298 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002299 1,
2300 &region);
2301
2302 // make sure the copy to buffer has finished
2303 transferBuffer->addMemoryBarrier(this,
2304 VK_ACCESS_TRANSFER_WRITE_BIT,
2305 VK_ACCESS_HOST_READ_BIT,
2306 VK_PIPELINE_STAGE_TRANSFER_BIT,
2307 VK_PIPELINE_STAGE_HOST_BIT,
2308 false);
2309
2310 // We need to submit the current command buffer to the Queue and make sure it finishes before
2311 // we can copy the data out of the buffer.
2312 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002313 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002314 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002315 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002316
egdaniel6fa0a912016-09-12 11:51:29 -07002317 if (copyFromOrigin) {
2318 uint32_t skipRows = region.imageExtent.height - height;
2319 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2320 }
2321
Brian Salomona6948702018-06-01 15:33:20 -04002322 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002323
2324 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002325 return true;
2326}
egdaniel066df7c2016-06-08 14:02:27 -07002327
egdaniel27bb2842016-07-07 11:58:35 -07002328// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2329// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2330// the the entire attachment. Similar requirements for the y and height components.
2331void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2332 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2333 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002334 if ((0 != granularity.width && 1 != granularity.width)) {
2335 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2336 int rightAdj = srcBounds.fRight % granularity.width;
2337 if (rightAdj != 0) {
2338 rightAdj = granularity.width - rightAdj;
2339 }
2340 dstBounds->fRight = srcBounds.fRight + rightAdj;
2341 if (dstBounds->fRight > maxWidth) {
2342 dstBounds->fRight = maxWidth;
2343 dstBounds->fLeft = 0;
2344 } else {
2345 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2346 }
egdaniel27bb2842016-07-07 11:58:35 -07002347 } else {
egdanield5797b32016-09-20 12:57:45 -07002348 dstBounds->fLeft = srcBounds.fLeft;
2349 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002350 }
2351
2352 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002353 if ((0 != granularity.height && 1 != granularity.height)) {
2354 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2355 int bottomAdj = srcBounds.fBottom % granularity.height;
2356 if (bottomAdj != 0) {
2357 bottomAdj = granularity.height - bottomAdj;
2358 }
2359 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2360 if (dstBounds->fBottom > maxHeight) {
2361 dstBounds->fBottom = maxHeight;
2362 dstBounds->fTop = 0;
2363 } else {
2364 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2365 }
egdaniel27bb2842016-07-07 11:58:35 -07002366 } else {
egdanield5797b32016-09-20 12:57:45 -07002367 dstBounds->fTop = srcBounds.fTop;
2368 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002369 }
2370}
2371
Greg Daniel22bc8652017-03-22 15:45:43 -04002372void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002373 const GrVkRenderPass* renderPass,
2374 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002375 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002376 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002377 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002378 const SkIRect* pBounds = &bounds;
2379 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002380 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002381 flippedBounds = bounds;
2382 flippedBounds.fTop = target->height() - bounds.fBottom;
2383 flippedBounds.fBottom = target->height() - bounds.fTop;
2384 pBounds = &flippedBounds;
2385 }
2386
egdaniel27bb2842016-07-07 11:58:35 -07002387 // The bounds we use for the render pass should be of the granularity supported
2388 // by the device.
2389 const VkExtent2D& granularity = renderPass->granularity();
2390 SkIRect adjustedBounds;
2391 if ((0 != granularity.width && 1 != granularity.width) ||
2392 (0 != granularity.height && 1 != granularity.height)) {
2393 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2394 target->width(), target->height());
2395 pBounds = &adjustedBounds;
2396 }
2397
Robert Phillips95214472017-08-08 18:00:03 -04002398#ifdef SK_DEBUG
2399 uint32_t index;
2400 bool result = renderPass->colorAttachmentIndex(&index);
2401 SkASSERT(result && 0 == index);
2402 result = renderPass->stencilAttachmentIndex(&index);
2403 if (result) {
2404 SkASSERT(1 == index);
2405 }
2406#endif
2407 VkClearValue clears[2];
2408 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002409 clears[1].depthStencil.depth = 0.0f;
2410 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002411
2412 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002413 for (int i = 0; i < buffers.count(); ++i) {
2414 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2415 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002416 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002417
Brian Salomon1fabd512018-02-09 09:54:25 -05002418 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002419}
egdaniel9cb63402016-06-23 08:37:05 -07002420
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002421void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2422 if (buffer->asRTCommandBuffer()) {
2423 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2424
2425 fCachedRTCommandBuffer->submit();
2426 fCachedRTCommandBuffer->reset();
2427 } else {
2428 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2429
2430 fCachedTexCommandBuffer->submit();
2431 fCachedTexCommandBuffer->reset();
2432 }
2433}
2434
Greg Daniel6be35232017-03-01 17:01:09 -05002435GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002436 VkFenceCreateInfo createInfo;
2437 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2438 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2439 createInfo.pNext = nullptr;
2440 createInfo.flags = 0;
2441 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002442
2443 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2444 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2445
2446 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002447 return (GrFence)fence;
2448}
2449
Greg Daniel6be35232017-03-01 17:01:09 -05002450bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2451 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2452
2453 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002454 return (VK_SUCCESS == result);
2455}
2456
2457void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002458 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2459}
2460
Greg Daniela5cb7812017-06-16 09:45:32 -04002461sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2462 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002463}
2464
Greg Daniel48661b82018-01-22 16:11:35 -05002465sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2466 GrResourceProvider::SemaphoreWrapType wrapType,
2467 GrWrapOwnership ownership) {
2468 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002469}
2470
Greg Daniel858e12c2018-12-06 11:11:37 -05002471void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002472 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2473
Greg Daniel48661b82018-01-22 16:11:35 -05002474 GrVkSemaphore::Resource* resource = vkSem->getResource();
2475 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002476 resource->ref();
2477 fSemaphoresToSignal.push_back(resource);
2478 }
Greg Daniel6be35232017-03-01 17:01:09 -05002479}
2480
Greg Daniel48661b82018-01-22 16:11:35 -05002481void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002482 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2483
Greg Daniel48661b82018-01-22 16:11:35 -05002484 GrVkSemaphore::Resource* resource = vkSem->getResource();
2485 if (resource->shouldWait()) {
2486 resource->ref();
2487 fSemaphoresToWaitOn.push_back(resource);
2488 }
jvanverth84741b32016-09-30 08:39:02 -07002489}
Brian Osman13dddce2017-05-09 13:19:50 -04002490
2491sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2492 SkASSERT(texture);
2493 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2494 vkTexture->setImageLayout(this,
2495 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2496 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002497 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002498 false);
2499 this->submitCommandBuffer(kSkip_SyncQueue);
2500
2501 // The image layout change serves as a barrier, so no semaphore is needed
2502 return nullptr;
2503}
Greg Danielf5d87582017-12-18 14:48:15 -05002504
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002505void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2506 fDrawables.emplace_back(std::move(drawable));
2507}
2508
Greg Daniel7a82edf2018-12-04 10:54:34 -05002509uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2510 const GrBackendFormat& format) {
2511 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2512 SkASSERT(ycbcrInfo);
2513 if (!ycbcrInfo->isValid()) {
2514 return 0;
2515 }
2516
2517 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2518 samplerState, *ycbcrInfo);
2519
2520 return sampler->uniqueID();
2521}
2522
Greg Daniela870b462019-01-08 15:49:46 -05002523void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002524 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002525 this->resourceProvider().storePipelineCacheData();
2526 }
2527}