blob: 6daf28d6f03c90d9f6479795d0749080064c2925 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrContextOptions.h"
13#include "include/private/SkTo.h"
14#include "src/core/SkConvertPixels.h"
15#include "src/core/SkMipMap.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGeometryProcessor.h"
18#include "src/gpu/GrGpuResourceCacheAccess.h"
19#include "src/gpu/GrMesh.h"
20#include "src/gpu/GrPipeline.h"
Greg Daniel797efca2019-05-09 14:04:20 -040021#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrRenderTargetPriv.h"
23#include "src/gpu/GrTexturePriv.h"
Greg Daniel797efca2019-05-09 14:04:20 -040024#include "src/gpu/SkGpuDevice.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/vk/GrVkAMDMemoryAllocator.h"
26#include "src/gpu/vk/GrVkCommandBuffer.h"
27#include "src/gpu/vk/GrVkCommandPool.h"
28#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
29#include "src/gpu/vk/GrVkImage.h"
30#include "src/gpu/vk/GrVkIndexBuffer.h"
31#include "src/gpu/vk/GrVkInterface.h"
32#include "src/gpu/vk/GrVkMemory.h"
33#include "src/gpu/vk/GrVkPipeline.h"
34#include "src/gpu/vk/GrVkPipelineState.h"
35#include "src/gpu/vk/GrVkRenderPass.h"
36#include "src/gpu/vk/GrVkResourceProvider.h"
37#include "src/gpu/vk/GrVkSemaphore.h"
38#include "src/gpu/vk/GrVkTexture.h"
39#include "src/gpu/vk/GrVkTextureRenderTarget.h"
40#include "src/gpu/vk/GrVkTransferBuffer.h"
41#include "src/gpu/vk/GrVkVertexBuffer.h"
Greg Daniel797efca2019-05-09 14:04:20 -040042#include "src/image/SkImage_Gpu.h"
43#include "src/image/SkSurface_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/sksl/SkSLCompiler.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040045
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "include/gpu/vk/GrVkExtensions.h"
47#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050048
Ben Wagnerf08d1d02018-06-18 15:11:00 -040049#include <utility>
50
Forrest Reiling44f85712017-03-27 23:22:20 -070051#if !defined(SK_BUILD_FOR_WIN)
52#include <unistd.h>
53#endif // !defined(SK_BUILD_FOR_WIN)
54
Greg Danieldef55462018-08-01 13:40:14 -040055#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050056#include "include/private/SkLeanWindows.h"
Greg Danieldef55462018-08-01 13:40:14 -040057#endif
58
Greg Daniel164a9f02016-02-22 09:56:40 -050059#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
60#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
61#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
62
Greg Danielf730c182018-07-02 20:15:37 +000063sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050064 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000065 if (backendContext.fInstance == VK_NULL_HANDLE ||
66 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
67 backendContext.fDevice == VK_NULL_HANDLE ||
68 backendContext.fQueue == VK_NULL_HANDLE) {
69 return nullptr;
70 }
Greg Danield3e65aa2018-08-01 09:19:45 -040071 if (!backendContext.fGetProc) {
72 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040073 }
Greg Danield3e65aa2018-08-01 09:19:45 -040074
Greg Daniel41f0e282019-01-28 13:15:05 -050075 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
76 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
77 backendContext.fGetProc("vkEnumerateInstanceVersion",
78 VK_NULL_HANDLE, VK_NULL_HANDLE));
79 uint32_t instanceVersion = 0;
80 if (!localEnumerateInstanceVersion) {
81 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
82 } else {
83 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
84 if (err) {
85 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
86 return nullptr;
87 }
88 }
89
Greg Danielc0b03d82018-08-03 14:41:15 -040090 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
91 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
92 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
93 backendContext.fInstance,
94 VK_NULL_HANDLE));
95
96 if (!localGetPhysicalDeviceProperties) {
97 return nullptr;
98 }
99 VkPhysicalDeviceProperties physDeviceProperties;
100 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
101 uint32_t physDevVersion = physDeviceProperties.apiVersion;
102
Greg Daniel41f0e282019-01-28 13:15:05 -0500103 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
104 : instanceVersion;
105
106 instanceVersion = SkTMin(instanceVersion, apiVersion);
107 physDevVersion = SkTMin(physDevVersion, apiVersion);
108
Greg Daniel98bffae2018-08-01 13:25:41 -0400109 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400110
Greg Daniel98bffae2018-08-01 13:25:41 -0400111 if (backendContext.fVkExtensions) {
112 interface.reset(new GrVkInterface(backendContext.fGetProc,
113 backendContext.fInstance,
114 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500115 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400116 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400117 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500118 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400119 return nullptr;
120 }
121 } else {
Greg Daniel98bffae2018-08-01 13:25:41 -0400122 GrVkExtensions extensions;
Greg Daniel88e8ddc2019-04-25 16:37:08 -0400123 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
124 // need to know if this is enabled to know if we can transition to a present layout when
125 // flushing a surface.
126 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
127 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
128 extensions.init(backendContext.fGetProc, backendContext.fInstance,
129 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
130 }
Greg Daniel98bffae2018-08-01 13:25:41 -0400131 interface.reset(new GrVkInterface(backendContext.fGetProc,
132 backendContext.fInstance,
133 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500134 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400135 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400136 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500137 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400138 return nullptr;
139 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500140 }
141
Greg Daniel41f0e282019-01-28 13:15:05 -0500142 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
143 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500144}
145
146////////////////////////////////////////////////////////////////////////////////
147
halcanary9d524f22016-03-29 09:03:52 -0700148GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500149 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
150 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500151 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400152 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000153 , fMemoryAllocator(backendContext.fMemoryAllocator)
154 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400155 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000156 , fDevice(backendContext.fDevice)
157 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400158 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500159 , fResourceProvider(this)
160 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000161 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700162
Greg Daniel81df0412018-05-31 13:13:33 -0400163 if (!fMemoryAllocator) {
164 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000165 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400166 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400167 }
168
ethannicholasb3058bd2016-07-01 08:22:01 -0700169 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700170
Greg Daniela0651ac2018-08-08 09:23:18 -0400171 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400172 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400173 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500174 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400175 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400176 } else if (backendContext.fDeviceFeatures) {
177 VkPhysicalDeviceFeatures2 features2;
178 features2.pNext = nullptr;
179 features2.features = *backendContext.fDeviceFeatures;
180 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500181 features2, instanceVersion, physicalDeviceVersion,
182 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400183 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 VkPhysicalDeviceFeatures2 features;
185 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
186 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400187 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400188 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400189 }
190 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400191 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400192 }
193 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400194 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400195 }
Greg Danielf808c5e2019-04-30 14:48:27 -0400196 GrVkExtensions extensions;
197 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
198 // need to know if this is enabled to know if we can transition to a present layout when
199 // flushing a surface.
200 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
201 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
202 extensions.init(backendContext.fGetProc, backendContext.fInstance,
203 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
204 }
Greg Daniel36443602018-08-02 12:51:52 -0400205 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielf808c5e2019-04-30 14:48:27 -0400206 features, instanceVersion, physicalDeviceVersion, extensions));
Greg Daniel36443602018-08-02 12:51:52 -0400207 }
jvanverth633b3562016-03-23 11:01:22 -0700208 fCaps.reset(SkRef(fVkCaps.get()));
209
Greg Danielf730c182018-07-02 20:15:37 +0000210 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
211 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700212
Greg Daniela870b462019-01-08 15:49:46 -0500213 fResourceProvider.init();
214
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500215 fCmdPool = fResourceProvider.findOrCreateCommandPool();
216 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000217 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700218 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500219}
220
Greg Daniel8606cf82017-05-08 16:17:53 -0400221void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500222 if (fCmdPool) {
223 fCmdPool->getPrimaryCommandBuffer()->end(this);
224 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400225 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500226
227 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500228 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700229
230 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
231 // on the command buffers even though they have completed. This causes an assert to fire when
232 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500233 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700234#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500235 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700236#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500237 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700238#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500239 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700240#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500241 }
egdanielf8c2be32016-06-24 13:18:27 -0700242#endif
243
egdanielbe9d8212016-09-20 08:54:23 -0700244#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400245 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700246#endif
halcanary9d524f22016-03-29 09:03:52 -0700247
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500248 if (fCmdPool) {
249 fCmdPool->unref(this);
250 fCmdPool = nullptr;
251 }
252
Greg Daniel6be35232017-03-01 17:01:09 -0500253 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
254 fSemaphoresToWaitOn[i]->unref(this);
255 }
256 fSemaphoresToWaitOn.reset();
257
Greg Daniela5cb7812017-06-16 09:45:32 -0400258 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
259 fSemaphoresToSignal[i]->unref(this);
260 }
261 fSemaphoresToSignal.reset();
262
263
egdanielbc9b2962016-09-27 08:00:53 -0700264 fCopyManager.destroyResources(this);
265
Jim Van Verth09557d72016-11-07 11:10:21 -0500266 // must call this just before we destroy the command pool and VkDevice
267 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500268
Greg Danielf730c182018-07-02 20:15:37 +0000269 fMemoryAllocator.reset();
270
271 fQueue = VK_NULL_HANDLE;
272 fDevice = VK_NULL_HANDLE;
273 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400274}
275
276GrVkGpu::~GrVkGpu() {
277 if (!fDisconnected) {
278 this->destroyResources();
279 }
280 delete fCompiler;
281}
282
283
284void GrVkGpu::disconnect(DisconnectType type) {
285 INHERITED::disconnect(type);
286 if (!fDisconnected) {
287 if (DisconnectType::kCleanup == type) {
288 this->destroyResources();
289 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500290 if (fCmdPool) {
291 fCmdPool->unrefAndAbandon();
292 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400293 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400294 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
295 fSemaphoresToWaitOn[i]->unrefAndAbandon();
296 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400297 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
298 fSemaphoresToSignal[i]->unrefAndAbandon();
299 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400300 fCopyManager.abandonResources();
301
302 // must call this just before we destroy the command pool and VkDevice
303 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400304
305 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400306 }
307 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400308 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400309 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400310 fDisconnected = true;
311 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500312}
313
314///////////////////////////////////////////////////////////////////////////////
315
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400316GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400317 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400318 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
319 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400320 if (!fCachedRTCommandBuffer) {
321 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
322 }
323
Greg Daniela41a74a2018-10-09 12:59:23 +0000324 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400325 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400326}
327
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400328GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
329 if (!fCachedTexCommandBuffer) {
330 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
331 }
332
333 fCachedTexCommandBuffer->set(texture, origin);
334 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700335}
336
Greg Daniela3aa75a2019-04-12 14:24:55 -0400337void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
338 GrGpuFinishedContext finishedContext) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500339 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400340
341 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
342 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
343 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400344 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400345 if (finishedProc) {
346 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
347 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400348 return;
349 }
350
Greg Daniel164a9f02016-02-22 09:56:40 -0500351 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500352 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400353 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500354
Greg Daniela3aa75a2019-04-12 14:24:55 -0400355 if (finishedProc) {
356 // Make sure this is called after closing the current command pool
357 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
358 }
359
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400360 // We must delete and drawables that have been waitint till submit for us to destroy.
361 fDrawables.reset();
362
Greg Daniel6be35232017-03-01 17:01:09 -0500363 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
364 fSemaphoresToWaitOn[i]->unref(this);
365 }
366 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400367 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
368 fSemaphoresToSignal[i]->unref(this);
369 }
370 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500371
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500372 // Release old command pool and create a new one
373 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500374 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500375 fCmdPool = fResourceProvider.findOrCreateCommandPool();
376 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500377 fCurrentCmdBuffer->begin(this);
378}
379
380///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500381sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
382 GrAccessPattern accessPattern, const void* data) {
383 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700384 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500385 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700386 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
387 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500388 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700389 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500390 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700391 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
392 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500393 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700394 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500395 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400396 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
397 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500398 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700399 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500400 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400401 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
402 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500403 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700404 break;
cdalton397536c2016-03-25 12:15:03 -0700405 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400406 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700407 return nullptr;
408 }
cdalton1bf3e712016-04-19 10:00:02 -0700409 if (data && buff) {
410 buff->updateData(data, size);
411 }
412 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500413}
414
Brian Salomona9b04b92018-06-01 15:04:28 -0400415bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
416 GrColorType srcColorType, const GrMipLevel texels[],
417 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500418 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
419 if (!vkTex) {
420 return false;
421 }
422
jvanverth900bd4a2016-04-29 13:53:12 -0700423 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400424 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800425 return false;
426 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800427
Jim Van Verth1676cb92019-01-15 13:24:45 -0500428 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500429 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400430 bool linearTiling = vkTex->isLinearTiled();
431 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400432 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400433 SkDebugf("Can't upload mipmap data to linear tiled texture");
434 return false;
435 }
436 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
437 // Need to change the layout to general in order to perform a host write
438 vkTex->setImageLayout(this,
439 VK_IMAGE_LAYOUT_GENERAL,
440 VK_ACCESS_HOST_WRITE_BIT,
441 VK_PIPELINE_STAGE_HOST_BIT,
442 false);
443 this->submitCommandBuffer(kForce_SyncQueue);
444 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400445 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400446 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500447 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400448 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400449 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
450 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500451 }
egdaniel4583ec52016-06-27 12:57:00 -0700452
jvanverth900bd4a2016-04-29 13:53:12 -0700453 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500454}
455
Brian Salomone05ba5a2019-04-08 11:59:07 -0400456bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
457 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
458 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500459 // Can't transfer compressed data
460 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
461
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400462 // Vulkan only supports 4-byte aligned offsets
463 if (SkToBool(bufferOffset & 0x2)) {
464 return false;
465 }
466 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
467 if (!vkTex) {
468 return false;
469 }
470 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
471 if (!vkBuffer) {
472 return false;
473 }
474
Greg Daniel660cc992017-06-26 14:55:05 -0400475 SkDEBUGCODE(
476 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
477 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
478 SkASSERT(bounds.contains(subRect));
479 )
Brian Salomonc320b152018-02-20 14:05:36 -0500480 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400481 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500482 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400483 }
484
485 // Set up copy region
486 VkBufferImageCopy region;
487 memset(&region, 0, sizeof(VkBufferImageCopy));
488 region.bufferOffset = bufferOffset;
489 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
490 region.bufferImageHeight = 0;
491 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
492 region.imageOffset = { left, top, 0 };
493 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
494
495 // Change layout of our target so it can be copied to
496 vkTex->setImageLayout(this,
497 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
498 VK_ACCESS_TRANSFER_WRITE_BIT,
499 VK_PIPELINE_STAGE_TRANSFER_BIT,
500 false);
501
502 // Copy the buffer to the image
503 fCurrentCmdBuffer->copyBufferToImage(this,
504 vkBuffer,
505 vkTex,
506 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
507 1,
508 &region);
509
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400510 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400511 return true;
512}
513
Brian Salomon26de56e2019-04-10 12:14:26 -0400514bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
515 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
516 size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400517 SkASSERT(surface);
518 SkASSERT(transferBuffer);
519
Brian Salomona585fe92019-04-09 14:57:00 -0400520 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
521
522 GrVkImage* srcImage;
523 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
524 // Reading from render targets that wrap a secondary command buffer is not allowed since
525 // it would require us to know the VkImage, which we don't have, as well as need us to
526 // stop and start the VkRenderPass which we don't have access to.
527 if (rt->wrapsSecondaryCommandBuffer()) {
528 return false;
529 }
530 // resolve the render target if necessary
531 switch (rt->getResolveType()) {
532 case GrVkRenderTarget::kCantResolve_ResolveType:
533 return false;
534 case GrVkRenderTarget::kAutoResolves_ResolveType:
535 break;
536 case GrVkRenderTarget::kCanResolve_ResolveType:
537 this->resolveRenderTargetNoFlush(rt);
538 break;
539 default:
540 SK_ABORT("Unknown resolve type");
541 }
542 srcImage = rt;
543 } else {
544 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
545 }
546
547 // Set up copy region
548 VkBufferImageCopy region;
549 memset(&region, 0, sizeof(VkBufferImageCopy));
550 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400551 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400552 region.bufferImageHeight = 0;
553 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
554 region.imageOffset = { left, top, 0 };
555 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
556
557 srcImage->setImageLayout(this,
558 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
559 VK_ACCESS_TRANSFER_READ_BIT,
560 VK_PIPELINE_STAGE_TRANSFER_BIT,
561 false);
562
563 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
564 vkBuffer, 1, &region);
565
566 // Make sure the copy to buffer has finished.
567 vkBuffer->addMemoryBarrier(this,
568 VK_ACCESS_TRANSFER_WRITE_BIT,
569 VK_ACCESS_HOST_READ_BIT,
570 VK_PIPELINE_STAGE_TRANSFER_BIT,
571 VK_PIPELINE_STAGE_HOST_BIT,
572 false);
573
574 // The caller is responsible for syncing.
575 this->submitCommandBuffer(kSkip_SyncQueue);
576
Brian Salomon26de56e2019-04-10 12:14:26 -0400577 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400578}
579
Brian Salomon1fabd512018-02-09 09:54:25 -0500580void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
581 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700582 SkASSERT(dst);
583 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
584
egdaniel4bcd62e2016-08-31 07:37:31 -0700585 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500586 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
587 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
588 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
589 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
590 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700591
Greg Danielbc26c392017-04-18 13:32:10 -0400592 GrVkImage* dstImage;
593 GrRenderTarget* dstRT = dst->asRenderTarget();
594 if (dstRT) {
595 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400596 dstImage = vkRT;
597 } else {
598 SkASSERT(dst->asTexture());
599 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
600 }
601 dstImage->setImageLayout(this,
602 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
603 VK_ACCESS_TRANSFER_WRITE_BIT,
604 VK_PIPELINE_STAGE_TRANSFER_BIT,
605 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700606
607 src->msaaImage()->setImageLayout(this,
608 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
609 VK_ACCESS_TRANSFER_READ_BIT,
610 VK_PIPELINE_STAGE_TRANSFER_BIT,
611 false);
612
Greg Danielbc26c392017-04-18 13:32:10 -0400613 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700614}
615
Brian Salomon1fabd512018-02-09 09:54:25 -0500616void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700617 if (target->needsResolve()) {
618 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700619 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
620 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500621
egdaniel4bcd62e2016-08-31 07:37:31 -0700622 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700623
Brian Salomon1fabd512018-02-09 09:54:25 -0500624 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700625
626 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500627
628 if (requiresSubmit) {
629 this->submitCommandBuffer(kSkip_SyncQueue);
630 }
egdaniel52ad2512016-08-04 12:50:01 -0700631 }
632}
633
Brian Salomona9b04b92018-06-01 15:04:28 -0400634bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
635 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500636 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700637 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500638
Jim Van Verth1676cb92019-01-15 13:24:45 -0500639 // If we're uploading compressed data then we should be using uploadCompressedTexData
640 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
641 GrSRGBEncoded::kNo)));
642
Greg Daniel660cc992017-06-26 14:55:05 -0400643 SkDEBUGCODE(
644 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
645 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
646 SkASSERT(bounds.contains(subRect));
647 )
Brian Salomonc320b152018-02-20 14:05:36 -0500648 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500649 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400650 if (!rowBytes) {
651 rowBytes = trimRowBytes;
652 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500653
jvanverth900bd4a2016-04-29 13:53:12 -0700654 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
655 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
656 const VkImageSubresource subres = {
657 VK_IMAGE_ASPECT_COLOR_BIT,
658 0, // mipLevel
659 0, // arraySlice
660 };
661 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500662
jvanverth900bd4a2016-04-29 13:53:12 -0700663 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500664
jvanverth900bd4a2016-04-29 13:53:12 -0700665 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700666 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700667 &subres,
668 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500669
jvanverth1e305ba2016-06-01 09:39:15 -0700670 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400671 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700672 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400673 SkASSERT(size + offset <= alloc.fSize);
674 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
675 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700676 return false;
677 }
Greg Daniel81df0412018-05-31 13:13:33 -0400678 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700679
Brian Salomona9b04b92018-06-01 15:04:28 -0400680 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
681 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700682
Greg Daniele35a99e2018-03-02 11:44:22 -0500683 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400684 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700685
686 return true;
687}
688
Brian Salomona9b04b92018-06-01 15:04:28 -0400689bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
690 GrColorType dataColorType, const GrMipLevel texels[],
691 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700692 SkASSERT(!tex->isLinearTiled());
693 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400694 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700695 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
696
Greg Danieldd20e912017-04-07 14:42:23 -0400697 // We assume that if the texture has mip levels, we either upload to all the levels or just the
698 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400699 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400700
Jim Van Verth1676cb92019-01-15 13:24:45 -0500701 // If we're uploading compressed data then we should be using uploadCompressedTexData
702 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
703 GrSRGBEncoded::kNo)));
704
jvanverth900bd4a2016-04-29 13:53:12 -0700705 if (width == 0 || height == 0) {
706 return false;
707 }
708
Greg Daniel475eb702018-09-28 14:16:50 -0400709 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
710 return false;
711 }
712
713 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
714 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500715 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
716 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400717 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
718 // blit or draw.
719 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
720 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
721 return false;
722 }
723 mipLevelCount = 1;
724 }
725
Brian Salomond1eaf492017-05-18 10:02:08 -0400726 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500727 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700728
729 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700730 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
731 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400732 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
733
Greg Daniel475eb702018-09-28 14:16:50 -0400734 texelsShallowCopy.reset(mipLevelCount);
735 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700736
Robert Phillips590533f2017-07-11 14:22:35 -0400737 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700738 individualMipOffsets.push_back(0);
739 size_t combinedBufferSize = width * bpp * height;
740 int currentWidth = width;
741 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400742 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400743 combinedBufferSize = 0;
744 }
745
Greg Daniel468fd632017-03-22 17:03:45 -0400746 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
747 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
748 SkASSERT((bpp & (bpp - 1)) == 0);
749 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400750 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700751 currentWidth = SkTMax(1, currentWidth/2);
752 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400753
Greg Daniel55afd6d2017-09-29 09:32:44 -0400754 if (texelsShallowCopy[currentMipLevel].fPixels) {
755 const size_t trimmedSize = currentWidth * bpp * currentHeight;
756 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
757 if (alignmentDiff != 0) {
758 combinedBufferSize += alignmentMask - alignmentDiff + 1;
759 }
760 individualMipOffsets.push_back(combinedBufferSize);
761 combinedBufferSize += trimmedSize;
762 } else {
763 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400764 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400765 }
766 if (0 == combinedBufferSize) {
767 // We don't actually have any data to upload so just return success
768 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700769 }
770
771 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500772 sk_sp<GrVkTransferBuffer> transferBuffer =
773 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400774 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700775 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400776 }
jvanverth900bd4a2016-04-29 13:53:12 -0700777
Greg Daniel475eb702018-09-28 14:16:50 -0400778 int uploadLeft = left;
779 int uploadTop = top;
780 GrVkTexture* uploadTexture = tex;
781 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
782 // R8G8B8A8_UNORM image and then copy it.
783 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500784 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400785 GrSurfaceDesc surfDesc;
786 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
787 surfDesc.fWidth = width;
788 surfDesc.fHeight = height;
789 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
790 surfDesc.fSampleCnt = 1;
791
792 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
793 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
794 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
795
796 GrVkImage::ImageDesc imageDesc;
797 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
798 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
799 imageDesc.fWidth = width;
800 imageDesc.fHeight = height;
801 imageDesc.fLevels = 1;
802 imageDesc.fSamples = 1;
803 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
804 imageDesc.fUsageFlags = usageFlags;
805 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
806
807 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
808 GrMipMapsStatus::kNotAllocated);
809 if (!copyTexture) {
810 return false;
811 }
Greg Daniel5c7b5412019-05-10 11:39:55 -0400812
813 bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid();
814 if (!this->vkCaps().canCopyAsBlit(tex->config(), 1, false, dstHasYcbcr,
815 copyTexture->config(), 1, false,
816 false) &&
817 !this->vkCaps().canCopyAsDraw(tex->config(), SkToBool(tex->asRenderTarget()),
818 dstHasYcbcr,
819 copyTexture->config(), true, false)) {
820 return false;
821 }
822
Greg Daniel475eb702018-09-28 14:16:50 -0400823 uploadTexture = copyTexture.get();
824 uploadLeft = 0;
825 uploadTop = 0;
826 }
827
jvanverth900bd4a2016-04-29 13:53:12 -0700828 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400829 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700830
jvanverthc578b0632016-05-02 10:58:12 -0700831 currentWidth = width;
832 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400833 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400834 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400835 if (texelsShallowCopy[currentMipLevel].fPixels) {
836 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
837 const size_t trimRowBytes = currentWidth * bpp;
838 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
839 ? texelsShallowCopy[currentMipLevel].fRowBytes
840 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700841
Greg Daniel55afd6d2017-09-29 09:32:44 -0400842 // copy data into the buffer, skipping the trailing bytes
843 char* dst = buffer + individualMipOffsets[currentMipLevel];
844 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400845 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400846
847 VkBufferImageCopy& region = regions.push_back();
848 memset(&region, 0, sizeof(VkBufferImageCopy));
849 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
850 region.bufferRowLength = currentWidth;
851 region.bufferImageHeight = currentHeight;
852 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400853 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400854 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700855 }
jvanverthc578b0632016-05-02 10:58:12 -0700856 currentWidth = SkTMax(1, currentWidth/2);
857 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400858 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700859 }
860
jvanverth9d54afc2016-09-20 09:20:03 -0700861 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700862 transferBuffer->unmap();
863
jvanverth900bd4a2016-04-29 13:53:12 -0700864 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400865 uploadTexture->setImageLayout(this,
866 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
867 VK_ACCESS_TRANSFER_WRITE_BIT,
868 VK_PIPELINE_STAGE_TRANSFER_BIT,
869 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700870
871 // Copy the buffer to the image
872 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500873 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400874 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700875 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
876 regions.count(),
877 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400878
879 // If we copied the data into a temporary image first, copy that image into our main texture
880 // now.
881 if (copyTexture.get()) {
882 SkASSERT(dataColorType == GrColorType::kRGB_888x);
883 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
884 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
885 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
886 false));
887 }
Robert Phillips590533f2017-07-11 14:22:35 -0400888 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400889 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400890 }
jvanverth900bd4a2016-04-29 13:53:12 -0700891
Greg Daniel164a9f02016-02-22 09:56:40 -0500892 return true;
893}
894
Jim Van Verth1676cb92019-01-15 13:24:45 -0500895// It's probably possible to roll this into uploadTexDataOptimal,
896// but for now it's easier to maintain as a separate entity.
897bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
898 GrColorType dataColorType, const GrMipLevel texels[],
899 int mipLevelCount) {
900 SkASSERT(!tex->isLinearTiled());
901 // For now the assumption is that our rect is the entire texture.
902 // Compressed textures are read-only so this should be a reasonable assumption.
903 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
904
905 // We assume that if the texture has mip levels, we either upload to all the levels or just the
906 // first.
907 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
908
909 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
910 GrSRGBEncoded::kNo)));
911
912 if (width == 0 || height == 0) {
913 return false;
914 }
915
916 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
917 return false;
918 }
919
920 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
921
922 SkTArray<size_t> individualMipOffsets(mipLevelCount);
923 individualMipOffsets.push_back(0);
924 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
925 int currentWidth = width;
926 int currentHeight = height;
927 if (!texels[0].fPixels) {
928 return false;
929 }
930
931 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
932 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
933 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
934 currentWidth = SkTMax(1, currentWidth / 2);
935 currentHeight = SkTMax(1, currentHeight / 2);
936
937 if (texels[currentMipLevel].fPixels) {
938 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
939 currentHeight);
940 individualMipOffsets.push_back(combinedBufferSize);
941 combinedBufferSize += dataSize;
942 } else {
943 return false;
944 }
945 }
946 if (0 == combinedBufferSize) {
947 // We don't have any data to upload so fail (compressed textures are read-only).
948 return false;
949 }
950
951 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500952 sk_sp<GrVkTransferBuffer> transferBuffer =
953 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500954 if (!transferBuffer) {
955 return false;
956 }
957
958 int uploadLeft = left;
959 int uploadTop = top;
960 GrVkTexture* uploadTexture = tex;
961
962 char* buffer = (char*)transferBuffer->map();
963 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
964
965 currentWidth = width;
966 currentHeight = height;
967 int layerHeight = uploadTexture->height();
968 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
969 if (texels[currentMipLevel].fPixels) {
970 // Again, we're assuming that our rect is the entire texture
971 SkASSERT(currentHeight == layerHeight);
972 SkASSERT(0 == uploadLeft && 0 == uploadTop);
973
974 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
975 currentHeight);
976
977 // copy data into the buffer, skipping the trailing bytes
978 char* dst = buffer + individualMipOffsets[currentMipLevel];
979 const char* src = (const char*)texels[currentMipLevel].fPixels;
980 memcpy(dst, src, dataSize);
981
982 VkBufferImageCopy& region = regions.push_back();
983 memset(&region, 0, sizeof(VkBufferImageCopy));
984 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
985 region.bufferRowLength = currentWidth;
986 region.bufferImageHeight = currentHeight;
987 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
988 region.imageOffset = { uploadLeft, uploadTop, 0 };
989 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
990 }
991 currentWidth = SkTMax(1, currentWidth / 2);
992 currentHeight = SkTMax(1, currentHeight / 2);
993 layerHeight = currentHeight;
994 }
995
996 // no need to flush non-coherent memory, unmap will do that for us
997 transferBuffer->unmap();
998
999 // Change layout of our target so it can be copied to
1000 uploadTexture->setImageLayout(this,
1001 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1002 VK_ACCESS_TRANSFER_WRITE_BIT,
1003 VK_PIPELINE_STAGE_TRANSFER_BIT,
1004 false);
1005
1006 // Copy the buffer to the image
1007 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -05001008 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -05001009 uploadTexture,
1010 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1011 regions.count(),
1012 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -05001013
1014 if (1 == mipLevelCount) {
1015 tex->texturePriv().markMipMapsDirty();
1016 }
1017
1018 return true;
1019}
1020
Greg Daniel164a9f02016-02-22 09:56:40 -05001021////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001022sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001023 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001024 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1025
1026 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001027 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001028
Greg Daniel164a9f02016-02-22 09:56:40 -05001029 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1030 if (renderTarget) {
1031 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1032 }
1033
1034 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1035 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1036 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001037 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001038 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1039 // texture.
1040 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1041
Greg Daniel164a9f02016-02-22 09:56:40 -05001042 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001043 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001044 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001045 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001046 GrVkImage::ImageDesc imageDesc;
1047 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1048 imageDesc.fFormat = pixelFormat;
1049 imageDesc.fWidth = desc.fWidth;
1050 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001051 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001052 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001053 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001054 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001055 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001056
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001057 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1058 if (mipLevels > 1) {
1059 mipMapsStatus = GrMipMapsStatus::kValid;
1060 for (int i = 0; i < mipLevels; ++i) {
1061 if (!texels[i].fPixels) {
1062 mipMapsStatus = GrMipMapsStatus::kDirty;
1063 break;
1064 }
Greg Daniel834f1202017-10-09 15:06:20 -04001065 }
1066 }
1067
Robert Phillips67d52cf2017-06-05 13:38:13 -04001068 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001069 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001070 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1071 imageDesc,
1072 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001073 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001074 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001075 }
1076
1077 if (!tex) {
1078 return nullptr;
1079 }
1080
Jim Van Verth1676cb92019-01-15 13:24:45 -05001081 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001082 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001083 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001084 bool success;
1085 if (isCompressed) {
1086 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1087 colorType, texels, mipLevelCount);
1088 } else {
1089 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1090 colorType, texels, mipLevelCount);
1091 }
1092 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001093 tex->unref();
1094 return nullptr;
1095 }
1096 }
1097
Jim Van Verth1676cb92019-01-15 13:24:45 -05001098 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001099 VkClearColorValue zeroClearColor;
1100 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1101 VkImageSubresourceRange range;
1102 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1103 range.baseArrayLayer = 0;
1104 range.baseMipLevel = 0;
1105 range.layerCount = 1;
1106 range.levelCount = 1;
1107 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1108 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001109 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001110 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001111 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001112}
1113
1114////////////////////////////////////////////////////////////////////////////////
1115
Greg Daniel6888c0d2017-08-25 11:55:50 -04001116void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1117 VkDeviceSize dstOffset, VkDeviceSize size) {
1118 VkBufferCopy copyRegion;
1119 copyRegion.srcOffset = srcOffset;
1120 copyRegion.dstOffset = dstOffset;
1121 copyRegion.size = size;
1122 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1123}
1124
jvanverthdb379092016-07-07 11:18:46 -07001125bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1126 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001127 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001128 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001129
1130 return true;
1131}
1132
1133////////////////////////////////////////////////////////////////////////////////
1134
Greg Daniel7e000222018-12-03 10:08:21 -05001135static bool check_image_info(const GrVkCaps& caps,
1136 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001137 GrPixelConfig config,
1138 bool isWrappedRT) {
1139 if (VK_NULL_HANDLE == info.fImage) {
1140 return false;
1141 }
1142
1143 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001144 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001145 }
1146
Greg Daniel7e000222018-12-03 10:08:21 -05001147 if (info.fYcbcrConversionInfo.isValid()) {
1148 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1149 return false;
1150 }
jvanverthfd359ca2016-03-18 11:57:24 -07001151 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001152
Greg Danielcb324152019-02-25 11:36:53 -05001153 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1154 return false;
1155 }
1156
Greg Daniel52e16d92018-04-10 09:34:07 -04001157 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001158 return true;
1159}
1160
1161sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001162 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1163 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001164 GrVkImageInfo imageInfo;
1165 if (!backendTex.getVkImageInfo(&imageInfo)) {
1166 return nullptr;
1167 }
1168
Greg Danielcb324152019-02-25 11:36:53 -05001169 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001170 return nullptr;
1171 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001172
Greg Daniel164a9f02016-02-22 09:56:40 -05001173 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001174 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001175 surfDesc.fWidth = backendTex.width();
1176 surfDesc.fHeight = backendTex.height();
1177 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001178 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001179
Greg Daniel52e16d92018-04-10 09:34:07 -04001180 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1181 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001182 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1183 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001184}
1185
1186sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001187 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001188 GrWrapOwnership ownership,
1189 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001190 GrVkImageInfo imageInfo;
1191 if (!backendTex.getVkImageInfo(&imageInfo)) {
1192 return nullptr;
1193 }
1194
Greg Danielcb324152019-02-25 11:36:53 -05001195 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001196 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001197 }
Brian Salomond17f6582017-07-19 18:28:58 -04001198
1199 GrSurfaceDesc surfDesc;
1200 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1201 surfDesc.fWidth = backendTex.width();
1202 surfDesc.fHeight = backendTex.height();
1203 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001204 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001205
Greg Daniel52e16d92018-04-10 09:34:07 -04001206 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1207 SkASSERT(layout);
1208
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001209 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1210 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001211}
1212
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001213sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001214 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1215 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1216 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1217 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001218 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001219 return nullptr;
1220 }
halcanary9d524f22016-03-29 09:03:52 -07001221
Greg Daniel323fbcf2018-04-10 13:46:30 -04001222 GrVkImageInfo info;
1223 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001224 return nullptr;
1225 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001226
Greg Danielcb324152019-02-25 11:36:53 -05001227 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001228 return nullptr;
1229 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001230
Greg Daniel164a9f02016-02-22 09:56:40 -05001231 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001232 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001233 desc.fWidth = backendRT.width();
1234 desc.fHeight = backendRT.height();
1235 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001236 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001237
Greg Daniel323fbcf2018-04-10 13:46:30 -04001238 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001239
Greg Daniel323fbcf2018-04-10 13:46:30 -04001240 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001241 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001242
1243 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1244 SkASSERT(!backendRT.stencilBits());
1245 if (tgt) {
1246 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001247 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001248
Ben Wagnerff134f22018-04-24 16:29:16 -04001249 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001250}
1251
Greg Daniel7ef28f32017-04-20 16:41:55 +00001252sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001253 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001254
Greg Daniel52e16d92018-04-10 09:34:07 -04001255 GrVkImageInfo imageInfo;
1256 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001257 return nullptr;
1258 }
Greg Danielcb324152019-02-25 11:36:53 -05001259 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001260 return nullptr;
1261 }
1262
Greg Danielcb324152019-02-25 11:36:53 -05001263
Brian Osman33910292017-04-18 14:38:53 -04001264 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001265 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001266 desc.fWidth = tex.width();
1267 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001268 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001269 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1270 if (!desc.fSampleCnt) {
1271 return nullptr;
1272 }
Brian Osman33910292017-04-18 14:38:53 -04001273
Greg Daniel52e16d92018-04-10 09:34:07 -04001274 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1275 SkASSERT(layout);
1276
Ben Wagnerff134f22018-04-24 16:29:16 -04001277 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001278}
1279
Greg Danielb46add82019-01-02 14:51:29 -05001280sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1281 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1282 int maxSize = this->caps()->maxTextureSize();
1283 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1284 return nullptr;
1285 }
1286
1287 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1288 if (!backendFormat.isValid()) {
1289 return nullptr;
1290 }
1291 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1292 imageInfo.colorType());
1293 if (config == kUnknown_GrPixelConfig) {
1294 return nullptr;
1295 }
1296
1297 GrSurfaceDesc desc;
1298 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1299 desc.fWidth = imageInfo.width();
1300 desc.fHeight = imageInfo.height();
1301 desc.fConfig = config;
1302 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1303 if (!desc.fSampleCnt) {
1304 return nullptr;
1305 }
1306
1307 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1308}
1309
Brian Salomon930f9392018-06-20 16:25:26 -04001310bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1311 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001312 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001313 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001314 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001315 return false;
jvanverth62340062016-04-26 08:01:44 -07001316 }
1317
jvanverth62340062016-04-26 08:01:44 -07001318 // determine if we can blit to and from this format
1319 const GrVkCaps& caps = this->vkCaps();
1320 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001321 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1322 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001323 return false;
jvanverth62340062016-04-26 08:01:44 -07001324 }
1325
egdaniel7ac5da82016-07-15 13:41:42 -07001326 int width = tex->width();
1327 int height = tex->height();
1328 VkImageBlit blitRegion;
1329 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001330
jvanverth82c05582016-05-03 11:19:01 -07001331 // SkMipMap doesn't include the base level in the level count so we have to add 1
1332 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001333 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001334
Greg Danielda86e282018-06-13 09:41:19 -04001335 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001336 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1337 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001338
jvanverth50c46c72016-05-06 12:31:28 -07001339 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001340 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001341 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1342 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001343 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1344 nullptr, // pNext
1345 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1346 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1347 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1348 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1349 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1350 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1351 vkTex->image(), // image
1352 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001353 };
1354
jvanverth62340062016-04-26 08:01:44 -07001355 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001356 uint32_t mipLevel = 1;
1357 while (mipLevel < levelCount) {
1358 int prevWidth = width;
1359 int prevHeight = height;
1360 width = SkTMax(1, width / 2);
1361 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001362
jvanverth50c46c72016-05-06 12:31:28 -07001363 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001364 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1365 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001366
1367 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001368 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001369 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001370 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1371 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001372 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001373 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001374 vkTex->resource(),
1375 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001376 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001377 vkTex->resource(),
1378 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001379 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001380 1,
1381 &blitRegion,
1382 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001383 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001384 }
Greg Danielee54f232019-04-03 14:58:40 -04001385 if (levelCount > 1) {
1386 // This barrier logically is not needed, but it changes the final level to the same layout
1387 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1388 // layouts and future layout changes easier. The alternative here would be to track layout
1389 // and memory accesses per layer which doesn't seem work it.
1390 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1391 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1392 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1393 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1394 }
Brian Salomon930f9392018-06-20 16:25:26 -04001395 return true;
jvanverth62340062016-04-26 08:01:44 -07001396}
1397
Greg Daniel164a9f02016-02-22 09:56:40 -05001398////////////////////////////////////////////////////////////////////////////////
1399
1400GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1401 int width,
1402 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001403 SkASSERT(width >= rt->width());
1404 SkASSERT(height >= rt->height());
1405
1406 int samples = rt->numStencilSamples();
1407
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001408 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001409
1410 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001411 width,
1412 height,
1413 samples,
1414 sFmt));
1415 fStats.incStencilAttachmentCreates();
1416 return stencil;
1417}
1418
1419////////////////////////////////////////////////////////////////////////////////
1420
Brian Salomon52e943a2018-03-13 09:32:39 -04001421bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001422 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1423 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001424 VkDeviceSize size = dstRowBytes * h;
1425 VkDeviceSize offset = bufferOffset;
1426 SkASSERT(size + offset <= alloc.fSize);
1427 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1428 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001429 return false;
1430 }
Greg Daniel81df0412018-05-31 13:13:33 -04001431 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001432
Greg Daniel20ece3a2017-03-28 10:24:43 -04001433 if (srcData) {
1434 // If there is no padding on dst we can do a single memcopy.
1435 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001436 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001437 } else {
1438 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1439 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001440 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001441 }
Greg Daniel81df0412018-05-31 13:13:33 -04001442 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1443 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001444 return true;
1445}
1446
Brian Salomonf865b052018-03-09 09:01:53 -05001447#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001448bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1449 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001450 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001451 SkASSERT(texturable || renderable);
1452 if (!texturable) {
1453 SkASSERT(GrMipMapped::kNo == mipMapped);
1454 SkASSERT(!srcData);
1455 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001456 VkFormat pixelFormat;
1457 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001458 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001459 }
1460
Brian Salomon52e943a2018-03-13 09:32:39 -04001461 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1462 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001463 }
1464
Brian Salomon52e943a2018-03-13 09:32:39 -04001465 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1466 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001467 }
1468
1469 // Currently we don't support uploading pixel data when mipped.
1470 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001471 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001472 }
1473
Brian Salomon52e943a2018-03-13 09:32:39 -04001474 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001475 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1476 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001477 if (texturable) {
1478 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1479 }
1480 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001481 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1482 }
1483
1484 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001485 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001486 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001487
1488 // Create Image
1489 VkSampleCountFlagBits vkSamples;
1490 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001491 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001492 }
1493
1494 // Figure out the number of mip levels.
1495 uint32_t mipLevels = 1;
1496 if (GrMipMapped::kYes == mipMapped) {
1497 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1498 }
1499
1500 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001501 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1502 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001503 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001504 VK_IMAGE_TYPE_2D, // VkImageType
1505 pixelFormat, // VkFormat
1506 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1507 mipLevels, // mipLevels
1508 1, // arrayLayers
1509 vkSamples, // samples
1510 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1511 usageFlags, // VkImageUsageFlags
1512 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1513 0, // queueFamilyCount
1514 0, // pQueueFamilyIndices
1515 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001516 };
1517
Brian Salomon52e943a2018-03-13 09:32:39 -04001518 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1519 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001520
Brian Salomonde9f5462018-03-07 14:23:58 -05001521 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001522 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001523 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001524 }
1525
1526 // 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 -05001527 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001528 VkBuffer buffer = VK_NULL_HANDLE;
1529
1530 VkResult err;
1531 const VkCommandBufferAllocateInfo cmdInfo = {
1532 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1533 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001534 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001535 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1536 1 // bufferCount
1537 };
1538
1539 VkCommandBuffer cmdBuffer;
1540 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1541 if (err) {
1542 GrVkMemory::FreeImageMemory(this, false, alloc);
1543 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001544 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001545 }
1546
1547 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1548 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1549 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1550 cmdBufferBeginInfo.pNext = nullptr;
1551 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1552 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1553
1554 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1555 SkASSERT(!err);
1556
1557 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001558 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001559
Robert Phillips646f6372018-09-25 09:31:10 -04001560 const size_t trimRowBytes = w * bpp;
1561 if (!srcRowBytes) {
1562 srcRowBytes = trimRowBytes;
1563 }
1564
Brian Salomonde9f5462018-03-07 14:23:58 -05001565 SkTArray<size_t> individualMipOffsets(mipLevels);
1566 individualMipOffsets.push_back(0);
1567 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001568 if (GrPixelConfigIsCompressed(config)) {
1569 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1570 bpp = 4; // we have at least this alignment, which will pass the code below
1571 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001572 int currentWidth = w;
1573 int currentHeight = h;
1574 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1575 // config. This works with the assumption that the bytes in pixel config is always a power
1576 // of 2.
1577 SkASSERT((bpp & (bpp - 1)) == 0);
1578 const size_t alignmentMask = 0x3 | (bpp - 1);
1579 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1580 currentWidth = SkTMax(1, currentWidth / 2);
1581 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001582
Jim Van Verth1676cb92019-01-15 13:24:45 -05001583 size_t trimmedSize;
1584 if (GrPixelConfigIsCompressed(config)) {
1585 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1586 } else {
1587 trimmedSize = currentWidth * bpp * currentHeight;
1588 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001589 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1590 if (alignmentDiff != 0) {
1591 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001592 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001593 individualMipOffsets.push_back(combinedBufferSize);
1594 combinedBufferSize += trimmedSize;
1595 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001596
Brian Salomonde9f5462018-03-07 14:23:58 -05001597 VkBufferCreateInfo bufInfo;
1598 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1599 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1600 bufInfo.flags = 0;
1601 bufInfo.size = combinedBufferSize;
1602 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1603 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1604 bufInfo.queueFamilyIndexCount = 0;
1605 bufInfo.pQueueFamilyIndices = nullptr;
1606 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001607
Brian Salomonde9f5462018-03-07 14:23:58 -05001608 if (err) {
1609 GrVkMemory::FreeImageMemory(this, false, alloc);
1610 VK_CALL(DestroyImage(fDevice, image, nullptr));
1611 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001612 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001613 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001614 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001615
Brian Salomonde9f5462018-03-07 14:23:58 -05001616 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1617 &bufferAlloc)) {
1618 GrVkMemory::FreeImageMemory(this, false, alloc);
1619 VK_CALL(DestroyImage(fDevice, image, nullptr));
1620 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1621 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001622 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001623 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001624 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001625
Brian Salomonde9f5462018-03-07 14:23:58 -05001626 currentWidth = w;
1627 currentHeight = h;
1628 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1629 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001630 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001631 bool result;
1632 if (GrPixelConfigIsCompressed(config)) {
1633 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1634 size_t currentRowBytes = levelSize / currentHeight;
1635 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1636 currentRowBytes, currentRowBytes, currentHeight);
1637 } else {
1638 size_t currentRowBytes = bpp * currentWidth;
1639 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1640 currentRowBytes, trimRowBytes, currentHeight);
1641 }
1642 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001643 GrVkMemory::FreeImageMemory(this, false, alloc);
1644 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001645 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001646 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1647 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001648 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001649 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001650 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001651 currentWidth = SkTMax(1, currentWidth / 2);
1652 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001653 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001654
1655 // Set image layout and add barrier
1656 VkImageMemoryBarrier barrier;
1657 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1658 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1659 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001660 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001661 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1662 barrier.oldLayout = initialLayout;
1663 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1664 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1665 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1666 barrier.image = image;
1667 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1668
Greg Danielf7828d02018-10-09 12:01:32 -04001669 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001670 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1671 &barrier));
1672 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1673
1674 SkTArray<VkBufferImageCopy> regions(mipLevels);
1675
1676 currentWidth = w;
1677 currentHeight = h;
1678 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1679 // Submit copy command
1680 VkBufferImageCopy& region = regions.push_back();
1681 memset(&region, 0, sizeof(VkBufferImageCopy));
1682 region.bufferOffset = individualMipOffsets[currentMipLevel];
1683 region.bufferRowLength = currentWidth;
1684 region.bufferImageHeight = currentHeight;
1685 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1686 region.imageOffset = {0, 0, 0};
1687 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1688 currentWidth = SkTMax(1, currentWidth / 2);
1689 currentHeight = SkTMax(1, currentHeight / 2);
1690 }
1691
1692 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1693 regions.begin()));
1694
Brian Salomon52e943a2018-03-13 09:32:39 -04001695 if (texturable) {
1696 // Change Image layout to shader read since if we use this texture as a borrowed textures
1697 // within Ganesh we require that its layout be set to that
1698 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1699 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1700 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001701 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001702 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1703 barrier.oldLayout = initialLayout;
1704 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1705 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1706 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1707 barrier.image = image;
1708 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001709 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001710 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1711 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001712 0,
1713 0, nullptr,
1714 0, nullptr,
1715 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001716 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001717 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001718
1719 // End CommandBuffer
1720 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1721 SkASSERT(!err);
1722
1723 // Create Fence for queue
1724 VkFence fence;
1725 VkFenceCreateInfo fenceInfo;
1726 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1727 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1728
1729 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1730 SkASSERT(!err);
1731
1732 VkSubmitInfo submitInfo;
1733 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1734 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1735 submitInfo.pNext = nullptr;
1736 submitInfo.waitSemaphoreCount = 0;
1737 submitInfo.pWaitSemaphores = nullptr;
1738 submitInfo.pWaitDstStageMask = 0;
1739 submitInfo.commandBufferCount = 1;
1740 submitInfo.pCommandBuffers = &cmdBuffer;
1741 submitInfo.signalSemaphoreCount = 0;
1742 submitInfo.pSignalSemaphores = nullptr;
1743 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1744 SkASSERT(!err);
1745
1746 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1747 if (VK_TIMEOUT == err) {
1748 GrVkMemory::FreeImageMemory(this, false, alloc);
1749 VK_CALL(DestroyImage(fDevice, image, nullptr));
1750 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1751 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001752 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001753 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1754 SkDebugf("Fence failed to signal: %d\n", err);
1755 SK_ABORT("failing");
1756 }
1757 SkASSERT(!err);
1758
1759 // Clean up transfer resources
1760 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1761 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1762 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1763 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001764 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001765 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1766
Brian Salomon52e943a2018-03-13 09:32:39 -04001767 info->fImage = image;
1768 info->fAlloc = alloc;
1769 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1770 info->fImageLayout = initialLayout;
1771 info->fFormat = pixelFormat;
1772 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001773
Brian Salomon52e943a2018-03-13 09:32:39 -04001774 return true;
1775}
1776
1777GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001778 GrColorType colorType,
1779 bool isRenderTarget,
1780 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001781 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001782
1783 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1784 return GrBackendTexture();
1785 }
1786
Robert Phillips646f6372018-09-25 09:31:10 -04001787 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1788 if (!this->caps()->isConfigTexturable(config)) {
1789 return GrBackendTexture();
1790 }
1791
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001792 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001793 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001794 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001795 return {};
1796 }
Greg Daniel108bb232018-07-03 16:18:29 -04001797 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1798 // Lots of tests don't go through Skia's public interface which will set the config so for
1799 // testing we make sure we set a config here.
1800 beTex.setPixelConfig(config);
1801 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001802}
1803
1804bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001805 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001806
Greg Daniel52e16d92018-04-10 09:34:07 -04001807 GrVkImageInfo backend;
1808 if (!tex.getVkImageInfo(&backend)) {
1809 return false;
1810 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001811
Greg Daniel52e16d92018-04-10 09:34:07 -04001812 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001813 VkMemoryRequirements req;
1814 memset(&req, 0, sizeof(req));
1815 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001816 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001817 &req));
1818 // TODO: find a better check
1819 // This will probably fail with a different driver
1820 return (req.size > 0) && (req.size <= 8192 * 8192);
1821 }
1822
1823 return false;
1824}
1825
Brian Salomon26102cb2018-03-09 09:33:19 -05001826void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001827 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001828
Greg Daniel52e16d92018-04-10 09:34:07 -04001829 GrVkImageInfo info;
1830 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001831 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001832 }
1833}
1834
Brian Osman2d010b62018-08-09 10:55:09 -04001835GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001836 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1837 return GrBackendRenderTarget();
1838 }
1839
Brian Salomon8a375832018-03-14 10:21:40 -04001840 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001841 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001842 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001843 if (kUnknown_GrPixelConfig == config) {
1844 return {};
1845 }
Robert Phillips646f6372018-09-25 09:31:10 -04001846 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001847 &info)) {
1848 return {};
1849 }
Greg Daniel108bb232018-07-03 16:18:29 -04001850 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1851 // Lots of tests don't go through Skia's public interface which will set the config so for
1852 // testing we make sure we set a config here.
1853 beRT.setPixelConfig(config);
1854 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001855}
1856
Brian Salomon52e943a2018-03-13 09:32:39 -04001857void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001858 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001859
Greg Daniel323fbcf2018-04-10 13:46:30 -04001860 GrVkImageInfo info;
1861 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001862 // something in the command buffer may still be using this, so force submit
1863 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001864 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001865 }
1866}
Brian Salomonf865b052018-03-09 09:01:53 -05001867
Greg Daniel26b50a42018-03-08 09:49:58 -05001868void GrVkGpu::testingOnly_flushGpuAndSync() {
1869 this->submitCommandBuffer(kForce_SyncQueue);
1870}
Brian Salomonf865b052018-03-09 09:01:53 -05001871#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001872
Greg Daniel164a9f02016-02-22 09:56:40 -05001873////////////////////////////////////////////////////////////////////////////////
1874
Greg Daniel59dc1482019-02-22 10:46:38 -05001875void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1876 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001877 VkPipelineStageFlags dstStageMask,
1878 bool byRegion,
1879 VkBufferMemoryBarrier* barrier) const {
1880 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001881 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001882 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001883 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001884 srcStageMask,
1885 dstStageMask,
1886 byRegion,
1887 GrVkCommandBuffer::kBufferMemory_BarrierType,
1888 barrier);
1889}
1890
Greg Daniel59dc1482019-02-22 10:46:38 -05001891void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1892 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001893 VkPipelineStageFlags dstStageMask,
1894 bool byRegion,
1895 VkImageMemoryBarrier* barrier) const {
1896 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001897 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001898 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001899 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001900 srcStageMask,
1901 dstStageMask,
1902 byRegion,
1903 GrVkCommandBuffer::kImageMemory_BarrierType,
1904 barrier);
1905}
1906
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001907void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04001908 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
1909 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001910 SkASSERT(n >= 0);
1911 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00001912 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1913 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001914 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05001915 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001916 for (int i = 0; i < n; ++i) {
1917 SkASSERT(proxies[i]->isInstantiated());
1918 if (GrTexture* tex = proxies[i]->peekTexture()) {
1919 image = static_cast<GrVkTexture*>(tex);
1920 } else {
1921 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1922 SkASSERT(rt);
1923 image = static_cast<GrVkRenderTarget*>(rt);
1924 }
1925 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05001926 }
Greg Danielbae71212019-03-01 15:24:35 -05001927 }
Greg Daniel797efca2019-05-09 14:04:20 -04001928
1929 // Handle requests for preparing for external IO
1930 for (int i = 0; i < externalRequests.fNumImages; ++i) {
1931 SkImage* image = externalRequests.fImages[i];
1932 if (!image->isTextureBacked()) {
1933 continue;
1934 }
1935 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
1936 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
1937 SkASSERT(proxy);
1938
1939 if (!proxy->isInstantiated()) {
1940 auto resourceProvider = this->getContext()->priv().resourceProvider();
1941 if (!proxy->instantiate(resourceProvider)) {
1942 continue;
1943 }
1944 }
1945
1946 GrTexture* tex = proxy->peekTexture();
1947 if (!tex) {
1948 continue;
1949 }
1950 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
1951 vkTex->prepareForExternal(this);
1952 }
1953 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
1954 SkSurface* surface = externalRequests.fSurfaces[i];
1955 if (!surface->getCanvas()->getGrContext()) {
1956 continue;
1957 }
1958 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
1959 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
1960 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
1961 if (!proxy->isInstantiated()) {
1962 auto resourceProvider = this->getContext()->priv().resourceProvider();
1963 if (!proxy->instantiate(resourceProvider)) {
1964 continue;
1965 }
1966 }
1967
1968 GrRenderTarget* rt = proxy->peekRenderTarget();
1969 SkASSERT(rt);
1970 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
1971 if (externalRequests.fPrepareSurfaceForPresent &&
1972 externalRequests.fPrepareSurfaceForPresent[i]) {
1973 vkRT->prepareForPresent(this);
1974 } else {
1975 vkRT->prepareForExternal(this);
1976 }
1977 }
1978
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001979 if (info.fFlags & kSyncCpu_GrFlushFlag) {
1980 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001981 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001982 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001983 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001984}
1985
Greg Daniel25af6712018-04-25 10:44:38 -04001986static int get_surface_sample_cnt(GrSurface* surf) {
1987 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1988 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001989 }
Greg Daniel25af6712018-04-25 10:44:38 -04001990 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001991}
1992
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001993void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1994 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001995 GrVkImage* dstImage,
1996 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001997 const SkIRect& srcRect,
1998 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001999#ifdef SK_DEBUG
2000 int dstSampleCnt = get_surface_sample_cnt(dst);
2001 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002002 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2003 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2004 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
2005 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04002006
2007#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05002008
Greg Daniel164a9f02016-02-22 09:56:40 -05002009 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
2010 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002011 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002012 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2013 VK_ACCESS_TRANSFER_WRITE_BIT,
2014 VK_PIPELINE_STAGE_TRANSFER_BIT,
2015 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002016
egdaniel17b89252016-04-05 07:23:38 -07002017 srcImage->setImageLayout(this,
2018 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002019 VK_ACCESS_TRANSFER_READ_BIT,
2020 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002021 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002022
2023 // Flip rect if necessary
2024 SkIRect srcVkRect = srcRect;
2025 int32_t dstY = dstPoint.fY;
2026
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002027 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2028 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05002029 srcVkRect.fTop = src->height() - srcRect.fBottom;
2030 srcVkRect.fBottom = src->height() - srcRect.fTop;
2031 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
2032 }
2033
2034 VkImageCopy copyRegion;
2035 memset(&copyRegion, 0, sizeof(VkImageCopy));
2036 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2037 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
2038 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2039 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07002040 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002041
2042 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002043 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002044 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002045 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002046 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2047 1,
2048 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002049
2050 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2051 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002052 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002053}
2054
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002055void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2056 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002057 GrVkImage* dstImage,
2058 GrVkImage* srcImage,
2059 const SkIRect& srcRect,
2060 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002061#ifdef SK_DEBUG
2062 int dstSampleCnt = get_surface_sample_cnt(dst);
2063 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002064 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2065 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04002066 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002067 dstHasYcbcr, src->config(), srcSampleCnt,
2068 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002069
Greg Daniel25af6712018-04-25 10:44:38 -04002070#endif
egdaniel17b89252016-04-05 07:23:38 -07002071 dstImage->setImageLayout(this,
2072 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002073 VK_ACCESS_TRANSFER_WRITE_BIT,
2074 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002075 false);
2076
egdaniel17b89252016-04-05 07:23:38 -07002077 srcImage->setImageLayout(this,
2078 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002079 VK_ACCESS_TRANSFER_READ_BIT,
2080 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002081 false);
2082
2083 // Flip rect if necessary
2084 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002085 srcVkRect.fLeft = srcRect.fLeft;
2086 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002087 SkIRect dstRect;
2088 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002089 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002090
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002091 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002092 srcVkRect.fTop = src->height() - srcRect.fBottom;
2093 srcVkRect.fBottom = src->height() - srcRect.fTop;
2094 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002095 srcVkRect.fTop = srcRect.fTop;
2096 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002097 }
2098
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002099 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002100 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2101 } else {
2102 dstRect.fTop = dstPoint.fY;
2103 }
2104 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2105
2106 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2107 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002108 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002109 using std::swap;
2110 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002111 }
2112
2113 VkImageBlit blitRegion;
2114 memset(&blitRegion, 0, sizeof(VkImageBlit));
2115 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2116 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002117 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002118 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2119 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002120 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002121
2122 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002123 *srcImage,
2124 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002125 1,
2126 &blitRegion,
2127 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002128
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002129 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002130 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002131}
2132
Brian Salomon1fabd512018-02-09 09:54:25 -05002133void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2134 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2135 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002136 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002137 SkIRect srcRect = origSrcRect;
2138 SkIPoint dstPoint = origDstPoint;
2139 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2140 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2141 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2142 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2143 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2144 }
2145 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002146 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2147 srcRect.width(), srcRect.height());
2148 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002149}
2150
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002151bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2152 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002153 const SkIRect& srcRect, const SkIPoint& dstPoint,
2154 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002155#ifdef SK_DEBUG
2156 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2157 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2158 }
2159 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2160 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2161 }
2162#endif
2163
Greg Daniel25af6712018-04-25 10:44:38 -04002164 GrPixelConfig dstConfig = dst->config();
2165 GrPixelConfig srcConfig = src->config();
2166
2167 int dstSampleCnt = get_surface_sample_cnt(dst);
2168 int srcSampleCnt = get_surface_sample_cnt(src);
2169
egdaniel17b89252016-04-05 07:23:38 -07002170 GrVkImage* dstImage;
2171 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002172 GrRenderTarget* dstRT = dst->asRenderTarget();
2173 if (dstRT) {
2174 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002175 if (vkRT->wrapsSecondaryCommandBuffer()) {
2176 return false;
2177 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002178 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2179 } else {
2180 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002181 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002182 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002183 GrRenderTarget* srcRT = src->asRenderTarget();
2184 if (srcRT) {
2185 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2186 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002187 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002188 SkASSERT(src->asTexture());
2189 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002190 }
2191
Greg Daniela51e93c2019-03-25 12:30:45 -04002192 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2193 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2194
2195 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2196 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2197 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2198 return true;
2199 }
2200
2201 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2202 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2203 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2204 dstPoint, canDiscardOutsideDstRect));
2205 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2206 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2207 return true;
2208 }
2209
2210 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2211 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002212 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2213 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002214 return true;
2215 }
2216
Greg Daniel25af6712018-04-25 10:44:38 -04002217 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002218 dstHasYcbcr, srcConfig, srcSampleCnt,
2219 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002220 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2221 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002222 return true;
2223 }
2224
Greg Daniel164a9f02016-02-22 09:56:40 -05002225 return false;
2226}
2227
Brian Salomona6948702018-06-01 15:33:20 -04002228bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2229 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002230 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002231 return false;
2232 }
2233
egdaniel66933552016-08-24 07:22:19 -07002234 GrVkImage* image = nullptr;
2235 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2236 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002237 // Reading from render targets that wrap a secondary command buffer is not allowed since
2238 // it would require us to know the VkImage, which we don't have, as well as need us to
2239 // stop and start the VkRenderPass which we don't have access to.
2240 if (rt->wrapsSecondaryCommandBuffer()) {
2241 return false;
2242 }
egdaniel66933552016-08-24 07:22:19 -07002243 // resolve the render target if necessary
2244 switch (rt->getResolveType()) {
2245 case GrVkRenderTarget::kCantResolve_ResolveType:
2246 return false;
2247 case GrVkRenderTarget::kAutoResolves_ResolveType:
2248 break;
2249 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002250 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002251 break;
2252 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002253 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002254 }
2255 image = rt;
2256 } else {
2257 image = static_cast<GrVkTexture*>(surface->asTexture());
2258 }
2259
2260 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002261 return false;
2262 }
2263
Greg Daniel475eb702018-09-28 14:16:50 -04002264 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2265 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2266 // image and then do the read pixels from that.
2267 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002268 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2269 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002270
2271 // Make a new surface that is RGBA to copy the RGB surface into.
2272 GrSurfaceDesc surfDesc;
2273 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2274 surfDesc.fWidth = width;
2275 surfDesc.fHeight = height;
2276 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2277 surfDesc.fSampleCnt = 1;
2278
2279 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2280 VK_IMAGE_USAGE_SAMPLED_BIT |
2281 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2282 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2283
2284 GrVkImage::ImageDesc imageDesc;
2285 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2286 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2287 imageDesc.fWidth = width;
2288 imageDesc.fHeight = height;
2289 imageDesc.fLevels = 1;
2290 imageDesc.fSamples = 1;
2291 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2292 imageDesc.fUsageFlags = usageFlags;
2293 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2294
2295 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2296 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2297 if (!copySurface) {
2298 return false;
2299 }
2300
2301 int srcSampleCount = 0;
2302 if (rt) {
2303 srcSampleCount = rt->numColorSamples();
2304 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002305 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel5c7b5412019-05-10 11:39:55 -04002306 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, false, false,
2307 surface->config(), srcSampleCount, image->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002308 srcHasYcbcr) &&
2309 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2310 surface->config(), SkToBool(surface->asTexture()),
2311 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002312 return false;
2313 }
2314 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
Greg Daniel5c7b5412019-05-10 11:39:55 -04002315 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniel475eb702018-09-28 14:16:50 -04002316 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2317 srcRect, SkIPoint::Make(0,0))) {
2318 return false;
2319 }
2320 top = 0;
2321 left = 0;
2322 dstColorType = GrColorType::kRGBA_8888;
2323 image = copySurface.get();
2324 }
2325
Greg Daniel164a9f02016-02-22 09:56:40 -05002326 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002327 image->setImageLayout(this,
2328 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2329 VK_ACCESS_TRANSFER_READ_BIT,
2330 VK_PIPELINE_STAGE_TRANSFER_BIT,
2331 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002332
Brian Salomonc320b152018-02-20 14:05:36 -05002333 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002334 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002335
Greg Daniel164a9f02016-02-22 09:56:40 -05002336 VkBufferImageCopy region;
2337 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002338
2339 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2340 if (copyFromOrigin) {
2341 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002342 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002343 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002344 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002345 region.imageOffset = offset;
2346 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2347 }
2348
2349 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002350 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002351 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002352 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002353 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002354 kStream_GrAccessPattern)
2355 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002356
2357 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002358 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002359 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002360 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2361 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002362
2363 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002364 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002365 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002366 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002367 1,
2368 &region);
2369
2370 // make sure the copy to buffer has finished
2371 transferBuffer->addMemoryBarrier(this,
2372 VK_ACCESS_TRANSFER_WRITE_BIT,
2373 VK_ACCESS_HOST_READ_BIT,
2374 VK_PIPELINE_STAGE_TRANSFER_BIT,
2375 VK_PIPELINE_STAGE_HOST_BIT,
2376 false);
2377
2378 // We need to submit the current command buffer to the Queue and make sure it finishes before
2379 // we can copy the data out of the buffer.
2380 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002381 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002382 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002383 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002384
egdaniel6fa0a912016-09-12 11:51:29 -07002385 if (copyFromOrigin) {
2386 uint32_t skipRows = region.imageExtent.height - height;
2387 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2388 }
2389
Brian Salomona6948702018-06-01 15:33:20 -04002390 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002391
2392 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002393 return true;
2394}
egdaniel066df7c2016-06-08 14:02:27 -07002395
egdaniel27bb2842016-07-07 11:58:35 -07002396// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2397// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2398// the the entire attachment. Similar requirements for the y and height components.
2399void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2400 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2401 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002402 if ((0 != granularity.width && 1 != granularity.width)) {
2403 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2404 int rightAdj = srcBounds.fRight % granularity.width;
2405 if (rightAdj != 0) {
2406 rightAdj = granularity.width - rightAdj;
2407 }
2408 dstBounds->fRight = srcBounds.fRight + rightAdj;
2409 if (dstBounds->fRight > maxWidth) {
2410 dstBounds->fRight = maxWidth;
2411 dstBounds->fLeft = 0;
2412 } else {
2413 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2414 }
egdaniel27bb2842016-07-07 11:58:35 -07002415 } else {
egdanield5797b32016-09-20 12:57:45 -07002416 dstBounds->fLeft = srcBounds.fLeft;
2417 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002418 }
2419
2420 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002421 if ((0 != granularity.height && 1 != granularity.height)) {
2422 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2423 int bottomAdj = srcBounds.fBottom % granularity.height;
2424 if (bottomAdj != 0) {
2425 bottomAdj = granularity.height - bottomAdj;
2426 }
2427 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2428 if (dstBounds->fBottom > maxHeight) {
2429 dstBounds->fBottom = maxHeight;
2430 dstBounds->fTop = 0;
2431 } else {
2432 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2433 }
egdaniel27bb2842016-07-07 11:58:35 -07002434 } else {
egdanield5797b32016-09-20 12:57:45 -07002435 dstBounds->fTop = srcBounds.fTop;
2436 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002437 }
2438}
2439
Greg Daniel22bc8652017-03-22 15:45:43 -04002440void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002441 const GrVkRenderPass* renderPass,
2442 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002443 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002444 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002445 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002446 const SkIRect* pBounds = &bounds;
2447 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002448 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002449 flippedBounds = bounds;
2450 flippedBounds.fTop = target->height() - bounds.fBottom;
2451 flippedBounds.fBottom = target->height() - bounds.fTop;
2452 pBounds = &flippedBounds;
2453 }
2454
egdaniel27bb2842016-07-07 11:58:35 -07002455 // The bounds we use for the render pass should be of the granularity supported
2456 // by the device.
2457 const VkExtent2D& granularity = renderPass->granularity();
2458 SkIRect adjustedBounds;
2459 if ((0 != granularity.width && 1 != granularity.width) ||
2460 (0 != granularity.height && 1 != granularity.height)) {
2461 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2462 target->width(), target->height());
2463 pBounds = &adjustedBounds;
2464 }
2465
Robert Phillips95214472017-08-08 18:00:03 -04002466#ifdef SK_DEBUG
2467 uint32_t index;
2468 bool result = renderPass->colorAttachmentIndex(&index);
2469 SkASSERT(result && 0 == index);
2470 result = renderPass->stencilAttachmentIndex(&index);
2471 if (result) {
2472 SkASSERT(1 == index);
2473 }
2474#endif
2475 VkClearValue clears[2];
2476 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002477 clears[1].depthStencil.depth = 0.0f;
2478 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002479
2480 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002481 for (int i = 0; i < buffers.count(); ++i) {
2482 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2483 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002484 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002485
Brian Salomon1fabd512018-02-09 09:54:25 -05002486 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002487}
egdaniel9cb63402016-06-23 08:37:05 -07002488
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002489void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2490 if (buffer->asRTCommandBuffer()) {
2491 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2492
2493 fCachedRTCommandBuffer->submit();
2494 fCachedRTCommandBuffer->reset();
2495 } else {
2496 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2497
2498 fCachedTexCommandBuffer->submit();
2499 fCachedTexCommandBuffer->reset();
2500 }
2501}
2502
Greg Daniel6be35232017-03-01 17:01:09 -05002503GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002504 VkFenceCreateInfo createInfo;
2505 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2506 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2507 createInfo.pNext = nullptr;
2508 createInfo.flags = 0;
2509 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002510
2511 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2512 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2513
2514 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002515 return (GrFence)fence;
2516}
2517
Greg Daniel6be35232017-03-01 17:01:09 -05002518bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2519 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2520
2521 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002522 return (VK_SUCCESS == result);
2523}
2524
2525void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002526 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2527}
2528
Greg Daniela5cb7812017-06-16 09:45:32 -04002529sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2530 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002531}
2532
Greg Daniel48661b82018-01-22 16:11:35 -05002533sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2534 GrResourceProvider::SemaphoreWrapType wrapType,
2535 GrWrapOwnership ownership) {
2536 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002537}
2538
Greg Daniel858e12c2018-12-06 11:11:37 -05002539void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002540 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2541
Greg Daniel48661b82018-01-22 16:11:35 -05002542 GrVkSemaphore::Resource* resource = vkSem->getResource();
2543 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002544 resource->ref();
2545 fSemaphoresToSignal.push_back(resource);
2546 }
Greg Daniel6be35232017-03-01 17:01:09 -05002547}
2548
Greg Daniel48661b82018-01-22 16:11:35 -05002549void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002550 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2551
Greg Daniel48661b82018-01-22 16:11:35 -05002552 GrVkSemaphore::Resource* resource = vkSem->getResource();
2553 if (resource->shouldWait()) {
2554 resource->ref();
2555 fSemaphoresToWaitOn.push_back(resource);
2556 }
jvanverth84741b32016-09-30 08:39:02 -07002557}
Brian Osman13dddce2017-05-09 13:19:50 -04002558
2559sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2560 SkASSERT(texture);
2561 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2562 vkTexture->setImageLayout(this,
2563 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2564 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002565 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002566 false);
2567 this->submitCommandBuffer(kSkip_SyncQueue);
2568
2569 // The image layout change serves as a barrier, so no semaphore is needed
2570 return nullptr;
2571}
Greg Danielf5d87582017-12-18 14:48:15 -05002572
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002573void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2574 fDrawables.emplace_back(std::move(drawable));
2575}
2576
Greg Daniel7a82edf2018-12-04 10:54:34 -05002577uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2578 const GrBackendFormat& format) {
2579 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2580 SkASSERT(ycbcrInfo);
2581 if (!ycbcrInfo->isValid()) {
2582 return 0;
2583 }
2584
2585 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2586 samplerState, *ycbcrInfo);
2587
2588 return sampler->uniqueID();
2589}
2590
Greg Daniela870b462019-01-08 15:49:46 -05002591void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002592 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002593 this->resourceProvider().storePipelineCacheData();
2594 }
2595}