blob: 5aca28c17d1b807429b34d5ae40b7af02926130f [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 }
812 uploadTexture = copyTexture.get();
813 uploadLeft = 0;
814 uploadTop = 0;
815 }
816
jvanverth900bd4a2016-04-29 13:53:12 -0700817 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400818 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700819
jvanverthc578b0632016-05-02 10:58:12 -0700820 currentWidth = width;
821 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400822 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400823 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400824 if (texelsShallowCopy[currentMipLevel].fPixels) {
825 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
826 const size_t trimRowBytes = currentWidth * bpp;
827 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
828 ? texelsShallowCopy[currentMipLevel].fRowBytes
829 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700830
Greg Daniel55afd6d2017-09-29 09:32:44 -0400831 // copy data into the buffer, skipping the trailing bytes
832 char* dst = buffer + individualMipOffsets[currentMipLevel];
833 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400834 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400835
836 VkBufferImageCopy& region = regions.push_back();
837 memset(&region, 0, sizeof(VkBufferImageCopy));
838 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
839 region.bufferRowLength = currentWidth;
840 region.bufferImageHeight = currentHeight;
841 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400842 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400843 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700844 }
jvanverthc578b0632016-05-02 10:58:12 -0700845 currentWidth = SkTMax(1, currentWidth/2);
846 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400847 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700848 }
849
jvanverth9d54afc2016-09-20 09:20:03 -0700850 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700851 transferBuffer->unmap();
852
jvanverth900bd4a2016-04-29 13:53:12 -0700853 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400854 uploadTexture->setImageLayout(this,
855 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
856 VK_ACCESS_TRANSFER_WRITE_BIT,
857 VK_PIPELINE_STAGE_TRANSFER_BIT,
858 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700859
860 // Copy the buffer to the image
861 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500862 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400863 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700864 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
865 regions.count(),
866 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400867
868 // If we copied the data into a temporary image first, copy that image into our main texture
869 // now.
870 if (copyTexture.get()) {
871 SkASSERT(dataColorType == GrColorType::kRGB_888x);
872 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
873 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
874 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
875 false));
876 }
Robert Phillips590533f2017-07-11 14:22:35 -0400877 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400878 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400879 }
jvanverth900bd4a2016-04-29 13:53:12 -0700880
Greg Daniel164a9f02016-02-22 09:56:40 -0500881 return true;
882}
883
Jim Van Verth1676cb92019-01-15 13:24:45 -0500884// It's probably possible to roll this into uploadTexDataOptimal,
885// but for now it's easier to maintain as a separate entity.
886bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
887 GrColorType dataColorType, const GrMipLevel texels[],
888 int mipLevelCount) {
889 SkASSERT(!tex->isLinearTiled());
890 // For now the assumption is that our rect is the entire texture.
891 // Compressed textures are read-only so this should be a reasonable assumption.
892 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
893
894 // We assume that if the texture has mip levels, we either upload to all the levels or just the
895 // first.
896 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
897
898 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
899 GrSRGBEncoded::kNo)));
900
901 if (width == 0 || height == 0) {
902 return false;
903 }
904
905 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
906 return false;
907 }
908
909 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
910
911 SkTArray<size_t> individualMipOffsets(mipLevelCount);
912 individualMipOffsets.push_back(0);
913 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
914 int currentWidth = width;
915 int currentHeight = height;
916 if (!texels[0].fPixels) {
917 return false;
918 }
919
920 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
921 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
922 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
923 currentWidth = SkTMax(1, currentWidth / 2);
924 currentHeight = SkTMax(1, currentHeight / 2);
925
926 if (texels[currentMipLevel].fPixels) {
927 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
928 currentHeight);
929 individualMipOffsets.push_back(combinedBufferSize);
930 combinedBufferSize += dataSize;
931 } else {
932 return false;
933 }
934 }
935 if (0 == combinedBufferSize) {
936 // We don't have any data to upload so fail (compressed textures are read-only).
937 return false;
938 }
939
940 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500941 sk_sp<GrVkTransferBuffer> transferBuffer =
942 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500943 if (!transferBuffer) {
944 return false;
945 }
946
947 int uploadLeft = left;
948 int uploadTop = top;
949 GrVkTexture* uploadTexture = tex;
950
951 char* buffer = (char*)transferBuffer->map();
952 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
953
954 currentWidth = width;
955 currentHeight = height;
956 int layerHeight = uploadTexture->height();
957 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
958 if (texels[currentMipLevel].fPixels) {
959 // Again, we're assuming that our rect is the entire texture
960 SkASSERT(currentHeight == layerHeight);
961 SkASSERT(0 == uploadLeft && 0 == uploadTop);
962
963 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
964 currentHeight);
965
966 // copy data into the buffer, skipping the trailing bytes
967 char* dst = buffer + individualMipOffsets[currentMipLevel];
968 const char* src = (const char*)texels[currentMipLevel].fPixels;
969 memcpy(dst, src, dataSize);
970
971 VkBufferImageCopy& region = regions.push_back();
972 memset(&region, 0, sizeof(VkBufferImageCopy));
973 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
974 region.bufferRowLength = currentWidth;
975 region.bufferImageHeight = currentHeight;
976 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
977 region.imageOffset = { uploadLeft, uploadTop, 0 };
978 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
979 }
980 currentWidth = SkTMax(1, currentWidth / 2);
981 currentHeight = SkTMax(1, currentHeight / 2);
982 layerHeight = currentHeight;
983 }
984
985 // no need to flush non-coherent memory, unmap will do that for us
986 transferBuffer->unmap();
987
988 // Change layout of our target so it can be copied to
989 uploadTexture->setImageLayout(this,
990 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
991 VK_ACCESS_TRANSFER_WRITE_BIT,
992 VK_PIPELINE_STAGE_TRANSFER_BIT,
993 false);
994
995 // Copy the buffer to the image
996 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500997 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500998 uploadTexture,
999 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1000 regions.count(),
1001 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -05001002
1003 if (1 == mipLevelCount) {
1004 tex->texturePriv().markMipMapsDirty();
1005 }
1006
1007 return true;
1008}
1009
Greg Daniel164a9f02016-02-22 09:56:40 -05001010////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001011sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001012 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001013 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1014
1015 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001016 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001017
Greg Daniel164a9f02016-02-22 09:56:40 -05001018 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1019 if (renderTarget) {
1020 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1021 }
1022
1023 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1024 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1025 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001026 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001027 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1028 // texture.
1029 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1030
Greg Daniel164a9f02016-02-22 09:56:40 -05001031 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001032 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001033 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001034 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001035 GrVkImage::ImageDesc imageDesc;
1036 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1037 imageDesc.fFormat = pixelFormat;
1038 imageDesc.fWidth = desc.fWidth;
1039 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001040 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001041 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001042 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001043 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001044 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001045
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001046 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1047 if (mipLevels > 1) {
1048 mipMapsStatus = GrMipMapsStatus::kValid;
1049 for (int i = 0; i < mipLevels; ++i) {
1050 if (!texels[i].fPixels) {
1051 mipMapsStatus = GrMipMapsStatus::kDirty;
1052 break;
1053 }
Greg Daniel834f1202017-10-09 15:06:20 -04001054 }
1055 }
1056
Robert Phillips67d52cf2017-06-05 13:38:13 -04001057 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001058 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001059 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1060 imageDesc,
1061 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001062 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001063 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001064 }
1065
1066 if (!tex) {
1067 return nullptr;
1068 }
1069
Jim Van Verth1676cb92019-01-15 13:24:45 -05001070 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001071 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001072 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001073 bool success;
1074 if (isCompressed) {
1075 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1076 colorType, texels, mipLevelCount);
1077 } else {
1078 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1079 colorType, texels, mipLevelCount);
1080 }
1081 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001082 tex->unref();
1083 return nullptr;
1084 }
1085 }
1086
Jim Van Verth1676cb92019-01-15 13:24:45 -05001087 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001088 VkClearColorValue zeroClearColor;
1089 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1090 VkImageSubresourceRange range;
1091 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1092 range.baseArrayLayer = 0;
1093 range.baseMipLevel = 0;
1094 range.layerCount = 1;
1095 range.levelCount = 1;
1096 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1097 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001098 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001099 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001100 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001101}
1102
1103////////////////////////////////////////////////////////////////////////////////
1104
Greg Daniel6888c0d2017-08-25 11:55:50 -04001105void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1106 VkDeviceSize dstOffset, VkDeviceSize size) {
1107 VkBufferCopy copyRegion;
1108 copyRegion.srcOffset = srcOffset;
1109 copyRegion.dstOffset = dstOffset;
1110 copyRegion.size = size;
1111 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1112}
1113
jvanverthdb379092016-07-07 11:18:46 -07001114bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1115 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001116 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001117 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001118
1119 return true;
1120}
1121
1122////////////////////////////////////////////////////////////////////////////////
1123
Greg Daniel7e000222018-12-03 10:08:21 -05001124static bool check_image_info(const GrVkCaps& caps,
1125 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001126 GrPixelConfig config,
1127 bool isWrappedRT) {
1128 if (VK_NULL_HANDLE == info.fImage) {
1129 return false;
1130 }
1131
1132 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001133 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001134 }
1135
Greg Daniel7e000222018-12-03 10:08:21 -05001136 if (info.fYcbcrConversionInfo.isValid()) {
1137 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1138 return false;
1139 }
jvanverthfd359ca2016-03-18 11:57:24 -07001140 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001141
Greg Danielcb324152019-02-25 11:36:53 -05001142 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1143 return false;
1144 }
1145
Greg Daniel52e16d92018-04-10 09:34:07 -04001146 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001147 return true;
1148}
1149
1150sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001151 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1152 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001153 GrVkImageInfo imageInfo;
1154 if (!backendTex.getVkImageInfo(&imageInfo)) {
1155 return nullptr;
1156 }
1157
Greg Danielcb324152019-02-25 11:36:53 -05001158 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001159 return nullptr;
1160 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001161
Greg Daniel164a9f02016-02-22 09:56:40 -05001162 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001163 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001164 surfDesc.fWidth = backendTex.width();
1165 surfDesc.fHeight = backendTex.height();
1166 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001167 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001168
Greg Daniel52e16d92018-04-10 09:34:07 -04001169 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1170 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001171 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1172 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001173}
1174
1175sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001176 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001177 GrWrapOwnership ownership,
1178 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001179 GrVkImageInfo imageInfo;
1180 if (!backendTex.getVkImageInfo(&imageInfo)) {
1181 return nullptr;
1182 }
1183
Greg Danielcb324152019-02-25 11:36:53 -05001184 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001185 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001186 }
Brian Salomond17f6582017-07-19 18:28:58 -04001187
1188 GrSurfaceDesc surfDesc;
1189 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1190 surfDesc.fWidth = backendTex.width();
1191 surfDesc.fHeight = backendTex.height();
1192 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001193 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001194
Greg Daniel52e16d92018-04-10 09:34:07 -04001195 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1196 SkASSERT(layout);
1197
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001198 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1199 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001200}
1201
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001202sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001203 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1204 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1205 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1206 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001207 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001208 return nullptr;
1209 }
halcanary9d524f22016-03-29 09:03:52 -07001210
Greg Daniel323fbcf2018-04-10 13:46:30 -04001211 GrVkImageInfo info;
1212 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001213 return nullptr;
1214 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001215
Greg Danielcb324152019-02-25 11:36:53 -05001216 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001217 return nullptr;
1218 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001219
Greg Daniel164a9f02016-02-22 09:56:40 -05001220 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001221 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001222 desc.fWidth = backendRT.width();
1223 desc.fHeight = backendRT.height();
1224 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001225 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001226
Greg Daniel323fbcf2018-04-10 13:46:30 -04001227 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001228
Greg Daniel323fbcf2018-04-10 13:46:30 -04001229 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001230 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001231
1232 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1233 SkASSERT(!backendRT.stencilBits());
1234 if (tgt) {
1235 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001236 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001237
Ben Wagnerff134f22018-04-24 16:29:16 -04001238 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001239}
1240
Greg Daniel7ef28f32017-04-20 16:41:55 +00001241sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001242 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001243
Greg Daniel52e16d92018-04-10 09:34:07 -04001244 GrVkImageInfo imageInfo;
1245 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001246 return nullptr;
1247 }
Greg Danielcb324152019-02-25 11:36:53 -05001248 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001249 return nullptr;
1250 }
1251
Greg Danielcb324152019-02-25 11:36:53 -05001252
Brian Osman33910292017-04-18 14:38:53 -04001253 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001254 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001255 desc.fWidth = tex.width();
1256 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001257 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001258 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1259 if (!desc.fSampleCnt) {
1260 return nullptr;
1261 }
Brian Osman33910292017-04-18 14:38:53 -04001262
Greg Daniel52e16d92018-04-10 09:34:07 -04001263 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1264 SkASSERT(layout);
1265
Ben Wagnerff134f22018-04-24 16:29:16 -04001266 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001267}
1268
Greg Danielb46add82019-01-02 14:51:29 -05001269sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1270 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1271 int maxSize = this->caps()->maxTextureSize();
1272 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1273 return nullptr;
1274 }
1275
1276 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1277 if (!backendFormat.isValid()) {
1278 return nullptr;
1279 }
1280 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1281 imageInfo.colorType());
1282 if (config == kUnknown_GrPixelConfig) {
1283 return nullptr;
1284 }
1285
1286 GrSurfaceDesc desc;
1287 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1288 desc.fWidth = imageInfo.width();
1289 desc.fHeight = imageInfo.height();
1290 desc.fConfig = config;
1291 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1292 if (!desc.fSampleCnt) {
1293 return nullptr;
1294 }
1295
1296 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1297}
1298
Brian Salomon930f9392018-06-20 16:25:26 -04001299bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1300 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001301 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001302 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001303 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001304 return false;
jvanverth62340062016-04-26 08:01:44 -07001305 }
1306
jvanverth62340062016-04-26 08:01:44 -07001307 // determine if we can blit to and from this format
1308 const GrVkCaps& caps = this->vkCaps();
1309 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001310 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1311 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001312 return false;
jvanverth62340062016-04-26 08:01:44 -07001313 }
1314
egdaniel7ac5da82016-07-15 13:41:42 -07001315 int width = tex->width();
1316 int height = tex->height();
1317 VkImageBlit blitRegion;
1318 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001319
jvanverth82c05582016-05-03 11:19:01 -07001320 // SkMipMap doesn't include the base level in the level count so we have to add 1
1321 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001322 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001323
Greg Danielda86e282018-06-13 09:41:19 -04001324 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001325 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1326 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001327
jvanverth50c46c72016-05-06 12:31:28 -07001328 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001329 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001330 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1331 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001332 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1333 nullptr, // pNext
1334 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1335 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1336 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1337 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1338 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1339 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1340 vkTex->image(), // image
1341 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001342 };
1343
jvanverth62340062016-04-26 08:01:44 -07001344 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001345 uint32_t mipLevel = 1;
1346 while (mipLevel < levelCount) {
1347 int prevWidth = width;
1348 int prevHeight = height;
1349 width = SkTMax(1, width / 2);
1350 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001351
jvanverth50c46c72016-05-06 12:31:28 -07001352 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001353 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1354 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001355
1356 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001357 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001358 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001359 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1360 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001361 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001362 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001363 vkTex->resource(),
1364 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001365 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001366 vkTex->resource(),
1367 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001368 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001369 1,
1370 &blitRegion,
1371 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001372 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001373 }
Greg Danielee54f232019-04-03 14:58:40 -04001374 if (levelCount > 1) {
1375 // This barrier logically is not needed, but it changes the final level to the same layout
1376 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1377 // layouts and future layout changes easier. The alternative here would be to track layout
1378 // and memory accesses per layer which doesn't seem work it.
1379 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1380 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1381 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1382 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1383 }
Brian Salomon930f9392018-06-20 16:25:26 -04001384 return true;
jvanverth62340062016-04-26 08:01:44 -07001385}
1386
Greg Daniel164a9f02016-02-22 09:56:40 -05001387////////////////////////////////////////////////////////////////////////////////
1388
1389GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1390 int width,
1391 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001392 SkASSERT(width >= rt->width());
1393 SkASSERT(height >= rt->height());
1394
1395 int samples = rt->numStencilSamples();
1396
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001397 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001398
1399 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001400 width,
1401 height,
1402 samples,
1403 sFmt));
1404 fStats.incStencilAttachmentCreates();
1405 return stencil;
1406}
1407
1408////////////////////////////////////////////////////////////////////////////////
1409
Brian Salomon52e943a2018-03-13 09:32:39 -04001410bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001411 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1412 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001413 VkDeviceSize size = dstRowBytes * h;
1414 VkDeviceSize offset = bufferOffset;
1415 SkASSERT(size + offset <= alloc.fSize);
1416 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1417 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001418 return false;
1419 }
Greg Daniel81df0412018-05-31 13:13:33 -04001420 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001421
Greg Daniel20ece3a2017-03-28 10:24:43 -04001422 if (srcData) {
1423 // If there is no padding on dst we can do a single memcopy.
1424 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001425 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001426 } else {
1427 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1428 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001429 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001430 }
Greg Daniel81df0412018-05-31 13:13:33 -04001431 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1432 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001433 return true;
1434}
1435
Brian Salomonf865b052018-03-09 09:01:53 -05001436#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001437bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1438 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001439 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001440 SkASSERT(texturable || renderable);
1441 if (!texturable) {
1442 SkASSERT(GrMipMapped::kNo == mipMapped);
1443 SkASSERT(!srcData);
1444 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001445 VkFormat pixelFormat;
1446 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001447 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001448 }
1449
Brian Salomon52e943a2018-03-13 09:32:39 -04001450 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1451 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001452 }
1453
Brian Salomon52e943a2018-03-13 09:32:39 -04001454 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1455 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001456 }
1457
1458 // Currently we don't support uploading pixel data when mipped.
1459 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001460 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001461 }
1462
Brian Salomon52e943a2018-03-13 09:32:39 -04001463 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001464 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1465 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001466 if (texturable) {
1467 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1468 }
1469 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001470 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1471 }
1472
1473 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001474 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001475 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001476
1477 // Create Image
1478 VkSampleCountFlagBits vkSamples;
1479 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001480 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001481 }
1482
1483 // Figure out the number of mip levels.
1484 uint32_t mipLevels = 1;
1485 if (GrMipMapped::kYes == mipMapped) {
1486 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1487 }
1488
1489 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001490 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1491 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001492 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001493 VK_IMAGE_TYPE_2D, // VkImageType
1494 pixelFormat, // VkFormat
1495 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1496 mipLevels, // mipLevels
1497 1, // arrayLayers
1498 vkSamples, // samples
1499 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1500 usageFlags, // VkImageUsageFlags
1501 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1502 0, // queueFamilyCount
1503 0, // pQueueFamilyIndices
1504 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001505 };
1506
Brian Salomon52e943a2018-03-13 09:32:39 -04001507 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1508 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001509
Brian Salomonde9f5462018-03-07 14:23:58 -05001510 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001511 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001512 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001513 }
1514
1515 // 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 -05001516 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001517 VkBuffer buffer = VK_NULL_HANDLE;
1518
1519 VkResult err;
1520 const VkCommandBufferAllocateInfo cmdInfo = {
1521 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1522 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001523 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001524 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1525 1 // bufferCount
1526 };
1527
1528 VkCommandBuffer cmdBuffer;
1529 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1530 if (err) {
1531 GrVkMemory::FreeImageMemory(this, false, alloc);
1532 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001533 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001534 }
1535
1536 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1537 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1538 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1539 cmdBufferBeginInfo.pNext = nullptr;
1540 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1541 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1542
1543 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1544 SkASSERT(!err);
1545
1546 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001547 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001548
Robert Phillips646f6372018-09-25 09:31:10 -04001549 const size_t trimRowBytes = w * bpp;
1550 if (!srcRowBytes) {
1551 srcRowBytes = trimRowBytes;
1552 }
1553
Brian Salomonde9f5462018-03-07 14:23:58 -05001554 SkTArray<size_t> individualMipOffsets(mipLevels);
1555 individualMipOffsets.push_back(0);
1556 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001557 if (GrPixelConfigIsCompressed(config)) {
1558 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1559 bpp = 4; // we have at least this alignment, which will pass the code below
1560 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001561 int currentWidth = w;
1562 int currentHeight = h;
1563 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1564 // config. This works with the assumption that the bytes in pixel config is always a power
1565 // of 2.
1566 SkASSERT((bpp & (bpp - 1)) == 0);
1567 const size_t alignmentMask = 0x3 | (bpp - 1);
1568 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1569 currentWidth = SkTMax(1, currentWidth / 2);
1570 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001571
Jim Van Verth1676cb92019-01-15 13:24:45 -05001572 size_t trimmedSize;
1573 if (GrPixelConfigIsCompressed(config)) {
1574 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1575 } else {
1576 trimmedSize = currentWidth * bpp * currentHeight;
1577 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001578 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1579 if (alignmentDiff != 0) {
1580 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001581 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001582 individualMipOffsets.push_back(combinedBufferSize);
1583 combinedBufferSize += trimmedSize;
1584 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001585
Brian Salomonde9f5462018-03-07 14:23:58 -05001586 VkBufferCreateInfo bufInfo;
1587 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1588 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1589 bufInfo.flags = 0;
1590 bufInfo.size = combinedBufferSize;
1591 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1592 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1593 bufInfo.queueFamilyIndexCount = 0;
1594 bufInfo.pQueueFamilyIndices = nullptr;
1595 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001596
Brian Salomonde9f5462018-03-07 14:23:58 -05001597 if (err) {
1598 GrVkMemory::FreeImageMemory(this, false, alloc);
1599 VK_CALL(DestroyImage(fDevice, image, nullptr));
1600 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001601 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001602 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001603 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001604
Brian Salomonde9f5462018-03-07 14:23:58 -05001605 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1606 &bufferAlloc)) {
1607 GrVkMemory::FreeImageMemory(this, false, alloc);
1608 VK_CALL(DestroyImage(fDevice, image, nullptr));
1609 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1610 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001611 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001612 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001613 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001614
Brian Salomonde9f5462018-03-07 14:23:58 -05001615 currentWidth = w;
1616 currentHeight = h;
1617 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1618 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001619 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001620 bool result;
1621 if (GrPixelConfigIsCompressed(config)) {
1622 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1623 size_t currentRowBytes = levelSize / currentHeight;
1624 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1625 currentRowBytes, currentRowBytes, currentHeight);
1626 } else {
1627 size_t currentRowBytes = bpp * currentWidth;
1628 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1629 currentRowBytes, trimRowBytes, currentHeight);
1630 }
1631 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001632 GrVkMemory::FreeImageMemory(this, false, alloc);
1633 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001634 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001635 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1636 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001637 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001638 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001639 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001640 currentWidth = SkTMax(1, currentWidth / 2);
1641 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001642 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001643
1644 // Set image layout and add barrier
1645 VkImageMemoryBarrier barrier;
1646 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1647 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1648 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001649 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001650 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1651 barrier.oldLayout = initialLayout;
1652 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1653 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1654 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1655 barrier.image = image;
1656 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1657
Greg Danielf7828d02018-10-09 12:01:32 -04001658 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001659 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1660 &barrier));
1661 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1662
1663 SkTArray<VkBufferImageCopy> regions(mipLevels);
1664
1665 currentWidth = w;
1666 currentHeight = h;
1667 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1668 // Submit copy command
1669 VkBufferImageCopy& region = regions.push_back();
1670 memset(&region, 0, sizeof(VkBufferImageCopy));
1671 region.bufferOffset = individualMipOffsets[currentMipLevel];
1672 region.bufferRowLength = currentWidth;
1673 region.bufferImageHeight = currentHeight;
1674 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1675 region.imageOffset = {0, 0, 0};
1676 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1677 currentWidth = SkTMax(1, currentWidth / 2);
1678 currentHeight = SkTMax(1, currentHeight / 2);
1679 }
1680
1681 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1682 regions.begin()));
1683
Brian Salomon52e943a2018-03-13 09:32:39 -04001684 if (texturable) {
1685 // Change Image layout to shader read since if we use this texture as a borrowed textures
1686 // within Ganesh we require that its layout be set to that
1687 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1688 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1689 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001690 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001691 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1692 barrier.oldLayout = initialLayout;
1693 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1694 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1695 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1696 barrier.image = image;
1697 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001698 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001699 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1700 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001701 0,
1702 0, nullptr,
1703 0, nullptr,
1704 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001705 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001706 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001707
1708 // End CommandBuffer
1709 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1710 SkASSERT(!err);
1711
1712 // Create Fence for queue
1713 VkFence fence;
1714 VkFenceCreateInfo fenceInfo;
1715 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1716 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1717
1718 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1719 SkASSERT(!err);
1720
1721 VkSubmitInfo submitInfo;
1722 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1723 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1724 submitInfo.pNext = nullptr;
1725 submitInfo.waitSemaphoreCount = 0;
1726 submitInfo.pWaitSemaphores = nullptr;
1727 submitInfo.pWaitDstStageMask = 0;
1728 submitInfo.commandBufferCount = 1;
1729 submitInfo.pCommandBuffers = &cmdBuffer;
1730 submitInfo.signalSemaphoreCount = 0;
1731 submitInfo.pSignalSemaphores = nullptr;
1732 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1733 SkASSERT(!err);
1734
1735 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1736 if (VK_TIMEOUT == err) {
1737 GrVkMemory::FreeImageMemory(this, false, alloc);
1738 VK_CALL(DestroyImage(fDevice, image, nullptr));
1739 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1740 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001741 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001742 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1743 SkDebugf("Fence failed to signal: %d\n", err);
1744 SK_ABORT("failing");
1745 }
1746 SkASSERT(!err);
1747
1748 // Clean up transfer resources
1749 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1750 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1751 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1752 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001753 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001754 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1755
Brian Salomon52e943a2018-03-13 09:32:39 -04001756 info->fImage = image;
1757 info->fAlloc = alloc;
1758 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1759 info->fImageLayout = initialLayout;
1760 info->fFormat = pixelFormat;
1761 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001762
Brian Salomon52e943a2018-03-13 09:32:39 -04001763 return true;
1764}
1765
1766GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001767 GrColorType colorType,
1768 bool isRenderTarget,
1769 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001770 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001771
1772 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1773 return GrBackendTexture();
1774 }
1775
Robert Phillips646f6372018-09-25 09:31:10 -04001776 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1777 if (!this->caps()->isConfigTexturable(config)) {
1778 return GrBackendTexture();
1779 }
1780
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001781 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001782 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001783 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001784 return {};
1785 }
Greg Daniel108bb232018-07-03 16:18:29 -04001786 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1787 // Lots of tests don't go through Skia's public interface which will set the config so for
1788 // testing we make sure we set a config here.
1789 beTex.setPixelConfig(config);
1790 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001791}
1792
1793bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001794 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001795
Greg Daniel52e16d92018-04-10 09:34:07 -04001796 GrVkImageInfo backend;
1797 if (!tex.getVkImageInfo(&backend)) {
1798 return false;
1799 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001800
Greg Daniel52e16d92018-04-10 09:34:07 -04001801 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001802 VkMemoryRequirements req;
1803 memset(&req, 0, sizeof(req));
1804 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001805 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001806 &req));
1807 // TODO: find a better check
1808 // This will probably fail with a different driver
1809 return (req.size > 0) && (req.size <= 8192 * 8192);
1810 }
1811
1812 return false;
1813}
1814
Brian Salomon26102cb2018-03-09 09:33:19 -05001815void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001816 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001817
Greg Daniel52e16d92018-04-10 09:34:07 -04001818 GrVkImageInfo info;
1819 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001820 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001821 }
1822}
1823
Brian Osman2d010b62018-08-09 10:55:09 -04001824GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001825 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1826 return GrBackendRenderTarget();
1827 }
1828
Brian Salomon8a375832018-03-14 10:21:40 -04001829 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001830 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001831 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001832 if (kUnknown_GrPixelConfig == config) {
1833 return {};
1834 }
Robert Phillips646f6372018-09-25 09:31:10 -04001835 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001836 &info)) {
1837 return {};
1838 }
Greg Daniel108bb232018-07-03 16:18:29 -04001839 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1840 // Lots of tests don't go through Skia's public interface which will set the config so for
1841 // testing we make sure we set a config here.
1842 beRT.setPixelConfig(config);
1843 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001844}
1845
Brian Salomon52e943a2018-03-13 09:32:39 -04001846void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001847 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001848
Greg Daniel323fbcf2018-04-10 13:46:30 -04001849 GrVkImageInfo info;
1850 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001851 // something in the command buffer may still be using this, so force submit
1852 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001853 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001854 }
1855}
Brian Salomonf865b052018-03-09 09:01:53 -05001856
Greg Daniel26b50a42018-03-08 09:49:58 -05001857void GrVkGpu::testingOnly_flushGpuAndSync() {
1858 this->submitCommandBuffer(kForce_SyncQueue);
1859}
Brian Salomonf865b052018-03-09 09:01:53 -05001860#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001861
Greg Daniel164a9f02016-02-22 09:56:40 -05001862////////////////////////////////////////////////////////////////////////////////
1863
Greg Daniel59dc1482019-02-22 10:46:38 -05001864void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1865 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001866 VkPipelineStageFlags dstStageMask,
1867 bool byRegion,
1868 VkBufferMemoryBarrier* barrier) const {
1869 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001870 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001871 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001872 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001873 srcStageMask,
1874 dstStageMask,
1875 byRegion,
1876 GrVkCommandBuffer::kBufferMemory_BarrierType,
1877 barrier);
1878}
1879
Greg Daniel59dc1482019-02-22 10:46:38 -05001880void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1881 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001882 VkPipelineStageFlags dstStageMask,
1883 bool byRegion,
1884 VkImageMemoryBarrier* barrier) const {
1885 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001886 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001887 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001888 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001889 srcStageMask,
1890 dstStageMask,
1891 byRegion,
1892 GrVkCommandBuffer::kImageMemory_BarrierType,
1893 barrier);
1894}
1895
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001896void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04001897 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
1898 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001899 SkASSERT(n >= 0);
1900 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00001901 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1902 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001903 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05001904 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001905 for (int i = 0; i < n; ++i) {
1906 SkASSERT(proxies[i]->isInstantiated());
1907 if (GrTexture* tex = proxies[i]->peekTexture()) {
1908 image = static_cast<GrVkTexture*>(tex);
1909 } else {
1910 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1911 SkASSERT(rt);
1912 image = static_cast<GrVkRenderTarget*>(rt);
1913 }
1914 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05001915 }
Greg Danielbae71212019-03-01 15:24:35 -05001916 }
Greg Daniel797efca2019-05-09 14:04:20 -04001917
1918 // Handle requests for preparing for external IO
1919 for (int i = 0; i < externalRequests.fNumImages; ++i) {
1920 SkImage* image = externalRequests.fImages[i];
1921 if (!image->isTextureBacked()) {
1922 continue;
1923 }
1924 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
1925 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
1926 SkASSERT(proxy);
1927
1928 if (!proxy->isInstantiated()) {
1929 auto resourceProvider = this->getContext()->priv().resourceProvider();
1930 if (!proxy->instantiate(resourceProvider)) {
1931 continue;
1932 }
1933 }
1934
1935 GrTexture* tex = proxy->peekTexture();
1936 if (!tex) {
1937 continue;
1938 }
1939 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
1940 vkTex->prepareForExternal(this);
1941 }
1942 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
1943 SkSurface* surface = externalRequests.fSurfaces[i];
1944 if (!surface->getCanvas()->getGrContext()) {
1945 continue;
1946 }
1947 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
1948 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
1949 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
1950 if (!proxy->isInstantiated()) {
1951 auto resourceProvider = this->getContext()->priv().resourceProvider();
1952 if (!proxy->instantiate(resourceProvider)) {
1953 continue;
1954 }
1955 }
1956
1957 GrRenderTarget* rt = proxy->peekRenderTarget();
1958 SkASSERT(rt);
1959 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
1960 if (externalRequests.fPrepareSurfaceForPresent &&
1961 externalRequests.fPrepareSurfaceForPresent[i]) {
1962 vkRT->prepareForPresent(this);
1963 } else {
1964 vkRT->prepareForExternal(this);
1965 }
1966 }
1967
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001968 if (info.fFlags & kSyncCpu_GrFlushFlag) {
1969 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001970 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001971 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001972 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001973}
1974
Greg Daniel25af6712018-04-25 10:44:38 -04001975static int get_surface_sample_cnt(GrSurface* surf) {
1976 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1977 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001978 }
Greg Daniel25af6712018-04-25 10:44:38 -04001979 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001980}
1981
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001982void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1983 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001984 GrVkImage* dstImage,
1985 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001986 const SkIRect& srcRect,
1987 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001988#ifdef SK_DEBUG
1989 int dstSampleCnt = get_surface_sample_cnt(dst);
1990 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001991 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1992 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1993 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1994 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001995
1996#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001997
Greg Daniel164a9f02016-02-22 09:56:40 -05001998 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1999 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002000 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002001 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2002 VK_ACCESS_TRANSFER_WRITE_BIT,
2003 VK_PIPELINE_STAGE_TRANSFER_BIT,
2004 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002005
egdaniel17b89252016-04-05 07:23:38 -07002006 srcImage->setImageLayout(this,
2007 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002008 VK_ACCESS_TRANSFER_READ_BIT,
2009 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002010 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002011
2012 // Flip rect if necessary
2013 SkIRect srcVkRect = srcRect;
2014 int32_t dstY = dstPoint.fY;
2015
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002016 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2017 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05002018 srcVkRect.fTop = src->height() - srcRect.fBottom;
2019 srcVkRect.fBottom = src->height() - srcRect.fTop;
2020 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
2021 }
2022
2023 VkImageCopy copyRegion;
2024 memset(&copyRegion, 0, sizeof(VkImageCopy));
2025 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2026 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
2027 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2028 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07002029 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002030
2031 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002032 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002033 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002034 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002035 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2036 1,
2037 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002038
2039 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2040 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002041 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002042}
2043
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002044void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2045 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002046 GrVkImage* dstImage,
2047 GrVkImage* srcImage,
2048 const SkIRect& srcRect,
2049 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002050#ifdef SK_DEBUG
2051 int dstSampleCnt = get_surface_sample_cnt(dst);
2052 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002053 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2054 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04002055 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002056 dstHasYcbcr, src->config(), srcSampleCnt,
2057 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002058
Greg Daniel25af6712018-04-25 10:44:38 -04002059#endif
egdaniel17b89252016-04-05 07:23:38 -07002060 dstImage->setImageLayout(this,
2061 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002062 VK_ACCESS_TRANSFER_WRITE_BIT,
2063 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002064 false);
2065
egdaniel17b89252016-04-05 07:23:38 -07002066 srcImage->setImageLayout(this,
2067 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002068 VK_ACCESS_TRANSFER_READ_BIT,
2069 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002070 false);
2071
2072 // Flip rect if necessary
2073 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002074 srcVkRect.fLeft = srcRect.fLeft;
2075 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002076 SkIRect dstRect;
2077 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002078 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002079
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002080 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002081 srcVkRect.fTop = src->height() - srcRect.fBottom;
2082 srcVkRect.fBottom = src->height() - srcRect.fTop;
2083 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002084 srcVkRect.fTop = srcRect.fTop;
2085 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002086 }
2087
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002088 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002089 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2090 } else {
2091 dstRect.fTop = dstPoint.fY;
2092 }
2093 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2094
2095 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2096 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002097 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002098 using std::swap;
2099 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002100 }
2101
2102 VkImageBlit blitRegion;
2103 memset(&blitRegion, 0, sizeof(VkImageBlit));
2104 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2105 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002106 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002107 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2108 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002109 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002110
2111 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002112 *srcImage,
2113 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002114 1,
2115 &blitRegion,
2116 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002117
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002118 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002119 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002120}
2121
Brian Salomon1fabd512018-02-09 09:54:25 -05002122void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2123 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2124 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002125 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002126 SkIRect srcRect = origSrcRect;
2127 SkIPoint dstPoint = origDstPoint;
2128 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2129 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2130 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2131 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2132 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2133 }
2134 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002135 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2136 srcRect.width(), srcRect.height());
2137 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002138}
2139
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002140bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2141 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002142 const SkIRect& srcRect, const SkIPoint& dstPoint,
2143 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002144#ifdef SK_DEBUG
2145 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2146 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2147 }
2148 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2149 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2150 }
2151#endif
2152
Greg Daniel25af6712018-04-25 10:44:38 -04002153 GrPixelConfig dstConfig = dst->config();
2154 GrPixelConfig srcConfig = src->config();
2155
2156 int dstSampleCnt = get_surface_sample_cnt(dst);
2157 int srcSampleCnt = get_surface_sample_cnt(src);
2158
egdaniel17b89252016-04-05 07:23:38 -07002159 GrVkImage* dstImage;
2160 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002161 GrRenderTarget* dstRT = dst->asRenderTarget();
2162 if (dstRT) {
2163 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002164 if (vkRT->wrapsSecondaryCommandBuffer()) {
2165 return false;
2166 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002167 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2168 } else {
2169 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002170 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002171 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002172 GrRenderTarget* srcRT = src->asRenderTarget();
2173 if (srcRT) {
2174 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2175 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002176 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002177 SkASSERT(src->asTexture());
2178 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002179 }
2180
Greg Daniela51e93c2019-03-25 12:30:45 -04002181 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2182 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2183
2184 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2185 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2186 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2187 return true;
2188 }
2189
2190 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2191 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2192 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2193 dstPoint, canDiscardOutsideDstRect));
2194 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2195 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2196 return true;
2197 }
2198
2199 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2200 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002201 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2202 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002203 return true;
2204 }
2205
Greg Daniel25af6712018-04-25 10:44:38 -04002206 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002207 dstHasYcbcr, srcConfig, srcSampleCnt,
2208 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002209 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2210 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002211 return true;
2212 }
2213
Greg Daniel164a9f02016-02-22 09:56:40 -05002214 return false;
2215}
2216
Brian Salomona6948702018-06-01 15:33:20 -04002217bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2218 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002219 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002220 return false;
2221 }
2222
egdaniel66933552016-08-24 07:22:19 -07002223 GrVkImage* image = nullptr;
2224 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2225 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002226 // Reading from render targets that wrap a secondary command buffer is not allowed since
2227 // it would require us to know the VkImage, which we don't have, as well as need us to
2228 // stop and start the VkRenderPass which we don't have access to.
2229 if (rt->wrapsSecondaryCommandBuffer()) {
2230 return false;
2231 }
egdaniel66933552016-08-24 07:22:19 -07002232 // resolve the render target if necessary
2233 switch (rt->getResolveType()) {
2234 case GrVkRenderTarget::kCantResolve_ResolveType:
2235 return false;
2236 case GrVkRenderTarget::kAutoResolves_ResolveType:
2237 break;
2238 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002239 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002240 break;
2241 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002242 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002243 }
2244 image = rt;
2245 } else {
2246 image = static_cast<GrVkTexture*>(surface->asTexture());
2247 }
2248
2249 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002250 return false;
2251 }
2252
Greg Daniel475eb702018-09-28 14:16:50 -04002253 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2254 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2255 // image and then do the read pixels from that.
2256 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002257 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2258 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002259
2260 // Make a new surface that is RGBA to copy the RGB surface into.
2261 GrSurfaceDesc surfDesc;
2262 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2263 surfDesc.fWidth = width;
2264 surfDesc.fHeight = height;
2265 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2266 surfDesc.fSampleCnt = 1;
2267
2268 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2269 VK_IMAGE_USAGE_SAMPLED_BIT |
2270 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2271 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2272
2273 GrVkImage::ImageDesc imageDesc;
2274 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2275 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2276 imageDesc.fWidth = width;
2277 imageDesc.fHeight = height;
2278 imageDesc.fLevels = 1;
2279 imageDesc.fSamples = 1;
2280 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2281 imageDesc.fUsageFlags = usageFlags;
2282 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2283
2284 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2285 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2286 if (!copySurface) {
2287 return false;
2288 }
2289
2290 int srcSampleCount = 0;
2291 if (rt) {
2292 srcSampleCount = rt->numColorSamples();
2293 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002294 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002295 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002296 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2297 surface->config(), srcSampleCount, kOrigin,
2298 srcHasYcbcr) &&
2299 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2300 surface->config(), SkToBool(surface->asTexture()),
2301 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002302 return false;
2303 }
2304 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2305 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2306 srcRect, SkIPoint::Make(0,0))) {
2307 return false;
2308 }
2309 top = 0;
2310 left = 0;
2311 dstColorType = GrColorType::kRGBA_8888;
2312 image = copySurface.get();
2313 }
2314
Greg Daniel164a9f02016-02-22 09:56:40 -05002315 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002316 image->setImageLayout(this,
2317 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2318 VK_ACCESS_TRANSFER_READ_BIT,
2319 VK_PIPELINE_STAGE_TRANSFER_BIT,
2320 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002321
Brian Salomonc320b152018-02-20 14:05:36 -05002322 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002323 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002324
Greg Daniel164a9f02016-02-22 09:56:40 -05002325 VkBufferImageCopy region;
2326 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002327
2328 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2329 if (copyFromOrigin) {
2330 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002331 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002332 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002333 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002334 region.imageOffset = offset;
2335 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2336 }
2337
2338 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002339 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002340 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002341 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002342 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002343 kStream_GrAccessPattern)
2344 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002345
2346 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002347 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002348 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002349 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2350 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002351
2352 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002353 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002354 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002355 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002356 1,
2357 &region);
2358
2359 // make sure the copy to buffer has finished
2360 transferBuffer->addMemoryBarrier(this,
2361 VK_ACCESS_TRANSFER_WRITE_BIT,
2362 VK_ACCESS_HOST_READ_BIT,
2363 VK_PIPELINE_STAGE_TRANSFER_BIT,
2364 VK_PIPELINE_STAGE_HOST_BIT,
2365 false);
2366
2367 // We need to submit the current command buffer to the Queue and make sure it finishes before
2368 // we can copy the data out of the buffer.
2369 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002370 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002371 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002372 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002373
egdaniel6fa0a912016-09-12 11:51:29 -07002374 if (copyFromOrigin) {
2375 uint32_t skipRows = region.imageExtent.height - height;
2376 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2377 }
2378
Brian Salomona6948702018-06-01 15:33:20 -04002379 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002380
2381 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002382 return true;
2383}
egdaniel066df7c2016-06-08 14:02:27 -07002384
egdaniel27bb2842016-07-07 11:58:35 -07002385// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2386// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2387// the the entire attachment. Similar requirements for the y and height components.
2388void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2389 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2390 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002391 if ((0 != granularity.width && 1 != granularity.width)) {
2392 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2393 int rightAdj = srcBounds.fRight % granularity.width;
2394 if (rightAdj != 0) {
2395 rightAdj = granularity.width - rightAdj;
2396 }
2397 dstBounds->fRight = srcBounds.fRight + rightAdj;
2398 if (dstBounds->fRight > maxWidth) {
2399 dstBounds->fRight = maxWidth;
2400 dstBounds->fLeft = 0;
2401 } else {
2402 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2403 }
egdaniel27bb2842016-07-07 11:58:35 -07002404 } else {
egdanield5797b32016-09-20 12:57:45 -07002405 dstBounds->fLeft = srcBounds.fLeft;
2406 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002407 }
2408
2409 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002410 if ((0 != granularity.height && 1 != granularity.height)) {
2411 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2412 int bottomAdj = srcBounds.fBottom % granularity.height;
2413 if (bottomAdj != 0) {
2414 bottomAdj = granularity.height - bottomAdj;
2415 }
2416 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2417 if (dstBounds->fBottom > maxHeight) {
2418 dstBounds->fBottom = maxHeight;
2419 dstBounds->fTop = 0;
2420 } else {
2421 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2422 }
egdaniel27bb2842016-07-07 11:58:35 -07002423 } else {
egdanield5797b32016-09-20 12:57:45 -07002424 dstBounds->fTop = srcBounds.fTop;
2425 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002426 }
2427}
2428
Greg Daniel22bc8652017-03-22 15:45:43 -04002429void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002430 const GrVkRenderPass* renderPass,
2431 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002432 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002433 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002434 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002435 const SkIRect* pBounds = &bounds;
2436 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002437 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002438 flippedBounds = bounds;
2439 flippedBounds.fTop = target->height() - bounds.fBottom;
2440 flippedBounds.fBottom = target->height() - bounds.fTop;
2441 pBounds = &flippedBounds;
2442 }
2443
egdaniel27bb2842016-07-07 11:58:35 -07002444 // The bounds we use for the render pass should be of the granularity supported
2445 // by the device.
2446 const VkExtent2D& granularity = renderPass->granularity();
2447 SkIRect adjustedBounds;
2448 if ((0 != granularity.width && 1 != granularity.width) ||
2449 (0 != granularity.height && 1 != granularity.height)) {
2450 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2451 target->width(), target->height());
2452 pBounds = &adjustedBounds;
2453 }
2454
Robert Phillips95214472017-08-08 18:00:03 -04002455#ifdef SK_DEBUG
2456 uint32_t index;
2457 bool result = renderPass->colorAttachmentIndex(&index);
2458 SkASSERT(result && 0 == index);
2459 result = renderPass->stencilAttachmentIndex(&index);
2460 if (result) {
2461 SkASSERT(1 == index);
2462 }
2463#endif
2464 VkClearValue clears[2];
2465 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002466 clears[1].depthStencil.depth = 0.0f;
2467 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002468
2469 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002470 for (int i = 0; i < buffers.count(); ++i) {
2471 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2472 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002473 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002474
Brian Salomon1fabd512018-02-09 09:54:25 -05002475 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002476}
egdaniel9cb63402016-06-23 08:37:05 -07002477
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002478void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2479 if (buffer->asRTCommandBuffer()) {
2480 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2481
2482 fCachedRTCommandBuffer->submit();
2483 fCachedRTCommandBuffer->reset();
2484 } else {
2485 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2486
2487 fCachedTexCommandBuffer->submit();
2488 fCachedTexCommandBuffer->reset();
2489 }
2490}
2491
Greg Daniel6be35232017-03-01 17:01:09 -05002492GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002493 VkFenceCreateInfo createInfo;
2494 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2495 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2496 createInfo.pNext = nullptr;
2497 createInfo.flags = 0;
2498 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002499
2500 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2501 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2502
2503 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002504 return (GrFence)fence;
2505}
2506
Greg Daniel6be35232017-03-01 17:01:09 -05002507bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2508 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2509
2510 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002511 return (VK_SUCCESS == result);
2512}
2513
2514void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002515 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2516}
2517
Greg Daniela5cb7812017-06-16 09:45:32 -04002518sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2519 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002520}
2521
Greg Daniel48661b82018-01-22 16:11:35 -05002522sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2523 GrResourceProvider::SemaphoreWrapType wrapType,
2524 GrWrapOwnership ownership) {
2525 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002526}
2527
Greg Daniel858e12c2018-12-06 11:11:37 -05002528void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002529 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2530
Greg Daniel48661b82018-01-22 16:11:35 -05002531 GrVkSemaphore::Resource* resource = vkSem->getResource();
2532 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002533 resource->ref();
2534 fSemaphoresToSignal.push_back(resource);
2535 }
Greg Daniel6be35232017-03-01 17:01:09 -05002536}
2537
Greg Daniel48661b82018-01-22 16:11:35 -05002538void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002539 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2540
Greg Daniel48661b82018-01-22 16:11:35 -05002541 GrVkSemaphore::Resource* resource = vkSem->getResource();
2542 if (resource->shouldWait()) {
2543 resource->ref();
2544 fSemaphoresToWaitOn.push_back(resource);
2545 }
jvanverth84741b32016-09-30 08:39:02 -07002546}
Brian Osman13dddce2017-05-09 13:19:50 -04002547
2548sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2549 SkASSERT(texture);
2550 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2551 vkTexture->setImageLayout(this,
2552 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2553 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002554 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002555 false);
2556 this->submitCommandBuffer(kSkip_SyncQueue);
2557
2558 // The image layout change serves as a barrier, so no semaphore is needed
2559 return nullptr;
2560}
Greg Danielf5d87582017-12-18 14:48:15 -05002561
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002562void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2563 fDrawables.emplace_back(std::move(drawable));
2564}
2565
Greg Daniel7a82edf2018-12-04 10:54:34 -05002566uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2567 const GrBackendFormat& format) {
2568 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2569 SkASSERT(ycbcrInfo);
2570 if (!ycbcrInfo->isValid()) {
2571 return 0;
2572 }
2573
2574 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2575 samplerState, *ycbcrInfo);
2576
2577 return sampler->uniqueID();
2578}
2579
Greg Daniela870b462019-01-08 15:49:46 -05002580void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002581 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002582 this->resourceProvider().storePipelineCacheData();
2583 }
2584}