blob: 34ec187667e2ac28d75c2291c52a393aa66dc3b0 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/vk/GrVkGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/gpu/GrBackendSemaphore.h"
11#include "include/gpu/GrBackendSurface.h"
12#include "include/gpu/GrContextOptions.h"
13#include "include/private/SkTo.h"
14#include "src/core/SkConvertPixels.h"
15#include "src/core/SkMipMap.h"
16#include "src/gpu/GrContextPriv.h"
17#include "src/gpu/GrGeometryProcessor.h"
18#include "src/gpu/GrGpuResourceCacheAccess.h"
19#include "src/gpu/GrMesh.h"
20#include "src/gpu/GrPipeline.h"
21#include "src/gpu/GrRenderTargetPriv.h"
22#include "src/gpu/GrTexturePriv.h"
23#include "src/gpu/vk/GrVkAMDMemoryAllocator.h"
24#include "src/gpu/vk/GrVkCommandBuffer.h"
25#include "src/gpu/vk/GrVkCommandPool.h"
26#include "src/gpu/vk/GrVkGpuCommandBuffer.h"
27#include "src/gpu/vk/GrVkImage.h"
28#include "src/gpu/vk/GrVkIndexBuffer.h"
29#include "src/gpu/vk/GrVkInterface.h"
30#include "src/gpu/vk/GrVkMemory.h"
31#include "src/gpu/vk/GrVkPipeline.h"
32#include "src/gpu/vk/GrVkPipelineState.h"
33#include "src/gpu/vk/GrVkRenderPass.h"
34#include "src/gpu/vk/GrVkResourceProvider.h"
35#include "src/gpu/vk/GrVkSemaphore.h"
36#include "src/gpu/vk/GrVkTexture.h"
37#include "src/gpu/vk/GrVkTextureRenderTarget.h"
38#include "src/gpu/vk/GrVkTransferBuffer.h"
39#include "src/gpu/vk/GrVkVertexBuffer.h"
40#include "src/sksl/SkSLCompiler.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040041
Mike Kleinc0bd9f92019-04-23 12:05:21 -050042#include "include/gpu/vk/GrVkExtensions.h"
43#include "include/gpu/vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050044
Ben Wagnerf08d1d02018-06-18 15:11:00 -040045#include <utility>
46
Forrest Reiling44f85712017-03-27 23:22:20 -070047#if !defined(SK_BUILD_FOR_WIN)
48#include <unistd.h>
49#endif // !defined(SK_BUILD_FOR_WIN)
50
Greg Danieldef55462018-08-01 13:40:14 -040051#if defined(SK_BUILD_FOR_WIN) && defined(SK_DEBUG)
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "include/private/SkLeanWindows.h"
Greg Danieldef55462018-08-01 13:40:14 -040053#endif
54
Greg Daniel164a9f02016-02-22 09:56:40 -050055#define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X)
56#define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X)
57#define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X)
58
Greg Danielf730c182018-07-02 20:15:37 +000059sk_sp<GrGpu> GrVkGpu::Make(const GrVkBackendContext& backendContext,
Brian Salomon384fab42017-12-07 12:33:05 -050060 const GrContextOptions& options, GrContext* context) {
Greg Danielf730c182018-07-02 20:15:37 +000061 if (backendContext.fInstance == VK_NULL_HANDLE ||
62 backendContext.fPhysicalDevice == VK_NULL_HANDLE ||
63 backendContext.fDevice == VK_NULL_HANDLE ||
64 backendContext.fQueue == VK_NULL_HANDLE) {
65 return nullptr;
66 }
Greg Danield3e65aa2018-08-01 09:19:45 -040067 if (!backendContext.fGetProc) {
68 return nullptr;
Greg Danielc8cd45a2018-07-12 10:02:37 -040069 }
Greg Danield3e65aa2018-08-01 09:19:45 -040070
Greg Daniel41f0e282019-01-28 13:15:05 -050071 PFN_vkEnumerateInstanceVersion localEnumerateInstanceVersion =
72 reinterpret_cast<PFN_vkEnumerateInstanceVersion>(
73 backendContext.fGetProc("vkEnumerateInstanceVersion",
74 VK_NULL_HANDLE, VK_NULL_HANDLE));
75 uint32_t instanceVersion = 0;
76 if (!localEnumerateInstanceVersion) {
77 instanceVersion = VK_MAKE_VERSION(1, 0, 0);
78 } else {
79 VkResult err = localEnumerateInstanceVersion(&instanceVersion);
80 if (err) {
81 SkDebugf("Failed to enumerate instance version. Err: %d\n", err);
82 return nullptr;
83 }
84 }
85
Greg Danielc0b03d82018-08-03 14:41:15 -040086 PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
87 reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
88 backendContext.fGetProc("vkGetPhysicalDeviceProperties",
89 backendContext.fInstance,
90 VK_NULL_HANDLE));
91
92 if (!localGetPhysicalDeviceProperties) {
93 return nullptr;
94 }
95 VkPhysicalDeviceProperties physDeviceProperties;
96 localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
97 uint32_t physDevVersion = physDeviceProperties.apiVersion;
98
Greg Daniel41f0e282019-01-28 13:15:05 -050099 uint32_t apiVersion = backendContext.fMaxAPIVersion ? backendContext.fMaxAPIVersion
100 : instanceVersion;
101
102 instanceVersion = SkTMin(instanceVersion, apiVersion);
103 physDevVersion = SkTMin(physDevVersion, apiVersion);
104
Greg Daniel98bffae2018-08-01 13:25:41 -0400105 sk_sp<const GrVkInterface> interface;
Greg Danield3e65aa2018-08-01 09:19:45 -0400106
Greg Daniel98bffae2018-08-01 13:25:41 -0400107 if (backendContext.fVkExtensions) {
108 interface.reset(new GrVkInterface(backendContext.fGetProc,
109 backendContext.fInstance,
110 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500111 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400112 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400113 backendContext.fVkExtensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500114 if (!interface->validate(instanceVersion, physDevVersion, backendContext.fVkExtensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400115 return nullptr;
116 }
117 } else {
Greg Daniel98bffae2018-08-01 13:25:41 -0400118 GrVkExtensions extensions;
Greg Daniel88e8ddc2019-04-25 16:37:08 -0400119 // The only extension flag that may effect the vulkan backend is the swapchain extension. We
120 // need to know if this is enabled to know if we can transition to a present layout when
121 // flushing a surface.
122 if (backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag) {
123 const char* swapChainExtName = VK_KHR_SWAPCHAIN_EXTENSION_NAME;
124 extensions.init(backendContext.fGetProc, backendContext.fInstance,
125 backendContext.fPhysicalDevice, 0, nullptr, 1, &swapChainExtName);
126 }
Greg Daniel98bffae2018-08-01 13:25:41 -0400127 interface.reset(new GrVkInterface(backendContext.fGetProc,
128 backendContext.fInstance,
129 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500130 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400131 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400132 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500133 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400134 return nullptr;
135 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500136 }
137
Greg Daniel41f0e282019-01-28 13:15:05 -0500138 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
139 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500140}
141
142////////////////////////////////////////////////////////////////////////////////
143
halcanary9d524f22016-03-29 09:03:52 -0700144GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500145 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
146 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500147 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400148 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000149 , fMemoryAllocator(backendContext.fMemoryAllocator)
150 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400151 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000152 , fDevice(backendContext.fDevice)
153 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400154 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500155 , fResourceProvider(this)
156 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000157 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700158
Greg Daniel81df0412018-05-31 13:13:33 -0400159 if (!fMemoryAllocator) {
160 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000161 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400162 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400163 }
164
ethannicholasb3058bd2016-07-01 08:22:01 -0700165 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700166
Greg Daniela0651ac2018-08-08 09:23:18 -0400167 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400168 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400169 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500170 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400171 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400172 } else if (backendContext.fDeviceFeatures) {
173 VkPhysicalDeviceFeatures2 features2;
174 features2.pNext = nullptr;
175 features2.features = *backendContext.fDeviceFeatures;
176 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500177 features2, instanceVersion, physicalDeviceVersion,
178 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400179 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400180 VkPhysicalDeviceFeatures2 features;
181 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
182 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400183 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400185 }
186 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400187 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400188 }
189 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400190 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400191 }
192 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500193 features, instanceVersion, physicalDeviceVersion,
194 GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400195 }
jvanverth633b3562016-03-23 11:01:22 -0700196 fCaps.reset(SkRef(fVkCaps.get()));
197
Greg Danielf730c182018-07-02 20:15:37 +0000198 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
199 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700200
Greg Daniela870b462019-01-08 15:49:46 -0500201 fResourceProvider.init();
202
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500203 fCmdPool = fResourceProvider.findOrCreateCommandPool();
204 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000205 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700206 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500207}
208
Greg Daniel8606cf82017-05-08 16:17:53 -0400209void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500210 if (fCmdPool) {
211 fCmdPool->getPrimaryCommandBuffer()->end(this);
212 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400213 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500214
215 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500216 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700217
218 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
219 // on the command buffers even though they have completed. This causes an assert to fire when
220 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500221 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700222#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500223 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700224#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500225 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700226#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500227 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700228#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500229 }
egdanielf8c2be32016-06-24 13:18:27 -0700230#endif
231
egdanielbe9d8212016-09-20 08:54:23 -0700232#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400233 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700234#endif
halcanary9d524f22016-03-29 09:03:52 -0700235
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500236 if (fCmdPool) {
237 fCmdPool->unref(this);
238 fCmdPool = nullptr;
239 }
240
Greg Daniel6be35232017-03-01 17:01:09 -0500241 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
242 fSemaphoresToWaitOn[i]->unref(this);
243 }
244 fSemaphoresToWaitOn.reset();
245
Greg Daniela5cb7812017-06-16 09:45:32 -0400246 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
247 fSemaphoresToSignal[i]->unref(this);
248 }
249 fSemaphoresToSignal.reset();
250
251
egdanielbc9b2962016-09-27 08:00:53 -0700252 fCopyManager.destroyResources(this);
253
Jim Van Verth09557d72016-11-07 11:10:21 -0500254 // must call this just before we destroy the command pool and VkDevice
255 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500256
Greg Danielf730c182018-07-02 20:15:37 +0000257 fMemoryAllocator.reset();
258
259 fQueue = VK_NULL_HANDLE;
260 fDevice = VK_NULL_HANDLE;
261 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400262}
263
264GrVkGpu::~GrVkGpu() {
265 if (!fDisconnected) {
266 this->destroyResources();
267 }
268 delete fCompiler;
269}
270
271
272void GrVkGpu::disconnect(DisconnectType type) {
273 INHERITED::disconnect(type);
274 if (!fDisconnected) {
275 if (DisconnectType::kCleanup == type) {
276 this->destroyResources();
277 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500278 if (fCmdPool) {
279 fCmdPool->unrefAndAbandon();
280 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400281 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400282 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
283 fSemaphoresToWaitOn[i]->unrefAndAbandon();
284 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400285 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
286 fSemaphoresToSignal[i]->unrefAndAbandon();
287 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400288 fCopyManager.abandonResources();
289
290 // must call this just before we destroy the command pool and VkDevice
291 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400292
293 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400294 }
295 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400296 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400297 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400298 fDisconnected = true;
299 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500300}
301
302///////////////////////////////////////////////////////////////////////////////
303
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400304GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400305 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400306 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
307 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400308 if (!fCachedRTCommandBuffer) {
309 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
310 }
311
Greg Daniela41a74a2018-10-09 12:59:23 +0000312 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400313 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400314}
315
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400316GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
317 if (!fCachedTexCommandBuffer) {
318 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
319 }
320
321 fCachedTexCommandBuffer->set(texture, origin);
322 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700323}
324
Greg Daniela3aa75a2019-04-12 14:24:55 -0400325void GrVkGpu::submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc,
326 GrGpuFinishedContext finishedContext) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500327 SkASSERT(fCurrentCmdBuffer);
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400328
329 if (!fCurrentCmdBuffer->hasWork() && kForce_SyncQueue != sync &&
330 !fSemaphoresToSignal.count() && !fSemaphoresToWaitOn.count()) {
331 SkASSERT(fDrawables.empty());
Robert Phillips84614c32019-04-05 09:36:00 -0400332 fResourceProvider.checkCommandBuffers();
Greg Daniela3aa75a2019-04-12 14:24:55 -0400333 if (finishedProc) {
334 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
335 }
Robert Phillipsce0a2bf2019-04-02 13:37:34 -0400336 return;
337 }
338
Greg Daniel164a9f02016-02-22 09:56:40 -0500339 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500340 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400341 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500342
Greg Daniela3aa75a2019-04-12 14:24:55 -0400343 if (finishedProc) {
344 // Make sure this is called after closing the current command pool
345 fResourceProvider.addFinishedProcToActiveCommandBuffers(finishedProc, finishedContext);
346 }
347
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400348 // We must delete and drawables that have been waitint till submit for us to destroy.
349 fDrawables.reset();
350
Greg Daniel6be35232017-03-01 17:01:09 -0500351 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
352 fSemaphoresToWaitOn[i]->unref(this);
353 }
354 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400355 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
356 fSemaphoresToSignal[i]->unref(this);
357 }
358 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500359
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500360 // Release old command pool and create a new one
361 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500362 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500363 fCmdPool = fResourceProvider.findOrCreateCommandPool();
364 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500365 fCurrentCmdBuffer->begin(this);
366}
367
368///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500369sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
370 GrAccessPattern accessPattern, const void* data) {
371 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700372 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500373 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700374 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
375 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500376 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700377 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500378 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700379 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
380 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500381 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700382 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500383 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400384 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
385 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500386 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700387 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500388 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400389 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
390 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500391 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700392 break;
cdalton397536c2016-03-25 12:15:03 -0700393 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400394 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700395 return nullptr;
396 }
cdalton1bf3e712016-04-19 10:00:02 -0700397 if (data && buff) {
398 buff->updateData(data, size);
399 }
400 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500401}
402
Brian Salomona9b04b92018-06-01 15:04:28 -0400403bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
404 GrColorType srcColorType, const GrMipLevel texels[],
405 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500406 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
407 if (!vkTex) {
408 return false;
409 }
410
jvanverth900bd4a2016-04-29 13:53:12 -0700411 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400412 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800413 return false;
414 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800415
Jim Van Verth1676cb92019-01-15 13:24:45 -0500416 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500417 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400418 bool linearTiling = vkTex->isLinearTiled();
419 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400420 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400421 SkDebugf("Can't upload mipmap data to linear tiled texture");
422 return false;
423 }
424 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
425 // Need to change the layout to general in order to perform a host write
426 vkTex->setImageLayout(this,
427 VK_IMAGE_LAYOUT_GENERAL,
428 VK_ACCESS_HOST_WRITE_BIT,
429 VK_PIPELINE_STAGE_HOST_BIT,
430 false);
431 this->submitCommandBuffer(kForce_SyncQueue);
432 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400433 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400434 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500435 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400436 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400437 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
438 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500439 }
egdaniel4583ec52016-06-27 12:57:00 -0700440
jvanverth900bd4a2016-04-29 13:53:12 -0700441 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500442}
443
Brian Salomone05ba5a2019-04-08 11:59:07 -0400444bool GrVkGpu::onTransferPixelsTo(GrTexture* texture, int left, int top, int width, int height,
445 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
446 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500447 // Can't transfer compressed data
448 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
449
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400450 // Vulkan only supports 4-byte aligned offsets
451 if (SkToBool(bufferOffset & 0x2)) {
452 return false;
453 }
454 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
455 if (!vkTex) {
456 return false;
457 }
458 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
459 if (!vkBuffer) {
460 return false;
461 }
462
Greg Daniel660cc992017-06-26 14:55:05 -0400463 SkDEBUGCODE(
464 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
465 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
466 SkASSERT(bounds.contains(subRect));
467 )
Brian Salomonc320b152018-02-20 14:05:36 -0500468 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400469 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500470 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400471 }
472
473 // Set up copy region
474 VkBufferImageCopy region;
475 memset(&region, 0, sizeof(VkBufferImageCopy));
476 region.bufferOffset = bufferOffset;
477 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
478 region.bufferImageHeight = 0;
479 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
480 region.imageOffset = { left, top, 0 };
481 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
482
483 // Change layout of our target so it can be copied to
484 vkTex->setImageLayout(this,
485 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
486 VK_ACCESS_TRANSFER_WRITE_BIT,
487 VK_PIPELINE_STAGE_TRANSFER_BIT,
488 false);
489
490 // Copy the buffer to the image
491 fCurrentCmdBuffer->copyBufferToImage(this,
492 vkBuffer,
493 vkTex,
494 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
495 1,
496 &region);
497
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400498 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400499 return true;
500}
501
Brian Salomon26de56e2019-04-10 12:14:26 -0400502bool GrVkGpu::onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
503 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
504 size_t offset) {
Brian Salomona585fe92019-04-09 14:57:00 -0400505 SkASSERT(surface);
506 SkASSERT(transferBuffer);
507
Brian Salomona585fe92019-04-09 14:57:00 -0400508 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
509
510 GrVkImage* srcImage;
511 if (GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget())) {
512 // Reading from render targets that wrap a secondary command buffer is not allowed since
513 // it would require us to know the VkImage, which we don't have, as well as need us to
514 // stop and start the VkRenderPass which we don't have access to.
515 if (rt->wrapsSecondaryCommandBuffer()) {
516 return false;
517 }
518 // resolve the render target if necessary
519 switch (rt->getResolveType()) {
520 case GrVkRenderTarget::kCantResolve_ResolveType:
521 return false;
522 case GrVkRenderTarget::kAutoResolves_ResolveType:
523 break;
524 case GrVkRenderTarget::kCanResolve_ResolveType:
525 this->resolveRenderTargetNoFlush(rt);
526 break;
527 default:
528 SK_ABORT("Unknown resolve type");
529 }
530 srcImage = rt;
531 } else {
532 srcImage = static_cast<GrVkTexture*>(surface->asTexture());
533 }
534
535 // Set up copy region
536 VkBufferImageCopy region;
537 memset(&region, 0, sizeof(VkBufferImageCopy));
538 region.bufferOffset = offset;
Brian Salomon26de56e2019-04-10 12:14:26 -0400539 region.bufferRowLength = width;
Brian Salomona585fe92019-04-09 14:57:00 -0400540 region.bufferImageHeight = 0;
541 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
542 region.imageOffset = { left, top, 0 };
543 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
544
545 srcImage->setImageLayout(this,
546 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
547 VK_ACCESS_TRANSFER_READ_BIT,
548 VK_PIPELINE_STAGE_TRANSFER_BIT,
549 false);
550
551 fCurrentCmdBuffer->copyImageToBuffer(this, srcImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
552 vkBuffer, 1, &region);
553
554 // Make sure the copy to buffer has finished.
555 vkBuffer->addMemoryBarrier(this,
556 VK_ACCESS_TRANSFER_WRITE_BIT,
557 VK_ACCESS_HOST_READ_BIT,
558 VK_PIPELINE_STAGE_TRANSFER_BIT,
559 VK_PIPELINE_STAGE_HOST_BIT,
560 false);
561
562 // The caller is responsible for syncing.
563 this->submitCommandBuffer(kSkip_SyncQueue);
564
Brian Salomon26de56e2019-04-10 12:14:26 -0400565 return true;
Brian Salomona585fe92019-04-09 14:57:00 -0400566}
567
Brian Salomon1fabd512018-02-09 09:54:25 -0500568void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
569 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700570 SkASSERT(dst);
571 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
572
egdaniel4bcd62e2016-08-31 07:37:31 -0700573 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500574 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
575 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
576 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
577 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
578 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700579
Greg Danielbc26c392017-04-18 13:32:10 -0400580 GrVkImage* dstImage;
581 GrRenderTarget* dstRT = dst->asRenderTarget();
582 if (dstRT) {
583 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400584 dstImage = vkRT;
585 } else {
586 SkASSERT(dst->asTexture());
587 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
588 }
589 dstImage->setImageLayout(this,
590 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
591 VK_ACCESS_TRANSFER_WRITE_BIT,
592 VK_PIPELINE_STAGE_TRANSFER_BIT,
593 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700594
595 src->msaaImage()->setImageLayout(this,
596 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
597 VK_ACCESS_TRANSFER_READ_BIT,
598 VK_PIPELINE_STAGE_TRANSFER_BIT,
599 false);
600
Greg Danielbc26c392017-04-18 13:32:10 -0400601 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700602}
603
Brian Salomon1fabd512018-02-09 09:54:25 -0500604void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700605 if (target->needsResolve()) {
606 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700607 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
608 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500609
egdaniel4bcd62e2016-08-31 07:37:31 -0700610 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700611
Brian Salomon1fabd512018-02-09 09:54:25 -0500612 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700613
614 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500615
616 if (requiresSubmit) {
617 this->submitCommandBuffer(kSkip_SyncQueue);
618 }
egdaniel52ad2512016-08-04 12:50:01 -0700619 }
620}
621
Brian Salomona9b04b92018-06-01 15:04:28 -0400622bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
623 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500624 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700625 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500626
Jim Van Verth1676cb92019-01-15 13:24:45 -0500627 // If we're uploading compressed data then we should be using uploadCompressedTexData
628 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
629 GrSRGBEncoded::kNo)));
630
Greg Daniel660cc992017-06-26 14:55:05 -0400631 SkDEBUGCODE(
632 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
633 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
634 SkASSERT(bounds.contains(subRect));
635 )
Brian Salomonc320b152018-02-20 14:05:36 -0500636 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500637 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400638 if (!rowBytes) {
639 rowBytes = trimRowBytes;
640 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500641
jvanverth900bd4a2016-04-29 13:53:12 -0700642 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
643 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
644 const VkImageSubresource subres = {
645 VK_IMAGE_ASPECT_COLOR_BIT,
646 0, // mipLevel
647 0, // arraySlice
648 };
649 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500650
jvanverth900bd4a2016-04-29 13:53:12 -0700651 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500652
jvanverth900bd4a2016-04-29 13:53:12 -0700653 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700654 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700655 &subres,
656 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500657
jvanverth1e305ba2016-06-01 09:39:15 -0700658 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400659 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700660 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400661 SkASSERT(size + offset <= alloc.fSize);
662 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
663 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700664 return false;
665 }
Greg Daniel81df0412018-05-31 13:13:33 -0400666 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700667
Brian Salomona9b04b92018-06-01 15:04:28 -0400668 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
669 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700670
Greg Daniele35a99e2018-03-02 11:44:22 -0500671 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400672 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700673
674 return true;
675}
676
Brian Salomona9b04b92018-06-01 15:04:28 -0400677bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
678 GrColorType dataColorType, const GrMipLevel texels[],
679 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700680 SkASSERT(!tex->isLinearTiled());
681 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400682 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700683 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
684
Greg Danieldd20e912017-04-07 14:42:23 -0400685 // We assume that if the texture has mip levels, we either upload to all the levels or just the
686 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400687 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400688
Jim Van Verth1676cb92019-01-15 13:24:45 -0500689 // If we're uploading compressed data then we should be using uploadCompressedTexData
690 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
691 GrSRGBEncoded::kNo)));
692
jvanverth900bd4a2016-04-29 13:53:12 -0700693 if (width == 0 || height == 0) {
694 return false;
695 }
696
Greg Daniel475eb702018-09-28 14:16:50 -0400697 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
698 return false;
699 }
700
701 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
702 // dst RGB texture. Thus we do not upload mip levels for that.
Greg Danielf259b8b2019-02-14 09:03:43 -0500703 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
704 SkASSERT(tex->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -0400705 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
706 // blit or draw.
707 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
708 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
709 return false;
710 }
711 mipLevelCount = 1;
712 }
713
Brian Salomond1eaf492017-05-18 10:02:08 -0400714 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500715 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700716
717 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700718 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
719 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400720 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
721
Greg Daniel475eb702018-09-28 14:16:50 -0400722 texelsShallowCopy.reset(mipLevelCount);
723 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700724
Robert Phillips590533f2017-07-11 14:22:35 -0400725 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700726 individualMipOffsets.push_back(0);
727 size_t combinedBufferSize = width * bpp * height;
728 int currentWidth = width;
729 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400730 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400731 combinedBufferSize = 0;
732 }
733
Greg Daniel468fd632017-03-22 17:03:45 -0400734 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
735 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
736 SkASSERT((bpp & (bpp - 1)) == 0);
737 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400738 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700739 currentWidth = SkTMax(1, currentWidth/2);
740 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400741
Greg Daniel55afd6d2017-09-29 09:32:44 -0400742 if (texelsShallowCopy[currentMipLevel].fPixels) {
743 const size_t trimmedSize = currentWidth * bpp * currentHeight;
744 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
745 if (alignmentDiff != 0) {
746 combinedBufferSize += alignmentMask - alignmentDiff + 1;
747 }
748 individualMipOffsets.push_back(combinedBufferSize);
749 combinedBufferSize += trimmedSize;
750 } else {
751 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400752 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400753 }
754 if (0 == combinedBufferSize) {
755 // We don't actually have any data to upload so just return success
756 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700757 }
758
759 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500760 sk_sp<GrVkTransferBuffer> transferBuffer =
761 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400762 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700763 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400764 }
jvanverth900bd4a2016-04-29 13:53:12 -0700765
Greg Daniel475eb702018-09-28 14:16:50 -0400766 int uploadLeft = left;
767 int uploadTop = top;
768 GrVkTexture* uploadTexture = tex;
769 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
770 // R8G8B8A8_UNORM image and then copy it.
771 sk_sp<GrVkTexture> copyTexture;
Greg Danielf259b8b2019-02-14 09:03:43 -0500772 if (dataColorType == GrColorType::kRGB_888x && tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
Greg Daniel475eb702018-09-28 14:16:50 -0400773 GrSurfaceDesc surfDesc;
774 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
775 surfDesc.fWidth = width;
776 surfDesc.fHeight = height;
777 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
778 surfDesc.fSampleCnt = 1;
779
780 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
781 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
782 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
783
784 GrVkImage::ImageDesc imageDesc;
785 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
786 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
787 imageDesc.fWidth = width;
788 imageDesc.fHeight = height;
789 imageDesc.fLevels = 1;
790 imageDesc.fSamples = 1;
791 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
792 imageDesc.fUsageFlags = usageFlags;
793 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
794
795 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
796 GrMipMapsStatus::kNotAllocated);
797 if (!copyTexture) {
798 return false;
799 }
800 uploadTexture = copyTexture.get();
801 uploadLeft = 0;
802 uploadTop = 0;
803 }
804
jvanverth900bd4a2016-04-29 13:53:12 -0700805 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400806 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700807
jvanverthc578b0632016-05-02 10:58:12 -0700808 currentWidth = width;
809 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400810 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400811 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400812 if (texelsShallowCopy[currentMipLevel].fPixels) {
813 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
814 const size_t trimRowBytes = currentWidth * bpp;
815 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
816 ? texelsShallowCopy[currentMipLevel].fRowBytes
817 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700818
Greg Daniel55afd6d2017-09-29 09:32:44 -0400819 // copy data into the buffer, skipping the trailing bytes
820 char* dst = buffer + individualMipOffsets[currentMipLevel];
821 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400822 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400823
824 VkBufferImageCopy& region = regions.push_back();
825 memset(&region, 0, sizeof(VkBufferImageCopy));
826 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
827 region.bufferRowLength = currentWidth;
828 region.bufferImageHeight = currentHeight;
829 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400830 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400831 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700832 }
jvanverthc578b0632016-05-02 10:58:12 -0700833 currentWidth = SkTMax(1, currentWidth/2);
834 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400835 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700836 }
837
jvanverth9d54afc2016-09-20 09:20:03 -0700838 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700839 transferBuffer->unmap();
840
jvanverth900bd4a2016-04-29 13:53:12 -0700841 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400842 uploadTexture->setImageLayout(this,
843 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
844 VK_ACCESS_TRANSFER_WRITE_BIT,
845 VK_PIPELINE_STAGE_TRANSFER_BIT,
846 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700847
848 // Copy the buffer to the image
849 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500850 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400851 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700852 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
853 regions.count(),
854 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400855
856 // If we copied the data into a temporary image first, copy that image into our main texture
857 // now.
858 if (copyTexture.get()) {
859 SkASSERT(dataColorType == GrColorType::kRGB_888x);
860 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
861 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
862 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
863 false));
864 }
Robert Phillips590533f2017-07-11 14:22:35 -0400865 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400866 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400867 }
jvanverth900bd4a2016-04-29 13:53:12 -0700868
Greg Daniel164a9f02016-02-22 09:56:40 -0500869 return true;
870}
871
Jim Van Verth1676cb92019-01-15 13:24:45 -0500872// It's probably possible to roll this into uploadTexDataOptimal,
873// but for now it's easier to maintain as a separate entity.
874bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
875 GrColorType dataColorType, const GrMipLevel texels[],
876 int mipLevelCount) {
877 SkASSERT(!tex->isLinearTiled());
878 // For now the assumption is that our rect is the entire texture.
879 // Compressed textures are read-only so this should be a reasonable assumption.
880 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
881
882 // We assume that if the texture has mip levels, we either upload to all the levels or just the
883 // first.
884 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
885
886 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
887 GrSRGBEncoded::kNo)));
888
889 if (width == 0 || height == 0) {
890 return false;
891 }
892
893 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
894 return false;
895 }
896
897 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
898
899 SkTArray<size_t> individualMipOffsets(mipLevelCount);
900 individualMipOffsets.push_back(0);
901 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
902 int currentWidth = width;
903 int currentHeight = height;
904 if (!texels[0].fPixels) {
905 return false;
906 }
907
908 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
909 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
910 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
911 currentWidth = SkTMax(1, currentWidth / 2);
912 currentHeight = SkTMax(1, currentHeight / 2);
913
914 if (texels[currentMipLevel].fPixels) {
915 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
916 currentHeight);
917 individualMipOffsets.push_back(combinedBufferSize);
918 combinedBufferSize += dataSize;
919 } else {
920 return false;
921 }
922 }
923 if (0 == combinedBufferSize) {
924 // We don't have any data to upload so fail (compressed textures are read-only).
925 return false;
926 }
927
928 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500929 sk_sp<GrVkTransferBuffer> transferBuffer =
930 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500931 if (!transferBuffer) {
932 return false;
933 }
934
935 int uploadLeft = left;
936 int uploadTop = top;
937 GrVkTexture* uploadTexture = tex;
938
939 char* buffer = (char*)transferBuffer->map();
940 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
941
942 currentWidth = width;
943 currentHeight = height;
944 int layerHeight = uploadTexture->height();
945 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
946 if (texels[currentMipLevel].fPixels) {
947 // Again, we're assuming that our rect is the entire texture
948 SkASSERT(currentHeight == layerHeight);
949 SkASSERT(0 == uploadLeft && 0 == uploadTop);
950
951 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
952 currentHeight);
953
954 // copy data into the buffer, skipping the trailing bytes
955 char* dst = buffer + individualMipOffsets[currentMipLevel];
956 const char* src = (const char*)texels[currentMipLevel].fPixels;
957 memcpy(dst, src, dataSize);
958
959 VkBufferImageCopy& region = regions.push_back();
960 memset(&region, 0, sizeof(VkBufferImageCopy));
961 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
962 region.bufferRowLength = currentWidth;
963 region.bufferImageHeight = currentHeight;
964 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
965 region.imageOffset = { uploadLeft, uploadTop, 0 };
966 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
967 }
968 currentWidth = SkTMax(1, currentWidth / 2);
969 currentHeight = SkTMax(1, currentHeight / 2);
970 layerHeight = currentHeight;
971 }
972
973 // no need to flush non-coherent memory, unmap will do that for us
974 transferBuffer->unmap();
975
976 // Change layout of our target so it can be copied to
977 uploadTexture->setImageLayout(this,
978 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
979 VK_ACCESS_TRANSFER_WRITE_BIT,
980 VK_PIPELINE_STAGE_TRANSFER_BIT,
981 false);
982
983 // Copy the buffer to the image
984 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500985 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500986 uploadTexture,
987 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
988 regions.count(),
989 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500990
991 if (1 == mipLevelCount) {
992 tex->texturePriv().markMipMapsDirty();
993 }
994
995 return true;
996}
997
Greg Daniel164a9f02016-02-22 09:56:40 -0500998////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400999sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -05001000 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001001 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
1002
1003 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001004 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -07001005
Greg Daniel164a9f02016-02-22 09:56:40 -05001006 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
1007 if (renderTarget) {
1008 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1009 }
1010
1011 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
1012 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
1013 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -07001014 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -05001015 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
1016 // texture.
1017 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
1018
Greg Daniel164a9f02016-02-22 09:56:40 -05001019 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -07001020 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -05001021 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -04001022 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -05001023 GrVkImage::ImageDesc imageDesc;
1024 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
1025 imageDesc.fFormat = pixelFormat;
1026 imageDesc.fWidth = desc.fWidth;
1027 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001028 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -05001029 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001030 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -05001031 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -04001032 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -05001033
Greg Daniel0fc4d2d2017-10-12 11:23:36 -04001034 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
1035 if (mipLevels > 1) {
1036 mipMapsStatus = GrMipMapsStatus::kValid;
1037 for (int i = 0; i < mipLevels; ++i) {
1038 if (!texels[i].fPixels) {
1039 mipMapsStatus = GrMipMapsStatus::kDirty;
1040 break;
1041 }
Greg Daniel834f1202017-10-09 15:06:20 -04001042 }
1043 }
1044
Robert Phillips67d52cf2017-06-05 13:38:13 -04001045 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -05001046 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -04001047 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
1048 imageDesc,
1049 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001050 } else {
Greg Daniel475eb702018-09-28 14:16:50 -04001051 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -05001052 }
1053
1054 if (!tex) {
1055 return nullptr;
1056 }
1057
Jim Van Verth1676cb92019-01-15 13:24:45 -05001058 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -05001059 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -04001060 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -05001061 bool success;
1062 if (isCompressed) {
1063 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1064 colorType, texels, mipLevelCount);
1065 } else {
1066 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
1067 colorType, texels, mipLevelCount);
1068 }
1069 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001070 tex->unref();
1071 return nullptr;
1072 }
1073 }
1074
Jim Van Verth1676cb92019-01-15 13:24:45 -05001075 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -04001076 VkClearColorValue zeroClearColor;
1077 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
1078 VkImageSubresourceRange range;
1079 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1080 range.baseArrayLayer = 0;
1081 range.baseMipLevel = 0;
1082 range.layerCount = 1;
1083 range.levelCount = 1;
1084 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1085 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -04001086 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -04001087 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001088 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001089}
1090
1091////////////////////////////////////////////////////////////////////////////////
1092
Greg Daniel6888c0d2017-08-25 11:55:50 -04001093void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1094 VkDeviceSize dstOffset, VkDeviceSize size) {
1095 VkBufferCopy copyRegion;
1096 copyRegion.srcOffset = srcOffset;
1097 copyRegion.dstOffset = dstOffset;
1098 copyRegion.size = size;
1099 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1100}
1101
jvanverthdb379092016-07-07 11:18:46 -07001102bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1103 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001104 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001105 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001106
1107 return true;
1108}
1109
1110////////////////////////////////////////////////////////////////////////////////
1111
Greg Daniel7e000222018-12-03 10:08:21 -05001112static bool check_image_info(const GrVkCaps& caps,
1113 const GrVkImageInfo& info,
Greg Danielcb324152019-02-25 11:36:53 -05001114 GrPixelConfig config,
1115 bool isWrappedRT) {
1116 if (VK_NULL_HANDLE == info.fImage) {
1117 return false;
1118 }
1119
1120 if (VK_NULL_HANDLE == info.fAlloc.fMemory && !isWrappedRT) {
Brian Salomond17f6582017-07-19 18:28:58 -04001121 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001122 }
1123
Greg Daniel7e000222018-12-03 10:08:21 -05001124 if (info.fYcbcrConversionInfo.isValid()) {
1125 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1126 return false;
1127 }
jvanverthfd359ca2016-03-18 11:57:24 -07001128 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001129
Greg Danielcb324152019-02-25 11:36:53 -05001130 if (info.fImageLayout == VK_IMAGE_LAYOUT_PRESENT_SRC_KHR && !caps.supportsSwapchain()) {
1131 return false;
1132 }
1133
Greg Daniel52e16d92018-04-10 09:34:07 -04001134 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001135 return true;
1136}
1137
1138sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001139 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1140 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001141 GrVkImageInfo imageInfo;
1142 if (!backendTex.getVkImageInfo(&imageInfo)) {
1143 return nullptr;
1144 }
1145
Greg Danielcb324152019-02-25 11:36:53 -05001146 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001147 return nullptr;
1148 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001149
Greg Daniel164a9f02016-02-22 09:56:40 -05001150 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001151 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001152 surfDesc.fWidth = backendTex.width();
1153 surfDesc.fHeight = backendTex.height();
1154 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001155 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001156
Greg Daniel52e16d92018-04-10 09:34:07 -04001157 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1158 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001159 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1160 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001161}
1162
1163sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001164 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001165 GrWrapOwnership ownership,
1166 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001167 GrVkImageInfo imageInfo;
1168 if (!backendTex.getVkImageInfo(&imageInfo)) {
1169 return nullptr;
1170 }
1171
Greg Danielcb324152019-02-25 11:36:53 -05001172 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config(), false)) {
Brian Salomond17f6582017-07-19 18:28:58 -04001173 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001174 }
Brian Salomond17f6582017-07-19 18:28:58 -04001175
1176 GrSurfaceDesc surfDesc;
1177 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1178 surfDesc.fWidth = backendTex.width();
1179 surfDesc.fHeight = backendTex.height();
1180 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001181 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001182
Greg Daniel52e16d92018-04-10 09:34:07 -04001183 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1184 SkASSERT(layout);
1185
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001186 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1187 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001188}
1189
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001190sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001191 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1192 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1193 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1194 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001195 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001196 return nullptr;
1197 }
halcanary9d524f22016-03-29 09:03:52 -07001198
Greg Daniel323fbcf2018-04-10 13:46:30 -04001199 GrVkImageInfo info;
1200 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001201 return nullptr;
1202 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001203
Greg Danielcb324152019-02-25 11:36:53 -05001204 if (!check_image_info(this->vkCaps(), info, backendRT.config(), true)) {
jvanverthfd359ca2016-03-18 11:57:24 -07001205 return nullptr;
1206 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001207
Greg Daniel164a9f02016-02-22 09:56:40 -05001208 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001209 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001210 desc.fWidth = backendRT.width();
1211 desc.fHeight = backendRT.height();
1212 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001213 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001214
Greg Daniel323fbcf2018-04-10 13:46:30 -04001215 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001216
Greg Daniel323fbcf2018-04-10 13:46:30 -04001217 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001218 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001219
1220 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1221 SkASSERT(!backendRT.stencilBits());
1222 if (tgt) {
1223 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001224 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001225
Ben Wagnerff134f22018-04-24 16:29:16 -04001226 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001227}
1228
Greg Daniel7ef28f32017-04-20 16:41:55 +00001229sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001230 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001231
Greg Daniel52e16d92018-04-10 09:34:07 -04001232 GrVkImageInfo imageInfo;
1233 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001234 return nullptr;
1235 }
Greg Danielcb324152019-02-25 11:36:53 -05001236 if (!check_image_info(this->vkCaps(), imageInfo, tex.config(), false)) {
Brian Osman33910292017-04-18 14:38:53 -04001237 return nullptr;
1238 }
1239
Greg Danielcb324152019-02-25 11:36:53 -05001240
Brian Osman33910292017-04-18 14:38:53 -04001241 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001242 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001243 desc.fWidth = tex.width();
1244 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001245 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001246 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1247 if (!desc.fSampleCnt) {
1248 return nullptr;
1249 }
Brian Osman33910292017-04-18 14:38:53 -04001250
Greg Daniel52e16d92018-04-10 09:34:07 -04001251 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1252 SkASSERT(layout);
1253
Ben Wagnerff134f22018-04-24 16:29:16 -04001254 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001255}
1256
Greg Danielb46add82019-01-02 14:51:29 -05001257sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1258 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1259 int maxSize = this->caps()->maxTextureSize();
1260 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1261 return nullptr;
1262 }
1263
1264 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1265 if (!backendFormat.isValid()) {
1266 return nullptr;
1267 }
1268 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1269 imageInfo.colorType());
1270 if (config == kUnknown_GrPixelConfig) {
1271 return nullptr;
1272 }
1273
1274 GrSurfaceDesc desc;
1275 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1276 desc.fWidth = imageInfo.width();
1277 desc.fHeight = imageInfo.height();
1278 desc.fConfig = config;
1279 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1280 if (!desc.fSampleCnt) {
1281 return nullptr;
1282 }
1283
1284 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1285}
1286
Brian Salomon930f9392018-06-20 16:25:26 -04001287bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1288 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001289 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001290 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001291 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001292 return false;
jvanverth62340062016-04-26 08:01:44 -07001293 }
1294
jvanverth62340062016-04-26 08:01:44 -07001295 // determine if we can blit to and from this format
1296 const GrVkCaps& caps = this->vkCaps();
1297 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001298 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1299 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001300 return false;
jvanverth62340062016-04-26 08:01:44 -07001301 }
1302
egdaniel7ac5da82016-07-15 13:41:42 -07001303 int width = tex->width();
1304 int height = tex->height();
1305 VkImageBlit blitRegion;
1306 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001307
jvanverth82c05582016-05-03 11:19:01 -07001308 // SkMipMap doesn't include the base level in the level count so we have to add 1
1309 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001310 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001311
Greg Danielda86e282018-06-13 09:41:19 -04001312 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001313 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1314 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001315
jvanverth50c46c72016-05-06 12:31:28 -07001316 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001317 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001318 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1319 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001320 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1321 nullptr, // pNext
1322 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1323 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1324 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1325 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1326 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1327 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1328 vkTex->image(), // image
1329 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001330 };
1331
jvanverth62340062016-04-26 08:01:44 -07001332 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001333 uint32_t mipLevel = 1;
1334 while (mipLevel < levelCount) {
1335 int prevWidth = width;
1336 int prevHeight = height;
1337 width = SkTMax(1, width / 2);
1338 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001339
jvanverth50c46c72016-05-06 12:31:28 -07001340 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
Greg Daniel59dc1482019-02-22 10:46:38 -05001341 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1342 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
jvanverth50c46c72016-05-06 12:31:28 -07001343
1344 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001345 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001346 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001347 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1348 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001349 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001350 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001351 vkTex->resource(),
1352 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001353 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001354 vkTex->resource(),
1355 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001356 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001357 1,
1358 &blitRegion,
1359 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001360 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001361 }
Greg Danielee54f232019-04-03 14:58:40 -04001362 if (levelCount > 1) {
1363 // This barrier logically is not needed, but it changes the final level to the same layout
1364 // as all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the
1365 // layouts and future layout changes easier. The alternative here would be to track layout
1366 // and memory accesses per layer which doesn't seem work it.
1367 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1368 this->addImageMemoryBarrier(vkTex->resource(), VK_PIPELINE_STAGE_TRANSFER_BIT,
1369 VK_PIPELINE_STAGE_TRANSFER_BIT, false, &imageMemoryBarrier);
1370 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1371 }
Brian Salomon930f9392018-06-20 16:25:26 -04001372 return true;
jvanverth62340062016-04-26 08:01:44 -07001373}
1374
Greg Daniel164a9f02016-02-22 09:56:40 -05001375////////////////////////////////////////////////////////////////////////////////
1376
1377GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1378 int width,
1379 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001380 SkASSERT(width >= rt->width());
1381 SkASSERT(height >= rt->height());
1382
1383 int samples = rt->numStencilSamples();
1384
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001385 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001386
1387 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001388 width,
1389 height,
1390 samples,
1391 sFmt));
1392 fStats.incStencilAttachmentCreates();
1393 return stencil;
1394}
1395
1396////////////////////////////////////////////////////////////////////////////////
1397
Brian Salomon52e943a2018-03-13 09:32:39 -04001398bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001399 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1400 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001401 VkDeviceSize size = dstRowBytes * h;
1402 VkDeviceSize offset = bufferOffset;
1403 SkASSERT(size + offset <= alloc.fSize);
1404 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1405 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001406 return false;
1407 }
Greg Daniel81df0412018-05-31 13:13:33 -04001408 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001409
Greg Daniel20ece3a2017-03-28 10:24:43 -04001410 if (srcData) {
1411 // If there is no padding on dst we can do a single memcopy.
1412 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001413 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001414 } else {
1415 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1416 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001417 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001418 }
Greg Daniel81df0412018-05-31 13:13:33 -04001419 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1420 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001421 return true;
1422}
1423
Brian Salomonf865b052018-03-09 09:01:53 -05001424#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001425bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1426 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001427 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001428 SkASSERT(texturable || renderable);
1429 if (!texturable) {
1430 SkASSERT(GrMipMapped::kNo == mipMapped);
1431 SkASSERT(!srcData);
1432 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001433 VkFormat pixelFormat;
1434 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001435 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001436 }
1437
Brian Salomon52e943a2018-03-13 09:32:39 -04001438 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1439 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001440 }
1441
Brian Salomon52e943a2018-03-13 09:32:39 -04001442 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1443 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001444 }
1445
1446 // Currently we don't support uploading pixel data when mipped.
1447 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001448 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001449 }
1450
Brian Salomon52e943a2018-03-13 09:32:39 -04001451 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001452 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1453 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001454 if (texturable) {
1455 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1456 }
1457 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001458 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1459 }
1460
1461 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001462 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001463 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001464
1465 // Create Image
1466 VkSampleCountFlagBits vkSamples;
1467 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001468 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001469 }
1470
1471 // Figure out the number of mip levels.
1472 uint32_t mipLevels = 1;
1473 if (GrMipMapped::kYes == mipMapped) {
1474 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1475 }
1476
1477 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001478 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1479 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001480 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001481 VK_IMAGE_TYPE_2D, // VkImageType
1482 pixelFormat, // VkFormat
1483 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1484 mipLevels, // mipLevels
1485 1, // arrayLayers
1486 vkSamples, // samples
1487 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1488 usageFlags, // VkImageUsageFlags
1489 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1490 0, // queueFamilyCount
1491 0, // pQueueFamilyIndices
1492 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001493 };
1494
Brian Salomon52e943a2018-03-13 09:32:39 -04001495 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1496 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001497
Brian Salomonde9f5462018-03-07 14:23:58 -05001498 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001499 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001500 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001501 }
1502
1503 // We need to declare these early so that we can delete them at the end outside of the if block.
Greg Daniel8385a8a2018-02-26 13:29:37 -05001504 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001505 VkBuffer buffer = VK_NULL_HANDLE;
1506
1507 VkResult err;
1508 const VkCommandBufferAllocateInfo cmdInfo = {
1509 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1510 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001511 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001512 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1513 1 // bufferCount
1514 };
1515
1516 VkCommandBuffer cmdBuffer;
1517 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1518 if (err) {
1519 GrVkMemory::FreeImageMemory(this, false, alloc);
1520 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001521 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001522 }
1523
1524 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1525 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1526 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1527 cmdBufferBeginInfo.pNext = nullptr;
1528 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1529 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1530
1531 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1532 SkASSERT(!err);
1533
1534 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001535 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001536
Robert Phillips646f6372018-09-25 09:31:10 -04001537 const size_t trimRowBytes = w * bpp;
1538 if (!srcRowBytes) {
1539 srcRowBytes = trimRowBytes;
1540 }
1541
Brian Salomonde9f5462018-03-07 14:23:58 -05001542 SkTArray<size_t> individualMipOffsets(mipLevels);
1543 individualMipOffsets.push_back(0);
1544 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001545 if (GrPixelConfigIsCompressed(config)) {
1546 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1547 bpp = 4; // we have at least this alignment, which will pass the code below
1548 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001549 int currentWidth = w;
1550 int currentHeight = h;
1551 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1552 // config. This works with the assumption that the bytes in pixel config is always a power
1553 // of 2.
1554 SkASSERT((bpp & (bpp - 1)) == 0);
1555 const size_t alignmentMask = 0x3 | (bpp - 1);
1556 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1557 currentWidth = SkTMax(1, currentWidth / 2);
1558 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001559
Jim Van Verth1676cb92019-01-15 13:24:45 -05001560 size_t trimmedSize;
1561 if (GrPixelConfigIsCompressed(config)) {
1562 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1563 } else {
1564 trimmedSize = currentWidth * bpp * currentHeight;
1565 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001566 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1567 if (alignmentDiff != 0) {
1568 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001569 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001570 individualMipOffsets.push_back(combinedBufferSize);
1571 combinedBufferSize += trimmedSize;
1572 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001573
Brian Salomonde9f5462018-03-07 14:23:58 -05001574 VkBufferCreateInfo bufInfo;
1575 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1576 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1577 bufInfo.flags = 0;
1578 bufInfo.size = combinedBufferSize;
1579 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1580 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1581 bufInfo.queueFamilyIndexCount = 0;
1582 bufInfo.pQueueFamilyIndices = nullptr;
1583 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001584
Brian Salomonde9f5462018-03-07 14:23:58 -05001585 if (err) {
1586 GrVkMemory::FreeImageMemory(this, false, alloc);
1587 VK_CALL(DestroyImage(fDevice, image, nullptr));
1588 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001589 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001590 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001591 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001592
Brian Salomonde9f5462018-03-07 14:23:58 -05001593 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1594 &bufferAlloc)) {
1595 GrVkMemory::FreeImageMemory(this, false, alloc);
1596 VK_CALL(DestroyImage(fDevice, image, nullptr));
1597 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1598 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001599 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001600 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001601 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001602
Brian Salomonde9f5462018-03-07 14:23:58 -05001603 currentWidth = w;
1604 currentHeight = h;
1605 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1606 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001607 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001608 bool result;
1609 if (GrPixelConfigIsCompressed(config)) {
1610 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1611 size_t currentRowBytes = levelSize / currentHeight;
1612 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1613 currentRowBytes, currentRowBytes, currentHeight);
1614 } else {
1615 size_t currentRowBytes = bpp * currentWidth;
1616 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1617 currentRowBytes, trimRowBytes, currentHeight);
1618 }
1619 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001620 GrVkMemory::FreeImageMemory(this, false, alloc);
1621 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001622 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001623 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1624 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001625 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001626 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001627 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001628 currentWidth = SkTMax(1, currentWidth / 2);
1629 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001630 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001631
1632 // Set image layout and add barrier
1633 VkImageMemoryBarrier barrier;
1634 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1635 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1636 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001637 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001638 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1639 barrier.oldLayout = initialLayout;
1640 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1641 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1642 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1643 barrier.image = image;
1644 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1645
Greg Danielf7828d02018-10-09 12:01:32 -04001646 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001647 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1648 &barrier));
1649 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1650
1651 SkTArray<VkBufferImageCopy> regions(mipLevels);
1652
1653 currentWidth = w;
1654 currentHeight = h;
1655 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1656 // Submit copy command
1657 VkBufferImageCopy& region = regions.push_back();
1658 memset(&region, 0, sizeof(VkBufferImageCopy));
1659 region.bufferOffset = individualMipOffsets[currentMipLevel];
1660 region.bufferRowLength = currentWidth;
1661 region.bufferImageHeight = currentHeight;
1662 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1663 region.imageOffset = {0, 0, 0};
1664 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1665 currentWidth = SkTMax(1, currentWidth / 2);
1666 currentHeight = SkTMax(1, currentHeight / 2);
1667 }
1668
1669 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1670 regions.begin()));
1671
Brian Salomon52e943a2018-03-13 09:32:39 -04001672 if (texturable) {
1673 // Change Image layout to shader read since if we use this texture as a borrowed textures
1674 // within Ganesh we require that its layout be set to that
1675 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1676 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1677 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001678 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001679 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1680 barrier.oldLayout = initialLayout;
1681 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1682 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1683 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1684 barrier.image = image;
1685 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001686 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001687 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1688 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001689 0,
1690 0, nullptr,
1691 0, nullptr,
1692 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001693 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001694 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001695
1696 // End CommandBuffer
1697 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1698 SkASSERT(!err);
1699
1700 // Create Fence for queue
1701 VkFence fence;
1702 VkFenceCreateInfo fenceInfo;
1703 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1704 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1705
1706 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1707 SkASSERT(!err);
1708
1709 VkSubmitInfo submitInfo;
1710 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1711 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1712 submitInfo.pNext = nullptr;
1713 submitInfo.waitSemaphoreCount = 0;
1714 submitInfo.pWaitSemaphores = nullptr;
1715 submitInfo.pWaitDstStageMask = 0;
1716 submitInfo.commandBufferCount = 1;
1717 submitInfo.pCommandBuffers = &cmdBuffer;
1718 submitInfo.signalSemaphoreCount = 0;
1719 submitInfo.pSignalSemaphores = nullptr;
1720 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1721 SkASSERT(!err);
1722
1723 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1724 if (VK_TIMEOUT == err) {
1725 GrVkMemory::FreeImageMemory(this, false, alloc);
1726 VK_CALL(DestroyImage(fDevice, image, nullptr));
1727 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1728 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001729 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001730 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1731 SkDebugf("Fence failed to signal: %d\n", err);
1732 SK_ABORT("failing");
1733 }
1734 SkASSERT(!err);
1735
1736 // Clean up transfer resources
1737 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1738 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1739 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1740 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001741 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001742 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1743
Brian Salomon52e943a2018-03-13 09:32:39 -04001744 info->fImage = image;
1745 info->fAlloc = alloc;
1746 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1747 info->fImageLayout = initialLayout;
1748 info->fFormat = pixelFormat;
1749 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001750
Brian Salomon52e943a2018-03-13 09:32:39 -04001751 return true;
1752}
1753
1754GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001755 GrColorType colorType,
1756 bool isRenderTarget,
1757 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001758 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001759
1760 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1761 return GrBackendTexture();
1762 }
1763
Robert Phillips646f6372018-09-25 09:31:10 -04001764 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1765 if (!this->caps()->isConfigTexturable(config)) {
1766 return GrBackendTexture();
1767 }
1768
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001769 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001770 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001771 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001772 return {};
1773 }
Greg Daniel108bb232018-07-03 16:18:29 -04001774 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1775 // Lots of tests don't go through Skia's public interface which will set the config so for
1776 // testing we make sure we set a config here.
1777 beTex.setPixelConfig(config);
1778 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001779}
1780
1781bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001782 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001783
Greg Daniel52e16d92018-04-10 09:34:07 -04001784 GrVkImageInfo backend;
1785 if (!tex.getVkImageInfo(&backend)) {
1786 return false;
1787 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001788
Greg Daniel52e16d92018-04-10 09:34:07 -04001789 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001790 VkMemoryRequirements req;
1791 memset(&req, 0, sizeof(req));
1792 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001793 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001794 &req));
1795 // TODO: find a better check
1796 // This will probably fail with a different driver
1797 return (req.size > 0) && (req.size <= 8192 * 8192);
1798 }
1799
1800 return false;
1801}
1802
Brian Salomon26102cb2018-03-09 09:33:19 -05001803void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001804 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001805
Greg Daniel52e16d92018-04-10 09:34:07 -04001806 GrVkImageInfo info;
1807 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001808 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001809 }
1810}
1811
Brian Osman2d010b62018-08-09 10:55:09 -04001812GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001813 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1814 return GrBackendRenderTarget();
1815 }
1816
Brian Salomon8a375832018-03-14 10:21:40 -04001817 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001818 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001819 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001820 if (kUnknown_GrPixelConfig == config) {
1821 return {};
1822 }
Robert Phillips646f6372018-09-25 09:31:10 -04001823 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001824 &info)) {
1825 return {};
1826 }
Greg Daniel108bb232018-07-03 16:18:29 -04001827 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1828 // Lots of tests don't go through Skia's public interface which will set the config so for
1829 // testing we make sure we set a config here.
1830 beRT.setPixelConfig(config);
1831 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001832}
1833
Brian Salomon52e943a2018-03-13 09:32:39 -04001834void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001835 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001836
Greg Daniel323fbcf2018-04-10 13:46:30 -04001837 GrVkImageInfo info;
1838 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001839 // something in the command buffer may still be using this, so force submit
1840 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001841 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001842 }
1843}
Brian Salomonf865b052018-03-09 09:01:53 -05001844
Greg Daniel26b50a42018-03-08 09:49:58 -05001845void GrVkGpu::testingOnly_flushGpuAndSync() {
1846 this->submitCommandBuffer(kForce_SyncQueue);
1847}
Brian Salomonf865b052018-03-09 09:01:53 -05001848#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001849
Greg Daniel164a9f02016-02-22 09:56:40 -05001850////////////////////////////////////////////////////////////////////////////////
1851
Greg Daniel59dc1482019-02-22 10:46:38 -05001852void GrVkGpu::addBufferMemoryBarrier(const GrVkResource* resource,
1853 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001854 VkPipelineStageFlags dstStageMask,
1855 bool byRegion,
1856 VkBufferMemoryBarrier* barrier) const {
1857 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001858 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001859 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001860 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001861 srcStageMask,
1862 dstStageMask,
1863 byRegion,
1864 GrVkCommandBuffer::kBufferMemory_BarrierType,
1865 barrier);
1866}
1867
Greg Daniel59dc1482019-02-22 10:46:38 -05001868void GrVkGpu::addImageMemoryBarrier(const GrVkResource* resource,
1869 VkPipelineStageFlags srcStageMask,
Greg Daniel164a9f02016-02-22 09:56:40 -05001870 VkPipelineStageFlags dstStageMask,
1871 bool byRegion,
1872 VkImageMemoryBarrier* barrier) const {
1873 SkASSERT(fCurrentCmdBuffer);
Greg Daniel59dc1482019-02-22 10:46:38 -05001874 SkASSERT(resource);
Greg Daniel164a9f02016-02-22 09:56:40 -05001875 fCurrentCmdBuffer->pipelineBarrier(this,
Greg Daniel59dc1482019-02-22 10:46:38 -05001876 resource,
Greg Daniel164a9f02016-02-22 09:56:40 -05001877 srcStageMask,
1878 dstStageMask,
1879 byRegion,
1880 GrVkCommandBuffer::kImageMemory_BarrierType,
1881 barrier);
1882}
1883
Greg Danielbae71212019-03-01 15:24:35 -05001884void GrVkGpu::onFinishFlush(GrSurfaceProxy* proxy, SkSurface::BackendSurfaceAccess access,
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001885 const GrFlushInfo& info) {
Greg Daniel51316782017-08-02 15:10:09 +00001886 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1887 // not effect what we do here.
Greg Danielbae71212019-03-01 15:24:35 -05001888 if (proxy && access == SkSurface::BackendSurfaceAccess::kPresent) {
1889 GrVkImage* image;
1890 SkASSERT(proxy->isInstantiated());
1891 if (GrTexture* tex = proxy->peekTexture()) {
1892 image = static_cast<GrVkTexture*>(tex);
1893 } else {
1894 GrRenderTarget* rt = proxy->peekRenderTarget();
1895 SkASSERT(rt);
1896 image = static_cast<GrVkRenderTarget*>(rt);
1897 }
1898 image->prepareForPresent(this);
1899 }
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001900 if (info.fFlags & kSyncCpu_GrFlushFlag) {
1901 this->submitCommandBuffer(kForce_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001902 } else {
Greg Daniele6bfb7d2019-04-17 15:26:11 -04001903 this->submitCommandBuffer(kSkip_SyncQueue, info.fFinishedProc, info.fFinishedContext);
Greg Danielbae71212019-03-01 15:24:35 -05001904 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001905}
1906
Greg Daniel25af6712018-04-25 10:44:38 -04001907static int get_surface_sample_cnt(GrSurface* surf) {
1908 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1909 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001910 }
Greg Daniel25af6712018-04-25 10:44:38 -04001911 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001912}
1913
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001914void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1915 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001916 GrVkImage* dstImage,
1917 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001918 const SkIRect& srcRect,
1919 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001920#ifdef SK_DEBUG
1921 int dstSampleCnt = get_surface_sample_cnt(dst);
1922 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001923 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1924 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
1925 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin, dstHasYcbcr,
1926 src->config(), srcSampleCnt, srcOrigin, srcHasYcbcr));
Greg Daniel25af6712018-04-25 10:44:38 -04001927
1928#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001929
Greg Daniel164a9f02016-02-22 09:56:40 -05001930 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1931 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001932 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001933 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1934 VK_ACCESS_TRANSFER_WRITE_BIT,
1935 VK_PIPELINE_STAGE_TRANSFER_BIT,
1936 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001937
egdaniel17b89252016-04-05 07:23:38 -07001938 srcImage->setImageLayout(this,
1939 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001940 VK_ACCESS_TRANSFER_READ_BIT,
1941 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001942 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001943
1944 // Flip rect if necessary
1945 SkIRect srcVkRect = srcRect;
1946 int32_t dstY = dstPoint.fY;
1947
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001948 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1949 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001950 srcVkRect.fTop = src->height() - srcRect.fBottom;
1951 srcVkRect.fBottom = src->height() - srcRect.fTop;
1952 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1953 }
1954
1955 VkImageCopy copyRegion;
1956 memset(&copyRegion, 0, sizeof(VkImageCopy));
1957 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1958 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1959 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1960 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001961 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001962
1963 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001964 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001965 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001966 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001967 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1968 1,
1969 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001970
1971 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1972 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001973 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001974}
1975
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001976void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1977 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001978 GrVkImage* dstImage,
1979 GrVkImage* srcImage,
1980 const SkIRect& srcRect,
1981 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001982#ifdef SK_DEBUG
1983 int dstSampleCnt = get_surface_sample_cnt(dst);
1984 int srcSampleCnt = get_surface_sample_cnt(src);
Greg Daniela51e93c2019-03-25 12:30:45 -04001985 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
1986 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
Greg Daniel25af6712018-04-25 10:44:38 -04001987 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04001988 dstHasYcbcr, src->config(), srcSampleCnt,
1989 srcImage->isLinearTiled(), srcHasYcbcr));
egdaniel17b89252016-04-05 07:23:38 -07001990
Greg Daniel25af6712018-04-25 10:44:38 -04001991#endif
egdaniel17b89252016-04-05 07:23:38 -07001992 dstImage->setImageLayout(this,
1993 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001994 VK_ACCESS_TRANSFER_WRITE_BIT,
1995 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001996 false);
1997
egdaniel17b89252016-04-05 07:23:38 -07001998 srcImage->setImageLayout(this,
1999 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07002000 VK_ACCESS_TRANSFER_READ_BIT,
2001 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07002002 false);
2003
2004 // Flip rect if necessary
2005 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07002006 srcVkRect.fLeft = srcRect.fLeft;
2007 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07002008 SkIRect dstRect;
2009 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07002010 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07002011
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002012 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002013 srcVkRect.fTop = src->height() - srcRect.fBottom;
2014 srcVkRect.fBottom = src->height() - srcRect.fTop;
2015 } else {
egdaniel8af936d2016-04-07 10:17:47 -07002016 srcVkRect.fTop = srcRect.fTop;
2017 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07002018 }
2019
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002020 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07002021 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
2022 } else {
2023 dstRect.fTop = dstPoint.fY;
2024 }
2025 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
2026
2027 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
2028 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002029 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04002030 using std::swap;
2031 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07002032 }
2033
2034 VkImageBlit blitRegion;
2035 memset(&blitRegion, 0, sizeof(VkImageBlit));
2036 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2037 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002038 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002039 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
2040 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04002041 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07002042
2043 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07002044 *srcImage,
2045 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07002046 1,
2047 &blitRegion,
2048 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07002049
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002050 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05002051 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07002052}
2053
Brian Salomon1fabd512018-02-09 09:54:25 -05002054void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
2055 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
2056 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07002057 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05002058 SkIRect srcRect = origSrcRect;
2059 SkIPoint dstPoint = origDstPoint;
2060 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
2061 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
2062 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
2063 origSrcRect.fRight, src->height() - origSrcRect.fTop};
2064 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
2065 }
2066 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04002067 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
2068 srcRect.width(), srcRect.height());
2069 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07002070}
2071
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002072bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
2073 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04002074 const SkIRect& srcRect, const SkIPoint& dstPoint,
2075 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002076#ifdef SK_DEBUG
2077 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
2078 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
2079 }
2080 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
2081 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
2082 }
2083#endif
2084
Greg Daniel25af6712018-04-25 10:44:38 -04002085 GrPixelConfig dstConfig = dst->config();
2086 GrPixelConfig srcConfig = src->config();
2087
2088 int dstSampleCnt = get_surface_sample_cnt(dst);
2089 int srcSampleCnt = get_surface_sample_cnt(src);
2090
egdaniel17b89252016-04-05 07:23:38 -07002091 GrVkImage* dstImage;
2092 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07002093 GrRenderTarget* dstRT = dst->asRenderTarget();
2094 if (dstRT) {
2095 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05002096 if (vkRT->wrapsSecondaryCommandBuffer()) {
2097 return false;
2098 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002099 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
2100 } else {
2101 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002102 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002103 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002104 GrRenderTarget* srcRT = src->asRenderTarget();
2105 if (srcRT) {
2106 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2107 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002108 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002109 SkASSERT(src->asTexture());
2110 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002111 }
2112
Greg Daniela51e93c2019-03-25 12:30:45 -04002113 bool dstHasYcbcr = dstImage->ycbcrConversionInfo().isValid();
2114 bool srcHasYcbcr = srcImage->ycbcrConversionInfo().isValid();
2115
2116 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2117 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
2118 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
2119 return true;
2120 }
2121
2122 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()), dstHasYcbcr,
2123 srcConfig, SkToBool(src->asTexture()), srcHasYcbcr)) {
2124 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
2125 dstPoint, canDiscardOutsideDstRect));
2126 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
2127 this->didWriteToSurface(dst, dstOrigin, &dstRect);
2128 return true;
2129 }
2130
2131 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin, dstHasYcbcr,
2132 srcConfig, srcSampleCnt, srcOrigin, srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002133 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2134 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002135 return true;
2136 }
2137
Greg Daniel25af6712018-04-25 10:44:38 -04002138 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
Greg Daniela51e93c2019-03-25 12:30:45 -04002139 dstHasYcbcr, srcConfig, srcSampleCnt,
2140 srcImage->isLinearTiled(), srcHasYcbcr)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002141 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2142 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002143 return true;
2144 }
2145
Greg Daniel164a9f02016-02-22 09:56:40 -05002146 return false;
2147}
2148
Brian Salomona6948702018-06-01 15:33:20 -04002149bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2150 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002151 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002152 return false;
2153 }
2154
egdaniel66933552016-08-24 07:22:19 -07002155 GrVkImage* image = nullptr;
2156 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2157 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002158 // Reading from render targets that wrap a secondary command buffer is not allowed since
2159 // it would require us to know the VkImage, which we don't have, as well as need us to
2160 // stop and start the VkRenderPass which we don't have access to.
2161 if (rt->wrapsSecondaryCommandBuffer()) {
2162 return false;
2163 }
egdaniel66933552016-08-24 07:22:19 -07002164 // resolve the render target if necessary
2165 switch (rt->getResolveType()) {
2166 case GrVkRenderTarget::kCantResolve_ResolveType:
2167 return false;
2168 case GrVkRenderTarget::kAutoResolves_ResolveType:
2169 break;
2170 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002171 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002172 break;
2173 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002174 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002175 }
2176 image = rt;
2177 } else {
2178 image = static_cast<GrVkTexture*>(surface->asTexture());
2179 }
2180
2181 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002182 return false;
2183 }
2184
Greg Daniel475eb702018-09-28 14:16:50 -04002185 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2186 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2187 // image and then do the read pixels from that.
2188 sk_sp<GrVkTextureRenderTarget> copySurface;
Greg Danielf259b8b2019-02-14 09:03:43 -05002189 if (dstColorType == GrColorType::kRGB_888x && image->imageFormat() == VK_FORMAT_R8G8B8_UNORM) {
2190 SkASSERT(surface->config() == kRGB_888_GrPixelConfig);
Greg Daniel475eb702018-09-28 14:16:50 -04002191
2192 // Make a new surface that is RGBA to copy the RGB surface into.
2193 GrSurfaceDesc surfDesc;
2194 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2195 surfDesc.fWidth = width;
2196 surfDesc.fHeight = height;
2197 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2198 surfDesc.fSampleCnt = 1;
2199
2200 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2201 VK_IMAGE_USAGE_SAMPLED_BIT |
2202 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2203 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2204
2205 GrVkImage::ImageDesc imageDesc;
2206 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2207 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2208 imageDesc.fWidth = width;
2209 imageDesc.fHeight = height;
2210 imageDesc.fLevels = 1;
2211 imageDesc.fSamples = 1;
2212 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2213 imageDesc.fUsageFlags = usageFlags;
2214 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2215
2216 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2217 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2218 if (!copySurface) {
2219 return false;
2220 }
2221
2222 int srcSampleCount = 0;
2223 if (rt) {
2224 srcSampleCount = rt->numColorSamples();
2225 }
Greg Daniela51e93c2019-03-25 12:30:45 -04002226 bool srcHasYcbcr = image->ycbcrConversionInfo().isValid();
Greg Daniel475eb702018-09-28 14:16:50 -04002227 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
Greg Daniela51e93c2019-03-25 12:30:45 -04002228 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin, false,
2229 surface->config(), srcSampleCount, kOrigin,
2230 srcHasYcbcr) &&
2231 !this->vkCaps().canCopyAsDraw(copySurface->config(), false, false,
2232 surface->config(), SkToBool(surface->asTexture()),
2233 srcHasYcbcr)) {
Greg Daniel475eb702018-09-28 14:16:50 -04002234 return false;
2235 }
2236 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2237 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2238 srcRect, SkIPoint::Make(0,0))) {
2239 return false;
2240 }
2241 top = 0;
2242 left = 0;
2243 dstColorType = GrColorType::kRGBA_8888;
2244 image = copySurface.get();
2245 }
2246
Greg Daniel164a9f02016-02-22 09:56:40 -05002247 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002248 image->setImageLayout(this,
2249 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2250 VK_ACCESS_TRANSFER_READ_BIT,
2251 VK_PIPELINE_STAGE_TRANSFER_BIT,
2252 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002253
Brian Salomonc320b152018-02-20 14:05:36 -05002254 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002255 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002256
Greg Daniel164a9f02016-02-22 09:56:40 -05002257 VkBufferImageCopy region;
2258 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002259
2260 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2261 if (copyFromOrigin) {
2262 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002263 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002264 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002265 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002266 region.imageOffset = offset;
2267 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2268 }
2269
2270 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002271 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002272 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002273 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002274 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002275 kStream_GrAccessPattern)
2276 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002277
2278 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002279 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002280 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002281 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2282 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002283
2284 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002285 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002286 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002287 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002288 1,
2289 &region);
2290
2291 // make sure the copy to buffer has finished
2292 transferBuffer->addMemoryBarrier(this,
2293 VK_ACCESS_TRANSFER_WRITE_BIT,
2294 VK_ACCESS_HOST_READ_BIT,
2295 VK_PIPELINE_STAGE_TRANSFER_BIT,
2296 VK_PIPELINE_STAGE_HOST_BIT,
2297 false);
2298
2299 // We need to submit the current command buffer to the Queue and make sure it finishes before
2300 // we can copy the data out of the buffer.
2301 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002302 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002303 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002304 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002305
egdaniel6fa0a912016-09-12 11:51:29 -07002306 if (copyFromOrigin) {
2307 uint32_t skipRows = region.imageExtent.height - height;
2308 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2309 }
2310
Brian Salomona6948702018-06-01 15:33:20 -04002311 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002312
2313 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002314 return true;
2315}
egdaniel066df7c2016-06-08 14:02:27 -07002316
egdaniel27bb2842016-07-07 11:58:35 -07002317// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2318// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2319// the the entire attachment. Similar requirements for the y and height components.
2320void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2321 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2322 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002323 if ((0 != granularity.width && 1 != granularity.width)) {
2324 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2325 int rightAdj = srcBounds.fRight % granularity.width;
2326 if (rightAdj != 0) {
2327 rightAdj = granularity.width - rightAdj;
2328 }
2329 dstBounds->fRight = srcBounds.fRight + rightAdj;
2330 if (dstBounds->fRight > maxWidth) {
2331 dstBounds->fRight = maxWidth;
2332 dstBounds->fLeft = 0;
2333 } else {
2334 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2335 }
egdaniel27bb2842016-07-07 11:58:35 -07002336 } else {
egdanield5797b32016-09-20 12:57:45 -07002337 dstBounds->fLeft = srcBounds.fLeft;
2338 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002339 }
2340
2341 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002342 if ((0 != granularity.height && 1 != granularity.height)) {
2343 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2344 int bottomAdj = srcBounds.fBottom % granularity.height;
2345 if (bottomAdj != 0) {
2346 bottomAdj = granularity.height - bottomAdj;
2347 }
2348 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2349 if (dstBounds->fBottom > maxHeight) {
2350 dstBounds->fBottom = maxHeight;
2351 dstBounds->fTop = 0;
2352 } else {
2353 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2354 }
egdaniel27bb2842016-07-07 11:58:35 -07002355 } else {
egdanield5797b32016-09-20 12:57:45 -07002356 dstBounds->fTop = srcBounds.fTop;
2357 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002358 }
2359}
2360
Greg Daniel22bc8652017-03-22 15:45:43 -04002361void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002362 const GrVkRenderPass* renderPass,
2363 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002364 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002365 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002366 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002367 const SkIRect* pBounds = &bounds;
2368 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002369 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002370 flippedBounds = bounds;
2371 flippedBounds.fTop = target->height() - bounds.fBottom;
2372 flippedBounds.fBottom = target->height() - bounds.fTop;
2373 pBounds = &flippedBounds;
2374 }
2375
egdaniel27bb2842016-07-07 11:58:35 -07002376 // The bounds we use for the render pass should be of the granularity supported
2377 // by the device.
2378 const VkExtent2D& granularity = renderPass->granularity();
2379 SkIRect adjustedBounds;
2380 if ((0 != granularity.width && 1 != granularity.width) ||
2381 (0 != granularity.height && 1 != granularity.height)) {
2382 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2383 target->width(), target->height());
2384 pBounds = &adjustedBounds;
2385 }
2386
Robert Phillips95214472017-08-08 18:00:03 -04002387#ifdef SK_DEBUG
2388 uint32_t index;
2389 bool result = renderPass->colorAttachmentIndex(&index);
2390 SkASSERT(result && 0 == index);
2391 result = renderPass->stencilAttachmentIndex(&index);
2392 if (result) {
2393 SkASSERT(1 == index);
2394 }
2395#endif
2396 VkClearValue clears[2];
2397 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002398 clears[1].depthStencil.depth = 0.0f;
2399 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002400
2401 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002402 for (int i = 0; i < buffers.count(); ++i) {
2403 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2404 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002405 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002406
Brian Salomon1fabd512018-02-09 09:54:25 -05002407 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002408}
egdaniel9cb63402016-06-23 08:37:05 -07002409
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002410void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2411 if (buffer->asRTCommandBuffer()) {
2412 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2413
2414 fCachedRTCommandBuffer->submit();
2415 fCachedRTCommandBuffer->reset();
2416 } else {
2417 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2418
2419 fCachedTexCommandBuffer->submit();
2420 fCachedTexCommandBuffer->reset();
2421 }
2422}
2423
Greg Daniel6be35232017-03-01 17:01:09 -05002424GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002425 VkFenceCreateInfo createInfo;
2426 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2427 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2428 createInfo.pNext = nullptr;
2429 createInfo.flags = 0;
2430 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002431
2432 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2433 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2434
2435 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002436 return (GrFence)fence;
2437}
2438
Greg Daniel6be35232017-03-01 17:01:09 -05002439bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2440 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2441
2442 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002443 return (VK_SUCCESS == result);
2444}
2445
2446void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002447 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2448}
2449
Greg Daniela5cb7812017-06-16 09:45:32 -04002450sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2451 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002452}
2453
Greg Daniel48661b82018-01-22 16:11:35 -05002454sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2455 GrResourceProvider::SemaphoreWrapType wrapType,
2456 GrWrapOwnership ownership) {
2457 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002458}
2459
Greg Daniel858e12c2018-12-06 11:11:37 -05002460void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002461 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2462
Greg Daniel48661b82018-01-22 16:11:35 -05002463 GrVkSemaphore::Resource* resource = vkSem->getResource();
2464 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002465 resource->ref();
2466 fSemaphoresToSignal.push_back(resource);
2467 }
Greg Daniel6be35232017-03-01 17:01:09 -05002468}
2469
Greg Daniel48661b82018-01-22 16:11:35 -05002470void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002471 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2472
Greg Daniel48661b82018-01-22 16:11:35 -05002473 GrVkSemaphore::Resource* resource = vkSem->getResource();
2474 if (resource->shouldWait()) {
2475 resource->ref();
2476 fSemaphoresToWaitOn.push_back(resource);
2477 }
jvanverth84741b32016-09-30 08:39:02 -07002478}
Brian Osman13dddce2017-05-09 13:19:50 -04002479
2480sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2481 SkASSERT(texture);
2482 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2483 vkTexture->setImageLayout(this,
2484 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2485 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002486 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002487 false);
2488 this->submitCommandBuffer(kSkip_SyncQueue);
2489
2490 // The image layout change serves as a barrier, so no semaphore is needed
2491 return nullptr;
2492}
Greg Danielf5d87582017-12-18 14:48:15 -05002493
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002494void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2495 fDrawables.emplace_back(std::move(drawable));
2496}
2497
Greg Daniel7a82edf2018-12-04 10:54:34 -05002498uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2499 const GrBackendFormat& format) {
2500 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2501 SkASSERT(ycbcrInfo);
2502 if (!ycbcrInfo->isValid()) {
2503 return 0;
2504 }
2505
2506 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2507 samplerState, *ycbcrInfo);
2508
2509 return sampler->uniqueID();
2510}
2511
Greg Daniela870b462019-01-08 15:49:46 -05002512void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002513 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002514 this->resourceProvider().storePipelineCacheData();
2515 }
2516}