blob: bda54fafbe9064bbfed8b51300d712b76d3dccdc [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/vk/GrVkImage.h"
31#include "src/gpu/vk/GrVkIndexBuffer.h"
32#include "src/gpu/vk/GrVkInterface.h"
33#include "src/gpu/vk/GrVkMemory.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040034#include "src/gpu/vk/GrVkOpsRenderPass.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#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 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -0400150 return 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
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400321GrOpsRenderPass* GrVkGpu::getOpsRenderPass(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400322 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400323 const GrOpsRenderPass::LoadAndStoreInfo& colorInfo,
Greg Danielb20d7e52019-09-03 13:54:39 -0400324 const GrOpsRenderPass::StencilLoadAndStoreInfo& stencilInfo,
325 const SkTArray<GrTextureProxy*, true>& sampledProxies) {
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400326 if (!fCachedOpsRenderPass) {
327 fCachedOpsRenderPass.reset(new GrVkOpsRenderPass(this));
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400328 }
329
Greg Danielb20d7e52019-09-03 13:54:39 -0400330 fCachedOpsRenderPass->set(rt, origin, colorInfo, stencilInfo, sampledProxies);
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400331 return fCachedOpsRenderPass.get();
egdaniel066df7c2016-06-08 14:02:27 -0700332}
333
Greg Daniela3aa75a2019-04-12 14:24:55 -0400334void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
335 GrGpuFinishedContext finishedContext) {
Brian Salomone39526b2019-06-24 16:35:53 -0400336 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Greg Daniel164a9f02016-02-22 09:56:40 -0500337 SkASSERT(fCurrentCmdBuffer);
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400338 SkASSERT(!fCachedOpsRenderPass || !fCachedOpsRenderPass->isActive());
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400339
340 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
341 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
342 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400343 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400344 if (finishedProc) {
345 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
346 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400347 return;
348 }
349
Greg Daniel164a9f02016-02-22 09:56:40 -0500350 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500351 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400352 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500353
Greg Daniela3aa75a2019-04-12 14:24:55 -0400354 if (finishedProc) {
355 // Make sure this is called after closing the current command pool
356 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
357 }
358
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400359 // We must delete and drawables that have been waitint till submit for us to destroy.
360 fDrawables.reset();
361
Greg Daniel6be35232017-03-01 17:01:09 -0500362 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
363 fSemaphoresToWaitOn[i]->unref(this);
364 }
365 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400366 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
367 fSemaphoresToSignal[i]->unref(this);
368 }
369 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500370
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500371 // Release old command pool and create a new one
372 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500373 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500374 fCmdPool = fResourceProvider.findOrCreateCommandPool();
375 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500376 fCurrentCmdBuffer->begin(this);
377}
378
379///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500380sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
381 GrAccessPattern accessPattern, const void* data) {
382 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700383 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500384 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700385 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
386 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500387 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700388 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500389 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700390 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
391 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500392 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700393 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500394 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400395 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
396 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500397 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700398 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500399 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400400 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
401 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500402 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700403 break;
cdalton397536c2016-03-25 12:15:03 -0700404 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400405 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700406 }
cdalton1bf3e712016-04-19 10:00:02 -0700407 if (data && buff) {
408 buff->updateData(data, size);
409 }
410 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500411}
412
Brian Salomona9b04b92018-06-01 15:04:28 -0400413bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400414 GrColorType surfaceColorType, GrColorType srcColorType,
Greg Danielb20d7e52019-09-03 13:54:39 -0400415 const GrMipLevel texels[], int mipLevelCount,
416 bool prepForTexSampling) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500417 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
418 if (!vkTex) {
419 return false;
420 }
421
jvanverth900bd4a2016-04-29 13:53:12 -0700422 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400423 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800424 return false;
425 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800426
Robert Phillips1f098982019-05-15 10:27:36 -0400427 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500428 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400429 bool linearTiling = vkTex->isLinearTiled();
430 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400431 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400432 SkDebugf("Can't upload mipmap data to linear tiled texture");
433 return false;
434 }
435 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
436 // Need to change the layout to general in order to perform a host write
437 vkTex->setImageLayout(this,
438 VK_IMAGE_LAYOUT_GENERAL,
439 VK_ACCESS_HOST_WRITE_BIT,
440 VK_PIPELINE_STAGE_HOST_BIT,
441 false);
442 this->submitCommandBuffer(kForce_SyncQueue);
443 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400444 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400445 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500446 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400447 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400448 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
449 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500450 }
egdaniel4583ec52016-06-27 12:57:00 -0700451
Greg Danielb20d7e52019-09-03 13:54:39 -0400452 if (prepForTexSampling) {
453 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
454 VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
455 false);
456 }
457
jvanverth900bd4a2016-04-29 13:53:12 -0700458 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500459}
460
Brian Salomone05ba5a2019-04-08 11:59:07 -0400461bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400462 GrColorType surfaceColorType, GrColorType bufferColorType,
463 GrGpuBuffer* transferBuffer, size_t bufferOffset,
464 size_t rowBytes) {
Brian Salomon42be09d2019-07-26 12:12:26 -0400465 // Vulkan only supports offsets that are both 4-byte aligned and aligned to a pixel.
466 if ((bufferOffset & 0x3) || (bufferOffset % GrColorTypeBytesPerPixel(bufferColorType))) {
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400467 return false;
468 }
469 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
470 if (!vkTex) {
471 return false;
472 }
Robert Phillips1f098982019-05-15 10:27:36 -0400473
474 // Can't transfer compressed data
475 SkASSERT(!GrVkFormatIsCompressed(vkTex->imageFormat()));
476
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400477 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
478 if (!vkBuffer) {
479 return false;
480 }
481
Greg Daniel660cc992017-06-26 14:55:05 -0400482 SkDEBUGCODE(
483 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
484 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
485 SkASSERT(bounds.contains(subRect));
486 )
Brian Salomonb28cb682019-07-26 12:48:47 -0400487 size_t bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400488
489 // Set up copy region
490 VkBufferImageCopy region;
491 memset(&region, 0, sizeof(VkBufferImageCopy));
492 region.bufferOffset = bufferOffset;
493 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
494 region.bufferImageHeight = 0;
495 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
496 region.imageOffset = { left, top, 0 };
497 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
498
499 // Change layout of our target so it can be copied to
500 vkTex->setImageLayout(this,
501 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
502 VK_ACCESS_TRANSFER_WRITE_BIT,
503 VK_PIPELINE_STAGE_TRANSFER_BIT,
504 false);
505
506 // Copy the buffer to the image
507 fCurrentCmdBuffer->copyBufferToImage(this,
508 vkBuffer,
509 vkTex,
510 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
511 1,
512 &region);
513
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400514 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400515 return true;
516}
517
Brian Salomon26de56e2019-04-10 12:14:26 -0400518bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -0400519 GrColorType surfaceColorType, GrColorType bufferColorType,
520 GrGpuBuffer* transferBuffer, size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400521 SkASSERT(surface);
522 SkASSERT(transferBuffer);
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400523 if (fProtectedContext == GrProtected::kYes) {
524 return false;
525 }
Brian Salomona585fe92019-04-09 14:57:00 -0400526
Brian Salomona585fe92019-04-09 14:57:00 -0400527 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
528
529 GrVkImage* srcImage;
530 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
531 // Reading from render targets that wrap a secondary command buffer is not allowed since
532 // it would require us to know the VkImage, which we don't have, as well as need us to
533 // stop and start the VkRenderPass which we don't have access to.
534 if (rt->wrapsSecondaryCommandBuffer()) {
535 return false;
536 }
537 // resolve the render target if necessary
538 switch (rt->getResolveType()) {
539 case GrVkRenderTarget::kCantResolve_ResolveType:
540 return false;
541 case GrVkRenderTarget::kAutoResolves_ResolveType:
542 break;
543 case GrVkRenderTarget::kCanResolve_ResolveType:
544 this->resolveRenderTargetNoFlush(rt);
545 break;
546 default:
547 SK_ABORT("Unknown resolve type");
548 }
549 srcImage = rt;
550 } else {
551 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
552 }
553
554 // Set up copy region
555 VkBufferImageCopy region;
556 memset(&region, 0, sizeof(VkBufferImageCopy));
557 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400558 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400559 region.bufferImageHeight = 0;
560 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
561 region.imageOffset = { left, top, 0 };
562 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
563
564 srcImage->setImageLayout(this,
565 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
566 VK_ACCESS_TRANSFER_READ_BIT,
567 VK_PIPELINE_STAGE_TRANSFER_BIT,
568 false);
569
570 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
571 vkBuffer, 1, &region);
572
573 // Make sure the copy to buffer has finished.
574 vkBuffer->addMemoryBarrier(this,
575 VK_ACCESS_TRANSFER_WRITE_BIT,
576 VK_ACCESS_HOST_READ_BIT,
577 VK_PIPELINE_STAGE_TRANSFER_BIT,
578 VK_PIPELINE_STAGE_HOST_BIT,
579 false);
Brian Salomon26de56e2019-04-10 12:14:26 -0400580 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400581}
582
Brian Salomon1fabd512018-02-09 09:54:25 -0500583void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
584 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700585 SkASSERT(dst);
Chris Dalton6ce447a2019-06-23 18:07:38 -0600586 SkASSERT(src && src->numSamples() > 1 && src->msaaImage());
egdaniel4bcd62e2016-08-31 07:37:31 -0700587
egdaniel4bcd62e2016-08-31 07:37:31 -0700588 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500589 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
590 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
591 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
592 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
593 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700594
Greg Danielbc26c392017-04-18 13:32:10 -0400595 GrVkImage* dstImage;
596 GrRenderTarget* dstRT = dst->asRenderTarget();
597 if (dstRT) {
598 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400599 dstImage = vkRT;
600 } else {
601 SkASSERT(dst->asTexture());
602 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
603 }
604 dstImage->setImageLayout(this,
605 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
606 VK_ACCESS_TRANSFER_WRITE_BIT,
607 VK_PIPELINE_STAGE_TRANSFER_BIT,
608 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700609
610 src->msaaImage()->setImageLayout(this,
611 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
612 VK_ACCESS_TRANSFER_READ_BIT,
613 VK_PIPELINE_STAGE_TRANSFER_BIT,
614 false);
615
Greg Danielbc26c392017-04-18 13:32:10 -0400616 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700617}
618
Brian Salomon1fabd512018-02-09 09:54:25 -0500619void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700620 if (target->needsResolve()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600621 SkASSERT(target->numSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700622 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
623 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500624
egdaniel4bcd62e2016-08-31 07:37:31 -0700625 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700626
Brian Salomon1fabd512018-02-09 09:54:25 -0500627 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700628
629 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500630
631 if (requiresSubmit) {
632 this->submitCommandBuffer(kSkip_SyncQueue);
633 }
egdaniel52ad2512016-08-04 12:50:01 -0700634 }
635}
636
Brian Salomona9b04b92018-06-01 15:04:28 -0400637bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
638 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500639 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700640 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500641
Greg Daniel660cc992017-06-26 14:55:05 -0400642 SkDEBUGCODE(
643 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
644 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
645 SkASSERT(bounds.contains(subRect));
646 )
Brian Salomonb28cb682019-07-26 12:48:47 -0400647 size_t bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500648 size_t trimRowBytes = width * bpp;
649
jvanverth900bd4a2016-04-29 13:53:12 -0700650 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
651 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
652 const VkImageSubresource subres = {
653 VK_IMAGE_ASPECT_COLOR_BIT,
654 0, // mipLevel
655 0, // arraySlice
656 };
657 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500658
jvanverth900bd4a2016-04-29 13:53:12 -0700659 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500660
jvanverth900bd4a2016-04-29 13:53:12 -0700661 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700662 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700663 &subres,
664 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500665
jvanverth1e305ba2016-06-01 09:39:15 -0700666 const GrVkAlloc& alloc = tex->alloc();
Jim Van Verth658d4992019-07-11 14:07:53 -0400667 if (VK_NULL_HANDLE == alloc.fMemory) {
668 return false;
669 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400670 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700671 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400672 SkASSERT(size + offset <= alloc.fSize);
673 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
674 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700675 return false;
676 }
Greg Daniel81df0412018-05-31 13:13:33 -0400677 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700678
Brian Salomona9b04b92018-06-01 15:04:28 -0400679 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
680 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700681
Greg Daniele35a99e2018-03-02 11:44:22 -0500682 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400683 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700684
685 return true;
686}
687
Brian Salomona9b04b92018-06-01 15:04:28 -0400688bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
689 GrColorType dataColorType, const GrMipLevel texels[],
690 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700691 SkASSERT(!tex->isLinearTiled());
692 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400693 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700694 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
695
Greg Danieldd20e912017-04-07 14:42:23 -0400696 // We assume that if the texture has mip levels, we either upload to all the levels or just the
697 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400698 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400699
jvanverth900bd4a2016-04-29 13:53:12 -0700700 if (width == 0 || height == 0) {
701 return false;
702 }
703
Greg Daniel475eb702018-09-28 14:16:50 -0400704 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
705 return false;
706 }
707
708 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
709 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500710 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
711 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400712 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
713 // blit or draw.
Greg Danielcaa795f2019-05-14 11:54:25 -0400714 if (!this->vkCaps().formatCanBeDstofBlit(VK_FORMAT_R8G8B8_UNORM, tex->isLinearTiled()) &&
Greg Daniel900583a2019-08-06 12:05:31 -0400715 !this->vkCaps().isFormatRenderable(VK_FORMAT_R8G8B8_UNORM, 1)) {
Greg Daniel475eb702018-09-28 14:16:50 -0400716 return false;
717 }
718 mipLevelCount = 1;
719 }
720
Greg Daniel2f2caea2019-07-08 14:24:47 -0400721 SkASSERT(this->vkCaps().isVkFormatTexturable(tex->imageFormat()));
Brian Salomonb28cb682019-07-26 12:48:47 -0400722 size_t bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700723
724 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700725 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
726 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400727 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
728
Greg Daniel475eb702018-09-28 14:16:50 -0400729 texelsShallowCopy.reset(mipLevelCount);
730 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700731
Robert Phillips590533f2017-07-11 14:22:35 -0400732 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700733 individualMipOffsets.push_back(0);
734 size_t combinedBufferSize = width * bpp * height;
735 int currentWidth = width;
736 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400737 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400738 combinedBufferSize = 0;
739 }
740
Greg Daniel468fd632017-03-22 17:03:45 -0400741 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
742 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
743 SkASSERT((bpp & (bpp - 1)) == 0);
744 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400745 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700746 currentWidth = SkTMax(1, currentWidth/2);
747 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400748
Greg Daniel55afd6d2017-09-29 09:32:44 -0400749 if (texelsShallowCopy[currentMipLevel].fPixels) {
750 const size_t trimmedSize = currentWidth * bpp * currentHeight;
751 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
752 if (alignmentDiff != 0) {
753 combinedBufferSize += alignmentMask - alignmentDiff + 1;
754 }
755 individualMipOffsets.push_back(combinedBufferSize);
756 combinedBufferSize += trimmedSize;
757 } else {
758 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400759 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400760 }
761 if (0 == combinedBufferSize) {
762 // We don't actually have any data to upload so just return success
763 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700764 }
765
766 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500767 sk_sp<GrVkTransferBuffer> transferBuffer =
768 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400769 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700770 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400771 }
jvanverth900bd4a2016-04-29 13:53:12 -0700772
Greg Daniel475eb702018-09-28 14:16:50 -0400773 int uploadLeft = left;
774 int uploadTop = top;
775 GrVkTexture* uploadTexture = tex;
776 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
777 // R8G8B8A8_UNORM image and then copy it.
778 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500779 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400780 bool dstHasYcbcr = tex->ycbcrConversionInfo().isValid();
Brian Salomon1c53a9f2019-08-12 14:10:12 -0400781 if (!this->vkCaps().canCopyAsBlit(tex->imageFormat(), 1, false, dstHasYcbcr,
782 VK_FORMAT_R8G8B8A8_UNORM, 1, false, false)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400783 return false;
784 }
Greg Daniel475eb702018-09-28 14:16:50 -0400785 GrSurfaceDesc surfDesc;
Greg Daniel475eb702018-09-28 14:16:50 -0400786 surfDesc.fWidth = width;
787 surfDesc.fHeight = height;
788 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -0400789
790 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
791 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
792 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
793
794 GrVkImage::ImageDesc imageDesc;
795 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
796 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
797 imageDesc.fWidth = width;
798 imageDesc.fHeight = height;
799 imageDesc.fLevels = 1;
800 imageDesc.fSamples = 1;
801 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
802 imageDesc.fUsageFlags = usageFlags;
803 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
804
805 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
806 GrMipMapsStatus::kNotAllocated);
807 if (!copyTexture) {
808 return false;
809 }
Greg Daniel5c7b5412019-05-10 11:39:55 -0400810
Greg Daniel475eb702018-09-28 14:16:50 -0400811 uploadTexture = copyTexture.get();
812 uploadLeft = 0;
813 uploadTop = 0;
814 }
815
jvanverth900bd4a2016-04-29 13:53:12 -0700816 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400817 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700818
jvanverthc578b0632016-05-02 10:58:12 -0700819 currentWidth = width;
820 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400821 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400822 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400823 if (texelsShallowCopy[currentMipLevel].fPixels) {
824 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
825 const size_t trimRowBytes = currentWidth * bpp;
Brian Salomon1047a492019-07-02 12:25:21 -0400826 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700827
Greg Daniel55afd6d2017-09-29 09:32:44 -0400828 // copy data into the buffer, skipping the trailing bytes
829 char* dst = buffer + individualMipOffsets[currentMipLevel];
830 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400831 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400832
833 VkBufferImageCopy& region = regions.push_back();
834 memset(&region, 0, sizeof(VkBufferImageCopy));
835 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
836 region.bufferRowLength = currentWidth;
837 region.bufferImageHeight = currentHeight;
838 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400839 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400840 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700841 }
jvanverthc578b0632016-05-02 10:58:12 -0700842 currentWidth = SkTMax(1, currentWidth/2);
843 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400844 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700845 }
846
jvanverth9d54afc2016-09-20 09:20:03 -0700847 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700848 transferBuffer->unmap();
849
jvanverth900bd4a2016-04-29 13:53:12 -0700850 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400851 uploadTexture->setImageLayout(this,
852 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
853 VK_ACCESS_TRANSFER_WRITE_BIT,
854 VK_PIPELINE_STAGE_TRANSFER_BIT,
855 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700856
857 // Copy the buffer to the image
858 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500859 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400860 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700861 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
862 regions.count(),
863 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400864
865 // If we copied the data into a temporary image first, copy that image into our main texture
866 // now.
867 if (copyTexture.get()) {
868 SkASSERT(dataColorType == GrColorType::kRGB_888x);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400869 SkAssertResult(this->copySurface(tex, copyTexture.get(), SkIRect::MakeWH(width, height),
Greg Daniele227fe42019-08-21 13:52:24 -0400870 SkIPoint::Make(left, top)));
Greg Daniel475eb702018-09-28 14:16:50 -0400871 }
Robert Phillips590533f2017-07-11 14:22:35 -0400872 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400873 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400874 }
jvanverth900bd4a2016-04-29 13:53:12 -0700875
Greg Daniel164a9f02016-02-22 09:56:40 -0500876 return true;
877}
878
Jim Van Verth1676cb92019-01-15 13:24:45 -0500879// It's probably possible to roll this into uploadTexDataOptimal,
880// but for now it's easier to maintain as a separate entity.
881bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400882 SkImage::CompressionType compressionType, const void* data) {
883 SkASSERT(data);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500884 SkASSERT(!tex->isLinearTiled());
885 // For now the assumption is that our rect is the entire texture.
886 // Compressed textures are read-only so this should be a reasonable assumption.
887 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
888
Jim Van Verth1676cb92019-01-15 13:24:45 -0500889 if (width == 0 || height == 0) {
890 return false;
891 }
892
Brian Salomonbb8dde82019-06-27 10:52:13 -0400893 SkImage::CompressionType textureCompressionType;
894 if (!GrVkFormatToCompressionType(tex->imageFormat(), &textureCompressionType) ||
895 textureCompressionType != compressionType) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500896 return false;
897 }
898
Greg Daniel2f2caea2019-07-08 14:24:47 -0400899 SkASSERT(this->vkCaps().isVkFormatTexturable(tex->imageFormat()));
Jim Van Verth1676cb92019-01-15 13:24:45 -0500900
Brian Salomonbb8dde82019-06-27 10:52:13 -0400901 size_t dataSize = GrCompressedDataSize(compressionType, width, height);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500902
903 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500904 sk_sp<GrVkTransferBuffer> transferBuffer =
Brian Salomonbb8dde82019-06-27 10:52:13 -0400905 GrVkTransferBuffer::Make(this, dataSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500906 if (!transferBuffer) {
907 return false;
908 }
909
910 int uploadLeft = left;
911 int uploadTop = top;
912 GrVkTexture* uploadTexture = tex;
913
914 char* buffer = (char*)transferBuffer->map();
Jim Van Verth1676cb92019-01-15 13:24:45 -0500915
Brian Salomonbb8dde82019-06-27 10:52:13 -0400916 memcpy(buffer, data, dataSize);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500917
Brian Salomonbb8dde82019-06-27 10:52:13 -0400918 VkBufferImageCopy region;
919 memset(&region, 0, sizeof(VkBufferImageCopy));
920 region.bufferOffset = transferBuffer->offset();
921 region.bufferRowLength = width;
922 region.bufferImageHeight = height;
923 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
924 region.imageOffset = { uploadLeft, uploadTop, 0 };
925 region.imageExtent = { SkToU32(width), SkToU32(height), 1 };
Jim Van Verth1676cb92019-01-15 13:24:45 -0500926
927 // no need to flush non-coherent memory, unmap will do that for us
928 transferBuffer->unmap();
929
930 // Change layout of our target so it can be copied to
931 uploadTexture->setImageLayout(this,
932 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
933 VK_ACCESS_TRANSFER_WRITE_BIT,
934 VK_PIPELINE_STAGE_TRANSFER_BIT,
935 false);
936
937 // Copy the buffer to the image
938 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500939 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500940 uploadTexture,
941 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
Brian Salomonbb8dde82019-06-27 10:52:13 -0400942 1,
943 &region);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500944
945 return true;
946}
947
Greg Daniel164a9f02016-02-22 09:56:40 -0500948////////////////////////////////////////////////////////////////////////////////
Brian Salomon81536f22019-08-08 16:30:49 -0400949sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc,
950 const GrBackendFormat& format,
951 GrRenderable renderable,
952 int renderTargetSampleCnt,
953 SkBudgeted budgeted,
954 GrProtected isProtected,
955 const GrMipLevel texels[],
Brian Salomon27b4d8d2019-07-22 14:23:45 -0400956 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500957 VkFormat pixelFormat;
Brian Salomon81536f22019-08-08 16:30:49 -0400958 SkAssertResult(format.asVkFormat(&pixelFormat));
959 SkASSERT(!GrVkFormatIsCompressed(pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700960
Greg Daniel164a9f02016-02-22 09:56:40 -0500961 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400962 if (renderable == GrRenderable::kYes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500963 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
964 }
965
966 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
967 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
968 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700969 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500970 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
971 // texture.
972 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
973
Greg Daniel164a9f02016-02-22 09:56:40 -0500974 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700975 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500976 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400977 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500978 GrVkImage::ImageDesc imageDesc;
979 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
980 imageDesc.fFormat = pixelFormat;
981 imageDesc.fWidth = desc.fWidth;
982 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400983 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500984 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400985 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500986 imageDesc.fUsageFlags = usageFlags;
Brian Salomone8a766b2019-07-19 14:24:36 -0400987 imageDesc.fIsProtected = isProtected;
Greg Daniel164a9f02016-02-22 09:56:40 -0500988
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400989 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
990 if (mipLevels > 1) {
991 mipMapsStatus = GrMipMapsStatus::kValid;
992 for (int i = 0; i < mipLevels; ++i) {
993 if (!texels[i].fPixels) {
994 mipMapsStatus = GrMipMapsStatus::kDirty;
995 break;
996 }
Greg Daniel834f1202017-10-09 15:06:20 -0400997 }
998 }
999
Robert Phillips67d52cf2017-06-05 13:38:13 -04001000 sk_sp<GrVkTexture> tex;
Brian Salomonf2c2ba92019-07-17 09:59:59 -04001001 if (renderable == GrRenderable::kYes) {
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001002 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
1003 this, budgeted, desc, renderTargetSampleCnt, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001004 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001005 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001006 }
1007
1008 if (!tex) {
1009 return nullptr;
1010 }
1011
Brian Salomonc320b152018-02-20 14:05:36 -05001012 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001013 if (mipLevelCount) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001014 if (!this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight, colorType,
1015 texels, mipLevelCount)) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001016 tex->unref();
1017 return nullptr;
1018 }
1019 }
1020
Brian Salomona3e29962019-07-16 11:52:08 -04001021 if (this->caps()->shouldInitializeTextures()) {
1022 SkSTArray<1, VkImageSubresourceRange> ranges;
1023 bool inRange = false;
1024 for (uint32_t i = 0; i < tex->mipLevels(); ++i) {
1025 if (i >= static_cast<uint32_t>(mipLevelCount) || !texels[i].fPixels) {
1026 if (inRange) {
1027 ranges.back().levelCount++;
1028 } else {
1029 auto& range = ranges.push_back();
1030 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1031 range.baseArrayLayer = 0;
1032 range.baseMipLevel = i;
1033 range.layerCount = 1;
1034 range.levelCount = 1;
1035 inRange = true;
1036 }
1037 } else if (inRange) {
1038 inRange = false;
1039 }
1040 }
1041
1042 if (!ranges.empty()) {
1043 static constexpr VkClearColorValue kZeroClearColor = {};
1044 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1045 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1046 false);
1047 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &kZeroClearColor,
1048 ranges.count(), ranges.begin());
1049 }
Brian Salomond17b4a62017-05-23 16:53:47 -04001050 }
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001051 return tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001052}
1053
Brian Salomonbb8dde82019-06-27 10:52:13 -04001054sk_sp<GrTexture> GrVkGpu::onCreateCompressedTexture(int width, int height,
Greg Daniel7bfc9132019-08-14 14:23:53 -04001055 const GrBackendFormat& format,
Brian Salomonbb8dde82019-06-27 10:52:13 -04001056 SkImage::CompressionType compressionType,
1057 SkBudgeted budgeted, const void* data) {
Brian Salomond4764a12019-08-08 12:08:24 -04001058 VkFormat pixelFormat;
1059 if (!format.asVkFormat(&pixelFormat)) {
Brian Salomonbb8dde82019-06-27 10:52:13 -04001060 return nullptr;
1061 }
Brian Salomonbb8dde82019-06-27 10:52:13 -04001062
1063 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1064
1065 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1066 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1067 // will be using this texture in some copy or not. Also this assumes, as is the current case,
1068 // that all render targets in vulkan are also textures. If we change this practice of setting
1069 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1070 // texture.
1071 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1072
1073 // Compressed textures with MIP levels or multiple samples are not supported as of now.
1074 GrVkImage::ImageDesc imageDesc;
1075 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1076 imageDesc.fFormat = pixelFormat;
1077 imageDesc.fWidth = width;
1078 imageDesc.fHeight = height;
1079 imageDesc.fLevels = 1;
1080 imageDesc.fSamples = 1;
1081 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1082 imageDesc.fUsageFlags = usageFlags;
1083 imageDesc.fIsProtected = GrProtected::kNo;
1084
1085 GrSurfaceDesc desc;
1086 desc.fConfig = GrCompressionTypePixelConfig(compressionType);
1087 desc.fWidth = width;
1088 desc.fHeight = height;
1089 auto tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc,
1090 GrMipMapsStatus::kNotAllocated);
1091 if (!tex) {
1092 return nullptr;
1093 }
1094
1095 if (!this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight, compressionType,
1096 data)) {
1097 return nullptr;
1098 }
1099
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001100 return tex;
Brian Salomonbb8dde82019-06-27 10:52:13 -04001101}
1102
Greg Daniel164a9f02016-02-22 09:56:40 -05001103////////////////////////////////////////////////////////////////////////////////
1104
Greg Daniel6888c0d2017-08-25 11:55:50 -04001105void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1106 VkDeviceSize dstOffset, VkDeviceSize size) {
1107 VkBufferCopy copyRegion;
1108 copyRegion.srcOffset = srcOffset;
1109 copyRegion.dstOffset = dstOffset;
1110 copyRegion.size = size;
1111 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1112}
1113
jvanverthdb379092016-07-07 11:18:46 -07001114bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1115 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001116 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001117 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001118
1119 return true;
1120}
1121
1122////////////////////////////////////////////////////////////////////////////////
1123
Greg Daniel7e000222018-12-03 10:08:21 -05001124static bool check_image_info(const GrVkCaps& caps,
1125 const GrVkImageInfo& info,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001126 GrColorType colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001127 bool needsAllocation) {
Greg Danielcb324152019-02-25 11:36:53 -05001128 if (VK_NULL_HANDLE == info.fImage) {
1129 return false;
1130 }
1131
Jim Van Verth658d4992019-07-11 14:07:53 -04001132 if (VK_NULL_HANDLE == info.fAlloc.fMemory && needsAllocation) {
Brian Salomond17f6582017-07-19 18:28:58 -04001133 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001134 }
1135
Greg Daniel7e000222018-12-03 10:08:21 -05001136 if (info.fYcbcrConversionInfo.isValid()) {
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001137 if (!caps.supportsYcbcrConversion()) {
Greg Daniel7e000222018-12-03 10:08:21 -05001138 return false;
1139 }
jvanverthfd359ca2016-03-18 11:57:24 -07001140 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001141
Greg Danielcb324152019-02-25 11:36:53 -05001142 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1143 return false;
1144 }
1145
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001146 SkASSERT(GrVkFormatColorTypePairIsValid(info.fFormat, colorType));
Brian Salomond17f6582017-07-19 18:28:58 -04001147 return true;
1148}
1149
Greg Danielcaa795f2019-05-14 11:54:25 -04001150static bool check_tex_image_info(const GrVkCaps& caps, const GrVkImageInfo& info) {
1151 if (info.fImageTiling == VK_IMAGE_TILING_OPTIMAL) {
Greg Daniel2f2caea2019-07-08 14:24:47 -04001152 if (!caps.isVkFormatTexturable(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001153 return false;
1154 }
1155 } else {
1156 SkASSERT(info.fImageTiling == VK_IMAGE_TILING_LINEAR);
Greg Daniel2f2caea2019-07-08 14:24:47 -04001157 if (!caps.isVkFormatTexturableLinearly(info.fFormat)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001158 return false;
1159 }
1160 }
1161 return true;
1162}
1163
Greg Daniel6fa62e22019-08-07 15:52:37 -04001164static bool check_rt_image_info(const GrVkCaps& caps, const GrVkImageInfo& info, int sampleCnt) {
1165 if (!caps.isFormatRenderable(info.fFormat, sampleCnt)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001166 return false;
1167 }
1168 return true;
1169}
1170
Brian Salomond17f6582017-07-19 18:28:58 -04001171sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001172 GrColorType colorType, GrWrapOwnership ownership,
1173 GrWrapCacheable cacheable, GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001174 GrVkImageInfo imageInfo;
1175 if (!backendTex.getVkImageInfo(&imageInfo)) {
1176 return nullptr;
1177 }
1178
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001179 if (!check_image_info(this->vkCaps(), imageInfo, colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001180 kAdopt_GrWrapOwnership == ownership)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001181 return nullptr;
1182 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001183 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1184 return nullptr;
1185 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001186
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001187 if (backendTex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1188 return nullptr;
1189 }
1190
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001191 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendTex.getBackendFormat(),
1192 colorType);
1193 SkASSERT(kUnknown_GrPixelConfig != config);
1194
Greg Daniel164a9f02016-02-22 09:56:40 -05001195 GrSurfaceDesc surfDesc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001196 surfDesc.fWidth = backendTex.width();
1197 surfDesc.fHeight = backendTex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001198 surfDesc.fConfig = config;
Greg Daniel164a9f02016-02-22 09:56:40 -05001199
Greg Daniel52e16d92018-04-10 09:34:07 -04001200 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1201 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001202 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1203 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001204}
1205
1206sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001207 int sampleCnt,
Robert Phillips0902c982019-07-16 07:47:56 -04001208 GrColorType colorType,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001209 GrWrapOwnership ownership,
1210 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001211 GrVkImageInfo imageInfo;
1212 if (!backendTex.getVkImageInfo(&imageInfo)) {
1213 return nullptr;
1214 }
1215
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001216 if (!check_image_info(this->vkCaps(), imageInfo, colorType,
Jim Van Verth658d4992019-07-11 14:07:53 -04001217 kAdopt_GrWrapOwnership == ownership)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001218 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001219 }
Greg Danielcaa795f2019-05-14 11:54:25 -04001220 if (!check_tex_image_info(this->vkCaps(), imageInfo)) {
1221 return nullptr;
1222 }
Greg Daniel6fa62e22019-08-07 15:52:37 -04001223 if (!check_rt_image_info(this->vkCaps(), imageInfo, sampleCnt)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001224 return nullptr;
1225 }
Brian Salomond17f6582017-07-19 18:28:58 -04001226
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001227 if (backendTex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1228 return nullptr;
1229 }
1230
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001231
1232 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendTex.getBackendFormat(),
1233 colorType);
1234 SkASSERT(kUnknown_GrPixelConfig != config);
Robert Phillips0902c982019-07-16 07:47:56 -04001235
Brian Salomond17f6582017-07-19 18:28:58 -04001236 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001237 surfDesc.fWidth = backendTex.width();
1238 surfDesc.fHeight = backendTex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001239 surfDesc.fConfig = config;
Greg Daniel6fa62e22019-08-07 15:52:37 -04001240 sampleCnt = this->vkCaps().getRenderTargetSampleCount(sampleCnt, imageInfo.fFormat);
Brian Salomond17f6582017-07-19 18:28:58 -04001241
Greg Daniel52e16d92018-04-10 09:34:07 -04001242 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1243 SkASSERT(layout);
1244
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001245 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001246 this, surfDesc, sampleCnt, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001247}
1248
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001249sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT,
1250 GrColorType colorType) {
Greg Daniele79b4732017-04-20 14:07:46 -04001251 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1252 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1253 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1254 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001255 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001256 return nullptr;
1257 }
halcanary9d524f22016-03-29 09:03:52 -07001258
Greg Daniel323fbcf2018-04-10 13:46:30 -04001259 GrVkImageInfo info;
1260 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001261 return nullptr;
1262 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001263
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001264 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendRT.getBackendFormat(),
1265 colorType);
1266 SkASSERT(kUnknown_GrPixelConfig != config);
1267
1268 if (!check_image_info(this->vkCaps(), info, colorType, false)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001269 return nullptr;
1270 }
Greg Daniel6fa62e22019-08-07 15:52:37 -04001271 if (!check_rt_image_info(this->vkCaps(), info, backendRT.sampleCnt())) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001272 return nullptr;
1273 }
1274
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001275 if (backendRT.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1276 return nullptr;
1277 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001278
Greg Daniel164a9f02016-02-22 09:56:40 -05001279 GrSurfaceDesc desc;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001280 desc.fWidth = backendRT.width();
1281 desc.fHeight = backendRT.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001282 desc.fConfig = config;
Greg Daniel164a9f02016-02-22 09:56:40 -05001283
Greg Daniel323fbcf2018-04-10 13:46:30 -04001284 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001285
Brian Salomon27b4d8d2019-07-22 14:23:45 -04001286 sk_sp<GrVkRenderTarget> tgt =
1287 GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, 1, info, std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001288
1289 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1290 SkASSERT(!backendRT.stencilBits());
1291 if (tgt) {
1292 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001293 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001294
Brian Salomon9c73e3d2019-08-15 10:55:49 -04001295 return tgt;
Greg Daniel164a9f02016-02-22 09:56:40 -05001296}
1297
Greg Daniel7ef28f32017-04-20 16:41:55 +00001298sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001299 int sampleCnt,
1300 GrColorType grColorType) {
Brian Osman33910292017-04-18 14:38:53 -04001301
Greg Daniel52e16d92018-04-10 09:34:07 -04001302 GrVkImageInfo imageInfo;
1303 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001304 return nullptr;
1305 }
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001306 if (!check_image_info(this->vkCaps(), imageInfo, grColorType, false)) {
Brian Osman33910292017-04-18 14:38:53 -04001307 return nullptr;
1308 }
Greg Daniel6fa62e22019-08-07 15:52:37 -04001309 if (!check_rt_image_info(this->vkCaps(), imageInfo, sampleCnt)) {
Greg Danielcaa795f2019-05-14 11:54:25 -04001310 return nullptr;
1311 }
Greg Danielcb324152019-02-25 11:36:53 -05001312
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001313 if (tex.isProtected() && (fProtectedContext == GrProtected::kNo)) {
1314 return nullptr;
1315 }
1316
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001317 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(tex.getBackendFormat(),
1318 grColorType);
1319 SkASSERT(kUnknown_GrPixelConfig != config);
1320
Brian Osman33910292017-04-18 14:38:53 -04001321 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001322 desc.fWidth = tex.width();
1323 desc.fHeight = tex.height();
Robert Phillipsc80b0e92019-07-23 10:27:09 -04001324 desc.fConfig = config;
1325
Greg Daniel6fa62e22019-08-07 15:52:37 -04001326 sampleCnt = this->vkCaps().getRenderTargetSampleCount(sampleCnt, imageInfo.fFormat);
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 Daniel6fa62e22019-08-07 15:52:37 -04001349 int sampleCnt = this->vkCaps().getRenderTargetSampleCount(1, vkInfo.fFormat);
Robert Phillipsd8f79a22019-06-24 13:25:42 -04001350 if (!sampleCnt) {
1351 return nullptr;
1352 }
1353
Greg Daniel6fa62e22019-08-07 15:52:37 -04001354 GrColorType grColorType = SkColorTypeToGrColorType(imageInfo.colorType());
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
Brian Salomond4764a12019-08-08 12:08:24 -04001864 VkFormat vkFormat;
1865 if (!format.asVkFormat(&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
Brian Salomond4764a12019-08-08 12:08:24 -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
Sergey Ulanov2739fd22019-08-11 22:46:33 -07001875 if (GrVkFormatNeedsYcbcrSampler(vkFormat)) {
1876 SkDebugf("Can't create BackendTexture that requires Ycbcb sampler.\n");
1877 return GrBackendTexture();
1878 }
1879
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001880 GrVkImageInfo info;
Brian Salomond4764a12019-08-08 12:08:24 -04001881 if (!this->createVkImageForBackendSurface(vkFormat, w, h, true,
Brian Salomonb450f3b2019-07-09 09:36:51 -04001882 GrRenderable::kYes == renderable, mipMapped, srcData,
1883 rowBytes, color, &info, isProtected)) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04001884 SkDebugf("Failed to create testing only image\n");
1885 return GrBackendTexture();
Brian Salomon52e943a2018-03-13 09:32:39 -04001886 }
Robert Phillips62221e72019-07-24 15:07:38 -04001887
1888 return GrBackendTexture(w, h, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001889}
1890
Robert Phillipsf0313ee2019-05-21 13:51:11 -04001891void GrVkGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -04001892 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
1893
1894 GrVkImageInfo info;
1895 if (tex.getVkImageInfo(&info)) {
1896 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
1897 }
1898}
1899
1900#if GR_TEST_UTILS
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001901bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001902 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001903
Greg Daniel52e16d92018-04-10 09:34:07 -04001904 GrVkImageInfo backend;
1905 if (!tex.getVkImageInfo(&backend)) {
1906 return false;
1907 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001908
Greg Daniel52e16d92018-04-10 09:34:07 -04001909 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001910 VkMemoryRequirements req;
1911 memset(&req, 0, sizeof(req));
1912 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001913 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001914 &req));
1915 // TODO: find a better check
1916 // This will probably fail with a different driver
1917 return (req.size > 0) && (req.size <= 8192 * 8192);
1918 }
1919
1920 return false;
1921}
1922
Brian Osman2d010b62018-08-09 10:55:09 -04001923GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Robert Phillipsf62e5752019-05-30 10:36:13 -04001924 this->handleDirtyContext();
1925
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001926 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1927 return GrBackendRenderTarget();
1928 }
1929
Greg Daniel0fac8692019-08-16 13:13:17 -04001930 VkFormat vkFormat = this->vkCaps().getFormatFromColorType(ct);
Robert Phillipsf62e5752019-05-30 10:36:13 -04001931
1932 GrVkImageInfo info;
Brian Salomonb450f3b2019-07-09 09:36:51 -04001933 if (!this->createVkImageForBackendSurface(vkFormat, w, h, false, true, GrMipMapped::kNo,
1934 nullptr, 0, &SkColors::kTransparent, &info,
1935 GrProtected::kNo)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001936 return {};
1937 }
Robert Phillips62221e72019-07-24 15:07:38 -04001938
1939 return GrBackendRenderTarget(w, h, 1, 0, info);
Brian Salomonf865b052018-03-09 09:01:53 -05001940}
1941
Brian Salomon52e943a2018-03-13 09:32:39 -04001942void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001943 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001944
Greg Daniel323fbcf2018-04-10 13:46:30 -04001945 GrVkImageInfo info;
1946 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001947 // something in the command buffer may still be using this, so force submit
1948 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001949 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001950 }
1951}
Brian Salomonf865b052018-03-09 09:01:53 -05001952
Greg Daniel26b50a42018-03-08 09:49:58 -05001953void GrVkGpu::testingOnly_flushGpuAndSync() {
1954 this->submitCommandBuffer(kForce_SyncQueue);
1955}
Brian Salomonf865b052018-03-09 09:01:53 -05001956#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001957
Greg Daniel164a9f02016-02-22 09:56:40 -05001958////////////////////////////////////////////////////////////////////////////////
1959
Greg Daniel59dc1482019-02-22 10:46:38 -05001960void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1961 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001962 VkPipelineStageFlags dstStageMask,
1963 bool byRegion,
1964 VkBufferMemoryBarrier* barrier) const {
1965 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001966 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001967 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001968 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001969 srcStageMask,
1970 dstStageMask,
1971 byRegion,
1972 GrVkCommandBuffer::kBufferMemory_BarrierType,
1973 barrier);
1974}
1975
Greg Daniel59dc1482019-02-22 10:46:38 -05001976void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1977 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001978 VkPipelineStageFlags dstStageMask,
1979 bool byRegion,
1980 VkImageMemoryBarrier* barrier) const {
1981 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001982 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001983 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001984 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001985 srcStageMask,
1986 dstStageMask,
1987 byRegion,
1988 GrVkCommandBuffer::kImageMemory_BarrierType,
1989 barrier);
1990}
1991
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001992void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxies[], int n,
Greg Daniel797efca2019-05-09 14:04:20 -04001993 SkSurface::BackendSurfaceAccess access, const GrFlushInfo& info,
1994 const GrPrepareForExternalIORequests& externalRequests) {
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001995 SkASSERT(n >= 0);
1996 SkASSERT(!n || proxies);
Greg Daniel51316782017-08-02 15:10:09 +00001997 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1998 // not effect what we do here.
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04001999 if (n && access == SkSurface::BackendSurfaceAccess::kPresent) {
Greg Danielbae71212019-03-01 15:24:35 -05002000 GrVkImage* image;
Brian Salomonf9a1fdf2019-05-09 10:30:12 -04002001 for (int i = 0; i < n; ++i) {
2002 SkASSERT(proxies[i]->isInstantiated());
2003 if (GrTexture* tex = proxies[i]->peekTexture()) {
2004 image = static_cast<GrVkTexture*>(tex);
2005 } else {
2006 GrRenderTarget* rt = proxies[i]->peekRenderTarget();
2007 SkASSERT(rt);
2008 image = static_cast<GrVkRenderTarget*>(rt);
2009 }
2010 image->prepareForPresent(this);
Greg Danielbae71212019-03-01 15:24:35 -05002011 }
Greg Danielbae71212019-03-01 15:24:35 -05002012 }
Greg Daniel797efca2019-05-09 14:04:20 -04002013
2014 // Handle requests for preparing for external IO
2015 for (int i = 0; i < externalRequests.fNumImages; ++i) {
2016 SkImage* image = externalRequests.fImages[i];
2017 if (!image->isTextureBacked()) {
2018 continue;
2019 }
2020 SkImage_GpuBase* gpuImage = static_cast<SkImage_GpuBase*>(as_IB(image));
2021 sk_sp<GrTextureProxy> proxy = gpuImage->asTextureProxyRef(this->getContext());
2022 SkASSERT(proxy);
2023
2024 if (!proxy->isInstantiated()) {
2025 auto resourceProvider = this->getContext()->priv().resourceProvider();
2026 if (!proxy->instantiate(resourceProvider)) {
2027 continue;
2028 }
2029 }
2030
2031 GrTexture* tex = proxy->peekTexture();
2032 if (!tex) {
2033 continue;
2034 }
2035 GrVkTexture* vkTex = static_cast<GrVkTexture*>(tex);
2036 vkTex->prepareForExternal(this);
2037 }
2038 for (int i = 0; i < externalRequests.fNumSurfaces; ++i) {
2039 SkSurface* surface = externalRequests.fSurfaces[i];
2040 if (!surface->getCanvas()->getGrContext()) {
2041 continue;
2042 }
2043 SkSurface_Gpu* gpuSurface = static_cast<SkSurface_Gpu*>(surface);
2044 auto* rtc = gpuSurface->getDevice()->accessRenderTargetContext();
2045 sk_sp<GrRenderTargetProxy> proxy = rtc->asRenderTargetProxyRef();
2046 if (!proxy->isInstantiated()) {
2047 auto resourceProvider = this->getContext()->priv().resourceProvider();
2048 if (!proxy->instantiate(resourceProvider)) {
2049 continue;
2050 }
2051 }
2052
2053 GrRenderTarget* rt = proxy->peekRenderTarget();
2054 SkASSERT(rt);
2055 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt);
2056 if (externalRequests.fPrepareSurfaceForPresent &&
2057 externalRequests.fPrepareSurfaceForPresent[i]) {
2058 vkRT->prepareForPresent(this);
2059 } else {
2060 vkRT->prepareForExternal(this);
2061 }
2062 }
2063
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002064 if (info.fFlags & kSyncCpu_GrFlushFlag) {
2065 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002066 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04002067 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05002068 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002069}
2070
Greg Daniel25af6712018-04-25 10:44:38 -04002071static int get_surface_sample_cnt(GrSurface* surf) {
2072 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002073 return rt->numSamples();
egdaniel17b89252016-04-05 07:23:38 -07002074 }
Greg Daniel25af6712018-04-25 10:44:38 -04002075 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05002076}
2077
Greg Daniel46cfbc62019-06-07 11:43:30 -04002078void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurface* src, GrVkImage* dstImage,
2079 GrVkImage* srcImage, const SkIRect& srcRect,
Greg Daniel164a9f02016-02-22 09:56:40 -05002080 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002081#ifdef SK_DEBUG
2082 int dstSampleCnt = get_surface_sample_cnt(dst);
2083 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002084 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2085 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002086 VkFormat dstFormat = dstImage->imageFormat();
2087 VkFormat srcFormat;
2088 SkAssertResult(dst->backendFormat().asVkFormat(&srcFormat));
2089 SkASSERT(this->vkCaps().canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
2090 srcFormat, srcSampleCnt, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04002091#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002092 if (src->isProtected() && !dst->isProtected()) {
2093 SkDebugf("Can't copy from protected memory to non-protected");
2094 return;
2095 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002096
Greg Daniel164a9f02016-02-22 09:56:40 -05002097 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
2098 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07002099 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07002100 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2101 VK_ACCESS_TRANSFER_WRITE_BIT,
2102 VK_PIPELINE_STAGE_TRANSFER_BIT,
2103 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002104
egdaniel17b89252016-04-05 07:23:38 -07002105 srcImage->setImageLayout(this,
2106 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002107 VK_ACCESS_TRANSFER_READ_BIT,
2108 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002109 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002110
Greg Daniel164a9f02016-02-22 09:56:40 -05002111 VkImageCopy copyRegion;
2112 memset(&copyRegion, 0, sizeof(VkImageCopy));
2113 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002114 copyRegion.srcOffset = { srcRect.fLeft, srcRect.fTop, 0 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002115 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002116 copyRegion.dstOffset = { dstPoint.fX, dstPoint.fY, 0 };
2117 copyRegion.extent = { (uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002118
2119 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07002120 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002121 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07002122 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05002123 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
2124 1,
2125 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07002126
2127 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
2128 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002129 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2130 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05002131}
2132
Greg Daniel46cfbc62019-06-07 11:43:30 -04002133void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurface* src, GrVkImage* dstImage,
2134 GrVkImage* srcImage, const SkIRect& srcRect,
egdaniel17b89252016-04-05 07:23:38 -07002135 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04002136#ifdef SK_DEBUG
2137 int dstSampleCnt = get_surface_sample_cnt(dst);
2138 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04002139 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2140 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002141 VkFormat dstFormat = dstImage->imageFormat();
2142 VkFormat srcFormat;
2143 SkAssertResult(dst->backendFormat().asVkFormat(&srcFormat));
2144 SkASSERT(this->vkCaps().canCopyAsBlit(dstFormat, dstSampleCnt, dstImage->isLinearTiled(),
2145 dstHasYcbcr, srcFormat, srcSampleCnt,
Greg Daniela51e93c2019-03-25 12:30:45 -04002146 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07002147
Greg Daniel25af6712018-04-25 10:44:38 -04002148#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002149 if (src->isProtected() && !dst->isProtected()) {
2150 SkDebugf("Can't copy from protected memory to non-protected");
2151 return;
2152 }
2153
egdaniel17b89252016-04-05 07:23:38 -07002154 dstImage->setImageLayout(this,
2155 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002156 VK_ACCESS_TRANSFER_WRITE_BIT,
2157 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002158 false);
2159
egdaniel17b89252016-04-05 07:23:38 -07002160 srcImage->setImageLayout(this,
2161 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002162 VK_ACCESS_TRANSFER_READ_BIT,
2163 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002164 false);
2165
2166 // Flip rect if necessary
Greg Daniel46cfbc62019-06-07 11:43:30 -04002167 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(),
2168 srcRect.height());
egdaniel17b89252016-04-05 07:23:38 -07002169
2170 VkImageBlit blitRegion;
2171 memset(&blitRegion, 0, sizeof(VkImageBlit));
2172 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel46cfbc62019-06-07 11:43:30 -04002173 blitRegion.srcOffsets[0] = { srcRect.fLeft, srcRect.fTop, 0 };
2174 blitRegion.srcOffsets[1] = { srcRect.fRight, srcRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002175 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2176 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002177 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002178
2179 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002180 *srcImage,
2181 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002182 1,
2183 &blitRegion,
2184 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002185
Greg Daniel46cfbc62019-06-07 11:43:30 -04002186 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2187 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002188}
2189
Greg Daniel46cfbc62019-06-07 11:43:30 -04002190void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
2191 const SkIPoint& dstPoint) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002192 if (src->isProtected() && !dst->isProtected()) {
2193 SkDebugf("Can't copy from protected memory to non-protected");
2194 return;
2195 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002196 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002197 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002198 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002199 srcRect.width(), srcRect.height());
Greg Daniel46cfbc62019-06-07 11:43:30 -04002200 // The rect is already in device space so we pass in kTopLeft so no flip is done.
2201 this->didWriteToSurface(dst, kTopLeft_GrSurfaceOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002202}
2203
Greg Daniel46cfbc62019-06-07 11:43:30 -04002204bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
Greg Daniele227fe42019-08-21 13:52:24 -04002205 const SkIPoint& dstPoint) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002206#ifdef SK_DEBUG
2207 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2208 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2209 }
2210 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2211 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2212 }
2213#endif
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002214 if (src->isProtected() && !dst->isProtected()) {
2215 SkDebugf("Can't copy from protected memory to non-protected");
2216 return false;
2217 }
Greg Danielbe7fc462019-01-03 16:40:42 -05002218
Greg Daniel25af6712018-04-25 10:44:38 -04002219 int dstSampleCnt = get_surface_sample_cnt(dst);
2220 int srcSampleCnt = get_surface_sample_cnt(src);
2221
egdaniel17b89252016-04-05 07:23:38 -07002222 GrVkImage* dstImage;
2223 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002224 GrRenderTarget* dstRT = dst->asRenderTarget();
2225 if (dstRT) {
2226 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002227 if (vkRT->wrapsSecondaryCommandBuffer()) {
2228 return false;
2229 }
Chris Dalton6ce447a2019-06-23 18:07:38 -06002230 dstImage = vkRT->numSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel4bcd62e2016-08-31 07:37:31 -07002231 } else {
2232 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002233 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002234 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002235 GrRenderTarget* srcRT = src->asRenderTarget();
2236 if (srcRT) {
2237 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
Chris Dalton6ce447a2019-06-23 18:07:38 -06002238 srcImage = vkRT->numSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002239 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002240 SkASSERT(src->asTexture());
2241 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002242 }
2243
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002244 VkFormat dstFormat = dstImage->imageFormat();
2245 VkFormat srcFormat = srcImage->imageFormat();
2246
Greg Daniela51e93c2019-03-25 12:30:45 -04002247 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2248 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2249
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002250 if (this->vkCaps().canCopyAsResolve(dstFormat, dstSampleCnt, dstHasYcbcr,
2251 srcFormat, srcSampleCnt, srcHasYcbcr)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002252 this->copySurfaceAsResolve(dst, src, srcRect, dstPoint);
Greg Daniela51e93c2019-03-25 12:30:45 -04002253 return true;
2254 }
2255
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002256 if (this->vkCaps().canCopyImage(dstFormat, dstSampleCnt, dstHasYcbcr,
2257 srcFormat, srcSampleCnt, srcHasYcbcr)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002258 this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002259 return true;
2260 }
2261
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002262 if (this->vkCaps().canCopyAsBlit(dstFormat, dstSampleCnt, dstImage->isLinearTiled(),
2263 dstHasYcbcr, srcFormat, srcSampleCnt,
Greg Daniela51e93c2019-03-25 12:30:45 -04002264 srcImage->isLinearTiled(), srcHasYcbcr)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002265 this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002266 return true;
2267 }
2268
Greg Daniel164a9f02016-02-22 09:56:40 -05002269 return false;
2270}
2271
Brian Salomona6948702018-06-01 15:33:20 -04002272bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
Brian Salomonf77c1462019-08-01 15:19:29 -04002273 GrColorType surfaceColorType, GrColorType dstColorType, void* buffer,
2274 size_t rowBytes) {
Emircan Uysaler23ca4e72019-06-24 10:53:09 -04002275 if (surface->isProtected()) {
2276 return false;
2277 }
2278
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002279 if (surfaceColorType != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002280 return false;
2281 }
2282
egdaniel66933552016-08-24 07:22:19 -07002283 GrVkImage* image = nullptr;
2284 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2285 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002286 // Reading from render targets that wrap a secondary command buffer is not allowed since
2287 // it would require us to know the VkImage, which we don't have, as well as need us to
2288 // stop and start the VkRenderPass which we don't have access to.
2289 if (rt->wrapsSecondaryCommandBuffer()) {
2290 return false;
2291 }
egdaniel66933552016-08-24 07:22:19 -07002292 // resolve the render target if necessary
2293 switch (rt->getResolveType()) {
2294 case GrVkRenderTarget::kCantResolve_ResolveType:
2295 return false;
2296 case GrVkRenderTarget::kAutoResolves_ResolveType:
2297 break;
2298 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002299 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002300 break;
2301 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002302 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002303 }
2304 image = rt;
2305 } else {
2306 image = static_cast<GrVkTexture*>(surface->asTexture());
2307 }
2308
2309 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002310 return false;
2311 }
2312
Greg Daniel475eb702018-09-28 14:16:50 -04002313 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2314 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2315 // image and then do the read pixels from that.
2316 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002317 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002318 int srcSampleCount = 0;
2319 if (rt) {
Chris Dalton6ce447a2019-06-23 18:07:38 -06002320 srcSampleCount = rt->numSamples();
Greg Daniel46cfbc62019-06-07 11:43:30 -04002321 }
2322 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Brian Salomon1c53a9f2019-08-12 14:10:12 -04002323 if (!this->vkCaps().canCopyAsBlit(VK_FORMAT_R8G8B8A8_UNORM, 1, false, false,
2324 image->imageFormat(), srcSampleCount,
2325 image->isLinearTiled(), srcHasYcbcr)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -04002326 return false;
2327 }
2328
Greg Daniel475eb702018-09-28 14:16:50 -04002329 // Make a new surface that is RGBA to copy the RGB surface into.
2330 GrSurfaceDesc surfDesc;
Greg Daniel475eb702018-09-28 14:16:50 -04002331 surfDesc.fWidth = width;
2332 surfDesc.fHeight = height;
2333 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
Greg Daniel475eb702018-09-28 14:16:50 -04002334
2335 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2336 VK_IMAGE_USAGE_SAMPLED_BIT |
2337 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2338 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2339
2340 GrVkImage::ImageDesc imageDesc;
2341 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2342 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2343 imageDesc.fWidth = width;
2344 imageDesc.fHeight = height;
2345 imageDesc.fLevels = 1;
2346 imageDesc.fSamples = 1;
2347 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2348 imageDesc.fUsageFlags = usageFlags;
2349 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2350
2351 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
Brian Salomon27b4d8d2019-07-22 14:23:45 -04002352 this, SkBudgeted::kYes, surfDesc, 1, imageDesc, GrMipMapsStatus::kNotAllocated);
Greg Daniel475eb702018-09-28 14:16:50 -04002353 if (!copySurface) {
2354 return false;
2355 }
2356
Greg Daniel475eb702018-09-28 14:16:50 -04002357 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
Greg Daniel46cfbc62019-06-07 11:43:30 -04002358 SkAssertResult(this->copySurface(copySurface.get(), surface, srcRect, SkIPoint::Make(0,0)));
2359
Greg Daniel475eb702018-09-28 14:16:50 -04002360 top = 0;
2361 left = 0;
2362 dstColorType = GrColorType::kRGBA_8888;
2363 image = copySurface.get();
2364 }
2365
Greg Daniel164a9f02016-02-22 09:56:40 -05002366 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002367 image->setImageLayout(this,
2368 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2369 VK_ACCESS_TRANSFER_READ_BIT,
2370 VK_PIPELINE_STAGE_TRANSFER_BIT,
2371 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002372
Brian Salomonb28cb682019-07-26 12:48:47 -04002373 size_t bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002374 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002375
Greg Daniel164a9f02016-02-22 09:56:40 -05002376 VkBufferImageCopy region;
2377 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002378
2379 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2380 if (copyFromOrigin) {
2381 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002382 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002383 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002384 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002385 region.imageOffset = offset;
2386 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2387 }
2388
2389 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002390 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002391 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002392 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002393 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002394 kStream_GrAccessPattern)
2395 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002396
2397 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002398 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002399 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002400 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2401 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002402
2403 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002404 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002405 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002406 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002407 1,
2408 &region);
2409
2410 // make sure the copy to buffer has finished
2411 transferBuffer->addMemoryBarrier(this,
2412 VK_ACCESS_TRANSFER_WRITE_BIT,
2413 VK_ACCESS_HOST_READ_BIT,
2414 VK_PIPELINE_STAGE_TRANSFER_BIT,
2415 VK_PIPELINE_STAGE_HOST_BIT,
2416 false);
2417
2418 // We need to submit the current command buffer to the Queue and make sure it finishes before
2419 // we can copy the data out of the buffer.
2420 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002421 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002422 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002423 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002424
egdaniel6fa0a912016-09-12 11:51:29 -07002425 if (copyFromOrigin) {
2426 uint32_t skipRows = region.imageExtent.height - height;
2427 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2428 }
2429
Brian Salomona6948702018-06-01 15:33:20 -04002430 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002431
2432 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002433 return true;
2434}
egdaniel066df7c2016-06-08 14:02:27 -07002435
egdaniel27bb2842016-07-07 11:58:35 -07002436// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2437// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2438// the the entire attachment. Similar requirements for the y and height components.
2439void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2440 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2441 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002442 if ((0 != granularity.width && 1 != granularity.width)) {
2443 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2444 int rightAdj = srcBounds.fRight % granularity.width;
2445 if (rightAdj != 0) {
2446 rightAdj = granularity.width - rightAdj;
2447 }
2448 dstBounds->fRight = srcBounds.fRight + rightAdj;
2449 if (dstBounds->fRight > maxWidth) {
2450 dstBounds->fRight = maxWidth;
2451 dstBounds->fLeft = 0;
2452 } else {
2453 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2454 }
egdaniel27bb2842016-07-07 11:58:35 -07002455 } else {
egdanield5797b32016-09-20 12:57:45 -07002456 dstBounds->fLeft = srcBounds.fLeft;
2457 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002458 }
2459
2460 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002461 if ((0 != granularity.height && 1 != granularity.height)) {
2462 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2463 int bottomAdj = srcBounds.fBottom % granularity.height;
2464 if (bottomAdj != 0) {
2465 bottomAdj = granularity.height - bottomAdj;
2466 }
2467 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2468 if (dstBounds->fBottom > maxHeight) {
2469 dstBounds->fBottom = maxHeight;
2470 dstBounds->fTop = 0;
2471 } else {
2472 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2473 }
egdaniel27bb2842016-07-07 11:58:35 -07002474 } else {
egdanield5797b32016-09-20 12:57:45 -07002475 dstBounds->fTop = srcBounds.fTop;
2476 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002477 }
2478}
2479
Greg Daniel8daf3b72019-07-30 09:57:26 -04002480void GrVkGpu::submitSecondaryCommandBuffer(
Greg Daniel228518f2019-08-07 16:55:17 -04002481 std::unique_ptr<GrVkSecondaryCommandBuffer> buffer,
Greg Daniel8daf3b72019-07-30 09:57:26 -04002482 const GrVkRenderPass* renderPass,
2483 const VkClearValue* colorClear,
2484 GrVkRenderTarget* target, GrSurfaceOrigin origin,
2485 const SkIRect& bounds) {
2486
Greg Danielbe7fc462019-01-03 16:40:42 -05002487 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002488 const SkIRect* pBounds = &bounds;
2489 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002490 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002491 flippedBounds = bounds;
2492 flippedBounds.fTop = target->height() - bounds.fBottom;
2493 flippedBounds.fBottom = target->height() - bounds.fTop;
2494 pBounds = &flippedBounds;
2495 }
2496
egdaniel27bb2842016-07-07 11:58:35 -07002497 // The bounds we use for the render pass should be of the granularity supported
2498 // by the device.
2499 const VkExtent2D& granularity = renderPass->granularity();
2500 SkIRect adjustedBounds;
2501 if ((0 != granularity.width && 1 != granularity.width) ||
2502 (0 != granularity.height && 1 != granularity.height)) {
2503 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2504 target->width(), target->height());
2505 pBounds = &adjustedBounds;
2506 }
2507
Robert Phillips95214472017-08-08 18:00:03 -04002508#ifdef SK_DEBUG
2509 uint32_t index;
2510 bool result = renderPass->colorAttachmentIndex(&index);
2511 SkASSERT(result && 0 == index);
2512 result = renderPass->stencilAttachmentIndex(&index);
2513 if (result) {
2514 SkASSERT(1 == index);
2515 }
2516#endif
2517 VkClearValue clears[2];
2518 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002519 clears[1].depthStencil.depth = 0.0f;
2520 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002521
2522 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel228518f2019-08-07 16:55:17 -04002523 fCurrentCmdBuffer->executeCommands(this, std::move(buffer));
Greg Daniel164a9f02016-02-22 09:56:40 -05002524 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002525
Brian Salomon1fabd512018-02-09 09:54:25 -05002526 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002527}
egdaniel9cb63402016-06-23 08:37:05 -07002528
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002529void GrVkGpu::submit(GrOpsRenderPass* renderPass) {
2530 SkASSERT(fCachedOpsRenderPass.get() == renderPass);
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002531
Greg Daniel2d41d0d2019-08-26 11:08:51 -04002532 fCachedOpsRenderPass->submit();
2533 fCachedOpsRenderPass->reset();
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002534}
2535
Greg Daniel6be35232017-03-01 17:01:09 -05002536GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002537 VkFenceCreateInfo createInfo;
2538 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2539 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2540 createInfo.pNext = nullptr;
2541 createInfo.flags = 0;
2542 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002543
2544 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2545 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2546
2547 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002548 return (GrFence)fence;
2549}
2550
Greg Daniel6be35232017-03-01 17:01:09 -05002551bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2552 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2553
2554 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002555 return (VK_SUCCESS == result);
2556}
2557
2558void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002559 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2560}
2561
Greg Daniela5cb7812017-06-16 09:45:32 -04002562sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2563 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002564}
2565
Greg Daniel48661b82018-01-22 16:11:35 -05002566sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2567 GrResourceProvider::SemaphoreWrapType wrapType,
2568 GrWrapOwnership ownership) {
2569 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002570}
2571
Greg Daniel858e12c2018-12-06 11:11:37 -05002572void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002573 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2574
Greg Daniel48661b82018-01-22 16:11:35 -05002575 GrVkSemaphore::Resource* resource = vkSem->getResource();
2576 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002577 resource->ref();
2578 fSemaphoresToSignal.push_back(resource);
2579 }
Greg Daniel6be35232017-03-01 17:01:09 -05002580}
2581
Greg Daniel48661b82018-01-22 16:11:35 -05002582void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002583 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2584
Greg Daniel48661b82018-01-22 16:11:35 -05002585 GrVkSemaphore::Resource* resource = vkSem->getResource();
2586 if (resource->shouldWait()) {
2587 resource->ref();
2588 fSemaphoresToWaitOn.push_back(resource);
2589 }
jvanverth84741b32016-09-30 08:39:02 -07002590}
Brian Osman13dddce2017-05-09 13:19:50 -04002591
2592sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2593 SkASSERT(texture);
2594 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2595 vkTexture->setImageLayout(this,
2596 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2597 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002598 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002599 false);
2600 this->submitCommandBuffer(kSkip_SyncQueue);
2601
Greg Danielb3f66542019-05-10 17:11:19 -04002602 // The image layout change serves as a barrier, so no semaphore is needed.
2603 // If we ever decide we need to return a semaphore here, we need to make sure GrVkSemaphore is
2604 // thread safe so that only the first thread that tries to use the semaphore actually submits
2605 // it. This additionally would also require thread safety in command buffer submissions to
2606 // queues in general.
Brian Osman13dddce2017-05-09 13:19:50 -04002607 return nullptr;
2608}
Greg Danielf5d87582017-12-18 14:48:15 -05002609
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002610void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2611 fDrawables.emplace_back(std::move(drawable));
2612}
2613
Greg Daniel7a82edf2018-12-04 10:54:34 -05002614uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2615 const GrBackendFormat& format) {
2616 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2617 SkASSERT(ycbcrInfo);
2618 if (!ycbcrInfo->isValid()) {
2619 return 0;
2620 }
2621
2622 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2623 samplerState, *ycbcrInfo);
2624
Sergey Ulanov2739fd22019-08-11 22:46:33 -07002625 uint32_t result = sampler->uniqueID();
2626
2627 sampler->unref(this);
2628
2629 return result;
Greg Daniel7a82edf2018-12-04 10:54:34 -05002630}
2631
Greg Daniela870b462019-01-08 15:49:46 -05002632void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002633 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002634 this->resourceProvider().storePipelineCacheData();
2635 }
2636}