blob: 4599fcfdca4373c66fc47e7fe3a289d7b348d2e6 [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"
Robert Phillipsbd1ef682019-05-31 12:48:49 -040017#include "src/gpu/GrDataUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrGeometryProcessor.h"
19#include "src/gpu/GrGpuResourceCacheAccess.h"
20#include "src/gpu/GrMesh.h"
21#include "src/gpu/GrPipeline.h"
Greg Daniel797efca2019-05-09 14:04:20 -040022#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrRenderTargetPriv.h"
24#include "src/gpu/GrTexturePriv.h"
Greg Daniel797efca2019-05-09 14:04:20 -040025#include "src/gpu/SkGpuDevice.h"
Robert Phillips9dbcdcc2019-05-13 10:40:06 -040026#include "src/gpu/SkGr.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/vk/GrVkAMDMemoryAllocator.h"
28#include "src/gpu/vk/GrVkCommandBuffer.h"
29#include "src/gpu/vk/GrVkCommandPool.h"
30#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
31#include "src/gpu/vk/GrVkImage.h"
32#include "src/gpu/vk/GrVkIndexBuffer.h"
33#include "src/gpu/vk/GrVkInterface.h"
34#include "src/gpu/vk/GrVkMemory.h"
35#include "src/gpu/vk/GrVkPipeline.h"
36#include "src/gpu/vk/GrVkPipelineState.h"
37#include "src/gpu/vk/GrVkRenderPass.h"
38#include "src/gpu/vk/GrVkResourceProvider.h"
39#include "src/gpu/vk/GrVkSemaphore.h"
40#include "src/gpu/vk/GrVkTexture.h"
41#include "src/gpu/vk/GrVkTextureRenderTarget.h"
42#include "src/gpu/vk/GrVkTransferBuffer.h"
43#include "src/gpu/vk/GrVkVertexBuffer.h"
Greg Daniel797efca2019-05-09 14:04:20 -040044#include "src/image/SkImage_Gpu.h"
45#include "src/image/SkSurface_Gpu.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050046#include "src/sksl/SkSLCompiler.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040047
Mike Kleinc0bd9f92019-04-23 12:05:21 -050048#include "include/gpu/vk/GrVkExtensions.h"
49#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050050
Ben Wagnerf08d1d02018-06-18 15:11:00 -040051#include <utility>
52
Forrest Reiling44f85712017-03-27 23:22:20 -070053#if !defined(SK_BUILD_FOR_WIN)
54#include <unistd.h>
55#endif // !defined(SK_BUILD_FOR_WIN)
56
Greg Danieldef55462018-08-01 13:40:14 -040057#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
Ben Wagnerab6eefe2019-05-20 11:02:49 -040058#include "src/core/SkLeanWindows.h"
Greg Danieldef55462018-08-01 13:40:14 -040059#endif
60
Greg Daniel164a9f02016-02-22 09:56:40 -050061#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
62#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
63#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
64
Greg Danielf730c182018-07-02 20:15:37 +000065sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050066 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000067 if (backendContext.fInstance == VK_NULL_HANDLE ||
68 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
69 backendContext.fDevice == VK_NULL_HANDLE ||
70 backendContext.fQueue == VK_NULL_HANDLE) {
71 return nullptr;
72 }
Greg Danield3e65aa2018-08-01 09:19:45 -040073 if (!backendContext.fGetProc) {
74 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040075 }
Greg Danield3e65aa2018-08-01 09:19:45 -040076
Greg Daniel41f0e282019-01-28 13:15:05 -050077 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
78 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
79 backendContext.fGetProc("vkEnumerateInstanceVersion",
80 VK_NULL_HANDLE, VK_NULL_HANDLE));
81 uint32_t instanceVersion = 0;
82 if (!localEnumerateInstanceVersion) {
83 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
84 } else {
85 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
86 if (err) {
87 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
88 return nullptr;
89 }
90 }
91
Greg Danielc0b03d82018-08-03 14:41:15 -040092 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
93 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
94 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
95 backendContext.fInstance,
96 VK_NULL_HANDLE));
97
98 if (!localGetPhysicalDeviceProperties) {
99 return nullptr;
100 }
101 VkPhysicalDeviceProperties physDeviceProperties;
102 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
103 uint32_t physDevVersion = physDeviceProperties.apiVersion;
104
Greg Daniel41f0e282019-01-28 13:15:05 -0500105 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
106 : instanceVersion;
107
108 instanceVersion = SkTMin(instanceVersion, apiVersion);
109 physDevVersion = SkTMin(physDevVersion, apiVersion);
110
Greg Daniel98bffae2018-08-01 13:25:41 -0400111 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400112
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 if (backendContext.fVkExtensions) {
114 interface.reset(new GrVkInterface(backendContext.fGetProc,
115 backendContext.fInstance,
116 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500117 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400118 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400119 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500120 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400121 return nullptr;
122 }
123 } else {
Greg Daniel98bffae2018-08-01 13:25:41 -0400124 GrVkExtensions extensions;
Greg Daniel88e8ddc2019-04-25 16:37:08 -0400125 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
126 // need to know if this is enabled to know if we can transition to a present layout when
127 // flushing a surface.
128 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
129 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
130 extensions.init(backendContext.fGetProc, backendContext.fInstance,
131 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
132 }
Greg Daniel98bffae2018-08-01 13:25:41 -0400133 interface.reset(new GrVkInterface(backendContext.fGetProc,
134 backendContext.fInstance,
135 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500136 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400137 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400138 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400140 return nullptr;
141 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500142 }
143
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400144 sk_sp<GrVkGpu> vkGpu(new GrVkGpu(context, options, backendContext, interface,
145 instanceVersion, physDevVersion));
146 if (backendContext.fProtectedContext == GrProtected::kYes &&
147 !vkGpu->vkCaps().supportsProtectedMemory()) {
148 return nullptr;
149 }
150 return std::move(vkGpu);
Greg Daniel164a9f02016-02-22 09:56:40 -0500151}
152
153////////////////////////////////////////////////////////////////////////////////
154
halcanary9d524f22016-03-29 09:03:52 -0700155GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500156 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
157 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500158 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400159 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000160 , fMemoryAllocator(backendContext.fMemoryAllocator)
161 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400162 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000163 , fDevice(backendContext.fDevice)
164 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400165 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500166 , fResourceProvider(this)
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400167 , fDisconnected(false)
168 , fProtectedContext(backendContext.fProtectedContext) {
Greg Danielf730c182018-07-02 20:15:37 +0000169 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700170
Greg Daniel81df0412018-05-31 13:13:33 -0400171 if (!fMemoryAllocator) {
172 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000173 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400174 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400175 }
176
ethannicholasb3058bd2016-07-01 08:22:01 -0700177 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700178
Greg Daniela0651ac2018-08-08 09:23:18 -0400179 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400180 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500182 physicalDeviceVersion,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400183 *backendContext.fVkExtensions, fProtectedContext));
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 } else if (backendContext.fDeviceFeatures) {
185 VkPhysicalDeviceFeatures2 features2;
186 features2.pNext = nullptr;
187 features2.features = *backendContext.fDeviceFeatures;
188 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500189 features2, instanceVersion, physicalDeviceVersion,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400190 *backendContext.fVkExtensions, fProtectedContext));
Greg Daniel36443602018-08-02 12:51:52 -0400191 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400192 VkPhysicalDeviceFeatures2 features;
193 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
194 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400195 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400196 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400197 }
198 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400199 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400200 }
201 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400202 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400203 }
Greg Danielf808c5e2019-04-30 14:48:27 -0400204 GrVkExtensions extensions;
205 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
206 // need to know if this is enabled to know if we can transition to a present layout when
207 // flushing a surface.
208 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
209 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
210 extensions.init(backendContext.fGetProc, backendContext.fInstance,
211 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
212 }
Greg Daniel36443602018-08-02 12:51:52 -0400213 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400214 features, instanceVersion, physicalDeviceVersion, extensions,
215 fProtectedContext));
Greg Daniel36443602018-08-02 12:51:52 -0400216 }
jvanverth633b3562016-03-23 11:01:22 -0700217 fCaps.reset(SkRef(fVkCaps.get()));
218
Greg Danielf730c182018-07-02 20:15:37 +0000219 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
220 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700221
Greg Daniela870b462019-01-08 15:49:46 -0500222 fResourceProvider.init();
223
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500224 fCmdPool = fResourceProvider.findOrCreateCommandPool();
225 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000226 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700227 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500228}
229
Greg Daniel8606cf82017-05-08 16:17:53 -0400230void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500231 if (fCmdPool) {
232 fCmdPool->getPrimaryCommandBuffer()->end(this);
233 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400234 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500235
236 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500237 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700238
239 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
240 // on the command buffers even though they have completed. This causes an assert to fire when
241 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500242 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700243#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500244 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700245#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500246 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700247#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500248 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700249#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500250 }
egdanielf8c2be32016-06-24 13:18:27 -0700251#endif
252
egdanielbe9d8212016-09-20 08:54:23 -0700253#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400254 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700255#endif
halcanary9d524f22016-03-29 09:03:52 -0700256
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500257 if (fCmdPool) {
258 fCmdPool->unref(this);
259 fCmdPool = nullptr;
260 }
261
Greg Daniel6be35232017-03-01 17:01:09 -0500262 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
263 fSemaphoresToWaitOn[i]->unref(this);
264 }
265 fSemaphoresToWaitOn.reset();
266
Greg Daniela5cb7812017-06-16 09:45:32 -0400267 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
268 fSemaphoresToSignal[i]->unref(this);
269 }
270 fSemaphoresToSignal.reset();
271
Jim Van Verth09557d72016-11-07 11:10:21 -0500272 // must call this just before we destroy the command pool and VkDevice
273 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500274
Greg Danielf730c182018-07-02 20:15:37 +0000275 fMemoryAllocator.reset();
276
277 fQueue = VK_NULL_HANDLE;
278 fDevice = VK_NULL_HANDLE;
279 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400280}
281
282GrVkGpu::~GrVkGpu() {
283 if (!fDisconnected) {
284 this->destroyResources();
285 }
286 delete fCompiler;
287}
288
289
290void GrVkGpu::disconnect(DisconnectType type) {
291 INHERITED::disconnect(type);
292 if (!fDisconnected) {
293 if (DisconnectType::kCleanup == type) {
294 this->destroyResources();
295 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500296 if (fCmdPool) {
297 fCmdPool->unrefAndAbandon();
298 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400299 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400300 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
301 fSemaphoresToWaitOn[i]->unrefAndAbandon();
302 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400303 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
304 fSemaphoresToSignal[i]->unrefAndAbandon();
305 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400306
307 // must call this just before we destroy the command pool and VkDevice
308 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400309
310 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400311 }
312 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400313 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400314 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400315 fDisconnected = true;
316 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500317}
318
319///////////////////////////////////////////////////////////////////////////////
320
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400321GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400322 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400323 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
324 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400325 if (!fCachedRTCommandBuffer) {
326 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
327 }
328
Greg Daniela41a74a2018-10-09 12:59:23 +0000329 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400330 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400331}
332
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400333GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
334 if (!fCachedTexCommandBuffer) {
335 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
336 }
337
338 fCachedTexCommandBuffer->set(texture, origin);
339 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700340}
341
Greg Daniela3aa75a2019-04-12 14:24:55 -0400342void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
343 GrGpuFinishedContext finishedContext) {
Brian Salomone39526b2019-06-24 16:35:53 -0400344 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel164a9f02016-02-22 09:56:40 -0500345 SkASSERT(fCurrentCmdBuffer);
Greg Danielb0c7ad12019-06-06 17:23:35 +0000346 SkASSERT(!fCachedRTCommandBuffer || !fCachedRTCommandBuffer->isActive());
347 SkASSERT(!fCachedTexCommandBuffer || !fCachedTexCommandBuffer->isActive());
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400348
349 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
350 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
351 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400352 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400353 if (finishedProc) {
354 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
355 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400356 return;
357 }
358
Greg Daniel164a9f02016-02-22 09:56:40 -0500359 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500360 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400361 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500362
Greg Daniela3aa75a2019-04-12 14:24:55 -0400363 if (finishedProc) {
364 // Make sure this is called after closing the current command pool
365 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
366 }
367
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400368 // We must delete and drawables that have been waitint till submit for us to destroy.
369 fDrawables.reset();
370
Greg Daniel6be35232017-03-01 17:01:09 -0500371 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
372 fSemaphoresToWaitOn[i]->unref(this);
373 }
374 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400375 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
376 fSemaphoresToSignal[i]->unref(this);
377 }
378 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500379
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500380 // Release old command pool and create a new one
381 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500382 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500383 fCmdPool = fResourceProvider.findOrCreateCommandPool();
384 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500385 fCurrentCmdBuffer->begin(this);
386}
387
388///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500389sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
390 GrAccessPattern accessPattern, const void* data) {
391 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700392 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500393 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700394 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
395 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500396 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700397 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500398 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700399 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
400 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500401 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700402 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500403 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400404 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
405 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500406 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700407 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500408 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400409 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
410 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500411 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700412 break;
cdalton397536c2016-03-25 12:15:03 -0700413 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400414 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700415 return nullptr;
416 }
cdalton1bf3e712016-04-19 10:00:02 -0700417 if (data && buff) {
418 buff->updateData(data, size);
419 }
420 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500421}
422
Brian Salomona9b04b92018-06-01 15:04:28 -0400423bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400424 GrColorType surfaceColorType, GrColorType srcColorType,
425 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500426 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
427 if (!vkTex) {
428 return false;
429 }
430
jvanverth900bd4a2016-04-29 13:53:12 -0700431 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400432 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800433 return false;
434 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800435
Robert Phillips1f098982019-05-15 10:27:36 -0400436 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500437 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400438 bool linearTiling = vkTex->isLinearTiled();
439 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400440 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400441 SkDebugf("Can't upload mipmap data to linear tiled texture");
442 return false;
443 }
444 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
445 // Need to change the layout to general in order to perform a host write
446 vkTex->setImageLayout(this,
447 VK_IMAGE_LAYOUT_GENERAL,
448 VK_ACCESS_HOST_WRITE_BIT,
449 VK_PIPELINE_STAGE_HOST_BIT,
450 false);
451 this->submitCommandBuffer(kForce_SyncQueue);
452 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400453 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400454 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500455 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400456 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400457 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
458 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500459 }
egdaniel4583ec52016-06-27 12:57:00 -0700460
jvanverth900bd4a2016-04-29 13:53:12 -0700461 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500462}
463
Brian Salomone05ba5a2019-04-08 11:59:07 -0400464bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400465 GrColorType surfaceColorType, GrColorType bufferColorType,
466 GrGpuBuffer* transferBuffer, size_t bufferOffset,
467 size_t rowBytes) {
Brian Salomon42be09d2019-07-26 12:12:26 -0400468 // Vulkan only supports offsets that are both 4-byte aligned and aligned to a pixel.
469 if ((bufferOffset & 0x3) || (bufferOffset % GrColorTypeBytesPerPixel(bufferColorType))) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400470 return false;
471 }
472 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
473 if (!vkTex) {
474 return false;
475 }
Robert Phillips1f098982019-05-15 10:27:36 -0400476
477 // Can't transfer compressed data
478 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
479
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400480 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
481 if (!vkBuffer) {
482 return false;
483 }
484
Greg Daniel660cc992017-06-26 14:55:05 -0400485 SkDEBUGCODE(
486 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
487 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
488 SkASSERT(bounds.contains(subRect));
489 )
Brian Salomonb28cb682019-07-26 12:48:47 -0400490 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400491
492 // Set up copy region
493 VkBufferImageCopy region;
494 memset(&region, 0, sizeof(VkBufferImageCopy));
495 region.bufferOffset = bufferOffset;
496 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
497 region.bufferImageHeight = 0;
498 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
499 region.imageOffset = { left, top, 0 };
500 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
501
502 // Change layout of our target so it can be copied to
503 vkTex->setImageLayout(this,
504 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
505 VK_ACCESS_TRANSFER_WRITE_BIT,
506 VK_PIPELINE_STAGE_TRANSFER_BIT,
507 false);
508
509 // Copy the buffer to the image
510 fCurrentCmdBuffer->copyBufferToImage(this,
511 vkBuffer,
512 vkTex,
513 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
514 1,
515 &region);
516
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400517 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400518 return true;
519}
520
Brian Salomon26de56e2019-04-10 12:14:26 -0400521bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400522 GrColorType surfaceColorType, GrColorType bufferColorType,
523 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400524 SkASSERT(surface);
525 SkASSERT(transferBuffer);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400526 if (fProtectedContext == GrProtected::kYes) {
527 return false;
528 }
Brian Salomona585fe92019-04-09 14:57:00 -0400529
Brian Salomona585fe92019-04-09 14:57:00 -0400530 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
531
532 GrVkImage* srcImage;
533 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
534 // Reading from render targets that wrap a secondary command buffer is not allowed since
535 // it would require us to know the VkImage, which we don't have, as well as need us to
536 // stop and start the VkRenderPass which we don't have access to.
537 if (rt->wrapsSecondaryCommandBuffer()) {
538 return false;
539 }
540 // resolve the render target if necessary
541 switch (rt->getResolveType()) {
542 case GrVkRenderTarget::kCantResolve_ResolveType:
543 return false;
544 case GrVkRenderTarget::kAutoResolves_ResolveType:
545 break;
546 case GrVkRenderTarget::kCanResolve_ResolveType:
547 this->resolveRenderTargetNoFlush(rt);
548 break;
549 default:
550 SK_ABORT("Unknown resolve type");
551 }
552 srcImage = rt;
553 } else {
554 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
555 }
556
557 // Set up copy region
558 VkBufferImageCopy region;
559 memset(&region, 0, sizeof(VkBufferImageCopy));
560 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400561 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400562 region.bufferImageHeight = 0;
563 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
564 region.imageOffset = { left, top, 0 };
565 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
566
567 srcImage->setImageLayout(this,
568 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
569 VK_ACCESS_TRANSFER_READ_BIT,
570 VK_PIPELINE_STAGE_TRANSFER_BIT,
571 false);
572
573 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
574 vkBuffer, 1, &region);
575
576 // Make sure the copy to buffer has finished.
577 vkBuffer->addMemoryBarrier(this,
578 VK_ACCESS_TRANSFER_WRITE_BIT,
579 VK_ACCESS_HOST_READ_BIT,
580 VK_PIPELINE_STAGE_TRANSFER_BIT,
581 VK_PIPELINE_STAGE_HOST_BIT,
582 false);
Brian Salomon26de56e2019-04-10 12:14:26 -0400583 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400584}
585
Brian Salomon1fabd512018-02-09 09:54:25 -0500586void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
587 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700588 SkASSERT(dst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600589 SkASSERT(src && src->numSamples() > 1 && src->msaaImage());
egdaniel4bcd62e2016-08-31 07:37:31 -0700590
egdaniel4bcd62e2016-08-31 07:37:31 -0700591 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500592 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
593 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
594 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
595 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
596 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700597
Greg Danielbc26c392017-04-18 13:32:10 -0400598 GrVkImage* dstImage;
599 GrRenderTarget* dstRT = dst->asRenderTarget();
600 if (dstRT) {
601 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400602 dstImage = vkRT;
603 } else {
604 SkASSERT(dst->asTexture());
605 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
606 }
607 dstImage->setImageLayout(this,
608 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
609 VK_ACCESS_TRANSFER_WRITE_BIT,
610 VK_PIPELINE_STAGE_TRANSFER_BIT,
611 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700612
613 src->msaaImage()->setImageLayout(this,
614 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
615 VK_ACCESS_TRANSFER_READ_BIT,
616 VK_PIPELINE_STAGE_TRANSFER_BIT,
617 false);
618
Greg Danielbc26c392017-04-18 13:32:10 -0400619 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700620}
621
Brian Salomon1fabd512018-02-09 09:54:25 -0500622void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700623 if (target->needsResolve()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600624 SkASSERT(target->numSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700625 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
626 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500627
egdaniel4bcd62e2016-08-31 07:37:31 -0700628 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700629
Brian Salomon1fabd512018-02-09 09:54:25 -0500630 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700631
632 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500633
634 if (requiresSubmit) {
635 this->submitCommandBuffer(kSkip_SyncQueue);
636 }
egdaniel52ad2512016-08-04 12:50:01 -0700637 }
638}
639
Brian Salomona9b04b92018-06-01 15:04:28 -0400640bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
641 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500642 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700643 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500644
Greg Daniel660cc992017-06-26 14:55:05 -0400645 SkDEBUGCODE(
646 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
647 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
648 SkASSERT(bounds.contains(subRect));
649 )
Brian Salomonb28cb682019-07-26 12:48:47 -0400650 size_t bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500651 size_t trimRowBytes = width * bpp;
652
jvanverth900bd4a2016-04-29 13:53:12 -0700653 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
654 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
655 const VkImageSubresource subres = {
656 VK_IMAGE_ASPECT_COLOR_BIT,
657 0, // mipLevel
658 0, // arraySlice
659 };
660 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500661
jvanverth900bd4a2016-04-29 13:53:12 -0700662 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500663
jvanverth900bd4a2016-04-29 13:53:12 -0700664 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700665 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700666 &subres,
667 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500668
jvanverth1e305ba2016-06-01 09:39:15 -0700669 const GrVkAlloc& alloc = tex->alloc();
Jim Van Verth658d4992019-07-11 14:07:53 -0400670 if (VK_NULL_HANDLE == alloc.fMemory) {
671 return false;
672 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400673 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700674 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400675 SkASSERT(size + offset <= alloc.fSize);
676 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
677 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700678 return false;
679 }
Greg Daniel81df0412018-05-31 13:13:33 -0400680 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700681
Brian Salomona9b04b92018-06-01 15:04:28 -0400682 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
683 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700684
Greg Daniele35a99e2018-03-02 11:44:22 -0500685 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400686 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700687
688 return true;
689}
690
Brian Salomona9b04b92018-06-01 15:04:28 -0400691bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
692 GrColorType dataColorType, const GrMipLevel texels[],
693 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700694 SkASSERT(!tex->isLinearTiled());
695 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400696 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700697 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
698
Greg Danieldd20e912017-04-07 14:42:23 -0400699 // We assume that if the texture has mip levels, we either upload to all the levels or just the
700 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400701 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400702
jvanverth900bd4a2016-04-29 13:53:12 -0700703 if (width == 0 || height == 0) {
704 return false;
705 }
706
Greg Daniel475eb702018-09-28 14:16:50 -0400707 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
708 return false;
709 }
710
711 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
712 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500713 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
714 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400715 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
716 // blit or draw.
Greg Danielcaa795f2019-05-14 11:54:25 -0400717 if (!this->vkCaps().formatCanBeDstofBlit(VK_FORMAT_R8G8B8_UNORM, tex->isLinearTiled()) &&
Greg Daniel900583a2019-08-06 12:05:31 -0400718 !this->vkCaps().isFormatRenderable(VK_FORMAT_R8G8B8_UNORM, 1)) {
Greg Daniel475eb702018-09-28 14:16:50 -0400719 return false;
720 }
721 mipLevelCount = 1;
722 }
723
Greg Daniel2f2caea2019-07-08 14:24:47 -0400724 SkASSERT(this->vkCaps().isVkFormatTexturable(tex->imageFormat()));
Brian Salomonb28cb682019-07-26 12:48:47 -0400725 size_t bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700726
727 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700728 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
729 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400730 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
731
Greg Daniel475eb702018-09-28 14:16:50 -0400732 texelsShallowCopy.reset(mipLevelCount);
733 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700734
Robert Phillips590533f2017-07-11 14:22:35 -0400735 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700736 individualMipOffsets.push_back(0);
737 size_t combinedBufferSize = width * bpp * height;
738 int currentWidth = width;
739 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400740 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400741 combinedBufferSize = 0;
742 }
743
Greg Daniel468fd632017-03-22 17:03:45 -0400744 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
745 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
746 SkASSERT((bpp & (bpp - 1)) == 0);
747 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400748 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700749 currentWidth = SkTMax(1, currentWidth/2);
750 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400751
Greg Daniel55afd6d2017-09-29 09:32:44 -0400752 if (texelsShallowCopy[currentMipLevel].fPixels) {
753 const size_t trimmedSize = currentWidth * bpp * currentHeight;
754 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
755 if (alignmentDiff != 0) {
756 combinedBufferSize += alignmentMask - alignmentDiff + 1;
757 }
758 individualMipOffsets.push_back(combinedBufferSize);
759 combinedBufferSize += trimmedSize;
760 } else {
761 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400762 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400763 }
764 if (0 == combinedBufferSize) {
765 // We don't actually have any data to upload so just return success
766 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700767 }
768
769 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500770 sk_sp<GrVkTransferBuffer> transferBuffer =
771 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400772 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700773 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400774 }
jvanverth900bd4a2016-04-29 13:53:12 -0700775
Greg Daniel475eb702018-09-28 14:16:50 -0400776 int uploadLeft = left;
777 int uploadTop = top;
778 GrVkTexture* uploadTexture = tex;
779 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
780 // R8G8B8A8_UNORM image and then copy it.
781 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500782 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400783 bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid();
784 if (!this->vkCaps().canCopyAsBlit(tex->config(), 1, false, dstHasYcbcr,
785 kRGBA_8888_GrPixelConfig, 1, false, false)) {
786 return false;
787 }
Greg Daniel475eb702018-09-28 14:16:50 -0400788 GrSurfaceDesc surfDesc;
Greg Daniel475eb702018-09-28 14:16:50 -0400789 surfDesc.fWidth = width;
790 surfDesc.fHeight = height;
791 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -0400792
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
Greg Daniel475eb702018-09-28 14:16:50 -0400814 uploadTexture = copyTexture.get();
815 uploadLeft = 0;
816 uploadTop = 0;
817 }
818
jvanverth900bd4a2016-04-29 13:53:12 -0700819 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400820 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700821
jvanverthc578b0632016-05-02 10:58:12 -0700822 currentWidth = width;
823 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400824 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400825 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400826 if (texelsShallowCopy[currentMipLevel].fPixels) {
827 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
828 const size_t trimRowBytes = currentWidth * bpp;
Brian Salomon1047a492019-07-02 12:25:21 -0400829 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700830
Greg Daniel55afd6d2017-09-29 09:32:44 -0400831 // copy data into the buffer, skipping the trailing bytes
832 char* dst = buffer + individualMipOffsets[currentMipLevel];
833 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400834 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400835
836 VkBufferImageCopy& region = regions.push_back();
837 memset(&region, 0, sizeof(VkBufferImageCopy));
838 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
839 region.bufferRowLength = currentWidth;
840 region.bufferImageHeight = currentHeight;
841 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400842 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400843 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700844 }
jvanverthc578b0632016-05-02 10:58:12 -0700845 currentWidth = SkTMax(1, currentWidth/2);
846 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400847 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700848 }
849
jvanverth9d54afc2016-09-20 09:20:03 -0700850 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700851 transferBuffer->unmap();
852
jvanverth900bd4a2016-04-29 13:53:12 -0700853 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400854 uploadTexture->setImageLayout(this,
855 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
856 VK_ACCESS_TRANSFER_WRITE_BIT,
857 VK_PIPELINE_STAGE_TRANSFER_BIT,
858 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700859
860 // Copy the buffer to the image
861 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500862 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400863 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700864 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
865 regions.count(),
866 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400867
868 // If we copied the data into a temporary image first, copy that image into our main texture
869 // now.
870 if (copyTexture.get()) {
871 SkASSERT(dataColorType == GrColorType::kRGB_888x);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400872 SkAssertResult(this->copySurface(tex, copyTexture.get(), SkIRect::MakeWH(width, height),
873 SkIPoint::Make(left, top), false));
Greg Daniel475eb702018-09-28 14:16:50 -0400874 }
Robert Phillips590533f2017-07-11 14:22:35 -0400875 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400876 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400877 }
jvanverth900bd4a2016-04-29 13:53:12 -0700878
Greg Daniel164a9f02016-02-22 09:56:40 -0500879 return true;
880}
881
Jim Van Verth1676cb92019-01-15 13:24:45 -0500882// It's probably possible to roll this into uploadTexDataOptimal,
883// but for now it's easier to maintain as a separate entity.
884bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400885 SkImage::CompressionType compressionType, const void* data) {
886 SkASSERT(data);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500887 SkASSERT(!tex->isLinearTiled());
888 // For now the assumption is that our rect is the entire texture.
889 // Compressed textures are read-only so this should be a reasonable assumption.
890 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
891
Jim Van Verth1676cb92019-01-15 13:24:45 -0500892 if (width == 0 || height == 0) {
893 return false;
894 }
895
Brian Salomonbb8dde82019-06-27 10:52:13 -0400896 SkImage::CompressionType textureCompressionType;
897 if (!GrVkFormatToCompressionType(tex->imageFormat(), &textureCompressionType) ||
898 textureCompressionType != compressionType) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500899 return false;
900 }
901
Greg Daniel2f2caea2019-07-08 14:24:47 -0400902 SkASSERT(this->vkCaps().isVkFormatTexturable(tex->imageFormat()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500903
Brian Salomonbb8dde82019-06-27 10:52:13 -0400904 size_t dataSize = GrCompressedDataSize(compressionType, width, height);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500905
906 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500907 sk_sp<GrVkTransferBuffer> transferBuffer =
Brian Salomonbb8dde82019-06-27 10:52:13 -0400908 GrVkTransferBuffer::Make(this, dataSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500909 if (!transferBuffer) {
910 return false;
911 }
912
913 int uploadLeft = left;
914 int uploadTop = top;
915 GrVkTexture* uploadTexture = tex;
916
917 char* buffer = (char*)transferBuffer->map();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500918
Brian Salomonbb8dde82019-06-27 10:52:13 -0400919 memcpy(buffer, data, dataSize);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500920
Brian Salomonbb8dde82019-06-27 10:52:13 -0400921 VkBufferImageCopy region;
922 memset(&region, 0, sizeof(VkBufferImageCopy));
923 region.bufferOffset = transferBuffer->offset();
924 region.bufferRowLength = width;
925 region.bufferImageHeight = height;
926 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
927 region.imageOffset = { uploadLeft, uploadTop, 0 };
928 region.imageExtent = { SkToU32(width), SkToU32(height), 1 };
Jim Van Verth1676cb92019-01-15 13:24:45 -0500929
930 // no need to flush non-coherent memory, unmap will do that for us
931 transferBuffer->unmap();
932
933 // Change layout of our target so it can be copied to
934 uploadTexture->setImageLayout(this,
935 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
936 VK_ACCESS_TRANSFER_WRITE_BIT,
937 VK_PIPELINE_STAGE_TRANSFER_BIT,
938 false);
939
940 // Copy the buffer to the image
941 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500942 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500943 uploadTexture,
944 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400945 1,
946 &region);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500947
948 return true;
949}
950
Greg Daniel164a9f02016-02-22 09:56:40 -0500951////////////////////////////////////////////////////////////////////////////////
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400952sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, GrRenderable renderable,
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400953 int renderTargetSampleCnt, SkBudgeted budgeted,
954 GrProtected isProtected, const GrMipLevel texels[],
955 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500956 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500957 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700958
Greg Daniel164a9f02016-02-22 09:56:40 -0500959 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400960 if (renderable == GrRenderable::kYes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500961 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
962 }
963
964 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
965 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
966 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700967 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500968 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
969 // texture.
970 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
971
Greg Daniel164a9f02016-02-22 09:56:40 -0500972 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700973 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500974 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400975 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500976 GrVkImage::ImageDesc imageDesc;
977 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
978 imageDesc.fFormat = pixelFormat;
979 imageDesc.fWidth = desc.fWidth;
980 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400981 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500982 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400983 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500984 imageDesc.fUsageFlags = usageFlags;
Brian Salomone8a766b2019-07-19 14:24:36 -0400985 imageDesc.fIsProtected = isProtected;
Greg Daniel164a9f02016-02-22 09:56:40 -0500986
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400987 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
988 if (mipLevels > 1) {
989 mipMapsStatus = GrMipMapsStatus::kValid;
990 for (int i = 0; i < mipLevels; ++i) {
991 if (!texels[i].fPixels) {
992 mipMapsStatus = GrMipMapsStatus::kDirty;
993 break;
994 }
Greg Daniel834f1202017-10-09 15:06:20 -0400995 }
996 }
997
Robert Phillips67d52cf2017-06-05 13:38:13 -0400998 sk_sp<GrVkTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400999 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001000 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
1001 this, budgeted, desc, renderTargetSampleCnt, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001002 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001003 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001004 }
1005
1006 if (!tex) {
1007 return nullptr;
1008 }
1009
Brian Salomonc320b152018-02-20 14:05:36 -05001010 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001011 if (mipLevelCount) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001012 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
1013 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001014 tex->unref();
1015 return nullptr;
1016 }
1017 }
1018
Brian Salomona3e29962019-07-16 11:52:08 -04001019 if (this->caps()->shouldInitializeTextures()) {
1020 SkSTArray<1, VkImageSubresourceRange> ranges;
1021 bool inRange = false;
1022 for (uint32_t i = 0; i < tex->mipLevels(); ++i) {
1023 if (i >= static_cast<uint32_t>(mipLevelCount) || !texels[i].fPixels) {
1024 if (inRange) {
1025 ranges.back().levelCount++;
1026 } else {
1027 auto& range = ranges.push_back();
1028 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1029 range.baseArrayLayer = 0;
1030 range.baseMipLevel = i;
1031 range.layerCount = 1;
1032 range.levelCount = 1;
1033 inRange = true;
1034 }
1035 } else if (inRange) {
1036 inRange = false;
1037 }
1038 }
1039
1040 if (!ranges.empty()) {
1041 static constexpr VkClearColorValue kZeroClearColor = {};
1042 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1043 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1044 false);
1045 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &kZeroClearColor,
1046 ranges.count(), ranges.begin());
1047 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001048 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001049 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001050}
1051
Brian Salomonbb8dde82019-06-27 10:52:13 -04001052sk_sp<GrTexture> GrVkGpu::onCreateCompressedTexture(int width, int height,
1053 SkImage::CompressionType compressionType,
1054 SkBudgeted budgeted, const void* data) {
1055 GrBackendFormat format = this->caps()->getBackendFormatFromCompressionType(compressionType);
1056 if (!format.getVkFormat()) {
1057 return nullptr;
1058 }
1059 VkFormat pixelFormat = *format.getVkFormat();
1060
1061 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1062
1063 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1064 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1065 // will be using this texture in some copy or not. Also this assumes, as is the current case,
1066 // that all render targets in vulkan are also textures. If we change this practice of setting
1067 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1068 // texture.
1069 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1070
1071 // Compressed textures with MIP levels or multiple samples are not supported as of now.
1072 GrVkImage::ImageDesc imageDesc;
1073 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1074 imageDesc.fFormat = pixelFormat;
1075 imageDesc.fWidth = width;
1076 imageDesc.fHeight = height;
1077 imageDesc.fLevels = 1;
1078 imageDesc.fSamples = 1;
1079 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1080 imageDesc.fUsageFlags = usageFlags;
1081 imageDesc.fIsProtected = GrProtected::kNo;
1082
1083 GrSurfaceDesc desc;
1084 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
1085 desc.fWidth = width;
1086 desc.fHeight = height;
1087 auto tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc,
1088 GrMipMapsStatus::kNotAllocated);
1089 if (!tex) {
1090 return nullptr;
1091 }
1092
1093 if (!this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight, compressionType,
1094 data)) {
1095 return nullptr;
1096 }
1097
1098 return std::move(tex);
1099}
1100
Greg Daniel164a9f02016-02-22 09:56:40 -05001101////////////////////////////////////////////////////////////////////////////////
1102
Greg Daniel6888c0d2017-08-25 11:55:50 -04001103void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1104 VkDeviceSize dstOffset, VkDeviceSize size) {
1105 VkBufferCopy copyRegion;
1106 copyRegion.srcOffset = srcOffset;
1107 copyRegion.dstOffset = dstOffset;
1108 copyRegion.size = size;
1109 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1110}
1111
jvanverthdb379092016-07-07 11:18:46 -07001112bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1113 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001114 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001115 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001116
1117 return true;
1118}
1119
1120////////////////////////////////////////////////////////////////////////////////
1121
Greg Daniel7e000222018-12-03 10:08:21 -05001122static bool check_image_info(const GrVkCaps& caps,
1123 const GrVkImageInfo& info,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001124 GrColorType colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001125 bool needsAllocation) {
Greg Danielcb324152019-02-25 11:36:53 -05001126 if (VK_NULL_HANDLE == info.fImage) {
1127 return false;
1128 }
1129
Jim Van Verth658d4992019-07-11 14:07:53 -04001130 if (VK_NULL_HANDLE == info.fAlloc.fMemory && needsAllocation) {
Brian Salomond17f6582017-07-19 18:28:58 -04001131 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001132 }
1133
Greg Daniel7e000222018-12-03 10:08:21 -05001134 if (info.fYcbcrConversionInfo.isValid()) {
1135 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1136 return false;
1137 }
jvanverthfd359ca2016-03-18 11:57:24 -07001138 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001139
Greg Danielcb324152019-02-25 11:36:53 -05001140 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1141 return false;
1142 }
1143
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001144 SkASSERT(GrVkFormatColorTypePairIsValid(info.fFormat, colorType));
Brian Salomond17f6582017-07-19 18:28:58 -04001145 return true;
1146}
1147
Greg Danielcaa795f2019-05-14 11:54:25 -04001148static bool check_tex_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) {
1149 if (info.fImageTiling == VK_IMAGE_TILING_OPTIMAL) {
Greg Daniel2f2caea2019-07-08 14:24:47 -04001150 if (!caps.isVkFormatTexturable(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001151 return false;
1152 }
1153 } else {
1154 SkASSERT(info.fImageTiling == VK_IMAGE_TILING_LINEAR);
Greg Daniel2f2caea2019-07-08 14:24:47 -04001155 if (!caps.isVkFormatTexturableLinearly(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001156 return false;
1157 }
1158 }
1159 return true;
1160}
1161
1162static bool check_rt_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) {
Greg Daniel900583a2019-08-06 12:05:31 -04001163 if (!caps.isFormatRenderable(info.fFormat, 1)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001164 return false;
1165 }
1166 return true;
1167}
1168
Brian Salomond17f6582017-07-19 18:28:58 -04001169sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001170 GrColorType colorType, GrWrapOwnership ownership,
1171 GrWrapCacheable cacheable, GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001172 GrVkImageInfo imageInfo;
1173 if (!backendTex.getVkImageInfo(&imageInfo)) {
1174 return nullptr;
1175 }
1176
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001177 if (!check_image_info(this->vkCaps(), imageInfo, colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001178 kAdopt_GrWrapOwnership == ownership)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001179 return nullptr;
1180 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001181 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1182 return nullptr;
1183 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001184
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001185 if (backendTex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1186 return nullptr;
1187 }
1188
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001189 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendTex.getBackendFormat(),
1190 colorType);
1191 SkASSERT(kUnknown_GrPixelConfig != config);
1192
Greg Daniel164a9f02016-02-22 09:56:40 -05001193 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001194 surfDesc.fWidth = backendTex.width();
1195 surfDesc.fHeight = backendTex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001196 surfDesc.fConfig = config;
Greg Daniel164a9f02016-02-22 09:56:40 -05001197
Greg Daniel52e16d92018-04-10 09:34:07 -04001198 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1199 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001200 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1201 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001202}
1203
1204sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001205 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -04001206 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001207 GrWrapOwnership ownership,
1208 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001209 GrVkImageInfo imageInfo;
1210 if (!backendTex.getVkImageInfo(&imageInfo)) {
1211 return nullptr;
1212 }
1213
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001214 if (!check_image_info(this->vkCaps(), imageInfo, colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001215 kAdopt_GrWrapOwnership == ownership)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001216 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001217 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001218 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1219 return nullptr;
1220 }
1221 if (!check_rt_image_info(this->vkCaps(), imageInfo)) {
1222 return nullptr;
1223 }
Brian Salomond17f6582017-07-19 18:28:58 -04001224
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001225 if (backendTex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1226 return nullptr;
1227 }
1228
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001229
1230 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendTex.getBackendFormat(),
1231 colorType);
1232 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillips0902c982019-07-16 07:47:56 -04001233
Brian Salomond17f6582017-07-19 18:28:58 -04001234 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001235 surfDesc.fWidth = backendTex.width();
1236 surfDesc.fHeight = backendTex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001237 surfDesc.fConfig = config;
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001238 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, colorType,
1239 backendTex.getBackendFormat());
Brian Salomond17f6582017-07-19 18:28:58 -04001240
Greg Daniel52e16d92018-04-10 09:34:07 -04001241 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1242 SkASSERT(layout);
1243
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001244 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001245 this, surfDesc, sampleCnt, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001246}
1247
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001248sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
1249 GrColorType colorType) {
Greg Daniele79b4732017-04-20 14:07:46 -04001250 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1251 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1252 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1253 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001254 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001255 return nullptr;
1256 }
halcanary9d524f22016-03-29 09:03:52 -07001257
Greg Daniel323fbcf2018-04-10 13:46:30 -04001258 GrVkImageInfo info;
1259 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001260 return nullptr;
1261 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001262
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001263 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
1264 colorType);
1265 SkASSERT(kUnknown_GrPixelConfig != config);
1266
1267 if (!check_image_info(this->vkCaps(), info, colorType, false)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001268 return nullptr;
1269 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001270 if (!check_rt_image_info(this->vkCaps(), info)) {
1271 return nullptr;
1272 }
1273
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001274 if (backendRT.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1275 return nullptr;
1276 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001277
Greg Daniel164a9f02016-02-22 09:56:40 -05001278 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001279 desc.fWidth = backendRT.width();
1280 desc.fHeight = backendRT.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001281 desc.fConfig = config;
Greg Daniel164a9f02016-02-22 09:56:40 -05001282
Greg Daniel323fbcf2018-04-10 13:46:30 -04001283 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001284
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001285 sk_sp<GrVkRenderTarget> tgt =
1286 GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, 1, info, std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001287
1288 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1289 SkASSERT(!backendRT.stencilBits());
1290 if (tgt) {
1291 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001292 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001293
Ben Wagnerff134f22018-04-24 16:29:16 -04001294 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001295}
1296
Greg Daniel7ef28f32017-04-20 16:41:55 +00001297sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001298 int sampleCnt,
1299 GrColorType grColorType) {
Brian Osman33910292017-04-18 14:38:53 -04001300
Greg Daniel52e16d92018-04-10 09:34:07 -04001301 GrVkImageInfo imageInfo;
1302 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001303 return nullptr;
1304 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001305 if (!check_image_info(this->vkCaps(), imageInfo, grColorType, false)) {
Brian Osman33910292017-04-18 14:38:53 -04001306 return nullptr;
1307 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001308 if (!check_rt_image_info(this->vkCaps(), imageInfo)) {
1309 return nullptr;
1310 }
Greg Danielcb324152019-02-25 11:36:53 -05001311
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001312 if (tex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1313 return nullptr;
1314 }
1315
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001316 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(tex.getBackendFormat(),
1317 grColorType);
1318 SkASSERT(kUnknown_GrPixelConfig != config);
1319
Brian Osman33910292017-04-18 14:38:53 -04001320 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001321 desc.fWidth = tex.width();
1322 desc.fHeight = tex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001323 desc.fConfig = config;
1324
1325 sampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, grColorType,
1326 tex.getBackendFormat());
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001327 if (!sampleCnt) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001328 return nullptr;
1329 }
Brian Osman33910292017-04-18 14:38:53 -04001330
Greg Daniel52e16d92018-04-10 09:34:07 -04001331 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1332 SkASSERT(layout);
1333
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001334 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, sampleCnt, imageInfo,
1335 std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001336}
1337
Greg Danielb46add82019-01-02 14:51:29 -05001338sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1339 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1340 int maxSize = this->caps()->maxTextureSize();
1341 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1342 return nullptr;
1343 }
1344
1345 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1346 if (!backendFormat.isValid()) {
1347 return nullptr;
1348 }
Greg Daniel627d0532019-07-08 16:48:14 -04001349 GrColorType grColorType = SkColorTypeToGrColorType(imageInfo.colorType());
Greg Daniel5c96db82019-07-09 14:06:58 -04001350 int sampleCnt = this->caps()->getRenderTargetSampleCount(1, grColorType, backendFormat);
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001351 if (!sampleCnt) {
1352 return nullptr;
1353 }
1354
Greg Daniel627d0532019-07-08 16:48:14 -04001355 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat, grColorType);
Greg Danielb46add82019-01-02 14:51:29 -05001356 if (config == kUnknown_GrPixelConfig) {
1357 return nullptr;
1358 }
1359
1360 GrSurfaceDesc desc;
Greg Danielb46add82019-01-02 14:51:29 -05001361 desc.fWidth = imageInfo.width();
1362 desc.fHeight = imageInfo.height();
1363 desc.fConfig = config;
Greg Danielb46add82019-01-02 14:51:29 -05001364
1365 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1366}
1367
Brian Salomon930f9392018-06-20 16:25:26 -04001368bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1369 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001370 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001371 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001372 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001373 return false;
jvanverth62340062016-04-26 08:01:44 -07001374 }
1375
jvanverth62340062016-04-26 08:01:44 -07001376 // determine if we can blit to and from this format
1377 const GrVkCaps& caps = this->vkCaps();
Greg Danielcaa795f2019-05-14 11:54:25 -04001378 if (!caps.formatCanBeDstofBlit(vkTex->imageFormat(), false) ||
1379 !caps.formatCanBeSrcofBlit(vkTex->imageFormat(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001380 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001381 return false;
jvanverth62340062016-04-26 08:01:44 -07001382 }
1383
egdaniel7ac5da82016-07-15 13:41:42 -07001384 int width = tex->width();
1385 int height = tex->height();
1386 VkImageBlit blitRegion;
1387 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001388
jvanverth82c05582016-05-03 11:19:01 -07001389 // SkMipMap doesn't include the base level in the level count so we have to add 1
1390 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001391 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001392
Greg Danielda86e282018-06-13 09:41:19 -04001393 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001394 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1395 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001396
jvanverth50c46c72016-05-06 12:31:28 -07001397 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001398 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001399 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001400 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1401 nullptr, // pNext
1402 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1403 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1404 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1405 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1406 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1407 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1408 vkTex->image(), // image
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001409 {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001410 };
1411
jvanverth62340062016-04-26 08:01:44 -07001412 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001413 uint32_t mipLevel = 1;
1414 while (mipLevel < levelCount) {
1415 int prevWidth = width;
1416 int prevHeight = height;
1417 width = SkTMax(1, width / 2);
1418 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001419
jvanverth50c46c72016-05-06 12:31:28 -07001420 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001421 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1422 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001423
1424 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001425 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001426 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001427 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1428 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001429 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001430 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001431 vkTex->resource(),
1432 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001433 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001434 vkTex->resource(),
1435 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001436 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001437 1,
1438 &blitRegion,
1439 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001440 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001441 }
Greg Danielee54f232019-04-03 14:58:40 -04001442 if (levelCount > 1) {
1443 // This barrier logically is not needed, but it changes the final level to the same layout
1444 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1445 // layouts and future layout changes easier. The alternative here would be to track layout
1446 // and memory accesses per layer which doesn't seem work it.
1447 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1448 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1449 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1450 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1451 }
Brian Salomon930f9392018-06-20 16:25:26 -04001452 return true;
jvanverth62340062016-04-26 08:01:44 -07001453}
1454
Greg Daniel164a9f02016-02-22 09:56:40 -05001455////////////////////////////////////////////////////////////////////////////////
1456
Chris Daltoneffee202019-07-01 22:28:03 -06001457GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(
1458 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
1459 SkASSERT(numStencilSamples == rt->numSamples());
Greg Daniel164a9f02016-02-22 09:56:40 -05001460 SkASSERT(width >= rt->width());
1461 SkASSERT(height >= rt->height());
1462
Chris Dalton6ce447a2019-06-23 18:07:38 -06001463 int samples = rt->numSamples();
Greg Daniel164a9f02016-02-22 09:56:40 -05001464
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001465 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001466
1467 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001468 width,
1469 height,
1470 samples,
1471 sFmt));
1472 fStats.incStencilAttachmentCreates();
1473 return stencil;
1474}
1475
1476////////////////////////////////////////////////////////////////////////////////
1477
Robert Phillips28a5a432019-06-07 12:46:21 -04001478bool copy_src_data(GrVkGpu* gpu, const GrVkAlloc& alloc, VkFormat vkFormat,
1479 int width, int height,
1480 const void* srcData, size_t srcRowBytes) {
1481 SkASSERT(srcData);
Brian Salomonbb8dde82019-06-27 10:52:13 -04001482 SkASSERT(!GrVkFormatIsCompressed(vkFormat));
1483
1484 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1485 if (!mapPtr) {
1486 return false;
1487 }
1488 size_t bytesPerPixel = GrVkBytesPerFormat(vkFormat);
1489 const size_t trimRowBytes = width * bytesPerPixel;
1490 if (!srcRowBytes) {
1491 srcRowBytes = trimRowBytes;
1492 }
1493 SkASSERT(trimRowBytes * height <= alloc.fSize);
1494
1495 SkRectMemcpy(mapPtr, trimRowBytes, srcData, srcRowBytes, trimRowBytes, height);
1496
1497 GrVkMemory::FlushMappedAlloc(gpu, alloc, 0, alloc.fSize);
1498 GrVkMemory::UnmapAlloc(gpu, alloc);
1499 return true;
1500}
1501
1502bool copy_compressed_src_data(GrVkGpu* gpu, const GrVkAlloc& alloc,
1503 SkImage::CompressionType compressionType, int width, int height,
1504 const void* data) {
1505 SkASSERT(data);
Robert Phillipsbd1ef682019-05-31 12:48:49 -04001506
Greg Daniel81df0412018-05-31 13:13:33 -04001507 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1508 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001509 return false;
1510 }
Robert Phillips28a5a432019-06-07 12:46:21 -04001511 mapPtr = reinterpret_cast<char*>(mapPtr);
egdaniel3602d4f2016-08-12 11:58:53 -07001512
Brian Salomonbb8dde82019-06-27 10:52:13 -04001513 size_t dataSize = GrCompressedDataSize(compressionType, width, height);
1514 SkASSERT(dataSize <= alloc.fSize);
1515 memcpy(mapPtr, data, dataSize);
Robert Phillips28a5a432019-06-07 12:46:21 -04001516 GrVkMemory::FlushMappedAlloc(gpu, alloc, 0, alloc.fSize);
Greg Daniel81df0412018-05-31 13:13:33 -04001517 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001518 return true;
1519}
Robert Phillips42dda082019-05-14 13:29:45 -04001520
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001521static void set_image_layout(const GrVkInterface* vkInterface, VkCommandBuffer cmdBuffer,
1522 GrVkImageInfo* info, VkImageLayout newLayout, uint32_t mipLevels,
Robert Phillips02dc0302019-07-02 17:58:27 -04001523 VkAccessFlags dstAccessMask, VkPipelineStageFlagBits dstStageMask) {
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001524 VkAccessFlags srcAccessMask = GrVkImage::LayoutToSrcAccessMask(info->fImageLayout);
1525 VkPipelineStageFlags srcStageMask = GrVkImage::LayoutToPipelineSrcStageFlags(
1526 info->fImageLayout);
1527
1528 VkImageMemoryBarrier barrier;
1529 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1530 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1531 barrier.pNext = nullptr;
1532 barrier.srcAccessMask = srcAccessMask;
1533 barrier.dstAccessMask = dstAccessMask;
1534 barrier.oldLayout = info->fImageLayout;
1535 barrier.newLayout = newLayout;
1536 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1537 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1538 barrier.image = info->fImage;
1539 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1540 GR_VK_CALL(vkInterface, CmdPipelineBarrier(
1541 cmdBuffer,
1542 srcStageMask,
1543 dstStageMask,
1544 0,
1545 0, nullptr,
1546 0, nullptr,
1547 1, &barrier));
1548 info->fImageLayout = newLayout;
1549}
1550
Brian Salomonb450f3b2019-07-09 09:36:51 -04001551bool GrVkGpu::createVkImageForBackendSurface(VkFormat vkFormat, int w, int h, bool texturable,
1552 bool renderable, GrMipMapped mipMapped,
1553 const void* srcData, size_t srcRowBytes,
1554 const SkColor4f* color, GrVkImageInfo* info,
1555 GrProtected isProtected) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001556 SkASSERT(texturable || renderable);
1557 if (!texturable) {
1558 SkASSERT(GrMipMapped::kNo == mipMapped);
1559 SkASSERT(!srcData);
1560 }
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001561
1562 if (fProtectedContext != isProtected) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001563 return false;
1564 }
1565
Greg Daniel2f2caea2019-07-08 14:24:47 -04001566 if (texturable && !fVkCaps->isVkFormatTexturable(vkFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001567 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001568 }
1569
Greg Daniel900583a2019-08-06 12:05:31 -04001570 if (renderable && !fVkCaps->isFormatRenderable(vkFormat, 1)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001571 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001572 }
1573
1574 // Currently we don't support uploading pixel data when mipped.
1575 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001576 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001577 }
1578
Brian Salomon52e943a2018-03-13 09:32:39 -04001579 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001580 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1581 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001582 if (texturable) {
1583 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1584 }
1585 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001586 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1587 }
1588
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001589 // Figure out the number of mip levels.
1590 uint32_t mipLevels = 1;
1591 if (GrMipMapped::kYes == mipMapped) {
1592 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1593 }
1594
Robert Phillipsf62e5752019-05-30 10:36:13 -04001595 GrVkImage::ImageDesc imageDesc;
1596 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1597 imageDesc.fFormat = vkFormat;
1598 imageDesc.fWidth = w;
1599 imageDesc.fHeight = h;
1600 imageDesc.fLevels = mipLevels;
1601 imageDesc.fSamples = 1;
1602 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1603 imageDesc.fUsageFlags = usageFlags;
1604 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001605 imageDesc.fIsProtected = fProtectedContext;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001606
Robert Phillipsf62e5752019-05-30 10:36:13 -04001607 if (!GrVkImage::InitImageInfo(this, imageDesc, info)) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001608 SkDebugf("Failed to init image info\n");
Brian Salomon52e943a2018-03-13 09:32:39 -04001609 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001610 }
1611
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001612 if (!srcData && !color) {
1613 return true;
1614 }
1615
1616 // We need to declare these early so that we can delete them at the end outside of
1617 // the if block.
Greg Daniel8385a8a2018-02-26 13:29:37 -05001618 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001619 VkBuffer buffer = VK_NULL_HANDLE;
1620
1621 VkResult err;
1622 const VkCommandBufferAllocateInfo cmdInfo = {
1623 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1624 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001625 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001626 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1627 1 // bufferCount
1628 };
1629
1630 VkCommandBuffer cmdBuffer;
1631 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1632 if (err) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001633 GrVkImage::DestroyImageInfo(this, info);
Brian Salomon52e943a2018-03-13 09:32:39 -04001634 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001635 }
1636
1637 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1638 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1639 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1640 cmdBufferBeginInfo.pNext = nullptr;
1641 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1642 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1643
1644 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1645 SkASSERT(!err);
1646
Brian Salomonb450f3b2019-07-09 09:36:51 -04001647 // Set image layout and add barrier
1648 set_image_layout(this->vkInterface(), cmdBuffer, info, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1649 mipLevels, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001650
Brian Salomonb450f3b2019-07-09 09:36:51 -04001651 // TODO: Lift this to GrContext level.
Brian Salomonbb8dde82019-06-27 10:52:13 -04001652 SkImage::CompressionType compressionType;
1653 bool isCompressed = GrVkFormatToCompressionType(vkFormat, &compressionType);
Brian Salomonb450f3b2019-07-09 09:36:51 -04001654 std::unique_ptr<char[]> tempData;
1655 if (isCompressed && !srcData) {
1656 SkASSERT(color);
1657 size_t size = GrCompressedDataSize(compressionType, w, h);
1658 tempData.reset(new char[size]);
1659 GrFillInCompressedData(compressionType, w, h, tempData.get(), *color);
1660 srcData = tempData.get();
1661 }
Robert Phillips28a5a432019-06-07 12:46:21 -04001662
Brian Salomonb450f3b2019-07-09 09:36:51 -04001663 if (srcData) {
1664 size_t bytesPerPixel = GrVkBytesPerFormat(vkFormat);
1665 SkASSERT(w && h);
1666
1667 SkTArray<size_t> individualMipOffsets(mipLevels);
1668
1669 SkImage::CompressionType compressionType;
1670 bool isCompressed = GrVkFormatToCompressionType(vkFormat, &compressionType);
1671
1672 size_t combinedBufferSize;
1673 if (isCompressed) {
1674 // Compressed textures currently must be non-MIP mapped.
1675 if (mipMapped == GrMipMapped::kYes) {
1676 return false;
1677 }
1678 combinedBufferSize = GrCompressedDataSize(compressionType, w, h);
1679 individualMipOffsets.push_back(0);
1680 } else {
1681 combinedBufferSize = GrComputeTightCombinedBufferSize(bytesPerPixel, w, h,
1682 &individualMipOffsets, mipLevels);
1683 }
1684
1685 VkBufferCreateInfo bufInfo;
1686 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1687 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1688 bufInfo.flags = fProtectedContext == GrProtected::kYes ? VK_BUFFER_CREATE_PROTECTED_BIT : 0;
1689 bufInfo.size = combinedBufferSize;
1690 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1691 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1692 bufInfo.queueFamilyIndexCount = 0;
1693 bufInfo.pQueueFamilyIndices = nullptr;
1694 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
1695
1696 if (err) {
1697 GrVkImage::DestroyImageInfo(this, info);
1698 VK_CALL(EndCommandBuffer(cmdBuffer));
1699 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001700 return false;
1701 }
Brian Salomonb450f3b2019-07-09 09:36:51 -04001702
1703 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1704 &bufferAlloc)) {
1705 GrVkImage::DestroyImageInfo(this, info);
1706 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1707 VK_CALL(EndCommandBuffer(cmdBuffer));
1708 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001709 return false;
1710 }
Brian Salomonb450f3b2019-07-09 09:36:51 -04001711
1712 bool result;
1713 if (isCompressed) {
1714 result = copy_compressed_src_data(this, bufferAlloc, compressionType, w, h, srcData);
1715 } else {
1716 SkASSERT(1 == mipLevels);
1717 result = copy_src_data(this, bufferAlloc, vkFormat, w, h, srcData, srcRowBytes);
1718 }
1719 if (!result) {
1720 GrVkImage::DestroyImageInfo(this, info);
1721 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1722 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1723 VK_CALL(EndCommandBuffer(cmdBuffer));
1724 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
1725 return false;
1726 }
1727
1728 SkTArray<VkBufferImageCopy> regions(mipLevels);
1729
1730 int currentWidth = w;
1731 int currentHeight = h;
1732 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1733 // Submit copy command
1734 VkBufferImageCopy& region = regions.push_back();
1735 memset(&region, 0, sizeof(VkBufferImageCopy));
1736 region.bufferOffset = individualMipOffsets[currentMipLevel];
1737 region.bufferRowLength = currentWidth;
1738 region.bufferImageHeight = currentHeight;
1739 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, currentMipLevel, 0, 1};
1740 region.imageOffset = {0, 0, 0};
1741 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1742 currentWidth = SkTMax(1, currentWidth / 2);
1743 currentHeight = SkTMax(1, currentHeight / 2);
1744 }
1745
1746 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, info->fImage, info->fImageLayout,
1747 regions.count(), regions.begin()));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001748 } else {
Brian Salomonb450f3b2019-07-09 09:36:51 -04001749 SkASSERT(color);
1750 VkClearColorValue vkColor;
1751 // If we ever support SINT or UINT formats this needs to be updated to use the int32 and
1752 // uint32 union members in those cases.
1753 vkColor.float32[0] = color->fR;
1754 vkColor.float32[1] = color->fG;
1755 vkColor.float32[2] = color->fB;
1756 vkColor.float32[3] = color->fA;
1757 VkImageSubresourceRange range;
1758 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1759 range.baseArrayLayer = 0;
1760 range.baseMipLevel = 0;
1761 range.layerCount = 1;
1762 range.levelCount = mipLevels;
1763 VK_CALL(CmdClearColorImage(cmdBuffer, info->fImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1764 &vkColor, 1, &range));
Brian Salomonbb8dde82019-06-27 10:52:13 -04001765 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001766
Robert Phillips02dc0302019-07-02 17:58:27 -04001767 if (!srcData && renderable) {
1768 SkASSERT(color);
1769
1770 // Change image layout to color-attachment-optimal since if we use this texture as a
1771 // borrowed texture within Ganesh we are probably going to render to it
1772 set_image_layout(this->vkInterface(), cmdBuffer, info,
1773 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, mipLevels,
1774 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
1775 VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1776 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
1777 } else if (texturable) {
1778 // Change image layout to shader read since if we use this texture as a borrowed
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001779 // texture within Ganesh we require that its layout be set to that
1780 set_image_layout(this->vkInterface(), cmdBuffer, info,
1781 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, mipLevels,
1782 VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
Brian Salomon52e943a2018-03-13 09:32:39 -04001783 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001784
1785 // End CommandBuffer
1786 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1787 SkASSERT(!err);
1788
1789 // Create Fence for queue
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001790 VkFenceCreateInfo fenceInfo;
1791 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1792 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001793 fenceInfo.pNext = nullptr;
1794 fenceInfo.flags = 0;
1795 VkFence fence = VK_NULL_HANDLE;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001796
1797 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1798 SkASSERT(!err);
1799
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001800 VkProtectedSubmitInfo protectedSubmitInfo;
1801 if (fProtectedContext == GrProtected::kYes) {
1802 memset(&protectedSubmitInfo, 0, sizeof(VkProtectedSubmitInfo));
1803 protectedSubmitInfo.sType = VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO;
1804 protectedSubmitInfo.pNext = nullptr;
1805 protectedSubmitInfo.protectedSubmit = VK_TRUE;
1806 }
1807
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001808 VkSubmitInfo submitInfo;
1809 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1810 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001811 submitInfo.pNext = fProtectedContext == GrProtected::kYes ? &protectedSubmitInfo : nullptr;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001812 submitInfo.waitSemaphoreCount = 0;
1813 submitInfo.pWaitSemaphores = nullptr;
1814 submitInfo.pWaitDstStageMask = 0;
1815 submitInfo.commandBufferCount = 1;
1816 submitInfo.pCommandBuffers = &cmdBuffer;
1817 submitInfo.signalSemaphoreCount = 0;
1818 submitInfo.pSignalSemaphores = nullptr;
1819 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1820 SkASSERT(!err);
1821
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001822 err = VK_CALL(WaitForFences(this->device(), 1, &fence, VK_TRUE, UINT64_MAX));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001823 if (VK_TIMEOUT == err) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001824 GrVkImage::DestroyImageInfo(this, info);
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001825 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1826 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1827 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1828 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001829 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001830 VK_CALL(DestroyFence(this->device(), fence, nullptr));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001831 SkDebugf("Fence failed to signal: %d\n", err);
1832 SK_ABORT("failing");
1833 }
1834 SkASSERT(!err);
1835
1836 // Clean up transfer resources
1837 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1838 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1839 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1840 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001841 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd1d869d2019-06-07 14:21:31 -04001842 VK_CALL(DestroyFence(this->device(), fence, nullptr));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001843
Brian Salomon52e943a2018-03-13 09:32:39 -04001844 return true;
1845}
1846
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001847GrBackendTexture GrVkGpu::createBackendTexture(int w, int h,
1848 const GrBackendFormat& format,
1849 GrMipMapped mipMapped,
1850 GrRenderable renderable,
Robert Phillips459b2952019-05-23 09:38:27 -04001851 const void* srcData, size_t rowBytes,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001852 const SkColor4f* color, GrProtected isProtected) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001853 const GrVkCaps& caps = this->vkCaps();
Brian Salomon8a375832018-03-14 10:21:40 -04001854 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001855
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001856 if (fProtectedContext != isProtected) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001857 return GrBackendTexture();
1858 }
1859
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001860 if (w > caps.maxTextureSize() || h > caps.maxTextureSize()) {
Robert Phillipsa479f962018-04-10 11:45:40 -04001861 return GrBackendTexture();
1862 }
1863
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001864 const VkFormat* vkFormat = format.getVkFormat();
1865 if (!vkFormat) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001866 SkDebugf("Could net get vkformat\n");
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001867 return GrBackendTexture();
1868 }
1869
Greg Daniel2f2caea2019-07-08 14:24:47 -04001870 if (!caps.isVkFormatTexturable(*vkFormat)) {
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001871 SkDebugf("Config is not texturable\n");
Robert Phillips9dbcdcc2019-05-13 10:40:06 -04001872 return GrBackendTexture();
1873 }
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001874
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001875 GrVkImageInfo info;
Brian Salomonb450f3b2019-07-09 09:36:51 -04001876 if (!this->createVkImageForBackendSurface(*vkFormat, w, h, true,
1877 GrRenderable::kYes == renderable, mipMapped, srcData,
1878 rowBytes, color, &info, isProtected)) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001879 SkDebugf("Failed to create testing only image\n");
1880 return GrBackendTexture();
Brian Salomon52e943a2018-03-13 09:32:39 -04001881 }
Robert Phillips62221e72019-07-24 15:07:38 -04001882
1883 return GrBackendTexture(w, h, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001884}
1885
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001886void GrVkGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04001887 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
1888
1889 GrVkImageInfo info;
1890 if (tex.getVkImageInfo(&info)) {
1891 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
1892 }
1893}
1894
1895#if GR_TEST_UTILS
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001896bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001897 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001898
Greg Daniel52e16d92018-04-10 09:34:07 -04001899 GrVkImageInfo backend;
1900 if (!tex.getVkImageInfo(&backend)) {
1901 return false;
1902 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001903
Greg Daniel52e16d92018-04-10 09:34:07 -04001904 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001905 VkMemoryRequirements req;
1906 memset(&req, 0, sizeof(req));
1907 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001908 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001909 &req));
1910 // TODO: find a better check
1911 // This will probably fail with a different driver
1912 return (req.size > 0) && (req.size <= 8192 * 8192);
1913 }
1914
1915 return false;
1916}
1917
Brian Osman2d010b62018-08-09 10:55:09 -04001918GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001919 this->handleDirtyContext();
1920
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001921 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1922 return GrBackendRenderTarget();
1923 }
1924
Greg Daniele877dce2019-07-11 10:52:43 -04001925 auto config = GrColorTypeToPixelConfig(ct);
Brian Salomon52e943a2018-03-13 09:32:39 -04001926 if (kUnknown_GrPixelConfig == config) {
1927 return {};
1928 }
Brian Salomonb450f3b2019-07-09 09:36:51 -04001929 VkFormat vkFormat;
1930 if (!GrPixelConfigToVkFormat(config, &vkFormat)) {
1931 return {};
1932 }
Robert Phillipsf62e5752019-05-30 10:36:13 -04001933
1934 GrVkImageInfo info;
Brian Salomonb450f3b2019-07-09 09:36:51 -04001935 if (!this->createVkImageForBackendSurface(vkFormat, w, h, false, true, GrMipMapped::kNo,
1936 nullptr, 0, &SkColors::kTransparent, &info,
1937 GrProtected::kNo)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001938 return {};
1939 }
Robert Phillips62221e72019-07-24 15:07:38 -04001940
1941 return GrBackendRenderTarget(w, h, 1, 0, info);
Brian Salomonf865b052018-03-09 09:01:53 -05001942}
1943
Brian Salomon52e943a2018-03-13 09:32:39 -04001944void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001945 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001946
Greg Daniel323fbcf2018-04-10 13:46:30 -04001947 GrVkImageInfo info;
1948 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001949 // something in the command buffer may still be using this, so force submit
1950 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001951 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001952 }
1953}
Brian Salomonf865b052018-03-09 09:01:53 -05001954
Greg Daniel26b50a42018-03-08 09:49:58 -05001955void GrVkGpu::testingOnly_flushGpuAndSync() {
1956 this->submitCommandBuffer(kForce_SyncQueue);
1957}
Brian Salomonf865b052018-03-09 09:01:53 -05001958#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001959
Greg Daniel164a9f02016-02-22 09:56:40 -05001960////////////////////////////////////////////////////////////////////////////////
1961
Greg Daniel59dc1482019-02-22 10:46:38 -05001962void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1963 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001964 VkPipelineStageFlags dstStageMask,
1965 bool byRegion,
1966 VkBufferMemoryBarrier* barrier) const {
1967 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001968 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001969 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001970 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001971 srcStageMask,
1972 dstStageMask,
1973 byRegion,
1974 GrVkCommandBuffer::kBufferMemory_BarrierType,
1975 barrier);
1976}
1977
Greg Daniel59dc1482019-02-22 10:46:38 -05001978void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1979 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001980 VkPipelineStageFlags dstStageMask,
1981 bool byRegion,
1982 VkImageMemoryBarrier* barrier) const {
1983 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001984 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001985 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001986 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001987 srcStageMask,
1988 dstStageMask,
1989 byRegion,
1990 GrVkCommandBuffer::kImageMemory_BarrierType,
1991 barrier);
1992}
1993
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001994void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04001995 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
1996 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001997 SkASSERT(n >= 0);
1998 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00001999 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
2000 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002001 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05002002 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002003 for (int i = 0; i < n; ++i) {
2004 SkASSERT(proxies[i]->isInstantiated());
2005 if (GrTexture* tex = proxies[i]->peekTexture()) {
2006 image = static_cast<GrVkTexture*>(tex);
2007 } else {
2008 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
2009 SkASSERT(rt);
2010 image = static_cast<GrVkRenderTarget*>(rt);
2011 }
2012 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05002013 }
Greg Danielbae71212019-03-01 15:24:35 -05002014 }
Greg Daniel797efca2019-05-09 14:04:20 -04002015
2016 // Handle requests for preparing for external IO
2017 for (int i = 0; i < externalRequests.fNumImages; ++i) {
2018 SkImage* image = externalRequests.fImages[i];
2019 if (!image->isTextureBacked()) {
2020 continue;
2021 }
2022 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
2023 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
2024 SkASSERT(proxy);
2025
2026 if (!proxy->isInstantiated()) {
2027 auto resourceProvider = this->getContext()->priv().resourceProvider();
2028 if (!proxy->instantiate(resourceProvider)) {
2029 continue;
2030 }
2031 }
2032
2033 GrTexture* tex = proxy->peekTexture();
2034 if (!tex) {
2035 continue;
2036 }
2037 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
2038 vkTex->prepareForExternal(this);
2039 }
2040 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
2041 SkSurface* surface = externalRequests.fSurfaces[i];
2042 if (!surface->getCanvas()->getGrContext()) {
2043 continue;
2044 }
2045 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
2046 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
2047 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
2048 if (!proxy->isInstantiated()) {
2049 auto resourceProvider = this->getContext()->priv().resourceProvider();
2050 if (!proxy->instantiate(resourceProvider)) {
2051 continue;
2052 }
2053 }
2054
2055 GrRenderTarget* rt = proxy->peekRenderTarget();
2056 SkASSERT(rt);
2057 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
2058 if (externalRequests.fPrepareSurfaceForPresent &&
2059 externalRequests.fPrepareSurfaceForPresent[i]) {
2060 vkRT->prepareForPresent(this);
2061 } else {
2062 vkRT->prepareForExternal(this);
2063 }
2064 }
2065
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002066 if (info.fFlags & kSyncCpu_GrFlushFlag) {
2067 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002068 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002069 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002070 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002071}
2072
Greg Daniel25af6712018-04-25 10:44:38 -04002073static int get_surface_sample_cnt(GrSurface* surf) {
2074 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002075 return rt->numSamples();
egdaniel17b89252016-04-05 07:23:38 -07002076 }
Greg Daniel25af6712018-04-25 10:44:38 -04002077 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05002078}
2079
Greg Daniel46cfbc62019-06-07 11:43:30 -04002080void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurface* src, GrVkImage* dstImage,
2081 GrVkImage* srcImage, const SkIRect& srcRect,
Greg Daniel164a9f02016-02-22 09:56:40 -05002082 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002083#ifdef SK_DEBUG
2084 int dstSampleCnt = get_surface_sample_cnt(dst);
2085 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002086 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2087 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002088 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstHasYcbcr,
2089 src->config(), srcSampleCnt, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04002090#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002091 if (src->isProtected() && !dst->isProtected()) {
2092 SkDebugf("Can't copy from protected memory to non-protected");
2093 return;
2094 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002095
Greg Daniel164a9f02016-02-22 09:56:40 -05002096 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
2097 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002098 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002099 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2100 VK_ACCESS_TRANSFER_WRITE_BIT,
2101 VK_PIPELINE_STAGE_TRANSFER_BIT,
2102 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002103
egdaniel17b89252016-04-05 07:23:38 -07002104 srcImage->setImageLayout(this,
2105 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002106 VK_ACCESS_TRANSFER_READ_BIT,
2107 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002108 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002109
Greg Daniel164a9f02016-02-22 09:56:40 -05002110 VkImageCopy copyRegion;
2111 memset(&copyRegion, 0, sizeof(VkImageCopy));
2112 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002113 copyRegion.srcOffset = { srcRect.fLeft, srcRect.fTop, 0 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002114 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002115 copyRegion.dstOffset = { dstPoint.fX, dstPoint.fY, 0 };
2116 copyRegion.extent = { (uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002117
2118 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002119 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002120 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002121 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002122 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2123 1,
2124 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002125
2126 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2127 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002128 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2129 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002130}
2131
Greg Daniel46cfbc62019-06-07 11:43:30 -04002132void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurface* src, GrVkImage* dstImage,
2133 GrVkImage* srcImage, const SkIRect& srcRect,
egdaniel17b89252016-04-05 07:23:38 -07002134 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002135#ifdef SK_DEBUG
2136 int dstSampleCnt = get_surface_sample_cnt(dst);
2137 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002138 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2139 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04002140 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002141 dstHasYcbcr, src->config(), srcSampleCnt,
2142 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002143
Greg Daniel25af6712018-04-25 10:44:38 -04002144#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002145 if (src->isProtected() && !dst->isProtected()) {
2146 SkDebugf("Can't copy from protected memory to non-protected");
2147 return;
2148 }
2149
egdaniel17b89252016-04-05 07:23:38 -07002150 dstImage->setImageLayout(this,
2151 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002152 VK_ACCESS_TRANSFER_WRITE_BIT,
2153 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002154 false);
2155
egdaniel17b89252016-04-05 07:23:38 -07002156 srcImage->setImageLayout(this,
2157 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002158 VK_ACCESS_TRANSFER_READ_BIT,
2159 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002160 false);
2161
2162 // Flip rect if necessary
Greg Daniel46cfbc62019-06-07 11:43:30 -04002163 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(),
2164 srcRect.height());
egdaniel17b89252016-04-05 07:23:38 -07002165
2166 VkImageBlit blitRegion;
2167 memset(&blitRegion, 0, sizeof(VkImageBlit));
2168 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002169 blitRegion.srcOffsets[0] = { srcRect.fLeft, srcRect.fTop, 0 };
2170 blitRegion.srcOffsets[1] = { srcRect.fRight, srcRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002171 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2172 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002173 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002174
2175 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002176 *srcImage,
2177 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002178 1,
2179 &blitRegion,
2180 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002181
Greg Daniel46cfbc62019-06-07 11:43:30 -04002182 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2183 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002184}
2185
Greg Daniel46cfbc62019-06-07 11:43:30 -04002186void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
2187 const SkIPoint& dstPoint) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002188 if (src->isProtected() && !dst->isProtected()) {
2189 SkDebugf("Can't copy from protected memory to non-protected");
2190 return;
2191 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002192 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002193 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002194 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002195 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002196 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2197 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002198}
2199
Greg Daniel46cfbc62019-06-07 11:43:30 -04002200bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
2201 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002202#ifdef SK_DEBUG
2203 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2204 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2205 }
2206 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2207 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2208 }
2209#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002210 if (src->isProtected() && !dst->isProtected()) {
2211 SkDebugf("Can't copy from protected memory to non-protected");
2212 return false;
2213 }
Greg Danielbe7fc462019-01-03 16:40:42 -05002214
Greg Daniel25af6712018-04-25 10:44:38 -04002215 GrPixelConfig dstConfig = dst->config();
2216 GrPixelConfig srcConfig = src->config();
2217
2218 int dstSampleCnt = get_surface_sample_cnt(dst);
2219 int srcSampleCnt = get_surface_sample_cnt(src);
2220
egdaniel17b89252016-04-05 07:23:38 -07002221 GrVkImage* dstImage;
2222 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002223 GrRenderTarget* dstRT = dst->asRenderTarget();
2224 if (dstRT) {
2225 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002226 if (vkRT->wrapsSecondaryCommandBuffer()) {
2227 return false;
2228 }
Chris Dalton6ce447a2019-06-23 18:07:38 -06002229 dstImage = vkRT->numSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel4bcd62e2016-08-31 07:37:31 -07002230 } else {
2231 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002232 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002233 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002234 GrRenderTarget* srcRT = src->asRenderTarget();
2235 if (srcRT) {
2236 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
Chris Dalton6ce447a2019-06-23 18:07:38 -06002237 srcImage = vkRT->numSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002238 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002239 SkASSERT(src->asTexture());
2240 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002241 }
2242
Greg Daniela51e93c2019-03-25 12:30:45 -04002243 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2244 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2245
Greg Daniel46cfbc62019-06-07 11:43:30 -04002246 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstHasYcbcr,
2247 srcConfig, srcSampleCnt, srcHasYcbcr)) {
2248 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
Greg Daniela51e93c2019-03-25 12:30:45 -04002249 return true;
2250 }
2251
Greg Daniel46cfbc62019-06-07 11:43:30 -04002252 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstHasYcbcr,
2253 srcConfig, srcSampleCnt, srcHasYcbcr)) {
2254 this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002255 return true;
2256 }
2257
Greg Daniel25af6712018-04-25 10:44:38 -04002258 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002259 dstHasYcbcr, srcConfig, srcSampleCnt,
2260 srcImage->isLinearTiled(), srcHasYcbcr)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002261 this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002262 return true;
2263 }
2264
Greg Daniel164a9f02016-02-22 09:56:40 -05002265 return false;
2266}
2267
Brian Salomona6948702018-06-01 15:33:20 -04002268bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002269 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2270 size_t rowBytes) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002271 if (surface->isProtected()) {
2272 return false;
2273 }
2274
Brian Salomonc320b152018-02-20 14:05:36 -05002275 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002276 return false;
2277 }
2278
egdaniel66933552016-08-24 07:22:19 -07002279 GrVkImage* image = nullptr;
2280 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2281 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002282 // Reading from render targets that wrap a secondary command buffer is not allowed since
2283 // it would require us to know the VkImage, which we don't have, as well as need us to
2284 // stop and start the VkRenderPass which we don't have access to.
2285 if (rt->wrapsSecondaryCommandBuffer()) {
2286 return false;
2287 }
egdaniel66933552016-08-24 07:22:19 -07002288 // resolve the render target if necessary
2289 switch (rt->getResolveType()) {
2290 case GrVkRenderTarget::kCantResolve_ResolveType:
2291 return false;
2292 case GrVkRenderTarget::kAutoResolves_ResolveType:
2293 break;
2294 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002295 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002296 break;
2297 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002298 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002299 }
2300 image = rt;
2301 } else {
2302 image = static_cast<GrVkTexture*>(surface->asTexture());
2303 }
2304
2305 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002306 return false;
2307 }
2308
Greg Daniel475eb702018-09-28 14:16:50 -04002309 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2310 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2311 // image and then do the read pixels from that.
2312 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002313 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2314 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002315
Greg Daniel46cfbc62019-06-07 11:43:30 -04002316 int srcSampleCount = 0;
2317 if (rt) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002318 srcSampleCount = rt->numSamples();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002319 }
2320 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
2321 if (!this->vkCaps().canCopyAsBlit(kRGBA_8888_GrPixelConfig, 1, false, false,
2322 surface->config(), srcSampleCount, image->isLinearTiled(),
2323 srcHasYcbcr)) {
2324 return false;
2325 }
2326
Greg Daniel475eb702018-09-28 14:16:50 -04002327 // Make a new surface that is RGBA to copy the RGB surface into.
2328 GrSurfaceDesc surfDesc;
Greg Daniel475eb702018-09-28 14:16:50 -04002329 surfDesc.fWidth = width;
2330 surfDesc.fHeight = height;
2331 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -04002332
2333 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2334 VK_IMAGE_USAGE_SAMPLED_BIT |
2335 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2336 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2337
2338 GrVkImage::ImageDesc imageDesc;
2339 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2340 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2341 imageDesc.fWidth = width;
2342 imageDesc.fHeight = height;
2343 imageDesc.fLevels = 1;
2344 imageDesc.fSamples = 1;
2345 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2346 imageDesc.fUsageFlags = usageFlags;
2347 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2348
2349 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
Brian Salomon27b4d8d2019-07-22 14:23:45 -04002350 this, SkBudgeted::kYes, surfDesc, 1, imageDesc, GrMipMapsStatus::kNotAllocated);
Greg Daniel475eb702018-09-28 14:16:50 -04002351 if (!copySurface) {
2352 return false;
2353 }
2354
Greg Daniel475eb702018-09-28 14:16:50 -04002355 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002356 SkAssertResult(this->copySurface(copySurface.get(), surface, srcRect, SkIPoint::Make(0,0)));
2357
Greg Daniel475eb702018-09-28 14:16:50 -04002358 top = 0;
2359 left = 0;
2360 dstColorType = GrColorType::kRGBA_8888;
2361 image = copySurface.get();
2362 }
2363
Greg Daniel164a9f02016-02-22 09:56:40 -05002364 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002365 image->setImageLayout(this,
2366 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2367 VK_ACCESS_TRANSFER_READ_BIT,
2368 VK_PIPELINE_STAGE_TRANSFER_BIT,
2369 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002370
Brian Salomonb28cb682019-07-26 12:48:47 -04002371 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002372 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002373
Greg Daniel164a9f02016-02-22 09:56:40 -05002374 VkBufferImageCopy region;
2375 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002376
2377 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2378 if (copyFromOrigin) {
2379 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002380 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002381 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002382 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002383 region.imageOffset = offset;
2384 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2385 }
2386
2387 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002388 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002389 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002390 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002391 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002392 kStream_GrAccessPattern)
2393 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002394
2395 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002396 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002397 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002398 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2399 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002400
2401 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002402 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002403 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002404 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002405 1,
2406 &region);
2407
2408 // make sure the copy to buffer has finished
2409 transferBuffer->addMemoryBarrier(this,
2410 VK_ACCESS_TRANSFER_WRITE_BIT,
2411 VK_ACCESS_HOST_READ_BIT,
2412 VK_PIPELINE_STAGE_TRANSFER_BIT,
2413 VK_PIPELINE_STAGE_HOST_BIT,
2414 false);
2415
2416 // We need to submit the current command buffer to the Queue and make sure it finishes before
2417 // we can copy the data out of the buffer.
2418 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002419 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002420 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002421 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002422
egdaniel6fa0a912016-09-12 11:51:29 -07002423 if (copyFromOrigin) {
2424 uint32_t skipRows = region.imageExtent.height - height;
2425 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2426 }
2427
Brian Salomona6948702018-06-01 15:33:20 -04002428 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002429
2430 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002431 return true;
2432}
egdaniel066df7c2016-06-08 14:02:27 -07002433
egdaniel27bb2842016-07-07 11:58:35 -07002434// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2435// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2436// the the entire attachment. Similar requirements for the y and height components.
2437void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2438 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2439 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002440 if ((0 != granularity.width && 1 != granularity.width)) {
2441 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2442 int rightAdj = srcBounds.fRight % granularity.width;
2443 if (rightAdj != 0) {
2444 rightAdj = granularity.width - rightAdj;
2445 }
2446 dstBounds->fRight = srcBounds.fRight + rightAdj;
2447 if (dstBounds->fRight > maxWidth) {
2448 dstBounds->fRight = maxWidth;
2449 dstBounds->fLeft = 0;
2450 } else {
2451 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2452 }
egdaniel27bb2842016-07-07 11:58:35 -07002453 } else {
egdanield5797b32016-09-20 12:57:45 -07002454 dstBounds->fLeft = srcBounds.fLeft;
2455 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002456 }
2457
2458 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002459 if ((0 != granularity.height && 1 != granularity.height)) {
2460 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2461 int bottomAdj = srcBounds.fBottom % granularity.height;
2462 if (bottomAdj != 0) {
2463 bottomAdj = granularity.height - bottomAdj;
2464 }
2465 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2466 if (dstBounds->fBottom > maxHeight) {
2467 dstBounds->fBottom = maxHeight;
2468 dstBounds->fTop = 0;
2469 } else {
2470 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2471 }
egdaniel27bb2842016-07-07 11:58:35 -07002472 } else {
egdanield5797b32016-09-20 12:57:45 -07002473 dstBounds->fTop = srcBounds.fTop;
2474 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002475 }
2476}
2477
Greg Daniel8daf3b72019-07-30 09:57:26 -04002478void GrVkGpu::submitSecondaryCommandBuffer(
Greg Daniel228518f2019-08-07 16:55:17 -04002479 std::unique_ptr<GrVkSecondaryCommandBuffer> buffer,
Greg Daniel8daf3b72019-07-30 09:57:26 -04002480 const GrVkRenderPass* renderPass,
2481 const VkClearValue* colorClear,
2482 GrVkRenderTarget* target, GrSurfaceOrigin origin,
2483 const SkIRect& bounds) {
2484
Greg Danielbe7fc462019-01-03 16:40:42 -05002485 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002486 const SkIRect* pBounds = &bounds;
2487 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002488 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002489 flippedBounds = bounds;
2490 flippedBounds.fTop = target->height() - bounds.fBottom;
2491 flippedBounds.fBottom = target->height() - bounds.fTop;
2492 pBounds = &flippedBounds;
2493 }
2494
egdaniel27bb2842016-07-07 11:58:35 -07002495 // The bounds we use for the render pass should be of the granularity supported
2496 // by the device.
2497 const VkExtent2D& granularity = renderPass->granularity();
2498 SkIRect adjustedBounds;
2499 if ((0 != granularity.width && 1 != granularity.width) ||
2500 (0 != granularity.height && 1 != granularity.height)) {
2501 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2502 target->width(), target->height());
2503 pBounds = &adjustedBounds;
2504 }
2505
Robert Phillips95214472017-08-08 18:00:03 -04002506#ifdef SK_DEBUG
2507 uint32_t index;
2508 bool result = renderPass->colorAttachmentIndex(&index);
2509 SkASSERT(result && 0 == index);
2510 result = renderPass->stencilAttachmentIndex(&index);
2511 if (result) {
2512 SkASSERT(1 == index);
2513 }
2514#endif
2515 VkClearValue clears[2];
2516 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002517 clears[1].depthStencil.depth = 0.0f;
2518 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002519
2520 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel228518f2019-08-07 16:55:17 -04002521 fCurrentCmdBuffer->executeCommands(this, std::move(buffer));
Greg Daniel164a9f02016-02-22 09:56:40 -05002522 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002523
Brian Salomon1fabd512018-02-09 09:54:25 -05002524 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002525}
egdaniel9cb63402016-06-23 08:37:05 -07002526
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002527void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2528 if (buffer->asRTCommandBuffer()) {
2529 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2530
2531 fCachedRTCommandBuffer->submit();
2532 fCachedRTCommandBuffer->reset();
2533 } else {
2534 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2535
2536 fCachedTexCommandBuffer->submit();
2537 fCachedTexCommandBuffer->reset();
2538 }
2539}
2540
Greg Daniel6be35232017-03-01 17:01:09 -05002541GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002542 VkFenceCreateInfo createInfo;
2543 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2544 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2545 createInfo.pNext = nullptr;
2546 createInfo.flags = 0;
2547 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002548
2549 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2550 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2551
2552 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002553 return (GrFence)fence;
2554}
2555
Greg Daniel6be35232017-03-01 17:01:09 -05002556bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2557 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2558
2559 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002560 return (VK_SUCCESS == result);
2561}
2562
2563void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002564 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2565}
2566
Greg Daniela5cb7812017-06-16 09:45:32 -04002567sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2568 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002569}
2570
Greg Daniel48661b82018-01-22 16:11:35 -05002571sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2572 GrResourceProvider::SemaphoreWrapType wrapType,
2573 GrWrapOwnership ownership) {
2574 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002575}
2576
Greg Daniel858e12c2018-12-06 11:11:37 -05002577void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002578 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2579
Greg Daniel48661b82018-01-22 16:11:35 -05002580 GrVkSemaphore::Resource* resource = vkSem->getResource();
2581 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002582 resource->ref();
2583 fSemaphoresToSignal.push_back(resource);
2584 }
Greg Daniel6be35232017-03-01 17:01:09 -05002585}
2586
Greg Daniel48661b82018-01-22 16:11:35 -05002587void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002588 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2589
Greg Daniel48661b82018-01-22 16:11:35 -05002590 GrVkSemaphore::Resource* resource = vkSem->getResource();
2591 if (resource->shouldWait()) {
2592 resource->ref();
2593 fSemaphoresToWaitOn.push_back(resource);
2594 }
jvanverth84741b32016-09-30 08:39:02 -07002595}
Brian Osman13dddce2017-05-09 13:19:50 -04002596
2597sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2598 SkASSERT(texture);
2599 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2600 vkTexture->setImageLayout(this,
2601 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2602 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002603 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002604 false);
2605 this->submitCommandBuffer(kSkip_SyncQueue);
2606
Greg Danielb3f66542019-05-10 17:11:19 -04002607 // The image layout change serves as a barrier, so no semaphore is needed.
2608 // If we ever decide we need to return a semaphore here, we need to make sure GrVkSemaphore is
2609 // thread safe so that only the first thread that tries to use the semaphore actually submits
2610 // it. This additionally would also require thread safety in command buffer submissions to
2611 // queues in general.
Brian Osman13dddce2017-05-09 13:19:50 -04002612 return nullptr;
2613}
Greg Danielf5d87582017-12-18 14:48:15 -05002614
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002615void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2616 fDrawables.emplace_back(std::move(drawable));
2617}
2618
Greg Daniel7a82edf2018-12-04 10:54:34 -05002619uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2620 const GrBackendFormat& format) {
2621 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2622 SkASSERT(ycbcrInfo);
2623 if (!ycbcrInfo->isValid()) {
2624 return 0;
2625 }
2626
2627 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2628 samplerState, *ycbcrInfo);
2629
2630 return sampler->uniqueID();
2631}
2632
Greg Daniela870b462019-01-08 15:49:46 -05002633void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002634 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002635 this->resourceProvider().storePipelineCacheData();
2636 }
2637}