blob: 546a54867ddc1ccdf433cd4bd65262369c9ec660 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrVkGpu.h"
9
Ethan Nicholas8e265a72018-12-12 16:22:40 -050010#include "GrContextPriv.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040011#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrContextOptions.h"
14#include "GrGeometryProcessor.h"
15#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070016#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrPipeline.h"
18#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040020#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkCommandBuffer.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050022#include "GrVkCommandPool.h"
egdaniel066df7c2016-06-08 14:02:27 -070023#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050024#include "GrVkImage.h"
25#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040026#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050027#include "GrVkMemory.h"
28#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070029#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050030#include "GrVkRenderPass.h"
31#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050032#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050033#include "GrVkTexture.h"
34#include "GrVkTextureRenderTarget.h"
35#include "GrVkTransferBuffer.h"
36#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050037#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070038#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "SkSLCompiler.h"
40#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040041
Greg Daniela31f4e52018-08-01 16:48:52 -040042#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070043#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050044
Ben Wagnerf08d1d02018-06-18 15:11:00 -040045#include <utility>
46
Forrest Reiling44f85712017-03-27 23:22:20 -070047#if !defined(SK_BUILD_FOR_WIN)
48#include <unistd.h>
49#endif // !defined(SK_BUILD_FOR_WIN)
50
Greg Danieldef55462018-08-01 13:40:14 -040051#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
52#include "SkLeanWindows.h"
53#endif
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
56#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
57#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
58
Greg Danielf730c182018-07-02 20:15:37 +000059sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050060 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000061 if (backendContext.fInstance == VK_NULL_HANDLE ||
62 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
63 backendContext.fDevice == VK_NULL_HANDLE ||
64 backendContext.fQueue == VK_NULL_HANDLE) {
65 return nullptr;
66 }
Greg Danield3e65aa2018-08-01 09:19:45 -040067 if (!backendContext.fGetProc) {
68 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040069 }
Greg Danield3e65aa2018-08-01 09:19:45 -040070
Greg Daniel41f0e282019-01-28 13:15:05 -050071 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
72 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
73 backendContext.fGetProc("vkEnumerateInstanceVersion",
74 VK_NULL_HANDLE, VK_NULL_HANDLE));
75 uint32_t instanceVersion = 0;
76 if (!localEnumerateInstanceVersion) {
77 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
78 } else {
79 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
80 if (err) {
81 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
82 return nullptr;
83 }
84 }
85
Greg Danielc0b03d82018-08-03 14:41:15 -040086 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
87 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
88 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
89 backendContext.fInstance,
90 VK_NULL_HANDLE));
91
92 if (!localGetPhysicalDeviceProperties) {
93 return nullptr;
94 }
95 VkPhysicalDeviceProperties physDeviceProperties;
96 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
97 uint32_t physDevVersion = physDeviceProperties.apiVersion;
98
Greg Daniel41f0e282019-01-28 13:15:05 -050099 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
100 : instanceVersion;
101
102 instanceVersion = SkTMin(instanceVersion, apiVersion);
103 physDevVersion = SkTMin(physDevVersion, apiVersion);
104
Greg Daniel98bffae2018-08-01 13:25:41 -0400105 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400106
Greg Daniel98bffae2018-08-01 13:25:41 -0400107 if (backendContext.fVkExtensions) {
108 interface.reset(new GrVkInterface(backendContext.fGetProc,
109 backendContext.fInstance,
110 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500111 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400112 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500114 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400115 return nullptr;
116 }
117 } else {
118 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
119 // make an empty GrVkExtensions and pass that to the GrVkInterface.
120 GrVkExtensions extensions;
121 interface.reset(new GrVkInterface(backendContext.fGetProc,
122 backendContext.fInstance,
123 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500124 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400125 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400126 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500127 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400128 return nullptr;
129 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 }
131
Greg Daniel41f0e282019-01-28 13:15:05 -0500132 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
133 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136////////////////////////////////////////////////////////////////////////////////
137
halcanary9d524f22016-03-29 09:03:52 -0700138GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
140 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500141 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400142 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000143 , fMemoryAllocator(backendContext.fMemoryAllocator)
144 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400145 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000146 , fDevice(backendContext.fDevice)
147 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400148 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500149 , fResourceProvider(this)
150 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000151 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700152
Greg Daniel81df0412018-05-31 13:13:33 -0400153 if (!fMemoryAllocator) {
154 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000155 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400156 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400157 }
158
ethannicholasb3058bd2016-07-01 08:22:01 -0700159 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700160
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400162 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400163 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500164 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400165 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400166 } else if (backendContext.fDeviceFeatures) {
167 VkPhysicalDeviceFeatures2 features2;
168 features2.pNext = nullptr;
169 features2.features = *backendContext.fDeviceFeatures;
170 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500171 features2, instanceVersion, physicalDeviceVersion,
172 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400173 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400174 VkPhysicalDeviceFeatures2 features;
175 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
176 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400177 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400178 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400179 }
180 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400182 }
183 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400185 }
186 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500187 features, instanceVersion, physicalDeviceVersion,
188 GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400189 }
jvanverth633b3562016-03-23 11:01:22 -0700190 fCaps.reset(SkRef(fVkCaps.get()));
191
Greg Danielf730c182018-07-02 20:15:37 +0000192 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
193 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700194
Greg Daniela870b462019-01-08 15:49:46 -0500195 fResourceProvider.init();
196
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500197 fCmdPool = fResourceProvider.findOrCreateCommandPool();
198 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000199 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700200 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500201}
202
Greg Daniel8606cf82017-05-08 16:17:53 -0400203void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500204 if (fCmdPool) {
205 fCmdPool->getPrimaryCommandBuffer()->end(this);
206 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400207 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
209 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500210 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700211
212 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
213 // on the command buffers even though they have completed. This causes an assert to fire when
214 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500215 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700216#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500217 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700218#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500219 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700220#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500221 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700222#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500223 }
egdanielf8c2be32016-06-24 13:18:27 -0700224#endif
225
egdanielbe9d8212016-09-20 08:54:23 -0700226#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400227 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700228#endif
halcanary9d524f22016-03-29 09:03:52 -0700229
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500230 if (fCmdPool) {
231 fCmdPool->unref(this);
232 fCmdPool = nullptr;
233 }
234
Greg Daniel6be35232017-03-01 17:01:09 -0500235 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
236 fSemaphoresToWaitOn[i]->unref(this);
237 }
238 fSemaphoresToWaitOn.reset();
239
Greg Daniela5cb7812017-06-16 09:45:32 -0400240 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
241 fSemaphoresToSignal[i]->unref(this);
242 }
243 fSemaphoresToSignal.reset();
244
245
egdanielbc9b2962016-09-27 08:00:53 -0700246 fCopyManager.destroyResources(this);
247
Jim Van Verth09557d72016-11-07 11:10:21 -0500248 // must call this just before we destroy the command pool and VkDevice
249 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500250
Greg Danielf730c182018-07-02 20:15:37 +0000251 fMemoryAllocator.reset();
252
253 fQueue = VK_NULL_HANDLE;
254 fDevice = VK_NULL_HANDLE;
255 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400256}
257
258GrVkGpu::~GrVkGpu() {
259 if (!fDisconnected) {
260 this->destroyResources();
261 }
262 delete fCompiler;
263}
264
265
266void GrVkGpu::disconnect(DisconnectType type) {
267 INHERITED::disconnect(type);
268 if (!fDisconnected) {
269 if (DisconnectType::kCleanup == type) {
270 this->destroyResources();
271 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500272 if (fCmdPool) {
273 fCmdPool->unrefAndAbandon();
274 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400275 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400276 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
277 fSemaphoresToWaitOn[i]->unrefAndAbandon();
278 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400279 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
280 fSemaphoresToSignal[i]->unrefAndAbandon();
281 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400282 fCopyManager.abandonResources();
283
284 // must call this just before we destroy the command pool and VkDevice
285 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400286
287 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400288 }
289 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400290 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400291 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400292 fDisconnected = true;
293 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500294}
295
296///////////////////////////////////////////////////////////////////////////////
297
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400298GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400299 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400300 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
301 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400302 if (!fCachedRTCommandBuffer) {
303 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
304 }
305
Greg Daniela41a74a2018-10-09 12:59:23 +0000306 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400307 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400308}
309
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400310GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
311 if (!fCachedTexCommandBuffer) {
312 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
313 }
314
315 fCachedTexCommandBuffer->set(texture, origin);
316 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700317}
318
Greg Daniela5cb7812017-06-16 09:45:32 -0400319void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400321
322 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
323 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
324 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400325 fResourceProvider.checkCommandBuffers();
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400326 return;
327 }
328
Greg Daniel164a9f02016-02-22 09:56:40 -0500329 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500330 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400331 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500332
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400333 // We must delete and drawables that have been waitint till submit for us to destroy.
334 fDrawables.reset();
335
Greg Daniel6be35232017-03-01 17:01:09 -0500336 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
337 fSemaphoresToWaitOn[i]->unref(this);
338 }
339 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400340 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
341 fSemaphoresToSignal[i]->unref(this);
342 }
343 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500344
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500345 // Release old command pool and create a new one
346 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500347 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500348 fCmdPool = fResourceProvider.findOrCreateCommandPool();
349 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500350 fCurrentCmdBuffer->begin(this);
351}
352
353///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500354sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
355 GrAccessPattern accessPattern, const void* data) {
356 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700357 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500358 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700359 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
360 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500361 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700362 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500363 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700364 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
365 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500366 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700367 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500368 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400369 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
370 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500371 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700372 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500373 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400374 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
375 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500376 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700377 break;
cdalton397536c2016-03-25 12:15:03 -0700378 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400379 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700380 return nullptr;
381 }
cdalton1bf3e712016-04-19 10:00:02 -0700382 if (data && buff) {
383 buff->updateData(data, size);
384 }
385 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500386}
387
Brian Salomona9b04b92018-06-01 15:04:28 -0400388bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
389 GrColorType srcColorType, const GrMipLevel texels[],
390 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500391 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
392 if (!vkTex) {
393 return false;
394 }
395
jvanverth900bd4a2016-04-29 13:53:12 -0700396 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400397 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800398 return false;
399 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800400
Jim Van Verth1676cb92019-01-15 13:24:45 -0500401 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500402 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400403 bool linearTiling = vkTex->isLinearTiled();
404 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400405 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400406 SkDebugf("Can't upload mipmap data to linear tiled texture");
407 return false;
408 }
409 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
410 // Need to change the layout to general in order to perform a host write
411 vkTex->setImageLayout(this,
412 VK_IMAGE_LAYOUT_GENERAL,
413 VK_ACCESS_HOST_WRITE_BIT,
414 VK_PIPELINE_STAGE_HOST_BIT,
415 false);
416 this->submitCommandBuffer(kForce_SyncQueue);
417 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400418 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400419 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500420 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400421 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400422 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
423 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500424 }
egdaniel4583ec52016-06-27 12:57:00 -0700425
jvanverth900bd4a2016-04-29 13:53:12 -0700426 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500427}
428
Brian Salomone05ba5a2019-04-08 11:59:07 -0400429bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
430 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
431 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500432 // Can't transfer compressed data
433 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
434
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400435 // Vulkan only supports 4-byte aligned offsets
436 if (SkToBool(bufferOffset & 0x2)) {
437 return false;
438 }
439 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
440 if (!vkTex) {
441 return false;
442 }
443 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
444 if (!vkBuffer) {
445 return false;
446 }
447
Greg Daniel660cc992017-06-26 14:55:05 -0400448 SkDEBUGCODE(
449 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
450 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
451 SkASSERT(bounds.contains(subRect));
452 )
Brian Salomonc320b152018-02-20 14:05:36 -0500453 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400454 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500455 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400456 }
457
458 // Set up copy region
459 VkBufferImageCopy region;
460 memset(&region, 0, sizeof(VkBufferImageCopy));
461 region.bufferOffset = bufferOffset;
462 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
463 region.bufferImageHeight = 0;
464 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
465 region.imageOffset = { left, top, 0 };
466 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
467
468 // Change layout of our target so it can be copied to
469 vkTex->setImageLayout(this,
470 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
471 VK_ACCESS_TRANSFER_WRITE_BIT,
472 VK_PIPELINE_STAGE_TRANSFER_BIT,
473 false);
474
475 // Copy the buffer to the image
476 fCurrentCmdBuffer->copyBufferToImage(this,
477 vkBuffer,
478 vkTex,
479 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
480 1,
481 &region);
482
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400483 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400484 return true;
485}
486
Brian Salomon1fabd512018-02-09 09:54:25 -0500487void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
488 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700489 SkASSERT(dst);
490 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
491
egdaniel4bcd62e2016-08-31 07:37:31 -0700492 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500493 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
494 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
495 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
496 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
497 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700498
Greg Danielbc26c392017-04-18 13:32:10 -0400499 GrVkImage* dstImage;
500 GrRenderTarget* dstRT = dst->asRenderTarget();
501 if (dstRT) {
502 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400503 dstImage = vkRT;
504 } else {
505 SkASSERT(dst->asTexture());
506 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
507 }
508 dstImage->setImageLayout(this,
509 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
510 VK_ACCESS_TRANSFER_WRITE_BIT,
511 VK_PIPELINE_STAGE_TRANSFER_BIT,
512 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700513
514 src->msaaImage()->setImageLayout(this,
515 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
516 VK_ACCESS_TRANSFER_READ_BIT,
517 VK_PIPELINE_STAGE_TRANSFER_BIT,
518 false);
519
Greg Danielbc26c392017-04-18 13:32:10 -0400520 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700521}
522
Brian Salomon1fabd512018-02-09 09:54:25 -0500523void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700524 if (target->needsResolve()) {
525 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700526 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
527 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500528
egdaniel4bcd62e2016-08-31 07:37:31 -0700529 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700530
Brian Salomon1fabd512018-02-09 09:54:25 -0500531 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700532
533 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500534
535 if (requiresSubmit) {
536 this->submitCommandBuffer(kSkip_SyncQueue);
537 }
egdaniel52ad2512016-08-04 12:50:01 -0700538 }
539}
540
Brian Salomona9b04b92018-06-01 15:04:28 -0400541bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
542 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500543 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700544 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500545
Jim Van Verth1676cb92019-01-15 13:24:45 -0500546 // If we're uploading compressed data then we should be using uploadCompressedTexData
547 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
548 GrSRGBEncoded::kNo)));
549
Greg Daniel660cc992017-06-26 14:55:05 -0400550 SkDEBUGCODE(
551 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
552 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
553 SkASSERT(bounds.contains(subRect));
554 )
Brian Salomonc320b152018-02-20 14:05:36 -0500555 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500556 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400557 if (!rowBytes) {
558 rowBytes = trimRowBytes;
559 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500560
jvanverth900bd4a2016-04-29 13:53:12 -0700561 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
562 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
563 const VkImageSubresource subres = {
564 VK_IMAGE_ASPECT_COLOR_BIT,
565 0, // mipLevel
566 0, // arraySlice
567 };
568 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500569
jvanverth900bd4a2016-04-29 13:53:12 -0700570 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500571
jvanverth900bd4a2016-04-29 13:53:12 -0700572 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700573 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700574 &subres,
575 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500576
jvanverth1e305ba2016-06-01 09:39:15 -0700577 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400578 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700579 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400580 SkASSERT(size + offset <= alloc.fSize);
581 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
582 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700583 return false;
584 }
Greg Daniel81df0412018-05-31 13:13:33 -0400585 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700586
Brian Salomona9b04b92018-06-01 15:04:28 -0400587 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
588 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700589
Greg Daniele35a99e2018-03-02 11:44:22 -0500590 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400591 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700592
593 return true;
594}
595
Brian Salomona9b04b92018-06-01 15:04:28 -0400596bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
597 GrColorType dataColorType, const GrMipLevel texels[],
598 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700599 SkASSERT(!tex->isLinearTiled());
600 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400601 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700602 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
603
Greg Danieldd20e912017-04-07 14:42:23 -0400604 // We assume that if the texture has mip levels, we either upload to all the levels or just the
605 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400606 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400607
Jim Van Verth1676cb92019-01-15 13:24:45 -0500608 // If we're uploading compressed data then we should be using uploadCompressedTexData
609 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
610 GrSRGBEncoded::kNo)));
611
jvanverth900bd4a2016-04-29 13:53:12 -0700612 if (width == 0 || height == 0) {
613 return false;
614 }
615
Greg Daniel475eb702018-09-28 14:16:50 -0400616 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
617 return false;
618 }
619
620 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
621 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500622 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
623 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400624 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
625 // blit or draw.
626 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
627 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
628 return false;
629 }
630 mipLevelCount = 1;
631 }
632
Brian Salomond1eaf492017-05-18 10:02:08 -0400633 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500634 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700635
636 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700637 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
638 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400639 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
640
Greg Daniel475eb702018-09-28 14:16:50 -0400641 texelsShallowCopy.reset(mipLevelCount);
642 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700643
Robert Phillips590533f2017-07-11 14:22:35 -0400644 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700645 individualMipOffsets.push_back(0);
646 size_t combinedBufferSize = width * bpp * height;
647 int currentWidth = width;
648 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400649 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400650 combinedBufferSize = 0;
651 }
652
Greg Daniel468fd632017-03-22 17:03:45 -0400653 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
654 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
655 SkASSERT((bpp & (bpp - 1)) == 0);
656 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400657 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700658 currentWidth = SkTMax(1, currentWidth/2);
659 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400660
Greg Daniel55afd6d2017-09-29 09:32:44 -0400661 if (texelsShallowCopy[currentMipLevel].fPixels) {
662 const size_t trimmedSize = currentWidth * bpp * currentHeight;
663 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
664 if (alignmentDiff != 0) {
665 combinedBufferSize += alignmentMask - alignmentDiff + 1;
666 }
667 individualMipOffsets.push_back(combinedBufferSize);
668 combinedBufferSize += trimmedSize;
669 } else {
670 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400671 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400672 }
673 if (0 == combinedBufferSize) {
674 // We don't actually have any data to upload so just return success
675 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700676 }
677
678 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500679 sk_sp<GrVkTransferBuffer> transferBuffer =
680 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400681 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700682 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400683 }
jvanverth900bd4a2016-04-29 13:53:12 -0700684
Greg Daniel475eb702018-09-28 14:16:50 -0400685 int uploadLeft = left;
686 int uploadTop = top;
687 GrVkTexture* uploadTexture = tex;
688 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
689 // R8G8B8A8_UNORM image and then copy it.
690 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500691 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400692 GrSurfaceDesc surfDesc;
693 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
694 surfDesc.fWidth = width;
695 surfDesc.fHeight = height;
696 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
697 surfDesc.fSampleCnt = 1;
698
699 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
700 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
701 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
702
703 GrVkImage::ImageDesc imageDesc;
704 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
705 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
706 imageDesc.fWidth = width;
707 imageDesc.fHeight = height;
708 imageDesc.fLevels = 1;
709 imageDesc.fSamples = 1;
710 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
711 imageDesc.fUsageFlags = usageFlags;
712 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
713
714 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
715 GrMipMapsStatus::kNotAllocated);
716 if (!copyTexture) {
717 return false;
718 }
719 uploadTexture = copyTexture.get();
720 uploadLeft = 0;
721 uploadTop = 0;
722 }
723
jvanverth900bd4a2016-04-29 13:53:12 -0700724 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400725 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700726
jvanverthc578b0632016-05-02 10:58:12 -0700727 currentWidth = width;
728 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400729 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400730 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400731 if (texelsShallowCopy[currentMipLevel].fPixels) {
732 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
733 const size_t trimRowBytes = currentWidth * bpp;
734 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
735 ? texelsShallowCopy[currentMipLevel].fRowBytes
736 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700737
Greg Daniel55afd6d2017-09-29 09:32:44 -0400738 // copy data into the buffer, skipping the trailing bytes
739 char* dst = buffer + individualMipOffsets[currentMipLevel];
740 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400741 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400742
743 VkBufferImageCopy& region = regions.push_back();
744 memset(&region, 0, sizeof(VkBufferImageCopy));
745 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
746 region.bufferRowLength = currentWidth;
747 region.bufferImageHeight = currentHeight;
748 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400749 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400750 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700751 }
jvanverthc578b0632016-05-02 10:58:12 -0700752 currentWidth = SkTMax(1, currentWidth/2);
753 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400754 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700755 }
756
jvanverth9d54afc2016-09-20 09:20:03 -0700757 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700758 transferBuffer->unmap();
759
jvanverth900bd4a2016-04-29 13:53:12 -0700760 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400761 uploadTexture->setImageLayout(this,
762 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
763 VK_ACCESS_TRANSFER_WRITE_BIT,
764 VK_PIPELINE_STAGE_TRANSFER_BIT,
765 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700766
767 // Copy the buffer to the image
768 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500769 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400770 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700771 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
772 regions.count(),
773 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400774
775 // If we copied the data into a temporary image first, copy that image into our main texture
776 // now.
777 if (copyTexture.get()) {
778 SkASSERT(dataColorType == GrColorType::kRGB_888x);
779 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
780 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
781 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
782 false));
783 }
Robert Phillips590533f2017-07-11 14:22:35 -0400784 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400785 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400786 }
jvanverth900bd4a2016-04-29 13:53:12 -0700787
Greg Daniel164a9f02016-02-22 09:56:40 -0500788 return true;
789}
790
Jim Van Verth1676cb92019-01-15 13:24:45 -0500791// It's probably possible to roll this into uploadTexDataOptimal,
792// but for now it's easier to maintain as a separate entity.
793bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
794 GrColorType dataColorType, const GrMipLevel texels[],
795 int mipLevelCount) {
796 SkASSERT(!tex->isLinearTiled());
797 // For now the assumption is that our rect is the entire texture.
798 // Compressed textures are read-only so this should be a reasonable assumption.
799 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
800
801 // We assume that if the texture has mip levels, we either upload to all the levels or just the
802 // first.
803 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
804
805 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
806 GrSRGBEncoded::kNo)));
807
808 if (width == 0 || height == 0) {
809 return false;
810 }
811
812 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
813 return false;
814 }
815
816 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
817
818 SkTArray<size_t> individualMipOffsets(mipLevelCount);
819 individualMipOffsets.push_back(0);
820 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
821 int currentWidth = width;
822 int currentHeight = height;
823 if (!texels[0].fPixels) {
824 return false;
825 }
826
827 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
828 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
829 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
830 currentWidth = SkTMax(1, currentWidth / 2);
831 currentHeight = SkTMax(1, currentHeight / 2);
832
833 if (texels[currentMipLevel].fPixels) {
834 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
835 currentHeight);
836 individualMipOffsets.push_back(combinedBufferSize);
837 combinedBufferSize += dataSize;
838 } else {
839 return false;
840 }
841 }
842 if (0 == combinedBufferSize) {
843 // We don't have any data to upload so fail (compressed textures are read-only).
844 return false;
845 }
846
847 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500848 sk_sp<GrVkTransferBuffer> transferBuffer =
849 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500850 if (!transferBuffer) {
851 return false;
852 }
853
854 int uploadLeft = left;
855 int uploadTop = top;
856 GrVkTexture* uploadTexture = tex;
857
858 char* buffer = (char*)transferBuffer->map();
859 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
860
861 currentWidth = width;
862 currentHeight = height;
863 int layerHeight = uploadTexture->height();
864 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
865 if (texels[currentMipLevel].fPixels) {
866 // Again, we're assuming that our rect is the entire texture
867 SkASSERT(currentHeight == layerHeight);
868 SkASSERT(0 == uploadLeft && 0 == uploadTop);
869
870 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
871 currentHeight);
872
873 // copy data into the buffer, skipping the trailing bytes
874 char* dst = buffer + individualMipOffsets[currentMipLevel];
875 const char* src = (const char*)texels[currentMipLevel].fPixels;
876 memcpy(dst, src, dataSize);
877
878 VkBufferImageCopy& region = regions.push_back();
879 memset(&region, 0, sizeof(VkBufferImageCopy));
880 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
881 region.bufferRowLength = currentWidth;
882 region.bufferImageHeight = currentHeight;
883 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
884 region.imageOffset = { uploadLeft, uploadTop, 0 };
885 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
886 }
887 currentWidth = SkTMax(1, currentWidth / 2);
888 currentHeight = SkTMax(1, currentHeight / 2);
889 layerHeight = currentHeight;
890 }
891
892 // no need to flush non-coherent memory, unmap will do that for us
893 transferBuffer->unmap();
894
895 // Change layout of our target so it can be copied to
896 uploadTexture->setImageLayout(this,
897 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
898 VK_ACCESS_TRANSFER_WRITE_BIT,
899 VK_PIPELINE_STAGE_TRANSFER_BIT,
900 false);
901
902 // Copy the buffer to the image
903 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500904 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500905 uploadTexture,
906 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
907 regions.count(),
908 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500909
910 if (1 == mipLevelCount) {
911 tex->texturePriv().markMipMapsDirty();
912 }
913
914 return true;
915}
916
Greg Daniel164a9f02016-02-22 09:56:40 -0500917////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400918sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500919 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500920 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
921
922 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500923 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700924
Greg Daniel164a9f02016-02-22 09:56:40 -0500925 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
926 if (renderTarget) {
927 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
928 }
929
930 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
931 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
932 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700933 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500934 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
935 // texture.
936 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
937
Greg Daniel164a9f02016-02-22 09:56:40 -0500938 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700939 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500940 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400941 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500942 GrVkImage::ImageDesc imageDesc;
943 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
944 imageDesc.fFormat = pixelFormat;
945 imageDesc.fWidth = desc.fWidth;
946 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400947 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500948 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400949 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500950 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400951 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500952
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400953 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
954 if (mipLevels > 1) {
955 mipMapsStatus = GrMipMapsStatus::kValid;
956 for (int i = 0; i < mipLevels; ++i) {
957 if (!texels[i].fPixels) {
958 mipMapsStatus = GrMipMapsStatus::kDirty;
959 break;
960 }
Greg Daniel834f1202017-10-09 15:06:20 -0400961 }
962 }
963
Robert Phillips67d52cf2017-06-05 13:38:13 -0400964 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500965 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400966 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
967 imageDesc,
968 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500969 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400970 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500971 }
972
973 if (!tex) {
974 return nullptr;
975 }
976
Jim Van Verth1676cb92019-01-15 13:24:45 -0500977 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -0500978 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400979 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500980 bool success;
981 if (isCompressed) {
982 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
983 colorType, texels, mipLevelCount);
984 } else {
985 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
986 colorType, texels, mipLevelCount);
987 }
988 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500989 tex->unref();
990 return nullptr;
991 }
992 }
993
Jim Van Verth1676cb92019-01-15 13:24:45 -0500994 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400995 VkClearColorValue zeroClearColor;
996 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
997 VkImageSubresourceRange range;
998 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
999 range.baseArrayLayer = 0;
1000 range.baseMipLevel = 0;
1001 range.layerCount = 1;
1002 range.levelCount = 1;
1003 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1004 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001005 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001006 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001007 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001008}
1009
1010////////////////////////////////////////////////////////////////////////////////
1011
Greg Daniel6888c0d2017-08-25 11:55:50 -04001012void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1013 VkDeviceSize dstOffset, VkDeviceSize size) {
1014 VkBufferCopy copyRegion;
1015 copyRegion.srcOffset = srcOffset;
1016 copyRegion.dstOffset = dstOffset;
1017 copyRegion.size = size;
1018 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1019}
1020
jvanverthdb379092016-07-07 11:18:46 -07001021bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1022 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001023 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001024 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001025
1026 return true;
1027}
1028
1029////////////////////////////////////////////////////////////////////////////////
1030
Greg Daniel7e000222018-12-03 10:08:21 -05001031static bool check_image_info(const GrVkCaps& caps,
1032 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001033 GrPixelConfig config,
1034 bool isWrappedRT) {
1035 if (VK_NULL_HANDLE == info.fImage) {
1036 return false;
1037 }
1038
1039 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001040 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001041 }
1042
Greg Daniel7e000222018-12-03 10:08:21 -05001043 if (info.fYcbcrConversionInfo.isValid()) {
1044 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1045 return false;
1046 }
jvanverthfd359ca2016-03-18 11:57:24 -07001047 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001048
Greg Danielcb324152019-02-25 11:36:53 -05001049 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1050 return false;
1051 }
1052
Greg Daniel52e16d92018-04-10 09:34:07 -04001053 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001054 return true;
1055}
1056
1057sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001058 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1059 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001060 GrVkImageInfo imageInfo;
1061 if (!backendTex.getVkImageInfo(&imageInfo)) {
1062 return nullptr;
1063 }
1064
Greg Danielcb324152019-02-25 11:36:53 -05001065 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001066 return nullptr;
1067 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001068
Greg Daniel164a9f02016-02-22 09:56:40 -05001069 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001070 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001071 surfDesc.fWidth = backendTex.width();
1072 surfDesc.fHeight = backendTex.height();
1073 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001074 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001075
Greg Daniel52e16d92018-04-10 09:34:07 -04001076 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1077 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001078 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1079 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001080}
1081
1082sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001083 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001084 GrWrapOwnership ownership,
1085 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001086 GrVkImageInfo imageInfo;
1087 if (!backendTex.getVkImageInfo(&imageInfo)) {
1088 return nullptr;
1089 }
1090
Greg Danielcb324152019-02-25 11:36:53 -05001091 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001092 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001093 }
Brian Salomond17f6582017-07-19 18:28:58 -04001094
1095 GrSurfaceDesc surfDesc;
1096 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1097 surfDesc.fWidth = backendTex.width();
1098 surfDesc.fHeight = backendTex.height();
1099 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001100 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001101
Greg Daniel52e16d92018-04-10 09:34:07 -04001102 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1103 SkASSERT(layout);
1104
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001105 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1106 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001107}
1108
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001109sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001110 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1111 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1112 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1113 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001114 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001115 return nullptr;
1116 }
halcanary9d524f22016-03-29 09:03:52 -07001117
Greg Daniel323fbcf2018-04-10 13:46:30 -04001118 GrVkImageInfo info;
1119 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001120 return nullptr;
1121 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001122
Greg Danielcb324152019-02-25 11:36:53 -05001123 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001124 return nullptr;
1125 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001126
Greg Daniel164a9f02016-02-22 09:56:40 -05001127 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001128 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001129 desc.fWidth = backendRT.width();
1130 desc.fHeight = backendRT.height();
1131 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001132 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001133
Greg Daniel323fbcf2018-04-10 13:46:30 -04001134 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001135
Greg Daniel323fbcf2018-04-10 13:46:30 -04001136 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001137 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001138
1139 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1140 SkASSERT(!backendRT.stencilBits());
1141 if (tgt) {
1142 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001143 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001144
Ben Wagnerff134f22018-04-24 16:29:16 -04001145 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001146}
1147
Greg Daniel7ef28f32017-04-20 16:41:55 +00001148sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001149 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001150
Greg Daniel52e16d92018-04-10 09:34:07 -04001151 GrVkImageInfo imageInfo;
1152 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001153 return nullptr;
1154 }
Greg Danielcb324152019-02-25 11:36:53 -05001155 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001156 return nullptr;
1157 }
1158
Greg Danielcb324152019-02-25 11:36:53 -05001159
Brian Osman33910292017-04-18 14:38:53 -04001160 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001161 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001162 desc.fWidth = tex.width();
1163 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001164 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001165 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1166 if (!desc.fSampleCnt) {
1167 return nullptr;
1168 }
Brian Osman33910292017-04-18 14:38:53 -04001169
Greg Daniel52e16d92018-04-10 09:34:07 -04001170 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1171 SkASSERT(layout);
1172
Ben Wagnerff134f22018-04-24 16:29:16 -04001173 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001174}
1175
Greg Danielb46add82019-01-02 14:51:29 -05001176sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1177 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1178 int maxSize = this->caps()->maxTextureSize();
1179 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1180 return nullptr;
1181 }
1182
1183 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1184 if (!backendFormat.isValid()) {
1185 return nullptr;
1186 }
1187 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1188 imageInfo.colorType());
1189 if (config == kUnknown_GrPixelConfig) {
1190 return nullptr;
1191 }
1192
1193 GrSurfaceDesc desc;
1194 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1195 desc.fWidth = imageInfo.width();
1196 desc.fHeight = imageInfo.height();
1197 desc.fConfig = config;
1198 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1199 if (!desc.fSampleCnt) {
1200 return nullptr;
1201 }
1202
1203 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1204}
1205
Brian Salomon930f9392018-06-20 16:25:26 -04001206bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1207 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001208 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001209 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001210 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001211 return false;
jvanverth62340062016-04-26 08:01:44 -07001212 }
1213
jvanverth62340062016-04-26 08:01:44 -07001214 // determine if we can blit to and from this format
1215 const GrVkCaps& caps = this->vkCaps();
1216 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001217 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1218 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001219 return false;
jvanverth62340062016-04-26 08:01:44 -07001220 }
1221
egdaniel7ac5da82016-07-15 13:41:42 -07001222 int width = tex->width();
1223 int height = tex->height();
1224 VkImageBlit blitRegion;
1225 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001226
jvanverth82c05582016-05-03 11:19:01 -07001227 // SkMipMap doesn't include the base level in the level count so we have to add 1
1228 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001229 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001230
Greg Danielda86e282018-06-13 09:41:19 -04001231 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001232 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1233 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001234
jvanverth50c46c72016-05-06 12:31:28 -07001235 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001236 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001237 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1238 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001239 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1240 nullptr, // pNext
1241 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1242 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1243 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1244 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1245 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1246 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1247 vkTex->image(), // image
1248 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001249 };
1250
jvanverth62340062016-04-26 08:01:44 -07001251 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001252 uint32_t mipLevel = 1;
1253 while (mipLevel < levelCount) {
1254 int prevWidth = width;
1255 int prevHeight = height;
1256 width = SkTMax(1, width / 2);
1257 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001258
jvanverth50c46c72016-05-06 12:31:28 -07001259 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001260 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1261 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001262
1263 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001264 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001265 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001266 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1267 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001268 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001269 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001270 vkTex->resource(),
1271 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001272 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001273 vkTex->resource(),
1274 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001275 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001276 1,
1277 &blitRegion,
1278 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001279 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001280 }
Greg Danielee54f232019-04-03 14:58:40 -04001281 if (levelCount > 1) {
1282 // This barrier logically is not needed, but it changes the final level to the same layout
1283 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1284 // layouts and future layout changes easier. The alternative here would be to track layout
1285 // and memory accesses per layer which doesn't seem work it.
1286 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1287 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1288 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1289 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1290 }
Brian Salomon930f9392018-06-20 16:25:26 -04001291 return true;
jvanverth62340062016-04-26 08:01:44 -07001292}
1293
Greg Daniel164a9f02016-02-22 09:56:40 -05001294////////////////////////////////////////////////////////////////////////////////
1295
1296GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1297 int width,
1298 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001299 SkASSERT(width >= rt->width());
1300 SkASSERT(height >= rt->height());
1301
1302 int samples = rt->numStencilSamples();
1303
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001304 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001305
1306 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001307 width,
1308 height,
1309 samples,
1310 sFmt));
1311 fStats.incStencilAttachmentCreates();
1312 return stencil;
1313}
1314
1315////////////////////////////////////////////////////////////////////////////////
1316
Brian Salomon52e943a2018-03-13 09:32:39 -04001317bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001318 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1319 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001320 VkDeviceSize size = dstRowBytes * h;
1321 VkDeviceSize offset = bufferOffset;
1322 SkASSERT(size + offset <= alloc.fSize);
1323 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1324 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001325 return false;
1326 }
Greg Daniel81df0412018-05-31 13:13:33 -04001327 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001328
Greg Daniel20ece3a2017-03-28 10:24:43 -04001329 if (srcData) {
1330 // If there is no padding on dst we can do a single memcopy.
1331 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001332 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001333 } else {
1334 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1335 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001336 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001337 }
Greg Daniel81df0412018-05-31 13:13:33 -04001338 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1339 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001340 return true;
1341}
1342
Brian Salomonf865b052018-03-09 09:01:53 -05001343#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001344bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1345 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001346 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001347 SkASSERT(texturable || renderable);
1348 if (!texturable) {
1349 SkASSERT(GrMipMapped::kNo == mipMapped);
1350 SkASSERT(!srcData);
1351 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001352 VkFormat pixelFormat;
1353 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001354 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001355 }
1356
Brian Salomon52e943a2018-03-13 09:32:39 -04001357 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1358 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001359 }
1360
Brian Salomon52e943a2018-03-13 09:32:39 -04001361 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1362 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001363 }
1364
1365 // Currently we don't support uploading pixel data when mipped.
1366 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001367 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001368 }
1369
Brian Salomon52e943a2018-03-13 09:32:39 -04001370 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001371 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1372 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001373 if (texturable) {
1374 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1375 }
1376 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001377 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1378 }
1379
1380 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001381 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001382 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001383
1384 // Create Image
1385 VkSampleCountFlagBits vkSamples;
1386 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001387 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001388 }
1389
1390 // Figure out the number of mip levels.
1391 uint32_t mipLevels = 1;
1392 if (GrMipMapped::kYes == mipMapped) {
1393 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1394 }
1395
1396 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001397 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1398 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001399 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001400 VK_IMAGE_TYPE_2D, // VkImageType
1401 pixelFormat, // VkFormat
1402 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1403 mipLevels, // mipLevels
1404 1, // arrayLayers
1405 vkSamples, // samples
1406 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1407 usageFlags, // VkImageUsageFlags
1408 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1409 0, // queueFamilyCount
1410 0, // pQueueFamilyIndices
1411 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001412 };
1413
Brian Salomon52e943a2018-03-13 09:32:39 -04001414 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1415 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001416
Brian Salomonde9f5462018-03-07 14:23:58 -05001417 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001418 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001419 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001420 }
1421
1422 // 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 -05001423 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001424 VkBuffer buffer = VK_NULL_HANDLE;
1425
1426 VkResult err;
1427 const VkCommandBufferAllocateInfo cmdInfo = {
1428 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1429 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001430 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001431 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1432 1 // bufferCount
1433 };
1434
1435 VkCommandBuffer cmdBuffer;
1436 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1437 if (err) {
1438 GrVkMemory::FreeImageMemory(this, false, alloc);
1439 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001440 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001441 }
1442
1443 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1444 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1445 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1446 cmdBufferBeginInfo.pNext = nullptr;
1447 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1448 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1449
1450 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1451 SkASSERT(!err);
1452
1453 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001454 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001455
Robert Phillips646f6372018-09-25 09:31:10 -04001456 const size_t trimRowBytes = w * bpp;
1457 if (!srcRowBytes) {
1458 srcRowBytes = trimRowBytes;
1459 }
1460
Brian Salomonde9f5462018-03-07 14:23:58 -05001461 SkTArray<size_t> individualMipOffsets(mipLevels);
1462 individualMipOffsets.push_back(0);
1463 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001464 if (GrPixelConfigIsCompressed(config)) {
1465 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1466 bpp = 4; // we have at least this alignment, which will pass the code below
1467 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001468 int currentWidth = w;
1469 int currentHeight = h;
1470 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1471 // config. This works with the assumption that the bytes in pixel config is always a power
1472 // of 2.
1473 SkASSERT((bpp & (bpp - 1)) == 0);
1474 const size_t alignmentMask = 0x3 | (bpp - 1);
1475 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1476 currentWidth = SkTMax(1, currentWidth / 2);
1477 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001478
Jim Van Verth1676cb92019-01-15 13:24:45 -05001479 size_t trimmedSize;
1480 if (GrPixelConfigIsCompressed(config)) {
1481 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1482 } else {
1483 trimmedSize = currentWidth * bpp * currentHeight;
1484 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001485 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1486 if (alignmentDiff != 0) {
1487 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001488 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001489 individualMipOffsets.push_back(combinedBufferSize);
1490 combinedBufferSize += trimmedSize;
1491 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001492
Brian Salomonde9f5462018-03-07 14:23:58 -05001493 VkBufferCreateInfo bufInfo;
1494 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1495 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1496 bufInfo.flags = 0;
1497 bufInfo.size = combinedBufferSize;
1498 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1499 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1500 bufInfo.queueFamilyIndexCount = 0;
1501 bufInfo.pQueueFamilyIndices = nullptr;
1502 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001503
Brian Salomonde9f5462018-03-07 14:23:58 -05001504 if (err) {
1505 GrVkMemory::FreeImageMemory(this, false, alloc);
1506 VK_CALL(DestroyImage(fDevice, image, nullptr));
1507 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001508 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001509 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001510 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001511
Brian Salomonde9f5462018-03-07 14:23:58 -05001512 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1513 &bufferAlloc)) {
1514 GrVkMemory::FreeImageMemory(this, false, alloc);
1515 VK_CALL(DestroyImage(fDevice, image, nullptr));
1516 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1517 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001518 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001519 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001520 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001521
Brian Salomonde9f5462018-03-07 14:23:58 -05001522 currentWidth = w;
1523 currentHeight = h;
1524 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1525 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001526 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001527 bool result;
1528 if (GrPixelConfigIsCompressed(config)) {
1529 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1530 size_t currentRowBytes = levelSize / currentHeight;
1531 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1532 currentRowBytes, currentRowBytes, currentHeight);
1533 } else {
1534 size_t currentRowBytes = bpp * currentWidth;
1535 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1536 currentRowBytes, trimRowBytes, currentHeight);
1537 }
1538 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001539 GrVkMemory::FreeImageMemory(this, false, alloc);
1540 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001541 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001542 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1543 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001544 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001545 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001546 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001547 currentWidth = SkTMax(1, currentWidth / 2);
1548 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001549 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001550
1551 // Set image layout and add barrier
1552 VkImageMemoryBarrier barrier;
1553 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1554 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1555 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001556 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001557 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1558 barrier.oldLayout = initialLayout;
1559 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1560 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1561 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1562 barrier.image = image;
1563 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1564
Greg Danielf7828d02018-10-09 12:01:32 -04001565 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001566 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1567 &barrier));
1568 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1569
1570 SkTArray<VkBufferImageCopy> regions(mipLevels);
1571
1572 currentWidth = w;
1573 currentHeight = h;
1574 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1575 // Submit copy command
1576 VkBufferImageCopy& region = regions.push_back();
1577 memset(&region, 0, sizeof(VkBufferImageCopy));
1578 region.bufferOffset = individualMipOffsets[currentMipLevel];
1579 region.bufferRowLength = currentWidth;
1580 region.bufferImageHeight = currentHeight;
1581 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1582 region.imageOffset = {0, 0, 0};
1583 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1584 currentWidth = SkTMax(1, currentWidth / 2);
1585 currentHeight = SkTMax(1, currentHeight / 2);
1586 }
1587
1588 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1589 regions.begin()));
1590
Brian Salomon52e943a2018-03-13 09:32:39 -04001591 if (texturable) {
1592 // Change Image layout to shader read since if we use this texture as a borrowed textures
1593 // within Ganesh we require that its layout be set to that
1594 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1595 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1596 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001597 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001598 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1599 barrier.oldLayout = initialLayout;
1600 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1601 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1602 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1603 barrier.image = image;
1604 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001605 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001606 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1607 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001608 0,
1609 0, nullptr,
1610 0, nullptr,
1611 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001612 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001613 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001614
1615 // End CommandBuffer
1616 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1617 SkASSERT(!err);
1618
1619 // Create Fence for queue
1620 VkFence fence;
1621 VkFenceCreateInfo fenceInfo;
1622 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1623 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1624
1625 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1626 SkASSERT(!err);
1627
1628 VkSubmitInfo submitInfo;
1629 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1630 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1631 submitInfo.pNext = nullptr;
1632 submitInfo.waitSemaphoreCount = 0;
1633 submitInfo.pWaitSemaphores = nullptr;
1634 submitInfo.pWaitDstStageMask = 0;
1635 submitInfo.commandBufferCount = 1;
1636 submitInfo.pCommandBuffers = &cmdBuffer;
1637 submitInfo.signalSemaphoreCount = 0;
1638 submitInfo.pSignalSemaphores = nullptr;
1639 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1640 SkASSERT(!err);
1641
1642 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1643 if (VK_TIMEOUT == err) {
1644 GrVkMemory::FreeImageMemory(this, false, alloc);
1645 VK_CALL(DestroyImage(fDevice, image, nullptr));
1646 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1647 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001648 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001649 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1650 SkDebugf("Fence failed to signal: %d\n", err);
1651 SK_ABORT("failing");
1652 }
1653 SkASSERT(!err);
1654
1655 // Clean up transfer resources
1656 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1657 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1658 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1659 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001660 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001661 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1662
Brian Salomon52e943a2018-03-13 09:32:39 -04001663 info->fImage = image;
1664 info->fAlloc = alloc;
1665 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1666 info->fImageLayout = initialLayout;
1667 info->fFormat = pixelFormat;
1668 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001669
Brian Salomon52e943a2018-03-13 09:32:39 -04001670 return true;
1671}
1672
1673GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001674 GrColorType colorType,
1675 bool isRenderTarget,
1676 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001677 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001678
1679 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1680 return GrBackendTexture();
1681 }
1682
Robert Phillips646f6372018-09-25 09:31:10 -04001683 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1684 if (!this->caps()->isConfigTexturable(config)) {
1685 return GrBackendTexture();
1686 }
1687
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001688 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001689 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001690 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001691 return {};
1692 }
Greg Daniel108bb232018-07-03 16:18:29 -04001693 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1694 // Lots of tests don't go through Skia's public interface which will set the config so for
1695 // testing we make sure we set a config here.
1696 beTex.setPixelConfig(config);
1697 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001698}
1699
1700bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001701 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001702
Greg Daniel52e16d92018-04-10 09:34:07 -04001703 GrVkImageInfo backend;
1704 if (!tex.getVkImageInfo(&backend)) {
1705 return false;
1706 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001707
Greg Daniel52e16d92018-04-10 09:34:07 -04001708 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001709 VkMemoryRequirements req;
1710 memset(&req, 0, sizeof(req));
1711 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001712 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001713 &req));
1714 // TODO: find a better check
1715 // This will probably fail with a different driver
1716 return (req.size > 0) && (req.size <= 8192 * 8192);
1717 }
1718
1719 return false;
1720}
1721
Brian Salomon26102cb2018-03-09 09:33:19 -05001722void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001723 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001724
Greg Daniel52e16d92018-04-10 09:34:07 -04001725 GrVkImageInfo info;
1726 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001727 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001728 }
1729}
1730
Brian Osman2d010b62018-08-09 10:55:09 -04001731GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001732 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1733 return GrBackendRenderTarget();
1734 }
1735
Brian Salomon8a375832018-03-14 10:21:40 -04001736 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001737 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001738 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001739 if (kUnknown_GrPixelConfig == config) {
1740 return {};
1741 }
Robert Phillips646f6372018-09-25 09:31:10 -04001742 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001743 &info)) {
1744 return {};
1745 }
Greg Daniel108bb232018-07-03 16:18:29 -04001746 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1747 // Lots of tests don't go through Skia's public interface which will set the config so for
1748 // testing we make sure we set a config here.
1749 beRT.setPixelConfig(config);
1750 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001751}
1752
Brian Salomon52e943a2018-03-13 09:32:39 -04001753void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001754 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001755
Greg Daniel323fbcf2018-04-10 13:46:30 -04001756 GrVkImageInfo info;
1757 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001758 // something in the command buffer may still be using this, so force submit
1759 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001760 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001761 }
1762}
Brian Salomonf865b052018-03-09 09:01:53 -05001763
Greg Daniel26b50a42018-03-08 09:49:58 -05001764void GrVkGpu::testingOnly_flushGpuAndSync() {
1765 this->submitCommandBuffer(kForce_SyncQueue);
1766}
Brian Salomonf865b052018-03-09 09:01:53 -05001767#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001768
Greg Daniel164a9f02016-02-22 09:56:40 -05001769////////////////////////////////////////////////////////////////////////////////
1770
Greg Daniel59dc1482019-02-22 10:46:38 -05001771void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1772 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001773 VkPipelineStageFlags dstStageMask,
1774 bool byRegion,
1775 VkBufferMemoryBarrier* barrier) const {
1776 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001777 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001778 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001779 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001780 srcStageMask,
1781 dstStageMask,
1782 byRegion,
1783 GrVkCommandBuffer::kBufferMemory_BarrierType,
1784 barrier);
1785}
1786
Greg Daniel59dc1482019-02-22 10:46:38 -05001787void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1788 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001789 VkPipelineStageFlags dstStageMask,
1790 bool byRegion,
1791 VkImageMemoryBarrier* barrier) const {
1792 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001793 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001794 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001795 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001796 srcStageMask,
1797 dstStageMask,
1798 byRegion,
1799 GrVkCommandBuffer::kImageMemory_BarrierType,
1800 barrier);
1801}
1802
Greg Danielbae71212019-03-01 15:24:35 -05001803void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
1804 SkSurface::FlushFlags flags, bool insertedSemaphore) {
Greg Daniel51316782017-08-02 15:10:09 +00001805 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1806 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001807 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1808 GrVkImage* image;
1809 SkASSERT(proxy->isInstantiated());
1810 if (GrTexture* tex = proxy->peekTexture()) {
1811 image = static_cast<GrVkTexture*>(tex);
1812 } else {
1813 GrRenderTarget* rt = proxy->peekRenderTarget();
1814 SkASSERT(rt);
1815 image = static_cast<GrVkRenderTarget*>(rt);
1816 }
1817 image->prepareForPresent(this);
1818 }
1819 if (flags & SkSurface::kSyncCpu_FlushFlag) {
1820 this->submitCommandBuffer(kForce_SyncQueue);
1821 } else {
1822 this->submitCommandBuffer(kSkip_SyncQueue);
1823 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001824}
1825
Greg Daniel25af6712018-04-25 10:44:38 -04001826static int get_surface_sample_cnt(GrSurface* surf) {
1827 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1828 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001829 }
Greg Daniel25af6712018-04-25 10:44:38 -04001830 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001831}
1832
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001833void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1834 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001835 GrVkImage* dstImage,
1836 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001837 const SkIRect& srcRect,
1838 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001839#ifdef SK_DEBUG
1840 int dstSampleCnt = get_surface_sample_cnt(dst);
1841 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001842 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1843 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1844 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1845 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001846
1847#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001848
Greg Daniel164a9f02016-02-22 09:56:40 -05001849 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1850 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001851 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001852 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1853 VK_ACCESS_TRANSFER_WRITE_BIT,
1854 VK_PIPELINE_STAGE_TRANSFER_BIT,
1855 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001856
egdaniel17b89252016-04-05 07:23:38 -07001857 srcImage->setImageLayout(this,
1858 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001859 VK_ACCESS_TRANSFER_READ_BIT,
1860 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001861 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001862
1863 // Flip rect if necessary
1864 SkIRect srcVkRect = srcRect;
1865 int32_t dstY = dstPoint.fY;
1866
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001867 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1868 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001869 srcVkRect.fTop = src->height() - srcRect.fBottom;
1870 srcVkRect.fBottom = src->height() - srcRect.fTop;
1871 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1872 }
1873
1874 VkImageCopy copyRegion;
1875 memset(&copyRegion, 0, sizeof(VkImageCopy));
1876 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1877 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1878 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1879 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001880 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001881
1882 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001883 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001884 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001885 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001886 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1887 1,
1888 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001889
1890 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1891 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001892 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001893}
1894
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001895void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1896 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001897 GrVkImage* dstImage,
1898 GrVkImage* srcImage,
1899 const SkIRect& srcRect,
1900 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001901#ifdef SK_DEBUG
1902 int dstSampleCnt = get_surface_sample_cnt(dst);
1903 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001904 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1905 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04001906 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04001907 dstHasYcbcr, src->config(), srcSampleCnt,
1908 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07001909
Greg Daniel25af6712018-04-25 10:44:38 -04001910#endif
egdaniel17b89252016-04-05 07:23:38 -07001911 dstImage->setImageLayout(this,
1912 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001913 VK_ACCESS_TRANSFER_WRITE_BIT,
1914 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001915 false);
1916
egdaniel17b89252016-04-05 07:23:38 -07001917 srcImage->setImageLayout(this,
1918 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001919 VK_ACCESS_TRANSFER_READ_BIT,
1920 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001921 false);
1922
1923 // Flip rect if necessary
1924 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001925 srcVkRect.fLeft = srcRect.fLeft;
1926 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001927 SkIRect dstRect;
1928 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001929 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001930
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001931 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001932 srcVkRect.fTop = src->height() - srcRect.fBottom;
1933 srcVkRect.fBottom = src->height() - srcRect.fTop;
1934 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001935 srcVkRect.fTop = srcRect.fTop;
1936 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001937 }
1938
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001939 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001940 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1941 } else {
1942 dstRect.fTop = dstPoint.fY;
1943 }
1944 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1945
1946 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1947 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001948 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001949 using std::swap;
1950 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001951 }
1952
1953 VkImageBlit blitRegion;
1954 memset(&blitRegion, 0, sizeof(VkImageBlit));
1955 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1956 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001957 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001958 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1959 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001960 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001961
1962 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001963 *srcImage,
1964 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001965 1,
1966 &blitRegion,
1967 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001968
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001969 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001970 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001971}
1972
Brian Salomon1fabd512018-02-09 09:54:25 -05001973void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1974 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1975 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001976 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001977 SkIRect srcRect = origSrcRect;
1978 SkIPoint dstPoint = origDstPoint;
1979 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1980 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1981 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1982 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1983 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1984 }
1985 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001986 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1987 srcRect.width(), srcRect.height());
1988 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001989}
1990
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001991bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1992 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001993 const SkIRect& srcRect, const SkIPoint& dstPoint,
1994 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001995#ifdef SK_DEBUG
1996 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
1997 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
1998 }
1999 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2000 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2001 }
2002#endif
2003
Greg Daniel25af6712018-04-25 10:44:38 -04002004 GrPixelConfig dstConfig = dst->config();
2005 GrPixelConfig srcConfig = src->config();
2006
2007 int dstSampleCnt = get_surface_sample_cnt(dst);
2008 int srcSampleCnt = get_surface_sample_cnt(src);
2009
egdaniel17b89252016-04-05 07:23:38 -07002010 GrVkImage* dstImage;
2011 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002012 GrRenderTarget* dstRT = dst->asRenderTarget();
2013 if (dstRT) {
2014 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002015 if (vkRT->wrapsSecondaryCommandBuffer()) {
2016 return false;
2017 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002018 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2019 } else {
2020 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002021 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002022 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002023 GrRenderTarget* srcRT = src->asRenderTarget();
2024 if (srcRT) {
2025 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2026 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002027 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002028 SkASSERT(src->asTexture());
2029 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002030 }
2031
Greg Daniela51e93c2019-03-25 12:30:45 -04002032 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2033 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2034
2035 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2036 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2037 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2038 return true;
2039 }
2040
2041 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2042 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2043 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2044 dstPoint, canDiscardOutsideDstRect));
2045 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2046 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2047 return true;
2048 }
2049
2050 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2051 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002052 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2053 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002054 return true;
2055 }
2056
Greg Daniel25af6712018-04-25 10:44:38 -04002057 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002058 dstHasYcbcr, srcConfig, srcSampleCnt,
2059 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002060 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2061 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002062 return true;
2063 }
2064
Greg Daniel164a9f02016-02-22 09:56:40 -05002065 return false;
2066}
2067
Brian Salomona6948702018-06-01 15:33:20 -04002068bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2069 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002070 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002071 return false;
2072 }
2073
egdaniel66933552016-08-24 07:22:19 -07002074 GrVkImage* image = nullptr;
2075 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2076 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002077 // Reading from render targets that wrap a secondary command buffer is not allowed since
2078 // it would require us to know the VkImage, which we don't have, as well as need us to
2079 // stop and start the VkRenderPass which we don't have access to.
2080 if (rt->wrapsSecondaryCommandBuffer()) {
2081 return false;
2082 }
egdaniel66933552016-08-24 07:22:19 -07002083 // resolve the render target if necessary
2084 switch (rt->getResolveType()) {
2085 case GrVkRenderTarget::kCantResolve_ResolveType:
2086 return false;
2087 case GrVkRenderTarget::kAutoResolves_ResolveType:
2088 break;
2089 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002090 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002091 break;
2092 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002093 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002094 }
2095 image = rt;
2096 } else {
2097 image = static_cast<GrVkTexture*>(surface->asTexture());
2098 }
2099
2100 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002101 return false;
2102 }
2103
Greg Daniel475eb702018-09-28 14:16:50 -04002104 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2105 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2106 // image and then do the read pixels from that.
2107 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002108 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2109 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002110
2111 // Make a new surface that is RGBA to copy the RGB surface into.
2112 GrSurfaceDesc surfDesc;
2113 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2114 surfDesc.fWidth = width;
2115 surfDesc.fHeight = height;
2116 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2117 surfDesc.fSampleCnt = 1;
2118
2119 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2120 VK_IMAGE_USAGE_SAMPLED_BIT |
2121 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2122 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2123
2124 GrVkImage::ImageDesc imageDesc;
2125 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2126 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2127 imageDesc.fWidth = width;
2128 imageDesc.fHeight = height;
2129 imageDesc.fLevels = 1;
2130 imageDesc.fSamples = 1;
2131 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2132 imageDesc.fUsageFlags = usageFlags;
2133 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2134
2135 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2136 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2137 if (!copySurface) {
2138 return false;
2139 }
2140
2141 int srcSampleCount = 0;
2142 if (rt) {
2143 srcSampleCount = rt->numColorSamples();
2144 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002145 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002146 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002147 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2148 surface->config(), srcSampleCount, kOrigin,
2149 srcHasYcbcr) &&
2150 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2151 surface->config(), SkToBool(surface->asTexture()),
2152 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002153 return false;
2154 }
2155 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2156 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2157 srcRect, SkIPoint::Make(0,0))) {
2158 return false;
2159 }
2160 top = 0;
2161 left = 0;
2162 dstColorType = GrColorType::kRGBA_8888;
2163 image = copySurface.get();
2164 }
2165
Greg Daniel164a9f02016-02-22 09:56:40 -05002166 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002167 image->setImageLayout(this,
2168 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2169 VK_ACCESS_TRANSFER_READ_BIT,
2170 VK_PIPELINE_STAGE_TRANSFER_BIT,
2171 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002172
Brian Salomonc320b152018-02-20 14:05:36 -05002173 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002174 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002175
Greg Daniel164a9f02016-02-22 09:56:40 -05002176 VkBufferImageCopy region;
2177 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002178
2179 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2180 if (copyFromOrigin) {
2181 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002182 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002183 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002184 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002185 region.imageOffset = offset;
2186 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2187 }
2188
2189 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002190 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002191 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002192 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002193 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002194 kStream_GrAccessPattern)
2195 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002196
2197 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002198 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002199 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002200 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2201 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002202
2203 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002204 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002205 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002206 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002207 1,
2208 &region);
2209
2210 // make sure the copy to buffer has finished
2211 transferBuffer->addMemoryBarrier(this,
2212 VK_ACCESS_TRANSFER_WRITE_BIT,
2213 VK_ACCESS_HOST_READ_BIT,
2214 VK_PIPELINE_STAGE_TRANSFER_BIT,
2215 VK_PIPELINE_STAGE_HOST_BIT,
2216 false);
2217
2218 // We need to submit the current command buffer to the Queue and make sure it finishes before
2219 // we can copy the data out of the buffer.
2220 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002221 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002222 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002223 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002224
egdaniel6fa0a912016-09-12 11:51:29 -07002225 if (copyFromOrigin) {
2226 uint32_t skipRows = region.imageExtent.height - height;
2227 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2228 }
2229
Brian Salomona6948702018-06-01 15:33:20 -04002230 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002231
2232 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002233 return true;
2234}
egdaniel066df7c2016-06-08 14:02:27 -07002235
egdaniel27bb2842016-07-07 11:58:35 -07002236// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2237// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2238// the the entire attachment. Similar requirements for the y and height components.
2239void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2240 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2241 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002242 if ((0 != granularity.width && 1 != granularity.width)) {
2243 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2244 int rightAdj = srcBounds.fRight % granularity.width;
2245 if (rightAdj != 0) {
2246 rightAdj = granularity.width - rightAdj;
2247 }
2248 dstBounds->fRight = srcBounds.fRight + rightAdj;
2249 if (dstBounds->fRight > maxWidth) {
2250 dstBounds->fRight = maxWidth;
2251 dstBounds->fLeft = 0;
2252 } else {
2253 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2254 }
egdaniel27bb2842016-07-07 11:58:35 -07002255 } else {
egdanield5797b32016-09-20 12:57:45 -07002256 dstBounds->fLeft = srcBounds.fLeft;
2257 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002258 }
2259
2260 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002261 if ((0 != granularity.height && 1 != granularity.height)) {
2262 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2263 int bottomAdj = srcBounds.fBottom % granularity.height;
2264 if (bottomAdj != 0) {
2265 bottomAdj = granularity.height - bottomAdj;
2266 }
2267 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2268 if (dstBounds->fBottom > maxHeight) {
2269 dstBounds->fBottom = maxHeight;
2270 dstBounds->fTop = 0;
2271 } else {
2272 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2273 }
egdaniel27bb2842016-07-07 11:58:35 -07002274 } else {
egdanield5797b32016-09-20 12:57:45 -07002275 dstBounds->fTop = srcBounds.fTop;
2276 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002277 }
2278}
2279
Greg Daniel22bc8652017-03-22 15:45:43 -04002280void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002281 const GrVkRenderPass* renderPass,
2282 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002283 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002284 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002285 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002286 const SkIRect* pBounds = &bounds;
2287 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002288 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002289 flippedBounds = bounds;
2290 flippedBounds.fTop = target->height() - bounds.fBottom;
2291 flippedBounds.fBottom = target->height() - bounds.fTop;
2292 pBounds = &flippedBounds;
2293 }
2294
egdaniel27bb2842016-07-07 11:58:35 -07002295 // The bounds we use for the render pass should be of the granularity supported
2296 // by the device.
2297 const VkExtent2D& granularity = renderPass->granularity();
2298 SkIRect adjustedBounds;
2299 if ((0 != granularity.width && 1 != granularity.width) ||
2300 (0 != granularity.height && 1 != granularity.height)) {
2301 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2302 target->width(), target->height());
2303 pBounds = &adjustedBounds;
2304 }
2305
Robert Phillips95214472017-08-08 18:00:03 -04002306#ifdef SK_DEBUG
2307 uint32_t index;
2308 bool result = renderPass->colorAttachmentIndex(&index);
2309 SkASSERT(result && 0 == index);
2310 result = renderPass->stencilAttachmentIndex(&index);
2311 if (result) {
2312 SkASSERT(1 == index);
2313 }
2314#endif
2315 VkClearValue clears[2];
2316 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002317 clears[1].depthStencil.depth = 0.0f;
2318 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002319
2320 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002321 for (int i = 0; i < buffers.count(); ++i) {
2322 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2323 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002324 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002325
Brian Salomon1fabd512018-02-09 09:54:25 -05002326 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002327}
egdaniel9cb63402016-06-23 08:37:05 -07002328
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002329void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2330 if (buffer->asRTCommandBuffer()) {
2331 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2332
2333 fCachedRTCommandBuffer->submit();
2334 fCachedRTCommandBuffer->reset();
2335 } else {
2336 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2337
2338 fCachedTexCommandBuffer->submit();
2339 fCachedTexCommandBuffer->reset();
2340 }
2341}
2342
Greg Daniel6be35232017-03-01 17:01:09 -05002343GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002344 VkFenceCreateInfo createInfo;
2345 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2346 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2347 createInfo.pNext = nullptr;
2348 createInfo.flags = 0;
2349 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002350
2351 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2352 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2353
2354 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002355 return (GrFence)fence;
2356}
2357
Greg Daniel6be35232017-03-01 17:01:09 -05002358bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2359 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2360
2361 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002362 return (VK_SUCCESS == result);
2363}
2364
2365void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002366 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2367}
2368
Greg Daniela5cb7812017-06-16 09:45:32 -04002369sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2370 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002371}
2372
Greg Daniel48661b82018-01-22 16:11:35 -05002373sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2374 GrResourceProvider::SemaphoreWrapType wrapType,
2375 GrWrapOwnership ownership) {
2376 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002377}
2378
Greg Daniel858e12c2018-12-06 11:11:37 -05002379void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002380 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2381
Greg Daniel48661b82018-01-22 16:11:35 -05002382 GrVkSemaphore::Resource* resource = vkSem->getResource();
2383 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002384 resource->ref();
2385 fSemaphoresToSignal.push_back(resource);
2386 }
Greg Daniel6be35232017-03-01 17:01:09 -05002387}
2388
Greg Daniel48661b82018-01-22 16:11:35 -05002389void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002390 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2391
Greg Daniel48661b82018-01-22 16:11:35 -05002392 GrVkSemaphore::Resource* resource = vkSem->getResource();
2393 if (resource->shouldWait()) {
2394 resource->ref();
2395 fSemaphoresToWaitOn.push_back(resource);
2396 }
jvanverth84741b32016-09-30 08:39:02 -07002397}
Brian Osman13dddce2017-05-09 13:19:50 -04002398
2399sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2400 SkASSERT(texture);
2401 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2402 vkTexture->setImageLayout(this,
2403 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2404 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002405 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002406 false);
2407 this->submitCommandBuffer(kSkip_SyncQueue);
2408
2409 // The image layout change serves as a barrier, so no semaphore is needed
2410 return nullptr;
2411}
Greg Danielf5d87582017-12-18 14:48:15 -05002412
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002413void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2414 fDrawables.emplace_back(std::move(drawable));
2415}
2416
Greg Daniel7a82edf2018-12-04 10:54:34 -05002417uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2418 const GrBackendFormat& format) {
2419 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2420 SkASSERT(ycbcrInfo);
2421 if (!ycbcrInfo->isValid()) {
2422 return 0;
2423 }
2424
2425 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2426 samplerState, *ycbcrInfo);
2427
2428 return sampler->uniqueID();
2429}
2430
Greg Daniela870b462019-01-08 15:49:46 -05002431void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002432 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002433 this->resourceProvider().storePipelineCacheData();
2434 }
2435}