blob: 1e048132a8855f01b6fd2bac83236a601d930246 [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"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040025#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/vk/GrVkAMDMemoryAllocator.h"
27#include "src/gpu/vk/GrVkCommandBuffer.h"
28#include "src/gpu/vk/GrVkCommandPool.h"
29#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
30#include "src/gpu/vk/GrVkImage.h"
31#include "src/gpu/vk/GrVkIndexBuffer.h"
32#include "src/gpu/vk/GrVkInterface.h"
33#include "src/gpu/vk/GrVkMemory.h"
34#include "src/gpu/vk/GrVkPipeline.h"
35#include "src/gpu/vk/GrVkPipelineState.h"
36#include "src/gpu/vk/GrVkRenderPass.h"
37#include "src/gpu/vk/GrVkResourceProvider.h"
38#include "src/gpu/vk/GrVkSemaphore.h"
39#include "src/gpu/vk/GrVkTexture.h"
40#include "src/gpu/vk/GrVkTextureRenderTarget.h"
41#include "src/gpu/vk/GrVkTransferBuffer.h"
42#include "src/gpu/vk/GrVkVertexBuffer.h"
Greg Daniel797efca2019-05-09 14:04:20 -040043#include "src/image/SkImage_Gpu.h"
44#include "src/image/SkSurface_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/sksl/SkSLCompiler.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040046
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "include/gpu/vk/GrVkExtensions.h"
48#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050049
Ben Wagnerf08d1d02018-06-18 15:11:00 -040050#include <utility>
51
Forrest Reiling44f85712017-03-27 23:22:20 -070052#if !defined(SK_BUILD_FOR_WIN)
53#include <unistd.h>
54#endif // !defined(SK_BUILD_FOR_WIN)
55
Greg Danieldef55462018-08-01 13:40:14 -040056#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050057#include "include/private/SkLeanWindows.h"
Greg Danieldef55462018-08-01 13:40:14 -040058#endif
59
Greg Daniel164a9f02016-02-22 09:56:40 -050060#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
61#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
62#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
63
Greg Danielf730c182018-07-02 20:15:37 +000064sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050065 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000066 if (backendContext.fInstance == VK_NULL_HANDLE ||
67 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
68 backendContext.fDevice == VK_NULL_HANDLE ||
69 backendContext.fQueue == VK_NULL_HANDLE) {
70 return nullptr;
71 }
Greg Danield3e65aa2018-08-01 09:19:45 -040072 if (!backendContext.fGetProc) {
73 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040074 }
Greg Danield3e65aa2018-08-01 09:19:45 -040075
Greg Daniel41f0e282019-01-28 13:15:05 -050076 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
77 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
78 backendContext.fGetProc("vkEnumerateInstanceVersion",
79 VK_NULL_HANDLE, VK_NULL_HANDLE));
80 uint32_t instanceVersion = 0;
81 if (!localEnumerateInstanceVersion) {
82 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
83 } else {
84 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
85 if (err) {
86 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
87 return nullptr;
88 }
89 }
90
Greg Danielc0b03d82018-08-03 14:41:15 -040091 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
92 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
93 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
94 backendContext.fInstance,
95 VK_NULL_HANDLE));
96
97 if (!localGetPhysicalDeviceProperties) {
98 return nullptr;
99 }
100 VkPhysicalDeviceProperties physDeviceProperties;
101 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
102 uint32_t physDevVersion = physDeviceProperties.apiVersion;
103
Greg Daniel41f0e282019-01-28 13:15:05 -0500104 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
105 : instanceVersion;
106
107 instanceVersion = SkTMin(instanceVersion, apiVersion);
108 physDevVersion = SkTMin(physDevVersion, apiVersion);
109
Greg Daniel98bffae2018-08-01 13:25:41 -0400110 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400111
Greg Daniel98bffae2018-08-01 13:25:41 -0400112 if (backendContext.fVkExtensions) {
113 interface.reset(new GrVkInterface(backendContext.fGetProc,
114 backendContext.fInstance,
115 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500116 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400117 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400118 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500119 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400120 return nullptr;
121 }
122 } else {
Greg Daniel98bffae2018-08-01 13:25:41 -0400123 GrVkExtensions extensions;
Greg Daniel88e8ddc2019-04-25 16:37:08 -0400124 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
125 // need to know if this is enabled to know if we can transition to a present layout when
126 // flushing a surface.
127 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
128 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
129 extensions.init(backendContext.fGetProc, backendContext.fInstance,
130 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
131 }
Greg Daniel98bffae2018-08-01 13:25:41 -0400132 interface.reset(new GrVkInterface(backendContext.fGetProc,
133 backendContext.fInstance,
134 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500135 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400136 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400137 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500138 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400139 return nullptr;
140 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500141 }
142
Greg Daniel41f0e282019-01-28 13:15:05 -0500143 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
144 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500145}
146
147////////////////////////////////////////////////////////////////////////////////
148
halcanary9d524f22016-03-29 09:03:52 -0700149GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500150 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
151 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500152 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400153 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000154 , fMemoryAllocator(backendContext.fMemoryAllocator)
155 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400156 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000157 , fDevice(backendContext.fDevice)
158 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400159 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500160 , fResourceProvider(this)
161 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000162 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700163
Greg Daniel81df0412018-05-31 13:13:33 -0400164 if (!fMemoryAllocator) {
165 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000166 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400167 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400168 }
169
ethannicholasb3058bd2016-07-01 08:22:01 -0700170 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700171
Greg Daniela0651ac2018-08-08 09:23:18 -0400172 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400173 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400174 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500175 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400176 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400177 } else if (backendContext.fDeviceFeatures) {
178 VkPhysicalDeviceFeatures2 features2;
179 features2.pNext = nullptr;
180 features2.features = *backendContext.fDeviceFeatures;
181 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500182 features2, instanceVersion, physicalDeviceVersion,
183 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400184 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400185 VkPhysicalDeviceFeatures2 features;
186 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
187 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400188 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400189 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400190 }
191 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400192 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400193 }
194 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400195 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400196 }
Greg Danielf808c5e2019-04-30 14:48:27 -0400197 GrVkExtensions extensions;
198 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
199 // need to know if this is enabled to know if we can transition to a present layout when
200 // flushing a surface.
201 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
202 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
203 extensions.init(backendContext.fGetProc, backendContext.fInstance,
204 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
205 }
Greg Daniel36443602018-08-02 12:51:52 -0400206 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Danielf808c5e2019-04-30 14:48:27 -0400207 features, instanceVersion, physicalDeviceVersion, extensions));
Greg Daniel36443602018-08-02 12:51:52 -0400208 }
jvanverth633b3562016-03-23 11:01:22 -0700209 fCaps.reset(SkRef(fVkCaps.get()));
210
Greg Danielf730c182018-07-02 20:15:37 +0000211 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
212 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700213
Greg Daniela870b462019-01-08 15:49:46 -0500214 fResourceProvider.init();
215
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500216 fCmdPool = fResourceProvider.findOrCreateCommandPool();
217 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000218 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700219 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500220}
221
Greg Daniel8606cf82017-05-08 16:17:53 -0400222void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500223 if (fCmdPool) {
224 fCmdPool->getPrimaryCommandBuffer()->end(this);
225 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400226 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500227
228 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500229 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700230
231 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
232 // on the command buffers even though they have completed. This causes an assert to fire when
233 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500234 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700235#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500236 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700237#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500238 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700239#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500240 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700241#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500242 }
egdanielf8c2be32016-06-24 13:18:27 -0700243#endif
244
egdanielbe9d8212016-09-20 08:54:23 -0700245#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400246 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700247#endif
halcanary9d524f22016-03-29 09:03:52 -0700248
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500249 if (fCmdPool) {
250 fCmdPool->unref(this);
251 fCmdPool = nullptr;
252 }
253
Greg Daniel6be35232017-03-01 17:01:09 -0500254 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
255 fSemaphoresToWaitOn[i]->unref(this);
256 }
257 fSemaphoresToWaitOn.reset();
258
Greg Daniela5cb7812017-06-16 09:45:32 -0400259 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
260 fSemaphoresToSignal[i]->unref(this);
261 }
262 fSemaphoresToSignal.reset();
263
264
egdanielbc9b2962016-09-27 08:00:53 -0700265 fCopyManager.destroyResources(this);
266
Jim Van Verth09557d72016-11-07 11:10:21 -0500267 // must call this just before we destroy the command pool and VkDevice
268 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500269
Greg Danielf730c182018-07-02 20:15:37 +0000270 fMemoryAllocator.reset();
271
272 fQueue = VK_NULL_HANDLE;
273 fDevice = VK_NULL_HANDLE;
274 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400275}
276
277GrVkGpu::~GrVkGpu() {
278 if (!fDisconnected) {
279 this->destroyResources();
280 }
281 delete fCompiler;
282}
283
284
285void GrVkGpu::disconnect(DisconnectType type) {
286 INHERITED::disconnect(type);
287 if (!fDisconnected) {
288 if (DisconnectType::kCleanup == type) {
289 this->destroyResources();
290 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500291 if (fCmdPool) {
292 fCmdPool->unrefAndAbandon();
293 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400294 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400295 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
296 fSemaphoresToWaitOn[i]->unrefAndAbandon();
297 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400298 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
299 fSemaphoresToSignal[i]->unrefAndAbandon();
300 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400301 fCopyManager.abandonResources();
302
303 // must call this just before we destroy the command pool and VkDevice
304 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400305
306 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400307 }
308 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400309 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400310 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400311 fDisconnected = true;
312 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500313}
314
315///////////////////////////////////////////////////////////////////////////////
316
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400317GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400318 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400319 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
320 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400321 if (!fCachedRTCommandBuffer) {
322 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
323 }
324
Greg Daniela41a74a2018-10-09 12:59:23 +0000325 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400326 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400327}
328
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400329GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
330 if (!fCachedTexCommandBuffer) {
331 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
332 }
333
334 fCachedTexCommandBuffer->set(texture, origin);
335 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700336}
337
Greg Daniela3aa75a2019-04-12 14:24:55 -0400338void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
339 GrGpuFinishedContext finishedContext) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500340 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400341
342 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
343 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
344 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400345 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400346 if (finishedProc) {
347 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
348 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400349 return;
350 }
351
Greg Daniel164a9f02016-02-22 09:56:40 -0500352 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500353 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400354 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500355
Greg Daniela3aa75a2019-04-12 14:24:55 -0400356 if (finishedProc) {
357 // Make sure this is called after closing the current command pool
358 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
359 }
360
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400361 // We must delete and drawables that have been waitint till submit for us to destroy.
362 fDrawables.reset();
363
Greg Daniel6be35232017-03-01 17:01:09 -0500364 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
365 fSemaphoresToWaitOn[i]->unref(this);
366 }
367 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400368 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
369 fSemaphoresToSignal[i]->unref(this);
370 }
371 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500372
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500373 // Release old command pool and create a new one
374 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500375 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500376 fCmdPool = fResourceProvider.findOrCreateCommandPool();
377 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500378 fCurrentCmdBuffer->begin(this);
379}
380
381///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500382sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
383 GrAccessPattern accessPattern, const void* data) {
384 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700385 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500386 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700387 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
388 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500389 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700390 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500391 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700392 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
393 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500394 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700395 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500396 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400397 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
398 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500399 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700400 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500401 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400402 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
403 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500404 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700405 break;
cdalton397536c2016-03-25 12:15:03 -0700406 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400407 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700408 return nullptr;
409 }
cdalton1bf3e712016-04-19 10:00:02 -0700410 if (data && buff) {
411 buff->updateData(data, size);
412 }
413 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500414}
415
Brian Salomona9b04b92018-06-01 15:04:28 -0400416bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
417 GrColorType srcColorType, const GrMipLevel texels[],
418 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500419 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
420 if (!vkTex) {
421 return false;
422 }
423
jvanverth900bd4a2016-04-29 13:53:12 -0700424 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400425 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800426 return false;
427 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800428
Jim Van Verth1676cb92019-01-15 13:24:45 -0500429 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500430 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400431 bool linearTiling = vkTex->isLinearTiled();
432 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400433 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400434 SkDebugf("Can't upload mipmap data to linear tiled texture");
435 return false;
436 }
437 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
438 // Need to change the layout to general in order to perform a host write
439 vkTex->setImageLayout(this,
440 VK_IMAGE_LAYOUT_GENERAL,
441 VK_ACCESS_HOST_WRITE_BIT,
442 VK_PIPELINE_STAGE_HOST_BIT,
443 false);
444 this->submitCommandBuffer(kForce_SyncQueue);
445 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400446 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400447 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500448 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400449 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400450 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
451 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500452 }
egdaniel4583ec52016-06-27 12:57:00 -0700453
jvanverth900bd4a2016-04-29 13:53:12 -0700454 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500455}
456
Brian Salomone05ba5a2019-04-08 11:59:07 -0400457bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
458 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
459 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500460 // Can't transfer compressed data
461 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
462
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400463 // Vulkan only supports 4-byte aligned offsets
464 if (SkToBool(bufferOffset & 0x2)) {
465 return false;
466 }
467 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
468 if (!vkTex) {
469 return false;
470 }
471 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
472 if (!vkBuffer) {
473 return false;
474 }
475
Greg Daniel660cc992017-06-26 14:55:05 -0400476 SkDEBUGCODE(
477 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
478 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
479 SkASSERT(bounds.contains(subRect));
480 )
Brian Salomonc320b152018-02-20 14:05:36 -0500481 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400482 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500483 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400484 }
485
486 // Set up copy region
487 VkBufferImageCopy region;
488 memset(&region, 0, sizeof(VkBufferImageCopy));
489 region.bufferOffset = bufferOffset;
490 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
491 region.bufferImageHeight = 0;
492 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
493 region.imageOffset = { left, top, 0 };
494 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
495
496 // Change layout of our target so it can be copied to
497 vkTex->setImageLayout(this,
498 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
499 VK_ACCESS_TRANSFER_WRITE_BIT,
500 VK_PIPELINE_STAGE_TRANSFER_BIT,
501 false);
502
503 // Copy the buffer to the image
504 fCurrentCmdBuffer->copyBufferToImage(this,
505 vkBuffer,
506 vkTex,
507 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
508 1,
509 &region);
510
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400511 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400512 return true;
513}
514
Brian Salomon26de56e2019-04-10 12:14:26 -0400515bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
516 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
517 size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400518 SkASSERT(surface);
519 SkASSERT(transferBuffer);
520
Brian Salomona585fe92019-04-09 14:57:00 -0400521 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
522
523 GrVkImage* srcImage;
524 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
525 // Reading from render targets that wrap a secondary command buffer is not allowed since
526 // it would require us to know the VkImage, which we don't have, as well as need us to
527 // stop and start the VkRenderPass which we don't have access to.
528 if (rt->wrapsSecondaryCommandBuffer()) {
529 return false;
530 }
531 // resolve the render target if necessary
532 switch (rt->getResolveType()) {
533 case GrVkRenderTarget::kCantResolve_ResolveType:
534 return false;
535 case GrVkRenderTarget::kAutoResolves_ResolveType:
536 break;
537 case GrVkRenderTarget::kCanResolve_ResolveType:
538 this->resolveRenderTargetNoFlush(rt);
539 break;
540 default:
541 SK_ABORT("Unknown resolve type");
542 }
543 srcImage = rt;
544 } else {
545 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
546 }
547
548 // Set up copy region
549 VkBufferImageCopy region;
550 memset(&region, 0, sizeof(VkBufferImageCopy));
551 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400552 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400553 region.bufferImageHeight = 0;
554 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
555 region.imageOffset = { left, top, 0 };
556 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
557
558 srcImage->setImageLayout(this,
559 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
560 VK_ACCESS_TRANSFER_READ_BIT,
561 VK_PIPELINE_STAGE_TRANSFER_BIT,
562 false);
563
564 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
565 vkBuffer, 1, &region);
566
567 // Make sure the copy to buffer has finished.
568 vkBuffer->addMemoryBarrier(this,
569 VK_ACCESS_TRANSFER_WRITE_BIT,
570 VK_ACCESS_HOST_READ_BIT,
571 VK_PIPELINE_STAGE_TRANSFER_BIT,
572 VK_PIPELINE_STAGE_HOST_BIT,
573 false);
574
575 // The caller is responsible for syncing.
576 this->submitCommandBuffer(kSkip_SyncQueue);
577
Brian Salomon26de56e2019-04-10 12:14:26 -0400578 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400579}
580
Brian Salomon1fabd512018-02-09 09:54:25 -0500581void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
582 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700583 SkASSERT(dst);
584 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
585
egdaniel4bcd62e2016-08-31 07:37:31 -0700586 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500587 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
588 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
589 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
590 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
591 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700592
Greg Danielbc26c392017-04-18 13:32:10 -0400593 GrVkImage* dstImage;
594 GrRenderTarget* dstRT = dst->asRenderTarget();
595 if (dstRT) {
596 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400597 dstImage = vkRT;
598 } else {
599 SkASSERT(dst->asTexture());
600 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
601 }
602 dstImage->setImageLayout(this,
603 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
604 VK_ACCESS_TRANSFER_WRITE_BIT,
605 VK_PIPELINE_STAGE_TRANSFER_BIT,
606 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700607
608 src->msaaImage()->setImageLayout(this,
609 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
610 VK_ACCESS_TRANSFER_READ_BIT,
611 VK_PIPELINE_STAGE_TRANSFER_BIT,
612 false);
613
Greg Danielbc26c392017-04-18 13:32:10 -0400614 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700615}
616
Brian Salomon1fabd512018-02-09 09:54:25 -0500617void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700618 if (target->needsResolve()) {
619 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700620 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
621 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500622
egdaniel4bcd62e2016-08-31 07:37:31 -0700623 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700624
Brian Salomon1fabd512018-02-09 09:54:25 -0500625 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700626
627 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500628
629 if (requiresSubmit) {
630 this->submitCommandBuffer(kSkip_SyncQueue);
631 }
egdaniel52ad2512016-08-04 12:50:01 -0700632 }
633}
634
Brian Salomona9b04b92018-06-01 15:04:28 -0400635bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
636 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500637 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700638 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500639
Jim Van Verth1676cb92019-01-15 13:24:45 -0500640 // If we're uploading compressed data then we should be using uploadCompressedTexData
641 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
642 GrSRGBEncoded::kNo)));
643
Greg Daniel660cc992017-06-26 14:55:05 -0400644 SkDEBUGCODE(
645 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
646 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
647 SkASSERT(bounds.contains(subRect));
648 )
Brian Salomonc320b152018-02-20 14:05:36 -0500649 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500650 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400651 if (!rowBytes) {
652 rowBytes = trimRowBytes;
653 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500654
jvanverth900bd4a2016-04-29 13:53:12 -0700655 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
656 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
657 const VkImageSubresource subres = {
658 VK_IMAGE_ASPECT_COLOR_BIT,
659 0, // mipLevel
660 0, // arraySlice
661 };
662 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500663
jvanverth900bd4a2016-04-29 13:53:12 -0700664 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500665
jvanverth900bd4a2016-04-29 13:53:12 -0700666 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700667 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700668 &subres,
669 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500670
jvanverth1e305ba2016-06-01 09:39:15 -0700671 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400672 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700673 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400674 SkASSERT(size + offset <= alloc.fSize);
675 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
676 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700677 return false;
678 }
Greg Daniel81df0412018-05-31 13:13:33 -0400679 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700680
Brian Salomona9b04b92018-06-01 15:04:28 -0400681 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
682 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700683
Greg Daniele35a99e2018-03-02 11:44:22 -0500684 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400685 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700686
687 return true;
688}
689
Brian Salomona9b04b92018-06-01 15:04:28 -0400690bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
691 GrColorType dataColorType, const GrMipLevel texels[],
692 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700693 SkASSERT(!tex->isLinearTiled());
694 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400695 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700696 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
697
Greg Danieldd20e912017-04-07 14:42:23 -0400698 // We assume that if the texture has mip levels, we either upload to all the levels or just the
699 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400700 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400701
Jim Van Verth1676cb92019-01-15 13:24:45 -0500702 // If we're uploading compressed data then we should be using uploadCompressedTexData
703 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
704 GrSRGBEncoded::kNo)));
705
jvanverth900bd4a2016-04-29 13:53:12 -0700706 if (width == 0 || height == 0) {
707 return false;
708 }
709
Greg Daniel475eb702018-09-28 14:16:50 -0400710 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
711 return false;
712 }
713
714 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
715 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500716 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
717 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400718 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
719 // blit or draw.
720 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
721 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
722 return false;
723 }
724 mipLevelCount = 1;
725 }
726
Brian Salomond1eaf492017-05-18 10:02:08 -0400727 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500728 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700729
730 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700731 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
732 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400733 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
734
Greg Daniel475eb702018-09-28 14:16:50 -0400735 texelsShallowCopy.reset(mipLevelCount);
736 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700737
Robert Phillips590533f2017-07-11 14:22:35 -0400738 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700739 individualMipOffsets.push_back(0);
740 size_t combinedBufferSize = width * bpp * height;
741 int currentWidth = width;
742 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400743 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400744 combinedBufferSize = 0;
745 }
746
Greg Daniel468fd632017-03-22 17:03:45 -0400747 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
748 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
749 SkASSERT((bpp & (bpp - 1)) == 0);
750 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400751 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700752 currentWidth = SkTMax(1, currentWidth/2);
753 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400754
Greg Daniel55afd6d2017-09-29 09:32:44 -0400755 if (texelsShallowCopy[currentMipLevel].fPixels) {
756 const size_t trimmedSize = currentWidth * bpp * currentHeight;
757 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
758 if (alignmentDiff != 0) {
759 combinedBufferSize += alignmentMask - alignmentDiff + 1;
760 }
761 individualMipOffsets.push_back(combinedBufferSize);
762 combinedBufferSize += trimmedSize;
763 } else {
764 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400765 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400766 }
767 if (0 == combinedBufferSize) {
768 // We don't actually have any data to upload so just return success
769 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700770 }
771
772 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500773 sk_sp<GrVkTransferBuffer> transferBuffer =
774 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400775 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700776 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400777 }
jvanverth900bd4a2016-04-29 13:53:12 -0700778
Greg Daniel475eb702018-09-28 14:16:50 -0400779 int uploadLeft = left;
780 int uploadTop = top;
781 GrVkTexture* uploadTexture = tex;
782 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
783 // R8G8B8A8_UNORM image and then copy it.
784 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500785 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400786 GrSurfaceDesc surfDesc;
787 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
788 surfDesc.fWidth = width;
789 surfDesc.fHeight = height;
790 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
791 surfDesc.fSampleCnt = 1;
792
793 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
794 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
795 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
796
797 GrVkImage::ImageDesc imageDesc;
798 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
799 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
800 imageDesc.fWidth = width;
801 imageDesc.fHeight = height;
802 imageDesc.fLevels = 1;
803 imageDesc.fSamples = 1;
804 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
805 imageDesc.fUsageFlags = usageFlags;
806 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
807
808 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
809 GrMipMapsStatus::kNotAllocated);
810 if (!copyTexture) {
811 return false;
812 }
Greg Daniel5c7b5412019-05-10 11:39:55 -0400813
814 bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid();
815 if (!this->vkCaps().canCopyAsBlit(tex->config(), 1, false, dstHasYcbcr,
816 copyTexture->config(), 1, false,
817 false) &&
818 !this->vkCaps().canCopyAsDraw(tex->config(), SkToBool(tex->asRenderTarget()),
819 dstHasYcbcr,
820 copyTexture->config(), true, false)) {
821 return false;
822 }
823
Greg Daniel475eb702018-09-28 14:16:50 -0400824 uploadTexture = copyTexture.get();
825 uploadLeft = 0;
826 uploadTop = 0;
827 }
828
jvanverth900bd4a2016-04-29 13:53:12 -0700829 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400830 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700831
jvanverthc578b0632016-05-02 10:58:12 -0700832 currentWidth = width;
833 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400834 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400835 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400836 if (texelsShallowCopy[currentMipLevel].fPixels) {
837 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
838 const size_t trimRowBytes = currentWidth * bpp;
839 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
840 ? texelsShallowCopy[currentMipLevel].fRowBytes
841 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700842
Greg Daniel55afd6d2017-09-29 09:32:44 -0400843 // copy data into the buffer, skipping the trailing bytes
844 char* dst = buffer + individualMipOffsets[currentMipLevel];
845 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400846 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400847
848 VkBufferImageCopy& region = regions.push_back();
849 memset(&region, 0, sizeof(VkBufferImageCopy));
850 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
851 region.bufferRowLength = currentWidth;
852 region.bufferImageHeight = currentHeight;
853 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400854 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400855 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700856 }
jvanverthc578b0632016-05-02 10:58:12 -0700857 currentWidth = SkTMax(1, currentWidth/2);
858 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400859 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700860 }
861
jvanverth9d54afc2016-09-20 09:20:03 -0700862 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700863 transferBuffer->unmap();
864
jvanverth900bd4a2016-04-29 13:53:12 -0700865 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400866 uploadTexture->setImageLayout(this,
867 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
868 VK_ACCESS_TRANSFER_WRITE_BIT,
869 VK_PIPELINE_STAGE_TRANSFER_BIT,
870 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700871
872 // Copy the buffer to the image
873 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500874 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400875 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700876 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
877 regions.count(),
878 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400879
880 // If we copied the data into a temporary image first, copy that image into our main texture
881 // now.
882 if (copyTexture.get()) {
883 SkASSERT(dataColorType == GrColorType::kRGB_888x);
884 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
885 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
886 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
887 false));
888 }
Robert Phillips590533f2017-07-11 14:22:35 -0400889 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400890 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400891 }
jvanverth900bd4a2016-04-29 13:53:12 -0700892
Greg Daniel164a9f02016-02-22 09:56:40 -0500893 return true;
894}
895
Jim Van Verth1676cb92019-01-15 13:24:45 -0500896// It's probably possible to roll this into uploadTexDataOptimal,
897// but for now it's easier to maintain as a separate entity.
898bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
899 GrColorType dataColorType, const GrMipLevel texels[],
900 int mipLevelCount) {
901 SkASSERT(!tex->isLinearTiled());
902 // For now the assumption is that our rect is the entire texture.
903 // Compressed textures are read-only so this should be a reasonable assumption.
904 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
905
906 // We assume that if the texture has mip levels, we either upload to all the levels or just the
907 // first.
908 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
909
910 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
911 GrSRGBEncoded::kNo)));
912
913 if (width == 0 || height == 0) {
914 return false;
915 }
916
917 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
918 return false;
919 }
920
921 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
922
923 SkTArray<size_t> individualMipOffsets(mipLevelCount);
924 individualMipOffsets.push_back(0);
925 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
926 int currentWidth = width;
927 int currentHeight = height;
928 if (!texels[0].fPixels) {
929 return false;
930 }
931
932 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
933 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
934 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
935 currentWidth = SkTMax(1, currentWidth / 2);
936 currentHeight = SkTMax(1, currentHeight / 2);
937
938 if (texels[currentMipLevel].fPixels) {
939 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
940 currentHeight);
941 individualMipOffsets.push_back(combinedBufferSize);
942 combinedBufferSize += dataSize;
943 } else {
944 return false;
945 }
946 }
947 if (0 == combinedBufferSize) {
948 // We don't have any data to upload so fail (compressed textures are read-only).
949 return false;
950 }
951
952 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500953 sk_sp<GrVkTransferBuffer> transferBuffer =
954 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500955 if (!transferBuffer) {
956 return false;
957 }
958
959 int uploadLeft = left;
960 int uploadTop = top;
961 GrVkTexture* uploadTexture = tex;
962
963 char* buffer = (char*)transferBuffer->map();
964 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
965
966 currentWidth = width;
967 currentHeight = height;
968 int layerHeight = uploadTexture->height();
969 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
970 if (texels[currentMipLevel].fPixels) {
971 // Again, we're assuming that our rect is the entire texture
972 SkASSERT(currentHeight == layerHeight);
973 SkASSERT(0 == uploadLeft && 0 == uploadTop);
974
975 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
976 currentHeight);
977
978 // copy data into the buffer, skipping the trailing bytes
979 char* dst = buffer + individualMipOffsets[currentMipLevel];
980 const char* src = (const char*)texels[currentMipLevel].fPixels;
981 memcpy(dst, src, dataSize);
982
983 VkBufferImageCopy& region = regions.push_back();
984 memset(&region, 0, sizeof(VkBufferImageCopy));
985 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
986 region.bufferRowLength = currentWidth;
987 region.bufferImageHeight = currentHeight;
988 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
989 region.imageOffset = { uploadLeft, uploadTop, 0 };
990 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
991 }
992 currentWidth = SkTMax(1, currentWidth / 2);
993 currentHeight = SkTMax(1, currentHeight / 2);
994 layerHeight = currentHeight;
995 }
996
997 // no need to flush non-coherent memory, unmap will do that for us
998 transferBuffer->unmap();
999
1000 // Change layout of our target so it can be copied to
1001 uploadTexture->setImageLayout(this,
1002 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1003 VK_ACCESS_TRANSFER_WRITE_BIT,
1004 VK_PIPELINE_STAGE_TRANSFER_BIT,
1005 false);
1006
1007 // Copy the buffer to the image
1008 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -05001009 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -05001010 uploadTexture,
1011 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1012 regions.count(),
1013 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -05001014
1015 if (1 == mipLevelCount) {
1016 tex->texturePriv().markMipMapsDirty();
1017 }
1018
1019 return true;
1020}
1021
Greg Daniel164a9f02016-02-22 09:56:40 -05001022////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -04001023sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001024 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001025 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1026
1027 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001028 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001029
Greg Daniel164a9f02016-02-22 09:56:40 -05001030 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1031 if (renderTarget) {
1032 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1033 }
1034
1035 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1036 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1037 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001038 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001039 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1040 // texture.
1041 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1042
Greg Daniel164a9f02016-02-22 09:56:40 -05001043 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001044 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001045 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001046 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001047 GrVkImage::ImageDesc imageDesc;
1048 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1049 imageDesc.fFormat = pixelFormat;
1050 imageDesc.fWidth = desc.fWidth;
1051 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001052 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001053 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001054 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001055 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001056 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001057
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001058 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1059 if (mipLevels > 1) {
1060 mipMapsStatus = GrMipMapsStatus::kValid;
1061 for (int i = 0; i < mipLevels; ++i) {
1062 if (!texels[i].fPixels) {
1063 mipMapsStatus = GrMipMapsStatus::kDirty;
1064 break;
1065 }
Greg Daniel834f1202017-10-09 15:06:20 -04001066 }
1067 }
1068
Robert Phillips67d52cf2017-06-05 13:38:13 -04001069 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001070 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001071 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1072 imageDesc,
1073 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001074 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001075 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001076 }
1077
1078 if (!tex) {
1079 return nullptr;
1080 }
1081
Jim Van Verth1676cb92019-01-15 13:24:45 -05001082 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001083 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001084 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001085 bool success;
1086 if (isCompressed) {
1087 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1088 colorType, texels, mipLevelCount);
1089 } else {
1090 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1091 colorType, texels, mipLevelCount);
1092 }
1093 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001094 tex->unref();
1095 return nullptr;
1096 }
1097 }
1098
Jim Van Verth1676cb92019-01-15 13:24:45 -05001099 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001100 VkClearColorValue zeroClearColor;
1101 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1102 VkImageSubresourceRange range;
1103 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1104 range.baseArrayLayer = 0;
1105 range.baseMipLevel = 0;
1106 range.layerCount = 1;
1107 range.levelCount = 1;
1108 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1109 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001110 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001111 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001112 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001113}
1114
1115////////////////////////////////////////////////////////////////////////////////
1116
Greg Daniel6888c0d2017-08-25 11:55:50 -04001117void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1118 VkDeviceSize dstOffset, VkDeviceSize size) {
1119 VkBufferCopy copyRegion;
1120 copyRegion.srcOffset = srcOffset;
1121 copyRegion.dstOffset = dstOffset;
1122 copyRegion.size = size;
1123 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1124}
1125
jvanverthdb379092016-07-07 11:18:46 -07001126bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1127 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001128 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001129 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001130
1131 return true;
1132}
1133
1134////////////////////////////////////////////////////////////////////////////////
1135
Greg Daniel7e000222018-12-03 10:08:21 -05001136static bool check_image_info(const GrVkCaps& caps,
1137 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001138 GrPixelConfig config,
1139 bool isWrappedRT) {
1140 if (VK_NULL_HANDLE == info.fImage) {
1141 return false;
1142 }
1143
1144 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001145 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001146 }
1147
Greg Daniel7e000222018-12-03 10:08:21 -05001148 if (info.fYcbcrConversionInfo.isValid()) {
1149 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1150 return false;
1151 }
jvanverthfd359ca2016-03-18 11:57:24 -07001152 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001153
Greg Danielcb324152019-02-25 11:36:53 -05001154 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1155 return false;
1156 }
1157
Greg Daniel52e16d92018-04-10 09:34:07 -04001158 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001159 return true;
1160}
1161
1162sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001163 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1164 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001165 GrVkImageInfo imageInfo;
1166 if (!backendTex.getVkImageInfo(&imageInfo)) {
1167 return nullptr;
1168 }
1169
Greg Danielcb324152019-02-25 11:36:53 -05001170 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001171 return nullptr;
1172 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001173
Greg Daniel164a9f02016-02-22 09:56:40 -05001174 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001175 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001176 surfDesc.fWidth = backendTex.width();
1177 surfDesc.fHeight = backendTex.height();
1178 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001179 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001180
Greg Daniel52e16d92018-04-10 09:34:07 -04001181 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1182 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001183 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1184 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001185}
1186
1187sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001188 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001189 GrWrapOwnership ownership,
1190 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001191 GrVkImageInfo imageInfo;
1192 if (!backendTex.getVkImageInfo(&imageInfo)) {
1193 return nullptr;
1194 }
1195
Greg Danielcb324152019-02-25 11:36:53 -05001196 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001197 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001198 }
Brian Salomond17f6582017-07-19 18:28:58 -04001199
1200 GrSurfaceDesc surfDesc;
1201 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1202 surfDesc.fWidth = backendTex.width();
1203 surfDesc.fHeight = backendTex.height();
1204 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001205 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001206
Greg Daniel52e16d92018-04-10 09:34:07 -04001207 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1208 SkASSERT(layout);
1209
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001210 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1211 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001212}
1213
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001214sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001215 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1216 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1217 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1218 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001219 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001220 return nullptr;
1221 }
halcanary9d524f22016-03-29 09:03:52 -07001222
Greg Daniel323fbcf2018-04-10 13:46:30 -04001223 GrVkImageInfo info;
1224 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001225 return nullptr;
1226 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001227
Greg Danielcb324152019-02-25 11:36:53 -05001228 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001229 return nullptr;
1230 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001231
Greg Daniel164a9f02016-02-22 09:56:40 -05001232 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001233 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001234 desc.fWidth = backendRT.width();
1235 desc.fHeight = backendRT.height();
1236 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001237 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001238
Greg Daniel323fbcf2018-04-10 13:46:30 -04001239 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001240
Greg Daniel323fbcf2018-04-10 13:46:30 -04001241 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001242 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001243
1244 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1245 SkASSERT(!backendRT.stencilBits());
1246 if (tgt) {
1247 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001248 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001249
Ben Wagnerff134f22018-04-24 16:29:16 -04001250 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001251}
1252
Greg Daniel7ef28f32017-04-20 16:41:55 +00001253sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001254 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001255
Greg Daniel52e16d92018-04-10 09:34:07 -04001256 GrVkImageInfo imageInfo;
1257 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001258 return nullptr;
1259 }
Greg Danielcb324152019-02-25 11:36:53 -05001260 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001261 return nullptr;
1262 }
1263
Greg Danielcb324152019-02-25 11:36:53 -05001264
Brian Osman33910292017-04-18 14:38:53 -04001265 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001266 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001267 desc.fWidth = tex.width();
1268 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001269 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001270 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1271 if (!desc.fSampleCnt) {
1272 return nullptr;
1273 }
Brian Osman33910292017-04-18 14:38:53 -04001274
Greg Daniel52e16d92018-04-10 09:34:07 -04001275 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1276 SkASSERT(layout);
1277
Ben Wagnerff134f22018-04-24 16:29:16 -04001278 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001279}
1280
Greg Danielb46add82019-01-02 14:51:29 -05001281sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1282 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1283 int maxSize = this->caps()->maxTextureSize();
1284 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1285 return nullptr;
1286 }
1287
1288 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1289 if (!backendFormat.isValid()) {
1290 return nullptr;
1291 }
1292 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1293 imageInfo.colorType());
1294 if (config == kUnknown_GrPixelConfig) {
1295 return nullptr;
1296 }
1297
1298 GrSurfaceDesc desc;
1299 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1300 desc.fWidth = imageInfo.width();
1301 desc.fHeight = imageInfo.height();
1302 desc.fConfig = config;
1303 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1304 if (!desc.fSampleCnt) {
1305 return nullptr;
1306 }
1307
1308 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1309}
1310
Brian Salomon930f9392018-06-20 16:25:26 -04001311bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1312 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001313 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001314 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001315 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001316 return false;
jvanverth62340062016-04-26 08:01:44 -07001317 }
1318
jvanverth62340062016-04-26 08:01:44 -07001319 // determine if we can blit to and from this format
1320 const GrVkCaps& caps = this->vkCaps();
1321 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001322 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1323 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001324 return false;
jvanverth62340062016-04-26 08:01:44 -07001325 }
1326
egdaniel7ac5da82016-07-15 13:41:42 -07001327 int width = tex->width();
1328 int height = tex->height();
1329 VkImageBlit blitRegion;
1330 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001331
jvanverth82c05582016-05-03 11:19:01 -07001332 // SkMipMap doesn't include the base level in the level count so we have to add 1
1333 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001334 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001335
Greg Danielda86e282018-06-13 09:41:19 -04001336 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001337 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1338 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001339
jvanverth50c46c72016-05-06 12:31:28 -07001340 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001341 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001342 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1343 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001344 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1345 nullptr, // pNext
1346 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1347 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1348 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1349 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1350 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1351 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1352 vkTex->image(), // image
1353 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001354 };
1355
jvanverth62340062016-04-26 08:01:44 -07001356 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001357 uint32_t mipLevel = 1;
1358 while (mipLevel < levelCount) {
1359 int prevWidth = width;
1360 int prevHeight = height;
1361 width = SkTMax(1, width / 2);
1362 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001363
jvanverth50c46c72016-05-06 12:31:28 -07001364 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001365 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1366 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001367
1368 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001369 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001370 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001371 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1372 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001373 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001374 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001375 vkTex->resource(),
1376 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001377 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001378 vkTex->resource(),
1379 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001380 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001381 1,
1382 &blitRegion,
1383 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001384 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001385 }
Greg Danielee54f232019-04-03 14:58:40 -04001386 if (levelCount > 1) {
1387 // This barrier logically is not needed, but it changes the final level to the same layout
1388 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1389 // layouts and future layout changes easier. The alternative here would be to track layout
1390 // and memory accesses per layer which doesn't seem work it.
1391 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1392 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1393 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1394 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1395 }
Brian Salomon930f9392018-06-20 16:25:26 -04001396 return true;
jvanverth62340062016-04-26 08:01:44 -07001397}
1398
Greg Daniel164a9f02016-02-22 09:56:40 -05001399////////////////////////////////////////////////////////////////////////////////
1400
1401GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1402 int width,
1403 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001404 SkASSERT(width >= rt->width());
1405 SkASSERT(height >= rt->height());
1406
1407 int samples = rt->numStencilSamples();
1408
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001409 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001410
1411 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001412 width,
1413 height,
1414 samples,
1415 sFmt));
1416 fStats.incStencilAttachmentCreates();
1417 return stencil;
1418}
1419
1420////////////////////////////////////////////////////////////////////////////////
1421
Brian Salomon52e943a2018-03-13 09:32:39 -04001422bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001423 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1424 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001425 VkDeviceSize size = dstRowBytes * h;
1426 VkDeviceSize offset = bufferOffset;
1427 SkASSERT(size + offset <= alloc.fSize);
1428 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1429 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001430 return false;
1431 }
Greg Daniel81df0412018-05-31 13:13:33 -04001432 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001433
Greg Daniel20ece3a2017-03-28 10:24:43 -04001434 if (srcData) {
1435 // If there is no padding on dst we can do a single memcopy.
1436 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001437 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001438 } else {
1439 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1440 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001441 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001442 }
Greg Daniel81df0412018-05-31 13:13:33 -04001443 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1444 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001445 return true;
1446}
1447
Brian Salomonf865b052018-03-09 09:01:53 -05001448#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001449bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1450 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001451 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001452 SkASSERT(texturable || renderable);
1453 if (!texturable) {
1454 SkASSERT(GrMipMapped::kNo == mipMapped);
1455 SkASSERT(!srcData);
1456 }
Robert Phillips0221e8b2019-05-13 19:55:23 +00001457 VkFormat pixelFormat;
1458 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001459 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001460 }
1461
Brian Salomon52e943a2018-03-13 09:32:39 -04001462 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1463 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001464 }
1465
Brian Salomon52e943a2018-03-13 09:32:39 -04001466 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1467 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001468 }
1469
1470 // Currently we don't support uploading pixel data when mipped.
1471 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001472 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001473 }
1474
Brian Salomon52e943a2018-03-13 09:32:39 -04001475 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001476 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1477 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001478 if (texturable) {
1479 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1480 }
1481 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001482 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1483 }
1484
1485 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001486 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001487 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001488
1489 // Create Image
1490 VkSampleCountFlagBits vkSamples;
1491 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001492 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001493 }
1494
1495 // Figure out the number of mip levels.
1496 uint32_t mipLevels = 1;
1497 if (GrMipMapped::kYes == mipMapped) {
1498 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1499 }
1500
1501 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001502 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1503 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001504 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001505 VK_IMAGE_TYPE_2D, // VkImageType
Robert Phillips0221e8b2019-05-13 19:55:23 +00001506 pixelFormat, // VkFormat
Brian Salomonde9f5462018-03-07 14:23:58 -05001507 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1508 mipLevels, // mipLevels
1509 1, // arrayLayers
1510 vkSamples, // samples
1511 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1512 usageFlags, // VkImageUsageFlags
1513 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1514 0, // queueFamilyCount
1515 0, // pQueueFamilyIndices
1516 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001517 };
1518
Brian Salomon52e943a2018-03-13 09:32:39 -04001519 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1520 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001521
Brian Salomonde9f5462018-03-07 14:23:58 -05001522 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001523 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001524 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001525 }
1526
1527 // 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 -05001528 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001529 VkBuffer buffer = VK_NULL_HANDLE;
1530
1531 VkResult err;
1532 const VkCommandBufferAllocateInfo cmdInfo = {
1533 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1534 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001535 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001536 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1537 1 // bufferCount
1538 };
1539
1540 VkCommandBuffer cmdBuffer;
1541 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1542 if (err) {
1543 GrVkMemory::FreeImageMemory(this, false, alloc);
1544 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001545 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001546 }
1547
1548 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1549 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1550 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1551 cmdBufferBeginInfo.pNext = nullptr;
1552 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1553 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1554
1555 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1556 SkASSERT(!err);
1557
Robert Phillips0221e8b2019-05-13 19:55:23 +00001558 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001559 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001560
Robert Phillips646f6372018-09-25 09:31:10 -04001561 const size_t trimRowBytes = w * bpp;
1562 if (!srcRowBytes) {
1563 srcRowBytes = trimRowBytes;
1564 }
1565
Brian Salomonde9f5462018-03-07 14:23:58 -05001566 SkTArray<size_t> individualMipOffsets(mipLevels);
1567 individualMipOffsets.push_back(0);
1568 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001569 if (GrPixelConfigIsCompressed(config)) {
1570 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1571 bpp = 4; // we have at least this alignment, which will pass the code below
1572 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001573 int currentWidth = w;
1574 int currentHeight = h;
1575 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1576 // config. This works with the assumption that the bytes in pixel config is always a power
1577 // of 2.
1578 SkASSERT((bpp & (bpp - 1)) == 0);
1579 const size_t alignmentMask = 0x3 | (bpp - 1);
1580 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1581 currentWidth = SkTMax(1, currentWidth / 2);
1582 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001583
Jim Van Verth1676cb92019-01-15 13:24:45 -05001584 size_t trimmedSize;
1585 if (GrPixelConfigIsCompressed(config)) {
1586 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1587 } else {
1588 trimmedSize = currentWidth * bpp * currentHeight;
1589 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001590 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1591 if (alignmentDiff != 0) {
1592 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001593 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001594 individualMipOffsets.push_back(combinedBufferSize);
1595 combinedBufferSize += trimmedSize;
1596 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001597
Brian Salomonde9f5462018-03-07 14:23:58 -05001598 VkBufferCreateInfo bufInfo;
1599 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1600 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1601 bufInfo.flags = 0;
1602 bufInfo.size = combinedBufferSize;
1603 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1604 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1605 bufInfo.queueFamilyIndexCount = 0;
1606 bufInfo.pQueueFamilyIndices = nullptr;
1607 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001608
Brian Salomonde9f5462018-03-07 14:23:58 -05001609 if (err) {
1610 GrVkMemory::FreeImageMemory(this, false, alloc);
1611 VK_CALL(DestroyImage(fDevice, image, nullptr));
1612 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001613 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001614 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001615 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001616
Brian Salomonde9f5462018-03-07 14:23:58 -05001617 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1618 &bufferAlloc)) {
1619 GrVkMemory::FreeImageMemory(this, false, alloc);
1620 VK_CALL(DestroyImage(fDevice, image, nullptr));
1621 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1622 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001623 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001624 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001625 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001626
Brian Salomonde9f5462018-03-07 14:23:58 -05001627 currentWidth = w;
1628 currentHeight = h;
1629 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1630 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001631 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001632 bool result;
1633 if (GrPixelConfigIsCompressed(config)) {
1634 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1635 size_t currentRowBytes = levelSize / currentHeight;
1636 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1637 currentRowBytes, currentRowBytes, currentHeight);
1638 } else {
1639 size_t currentRowBytes = bpp * currentWidth;
1640 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1641 currentRowBytes, trimRowBytes, currentHeight);
1642 }
1643 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001644 GrVkMemory::FreeImageMemory(this, false, alloc);
1645 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001646 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001647 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1648 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001649 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001650 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001651 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001652 currentWidth = SkTMax(1, currentWidth / 2);
1653 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001654 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001655
1656 // Set image layout and add barrier
1657 VkImageMemoryBarrier barrier;
1658 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1659 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1660 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001661 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001662 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1663 barrier.oldLayout = initialLayout;
1664 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1665 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1666 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1667 barrier.image = image;
1668 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1669
Greg Danielf7828d02018-10-09 12:01:32 -04001670 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001671 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1672 &barrier));
1673 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1674
1675 SkTArray<VkBufferImageCopy> regions(mipLevels);
1676
1677 currentWidth = w;
1678 currentHeight = h;
1679 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1680 // Submit copy command
1681 VkBufferImageCopy& region = regions.push_back();
1682 memset(&region, 0, sizeof(VkBufferImageCopy));
1683 region.bufferOffset = individualMipOffsets[currentMipLevel];
1684 region.bufferRowLength = currentWidth;
1685 region.bufferImageHeight = currentHeight;
1686 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1687 region.imageOffset = {0, 0, 0};
1688 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1689 currentWidth = SkTMax(1, currentWidth / 2);
1690 currentHeight = SkTMax(1, currentHeight / 2);
1691 }
1692
1693 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1694 regions.begin()));
1695
Brian Salomon52e943a2018-03-13 09:32:39 -04001696 if (texturable) {
1697 // Change Image layout to shader read since if we use this texture as a borrowed textures
1698 // within Ganesh we require that its layout be set to that
1699 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1700 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1701 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001702 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001703 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1704 barrier.oldLayout = initialLayout;
1705 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1706 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1707 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1708 barrier.image = image;
1709 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001710 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001711 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1712 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001713 0,
1714 0, nullptr,
1715 0, nullptr,
1716 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001717 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001718 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001719
1720 // End CommandBuffer
1721 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1722 SkASSERT(!err);
1723
1724 // Create Fence for queue
1725 VkFence fence;
1726 VkFenceCreateInfo fenceInfo;
1727 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1728 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1729
1730 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1731 SkASSERT(!err);
1732
1733 VkSubmitInfo submitInfo;
1734 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1735 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1736 submitInfo.pNext = nullptr;
1737 submitInfo.waitSemaphoreCount = 0;
1738 submitInfo.pWaitSemaphores = nullptr;
1739 submitInfo.pWaitDstStageMask = 0;
1740 submitInfo.commandBufferCount = 1;
1741 submitInfo.pCommandBuffers = &cmdBuffer;
1742 submitInfo.signalSemaphoreCount = 0;
1743 submitInfo.pSignalSemaphores = nullptr;
1744 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1745 SkASSERT(!err);
1746
1747 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1748 if (VK_TIMEOUT == err) {
1749 GrVkMemory::FreeImageMemory(this, false, alloc);
1750 VK_CALL(DestroyImage(fDevice, image, nullptr));
1751 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1752 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
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 SkDebugf("Fence failed to signal: %d\n", err);
1756 SK_ABORT("failing");
1757 }
1758 SkASSERT(!err);
1759
1760 // Clean up transfer resources
1761 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1762 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1763 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1764 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001765 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001766 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1767
Brian Salomon52e943a2018-03-13 09:32:39 -04001768 info->fImage = image;
1769 info->fAlloc = alloc;
1770 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1771 info->fImageLayout = initialLayout;
Robert Phillips0221e8b2019-05-13 19:55:23 +00001772 info->fFormat = pixelFormat;
Brian Salomon52e943a2018-03-13 09:32:39 -04001773 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001774
Brian Salomon52e943a2018-03-13 09:32:39 -04001775 return true;
1776}
1777
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001778static bool vk_format_to_pixel_config(VkFormat format, GrPixelConfig* config) {
1779 GrPixelConfig dontCare;
1780 if (!config) {
1781 config = &dontCare;
1782 }
1783
1784 switch (format) {
1785 case VK_FORMAT_UNDEFINED:
1786 *config = kUnknown_GrPixelConfig;
1787 return false;
1788 case VK_FORMAT_R8G8B8A8_UNORM:
1789 *config = kRGBA_8888_GrPixelConfig;
1790 return true;
1791 case VK_FORMAT_R8G8B8_UNORM:
1792 *config = kRGB_888_GrPixelConfig;
1793 return true;
1794 case VK_FORMAT_R8G8_UNORM:
1795 *config = kRG_88_GrPixelConfig;
1796 return true;
1797 case VK_FORMAT_B8G8R8A8_UNORM:
1798 *config = kBGRA_8888_GrPixelConfig;
1799 return true;
1800 case VK_FORMAT_R8G8B8A8_SRGB:
1801 *config = kSRGBA_8888_GrPixelConfig;
1802 return true;
1803 case VK_FORMAT_B8G8R8A8_SRGB:
1804 *config = kSBGRA_8888_GrPixelConfig;
1805 return true;
1806 case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
1807 *config = kRGBA_1010102_GrPixelConfig;
1808 return true;
1809 case VK_FORMAT_R5G6B5_UNORM_PACK16:
1810 *config = kRGB_565_GrPixelConfig;
1811 return true;
1812 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
1813 *config = kRGBA_4444_GrPixelConfig; // we're swizzling in this case
1814 return true;
1815 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
1816 *config = kRGBA_4444_GrPixelConfig;
1817 return true;
1818 case VK_FORMAT_R8_UNORM:
1819 *config = kAlpha_8_GrPixelConfig;
1820 return true;
1821 case VK_FORMAT_R32G32B32A32_SFLOAT:
1822 *config = kRGBA_float_GrPixelConfig;
1823 return true;
1824 case VK_FORMAT_R32G32_SFLOAT:
1825 *config = kRG_float_GrPixelConfig;
1826 return true;
1827 case VK_FORMAT_R16G16B16A16_SFLOAT:
1828 *config = kRGBA_half_GrPixelConfig;
1829 return true;
1830 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
1831 *config = kRGB_ETC1_GrPixelConfig;
1832 return true;
1833 case VK_FORMAT_R16_SFLOAT:
1834 *config = kAlpha_half_GrPixelConfig;
1835 return true;
1836 default:
1837 return false;
1838 }
1839 SK_ABORT("Unexpected config");
1840 return false;
1841}
1842
1843GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(int w, int h,
1844 const GrBackendFormat& format,
1845 GrMipMapped mipMapped,
1846 GrRenderable renderable,
1847 const void* srcData, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001848 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001849
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001850
Robert Phillipsa479f962018-04-10 11:45:40 -04001851 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1852 return GrBackendTexture();
1853 }
1854
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001855 const VkFormat* vkFormat = format.getVkFormat();
1856 if (!vkFormat) {
1857 return GrBackendTexture();
1858 }
1859
1860 GrPixelConfig config;
1861
1862 if (!vk_format_to_pixel_config(*vkFormat, &config)) {
1863 return GrBackendTexture();
1864 }
Robert Phillips646f6372018-09-25 09:31:10 -04001865 if (!this->caps()->isConfigTexturable(config)) {
1866 return GrBackendTexture();
1867 }
1868
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001869 GrVkImageInfo info;
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001870 if (!this->createTestingOnlyVkImage(config, w, h, true, GrRenderable::kYes == renderable,
1871 mipMapped, srcData, rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001872 return {};
1873 }
Greg Daniel108bb232018-07-03 16:18:29 -04001874 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1875 // Lots of tests don't go through Skia's public interface which will set the config so for
1876 // testing we make sure we set a config here.
1877 beTex.setPixelConfig(config);
1878 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001879}
1880
1881bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001882 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001883
Greg Daniel52e16d92018-04-10 09:34:07 -04001884 GrVkImageInfo backend;
1885 if (!tex.getVkImageInfo(&backend)) {
1886 return false;
1887 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001888
Greg Daniel52e16d92018-04-10 09:34:07 -04001889 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001890 VkMemoryRequirements req;
1891 memset(&req, 0, sizeof(req));
1892 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001893 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001894 &req));
1895 // TODO: find a better check
1896 // This will probably fail with a different driver
1897 return (req.size > 0) && (req.size <= 8192 * 8192);
1898 }
1899
1900 return false;
1901}
1902
Brian Salomon26102cb2018-03-09 09:33:19 -05001903void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001904 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001905
Greg Daniel52e16d92018-04-10 09:34:07 -04001906 GrVkImageInfo info;
1907 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001908 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001909 }
1910}
1911
Brian Osman2d010b62018-08-09 10:55:09 -04001912GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001913 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1914 return GrBackendRenderTarget();
1915 }
1916
Brian Salomon8a375832018-03-14 10:21:40 -04001917 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001918 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001919 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001920 if (kUnknown_GrPixelConfig == config) {
1921 return {};
1922 }
Robert Phillips646f6372018-09-25 09:31:10 -04001923 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001924 &info)) {
1925 return {};
1926 }
Greg Daniel108bb232018-07-03 16:18:29 -04001927 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1928 // Lots of tests don't go through Skia's public interface which will set the config so for
1929 // testing we make sure we set a config here.
1930 beRT.setPixelConfig(config);
1931 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001932}
1933
Brian Salomon52e943a2018-03-13 09:32:39 -04001934void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001935 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001936
Greg Daniel323fbcf2018-04-10 13:46:30 -04001937 GrVkImageInfo info;
1938 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001939 // something in the command buffer may still be using this, so force submit
1940 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001941 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001942 }
1943}
Brian Salomonf865b052018-03-09 09:01:53 -05001944
Greg Daniel26b50a42018-03-08 09:49:58 -05001945void GrVkGpu::testingOnly_flushGpuAndSync() {
1946 this->submitCommandBuffer(kForce_SyncQueue);
1947}
Brian Salomonf865b052018-03-09 09:01:53 -05001948#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001949
Greg Daniel164a9f02016-02-22 09:56:40 -05001950////////////////////////////////////////////////////////////////////////////////
1951
Greg Daniel59dc1482019-02-22 10:46:38 -05001952void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1953 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001954 VkPipelineStageFlags dstStageMask,
1955 bool byRegion,
1956 VkBufferMemoryBarrier* barrier) const {
1957 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001958 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001959 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001960 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001961 srcStageMask,
1962 dstStageMask,
1963 byRegion,
1964 GrVkCommandBuffer::kBufferMemory_BarrierType,
1965 barrier);
1966}
1967
Greg Daniel59dc1482019-02-22 10:46:38 -05001968void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1969 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001970 VkPipelineStageFlags dstStageMask,
1971 bool byRegion,
1972 VkImageMemoryBarrier* barrier) const {
1973 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001974 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001975 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001976 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001977 srcStageMask,
1978 dstStageMask,
1979 byRegion,
1980 GrVkCommandBuffer::kImageMemory_BarrierType,
1981 barrier);
1982}
1983
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001984void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04001985 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
1986 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001987 SkASSERT(n >= 0);
1988 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00001989 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1990 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001991 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05001992 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001993 for (int i = 0; i < n; ++i) {
1994 SkASSERT(proxies[i]->isInstantiated());
1995 if (GrTexture* tex = proxies[i]->peekTexture()) {
1996 image = static_cast<GrVkTexture*>(tex);
1997 } else {
1998 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
1999 SkASSERT(rt);
2000 image = static_cast<GrVkRenderTarget*>(rt);
2001 }
2002 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05002003 }
Greg Danielbae71212019-03-01 15:24:35 -05002004 }
Greg Daniel797efca2019-05-09 14:04:20 -04002005
2006 // Handle requests for preparing for external IO
2007 for (int i = 0; i < externalRequests.fNumImages; ++i) {
2008 SkImage* image = externalRequests.fImages[i];
2009 if (!image->isTextureBacked()) {
2010 continue;
2011 }
2012 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
2013 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
2014 SkASSERT(proxy);
2015
2016 if (!proxy->isInstantiated()) {
2017 auto resourceProvider = this->getContext()->priv().resourceProvider();
2018 if (!proxy->instantiate(resourceProvider)) {
2019 continue;
2020 }
2021 }
2022
2023 GrTexture* tex = proxy->peekTexture();
2024 if (!tex) {
2025 continue;
2026 }
2027 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
2028 vkTex->prepareForExternal(this);
2029 }
2030 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
2031 SkSurface* surface = externalRequests.fSurfaces[i];
2032 if (!surface->getCanvas()->getGrContext()) {
2033 continue;
2034 }
2035 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
2036 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
2037 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
2038 if (!proxy->isInstantiated()) {
2039 auto resourceProvider = this->getContext()->priv().resourceProvider();
2040 if (!proxy->instantiate(resourceProvider)) {
2041 continue;
2042 }
2043 }
2044
2045 GrRenderTarget* rt = proxy->peekRenderTarget();
2046 SkASSERT(rt);
2047 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
2048 if (externalRequests.fPrepareSurfaceForPresent &&
2049 externalRequests.fPrepareSurfaceForPresent[i]) {
2050 vkRT->prepareForPresent(this);
2051 } else {
2052 vkRT->prepareForExternal(this);
2053 }
2054 }
2055
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002056 if (info.fFlags & kSyncCpu_GrFlushFlag) {
2057 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002058 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002059 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002060 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002061}
2062
Greg Daniel25af6712018-04-25 10:44:38 -04002063static int get_surface_sample_cnt(GrSurface* surf) {
2064 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
2065 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07002066 }
Greg Daniel25af6712018-04-25 10:44:38 -04002067 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05002068}
2069
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002070void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2071 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002072 GrVkImage* dstImage,
2073 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002074 const SkIRect& srcRect,
2075 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002076#ifdef SK_DEBUG
2077 int dstSampleCnt = get_surface_sample_cnt(dst);
2078 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002079 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2080 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2081 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
2082 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04002083
2084#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05002085
Greg Daniel164a9f02016-02-22 09:56:40 -05002086 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
2087 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002088 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002089 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2090 VK_ACCESS_TRANSFER_WRITE_BIT,
2091 VK_PIPELINE_STAGE_TRANSFER_BIT,
2092 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002093
egdaniel17b89252016-04-05 07:23:38 -07002094 srcImage->setImageLayout(this,
2095 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002096 VK_ACCESS_TRANSFER_READ_BIT,
2097 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002098 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002099
2100 // Flip rect if necessary
2101 SkIRect srcVkRect = srcRect;
2102 int32_t dstY = dstPoint.fY;
2103
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002104 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2105 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05002106 srcVkRect.fTop = src->height() - srcRect.fBottom;
2107 srcVkRect.fBottom = src->height() - srcRect.fTop;
2108 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
2109 }
2110
2111 VkImageCopy copyRegion;
2112 memset(&copyRegion, 0, sizeof(VkImageCopy));
2113 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2114 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
2115 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2116 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07002117 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002118
2119 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002120 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002121 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002122 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002123 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2124 1,
2125 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002126
2127 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2128 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002129 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002130}
2131
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002132void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2133 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07002134 GrVkImage* dstImage,
2135 GrVkImage* srcImage,
2136 const SkIRect& srcRect,
2137 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002138#ifdef SK_DEBUG
2139 int dstSampleCnt = get_surface_sample_cnt(dst);
2140 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002141 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2142 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04002143 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002144 dstHasYcbcr, src->config(), srcSampleCnt,
2145 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002146
Greg Daniel25af6712018-04-25 10:44:38 -04002147#endif
egdaniel17b89252016-04-05 07:23:38 -07002148 dstImage->setImageLayout(this,
2149 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002150 VK_ACCESS_TRANSFER_WRITE_BIT,
2151 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002152 false);
2153
egdaniel17b89252016-04-05 07:23:38 -07002154 srcImage->setImageLayout(this,
2155 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002156 VK_ACCESS_TRANSFER_READ_BIT,
2157 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002158 false);
2159
2160 // Flip rect if necessary
2161 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002162 srcVkRect.fLeft = srcRect.fLeft;
2163 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002164 SkIRect dstRect;
2165 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002166 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002167
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002168 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002169 srcVkRect.fTop = src->height() - srcRect.fBottom;
2170 srcVkRect.fBottom = src->height() - srcRect.fTop;
2171 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002172 srcVkRect.fTop = srcRect.fTop;
2173 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002174 }
2175
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002176 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002177 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2178 } else {
2179 dstRect.fTop = dstPoint.fY;
2180 }
2181 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2182
2183 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2184 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002185 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002186 using std::swap;
2187 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002188 }
2189
2190 VkImageBlit blitRegion;
2191 memset(&blitRegion, 0, sizeof(VkImageBlit));
2192 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2193 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002194 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002195 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2196 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002197 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002198
2199 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002200 *srcImage,
2201 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002202 1,
2203 &blitRegion,
2204 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002205
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002206 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002207 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002208}
2209
Brian Salomon1fabd512018-02-09 09:54:25 -05002210void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2211 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2212 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002213 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002214 SkIRect srcRect = origSrcRect;
2215 SkIPoint dstPoint = origDstPoint;
2216 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2217 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2218 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2219 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2220 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2221 }
2222 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002223 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2224 srcRect.width(), srcRect.height());
2225 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002226}
2227
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002228bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2229 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002230 const SkIRect& srcRect, const SkIPoint& dstPoint,
2231 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002232#ifdef SK_DEBUG
2233 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2234 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2235 }
2236 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2237 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2238 }
2239#endif
2240
Greg Daniel25af6712018-04-25 10:44:38 -04002241 GrPixelConfig dstConfig = dst->config();
2242 GrPixelConfig srcConfig = src->config();
2243
2244 int dstSampleCnt = get_surface_sample_cnt(dst);
2245 int srcSampleCnt = get_surface_sample_cnt(src);
2246
egdaniel17b89252016-04-05 07:23:38 -07002247 GrVkImage* dstImage;
2248 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002249 GrRenderTarget* dstRT = dst->asRenderTarget();
2250 if (dstRT) {
2251 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002252 if (vkRT->wrapsSecondaryCommandBuffer()) {
2253 return false;
2254 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002255 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2256 } else {
2257 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002258 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002259 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002260 GrRenderTarget* srcRT = src->asRenderTarget();
2261 if (srcRT) {
2262 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2263 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002264 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002265 SkASSERT(src->asTexture());
2266 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002267 }
2268
Greg Daniela51e93c2019-03-25 12:30:45 -04002269 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2270 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2271
2272 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2273 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2274 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2275 return true;
2276 }
2277
2278 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2279 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2280 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2281 dstPoint, canDiscardOutsideDstRect));
2282 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2283 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2284 return true;
2285 }
2286
2287 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2288 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002289 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2290 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002291 return true;
2292 }
2293
Greg Daniel25af6712018-04-25 10:44:38 -04002294 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002295 dstHasYcbcr, srcConfig, srcSampleCnt,
2296 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002297 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2298 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002299 return true;
2300 }
2301
Greg Daniel164a9f02016-02-22 09:56:40 -05002302 return false;
2303}
2304
Brian Salomona6948702018-06-01 15:33:20 -04002305bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2306 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002307 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002308 return false;
2309 }
2310
egdaniel66933552016-08-24 07:22:19 -07002311 GrVkImage* image = nullptr;
2312 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2313 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002314 // Reading from render targets that wrap a secondary command buffer is not allowed since
2315 // it would require us to know the VkImage, which we don't have, as well as need us to
2316 // stop and start the VkRenderPass which we don't have access to.
2317 if (rt->wrapsSecondaryCommandBuffer()) {
2318 return false;
2319 }
egdaniel66933552016-08-24 07:22:19 -07002320 // resolve the render target if necessary
2321 switch (rt->getResolveType()) {
2322 case GrVkRenderTarget::kCantResolve_ResolveType:
2323 return false;
2324 case GrVkRenderTarget::kAutoResolves_ResolveType:
2325 break;
2326 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002327 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002328 break;
2329 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002330 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002331 }
2332 image = rt;
2333 } else {
2334 image = static_cast<GrVkTexture*>(surface->asTexture());
2335 }
2336
2337 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002338 return false;
2339 }
2340
Greg Daniel475eb702018-09-28 14:16:50 -04002341 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2342 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2343 // image and then do the read pixels from that.
2344 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002345 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2346 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002347
2348 // Make a new surface that is RGBA to copy the RGB surface into.
2349 GrSurfaceDesc surfDesc;
2350 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2351 surfDesc.fWidth = width;
2352 surfDesc.fHeight = height;
2353 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2354 surfDesc.fSampleCnt = 1;
2355
2356 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2357 VK_IMAGE_USAGE_SAMPLED_BIT |
2358 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2359 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2360
2361 GrVkImage::ImageDesc imageDesc;
2362 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2363 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2364 imageDesc.fWidth = width;
2365 imageDesc.fHeight = height;
2366 imageDesc.fLevels = 1;
2367 imageDesc.fSamples = 1;
2368 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2369 imageDesc.fUsageFlags = usageFlags;
2370 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2371
2372 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2373 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2374 if (!copySurface) {
2375 return false;
2376 }
2377
2378 int srcSampleCount = 0;
2379 if (rt) {
2380 srcSampleCount = rt->numColorSamples();
2381 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002382 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel5c7b5412019-05-10 11:39:55 -04002383 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, false, false,
2384 surface->config(), srcSampleCount, image->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002385 srcHasYcbcr) &&
2386 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2387 surface->config(), SkToBool(surface->asTexture()),
2388 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002389 return false;
2390 }
2391 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
Greg Daniel5c7b5412019-05-10 11:39:55 -04002392 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniel475eb702018-09-28 14:16:50 -04002393 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2394 srcRect, SkIPoint::Make(0,0))) {
2395 return false;
2396 }
2397 top = 0;
2398 left = 0;
2399 dstColorType = GrColorType::kRGBA_8888;
2400 image = copySurface.get();
2401 }
2402
Greg Daniel164a9f02016-02-22 09:56:40 -05002403 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002404 image->setImageLayout(this,
2405 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2406 VK_ACCESS_TRANSFER_READ_BIT,
2407 VK_PIPELINE_STAGE_TRANSFER_BIT,
2408 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002409
Brian Salomonc320b152018-02-20 14:05:36 -05002410 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002411 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002412
Greg Daniel164a9f02016-02-22 09:56:40 -05002413 VkBufferImageCopy region;
2414 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002415
2416 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2417 if (copyFromOrigin) {
2418 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002419 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002420 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002421 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002422 region.imageOffset = offset;
2423 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2424 }
2425
2426 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002427 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002428 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002429 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002430 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002431 kStream_GrAccessPattern)
2432 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002433
2434 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002435 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002436 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002437 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2438 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002439
2440 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002441 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002442 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002443 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002444 1,
2445 &region);
2446
2447 // make sure the copy to buffer has finished
2448 transferBuffer->addMemoryBarrier(this,
2449 VK_ACCESS_TRANSFER_WRITE_BIT,
2450 VK_ACCESS_HOST_READ_BIT,
2451 VK_PIPELINE_STAGE_TRANSFER_BIT,
2452 VK_PIPELINE_STAGE_HOST_BIT,
2453 false);
2454
2455 // We need to submit the current command buffer to the Queue and make sure it finishes before
2456 // we can copy the data out of the buffer.
2457 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002458 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002459 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002460 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002461
egdaniel6fa0a912016-09-12 11:51:29 -07002462 if (copyFromOrigin) {
2463 uint32_t skipRows = region.imageExtent.height - height;
2464 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2465 }
2466
Brian Salomona6948702018-06-01 15:33:20 -04002467 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002468
2469 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002470 return true;
2471}
egdaniel066df7c2016-06-08 14:02:27 -07002472
egdaniel27bb2842016-07-07 11:58:35 -07002473// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2474// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2475// the the entire attachment. Similar requirements for the y and height components.
2476void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2477 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2478 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002479 if ((0 != granularity.width && 1 != granularity.width)) {
2480 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2481 int rightAdj = srcBounds.fRight % granularity.width;
2482 if (rightAdj != 0) {
2483 rightAdj = granularity.width - rightAdj;
2484 }
2485 dstBounds->fRight = srcBounds.fRight + rightAdj;
2486 if (dstBounds->fRight > maxWidth) {
2487 dstBounds->fRight = maxWidth;
2488 dstBounds->fLeft = 0;
2489 } else {
2490 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2491 }
egdaniel27bb2842016-07-07 11:58:35 -07002492 } else {
egdanield5797b32016-09-20 12:57:45 -07002493 dstBounds->fLeft = srcBounds.fLeft;
2494 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002495 }
2496
2497 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002498 if ((0 != granularity.height && 1 != granularity.height)) {
2499 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2500 int bottomAdj = srcBounds.fBottom % granularity.height;
2501 if (bottomAdj != 0) {
2502 bottomAdj = granularity.height - bottomAdj;
2503 }
2504 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2505 if (dstBounds->fBottom > maxHeight) {
2506 dstBounds->fBottom = maxHeight;
2507 dstBounds->fTop = 0;
2508 } else {
2509 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2510 }
egdaniel27bb2842016-07-07 11:58:35 -07002511 } else {
egdanield5797b32016-09-20 12:57:45 -07002512 dstBounds->fTop = srcBounds.fTop;
2513 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002514 }
2515}
2516
Greg Daniel22bc8652017-03-22 15:45:43 -04002517void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002518 const GrVkRenderPass* renderPass,
2519 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002520 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002521 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002522 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002523 const SkIRect* pBounds = &bounds;
2524 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002525 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002526 flippedBounds = bounds;
2527 flippedBounds.fTop = target->height() - bounds.fBottom;
2528 flippedBounds.fBottom = target->height() - bounds.fTop;
2529 pBounds = &flippedBounds;
2530 }
2531
egdaniel27bb2842016-07-07 11:58:35 -07002532 // The bounds we use for the render pass should be of the granularity supported
2533 // by the device.
2534 const VkExtent2D& granularity = renderPass->granularity();
2535 SkIRect adjustedBounds;
2536 if ((0 != granularity.width && 1 != granularity.width) ||
2537 (0 != granularity.height && 1 != granularity.height)) {
2538 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2539 target->width(), target->height());
2540 pBounds = &adjustedBounds;
2541 }
2542
Robert Phillips95214472017-08-08 18:00:03 -04002543#ifdef SK_DEBUG
2544 uint32_t index;
2545 bool result = renderPass->colorAttachmentIndex(&index);
2546 SkASSERT(result && 0 == index);
2547 result = renderPass->stencilAttachmentIndex(&index);
2548 if (result) {
2549 SkASSERT(1 == index);
2550 }
2551#endif
2552 VkClearValue clears[2];
2553 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002554 clears[1].depthStencil.depth = 0.0f;
2555 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002556
2557 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002558 for (int i = 0; i < buffers.count(); ++i) {
2559 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2560 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002561 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002562
Brian Salomon1fabd512018-02-09 09:54:25 -05002563 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002564}
egdaniel9cb63402016-06-23 08:37:05 -07002565
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002566void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2567 if (buffer->asRTCommandBuffer()) {
2568 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2569
2570 fCachedRTCommandBuffer->submit();
2571 fCachedRTCommandBuffer->reset();
2572 } else {
2573 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2574
2575 fCachedTexCommandBuffer->submit();
2576 fCachedTexCommandBuffer->reset();
2577 }
2578}
2579
Greg Daniel6be35232017-03-01 17:01:09 -05002580GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002581 VkFenceCreateInfo createInfo;
2582 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2583 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2584 createInfo.pNext = nullptr;
2585 createInfo.flags = 0;
2586 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002587
2588 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2589 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2590
2591 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002592 return (GrFence)fence;
2593}
2594
Greg Daniel6be35232017-03-01 17:01:09 -05002595bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2596 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2597
2598 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002599 return (VK_SUCCESS == result);
2600}
2601
2602void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002603 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2604}
2605
Greg Daniela5cb7812017-06-16 09:45:32 -04002606sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2607 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002608}
2609
Greg Daniel48661b82018-01-22 16:11:35 -05002610sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2611 GrResourceProvider::SemaphoreWrapType wrapType,
2612 GrWrapOwnership ownership) {
2613 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002614}
2615
Greg Daniel858e12c2018-12-06 11:11:37 -05002616void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002617 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2618
Greg Daniel48661b82018-01-22 16:11:35 -05002619 GrVkSemaphore::Resource* resource = vkSem->getResource();
2620 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002621 resource->ref();
2622 fSemaphoresToSignal.push_back(resource);
2623 }
Greg Daniel6be35232017-03-01 17:01:09 -05002624}
2625
Greg Daniel48661b82018-01-22 16:11:35 -05002626void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002627 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2628
Greg Daniel48661b82018-01-22 16:11:35 -05002629 GrVkSemaphore::Resource* resource = vkSem->getResource();
2630 if (resource->shouldWait()) {
2631 resource->ref();
2632 fSemaphoresToWaitOn.push_back(resource);
2633 }
jvanverth84741b32016-09-30 08:39:02 -07002634}
Brian Osman13dddce2017-05-09 13:19:50 -04002635
2636sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2637 SkASSERT(texture);
2638 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2639 vkTexture->setImageLayout(this,
2640 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2641 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002642 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002643 false);
2644 this->submitCommandBuffer(kSkip_SyncQueue);
2645
Greg Danielb3f66542019-05-10 17:11:19 -04002646 // The image layout change serves as a barrier, so no semaphore is needed.
2647 // If we ever decide we need to return a semaphore here, we need to make sure GrVkSemaphore is
2648 // thread safe so that only the first thread that tries to use the semaphore actually submits
2649 // it. This additionally would also require thread safety in command buffer submissions to
2650 // queues in general.
Brian Osman13dddce2017-05-09 13:19:50 -04002651 return nullptr;
2652}
Greg Danielf5d87582017-12-18 14:48:15 -05002653
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002654void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2655 fDrawables.emplace_back(std::move(drawable));
2656}
2657
Greg Daniel7a82edf2018-12-04 10:54:34 -05002658uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2659 const GrBackendFormat& format) {
2660 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2661 SkASSERT(ycbcrInfo);
2662 if (!ycbcrInfo->isValid()) {
2663 return 0;
2664 }
2665
2666 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2667 samplerState, *ycbcrInfo);
2668
2669 return sampler->uniqueID();
2670}
2671
Greg Daniela870b462019-01-08 15:49:46 -05002672void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002673 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002674 this->resourceProvider().storePipelineCacheData();
2675 }
2676}