blob: 934d858f0736bd91267dbb72ca370ea0e0808334 [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);
321 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500322 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400323 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500324
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400325 // We must delete and drawables that have been waitint till submit for us to destroy.
326 fDrawables.reset();
327
Greg Daniel6be35232017-03-01 17:01:09 -0500328 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
329 fSemaphoresToWaitOn[i]->unref(this);
330 }
331 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400332 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
333 fSemaphoresToSignal[i]->unref(this);
334 }
335 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500336
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500337 // Release old command pool and create a new one
338 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500339 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500340 fCmdPool = fResourceProvider.findOrCreateCommandPool();
341 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500342 fCurrentCmdBuffer->begin(this);
343}
344
345///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500346sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
347 GrAccessPattern accessPattern, const void* data) {
348 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700349 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500350 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700351 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
352 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500353 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700354 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500355 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700356 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
357 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500358 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700359 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500360 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400361 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
362 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500363 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700364 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500365 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400366 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
367 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500368 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700369 break;
cdalton397536c2016-03-25 12:15:03 -0700370 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400371 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700372 return nullptr;
373 }
cdalton1bf3e712016-04-19 10:00:02 -0700374 if (data && buff) {
375 buff->updateData(data, size);
376 }
377 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500378}
379
Brian Salomona9b04b92018-06-01 15:04:28 -0400380bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
381 GrColorType srcColorType, const GrMipLevel texels[],
382 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500383 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
384 if (!vkTex) {
385 return false;
386 }
387
jvanverth900bd4a2016-04-29 13:53:12 -0700388 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400389 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800390 return false;
391 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800392
Jim Van Verth1676cb92019-01-15 13:24:45 -0500393 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500394 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400395 bool linearTiling = vkTex->isLinearTiled();
396 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400397 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400398 SkDebugf("Can't upload mipmap data to linear tiled texture");
399 return false;
400 }
401 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
402 // Need to change the layout to general in order to perform a host write
403 vkTex->setImageLayout(this,
404 VK_IMAGE_LAYOUT_GENERAL,
405 VK_ACCESS_HOST_WRITE_BIT,
406 VK_PIPELINE_STAGE_HOST_BIT,
407 false);
408 this->submitCommandBuffer(kForce_SyncQueue);
409 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400410 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400411 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500412 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400413 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400414 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
415 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500416 }
egdaniel4583ec52016-06-27 12:57:00 -0700417
jvanverth900bd4a2016-04-29 13:53:12 -0700418 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500419}
420
Brian Salomonc320b152018-02-20 14:05:36 -0500421bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomondbf70722019-02-07 11:31:24 -0500422 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400423 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500424 // Can't transfer compressed data
425 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
426
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400427 // Vulkan only supports 4-byte aligned offsets
428 if (SkToBool(bufferOffset & 0x2)) {
429 return false;
430 }
431 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
432 if (!vkTex) {
433 return false;
434 }
435 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
436 if (!vkBuffer) {
437 return false;
438 }
439
Greg Daniel660cc992017-06-26 14:55:05 -0400440 SkDEBUGCODE(
441 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
442 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
443 SkASSERT(bounds.contains(subRect));
444 )
Brian Salomonc320b152018-02-20 14:05:36 -0500445 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400446 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500447 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400448 }
449
450 // Set up copy region
451 VkBufferImageCopy region;
452 memset(&region, 0, sizeof(VkBufferImageCopy));
453 region.bufferOffset = bufferOffset;
454 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
455 region.bufferImageHeight = 0;
456 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
457 region.imageOffset = { left, top, 0 };
458 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
459
460 // Change layout of our target so it can be copied to
461 vkTex->setImageLayout(this,
462 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
463 VK_ACCESS_TRANSFER_WRITE_BIT,
464 VK_PIPELINE_STAGE_TRANSFER_BIT,
465 false);
466
467 // Copy the buffer to the image
468 fCurrentCmdBuffer->copyBufferToImage(this,
469 vkBuffer,
470 vkTex,
471 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
472 1,
473 &region);
474
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400475 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400476 return true;
477}
478
Brian Salomon1fabd512018-02-09 09:54:25 -0500479void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
480 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700481 SkASSERT(dst);
482 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
483
egdaniel4bcd62e2016-08-31 07:37:31 -0700484 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500485 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
486 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
487 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
488 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
489 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700490
Greg Danielbc26c392017-04-18 13:32:10 -0400491 GrVkImage* dstImage;
492 GrRenderTarget* dstRT = dst->asRenderTarget();
493 if (dstRT) {
494 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400495 dstImage = vkRT;
496 } else {
497 SkASSERT(dst->asTexture());
498 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
499 }
500 dstImage->setImageLayout(this,
501 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
502 VK_ACCESS_TRANSFER_WRITE_BIT,
503 VK_PIPELINE_STAGE_TRANSFER_BIT,
504 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700505
506 src->msaaImage()->setImageLayout(this,
507 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
508 VK_ACCESS_TRANSFER_READ_BIT,
509 VK_PIPELINE_STAGE_TRANSFER_BIT,
510 false);
511
Greg Danielbc26c392017-04-18 13:32:10 -0400512 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700513}
514
Brian Salomon1fabd512018-02-09 09:54:25 -0500515void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700516 if (target->needsResolve()) {
517 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700518 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
519 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500520
egdaniel4bcd62e2016-08-31 07:37:31 -0700521 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700522
Brian Salomon1fabd512018-02-09 09:54:25 -0500523 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700524
525 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500526
527 if (requiresSubmit) {
528 this->submitCommandBuffer(kSkip_SyncQueue);
529 }
egdaniel52ad2512016-08-04 12:50:01 -0700530 }
531}
532
Brian Salomona9b04b92018-06-01 15:04:28 -0400533bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
534 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500535 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700536 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500537
Jim Van Verth1676cb92019-01-15 13:24:45 -0500538 // If we're uploading compressed data then we should be using uploadCompressedTexData
539 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
540 GrSRGBEncoded::kNo)));
541
Greg Daniel660cc992017-06-26 14:55:05 -0400542 SkDEBUGCODE(
543 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
544 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
545 SkASSERT(bounds.contains(subRect));
546 )
Brian Salomonc320b152018-02-20 14:05:36 -0500547 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500548 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400549 if (!rowBytes) {
550 rowBytes = trimRowBytes;
551 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500552
jvanverth900bd4a2016-04-29 13:53:12 -0700553 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
554 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
555 const VkImageSubresource subres = {
556 VK_IMAGE_ASPECT_COLOR_BIT,
557 0, // mipLevel
558 0, // arraySlice
559 };
560 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500561
jvanverth900bd4a2016-04-29 13:53:12 -0700562 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500563
jvanverth900bd4a2016-04-29 13:53:12 -0700564 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700565 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700566 &subres,
567 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500568
jvanverth1e305ba2016-06-01 09:39:15 -0700569 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400570 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700571 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400572 SkASSERT(size + offset <= alloc.fSize);
573 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
574 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700575 return false;
576 }
Greg Daniel81df0412018-05-31 13:13:33 -0400577 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700578
Brian Salomona9b04b92018-06-01 15:04:28 -0400579 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
580 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700581
Greg Daniele35a99e2018-03-02 11:44:22 -0500582 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400583 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700584
585 return true;
586}
587
Brian Salomona9b04b92018-06-01 15:04:28 -0400588bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
589 GrColorType dataColorType, const GrMipLevel texels[],
590 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700591 SkASSERT(!tex->isLinearTiled());
592 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400593 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700594 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
595
Greg Danieldd20e912017-04-07 14:42:23 -0400596 // We assume that if the texture has mip levels, we either upload to all the levels or just the
597 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400598 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400599
Jim Van Verth1676cb92019-01-15 13:24:45 -0500600 // If we're uploading compressed data then we should be using uploadCompressedTexData
601 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
602 GrSRGBEncoded::kNo)));
603
jvanverth900bd4a2016-04-29 13:53:12 -0700604 if (width == 0 || height == 0) {
605 return false;
606 }
607
Greg Daniel475eb702018-09-28 14:16:50 -0400608 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
609 return false;
610 }
611
612 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
613 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500614 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
615 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400616 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
617 // blit or draw.
618 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
619 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
620 return false;
621 }
622 mipLevelCount = 1;
623 }
624
Brian Salomond1eaf492017-05-18 10:02:08 -0400625 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500626 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700627
628 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700629 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
630 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400631 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
632
Greg Daniel475eb702018-09-28 14:16:50 -0400633 texelsShallowCopy.reset(mipLevelCount);
634 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700635
Robert Phillips590533f2017-07-11 14:22:35 -0400636 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700637 individualMipOffsets.push_back(0);
638 size_t combinedBufferSize = width * bpp * height;
639 int currentWidth = width;
640 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400641 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400642 combinedBufferSize = 0;
643 }
644
Greg Daniel468fd632017-03-22 17:03:45 -0400645 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
646 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
647 SkASSERT((bpp & (bpp - 1)) == 0);
648 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400649 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700650 currentWidth = SkTMax(1, currentWidth/2);
651 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400652
Greg Daniel55afd6d2017-09-29 09:32:44 -0400653 if (texelsShallowCopy[currentMipLevel].fPixels) {
654 const size_t trimmedSize = currentWidth * bpp * currentHeight;
655 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
656 if (alignmentDiff != 0) {
657 combinedBufferSize += alignmentMask - alignmentDiff + 1;
658 }
659 individualMipOffsets.push_back(combinedBufferSize);
660 combinedBufferSize += trimmedSize;
661 } else {
662 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400663 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400664 }
665 if (0 == combinedBufferSize) {
666 // We don't actually have any data to upload so just return success
667 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700668 }
669
670 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500671 sk_sp<GrVkTransferBuffer> transferBuffer =
672 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400673 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700674 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400675 }
jvanverth900bd4a2016-04-29 13:53:12 -0700676
Greg Daniel475eb702018-09-28 14:16:50 -0400677 int uploadLeft = left;
678 int uploadTop = top;
679 GrVkTexture* uploadTexture = tex;
680 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
681 // R8G8B8A8_UNORM image and then copy it.
682 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500683 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400684 GrSurfaceDesc surfDesc;
685 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
686 surfDesc.fWidth = width;
687 surfDesc.fHeight = height;
688 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
689 surfDesc.fSampleCnt = 1;
690
691 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
692 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
693 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
694
695 GrVkImage::ImageDesc imageDesc;
696 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
697 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
698 imageDesc.fWidth = width;
699 imageDesc.fHeight = height;
700 imageDesc.fLevels = 1;
701 imageDesc.fSamples = 1;
702 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
703 imageDesc.fUsageFlags = usageFlags;
704 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
705
706 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
707 GrMipMapsStatus::kNotAllocated);
708 if (!copyTexture) {
709 return false;
710 }
711 uploadTexture = copyTexture.get();
712 uploadLeft = 0;
713 uploadTop = 0;
714 }
715
jvanverth900bd4a2016-04-29 13:53:12 -0700716 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400717 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700718
jvanverthc578b0632016-05-02 10:58:12 -0700719 currentWidth = width;
720 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400721 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400722 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400723 if (texelsShallowCopy[currentMipLevel].fPixels) {
724 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
725 const size_t trimRowBytes = currentWidth * bpp;
726 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
727 ? texelsShallowCopy[currentMipLevel].fRowBytes
728 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700729
Greg Daniel55afd6d2017-09-29 09:32:44 -0400730 // copy data into the buffer, skipping the trailing bytes
731 char* dst = buffer + individualMipOffsets[currentMipLevel];
732 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400733 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400734
735 VkBufferImageCopy& region = regions.push_back();
736 memset(&region, 0, sizeof(VkBufferImageCopy));
737 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
738 region.bufferRowLength = currentWidth;
739 region.bufferImageHeight = currentHeight;
740 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400741 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400742 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700743 }
jvanverthc578b0632016-05-02 10:58:12 -0700744 currentWidth = SkTMax(1, currentWidth/2);
745 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400746 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700747 }
748
jvanverth9d54afc2016-09-20 09:20:03 -0700749 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700750 transferBuffer->unmap();
751
jvanverth900bd4a2016-04-29 13:53:12 -0700752 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400753 uploadTexture->setImageLayout(this,
754 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
755 VK_ACCESS_TRANSFER_WRITE_BIT,
756 VK_PIPELINE_STAGE_TRANSFER_BIT,
757 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700758
759 // Copy the buffer to the image
760 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500761 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400762 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700763 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
764 regions.count(),
765 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400766
767 // If we copied the data into a temporary image first, copy that image into our main texture
768 // now.
769 if (copyTexture.get()) {
770 SkASSERT(dataColorType == GrColorType::kRGB_888x);
771 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
772 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
773 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
774 false));
775 }
Robert Phillips590533f2017-07-11 14:22:35 -0400776 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400777 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400778 }
jvanverth900bd4a2016-04-29 13:53:12 -0700779
Greg Daniel164a9f02016-02-22 09:56:40 -0500780 return true;
781}
782
Jim Van Verth1676cb92019-01-15 13:24:45 -0500783// It's probably possible to roll this into uploadTexDataOptimal,
784// but for now it's easier to maintain as a separate entity.
785bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
786 GrColorType dataColorType, const GrMipLevel texels[],
787 int mipLevelCount) {
788 SkASSERT(!tex->isLinearTiled());
789 // For now the assumption is that our rect is the entire texture.
790 // Compressed textures are read-only so this should be a reasonable assumption.
791 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
792
793 // We assume that if the texture has mip levels, we either upload to all the levels or just the
794 // first.
795 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
796
797 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
798 GrSRGBEncoded::kNo)));
799
800 if (width == 0 || height == 0) {
801 return false;
802 }
803
804 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
805 return false;
806 }
807
808 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
809
810 SkTArray<size_t> individualMipOffsets(mipLevelCount);
811 individualMipOffsets.push_back(0);
812 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
813 int currentWidth = width;
814 int currentHeight = height;
815 if (!texels[0].fPixels) {
816 return false;
817 }
818
819 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
820 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
821 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
822 currentWidth = SkTMax(1, currentWidth / 2);
823 currentHeight = SkTMax(1, currentHeight / 2);
824
825 if (texels[currentMipLevel].fPixels) {
826 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
827 currentHeight);
828 individualMipOffsets.push_back(combinedBufferSize);
829 combinedBufferSize += dataSize;
830 } else {
831 return false;
832 }
833 }
834 if (0 == combinedBufferSize) {
835 // We don't have any data to upload so fail (compressed textures are read-only).
836 return false;
837 }
838
839 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500840 sk_sp<GrVkTransferBuffer> transferBuffer =
841 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500842 if (!transferBuffer) {
843 return false;
844 }
845
846 int uploadLeft = left;
847 int uploadTop = top;
848 GrVkTexture* uploadTexture = tex;
849
850 char* buffer = (char*)transferBuffer->map();
851 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
852
853 currentWidth = width;
854 currentHeight = height;
855 int layerHeight = uploadTexture->height();
856 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
857 if (texels[currentMipLevel].fPixels) {
858 // Again, we're assuming that our rect is the entire texture
859 SkASSERT(currentHeight == layerHeight);
860 SkASSERT(0 == uploadLeft && 0 == uploadTop);
861
862 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
863 currentHeight);
864
865 // copy data into the buffer, skipping the trailing bytes
866 char* dst = buffer + individualMipOffsets[currentMipLevel];
867 const char* src = (const char*)texels[currentMipLevel].fPixels;
868 memcpy(dst, src, dataSize);
869
870 VkBufferImageCopy& region = regions.push_back();
871 memset(&region, 0, sizeof(VkBufferImageCopy));
872 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
873 region.bufferRowLength = currentWidth;
874 region.bufferImageHeight = currentHeight;
875 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
876 region.imageOffset = { uploadLeft, uploadTop, 0 };
877 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
878 }
879 currentWidth = SkTMax(1, currentWidth / 2);
880 currentHeight = SkTMax(1, currentHeight / 2);
881 layerHeight = currentHeight;
882 }
883
884 // no need to flush non-coherent memory, unmap will do that for us
885 transferBuffer->unmap();
886
887 // Change layout of our target so it can be copied to
888 uploadTexture->setImageLayout(this,
889 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
890 VK_ACCESS_TRANSFER_WRITE_BIT,
891 VK_PIPELINE_STAGE_TRANSFER_BIT,
892 false);
893
894 // Copy the buffer to the image
895 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500896 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500897 uploadTexture,
898 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
899 regions.count(),
900 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500901
902 if (1 == mipLevelCount) {
903 tex->texturePriv().markMipMapsDirty();
904 }
905
906 return true;
907}
908
Greg Daniel164a9f02016-02-22 09:56:40 -0500909////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400910sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500911 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500912 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
913
914 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500915 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700916
Greg Daniel164a9f02016-02-22 09:56:40 -0500917 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
918 if (renderTarget) {
919 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
920 }
921
922 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
923 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
924 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700925 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500926 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
927 // texture.
928 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
929
Greg Daniel164a9f02016-02-22 09:56:40 -0500930 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700931 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500932 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400933 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500934 GrVkImage::ImageDesc imageDesc;
935 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
936 imageDesc.fFormat = pixelFormat;
937 imageDesc.fWidth = desc.fWidth;
938 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400939 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500940 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400941 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500942 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400943 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500944
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400945 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
946 if (mipLevels > 1) {
947 mipMapsStatus = GrMipMapsStatus::kValid;
948 for (int i = 0; i < mipLevels; ++i) {
949 if (!texels[i].fPixels) {
950 mipMapsStatus = GrMipMapsStatus::kDirty;
951 break;
952 }
Greg Daniel834f1202017-10-09 15:06:20 -0400953 }
954 }
955
Robert Phillips67d52cf2017-06-05 13:38:13 -0400956 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500957 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400958 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
959 imageDesc,
960 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500961 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400962 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500963 }
964
965 if (!tex) {
966 return nullptr;
967 }
968
Jim Van Verth1676cb92019-01-15 13:24:45 -0500969 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -0500970 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400971 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500972 bool success;
973 if (isCompressed) {
974 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
975 colorType, texels, mipLevelCount);
976 } else {
977 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
978 colorType, texels, mipLevelCount);
979 }
980 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500981 tex->unref();
982 return nullptr;
983 }
984 }
985
Jim Van Verth1676cb92019-01-15 13:24:45 -0500986 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400987 VkClearColorValue zeroClearColor;
988 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
989 VkImageSubresourceRange range;
990 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
991 range.baseArrayLayer = 0;
992 range.baseMipLevel = 0;
993 range.layerCount = 1;
994 range.levelCount = 1;
995 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
996 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400997 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400998 }
Ben Wagnerff134f22018-04-24 16:29:16 -0400999 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001000}
1001
1002////////////////////////////////////////////////////////////////////////////////
1003
Greg Daniel6888c0d2017-08-25 11:55:50 -04001004void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1005 VkDeviceSize dstOffset, VkDeviceSize size) {
1006 VkBufferCopy copyRegion;
1007 copyRegion.srcOffset = srcOffset;
1008 copyRegion.dstOffset = dstOffset;
1009 copyRegion.size = size;
1010 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1011}
1012
jvanverthdb379092016-07-07 11:18:46 -07001013bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1014 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001015 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001016 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001017
1018 return true;
1019}
1020
1021////////////////////////////////////////////////////////////////////////////////
1022
Greg Daniel7e000222018-12-03 10:08:21 -05001023static bool check_image_info(const GrVkCaps& caps,
1024 const GrVkImageInfo& info,
1025 GrPixelConfig config) {
1026 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -04001027 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001028 }
1029
Greg Daniel7e000222018-12-03 10:08:21 -05001030 if (info.fYcbcrConversionInfo.isValid()) {
1031 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1032 return false;
1033 }
jvanverthfd359ca2016-03-18 11:57:24 -07001034 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001035
Greg Daniel52e16d92018-04-10 09:34:07 -04001036 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001037 return true;
1038}
1039
1040sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001041 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1042 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001043 GrVkImageInfo imageInfo;
1044 if (!backendTex.getVkImageInfo(&imageInfo)) {
1045 return nullptr;
1046 }
1047
1048 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -04001049 return nullptr;
1050 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001051
Greg Daniel164a9f02016-02-22 09:56:40 -05001052 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001053 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001054 surfDesc.fWidth = backendTex.width();
1055 surfDesc.fHeight = backendTex.height();
1056 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001057 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001058
Greg Daniel52e16d92018-04-10 09:34:07 -04001059 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1060 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001061 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1062 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001063}
1064
1065sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001066 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001067 GrWrapOwnership ownership,
1068 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001069 GrVkImageInfo imageInfo;
1070 if (!backendTex.getVkImageInfo(&imageInfo)) {
1071 return nullptr;
1072 }
1073
1074 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -04001075 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001076 }
Brian Salomond17f6582017-07-19 18:28:58 -04001077
1078 GrSurfaceDesc surfDesc;
1079 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1080 surfDesc.fWidth = backendTex.width();
1081 surfDesc.fHeight = backendTex.height();
1082 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001083 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001084
Greg Daniel52e16d92018-04-10 09:34:07 -04001085 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1086 SkASSERT(layout);
1087
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001088 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1089 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001090}
1091
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001092sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001093 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1094 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1095 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1096 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001097 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001098 return nullptr;
1099 }
halcanary9d524f22016-03-29 09:03:52 -07001100
Greg Daniel323fbcf2018-04-10 13:46:30 -04001101 GrVkImageInfo info;
1102 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001103 return nullptr;
1104 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001105
1106 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -07001107 return nullptr;
1108 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001109
Greg Daniel164a9f02016-02-22 09:56:40 -05001110 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001111 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001112 desc.fWidth = backendRT.width();
1113 desc.fHeight = backendRT.height();
1114 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001115 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001116
Greg Daniel323fbcf2018-04-10 13:46:30 -04001117 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001118
Greg Daniel323fbcf2018-04-10 13:46:30 -04001119 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001120 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001121
1122 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1123 SkASSERT(!backendRT.stencilBits());
1124 if (tgt) {
1125 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001126 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001127
Ben Wagnerff134f22018-04-24 16:29:16 -04001128 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001129}
1130
Greg Daniel7ef28f32017-04-20 16:41:55 +00001131sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001132 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001133
Greg Daniel52e16d92018-04-10 09:34:07 -04001134 GrVkImageInfo imageInfo;
1135 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001136 return nullptr;
1137 }
Greg Daniel52e16d92018-04-10 09:34:07 -04001138 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -04001139 return nullptr;
1140 }
1141
1142 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001143 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001144 desc.fWidth = tex.width();
1145 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001146 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001147 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1148 if (!desc.fSampleCnt) {
1149 return nullptr;
1150 }
Brian Osman33910292017-04-18 14:38:53 -04001151
Greg Daniel52e16d92018-04-10 09:34:07 -04001152 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1153 SkASSERT(layout);
1154
Ben Wagnerff134f22018-04-24 16:29:16 -04001155 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001156}
1157
Greg Danielb46add82019-01-02 14:51:29 -05001158sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1159 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1160 int maxSize = this->caps()->maxTextureSize();
1161 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1162 return nullptr;
1163 }
1164
1165 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1166 if (!backendFormat.isValid()) {
1167 return nullptr;
1168 }
1169 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1170 imageInfo.colorType());
1171 if (config == kUnknown_GrPixelConfig) {
1172 return nullptr;
1173 }
1174
1175 GrSurfaceDesc desc;
1176 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1177 desc.fWidth = imageInfo.width();
1178 desc.fHeight = imageInfo.height();
1179 desc.fConfig = config;
1180 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1181 if (!desc.fSampleCnt) {
1182 return nullptr;
1183 }
1184
1185 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1186}
1187
Brian Salomon930f9392018-06-20 16:25:26 -04001188bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1189 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001190 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001191 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001192 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001193 return false;
jvanverth62340062016-04-26 08:01:44 -07001194 }
1195
jvanverth62340062016-04-26 08:01:44 -07001196 // determine if we can blit to and from this format
1197 const GrVkCaps& caps = this->vkCaps();
1198 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001199 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1200 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001201 return false;
jvanverth62340062016-04-26 08:01:44 -07001202 }
1203
egdaniel7ac5da82016-07-15 13:41:42 -07001204 int width = tex->width();
1205 int height = tex->height();
1206 VkImageBlit blitRegion;
1207 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001208
jvanverth82c05582016-05-03 11:19:01 -07001209 // SkMipMap doesn't include the base level in the level count so we have to add 1
1210 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001211 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001212
Greg Danielda86e282018-06-13 09:41:19 -04001213 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001214 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1215 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001216
jvanverth50c46c72016-05-06 12:31:28 -07001217 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001218 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001219 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1220 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001221 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1222 nullptr, // pNext
1223 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1224 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1225 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1226 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1227 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1228 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1229 vkTex->image(), // image
1230 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001231 };
1232
jvanverth62340062016-04-26 08:01:44 -07001233 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001234 uint32_t mipLevel = 1;
1235 while (mipLevel < levelCount) {
1236 int prevWidth = width;
1237 int prevHeight = height;
1238 width = SkTMax(1, width / 2);
1239 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001240
jvanverth50c46c72016-05-06 12:31:28 -07001241 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1242 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1243 false, &imageMemoryBarrier);
1244
1245 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001246 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001247 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001248 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1249 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001250 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001251 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001252 vkTex->resource(),
1253 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001254 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001255 vkTex->resource(),
1256 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001257 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001258 1,
1259 &blitRegion,
1260 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001261 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001262 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001263 // This barrier logically is not needed, but it changes the final level to the same layout as
1264 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1265 // future layout changes easier.
1266 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1267 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1268 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001269 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1270 return true;
jvanverth62340062016-04-26 08:01:44 -07001271}
1272
Greg Daniel164a9f02016-02-22 09:56:40 -05001273////////////////////////////////////////////////////////////////////////////////
1274
1275GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1276 int width,
1277 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001278 SkASSERT(width >= rt->width());
1279 SkASSERT(height >= rt->height());
1280
1281 int samples = rt->numStencilSamples();
1282
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001283 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001284
1285 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001286 width,
1287 height,
1288 samples,
1289 sFmt));
1290 fStats.incStencilAttachmentCreates();
1291 return stencil;
1292}
1293
1294////////////////////////////////////////////////////////////////////////////////
1295
Brian Salomon52e943a2018-03-13 09:32:39 -04001296bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001297 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1298 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001299 VkDeviceSize size = dstRowBytes * h;
1300 VkDeviceSize offset = bufferOffset;
1301 SkASSERT(size + offset <= alloc.fSize);
1302 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1303 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001304 return false;
1305 }
Greg Daniel81df0412018-05-31 13:13:33 -04001306 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001307
Greg Daniel20ece3a2017-03-28 10:24:43 -04001308 if (srcData) {
1309 // If there is no padding on dst we can do a single memcopy.
1310 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001311 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001312 } else {
1313 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1314 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001315 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001316 }
Greg Daniel81df0412018-05-31 13:13:33 -04001317 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1318 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001319 return true;
1320}
1321
Brian Salomonf865b052018-03-09 09:01:53 -05001322#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001323bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1324 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001325 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001326 SkASSERT(texturable || renderable);
1327 if (!texturable) {
1328 SkASSERT(GrMipMapped::kNo == mipMapped);
1329 SkASSERT(!srcData);
1330 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001331 VkFormat pixelFormat;
1332 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001333 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001334 }
1335
Brian Salomon52e943a2018-03-13 09:32:39 -04001336 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1337 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001338 }
1339
Brian Salomon52e943a2018-03-13 09:32:39 -04001340 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1341 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001342 }
1343
1344 // Currently we don't support uploading pixel data when mipped.
1345 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001346 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001347 }
1348
Brian Salomon52e943a2018-03-13 09:32:39 -04001349 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001350 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1351 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001352 if (texturable) {
1353 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1354 }
1355 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001356 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1357 }
1358
1359 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001360 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001361 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001362
1363 // Create Image
1364 VkSampleCountFlagBits vkSamples;
1365 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001366 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001367 }
1368
1369 // Figure out the number of mip levels.
1370 uint32_t mipLevels = 1;
1371 if (GrMipMapped::kYes == mipMapped) {
1372 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1373 }
1374
1375 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001376 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1377 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001378 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001379 VK_IMAGE_TYPE_2D, // VkImageType
1380 pixelFormat, // VkFormat
1381 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1382 mipLevels, // mipLevels
1383 1, // arrayLayers
1384 vkSamples, // samples
1385 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1386 usageFlags, // VkImageUsageFlags
1387 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1388 0, // queueFamilyCount
1389 0, // pQueueFamilyIndices
1390 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001391 };
1392
Brian Salomon52e943a2018-03-13 09:32:39 -04001393 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1394 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001395
Brian Salomonde9f5462018-03-07 14:23:58 -05001396 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001397 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001398 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001399 }
1400
1401 // 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 -05001402 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001403 VkBuffer buffer = VK_NULL_HANDLE;
1404
1405 VkResult err;
1406 const VkCommandBufferAllocateInfo cmdInfo = {
1407 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1408 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001409 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001410 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1411 1 // bufferCount
1412 };
1413
1414 VkCommandBuffer cmdBuffer;
1415 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1416 if (err) {
1417 GrVkMemory::FreeImageMemory(this, false, alloc);
1418 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001419 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001420 }
1421
1422 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1423 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1424 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1425 cmdBufferBeginInfo.pNext = nullptr;
1426 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1427 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1428
1429 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1430 SkASSERT(!err);
1431
1432 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001433 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001434
Robert Phillips646f6372018-09-25 09:31:10 -04001435 const size_t trimRowBytes = w * bpp;
1436 if (!srcRowBytes) {
1437 srcRowBytes = trimRowBytes;
1438 }
1439
Brian Salomonde9f5462018-03-07 14:23:58 -05001440 SkTArray<size_t> individualMipOffsets(mipLevels);
1441 individualMipOffsets.push_back(0);
1442 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001443 if (GrPixelConfigIsCompressed(config)) {
1444 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1445 bpp = 4; // we have at least this alignment, which will pass the code below
1446 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001447 int currentWidth = w;
1448 int currentHeight = h;
1449 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1450 // config. This works with the assumption that the bytes in pixel config is always a power
1451 // of 2.
1452 SkASSERT((bpp & (bpp - 1)) == 0);
1453 const size_t alignmentMask = 0x3 | (bpp - 1);
1454 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1455 currentWidth = SkTMax(1, currentWidth / 2);
1456 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001457
Jim Van Verth1676cb92019-01-15 13:24:45 -05001458 size_t trimmedSize;
1459 if (GrPixelConfigIsCompressed(config)) {
1460 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1461 } else {
1462 trimmedSize = currentWidth * bpp * currentHeight;
1463 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001464 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1465 if (alignmentDiff != 0) {
1466 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001467 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001468 individualMipOffsets.push_back(combinedBufferSize);
1469 combinedBufferSize += trimmedSize;
1470 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001471
Brian Salomonde9f5462018-03-07 14:23:58 -05001472 VkBufferCreateInfo bufInfo;
1473 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1474 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1475 bufInfo.flags = 0;
1476 bufInfo.size = combinedBufferSize;
1477 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1478 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1479 bufInfo.queueFamilyIndexCount = 0;
1480 bufInfo.pQueueFamilyIndices = nullptr;
1481 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001482
Brian Salomonde9f5462018-03-07 14:23:58 -05001483 if (err) {
1484 GrVkMemory::FreeImageMemory(this, false, alloc);
1485 VK_CALL(DestroyImage(fDevice, image, nullptr));
1486 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001487 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001488 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001489 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001490
Brian Salomonde9f5462018-03-07 14:23:58 -05001491 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1492 &bufferAlloc)) {
1493 GrVkMemory::FreeImageMemory(this, false, alloc);
1494 VK_CALL(DestroyImage(fDevice, image, nullptr));
1495 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1496 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001497 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001498 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001499 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001500
Brian Salomonde9f5462018-03-07 14:23:58 -05001501 currentWidth = w;
1502 currentHeight = h;
1503 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1504 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001505 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001506 bool result;
1507 if (GrPixelConfigIsCompressed(config)) {
1508 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1509 size_t currentRowBytes = levelSize / currentHeight;
1510 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1511 currentRowBytes, currentRowBytes, currentHeight);
1512 } else {
1513 size_t currentRowBytes = bpp * currentWidth;
1514 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1515 currentRowBytes, trimRowBytes, currentHeight);
1516 }
1517 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001518 GrVkMemory::FreeImageMemory(this, false, alloc);
1519 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001520 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001521 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1522 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001523 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001524 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001525 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001526 currentWidth = SkTMax(1, currentWidth / 2);
1527 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001528 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001529
1530 // Set image layout and add barrier
1531 VkImageMemoryBarrier barrier;
1532 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1533 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1534 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001535 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001536 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1537 barrier.oldLayout = initialLayout;
1538 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1539 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1540 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1541 barrier.image = image;
1542 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1543
Greg Danielf7828d02018-10-09 12:01:32 -04001544 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001545 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1546 &barrier));
1547 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1548
1549 SkTArray<VkBufferImageCopy> regions(mipLevels);
1550
1551 currentWidth = w;
1552 currentHeight = h;
1553 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1554 // Submit copy command
1555 VkBufferImageCopy& region = regions.push_back();
1556 memset(&region, 0, sizeof(VkBufferImageCopy));
1557 region.bufferOffset = individualMipOffsets[currentMipLevel];
1558 region.bufferRowLength = currentWidth;
1559 region.bufferImageHeight = currentHeight;
1560 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1561 region.imageOffset = {0, 0, 0};
1562 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1563 currentWidth = SkTMax(1, currentWidth / 2);
1564 currentHeight = SkTMax(1, currentHeight / 2);
1565 }
1566
1567 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1568 regions.begin()));
1569
Brian Salomon52e943a2018-03-13 09:32:39 -04001570 if (texturable) {
1571 // Change Image layout to shader read since if we use this texture as a borrowed textures
1572 // within Ganesh we require that its layout be set to that
1573 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1574 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1575 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001576 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001577 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1578 barrier.oldLayout = initialLayout;
1579 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1580 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1581 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1582 barrier.image = image;
1583 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001584 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001585 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1586 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001587 0,
1588 0, nullptr,
1589 0, nullptr,
1590 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001591 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001592 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001593
1594 // End CommandBuffer
1595 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1596 SkASSERT(!err);
1597
1598 // Create Fence for queue
1599 VkFence fence;
1600 VkFenceCreateInfo fenceInfo;
1601 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1602 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1603
1604 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1605 SkASSERT(!err);
1606
1607 VkSubmitInfo submitInfo;
1608 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1609 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1610 submitInfo.pNext = nullptr;
1611 submitInfo.waitSemaphoreCount = 0;
1612 submitInfo.pWaitSemaphores = nullptr;
1613 submitInfo.pWaitDstStageMask = 0;
1614 submitInfo.commandBufferCount = 1;
1615 submitInfo.pCommandBuffers = &cmdBuffer;
1616 submitInfo.signalSemaphoreCount = 0;
1617 submitInfo.pSignalSemaphores = nullptr;
1618 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1619 SkASSERT(!err);
1620
1621 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1622 if (VK_TIMEOUT == err) {
1623 GrVkMemory::FreeImageMemory(this, false, alloc);
1624 VK_CALL(DestroyImage(fDevice, image, nullptr));
1625 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1626 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001627 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001628 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1629 SkDebugf("Fence failed to signal: %d\n", err);
1630 SK_ABORT("failing");
1631 }
1632 SkASSERT(!err);
1633
1634 // Clean up transfer resources
1635 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1636 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1637 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1638 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001639 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001640 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1641
Brian Salomon52e943a2018-03-13 09:32:39 -04001642 info->fImage = image;
1643 info->fAlloc = alloc;
1644 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1645 info->fImageLayout = initialLayout;
1646 info->fFormat = pixelFormat;
1647 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001648
Brian Salomon52e943a2018-03-13 09:32:39 -04001649 return true;
1650}
1651
1652GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001653 GrColorType colorType,
1654 bool isRenderTarget,
1655 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001656 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001657
1658 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1659 return GrBackendTexture();
1660 }
1661
Robert Phillips646f6372018-09-25 09:31:10 -04001662 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1663 if (!this->caps()->isConfigTexturable(config)) {
1664 return GrBackendTexture();
1665 }
1666
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001667 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001668 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001669 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001670 return {};
1671 }
Greg Daniel108bb232018-07-03 16:18:29 -04001672 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1673 // Lots of tests don't go through Skia's public interface which will set the config so for
1674 // testing we make sure we set a config here.
1675 beTex.setPixelConfig(config);
1676 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001677}
1678
1679bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001680 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001681
Greg Daniel52e16d92018-04-10 09:34:07 -04001682 GrVkImageInfo backend;
1683 if (!tex.getVkImageInfo(&backend)) {
1684 return false;
1685 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001686
Greg Daniel52e16d92018-04-10 09:34:07 -04001687 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001688 VkMemoryRequirements req;
1689 memset(&req, 0, sizeof(req));
1690 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001691 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001692 &req));
1693 // TODO: find a better check
1694 // This will probably fail with a different driver
1695 return (req.size > 0) && (req.size <= 8192 * 8192);
1696 }
1697
1698 return false;
1699}
1700
Brian Salomon26102cb2018-03-09 09:33:19 -05001701void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001702 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001703
Greg Daniel52e16d92018-04-10 09:34:07 -04001704 GrVkImageInfo info;
1705 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001706 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001707 }
1708}
1709
Brian Osman2d010b62018-08-09 10:55:09 -04001710GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001711 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1712 return GrBackendRenderTarget();
1713 }
1714
Brian Salomon8a375832018-03-14 10:21:40 -04001715 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001716 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001717 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001718 if (kUnknown_GrPixelConfig == config) {
1719 return {};
1720 }
Robert Phillips646f6372018-09-25 09:31:10 -04001721 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001722 &info)) {
1723 return {};
1724 }
Greg Daniel108bb232018-07-03 16:18:29 -04001725 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1726 // Lots of tests don't go through Skia's public interface which will set the config so for
1727 // testing we make sure we set a config here.
1728 beRT.setPixelConfig(config);
1729 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001730}
1731
Brian Salomon52e943a2018-03-13 09:32:39 -04001732void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001733 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001734
Greg Daniel323fbcf2018-04-10 13:46:30 -04001735 GrVkImageInfo info;
1736 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001737 // something in the command buffer may still be using this, so force submit
1738 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001739 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001740 }
1741}
Brian Salomonf865b052018-03-09 09:01:53 -05001742
Greg Daniel26b50a42018-03-08 09:49:58 -05001743void GrVkGpu::testingOnly_flushGpuAndSync() {
1744 this->submitCommandBuffer(kForce_SyncQueue);
1745}
Brian Salomonf865b052018-03-09 09:01:53 -05001746#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001747
Greg Daniel164a9f02016-02-22 09:56:40 -05001748////////////////////////////////////////////////////////////////////////////////
1749
1750void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1751 VkPipelineStageFlags dstStageMask,
1752 bool byRegion,
1753 VkMemoryBarrier* barrier) const {
1754 SkASSERT(fCurrentCmdBuffer);
1755 fCurrentCmdBuffer->pipelineBarrier(this,
1756 srcStageMask,
1757 dstStageMask,
1758 byRegion,
1759 GrVkCommandBuffer::kMemory_BarrierType,
1760 barrier);
1761}
1762
1763void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1764 VkPipelineStageFlags dstStageMask,
1765 bool byRegion,
1766 VkBufferMemoryBarrier* barrier) const {
1767 SkASSERT(fCurrentCmdBuffer);
1768 fCurrentCmdBuffer->pipelineBarrier(this,
1769 srcStageMask,
1770 dstStageMask,
1771 byRegion,
1772 GrVkCommandBuffer::kBufferMemory_BarrierType,
1773 barrier);
1774}
1775
1776void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1777 VkPipelineStageFlags dstStageMask,
1778 bool byRegion,
1779 VkImageMemoryBarrier* barrier) const {
1780 SkASSERT(fCurrentCmdBuffer);
1781 fCurrentCmdBuffer->pipelineBarrier(this,
1782 srcStageMask,
1783 dstStageMask,
1784 byRegion,
1785 GrVkCommandBuffer::kImageMemory_BarrierType,
1786 barrier);
1787}
1788
Greg Daniel51316782017-08-02 15:10:09 +00001789void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1790 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1791 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001792 this->submitCommandBuffer(kSkip_SyncQueue);
1793}
1794
Greg Daniel25af6712018-04-25 10:44:38 -04001795static int get_surface_sample_cnt(GrSurface* surf) {
1796 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1797 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001798 }
Greg Daniel25af6712018-04-25 10:44:38 -04001799 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001800}
1801
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001802void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1803 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001804 GrVkImage* dstImage,
1805 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001806 const SkIRect& srcRect,
1807 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001808#ifdef SK_DEBUG
1809 int dstSampleCnt = get_surface_sample_cnt(dst);
1810 int srcSampleCnt = get_surface_sample_cnt(src);
1811 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1812 src->config(), srcSampleCnt, srcOrigin));
1813
1814#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001815
Greg Daniel164a9f02016-02-22 09:56:40 -05001816 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1817 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001818 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001819 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1820 VK_ACCESS_TRANSFER_WRITE_BIT,
1821 VK_PIPELINE_STAGE_TRANSFER_BIT,
1822 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001823
egdaniel17b89252016-04-05 07:23:38 -07001824 srcImage->setImageLayout(this,
1825 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001826 VK_ACCESS_TRANSFER_READ_BIT,
1827 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001828 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001829
1830 // Flip rect if necessary
1831 SkIRect srcVkRect = srcRect;
1832 int32_t dstY = dstPoint.fY;
1833
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001834 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1835 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001836 srcVkRect.fTop = src->height() - srcRect.fBottom;
1837 srcVkRect.fBottom = src->height() - srcRect.fTop;
1838 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1839 }
1840
1841 VkImageCopy copyRegion;
1842 memset(&copyRegion, 0, sizeof(VkImageCopy));
1843 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1844 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1845 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1846 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001847 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001848
1849 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001850 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001851 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001852 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001853 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1854 1,
1855 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001856
1857 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1858 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001859 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001860}
1861
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001862void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1863 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001864 GrVkImage* dstImage,
1865 GrVkImage* srcImage,
1866 const SkIRect& srcRect,
1867 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001868#ifdef SK_DEBUG
1869 int dstSampleCnt = get_surface_sample_cnt(dst);
1870 int srcSampleCnt = get_surface_sample_cnt(src);
1871 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1872 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001873
Greg Daniel25af6712018-04-25 10:44:38 -04001874#endif
egdaniel17b89252016-04-05 07:23:38 -07001875 dstImage->setImageLayout(this,
1876 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001877 VK_ACCESS_TRANSFER_WRITE_BIT,
1878 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001879 false);
1880
egdaniel17b89252016-04-05 07:23:38 -07001881 srcImage->setImageLayout(this,
1882 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001883 VK_ACCESS_TRANSFER_READ_BIT,
1884 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001885 false);
1886
1887 // Flip rect if necessary
1888 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001889 srcVkRect.fLeft = srcRect.fLeft;
1890 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001891 SkIRect dstRect;
1892 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001893 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001894
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001895 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001896 srcVkRect.fTop = src->height() - srcRect.fBottom;
1897 srcVkRect.fBottom = src->height() - srcRect.fTop;
1898 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001899 srcVkRect.fTop = srcRect.fTop;
1900 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001901 }
1902
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001903 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001904 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1905 } else {
1906 dstRect.fTop = dstPoint.fY;
1907 }
1908 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1909
1910 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1911 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001912 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001913 using std::swap;
1914 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001915 }
1916
1917 VkImageBlit blitRegion;
1918 memset(&blitRegion, 0, sizeof(VkImageBlit));
1919 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1920 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001921 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001922 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1923 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001924 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001925
1926 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001927 *srcImage,
1928 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001929 1,
1930 &blitRegion,
1931 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001932
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001933 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001934 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001935}
1936
Brian Salomon1fabd512018-02-09 09:54:25 -05001937void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1938 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1939 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001940 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001941 SkIRect srcRect = origSrcRect;
1942 SkIPoint dstPoint = origDstPoint;
1943 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1944 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1945 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1946 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1947 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1948 }
1949 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001950 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1951 srcRect.width(), srcRect.height());
1952 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001953}
1954
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001955bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1956 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001957 const SkIRect& srcRect, const SkIPoint& dstPoint,
1958 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001959#ifdef SK_DEBUG
1960 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
1961 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
1962 }
1963 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
1964 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
1965 }
1966#endif
1967
Greg Daniel25af6712018-04-25 10:44:38 -04001968 GrPixelConfig dstConfig = dst->config();
1969 GrPixelConfig srcConfig = src->config();
1970
1971 int dstSampleCnt = get_surface_sample_cnt(dst);
1972 int srcSampleCnt = get_surface_sample_cnt(src);
1973
1974 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1975 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001976 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001977 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001978 }
1979
Greg Daniel25af6712018-04-25 10:44:38 -04001980 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1981 srcConfig, SkToBool(src->asTexture()))) {
1982 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1983 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001984 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1985 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001986 return true;
1987 }
1988
egdaniel17b89252016-04-05 07:23:38 -07001989 GrVkImage* dstImage;
1990 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001991 GrRenderTarget* dstRT = dst->asRenderTarget();
1992 if (dstRT) {
1993 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05001994 if (vkRT->wrapsSecondaryCommandBuffer()) {
1995 return false;
1996 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001997 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1998 } else {
1999 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002000 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002001 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002002 GrRenderTarget* srcRT = src->asRenderTarget();
2003 if (srcRT) {
2004 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2005 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002006 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002007 SkASSERT(src->asTexture());
2008 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002009 }
2010
Greg Daniel25af6712018-04-25 10:44:38 -04002011 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
2012 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002013 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2014 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002015 return true;
2016 }
2017
Greg Daniel25af6712018-04-25 10:44:38 -04002018 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
2019 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002020 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2021 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002022 return true;
2023 }
2024
Greg Daniel164a9f02016-02-22 09:56:40 -05002025 return false;
2026}
2027
Brian Salomona6948702018-06-01 15:33:20 -04002028bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2029 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002030 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002031 return false;
2032 }
2033
egdaniel66933552016-08-24 07:22:19 -07002034 GrVkImage* image = nullptr;
2035 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2036 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002037 // Reading from render targets that wrap a secondary command buffer is not allowed since
2038 // it would require us to know the VkImage, which we don't have, as well as need us to
2039 // stop and start the VkRenderPass which we don't have access to.
2040 if (rt->wrapsSecondaryCommandBuffer()) {
2041 return false;
2042 }
egdaniel66933552016-08-24 07:22:19 -07002043 // resolve the render target if necessary
2044 switch (rt->getResolveType()) {
2045 case GrVkRenderTarget::kCantResolve_ResolveType:
2046 return false;
2047 case GrVkRenderTarget::kAutoResolves_ResolveType:
2048 break;
2049 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002050 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002051 break;
2052 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002053 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002054 }
2055 image = rt;
2056 } else {
2057 image = static_cast<GrVkTexture*>(surface->asTexture());
2058 }
2059
2060 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002061 return false;
2062 }
2063
Greg Daniel475eb702018-09-28 14:16:50 -04002064 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2065 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2066 // image and then do the read pixels from that.
2067 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002068 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2069 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002070
2071 // Make a new surface that is RGBA to copy the RGB surface into.
2072 GrSurfaceDesc surfDesc;
2073 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2074 surfDesc.fWidth = width;
2075 surfDesc.fHeight = height;
2076 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2077 surfDesc.fSampleCnt = 1;
2078
2079 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2080 VK_IMAGE_USAGE_SAMPLED_BIT |
2081 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2082 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2083
2084 GrVkImage::ImageDesc imageDesc;
2085 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2086 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2087 imageDesc.fWidth = width;
2088 imageDesc.fHeight = height;
2089 imageDesc.fLevels = 1;
2090 imageDesc.fSamples = 1;
2091 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2092 imageDesc.fUsageFlags = usageFlags;
2093 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2094
2095 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2096 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2097 if (!copySurface) {
2098 return false;
2099 }
2100
2101 int srcSampleCount = 0;
2102 if (rt) {
2103 srcSampleCount = rt->numColorSamples();
2104 }
2105 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
2106 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin,
2107 surface->config(), srcSampleCount, kOrigin) &&
2108 !this->vkCaps().canCopyAsDraw(copySurface->config(), false,
2109 surface->config(), SkToBool(surface->asTexture()))) {
2110 return false;
2111 }
2112 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2113 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2114 srcRect, SkIPoint::Make(0,0))) {
2115 return false;
2116 }
2117 top = 0;
2118 left = 0;
2119 dstColorType = GrColorType::kRGBA_8888;
2120 image = copySurface.get();
2121 }
2122
Greg Daniel164a9f02016-02-22 09:56:40 -05002123 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002124 image->setImageLayout(this,
2125 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2126 VK_ACCESS_TRANSFER_READ_BIT,
2127 VK_PIPELINE_STAGE_TRANSFER_BIT,
2128 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002129
Brian Salomonc320b152018-02-20 14:05:36 -05002130 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002131 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002132
Greg Daniel164a9f02016-02-22 09:56:40 -05002133 VkBufferImageCopy region;
2134 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002135
2136 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2137 if (copyFromOrigin) {
2138 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002139 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002140 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002141 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002142 region.imageOffset = offset;
2143 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2144 }
2145
2146 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002147 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002148 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002149 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002150 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002151 kStream_GrAccessPattern)
2152 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002153
2154 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002155 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002156 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002157 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2158 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002159
2160 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002161 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002162 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002163 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002164 1,
2165 &region);
2166
2167 // make sure the copy to buffer has finished
2168 transferBuffer->addMemoryBarrier(this,
2169 VK_ACCESS_TRANSFER_WRITE_BIT,
2170 VK_ACCESS_HOST_READ_BIT,
2171 VK_PIPELINE_STAGE_TRANSFER_BIT,
2172 VK_PIPELINE_STAGE_HOST_BIT,
2173 false);
2174
2175 // We need to submit the current command buffer to the Queue and make sure it finishes before
2176 // we can copy the data out of the buffer.
2177 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002178 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002179 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002180 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002181
egdaniel6fa0a912016-09-12 11:51:29 -07002182 if (copyFromOrigin) {
2183 uint32_t skipRows = region.imageExtent.height - height;
2184 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2185 }
2186
Brian Salomona6948702018-06-01 15:33:20 -04002187 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002188
2189 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002190 return true;
2191}
egdaniel066df7c2016-06-08 14:02:27 -07002192
egdaniel27bb2842016-07-07 11:58:35 -07002193// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2194// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2195// the the entire attachment. Similar requirements for the y and height components.
2196void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2197 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2198 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002199 if ((0 != granularity.width && 1 != granularity.width)) {
2200 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2201 int rightAdj = srcBounds.fRight % granularity.width;
2202 if (rightAdj != 0) {
2203 rightAdj = granularity.width - rightAdj;
2204 }
2205 dstBounds->fRight = srcBounds.fRight + rightAdj;
2206 if (dstBounds->fRight > maxWidth) {
2207 dstBounds->fRight = maxWidth;
2208 dstBounds->fLeft = 0;
2209 } else {
2210 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2211 }
egdaniel27bb2842016-07-07 11:58:35 -07002212 } else {
egdanield5797b32016-09-20 12:57:45 -07002213 dstBounds->fLeft = srcBounds.fLeft;
2214 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002215 }
2216
2217 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002218 if ((0 != granularity.height && 1 != granularity.height)) {
2219 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2220 int bottomAdj = srcBounds.fBottom % granularity.height;
2221 if (bottomAdj != 0) {
2222 bottomAdj = granularity.height - bottomAdj;
2223 }
2224 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2225 if (dstBounds->fBottom > maxHeight) {
2226 dstBounds->fBottom = maxHeight;
2227 dstBounds->fTop = 0;
2228 } else {
2229 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2230 }
egdaniel27bb2842016-07-07 11:58:35 -07002231 } else {
egdanield5797b32016-09-20 12:57:45 -07002232 dstBounds->fTop = srcBounds.fTop;
2233 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002234 }
2235}
2236
Greg Daniel22bc8652017-03-22 15:45:43 -04002237void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002238 const GrVkRenderPass* renderPass,
2239 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002240 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002241 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002242 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002243 const SkIRect* pBounds = &bounds;
2244 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002245 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002246 flippedBounds = bounds;
2247 flippedBounds.fTop = target->height() - bounds.fBottom;
2248 flippedBounds.fBottom = target->height() - bounds.fTop;
2249 pBounds = &flippedBounds;
2250 }
2251
egdaniel27bb2842016-07-07 11:58:35 -07002252 // The bounds we use for the render pass should be of the granularity supported
2253 // by the device.
2254 const VkExtent2D& granularity = renderPass->granularity();
2255 SkIRect adjustedBounds;
2256 if ((0 != granularity.width && 1 != granularity.width) ||
2257 (0 != granularity.height && 1 != granularity.height)) {
2258 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2259 target->width(), target->height());
2260 pBounds = &adjustedBounds;
2261 }
2262
Robert Phillips95214472017-08-08 18:00:03 -04002263#ifdef SK_DEBUG
2264 uint32_t index;
2265 bool result = renderPass->colorAttachmentIndex(&index);
2266 SkASSERT(result && 0 == index);
2267 result = renderPass->stencilAttachmentIndex(&index);
2268 if (result) {
2269 SkASSERT(1 == index);
2270 }
2271#endif
2272 VkClearValue clears[2];
2273 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002274 clears[1].depthStencil.depth = 0.0f;
2275 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002276
2277 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002278 for (int i = 0; i < buffers.count(); ++i) {
2279 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2280 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002281 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002282
Brian Salomon1fabd512018-02-09 09:54:25 -05002283 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002284}
egdaniel9cb63402016-06-23 08:37:05 -07002285
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002286void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2287 if (buffer->asRTCommandBuffer()) {
2288 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2289
2290 fCachedRTCommandBuffer->submit();
2291 fCachedRTCommandBuffer->reset();
2292 } else {
2293 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2294
2295 fCachedTexCommandBuffer->submit();
2296 fCachedTexCommandBuffer->reset();
2297 }
2298}
2299
Greg Daniel6be35232017-03-01 17:01:09 -05002300GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002301 VkFenceCreateInfo createInfo;
2302 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2303 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2304 createInfo.pNext = nullptr;
2305 createInfo.flags = 0;
2306 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002307
2308 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2309 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2310
2311 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002312 return (GrFence)fence;
2313}
2314
Greg Daniel6be35232017-03-01 17:01:09 -05002315bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2316 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2317
2318 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002319 return (VK_SUCCESS == result);
2320}
2321
2322void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002323 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2324}
2325
Greg Daniela5cb7812017-06-16 09:45:32 -04002326sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2327 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002328}
2329
Greg Daniel48661b82018-01-22 16:11:35 -05002330sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2331 GrResourceProvider::SemaphoreWrapType wrapType,
2332 GrWrapOwnership ownership) {
2333 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002334}
2335
Greg Daniel858e12c2018-12-06 11:11:37 -05002336void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002337 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2338
Greg Daniel48661b82018-01-22 16:11:35 -05002339 GrVkSemaphore::Resource* resource = vkSem->getResource();
2340 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002341 resource->ref();
2342 fSemaphoresToSignal.push_back(resource);
2343 }
Greg Daniel6be35232017-03-01 17:01:09 -05002344}
2345
Greg Daniel48661b82018-01-22 16:11:35 -05002346void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002347 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2348
Greg Daniel48661b82018-01-22 16:11:35 -05002349 GrVkSemaphore::Resource* resource = vkSem->getResource();
2350 if (resource->shouldWait()) {
2351 resource->ref();
2352 fSemaphoresToWaitOn.push_back(resource);
2353 }
jvanverth84741b32016-09-30 08:39:02 -07002354}
Brian Osman13dddce2017-05-09 13:19:50 -04002355
2356sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2357 SkASSERT(texture);
2358 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2359 vkTexture->setImageLayout(this,
2360 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2361 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002362 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002363 false);
2364 this->submitCommandBuffer(kSkip_SyncQueue);
2365
2366 // The image layout change serves as a barrier, so no semaphore is needed
2367 return nullptr;
2368}
Greg Danielf5d87582017-12-18 14:48:15 -05002369
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002370void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2371 fDrawables.emplace_back(std::move(drawable));
2372}
2373
Greg Daniel7a82edf2018-12-04 10:54:34 -05002374uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2375 const GrBackendFormat& format) {
2376 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2377 SkASSERT(ycbcrInfo);
2378 if (!ycbcrInfo->isValid()) {
2379 return 0;
2380 }
2381
2382 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2383 samplerState, *ycbcrInfo);
2384
2385 return sampler->uniqueID();
2386}
2387
Greg Daniela870b462019-01-08 15:49:46 -05002388void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002389 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002390 this->resourceProvider().storePipelineCacheData();
2391 }
2392}