blob: 3a820d978276eae3c0a5b129c0d873fce4ae0279 [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
8#include "GrVkGpu.h"
9
Ethan Nicholas8e265a72018-12-12 16:22:40 -050010#include "GrContextPriv.h"
Greg Daniela5cb7812017-06-16 09:45:32 -040011#include "GrBackendSemaphore.h"
Greg Daniel7ef28f32017-04-20 16:41:55 +000012#include "GrBackendSurface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrContextOptions.h"
14#include "GrGeometryProcessor.h"
15#include "GrGpuResourceCacheAccess.h"
egdaniel0e1853c2016-03-17 11:35:45 -070016#include "GrMesh.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrPipeline.h"
18#include "GrRenderTargetPriv.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrTexturePriv.h"
Greg Daniel81df0412018-05-31 13:13:33 -040020#include "GrVkAMDMemoryAllocator.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050021#include "GrVkCommandBuffer.h"
Ethan Nicholas8e265a72018-12-12 16:22:40 -050022#include "GrVkCommandPool.h"
egdaniel066df7c2016-06-08 14:02:27 -070023#include "GrVkGpuCommandBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050024#include "GrVkImage.h"
25#include "GrVkIndexBuffer.h"
Greg Danield3e65aa2018-08-01 09:19:45 -040026#include "GrVkInterface.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050027#include "GrVkMemory.h"
28#include "GrVkPipeline.h"
egdaniel22281c12016-03-23 13:49:40 -070029#include "GrVkPipelineState.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050030#include "GrVkRenderPass.h"
31#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050032#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050033#include "GrVkTexture.h"
34#include "GrVkTextureRenderTarget.h"
35#include "GrVkTransferBuffer.h"
36#include "GrVkVertexBuffer.h"
Matt Sarett485c4992017-02-14 14:18:27 -050037#include "SkConvertPixels.h"
jvanverth900bd4a2016-04-29 13:53:12 -070038#include "SkMipMap.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040039#include "SkSLCompiler.h"
40#include "SkTo.h"
Greg Daniel98bffae2018-08-01 13:25:41 -040041
Greg Daniela31f4e52018-08-01 16:48:52 -040042#include "vk/GrVkExtensions.h"
jvanverthfd359ca2016-03-18 11:57:24 -070043#include "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)
52#include "SkLeanWindows.h"
53#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 {
118 // None of our current GrVkExtension flags actually affect the vulkan backend so we just
119 // make an empty GrVkExtensions and pass that to the GrVkInterface.
120 GrVkExtensions extensions;
121 interface.reset(new GrVkInterface(backendContext.fGetProc,
122 backendContext.fInstance,
123 backendContext.fDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500124 instanceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400125 physDevVersion,
Greg Daniel98bffae2018-08-01 13:25:41 -0400126 &extensions));
Greg Daniel41f0e282019-01-28 13:15:05 -0500127 if (!interface->validate(instanceVersion, physDevVersion, &extensions)) {
Greg Daniel98bffae2018-08-01 13:25:41 -0400128 return nullptr;
129 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500130 }
131
Greg Daniel41f0e282019-01-28 13:15:05 -0500132 return sk_sp<GrGpu>(new GrVkGpu(context, options, backendContext, interface, instanceVersion,
133 physDevVersion));
Greg Daniel164a9f02016-02-22 09:56:40 -0500134}
135
136////////////////////////////////////////////////////////////////////////////////
137
halcanary9d524f22016-03-29 09:03:52 -0700138GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options,
Greg Daniel41f0e282019-01-28 13:15:05 -0500139 const GrVkBackendContext& backendContext, sk_sp<const GrVkInterface> interface,
140 uint32_t instanceVersion, uint32_t physicalDeviceVersion)
Brian Salomon384fab42017-12-07 12:33:05 -0500141 : INHERITED(context)
Greg Danielc8cd45a2018-07-12 10:02:37 -0400142 , fInterface(std::move(interface))
Greg Danielf730c182018-07-02 20:15:37 +0000143 , fMemoryAllocator(backendContext.fMemoryAllocator)
144 , fInstance(backendContext.fInstance)
Greg Daniel637c06a2018-09-12 09:44:25 -0400145 , fPhysicalDevice(backendContext.fPhysicalDevice)
Greg Danielf730c182018-07-02 20:15:37 +0000146 , fDevice(backendContext.fDevice)
147 , fQueue(backendContext.fQueue)
Greg Danielecddbc02018-08-30 16:39:34 -0400148 , fQueueIndex(backendContext.fGraphicsQueueIndex)
Brian Salomon384fab42017-12-07 12:33:05 -0500149 , fResourceProvider(this)
150 , fDisconnected(false) {
Greg Danielf730c182018-07-02 20:15:37 +0000151 SkASSERT(!backendContext.fOwnsInstanceAndDevice);
jvanverth633b3562016-03-23 11:01:22 -0700152
Greg Daniel81df0412018-05-31 13:13:33 -0400153 if (!fMemoryAllocator) {
154 // We were not given a memory allocator at creation
Greg Danielf730c182018-07-02 20:15:37 +0000155 fMemoryAllocator.reset(new GrVkAMDMemoryAllocator(backendContext.fPhysicalDevice,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400156 fDevice, fInterface));
Greg Daniel81df0412018-05-31 13:13:33 -0400157 }
158
ethannicholasb3058bd2016-07-01 08:22:01 -0700159 fCompiler = new SkSL::Compiler();
jvanverth633b3562016-03-23 11:01:22 -0700160
Greg Daniela0651ac2018-08-08 09:23:18 -0400161 if (backendContext.fDeviceFeatures2) {
Greg Daniel36443602018-08-02 12:51:52 -0400162 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniela0651ac2018-08-08 09:23:18 -0400163 *backendContext.fDeviceFeatures2, instanceVersion,
Greg Daniel41f0e282019-01-28 13:15:05 -0500164 physicalDeviceVersion,
Greg Danielc0b03d82018-08-03 14:41:15 -0400165 *backendContext.fVkExtensions));
Greg Daniela0651ac2018-08-08 09:23:18 -0400166 } else if (backendContext.fDeviceFeatures) {
167 VkPhysicalDeviceFeatures2 features2;
168 features2.pNext = nullptr;
169 features2.features = *backendContext.fDeviceFeatures;
170 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500171 features2, instanceVersion, physicalDeviceVersion,
172 *backendContext.fVkExtensions));
Greg Daniel36443602018-08-02 12:51:52 -0400173 } else {
Greg Daniela0651ac2018-08-08 09:23:18 -0400174 VkPhysicalDeviceFeatures2 features;
175 memset(&features, 0, sizeof(VkPhysicalDeviceFeatures2));
176 features.pNext = nullptr;
Greg Daniel36443602018-08-02 12:51:52 -0400177 if (backendContext.fFeatures & kGeometryShader_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400178 features.features.geometryShader = true;
Greg Daniel36443602018-08-02 12:51:52 -0400179 }
180 if (backendContext.fFeatures & kDualSrcBlend_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400181 features.features.dualSrcBlend = true;
Greg Daniel36443602018-08-02 12:51:52 -0400182 }
183 if (backendContext.fFeatures & kSampleRateShading_GrVkFeatureFlag) {
Greg Daniela0651ac2018-08-08 09:23:18 -0400184 features.features.sampleRateShading = true;
Greg Daniel36443602018-08-02 12:51:52 -0400185 }
186 fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendContext.fPhysicalDevice,
Greg Daniel41f0e282019-01-28 13:15:05 -0500187 features, instanceVersion, physicalDeviceVersion,
188 GrVkExtensions()));
Greg Daniel36443602018-08-02 12:51:52 -0400189 }
jvanverth633b3562016-03-23 11:01:22 -0700190 fCaps.reset(SkRef(fVkCaps.get()));
191
Greg Danielf730c182018-07-02 20:15:37 +0000192 VK_CALL(GetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &fPhysDevProps));
193 VK_CALL(GetPhysicalDeviceMemoryProperties(backendContext.fPhysicalDevice, &fPhysDevMemProps));
jvanverth633b3562016-03-23 11:01:22 -0700194
Greg Daniela870b462019-01-08 15:49:46 -0500195 fResourceProvider.init();
196
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500197 fCmdPool = fResourceProvider.findOrCreateCommandPool();
198 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Ethan Nicholasbff4e072018-12-12 18:17:24 +0000199 SkASSERT(fCurrentCmdBuffer);
jvanverth633b3562016-03-23 11:01:22 -0700200 fCurrentCmdBuffer->begin(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500201}
202
Greg Daniel8606cf82017-05-08 16:17:53 -0400203void GrVkGpu::destroyResources() {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500204 if (fCmdPool) {
205 fCmdPool->getPrimaryCommandBuffer()->end(this);
206 fCmdPool->close();
Greg Daniel8606cf82017-05-08 16:17:53 -0400207 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500208
209 // wait for all commands to finish
Jim Van Verth09557d72016-11-07 11:10:21 -0500210 VkResult res = VK_CALL(QueueWaitIdle(fQueue));
egdanielf8c2be32016-06-24 13:18:27 -0700211
212 // On windows, sometimes calls to QueueWaitIdle return before actually signalling the fences
213 // on the command buffers even though they have completed. This causes an assert to fire when
214 // destroying the command buffers. Currently this ony seems to happen on windows, so we add a
Jim Van Verth09557d72016-11-07 11:10:21 -0500215 // sleep to make sure the fence signals.
egdanielf8c2be32016-06-24 13:18:27 -0700216#ifdef SK_DEBUG
Greg Daniel80a08dd2017-01-20 10:45:49 -0500217 if (this->vkCaps().mustSleepOnTearDown()) {
egdanielf8c2be32016-06-24 13:18:27 -0700218#if defined(SK_BUILD_FOR_WIN)
Greg Daniel80a08dd2017-01-20 10:45:49 -0500219 Sleep(10); // In milliseconds
egdanielf8c2be32016-06-24 13:18:27 -0700220#else
Greg Daniel80a08dd2017-01-20 10:45:49 -0500221 sleep(1); // In seconds
egdanielf8c2be32016-06-24 13:18:27 -0700222#endif
Greg Daniel80a08dd2017-01-20 10:45:49 -0500223 }
egdanielf8c2be32016-06-24 13:18:27 -0700224#endif
225
egdanielbe9d8212016-09-20 08:54:23 -0700226#ifdef SK_DEBUG
Greg Daniel8a8668b2016-10-31 16:34:42 -0400227 SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res);
egdanielbe9d8212016-09-20 08:54:23 -0700228#endif
halcanary9d524f22016-03-29 09:03:52 -0700229
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500230 if (fCmdPool) {
231 fCmdPool->unref(this);
232 fCmdPool = nullptr;
233 }
234
Greg Daniel6be35232017-03-01 17:01:09 -0500235 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
236 fSemaphoresToWaitOn[i]->unref(this);
237 }
238 fSemaphoresToWaitOn.reset();
239
Greg Daniela5cb7812017-06-16 09:45:32 -0400240 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
241 fSemaphoresToSignal[i]->unref(this);
242 }
243 fSemaphoresToSignal.reset();
244
245
egdanielbc9b2962016-09-27 08:00:53 -0700246 fCopyManager.destroyResources(this);
247
Jim Van Verth09557d72016-11-07 11:10:21 -0500248 // must call this just before we destroy the command pool and VkDevice
249 fResourceProvider.destroyResources(VK_ERROR_DEVICE_LOST == res);
Greg Daniel164a9f02016-02-22 09:56:40 -0500250
Greg Danielf730c182018-07-02 20:15:37 +0000251 fMemoryAllocator.reset();
252
253 fQueue = VK_NULL_HANDLE;
254 fDevice = VK_NULL_HANDLE;
255 fInstance = VK_NULL_HANDLE;
Greg Daniel8606cf82017-05-08 16:17:53 -0400256}
257
258GrVkGpu::~GrVkGpu() {
259 if (!fDisconnected) {
260 this->destroyResources();
261 }
262 delete fCompiler;
263}
264
265
266void GrVkGpu::disconnect(DisconnectType type) {
267 INHERITED::disconnect(type);
268 if (!fDisconnected) {
269 if (DisconnectType::kCleanup == type) {
270 this->destroyResources();
271 } else {
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500272 if (fCmdPool) {
273 fCmdPool->unrefAndAbandon();
274 fCmdPool = nullptr;
Greg Danieladb4bfe2018-08-23 16:15:05 -0400275 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400276 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
277 fSemaphoresToWaitOn[i]->unrefAndAbandon();
278 }
Greg Daniela5cb7812017-06-16 09:45:32 -0400279 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
280 fSemaphoresToSignal[i]->unrefAndAbandon();
281 }
Greg Daniel8606cf82017-05-08 16:17:53 -0400282 fCopyManager.abandonResources();
283
284 // must call this just before we destroy the command pool and VkDevice
285 fResourceProvider.abandonResources();
Greg Danieladb4bfe2018-08-23 16:15:05 -0400286
287 fMemoryAllocator.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400288 }
289 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400290 fSemaphoresToSignal.reset();
Greg Daniel8606cf82017-05-08 16:17:53 -0400291 fCurrentCmdBuffer = nullptr;
Greg Daniel8606cf82017-05-08 16:17:53 -0400292 fDisconnected = true;
293 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500294}
295
296///////////////////////////////////////////////////////////////////////////////
297
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400298GrGpuRTCommandBuffer* GrVkGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -0400299 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Greg Daniel500d58b2017-08-24 15:59:33 -0400300 const GrGpuRTCommandBuffer::LoadAndStoreInfo& colorInfo,
301 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo& stencilInfo) {
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400302 if (!fCachedRTCommandBuffer) {
303 fCachedRTCommandBuffer.reset(new GrVkGpuRTCommandBuffer(this));
304 }
305
Greg Daniela41a74a2018-10-09 12:59:23 +0000306 fCachedRTCommandBuffer->set(rt, origin, colorInfo, stencilInfo);
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400307 return fCachedRTCommandBuffer.get();
Greg Daniel500d58b2017-08-24 15:59:33 -0400308}
309
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400310GrGpuTextureCommandBuffer* GrVkGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
311 if (!fCachedTexCommandBuffer) {
312 fCachedTexCommandBuffer.reset(new GrVkGpuTextureCommandBuffer(this));
313 }
314
315 fCachedTexCommandBuffer->set(texture, origin);
316 return fCachedTexCommandBuffer.get();
egdaniel066df7c2016-06-08 14:02:27 -0700317}
318
Greg Daniela5cb7812017-06-16 09:45:32 -0400319void GrVkGpu::submitCommandBuffer(SyncQueue sync) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500320 SkASSERT(fCurrentCmdBuffer);
321 fCurrentCmdBuffer->end(this);
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500322 fCmdPool->close();
Greg Daniela5cb7812017-06-16 09:45:32 -0400323 fCurrentCmdBuffer->submitToQueue(this, fQueue, sync, fSemaphoresToSignal, fSemaphoresToWaitOn);
Greg Daniel6be35232017-03-01 17:01:09 -0500324
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400325 // We must delete and drawables that have been waitint till submit for us to destroy.
326 fDrawables.reset();
327
Greg Daniel6be35232017-03-01 17:01:09 -0500328 for (int i = 0; i < fSemaphoresToWaitOn.count(); ++i) {
329 fSemaphoresToWaitOn[i]->unref(this);
330 }
331 fSemaphoresToWaitOn.reset();
Greg Daniela5cb7812017-06-16 09:45:32 -0400332 for (int i = 0; i < fSemaphoresToSignal.count(); ++i) {
333 fSemaphoresToSignal[i]->unref(this);
334 }
335 fSemaphoresToSignal.reset();
Greg Daniel6be35232017-03-01 17:01:09 -0500336
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500337 // Release old command pool and create a new one
338 fCmdPool->unref(this);
Greg Daniel164a9f02016-02-22 09:56:40 -0500339 fResourceProvider.checkCommandBuffers();
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500340 fCmdPool = fResourceProvider.findOrCreateCommandPool();
341 fCurrentCmdBuffer = fCmdPool->getPrimaryCommandBuffer();
Greg Daniel164a9f02016-02-22 09:56:40 -0500342 fCurrentCmdBuffer->begin(this);
343}
344
345///////////////////////////////////////////////////////////////////////////////
Brian Salomondbf70722019-02-07 11:31:24 -0500346sk_sp<GrGpuBuffer> GrVkGpu::onCreateBuffer(size_t size, GrGpuBufferType type,
347 GrAccessPattern accessPattern, const void* data) {
348 sk_sp<GrGpuBuffer> buff;
cdalton397536c2016-03-25 12:15:03 -0700349 switch (type) {
Brian Salomonae64c192019-02-05 09:41:37 -0500350 case GrGpuBufferType::kVertex:
cdalton397536c2016-03-25 12:15:03 -0700351 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
352 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500353 buff = GrVkVertexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700354 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500355 case GrGpuBufferType::kIndex:
cdalton397536c2016-03-25 12:15:03 -0700356 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
357 kStatic_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500358 buff = GrVkIndexBuffer::Make(this, size, kDynamic_GrAccessPattern == accessPattern);
egdaniele05bbbb2016-04-19 12:13:41 -0700359 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500360 case GrGpuBufferType::kXferCpuToGpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400361 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
362 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500363 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyRead_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700364 break;
Brian Salomonae64c192019-02-05 09:41:37 -0500365 case GrGpuBufferType::kXferGpuToCpu:
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400366 SkASSERT(kDynamic_GrAccessPattern == accessPattern ||
367 kStream_GrAccessPattern == accessPattern);
Brian Salomon12d22642019-01-29 14:38:50 -0500368 buff = GrVkTransferBuffer::Make(this, size, GrVkBuffer::kCopyWrite_Type);
egdaniele05bbbb2016-04-19 12:13:41 -0700369 break;
cdalton397536c2016-03-25 12:15:03 -0700370 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400371 SK_ABORT("Unknown buffer type.");
cdalton397536c2016-03-25 12:15:03 -0700372 return nullptr;
373 }
cdalton1bf3e712016-04-19 10:00:02 -0700374 if (data && buff) {
375 buff->updateData(data, size);
376 }
377 return buff;
Greg Daniel164a9f02016-02-22 09:56:40 -0500378}
379
Brian Salomona9b04b92018-06-01 15:04:28 -0400380bool GrVkGpu::onWritePixels(GrSurface* surface, int left, int top, int width, int height,
381 GrColorType srcColorType, const GrMipLevel texels[],
382 int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500383 GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture());
384 if (!vkTex) {
385 return false;
386 }
387
jvanverth900bd4a2016-04-29 13:53:12 -0700388 // Make sure we have at least the base level
Robert Phillips590533f2017-07-11 14:22:35 -0400389 if (!mipLevelCount || !texels[0].fPixels) {
jvanverth03509ea2016-03-02 13:19:47 -0800390 return false;
391 }
bsalomona1e6b3b2016-03-02 10:58:23 -0800392
Jim Van Verth1676cb92019-01-15 13:24:45 -0500393 SkASSERT(!GrPixelConfigIsCompressed(vkTex->config()));
Greg Daniel164a9f02016-02-22 09:56:40 -0500394 bool success = false;
Robert Phillips92de6312017-05-23 07:43:48 -0400395 bool linearTiling = vkTex->isLinearTiled();
396 if (linearTiling) {
Robert Phillips590533f2017-07-11 14:22:35 -0400397 if (mipLevelCount > 1) {
Robert Phillips92de6312017-05-23 07:43:48 -0400398 SkDebugf("Can't upload mipmap data to linear tiled texture");
399 return false;
400 }
401 if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) {
402 // Need to change the layout to general in order to perform a host write
403 vkTex->setImageLayout(this,
404 VK_IMAGE_LAYOUT_GENERAL,
405 VK_ACCESS_HOST_WRITE_BIT,
406 VK_PIPELINE_STAGE_HOST_BIT,
407 false);
408 this->submitCommandBuffer(kForce_SyncQueue);
409 }
Brian Salomona9b04b92018-06-01 15:04:28 -0400410 success = this->uploadTexDataLinear(vkTex, left, top, width, height, srcColorType,
Robert Phillips590533f2017-07-11 14:22:35 -0400411 texels[0].fPixels, texels[0].fRowBytes);
Greg Daniel164a9f02016-02-22 09:56:40 -0500412 } else {
Greg Danielda86e282018-06-13 09:41:19 -0400413 SkASSERT(mipLevelCount <= vkTex->texturePriv().maxMipMapLevel() + 1);
Brian Salomona9b04b92018-06-01 15:04:28 -0400414 success = this->uploadTexDataOptimal(vkTex, left, top, width, height, srcColorType, texels,
415 mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500416 }
egdaniel4583ec52016-06-27 12:57:00 -0700417
jvanverth900bd4a2016-04-29 13:53:12 -0700418 return success;
Greg Daniel164a9f02016-02-22 09:56:40 -0500419}
420
Brian Salomonc320b152018-02-20 14:05:36 -0500421bool GrVkGpu::onTransferPixels(GrTexture* texture, int left, int top, int width, int height,
Brian Salomondbf70722019-02-07 11:31:24 -0500422 GrColorType bufferColorType, GrGpuBuffer* transferBuffer,
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400423 size_t bufferOffset, size_t rowBytes) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500424 // Can't transfer compressed data
425 SkASSERT(!GrPixelConfigIsCompressed(texture->config()));
426
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400427 // Vulkan only supports 4-byte aligned offsets
428 if (SkToBool(bufferOffset & 0x2)) {
429 return false;
430 }
431 GrVkTexture* vkTex = static_cast<GrVkTexture*>(texture);
432 if (!vkTex) {
433 return false;
434 }
435 GrVkTransferBuffer* vkBuffer = static_cast<GrVkTransferBuffer*>(transferBuffer);
436 if (!vkBuffer) {
437 return false;
438 }
439
Greg Daniel660cc992017-06-26 14:55:05 -0400440 SkDEBUGCODE(
441 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
442 SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
443 SkASSERT(bounds.contains(subRect));
444 )
Brian Salomonc320b152018-02-20 14:05:36 -0500445 int bpp = GrColorTypeBytesPerPixel(bufferColorType);
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400446 if (rowBytes == 0) {
Brian Salomonc320b152018-02-20 14:05:36 -0500447 rowBytes = bpp * width;
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400448 }
449
450 // Set up copy region
451 VkBufferImageCopy region;
452 memset(&region, 0, sizeof(VkBufferImageCopy));
453 region.bufferOffset = bufferOffset;
454 region.bufferRowLength = (uint32_t)(rowBytes/bpp);
455 region.bufferImageHeight = 0;
456 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
457 region.imageOffset = { left, top, 0 };
458 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
459
460 // Change layout of our target so it can be copied to
461 vkTex->setImageLayout(this,
462 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
463 VK_ACCESS_TRANSFER_WRITE_BIT,
464 VK_PIPELINE_STAGE_TRANSFER_BIT,
465 false);
466
467 // Copy the buffer to the image
468 fCurrentCmdBuffer->copyBufferToImage(this,
469 vkBuffer,
470 vkTex,
471 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
472 1,
473 &region);
474
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400475 vkTex->texturePriv().markMipMapsDirty();
Jim Van Verth2e5eaf02017-06-21 15:55:46 -0400476 return true;
477}
478
Brian Salomon1fabd512018-02-09 09:54:25 -0500479void GrVkGpu::resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
480 const SkIPoint& dstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -0700481 SkASSERT(dst);
482 SkASSERT(src && src->numColorSamples() > 1 && src->msaaImage());
483
egdaniel4bcd62e2016-08-31 07:37:31 -0700484 VkImageResolve resolveInfo;
Brian Salomon1fabd512018-02-09 09:54:25 -0500485 resolveInfo.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
486 resolveInfo.srcOffset = {srcRect.fLeft, srcRect.fTop, 0};
487 resolveInfo.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
488 resolveInfo.dstOffset = {dstPoint.fX, dstPoint.fY, 0};
489 resolveInfo.extent = {(uint32_t)srcRect.width(), (uint32_t)srcRect.height(), 1};
egdaniel4bcd62e2016-08-31 07:37:31 -0700490
Greg Danielbc26c392017-04-18 13:32:10 -0400491 GrVkImage* dstImage;
492 GrRenderTarget* dstRT = dst->asRenderTarget();
493 if (dstRT) {
494 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbc26c392017-04-18 13:32:10 -0400495 dstImage = vkRT;
496 } else {
497 SkASSERT(dst->asTexture());
498 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
499 }
500 dstImage->setImageLayout(this,
501 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
502 VK_ACCESS_TRANSFER_WRITE_BIT,
503 VK_PIPELINE_STAGE_TRANSFER_BIT,
504 false);
egdaniel4bcd62e2016-08-31 07:37:31 -0700505
506 src->msaaImage()->setImageLayout(this,
507 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
508 VK_ACCESS_TRANSFER_READ_BIT,
509 VK_PIPELINE_STAGE_TRANSFER_BIT,
510 false);
511
Greg Danielbc26c392017-04-18 13:32:10 -0400512 fCurrentCmdBuffer->resolveImage(this, *src->msaaImage(), *dstImage, 1, &resolveInfo);
egdaniel4bcd62e2016-08-31 07:37:31 -0700513}
514
Brian Salomon1fabd512018-02-09 09:54:25 -0500515void GrVkGpu::internalResolveRenderTarget(GrRenderTarget* target, bool requiresSubmit) {
egdaniel66933552016-08-24 07:22:19 -0700516 if (target->needsResolve()) {
517 SkASSERT(target->numColorSamples() > 1);
egdaniel52ad2512016-08-04 12:50:01 -0700518 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(target);
519 SkASSERT(rt->msaaImage());
Greg Daniel69d49922017-02-23 09:44:02 -0500520
egdaniel4bcd62e2016-08-31 07:37:31 -0700521 const SkIRect& srcRect = rt->getResolveRect();
egdaniel52ad2512016-08-04 12:50:01 -0700522
Brian Salomon1fabd512018-02-09 09:54:25 -0500523 this->resolveImage(target, rt, srcRect, SkIPoint::Make(srcRect.fLeft, srcRect.fTop));
egdaniel52ad2512016-08-04 12:50:01 -0700524
525 rt->flagAsResolved();
Greg Daniel69d49922017-02-23 09:44:02 -0500526
527 if (requiresSubmit) {
528 this->submitCommandBuffer(kSkip_SyncQueue);
529 }
egdaniel52ad2512016-08-04 12:50:01 -0700530 }
531}
532
Brian Salomona9b04b92018-06-01 15:04:28 -0400533bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
534 GrColorType dataColorType, const void* data, size_t rowBytes) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500535 SkASSERT(data);
jvanverth900bd4a2016-04-29 13:53:12 -0700536 SkASSERT(tex->isLinearTiled());
Greg Daniel164a9f02016-02-22 09:56:40 -0500537
Jim Van Verth1676cb92019-01-15 13:24:45 -0500538 // If we're uploading compressed data then we should be using uploadCompressedTexData
539 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
540 GrSRGBEncoded::kNo)));
541
Greg Daniel660cc992017-06-26 14:55:05 -0400542 SkDEBUGCODE(
543 SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
544 SkIRect bounds = SkIRect::MakeWH(tex->width(), tex->height());
545 SkASSERT(bounds.contains(subRect));
546 )
Brian Salomonc320b152018-02-20 14:05:36 -0500547 int bpp = GrColorTypeBytesPerPixel(dataColorType);
Greg Daniel164a9f02016-02-22 09:56:40 -0500548 size_t trimRowBytes = width * bpp;
Greg Daniel660cc992017-06-26 14:55:05 -0400549 if (!rowBytes) {
550 rowBytes = trimRowBytes;
551 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500552
jvanverth900bd4a2016-04-29 13:53:12 -0700553 SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() ||
554 VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout());
555 const VkImageSubresource subres = {
556 VK_IMAGE_ASPECT_COLOR_BIT,
557 0, // mipLevel
558 0, // arraySlice
559 };
560 VkSubresourceLayout layout;
Greg Daniel164a9f02016-02-22 09:56:40 -0500561
jvanverth900bd4a2016-04-29 13:53:12 -0700562 const GrVkInterface* interface = this->vkInterface();
Greg Daniel164a9f02016-02-22 09:56:40 -0500563
jvanverth900bd4a2016-04-29 13:53:12 -0700564 GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice,
egdanielb2df0c22016-05-13 11:30:37 -0700565 tex->image(),
jvanverth900bd4a2016-04-29 13:53:12 -0700566 &subres,
567 &layout));
Greg Daniel164a9f02016-02-22 09:56:40 -0500568
jvanverth1e305ba2016-06-01 09:39:15 -0700569 const GrVkAlloc& alloc = tex->alloc();
Brian Salomona9b04b92018-06-01 15:04:28 -0400570 VkDeviceSize offset = top * layout.rowPitch + left * bpp;
jvanverth900bd4a2016-04-29 13:53:12 -0700571 VkDeviceSize size = height*layout.rowPitch;
Greg Daniel81df0412018-05-31 13:13:33 -0400572 SkASSERT(size + offset <= alloc.fSize);
573 void* mapPtr = GrVkMemory::MapAlloc(this, alloc);
574 if (!mapPtr) {
jvanverth900bd4a2016-04-29 13:53:12 -0700575 return false;
576 }
Greg Daniel81df0412018-05-31 13:13:33 -0400577 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
jvanverth900bd4a2016-04-29 13:53:12 -0700578
Brian Salomona9b04b92018-06-01 15:04:28 -0400579 SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, trimRowBytes,
580 height);
jvanverth900bd4a2016-04-29 13:53:12 -0700581
Greg Daniele35a99e2018-03-02 11:44:22 -0500582 GrVkMemory::FlushMappedAlloc(this, alloc, offset, size);
Greg Daniel81df0412018-05-31 13:13:33 -0400583 GrVkMemory::UnmapAlloc(this, alloc);
jvanverth900bd4a2016-04-29 13:53:12 -0700584
585 return true;
586}
587
Brian Salomona9b04b92018-06-01 15:04:28 -0400588bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
589 GrColorType dataColorType, const GrMipLevel texels[],
590 int mipLevelCount) {
jvanverth900bd4a2016-04-29 13:53:12 -0700591 SkASSERT(!tex->isLinearTiled());
592 // The assumption is either that we have no mipmaps, or that our rect is the entire texture
Robert Phillips590533f2017-07-11 14:22:35 -0400593 SkASSERT(1 == mipLevelCount ||
jvanverth900bd4a2016-04-29 13:53:12 -0700594 (0 == left && 0 == top && width == tex->width() && height == tex->height()));
595
Greg Danieldd20e912017-04-07 14:42:23 -0400596 // We assume that if the texture has mip levels, we either upload to all the levels or just the
597 // first.
Robert Phillips590533f2017-07-11 14:22:35 -0400598 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
Greg Danieldd20e912017-04-07 14:42:23 -0400599
Jim Van Verth1676cb92019-01-15 13:24:45 -0500600 // If we're uploading compressed data then we should be using uploadCompressedTexData
601 SkASSERT(!GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
602 GrSRGBEncoded::kNo)));
603
jvanverth900bd4a2016-04-29 13:53:12 -0700604 if (width == 0 || height == 0) {
605 return false;
606 }
607
Greg Daniel475eb702018-09-28 14:16:50 -0400608 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
609 return false;
610 }
611
612 // For RGB_888x src data we are uploading it first to an RGBA texture and then copying it to the
613 // dst RGB texture. Thus we do not upload mip levels for that.
614 if (dataColorType == GrColorType::kRGB_888x) {
615 SkASSERT(tex->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
616 tex->config() == kRGB_888_GrPixelConfig);
617 // First check that we'll be able to do the copy to the to the R8G8B8 image in the end via a
618 // blit or draw.
619 if (!this->vkCaps().configCanBeDstofBlit(kRGB_888_GrPixelConfig, tex->isLinearTiled()) &&
620 !this->vkCaps().maxRenderTargetSampleCount(kRGB_888_GrPixelConfig)) {
621 return false;
622 }
623 mipLevelCount = 1;
624 }
625
Brian Salomond1eaf492017-05-18 10:02:08 -0400626 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
Brian Salomonc320b152018-02-20 14:05:36 -0500627 int bpp = GrColorTypeBytesPerPixel(dataColorType);
jvanverth900bd4a2016-04-29 13:53:12 -0700628
629 // texels is const.
jvanverthc578b0632016-05-02 10:58:12 -0700630 // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes.
631 // Because of this we need to make a non-const shallow copy of texels.
Robert Phillips0f992772017-07-12 08:24:56 -0400632 SkAutoTMalloc<GrMipLevel> texelsShallowCopy;
633
Greg Daniel475eb702018-09-28 14:16:50 -0400634 texelsShallowCopy.reset(mipLevelCount);
635 memcpy(texelsShallowCopy.get(), texels, mipLevelCount*sizeof(GrMipLevel));
jvanverth900bd4a2016-04-29 13:53:12 -0700636
Robert Phillips590533f2017-07-11 14:22:35 -0400637 SkTArray<size_t> individualMipOffsets(mipLevelCount);
jvanverthc578b0632016-05-02 10:58:12 -0700638 individualMipOffsets.push_back(0);
639 size_t combinedBufferSize = width * bpp * height;
640 int currentWidth = width;
641 int currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400642 if (!texelsShallowCopy[0].fPixels) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400643 combinedBufferSize = 0;
644 }
645
Greg Daniel468fd632017-03-22 17:03:45 -0400646 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
647 // config. This works with the assumption that the bytes in pixel config is always a power of 2.
648 SkASSERT((bpp & (bpp - 1)) == 0);
649 const size_t alignmentMask = 0x3 | (bpp - 1);
Robert Phillips590533f2017-07-11 14:22:35 -0400650 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
jvanverthc578b0632016-05-02 10:58:12 -0700651 currentWidth = SkTMax(1, currentWidth/2);
652 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniel660cc992017-06-26 14:55:05 -0400653
Greg Daniel55afd6d2017-09-29 09:32:44 -0400654 if (texelsShallowCopy[currentMipLevel].fPixels) {
655 const size_t trimmedSize = currentWidth * bpp * currentHeight;
656 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
657 if (alignmentDiff != 0) {
658 combinedBufferSize += alignmentMask - alignmentDiff + 1;
659 }
660 individualMipOffsets.push_back(combinedBufferSize);
661 combinedBufferSize += trimmedSize;
662 } else {
663 individualMipOffsets.push_back(0);
Greg Daniel468fd632017-03-22 17:03:45 -0400664 }
Greg Daniel55afd6d2017-09-29 09:32:44 -0400665 }
666 if (0 == combinedBufferSize) {
667 // We don't actually have any data to upload so just return success
668 return true;
jvanverth900bd4a2016-04-29 13:53:12 -0700669 }
670
671 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500672 sk_sp<GrVkTransferBuffer> transferBuffer =
673 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Greg Daniel475eb702018-09-28 14:16:50 -0400674 if (!transferBuffer) {
Forrest Reilingc04f8452017-04-26 19:26:12 -0700675 return false;
Greg Daniel6888c0d2017-08-25 11:55:50 -0400676 }
jvanverth900bd4a2016-04-29 13:53:12 -0700677
Greg Daniel475eb702018-09-28 14:16:50 -0400678 int uploadLeft = left;
679 int uploadTop = top;
680 GrVkTexture* uploadTexture = tex;
681 // For uploading RGB_888x data to an R8G8B8_UNORM texture we must first upload the data to an
682 // R8G8B8A8_UNORM image and then copy it.
683 sk_sp<GrVkTexture> copyTexture;
684 if (dataColorType == GrColorType::kRGB_888x) {
685 GrSurfaceDesc surfDesc;
686 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
687 surfDesc.fWidth = width;
688 surfDesc.fHeight = height;
689 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
690 surfDesc.fSampleCnt = 1;
691
692 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT |
693 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
694 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
695
696 GrVkImage::ImageDesc imageDesc;
697 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
698 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
699 imageDesc.fWidth = width;
700 imageDesc.fHeight = height;
701 imageDesc.fLevels = 1;
702 imageDesc.fSamples = 1;
703 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
704 imageDesc.fUsageFlags = usageFlags;
705 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
706
707 copyTexture = GrVkTexture::MakeNewTexture(this, SkBudgeted::kYes, surfDesc, imageDesc,
708 GrMipMapsStatus::kNotAllocated);
709 if (!copyTexture) {
710 return false;
711 }
712 uploadTexture = copyTexture.get();
713 uploadLeft = 0;
714 uploadTop = 0;
715 }
716
jvanverth900bd4a2016-04-29 13:53:12 -0700717 char* buffer = (char*) transferBuffer->map();
Robert Phillips590533f2017-07-11 14:22:35 -0400718 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
jvanverth900bd4a2016-04-29 13:53:12 -0700719
jvanverthc578b0632016-05-02 10:58:12 -0700720 currentWidth = width;
721 currentHeight = height;
Greg Daniel475eb702018-09-28 14:16:50 -0400722 int layerHeight = uploadTexture->height();
Robert Phillips590533f2017-07-11 14:22:35 -0400723 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
Greg Daniel55afd6d2017-09-29 09:32:44 -0400724 if (texelsShallowCopy[currentMipLevel].fPixels) {
725 SkASSERT(1 == mipLevelCount || currentHeight == layerHeight);
726 const size_t trimRowBytes = currentWidth * bpp;
727 const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes
728 ? texelsShallowCopy[currentMipLevel].fRowBytes
729 : trimRowBytes;
jvanverth900bd4a2016-04-29 13:53:12 -0700730
Greg Daniel55afd6d2017-09-29 09:32:44 -0400731 // copy data into the buffer, skipping the trailing bytes
732 char* dst = buffer + individualMipOffsets[currentMipLevel];
733 const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels;
Brian Salomona9b04b92018-06-01 15:04:28 -0400734 SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400735
736 VkBufferImageCopy& region = regions.push_back();
737 memset(&region, 0, sizeof(VkBufferImageCopy));
738 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
739 region.bufferRowLength = currentWidth;
740 region.bufferImageHeight = currentHeight;
741 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
Greg Daniel475eb702018-09-28 14:16:50 -0400742 region.imageOffset = {uploadLeft, uploadTop, 0};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400743 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
jvanverth900bd4a2016-04-29 13:53:12 -0700744 }
jvanverthc578b0632016-05-02 10:58:12 -0700745 currentWidth = SkTMax(1, currentWidth/2);
746 currentHeight = SkTMax(1, currentHeight/2);
Greg Daniela1b282b2017-03-28 14:56:46 -0400747 layerHeight = currentHeight;
jvanverth900bd4a2016-04-29 13:53:12 -0700748 }
749
jvanverth9d54afc2016-09-20 09:20:03 -0700750 // no need to flush non-coherent memory, unmap will do that for us
jvanverth900bd4a2016-04-29 13:53:12 -0700751 transferBuffer->unmap();
752
jvanverth900bd4a2016-04-29 13:53:12 -0700753 // Change layout of our target so it can be copied to
Greg Daniel475eb702018-09-28 14:16:50 -0400754 uploadTexture->setImageLayout(this,
755 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
756 VK_ACCESS_TRANSFER_WRITE_BIT,
757 VK_PIPELINE_STAGE_TRANSFER_BIT,
758 false);
jvanverth900bd4a2016-04-29 13:53:12 -0700759
760 // Copy the buffer to the image
761 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500762 transferBuffer.get(),
Greg Daniel475eb702018-09-28 14:16:50 -0400763 uploadTexture,
jvanverth900bd4a2016-04-29 13:53:12 -0700764 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
765 regions.count(),
766 regions.begin());
Greg Daniel475eb702018-09-28 14:16:50 -0400767
768 // If we copied the data into a temporary image first, copy that image into our main texture
769 // now.
770 if (copyTexture.get()) {
771 SkASSERT(dataColorType == GrColorType::kRGB_888x);
772 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
773 SkAssertResult(this->copySurface(tex, kOrigin, copyTexture.get(), kOrigin,
774 SkIRect::MakeWH(width, height), SkIPoint::Make(left, top),
775 false));
776 }
Robert Phillips590533f2017-07-11 14:22:35 -0400777 if (1 == mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400778 tex->texturePriv().markMipMapsDirty();
Greg Danieldd20e912017-04-07 14:42:23 -0400779 }
jvanverth900bd4a2016-04-29 13:53:12 -0700780
Greg Daniel164a9f02016-02-22 09:56:40 -0500781 return true;
782}
783
Jim Van Verth1676cb92019-01-15 13:24:45 -0500784// It's probably possible to roll this into uploadTexDataOptimal,
785// but for now it's easier to maintain as a separate entity.
786bool GrVkGpu::uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
787 GrColorType dataColorType, const GrMipLevel texels[],
788 int mipLevelCount) {
789 SkASSERT(!tex->isLinearTiled());
790 // For now the assumption is that our rect is the entire texture.
791 // Compressed textures are read-only so this should be a reasonable assumption.
792 SkASSERT(0 == left && 0 == top && width == tex->width() && height == tex->height());
793
794 // We assume that if the texture has mip levels, we either upload to all the levels or just the
795 // first.
796 SkASSERT(1 == mipLevelCount || mipLevelCount == (tex->texturePriv().maxMipMapLevel() + 1));
797
798 SkASSERT(GrPixelConfigIsCompressed(GrColorTypeToPixelConfig(dataColorType,
799 GrSRGBEncoded::kNo)));
800
801 if (width == 0 || height == 0) {
802 return false;
803 }
804
805 if (GrPixelConfigToColorType(tex->config()) != dataColorType) {
806 return false;
807 }
808
809 SkASSERT(this->caps()->isConfigTexturable(tex->config()));
810
811 SkTArray<size_t> individualMipOffsets(mipLevelCount);
812 individualMipOffsets.push_back(0);
813 size_t combinedBufferSize = GrCompressedFormatDataSize(tex->config(), width, height);
814 int currentWidth = width;
815 int currentHeight = height;
816 if (!texels[0].fPixels) {
817 return false;
818 }
819
820 // We assume that the alignment for any compressed format is at least 4 bytes and so we don't
821 // need to worry about alignment issues. For example, each block in ETC1 is 8 bytes.
822 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; currentMipLevel++) {
823 currentWidth = SkTMax(1, currentWidth / 2);
824 currentHeight = SkTMax(1, currentHeight / 2);
825
826 if (texels[currentMipLevel].fPixels) {
827 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
828 currentHeight);
829 individualMipOffsets.push_back(combinedBufferSize);
830 combinedBufferSize += dataSize;
831 } else {
832 return false;
833 }
834 }
835 if (0 == combinedBufferSize) {
836 // We don't have any data to upload so fail (compressed textures are read-only).
837 return false;
838 }
839
840 // allocate buffer to hold our mip data
Brian Salomon12d22642019-01-29 14:38:50 -0500841 sk_sp<GrVkTransferBuffer> transferBuffer =
842 GrVkTransferBuffer::Make(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500843 if (!transferBuffer) {
844 return false;
845 }
846
847 int uploadLeft = left;
848 int uploadTop = top;
849 GrVkTexture* uploadTexture = tex;
850
851 char* buffer = (char*)transferBuffer->map();
852 SkTArray<VkBufferImageCopy> regions(mipLevelCount);
853
854 currentWidth = width;
855 currentHeight = height;
856 int layerHeight = uploadTexture->height();
857 for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
858 if (texels[currentMipLevel].fPixels) {
859 // Again, we're assuming that our rect is the entire texture
860 SkASSERT(currentHeight == layerHeight);
861 SkASSERT(0 == uploadLeft && 0 == uploadTop);
862
863 const size_t dataSize = GrCompressedFormatDataSize(tex->config(), currentWidth,
864 currentHeight);
865
866 // copy data into the buffer, skipping the trailing bytes
867 char* dst = buffer + individualMipOffsets[currentMipLevel];
868 const char* src = (const char*)texels[currentMipLevel].fPixels;
869 memcpy(dst, src, dataSize);
870
871 VkBufferImageCopy& region = regions.push_back();
872 memset(&region, 0, sizeof(VkBufferImageCopy));
873 region.bufferOffset = transferBuffer->offset() + individualMipOffsets[currentMipLevel];
874 region.bufferRowLength = currentWidth;
875 region.bufferImageHeight = currentHeight;
876 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 };
877 region.imageOffset = { uploadLeft, uploadTop, 0 };
878 region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 };
879 }
880 currentWidth = SkTMax(1, currentWidth / 2);
881 currentHeight = SkTMax(1, currentHeight / 2);
882 layerHeight = currentHeight;
883 }
884
885 // no need to flush non-coherent memory, unmap will do that for us
886 transferBuffer->unmap();
887
888 // Change layout of our target so it can be copied to
889 uploadTexture->setImageLayout(this,
890 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
891 VK_ACCESS_TRANSFER_WRITE_BIT,
892 VK_PIPELINE_STAGE_TRANSFER_BIT,
893 false);
894
895 // Copy the buffer to the image
896 fCurrentCmdBuffer->copyBufferToImage(this,
Brian Salomon12d22642019-01-29 14:38:50 -0500897 transferBuffer.get(),
Jim Van Verth1676cb92019-01-15 13:24:45 -0500898 uploadTexture,
899 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
900 regions.count(),
901 regions.begin());
Jim Van Verth1676cb92019-01-15 13:24:45 -0500902
903 if (1 == mipLevelCount) {
904 tex->texturePriv().markMipMapsDirty();
905 }
906
907 return true;
908}
909
Greg Daniel164a9f02016-02-22 09:56:40 -0500910////////////////////////////////////////////////////////////////////////////////
Robert Phillips67d52cf2017-06-05 13:38:13 -0400911sk_sp<GrTexture> GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -0500912 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500913 bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
914
915 VkFormat pixelFormat;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500916 SkAssertResult(GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat));
egdaniel0a3a7f72016-06-24 09:22:31 -0700917
Greg Daniel164a9f02016-02-22 09:56:40 -0500918 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
919 if (renderTarget) {
920 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
921 }
922
923 // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and
924 // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we
925 // will be using this texture in some copy or not. Also this assumes, as is the current case,
jvanverth62340062016-04-26 08:01:44 -0700926 // that all render targets in vulkan are also textures. If we change this practice of setting
Greg Daniel164a9f02016-02-22 09:56:40 -0500927 // both bits, we must make sure to set the destination bit if we are uploading srcData to the
928 // texture.
929 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
930
Greg Daniel164a9f02016-02-22 09:56:40 -0500931 // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is
jvanverth62340062016-04-26 08:01:44 -0700932 // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set
Greg Daniel164a9f02016-02-22 09:56:40 -0500933 // to 1.
Robert Phillips590533f2017-07-11 14:22:35 -0400934 int mipLevels = !mipLevelCount ? 1 : mipLevelCount;
Greg Daniel164a9f02016-02-22 09:56:40 -0500935 GrVkImage::ImageDesc imageDesc;
936 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
937 imageDesc.fFormat = pixelFormat;
938 imageDesc.fWidth = desc.fWidth;
939 imageDesc.fHeight = desc.fHeight;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400940 imageDesc.fLevels = mipLevels;
Greg Daniel164a9f02016-02-22 09:56:40 -0500941 imageDesc.fSamples = 1;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400942 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
Greg Daniel164a9f02016-02-22 09:56:40 -0500943 imageDesc.fUsageFlags = usageFlags;
Brian Salomon7128fdd2017-05-22 14:00:07 -0400944 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
Greg Daniel164a9f02016-02-22 09:56:40 -0500945
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400946 GrMipMapsStatus mipMapsStatus = GrMipMapsStatus::kNotAllocated;
947 if (mipLevels > 1) {
948 mipMapsStatus = GrMipMapsStatus::kValid;
949 for (int i = 0; i < mipLevels; ++i) {
950 if (!texels[i].fPixels) {
951 mipMapsStatus = GrMipMapsStatus::kDirty;
952 break;
953 }
Greg Daniel834f1202017-10-09 15:06:20 -0400954 }
955 }
956
Robert Phillips67d52cf2017-06-05 13:38:13 -0400957 sk_sp<GrVkTexture> tex;
Greg Daniel164a9f02016-02-22 09:56:40 -0500958 if (renderTarget) {
Greg Daniel475eb702018-09-28 14:16:50 -0400959 tex = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(this, budgeted, desc,
960 imageDesc,
961 mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500962 } else {
Greg Daniel475eb702018-09-28 14:16:50 -0400963 tex = GrVkTexture::MakeNewTexture(this, budgeted, desc, imageDesc, mipMapsStatus);
Greg Daniel164a9f02016-02-22 09:56:40 -0500964 }
965
966 if (!tex) {
967 return nullptr;
968 }
969
Jim Van Verth1676cb92019-01-15 13:24:45 -0500970 bool isCompressed = GrPixelConfigIsCompressed(desc.fConfig);
Brian Salomonc320b152018-02-20 14:05:36 -0500971 auto colorType = GrPixelConfigToColorType(desc.fConfig);
Robert Phillips590533f2017-07-11 14:22:35 -0400972 if (mipLevelCount) {
Jim Van Verth1676cb92019-01-15 13:24:45 -0500973 bool success;
974 if (isCompressed) {
975 success = this->uploadTexDataCompressed(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
976 colorType, texels, mipLevelCount);
977 } else {
978 success = this->uploadTexDataOptimal(tex.get(), 0, 0, desc.fWidth, desc.fHeight,
979 colorType, texels, mipLevelCount);
980 }
981 if (!success) {
Greg Daniel164a9f02016-02-22 09:56:40 -0500982 tex->unref();
983 return nullptr;
984 }
985 }
986
Jim Van Verth1676cb92019-01-15 13:24:45 -0500987 if (SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag) && !isCompressed) {
Brian Salomond17b4a62017-05-23 16:53:47 -0400988 VkClearColorValue zeroClearColor;
989 memset(&zeroClearColor, 0, sizeof(zeroClearColor));
990 VkImageSubresourceRange range;
991 range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
992 range.baseArrayLayer = 0;
993 range.baseMipLevel = 0;
994 range.layerCount = 1;
995 range.levelCount = 1;
996 tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
997 VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false);
Robert Phillips67d52cf2017-06-05 13:38:13 -0400998 this->currentCommandBuffer()->clearColorImage(this, tex.get(), &zeroClearColor, 1, &range);
Brian Salomond17b4a62017-05-23 16:53:47 -0400999 }
Ben Wagnerff134f22018-04-24 16:29:16 -04001000 return std::move(tex);
Greg Daniel164a9f02016-02-22 09:56:40 -05001001}
1002
1003////////////////////////////////////////////////////////////////////////////////
1004
Greg Daniel6888c0d2017-08-25 11:55:50 -04001005void GrVkGpu::copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
1006 VkDeviceSize dstOffset, VkDeviceSize size) {
1007 VkBufferCopy copyRegion;
1008 copyRegion.srcOffset = srcOffset;
1009 copyRegion.dstOffset = dstOffset;
1010 copyRegion.size = size;
1011 fCurrentCmdBuffer->copyBuffer(this, srcBuffer, dstBuffer, 1, &copyRegion);
1012}
1013
jvanverthdb379092016-07-07 11:18:46 -07001014bool GrVkGpu::updateBuffer(GrVkBuffer* buffer, const void* src,
1015 VkDeviceSize offset, VkDeviceSize size) {
jvanvertha584de92016-06-30 09:10:52 -07001016 // Update the buffer
jvanverthdb379092016-07-07 11:18:46 -07001017 fCurrentCmdBuffer->updateBuffer(this, buffer, offset, size, src);
jvanvertha584de92016-06-30 09:10:52 -07001018
1019 return true;
1020}
1021
1022////////////////////////////////////////////////////////////////////////////////
1023
Greg Daniel7e000222018-12-03 10:08:21 -05001024static bool check_image_info(const GrVkCaps& caps,
1025 const GrVkImageInfo& info,
1026 GrPixelConfig config) {
1027 if (VK_NULL_HANDLE == info.fImage || VK_NULL_HANDLE == info.fAlloc.fMemory) {
Brian Salomond17f6582017-07-19 18:28:58 -04001028 return false;
Greg Daniel164a9f02016-02-22 09:56:40 -05001029 }
1030
Greg Daniel7e000222018-12-03 10:08:21 -05001031 if (info.fYcbcrConversionInfo.isValid()) {
1032 if (!caps.supportsYcbcrConversion() || info.fFormat != VK_NULL_HANDLE) {
1033 return false;
1034 }
jvanverthfd359ca2016-03-18 11:57:24 -07001035 }
Greg Daniel7ef28f32017-04-20 16:41:55 +00001036
Greg Daniel52e16d92018-04-10 09:34:07 -04001037 SkASSERT(GrVkFormatPixelConfigPairIsValid(info.fFormat, config));
Brian Salomond17f6582017-07-19 18:28:58 -04001038 return true;
1039}
1040
1041sk_sp<GrTexture> GrVkGpu::onWrapBackendTexture(const GrBackendTexture& backendTex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001042 GrWrapOwnership ownership, GrWrapCacheable cacheable,
1043 GrIOType ioType) {
Greg Daniel7e000222018-12-03 10:08:21 -05001044 GrVkImageInfo imageInfo;
1045 if (!backendTex.getVkImageInfo(&imageInfo)) {
1046 return nullptr;
1047 }
1048
1049 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -04001050 return nullptr;
1051 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001052
Greg Daniel164a9f02016-02-22 09:56:40 -05001053 GrSurfaceDesc surfDesc;
Brian Salomond17f6582017-07-19 18:28:58 -04001054 surfDesc.fFlags = kNone_GrSurfaceFlags;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001055 surfDesc.fWidth = backendTex.width();
1056 surfDesc.fHeight = backendTex.height();
1057 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001058 surfDesc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001059
Greg Daniel52e16d92018-04-10 09:34:07 -04001060 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1061 SkASSERT(layout);
Brian Salomonfa2ebea2019-01-24 15:58:58 -05001062 return GrVkTexture::MakeWrappedTexture(this, surfDesc, ownership, cacheable, ioType, imageInfo,
1063 std::move(layout));
Brian Salomond17f6582017-07-19 18:28:58 -04001064}
1065
1066sk_sp<GrTexture> GrVkGpu::onWrapRenderableBackendTexture(const GrBackendTexture& backendTex,
Brian Salomond17f6582017-07-19 18:28:58 -04001067 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001068 GrWrapOwnership ownership,
1069 GrWrapCacheable cacheable) {
Greg Daniel7e000222018-12-03 10:08:21 -05001070 GrVkImageInfo imageInfo;
1071 if (!backendTex.getVkImageInfo(&imageInfo)) {
1072 return nullptr;
1073 }
1074
1075 if (!check_image_info(this->vkCaps(), imageInfo, backendTex.config())) {
Brian Salomond17f6582017-07-19 18:28:58 -04001076 return nullptr;
Greg Daniel164a9f02016-02-22 09:56:40 -05001077 }
Brian Salomond17f6582017-07-19 18:28:58 -04001078
1079 GrSurfaceDesc surfDesc;
1080 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
1081 surfDesc.fWidth = backendTex.width();
1082 surfDesc.fHeight = backendTex.height();
1083 surfDesc.fConfig = backendTex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001084 surfDesc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config());
Brian Salomond17f6582017-07-19 18:28:58 -04001085
Greg Daniel52e16d92018-04-10 09:34:07 -04001086 sk_sp<GrVkImageLayout> layout = backendTex.getGrVkImageLayout();
1087 SkASSERT(layout);
1088
Brian Salomonaa6ca0a2019-01-24 16:03:07 -05001089 return GrVkTextureRenderTarget::MakeWrappedTextureRenderTarget(
1090 this, surfDesc, ownership, cacheable, imageInfo, std::move(layout));
Greg Daniel164a9f02016-02-22 09:56:40 -05001091}
1092
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001093sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& backendRT){
Greg Daniele79b4732017-04-20 14:07:46 -04001094 // Currently the Vulkan backend does not support wrapping of msaa render targets directly. In
1095 // general this is not an issue since swapchain images in vulkan are never multisampled. Thus if
1096 // you want a multisampled RT it is best to wrap the swapchain images and then let Skia handle
1097 // creating and owning the MSAA images.
Brian Salomonbdecacf2018-02-02 20:32:49 -05001098 if (backendRT.sampleCnt() > 1) {
Greg Daniele79b4732017-04-20 14:07:46 -04001099 return nullptr;
1100 }
halcanary9d524f22016-03-29 09:03:52 -07001101
Greg Daniel323fbcf2018-04-10 13:46:30 -04001102 GrVkImageInfo info;
1103 if (!backendRT.getVkImageInfo(&info)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001104 return nullptr;
1105 }
Greg Daniel323fbcf2018-04-10 13:46:30 -04001106
1107 if (VK_NULL_HANDLE == info.fImage) {
jvanverthfd359ca2016-03-18 11:57:24 -07001108 return nullptr;
1109 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001110
Greg Daniel164a9f02016-02-22 09:56:40 -05001111 GrSurfaceDesc desc;
Brian Salomon0ec981b2017-05-15 13:48:50 -04001112 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillips16d8ec62017-07-27 16:16:25 -04001113 desc.fWidth = backendRT.width();
1114 desc.fHeight = backendRT.height();
1115 desc.fConfig = backendRT.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001116 desc.fSampleCnt = 1;
Greg Daniel164a9f02016-02-22 09:56:40 -05001117
Greg Daniel323fbcf2018-04-10 13:46:30 -04001118 sk_sp<GrVkImageLayout> layout = backendRT.getGrVkImageLayout();
Greg Daniel52e16d92018-04-10 09:34:07 -04001119
Greg Daniel323fbcf2018-04-10 13:46:30 -04001120 sk_sp<GrVkRenderTarget> tgt = GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, info,
Greg Daniel52e16d92018-04-10 09:34:07 -04001121 std::move(layout));
Brian Salomonafdc6b12018-03-09 12:02:32 -05001122
1123 // We don't allow the client to supply a premade stencil buffer. We always create one if needed.
1124 SkASSERT(!backendRT.stencilBits());
1125 if (tgt) {
1126 SkASSERT(tgt->canAttemptStencilAttachment());
Greg Daniel164a9f02016-02-22 09:56:40 -05001127 }
Brian Salomonafdc6b12018-03-09 12:02:32 -05001128
Ben Wagnerff134f22018-04-24 16:29:16 -04001129 return std::move(tgt);
Greg Daniel164a9f02016-02-22 09:56:40 -05001130}
1131
Greg Daniel7ef28f32017-04-20 16:41:55 +00001132sk_sp<GrRenderTarget> GrVkGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001133 int sampleCnt) {
Brian Osman33910292017-04-18 14:38:53 -04001134
Greg Daniel52e16d92018-04-10 09:34:07 -04001135 GrVkImageInfo imageInfo;
1136 if (!tex.getVkImageInfo(&imageInfo)) {
Greg Danielbcf612b2017-05-01 13:50:58 +00001137 return nullptr;
1138 }
Greg Daniel52e16d92018-04-10 09:34:07 -04001139 if (VK_NULL_HANDLE == imageInfo.fImage) {
Brian Osman33910292017-04-18 14:38:53 -04001140 return nullptr;
1141 }
1142
1143 GrSurfaceDesc desc;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001144 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Greg Daniel7ef28f32017-04-20 16:41:55 +00001145 desc.fWidth = tex.width();
1146 desc.fHeight = tex.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -04001147 desc.fConfig = tex.config();
Brian Salomonbdecacf2018-02-02 20:32:49 -05001148 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config());
1149 if (!desc.fSampleCnt) {
1150 return nullptr;
1151 }
Brian Osman33910292017-04-18 14:38:53 -04001152
Greg Daniel52e16d92018-04-10 09:34:07 -04001153 sk_sp<GrVkImageLayout> layout = tex.getGrVkImageLayout();
1154 SkASSERT(layout);
1155
Ben Wagnerff134f22018-04-24 16:29:16 -04001156 return GrVkRenderTarget::MakeWrappedRenderTarget(this, desc, imageInfo, std::move(layout));
Brian Osman33910292017-04-18 14:38:53 -04001157}
1158
Greg Danielb46add82019-01-02 14:51:29 -05001159sk_sp<GrRenderTarget> GrVkGpu::onWrapVulkanSecondaryCBAsRenderTarget(
1160 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo) {
1161 int maxSize = this->caps()->maxTextureSize();
1162 if (imageInfo.width() > maxSize || imageInfo.height() > maxSize) {
1163 return nullptr;
1164 }
1165
1166 GrBackendFormat backendFormat = GrBackendFormat::MakeVk(vkInfo.fFormat);
1167 if (!backendFormat.isValid()) {
1168 return nullptr;
1169 }
1170 GrPixelConfig config = this->caps()->getConfigFromBackendFormat(backendFormat,
1171 imageInfo.colorType());
1172 if (config == kUnknown_GrPixelConfig) {
1173 return nullptr;
1174 }
1175
1176 GrSurfaceDesc desc;
1177 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1178 desc.fWidth = imageInfo.width();
1179 desc.fHeight = imageInfo.height();
1180 desc.fConfig = config;
1181 desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(1, config);
1182 if (!desc.fSampleCnt) {
1183 return nullptr;
1184 }
1185
1186 return GrVkRenderTarget::MakeSecondaryCBRenderTarget(this, desc, vkInfo);
1187}
1188
Brian Salomon930f9392018-06-20 16:25:26 -04001189bool GrVkGpu::onRegenerateMipMapLevels(GrTexture* tex) {
1190 auto* vkTex = static_cast<GrVkTexture*>(tex);
jvanverth900bd4a2016-04-29 13:53:12 -07001191 // don't do anything for linearly tiled textures (can't have mipmaps)
Brian Salomon930f9392018-06-20 16:25:26 -04001192 if (vkTex->isLinearTiled()) {
jvanverth900bd4a2016-04-29 13:53:12 -07001193 SkDebugf("Trying to create mipmap for linear tiled texture");
Brian Salomon930f9392018-06-20 16:25:26 -04001194 return false;
jvanverth62340062016-04-26 08:01:44 -07001195 }
1196
jvanverth62340062016-04-26 08:01:44 -07001197 // determine if we can blit to and from this format
1198 const GrVkCaps& caps = this->vkCaps();
1199 if (!caps.configCanBeDstofBlit(tex->config(), false) ||
egdaniel2f5792a2016-07-06 08:51:23 -07001200 !caps.configCanBeSrcofBlit(tex->config(), false) ||
1201 !caps.mipMapSupport()) {
Brian Salomon930f9392018-06-20 16:25:26 -04001202 return false;
jvanverth62340062016-04-26 08:01:44 -07001203 }
1204
egdaniel7ac5da82016-07-15 13:41:42 -07001205 int width = tex->width();
1206 int height = tex->height();
1207 VkImageBlit blitRegion;
1208 memset(&blitRegion, 0, sizeof(VkImageBlit));
jvanverth62340062016-04-26 08:01:44 -07001209
jvanverth82c05582016-05-03 11:19:01 -07001210 // SkMipMap doesn't include the base level in the level count so we have to add 1
1211 uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1;
Brian Salomon930f9392018-06-20 16:25:26 -04001212 SkASSERT(levelCount == vkTex->mipLevels());
egdaniel7ac5da82016-07-15 13:41:42 -07001213
Greg Danielda86e282018-06-13 09:41:19 -04001214 // change layout of the layers so we can write to them.
Brian Salomon930f9392018-06-20 16:25:26 -04001215 vkTex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT,
1216 VK_PIPELINE_STAGE_TRANSFER_BIT, false);
jvanverth62340062016-04-26 08:01:44 -07001217
jvanverth50c46c72016-05-06 12:31:28 -07001218 // setup memory barrier
Brian Salomon930f9392018-06-20 16:25:26 -04001219 SkASSERT(GrVkFormatIsSupported(vkTex->imageFormat()));
jvanverth50c46c72016-05-06 12:31:28 -07001220 VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
1221 VkImageMemoryBarrier imageMemoryBarrier = {
Brian Salomon930f9392018-06-20 16:25:26 -04001222 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
1223 nullptr, // pNext
1224 VK_ACCESS_TRANSFER_WRITE_BIT, // srcAccessMask
1225 VK_ACCESS_TRANSFER_READ_BIT, // dstAccessMask
1226 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, // oldLayout
1227 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, // newLayout
1228 VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex
1229 VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex
1230 vkTex->image(), // image
1231 {aspectFlags, 0, 1, 0, 1} // subresourceRange
jvanverth50c46c72016-05-06 12:31:28 -07001232 };
1233
jvanverth62340062016-04-26 08:01:44 -07001234 // Blit the miplevels
jvanverth82c05582016-05-03 11:19:01 -07001235 uint32_t mipLevel = 1;
1236 while (mipLevel < levelCount) {
1237 int prevWidth = width;
1238 int prevHeight = height;
1239 width = SkTMax(1, width / 2);
1240 height = SkTMax(1, height / 2);
jvanverth62340062016-04-26 08:01:44 -07001241
jvanverth50c46c72016-05-06 12:31:28 -07001242 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1243 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1244 false, &imageMemoryBarrier);
1245
1246 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001247 blitRegion.srcOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001248 blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 1 };
jvanverth82c05582016-05-03 11:19:01 -07001249 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 };
1250 blitRegion.dstOffsets[0] = { 0, 0, 0 };
brianosmane9906e72016-06-08 12:44:27 -07001251 blitRegion.dstOffsets[1] = { width, height, 1 };
jvanverth62340062016-04-26 08:01:44 -07001252 fCurrentCmdBuffer->blitImage(this,
Brian Salomon930f9392018-06-20 16:25:26 -04001253 vkTex->resource(),
1254 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001255 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon930f9392018-06-20 16:25:26 -04001256 vkTex->resource(),
1257 vkTex->image(),
Greg Daniel31cc7312018-03-05 11:41:06 -05001258 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth62340062016-04-26 08:01:44 -07001259 1,
1260 &blitRegion,
1261 VK_FILTER_LINEAR);
jvanverth82c05582016-05-03 11:19:01 -07001262 ++mipLevel;
jvanverth62340062016-04-26 08:01:44 -07001263 }
Greg Daniel31cc7312018-03-05 11:41:06 -05001264 // This barrier logically is not needed, but it changes the final level to the same layout as
1265 // all the others, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL. This makes tracking of the layouts and
1266 // future layout changes easier.
1267 imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1;
1268 this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1269 false, &imageMemoryBarrier);
Brian Salomon930f9392018-06-20 16:25:26 -04001270 vkTex->updateImageLayout(VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
1271 return true;
jvanverth62340062016-04-26 08:01:44 -07001272}
1273
Greg Daniel164a9f02016-02-22 09:56:40 -05001274////////////////////////////////////////////////////////////////////////////////
1275
1276GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
1277 int width,
1278 int height) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001279 SkASSERT(width >= rt->width());
1280 SkASSERT(height >= rt->height());
1281
1282 int samples = rt->numStencilSamples();
1283
Ethan Nicholasf610bae2018-09-20 16:55:21 -04001284 const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferredStencilFormat();
Greg Daniel164a9f02016-02-22 09:56:40 -05001285
1286 GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this,
Greg Daniel164a9f02016-02-22 09:56:40 -05001287 width,
1288 height,
1289 samples,
1290 sFmt));
1291 fStats.incStencilAttachmentCreates();
1292 return stencil;
1293}
1294
1295////////////////////////////////////////////////////////////////////////////////
1296
Brian Salomon52e943a2018-03-13 09:32:39 -04001297bool copy_testing_data(GrVkGpu* gpu, const void* srcData, const GrVkAlloc& alloc,
Robert Phillips646f6372018-09-25 09:31:10 -04001298 size_t bufferOffset, size_t srcRowBytes, size_t dstRowBytes,
1299 size_t trimRowBytes, int h) {
Greg Daniel81df0412018-05-31 13:13:33 -04001300 VkDeviceSize size = dstRowBytes * h;
1301 VkDeviceSize offset = bufferOffset;
1302 SkASSERT(size + offset <= alloc.fSize);
1303 void* mapPtr = GrVkMemory::MapAlloc(gpu, alloc);
1304 if (!mapPtr) {
egdaniel3602d4f2016-08-12 11:58:53 -07001305 return false;
1306 }
Greg Daniel81df0412018-05-31 13:13:33 -04001307 mapPtr = reinterpret_cast<char*>(mapPtr) + offset;
egdaniel3602d4f2016-08-12 11:58:53 -07001308
Greg Daniel20ece3a2017-03-28 10:24:43 -04001309 if (srcData) {
1310 // If there is no padding on dst we can do a single memcopy.
1311 // This assumes the srcData comes in with no padding.
Robert Phillips646f6372018-09-25 09:31:10 -04001312 SkRectMemcpy(mapPtr, dstRowBytes, srcData, srcRowBytes, trimRowBytes, h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001313 } else {
1314 // If there is no srcdata we always copy 0's into the textures so that it is initialized
1315 // with some data.
Robert Phillips646f6372018-09-25 09:31:10 -04001316 memset(mapPtr, 0, dstRowBytes * h);
Greg Daniel20ece3a2017-03-28 10:24:43 -04001317 }
Greg Daniel81df0412018-05-31 13:13:33 -04001318 GrVkMemory::FlushMappedAlloc(gpu, alloc, offset, size);
1319 GrVkMemory::UnmapAlloc(gpu, alloc);
egdaniel3602d4f2016-08-12 11:58:53 -07001320 return true;
1321}
1322
Brian Salomonf865b052018-03-09 09:01:53 -05001323#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -04001324bool GrVkGpu::createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
1325 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001326 size_t srcRowBytes, GrVkImageInfo* info) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001327 SkASSERT(texturable || renderable);
1328 if (!texturable) {
1329 SkASSERT(GrMipMapped::kNo == mipMapped);
1330 SkASSERT(!srcData);
1331 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001332 VkFormat pixelFormat;
1333 if (!GrPixelConfigToVkFormat(config, &pixelFormat)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001334 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001335 }
1336
Brian Salomon52e943a2018-03-13 09:32:39 -04001337 if (texturable && !fVkCaps->isConfigTexturable(config)) {
1338 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001339 }
1340
Brian Salomon52e943a2018-03-13 09:32:39 -04001341 if (renderable && !fVkCaps->isConfigRenderable(config)) {
1342 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001343 }
1344
1345 // Currently we don't support uploading pixel data when mipped.
1346 if (srcData && GrMipMapped::kYes == mipMapped) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001347 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001348 }
1349
Brian Salomon52e943a2018-03-13 09:32:39 -04001350 VkImageUsageFlags usageFlags = 0;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001351 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1352 usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Brian Salomon52e943a2018-03-13 09:32:39 -04001353 if (texturable) {
1354 usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
1355 }
1356 if (renderable) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001357 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
1358 }
1359
1360 VkImage image = VK_NULL_HANDLE;
Greg Daniel8385a8a2018-02-26 13:29:37 -05001361 GrVkAlloc alloc;
Brian Salomonde9f5462018-03-07 14:23:58 -05001362 VkImageLayout initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001363
1364 // Create Image
1365 VkSampleCountFlagBits vkSamples;
1366 if (!GrSampleCountToVkSampleCount(1, &vkSamples)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001367 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001368 }
1369
1370 // Figure out the number of mip levels.
1371 uint32_t mipLevels = 1;
1372 if (GrMipMapped::kYes == mipMapped) {
1373 mipLevels = SkMipMap::ComputeLevelCount(w, h) + 1;
1374 }
1375
1376 const VkImageCreateInfo imageCreateInfo = {
Brian Salomonde9f5462018-03-07 14:23:58 -05001377 VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType
1378 nullptr, // pNext
Brian Osman2b23c4b2018-06-01 12:25:08 -04001379 0, // VkImageCreateFlags
Brian Salomonde9f5462018-03-07 14:23:58 -05001380 VK_IMAGE_TYPE_2D, // VkImageType
1381 pixelFormat, // VkFormat
1382 {(uint32_t)w, (uint32_t)h, 1}, // VkExtent3D
1383 mipLevels, // mipLevels
1384 1, // arrayLayers
1385 vkSamples, // samples
1386 VK_IMAGE_TILING_OPTIMAL, // VkImageTiling
1387 usageFlags, // VkImageUsageFlags
1388 VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode
1389 0, // queueFamilyCount
1390 0, // pQueueFamilyIndices
1391 initialLayout // initialLayout
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001392 };
1393
Brian Salomon52e943a2018-03-13 09:32:39 -04001394 GR_VK_CALL_ERRCHECK(this->vkInterface(),
1395 CreateImage(this->device(), &imageCreateInfo, nullptr, &image));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001396
Brian Salomonde9f5462018-03-07 14:23:58 -05001397 if (!GrVkMemory::AllocAndBindImageMemory(this, image, false, &alloc)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001398 VK_CALL(DestroyImage(this->device(), image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001399 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001400 }
1401
1402 // 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 -05001403 GrVkAlloc bufferAlloc;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001404 VkBuffer buffer = VK_NULL_HANDLE;
1405
1406 VkResult err;
1407 const VkCommandBufferAllocateInfo cmdInfo = {
1408 VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, // sType
1409 nullptr, // pNext
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001410 fCmdPool->vkCommandPool(), // commandPool
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001411 VK_COMMAND_BUFFER_LEVEL_PRIMARY, // level
1412 1 // bufferCount
1413 };
1414
1415 VkCommandBuffer cmdBuffer;
1416 err = VK_CALL(AllocateCommandBuffers(fDevice, &cmdInfo, &cmdBuffer));
1417 if (err) {
1418 GrVkMemory::FreeImageMemory(this, false, alloc);
1419 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomon52e943a2018-03-13 09:32:39 -04001420 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001421 }
1422
1423 VkCommandBufferBeginInfo cmdBufferBeginInfo;
1424 memset(&cmdBufferBeginInfo, 0, sizeof(VkCommandBufferBeginInfo));
1425 cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
1426 cmdBufferBeginInfo.pNext = nullptr;
1427 cmdBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
1428 cmdBufferBeginInfo.pInheritanceInfo = nullptr;
1429
1430 err = VK_CALL(BeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo));
1431 SkASSERT(!err);
1432
1433 size_t bpp = GrBytesPerPixel(config);
Brian Salomonde9f5462018-03-07 14:23:58 -05001434 SkASSERT(w && h);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001435
Robert Phillips646f6372018-09-25 09:31:10 -04001436 const size_t trimRowBytes = w * bpp;
1437 if (!srcRowBytes) {
1438 srcRowBytes = trimRowBytes;
1439 }
1440
Brian Salomonde9f5462018-03-07 14:23:58 -05001441 SkTArray<size_t> individualMipOffsets(mipLevels);
1442 individualMipOffsets.push_back(0);
1443 size_t combinedBufferSize = w * bpp * h;
Jim Van Verth1676cb92019-01-15 13:24:45 -05001444 if (GrPixelConfigIsCompressed(config)) {
1445 combinedBufferSize = GrCompressedFormatDataSize(config, w, h);
1446 bpp = 4; // we have at least this alignment, which will pass the code below
1447 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001448 int currentWidth = w;
1449 int currentHeight = h;
1450 // The alignment must be at least 4 bytes and a multiple of the bytes per pixel of the image
1451 // config. This works with the assumption that the bytes in pixel config is always a power
1452 // of 2.
1453 SkASSERT((bpp & (bpp - 1)) == 0);
1454 const size_t alignmentMask = 0x3 | (bpp - 1);
1455 for (uint32_t currentMipLevel = 1; currentMipLevel < mipLevels; currentMipLevel++) {
1456 currentWidth = SkTMax(1, currentWidth / 2);
1457 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001458
Jim Van Verth1676cb92019-01-15 13:24:45 -05001459 size_t trimmedSize;
1460 if (GrPixelConfigIsCompressed(config)) {
1461 trimmedSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1462 } else {
1463 trimmedSize = currentWidth * bpp * currentHeight;
1464 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001465 const size_t alignmentDiff = combinedBufferSize & alignmentMask;
1466 if (alignmentDiff != 0) {
1467 combinedBufferSize += alignmentMask - alignmentDiff + 1;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001468 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001469 individualMipOffsets.push_back(combinedBufferSize);
1470 combinedBufferSize += trimmedSize;
1471 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001472
Brian Salomonde9f5462018-03-07 14:23:58 -05001473 VkBufferCreateInfo bufInfo;
1474 memset(&bufInfo, 0, sizeof(VkBufferCreateInfo));
1475 bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
1476 bufInfo.flags = 0;
1477 bufInfo.size = combinedBufferSize;
1478 bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1479 bufInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
1480 bufInfo.queueFamilyIndexCount = 0;
1481 bufInfo.pQueueFamilyIndices = nullptr;
1482 err = VK_CALL(CreateBuffer(fDevice, &bufInfo, nullptr, &buffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001483
Brian Salomonde9f5462018-03-07 14:23:58 -05001484 if (err) {
1485 GrVkMemory::FreeImageMemory(this, false, alloc);
1486 VK_CALL(DestroyImage(fDevice, image, nullptr));
1487 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001488 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001489 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001490 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001491
Brian Salomonde9f5462018-03-07 14:23:58 -05001492 if (!GrVkMemory::AllocAndBindBufferMemory(this, buffer, GrVkBuffer::kCopyRead_Type, true,
1493 &bufferAlloc)) {
1494 GrVkMemory::FreeImageMemory(this, false, alloc);
1495 VK_CALL(DestroyImage(fDevice, image, nullptr));
1496 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1497 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001498 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001499 return false;
Brian Salomonde9f5462018-03-07 14:23:58 -05001500 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001501
Brian Salomonde9f5462018-03-07 14:23:58 -05001502 currentWidth = w;
1503 currentHeight = h;
1504 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1505 SkASSERT(0 == currentMipLevel || !srcData);
Brian Salomonde9f5462018-03-07 14:23:58 -05001506 size_t bufferOffset = individualMipOffsets[currentMipLevel];
Jim Van Verth1676cb92019-01-15 13:24:45 -05001507 bool result;
1508 if (GrPixelConfigIsCompressed(config)) {
1509 size_t levelSize = GrCompressedFormatDataSize(config, currentWidth, currentHeight);
1510 size_t currentRowBytes = levelSize / currentHeight;
1511 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, currentRowBytes,
1512 currentRowBytes, currentRowBytes, currentHeight);
1513 } else {
1514 size_t currentRowBytes = bpp * currentWidth;
1515 result = copy_testing_data(this, srcData, bufferAlloc, bufferOffset, srcRowBytes,
1516 currentRowBytes, trimRowBytes, currentHeight);
1517 }
1518 if (!result) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001519 GrVkMemory::FreeImageMemory(this, false, alloc);
1520 VK_CALL(DestroyImage(fDevice, image, nullptr));
Brian Salomonde9f5462018-03-07 14:23:58 -05001521 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001522 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1523 VK_CALL(EndCommandBuffer(cmdBuffer));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001524 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Brian Salomon52e943a2018-03-13 09:32:39 -04001525 return false;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001526 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001527 currentWidth = SkTMax(1, currentWidth / 2);
1528 currentHeight = SkTMax(1, currentHeight / 2);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001529 }
Brian Salomonde9f5462018-03-07 14:23:58 -05001530
1531 // Set image layout and add barrier
1532 VkImageMemoryBarrier barrier;
1533 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1534 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1535 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001536 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomonde9f5462018-03-07 14:23:58 -05001537 barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
1538 barrier.oldLayout = initialLayout;
1539 barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1540 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1541 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1542 barrier.image = image;
1543 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
1544
Greg Danielf7828d02018-10-09 12:01:32 -04001545 VK_CALL(CmdPipelineBarrier(cmdBuffer, GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
Brian Salomonde9f5462018-03-07 14:23:58 -05001546 VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1,
1547 &barrier));
1548 initialLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1549
1550 SkTArray<VkBufferImageCopy> regions(mipLevels);
1551
1552 currentWidth = w;
1553 currentHeight = h;
1554 for (uint32_t currentMipLevel = 0; currentMipLevel < mipLevels; currentMipLevel++) {
1555 // Submit copy command
1556 VkBufferImageCopy& region = regions.push_back();
1557 memset(&region, 0, sizeof(VkBufferImageCopy));
1558 region.bufferOffset = individualMipOffsets[currentMipLevel];
1559 region.bufferRowLength = currentWidth;
1560 region.bufferImageHeight = currentHeight;
1561 region.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
1562 region.imageOffset = {0, 0, 0};
1563 region.imageExtent = {(uint32_t)currentWidth, (uint32_t)currentHeight, 1};
1564 currentWidth = SkTMax(1, currentWidth / 2);
1565 currentHeight = SkTMax(1, currentHeight / 2);
1566 }
1567
1568 VK_CALL(CmdCopyBufferToImage(cmdBuffer, buffer, image, initialLayout, regions.count(),
1569 regions.begin()));
1570
Brian Salomon52e943a2018-03-13 09:32:39 -04001571 if (texturable) {
1572 // Change Image layout to shader read since if we use this texture as a borrowed textures
1573 // within Ganesh we require that its layout be set to that
1574 memset(&barrier, 0, sizeof(VkImageMemoryBarrier));
1575 barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1576 barrier.pNext = nullptr;
Greg Daniel6ddbafc2018-05-24 12:34:29 -04001577 barrier.srcAccessMask = GrVkImage::LayoutToSrcAccessMask(initialLayout);
Brian Salomon52e943a2018-03-13 09:32:39 -04001578 barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
1579 barrier.oldLayout = initialLayout;
1580 barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
1581 barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1582 barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1583 barrier.image = image;
1584 barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, mipLevels, 0, 1};
Brian Salomon52e943a2018-03-13 09:32:39 -04001585 VK_CALL(CmdPipelineBarrier(cmdBuffer,
Greg Danielf7828d02018-10-09 12:01:32 -04001586 GrVkImage::LayoutToPipelineSrcStageFlags(initialLayout),
1587 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Salomon52e943a2018-03-13 09:32:39 -04001588 0,
1589 0, nullptr,
1590 0, nullptr,
1591 1, &barrier));
Greg Daniel4f4a53f2018-03-15 10:20:45 -04001592 initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
Brian Salomon52e943a2018-03-13 09:32:39 -04001593 }
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001594
1595 // End CommandBuffer
1596 err = VK_CALL(EndCommandBuffer(cmdBuffer));
1597 SkASSERT(!err);
1598
1599 // Create Fence for queue
1600 VkFence fence;
1601 VkFenceCreateInfo fenceInfo;
1602 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
1603 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
1604
1605 err = VK_CALL(CreateFence(fDevice, &fenceInfo, nullptr, &fence));
1606 SkASSERT(!err);
1607
1608 VkSubmitInfo submitInfo;
1609 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
1610 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
1611 submitInfo.pNext = nullptr;
1612 submitInfo.waitSemaphoreCount = 0;
1613 submitInfo.pWaitSemaphores = nullptr;
1614 submitInfo.pWaitDstStageMask = 0;
1615 submitInfo.commandBufferCount = 1;
1616 submitInfo.pCommandBuffers = &cmdBuffer;
1617 submitInfo.signalSemaphoreCount = 0;
1618 submitInfo.pSignalSemaphores = nullptr;
1619 err = VK_CALL(QueueSubmit(this->queue(), 1, &submitInfo, fence));
1620 SkASSERT(!err);
1621
1622 err = VK_CALL(WaitForFences(fDevice, 1, &fence, true, UINT64_MAX));
1623 if (VK_TIMEOUT == err) {
1624 GrVkMemory::FreeImageMemory(this, false, alloc);
1625 VK_CALL(DestroyImage(fDevice, image, nullptr));
1626 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1627 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001628 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001629 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1630 SkDebugf("Fence failed to signal: %d\n", err);
1631 SK_ABORT("failing");
1632 }
1633 SkASSERT(!err);
1634
1635 // Clean up transfer resources
1636 if (buffer != VK_NULL_HANDLE) { // workaround for an older NVidia driver crash
1637 GrVkMemory::FreeBufferMemory(this, GrVkBuffer::kCopyRead_Type, bufferAlloc);
1638 VK_CALL(DestroyBuffer(fDevice, buffer, nullptr));
1639 }
Ethan Nicholas8e265a72018-12-12 16:22:40 -05001640 VK_CALL(FreeCommandBuffers(fDevice, fCmdPool->vkCommandPool(), 1, &cmdBuffer));
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001641 VK_CALL(DestroyFence(fDevice, fence, nullptr));
1642
Brian Salomon52e943a2018-03-13 09:32:39 -04001643 info->fImage = image;
1644 info->fAlloc = alloc;
1645 info->fImageTiling = VK_IMAGE_TILING_OPTIMAL;
1646 info->fImageLayout = initialLayout;
1647 info->fFormat = pixelFormat;
1648 info->fLevelCount = mipLevels;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001649
Brian Salomon52e943a2018-03-13 09:32:39 -04001650 return true;
1651}
1652
1653GrBackendTexture GrVkGpu::createTestingOnlyBackendTexture(const void* srcData, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -04001654 GrColorType colorType,
1655 bool isRenderTarget,
1656 GrMipMapped mipMapped, size_t rowBytes) {
Brian Salomon8a375832018-03-14 10:21:40 -04001657 this->handleDirtyContext();
Robert Phillipsa479f962018-04-10 11:45:40 -04001658
1659 if (w > this->caps()->maxTextureSize() || h > this->caps()->maxTextureSize()) {
1660 return GrBackendTexture();
1661 }
1662
Robert Phillips646f6372018-09-25 09:31:10 -04001663 GrPixelConfig config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo);
1664 if (!this->caps()->isConfigTexturable(config)) {
1665 return GrBackendTexture();
1666 }
1667
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001668 GrVkImageInfo info;
Brian Salomon52e943a2018-03-13 09:32:39 -04001669 if (!this->createTestingOnlyVkImage(config, w, h, true, isRenderTarget, mipMapped, srcData,
Robert Phillips646f6372018-09-25 09:31:10 -04001670 rowBytes, &info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001671 return {};
1672 }
Greg Daniel108bb232018-07-03 16:18:29 -04001673 GrBackendTexture beTex = GrBackendTexture(w, h, info);
1674 // Lots of tests don't go through Skia's public interface which will set the config so for
1675 // testing we make sure we set a config here.
1676 beTex.setPixelConfig(config);
1677 return beTex;
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001678}
1679
1680bool GrVkGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001681 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001682
Greg Daniel52e16d92018-04-10 09:34:07 -04001683 GrVkImageInfo backend;
1684 if (!tex.getVkImageInfo(&backend)) {
1685 return false;
1686 }
Greg Daniel164a9f02016-02-22 09:56:40 -05001687
Greg Daniel52e16d92018-04-10 09:34:07 -04001688 if (backend.fImage && backend.fAlloc.fMemory) {
Greg Daniel164a9f02016-02-22 09:56:40 -05001689 VkMemoryRequirements req;
1690 memset(&req, 0, sizeof(req));
1691 GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice,
Greg Daniel52e16d92018-04-10 09:34:07 -04001692 backend.fImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001693 &req));
1694 // TODO: find a better check
1695 // This will probably fail with a different driver
1696 return (req.size > 0) && (req.size <= 8192 * 8192);
1697 }
1698
1699 return false;
1700}
1701
Brian Salomon26102cb2018-03-09 09:33:19 -05001702void GrVkGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001703 SkASSERT(GrBackendApi::kVulkan == tex.fBackend);
Robert Phillipsd21b2a52017-12-12 13:01:25 -05001704
Greg Daniel52e16d92018-04-10 09:34:07 -04001705 GrVkImageInfo info;
1706 if (tex.getVkImageInfo(&info)) {
Greg Daniel52e16d92018-04-10 09:34:07 -04001707 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Greg Daniel164a9f02016-02-22 09:56:40 -05001708 }
1709}
1710
Brian Osman2d010b62018-08-09 10:55:09 -04001711GrBackendRenderTarget GrVkGpu::createTestingOnlyBackendRenderTarget(int w, int h, GrColorType ct) {
Greg Daniel92cbf3f2018-04-12 16:50:17 -04001712 if (w > this->caps()->maxRenderTargetSize() || h > this->caps()->maxRenderTargetSize()) {
1713 return GrBackendRenderTarget();
1714 }
1715
Brian Salomon8a375832018-03-14 10:21:40 -04001716 this->handleDirtyContext();
Brian Salomon52e943a2018-03-13 09:32:39 -04001717 GrVkImageInfo info;
Brian Osman2d010b62018-08-09 10:55:09 -04001718 auto config = GrColorTypeToPixelConfig(ct, GrSRGBEncoded::kNo);
Brian Salomon52e943a2018-03-13 09:32:39 -04001719 if (kUnknown_GrPixelConfig == config) {
1720 return {};
1721 }
Robert Phillips646f6372018-09-25 09:31:10 -04001722 if (!this->createTestingOnlyVkImage(config, w, h, false, true, GrMipMapped::kNo, nullptr, 0,
Brian Salomon52e943a2018-03-13 09:32:39 -04001723 &info)) {
1724 return {};
1725 }
Greg Daniel108bb232018-07-03 16:18:29 -04001726 GrBackendRenderTarget beRT = GrBackendRenderTarget(w, h, 1, 0, info);
1727 // Lots of tests don't go through Skia's public interface which will set the config so for
1728 // testing we make sure we set a config here.
1729 beRT.setPixelConfig(config);
1730 return beRT;
Brian Salomonf865b052018-03-09 09:01:53 -05001731}
1732
Brian Salomon52e943a2018-03-13 09:32:39 -04001733void GrVkGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget& rt) {
Greg Danielbdf12ad2018-10-12 09:31:11 -04001734 SkASSERT(GrBackendApi::kVulkan == rt.fBackend);
Brian Salomonf865b052018-03-09 09:01:53 -05001735
Greg Daniel323fbcf2018-04-10 13:46:30 -04001736 GrVkImageInfo info;
1737 if (rt.getVkImageInfo(&info)) {
Brian Salomon52e943a2018-03-13 09:32:39 -04001738 // something in the command buffer may still be using this, so force submit
1739 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel323fbcf2018-04-10 13:46:30 -04001740 GrVkImage::DestroyImageInfo(this, const_cast<GrVkImageInfo*>(&info));
Brian Salomon52e943a2018-03-13 09:32:39 -04001741 }
1742}
Brian Salomonf865b052018-03-09 09:01:53 -05001743
Greg Daniel26b50a42018-03-08 09:49:58 -05001744void GrVkGpu::testingOnly_flushGpuAndSync() {
1745 this->submitCommandBuffer(kForce_SyncQueue);
1746}
Brian Salomonf865b052018-03-09 09:01:53 -05001747#endif
Greg Daniel26b50a42018-03-08 09:49:58 -05001748
Greg Daniel164a9f02016-02-22 09:56:40 -05001749////////////////////////////////////////////////////////////////////////////////
1750
1751void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask,
1752 VkPipelineStageFlags dstStageMask,
1753 bool byRegion,
1754 VkMemoryBarrier* barrier) const {
1755 SkASSERT(fCurrentCmdBuffer);
1756 fCurrentCmdBuffer->pipelineBarrier(this,
1757 srcStageMask,
1758 dstStageMask,
1759 byRegion,
1760 GrVkCommandBuffer::kMemory_BarrierType,
1761 barrier);
1762}
1763
1764void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
1765 VkPipelineStageFlags dstStageMask,
1766 bool byRegion,
1767 VkBufferMemoryBarrier* barrier) const {
1768 SkASSERT(fCurrentCmdBuffer);
1769 fCurrentCmdBuffer->pipelineBarrier(this,
1770 srcStageMask,
1771 dstStageMask,
1772 byRegion,
1773 GrVkCommandBuffer::kBufferMemory_BarrierType,
1774 barrier);
1775}
1776
1777void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
1778 VkPipelineStageFlags dstStageMask,
1779 bool byRegion,
1780 VkImageMemoryBarrier* barrier) const {
1781 SkASSERT(fCurrentCmdBuffer);
1782 fCurrentCmdBuffer->pipelineBarrier(this,
1783 srcStageMask,
1784 dstStageMask,
1785 byRegion,
1786 GrVkCommandBuffer::kImageMemory_BarrierType,
1787 barrier);
1788}
1789
Greg Daniel51316782017-08-02 15:10:09 +00001790void GrVkGpu::onFinishFlush(bool insertedSemaphore) {
1791 // Submit the current command buffer to the Queue. Whether we inserted semaphores or not does
1792 // not effect what we do here.
Greg Daniel164a9f02016-02-22 09:56:40 -05001793 this->submitCommandBuffer(kSkip_SyncQueue);
1794}
1795
Greg Daniel25af6712018-04-25 10:44:38 -04001796static int get_surface_sample_cnt(GrSurface* surf) {
1797 if (const GrRenderTarget* rt = surf->asRenderTarget()) {
1798 return rt->numColorSamples();
egdaniel17b89252016-04-05 07:23:38 -07001799 }
Greg Daniel25af6712018-04-25 10:44:38 -04001800 return 0;
Greg Daniel164a9f02016-02-22 09:56:40 -05001801}
1802
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001803void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1804 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001805 GrVkImage* dstImage,
1806 GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001807 const SkIRect& srcRect,
1808 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001809#ifdef SK_DEBUG
1810 int dstSampleCnt = get_surface_sample_cnt(dst);
1811 int srcSampleCnt = get_surface_sample_cnt(src);
1812 SkASSERT(this->vkCaps().canCopyImage(dst->config(), dstSampleCnt, dstOrigin,
1813 src->config(), srcSampleCnt, srcOrigin));
1814
1815#endif
Greg Daniel164a9f02016-02-22 09:56:40 -05001816
Greg Daniel164a9f02016-02-22 09:56:40 -05001817 // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if
1818 // the cache is flushed since it is only being written to.
egdaniel17b89252016-04-05 07:23:38 -07001819 dstImage->setImageLayout(this,
jvanverth50c46c72016-05-06 12:31:28 -07001820 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1821 VK_ACCESS_TRANSFER_WRITE_BIT,
1822 VK_PIPELINE_STAGE_TRANSFER_BIT,
1823 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001824
egdaniel17b89252016-04-05 07:23:38 -07001825 srcImage->setImageLayout(this,
1826 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001827 VK_ACCESS_TRANSFER_READ_BIT,
1828 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001829 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05001830
1831 // Flip rect if necessary
1832 SkIRect srcVkRect = srcRect;
1833 int32_t dstY = dstPoint.fY;
1834
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001835 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1836 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
Greg Daniel164a9f02016-02-22 09:56:40 -05001837 srcVkRect.fTop = src->height() - srcRect.fBottom;
1838 srcVkRect.fBottom = src->height() - srcRect.fTop;
1839 dstY = dst->height() - dstPoint.fY - srcVkRect.height();
1840 }
1841
1842 VkImageCopy copyRegion;
1843 memset(&copyRegion, 0, sizeof(VkImageCopy));
1844 copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1845 copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
1846 copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1847 copyRegion.dstOffset = { dstPoint.fX, dstY, 0 };
egdanielc355bc82016-04-27 11:31:59 -07001848 copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05001849
1850 fCurrentCmdBuffer->copyImage(this,
egdaniel17b89252016-04-05 07:23:38 -07001851 srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001852 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
egdaniel17b89252016-04-05 07:23:38 -07001853 dstImage,
Greg Daniel164a9f02016-02-22 09:56:40 -05001854 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1855 1,
1856 &copyRegion);
jvanverth900bd4a2016-04-29 13:53:12 -07001857
1858 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY,
1859 srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001860 this->didWriteToSurface(dst, dstOrigin, &dstRect);
Greg Daniel164a9f02016-02-22 09:56:40 -05001861}
1862
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001863void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1864 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel17b89252016-04-05 07:23:38 -07001865 GrVkImage* dstImage,
1866 GrVkImage* srcImage,
1867 const SkIRect& srcRect,
1868 const SkIPoint& dstPoint) {
Greg Daniel25af6712018-04-25 10:44:38 -04001869#ifdef SK_DEBUG
1870 int dstSampleCnt = get_surface_sample_cnt(dst);
1871 int srcSampleCnt = get_surface_sample_cnt(src);
1872 SkASSERT(this->vkCaps().canCopyAsBlit(dst->config(), dstSampleCnt, dstImage->isLinearTiled(),
1873 src->config(), srcSampleCnt, srcImage->isLinearTiled()));
egdaniel17b89252016-04-05 07:23:38 -07001874
Greg Daniel25af6712018-04-25 10:44:38 -04001875#endif
egdaniel17b89252016-04-05 07:23:38 -07001876 dstImage->setImageLayout(this,
1877 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001878 VK_ACCESS_TRANSFER_WRITE_BIT,
1879 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001880 false);
1881
egdaniel17b89252016-04-05 07:23:38 -07001882 srcImage->setImageLayout(this,
1883 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
jvanverth50c46c72016-05-06 12:31:28 -07001884 VK_ACCESS_TRANSFER_READ_BIT,
1885 VK_PIPELINE_STAGE_TRANSFER_BIT,
egdaniel17b89252016-04-05 07:23:38 -07001886 false);
1887
1888 // Flip rect if necessary
1889 SkIRect srcVkRect;
egdaniel8af936d2016-04-07 10:17:47 -07001890 srcVkRect.fLeft = srcRect.fLeft;
1891 srcVkRect.fRight = srcRect.fRight;
egdaniel17b89252016-04-05 07:23:38 -07001892 SkIRect dstRect;
1893 dstRect.fLeft = dstPoint.fX;
egdaniel8af936d2016-04-07 10:17:47 -07001894 dstRect.fRight = dstPoint.fX + srcRect.width();
egdaniel17b89252016-04-05 07:23:38 -07001895
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001896 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001897 srcVkRect.fTop = src->height() - srcRect.fBottom;
1898 srcVkRect.fBottom = src->height() - srcRect.fTop;
1899 } else {
egdaniel8af936d2016-04-07 10:17:47 -07001900 srcVkRect.fTop = srcRect.fTop;
1901 srcVkRect.fBottom = srcRect.fBottom;
egdaniel17b89252016-04-05 07:23:38 -07001902 }
1903
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001904 if (kBottomLeft_GrSurfaceOrigin == dstOrigin) {
egdaniel17b89252016-04-05 07:23:38 -07001905 dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height();
1906 } else {
1907 dstRect.fTop = dstPoint.fY;
1908 }
1909 dstRect.fBottom = dstRect.fTop + srcVkRect.height();
1910
1911 // If we have different origins, we need to flip the top and bottom of the dst rect so that we
1912 // get the correct origintation of the copied data.
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001913 if (srcOrigin != dstOrigin) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -04001914 using std::swap;
1915 swap(dstRect.fTop, dstRect.fBottom);
egdaniel17b89252016-04-05 07:23:38 -07001916 }
1917
1918 VkImageBlit blitRegion;
1919 memset(&blitRegion, 0, sizeof(VkImageBlit));
1920 blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1921 blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001922 blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001923 blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
1924 blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 };
Greg Daniele76071c2016-11-02 11:57:06 -04001925 blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 1 };
egdaniel17b89252016-04-05 07:23:38 -07001926
1927 fCurrentCmdBuffer->blitImage(this,
egdanielb2df0c22016-05-13 11:30:37 -07001928 *srcImage,
1929 *dstImage,
egdaniel17b89252016-04-05 07:23:38 -07001930 1,
1931 &blitRegion,
1932 VK_FILTER_NEAREST); // We never scale so any filter works here
jvanverth900bd4a2016-04-29 13:53:12 -07001933
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001934 dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, srcRect.width(), srcRect.height());
Brian Salomon1fabd512018-02-09 09:54:25 -05001935 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel17b89252016-04-05 07:23:38 -07001936}
1937
Brian Salomon1fabd512018-02-09 09:54:25 -05001938void GrVkGpu::copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
1939 GrSurfaceOrigin srcOrigin, const SkIRect& origSrcRect,
1940 const SkIPoint& origDstPoint) {
egdaniel4bcd62e2016-08-31 07:37:31 -07001941 GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget());
Brian Salomon1fabd512018-02-09 09:54:25 -05001942 SkIRect srcRect = origSrcRect;
1943 SkIPoint dstPoint = origDstPoint;
1944 if (kBottomLeft_GrSurfaceOrigin == srcOrigin) {
1945 SkASSERT(kBottomLeft_GrSurfaceOrigin == dstOrigin);
1946 srcRect = {origSrcRect.fLeft, src->height() - origSrcRect.fBottom,
1947 origSrcRect.fRight, src->height() - origSrcRect.fTop};
1948 dstPoint.fY = dst->height() - dstPoint.fY - srcRect.height();
1949 }
1950 this->resolveImage(dst, srcRT, srcRect, dstPoint);
Greg Daniel1ba1bfc2018-06-21 13:55:19 -04001951 SkIRect dstRect = SkIRect::MakeXYWH(origDstPoint.fX, origDstPoint.fY,
1952 srcRect.width(), srcRect.height());
1953 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdaniel4bcd62e2016-08-31 07:37:31 -07001954}
1955
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001956bool GrVkGpu::onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
1957 GrSurface* src, GrSurfaceOrigin srcOrigin,
Greg Daniel55fa6472018-03-16 16:13:10 -04001958 const SkIRect& srcRect, const SkIPoint& dstPoint,
1959 bool canDiscardOutsideDstRect) {
Greg Danielbe7fc462019-01-03 16:40:42 -05001960#ifdef SK_DEBUG
1961 if (GrVkRenderTarget* srcRT = static_cast<GrVkRenderTarget*>(src->asRenderTarget())) {
1962 SkASSERT(!srcRT->wrapsSecondaryCommandBuffer());
1963 }
1964 if (GrVkRenderTarget* dstRT = static_cast<GrVkRenderTarget*>(dst->asRenderTarget())) {
1965 SkASSERT(!dstRT->wrapsSecondaryCommandBuffer());
1966 }
1967#endif
1968
Greg Daniel25af6712018-04-25 10:44:38 -04001969 GrPixelConfig dstConfig = dst->config();
1970 GrPixelConfig srcConfig = src->config();
1971
1972 int dstSampleCnt = get_surface_sample_cnt(dst);
1973 int srcSampleCnt = get_surface_sample_cnt(src);
1974
1975 if (this->vkCaps().canCopyAsResolve(dstConfig, dstSampleCnt, dstOrigin,
1976 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04001977 this->copySurfaceAsResolve(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint);
egdanielec440992016-09-13 09:54:11 -07001978 return true;
egdaniel4bcd62e2016-08-31 07:37:31 -07001979 }
1980
Greg Daniel25af6712018-04-25 10:44:38 -04001981 if (this->vkCaps().canCopyAsDraw(dstConfig, SkToBool(dst->asRenderTarget()),
1982 srcConfig, SkToBool(src->asTexture()))) {
1983 SkAssertResult(fCopyManager.copySurfaceAsDraw(this, dst, dstOrigin, src, srcOrigin, srcRect,
1984 dstPoint, canDiscardOutsideDstRect));
Brian Salomon3d86a192018-02-27 16:46:11 -05001985 auto dstRect = srcRect.makeOffset(dstPoint.fX, dstPoint.fY);
1986 this->didWriteToSurface(dst, dstOrigin, &dstRect);
egdanielbc9b2962016-09-27 08:00:53 -07001987 return true;
1988 }
1989
egdaniel17b89252016-04-05 07:23:38 -07001990 GrVkImage* dstImage;
1991 GrVkImage* srcImage;
egdaniel4bcd62e2016-08-31 07:37:31 -07001992 GrRenderTarget* dstRT = dst->asRenderTarget();
1993 if (dstRT) {
1994 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(dstRT);
Greg Danielbe7fc462019-01-03 16:40:42 -05001995 if (vkRT->wrapsSecondaryCommandBuffer()) {
1996 return false;
1997 }
egdaniel4bcd62e2016-08-31 07:37:31 -07001998 dstImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
1999 } else {
2000 SkASSERT(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002001 dstImage = static_cast<GrVkTexture*>(dst->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002002 }
egdaniel4bcd62e2016-08-31 07:37:31 -07002003 GrRenderTarget* srcRT = src->asRenderTarget();
2004 if (srcRT) {
2005 GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(srcRT);
2006 srcImage = vkRT->numColorSamples() > 1 ? vkRT->msaaImage() : vkRT;
egdaniel17b89252016-04-05 07:23:38 -07002007 } else {
egdaniel4bcd62e2016-08-31 07:37:31 -07002008 SkASSERT(src->asTexture());
2009 srcImage = static_cast<GrVkTexture*>(src->asTexture());
egdaniel17b89252016-04-05 07:23:38 -07002010 }
2011
Greg Daniel25af6712018-04-25 10:44:38 -04002012 if (this->vkCaps().canCopyImage(dstConfig, dstSampleCnt, dstOrigin,
2013 srcConfig, srcSampleCnt, srcOrigin)) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002014 this->copySurfaceAsCopyImage(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2015 srcRect, dstPoint);
egdaniel17b89252016-04-05 07:23:38 -07002016 return true;
2017 }
2018
Greg Daniel25af6712018-04-25 10:44:38 -04002019 if (this->vkCaps().canCopyAsBlit(dstConfig, dstSampleCnt, dstImage->isLinearTiled(),
2020 srcConfig, srcSampleCnt, srcImage->isLinearTiled())) {
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002021 this->copySurfaceAsBlit(dst, dstOrigin, src, srcOrigin, dstImage, srcImage,
2022 srcRect, dstPoint);
Greg Daniel164a9f02016-02-22 09:56:40 -05002023 return true;
2024 }
2025
Greg Daniel164a9f02016-02-22 09:56:40 -05002026 return false;
2027}
2028
Brian Salomona6948702018-06-01 15:33:20 -04002029bool GrVkGpu::onReadPixels(GrSurface* surface, int left, int top, int width, int height,
2030 GrColorType dstColorType, void* buffer, size_t rowBytes) {
Brian Salomonc320b152018-02-20 14:05:36 -05002031 if (GrPixelConfigToColorType(surface->config()) != dstColorType) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002032 return false;
2033 }
2034
egdaniel66933552016-08-24 07:22:19 -07002035 GrVkImage* image = nullptr;
2036 GrVkRenderTarget* rt = static_cast<GrVkRenderTarget*>(surface->asRenderTarget());
2037 if (rt) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002038 // Reading from render targets that wrap a secondary command buffer is not allowed since
2039 // it would require us to know the VkImage, which we don't have, as well as need us to
2040 // stop and start the VkRenderPass which we don't have access to.
2041 if (rt->wrapsSecondaryCommandBuffer()) {
2042 return false;
2043 }
egdaniel66933552016-08-24 07:22:19 -07002044 // resolve the render target if necessary
2045 switch (rt->getResolveType()) {
2046 case GrVkRenderTarget::kCantResolve_ResolveType:
2047 return false;
2048 case GrVkRenderTarget::kAutoResolves_ResolveType:
2049 break;
2050 case GrVkRenderTarget::kCanResolve_ResolveType:
Greg Daniel0a77f432018-12-06 11:23:32 -05002051 this->resolveRenderTargetNoFlush(rt);
egdaniel66933552016-08-24 07:22:19 -07002052 break;
2053 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -04002054 SK_ABORT("Unknown resolve type");
egdaniel66933552016-08-24 07:22:19 -07002055 }
2056 image = rt;
2057 } else {
2058 image = static_cast<GrVkTexture*>(surface->asTexture());
2059 }
2060
2061 if (!image) {
Greg Daniel164a9f02016-02-22 09:56:40 -05002062 return false;
2063 }
2064
Greg Daniel475eb702018-09-28 14:16:50 -04002065 // Skia's RGB_888x color type, which we map to the vulkan R8G8B8_UNORM, expects the data to be
2066 // 32 bits, but the Vulkan format is only 24. So we first copy the surface into an R8G8B8A8
2067 // image and then do the read pixels from that.
2068 sk_sp<GrVkTextureRenderTarget> copySurface;
2069 if (dstColorType == GrColorType::kRGB_888x) {
2070 SkASSERT(image->imageFormat() == VK_FORMAT_R8G8B8_UNORM &&
2071 surface->config() == kRGB_888_GrPixelConfig);
2072
2073 // Make a new surface that is RGBA to copy the RGB surface into.
2074 GrSurfaceDesc surfDesc;
2075 surfDesc.fFlags = kRenderTarget_GrSurfaceFlag;
2076 surfDesc.fWidth = width;
2077 surfDesc.fHeight = height;
2078 surfDesc.fConfig = kRGBA_8888_GrPixelConfig;
2079 surfDesc.fSampleCnt = 1;
2080
2081 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
2082 VK_IMAGE_USAGE_SAMPLED_BIT |
2083 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
2084 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
2085
2086 GrVkImage::ImageDesc imageDesc;
2087 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
2088 imageDesc.fFormat = VK_FORMAT_R8G8B8A8_UNORM;
2089 imageDesc.fWidth = width;
2090 imageDesc.fHeight = height;
2091 imageDesc.fLevels = 1;
2092 imageDesc.fSamples = 1;
2093 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
2094 imageDesc.fUsageFlags = usageFlags;
2095 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
2096
2097 copySurface = GrVkTextureRenderTarget::MakeNewTextureRenderTarget(
2098 this, SkBudgeted::kYes, surfDesc, imageDesc, GrMipMapsStatus::kNotAllocated);
2099 if (!copySurface) {
2100 return false;
2101 }
2102
2103 int srcSampleCount = 0;
2104 if (rt) {
2105 srcSampleCount = rt->numColorSamples();
2106 }
2107 static const GrSurfaceOrigin kOrigin = kTopLeft_GrSurfaceOrigin;
2108 if (!this->vkCaps().canCopyAsBlit(copySurface->config(), 1, kOrigin,
2109 surface->config(), srcSampleCount, kOrigin) &&
2110 !this->vkCaps().canCopyAsDraw(copySurface->config(), false,
2111 surface->config(), SkToBool(surface->asTexture()))) {
2112 return false;
2113 }
2114 SkIRect srcRect = SkIRect::MakeXYWH(left, top, width, height);
2115 if (!this->copySurface(copySurface.get(), kOrigin, surface, kOrigin,
2116 srcRect, SkIPoint::Make(0,0))) {
2117 return false;
2118 }
2119 top = 0;
2120 left = 0;
2121 dstColorType = GrColorType::kRGBA_8888;
2122 image = copySurface.get();
2123 }
2124
Greg Daniel164a9f02016-02-22 09:56:40 -05002125 // Change layout of our target so it can be used as copy
egdaniel66933552016-08-24 07:22:19 -07002126 image->setImageLayout(this,
2127 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
2128 VK_ACCESS_TRANSFER_READ_BIT,
2129 VK_PIPELINE_STAGE_TRANSFER_BIT,
2130 false);
Greg Daniel164a9f02016-02-22 09:56:40 -05002131
Brian Salomonc320b152018-02-20 14:05:36 -05002132 int bpp = GrColorTypeBytesPerPixel(dstColorType);
egdaniel6fa0a912016-09-12 11:51:29 -07002133 size_t tightRowBytes = bpp * width;
Greg Daniel164a9f02016-02-22 09:56:40 -05002134
Greg Daniel164a9f02016-02-22 09:56:40 -05002135 VkBufferImageCopy region;
2136 memset(&region, 0, sizeof(VkBufferImageCopy));
egdaniel6fa0a912016-09-12 11:51:29 -07002137
2138 bool copyFromOrigin = this->vkCaps().mustDoCopiesFromOrigin();
2139 if (copyFromOrigin) {
2140 region.imageOffset = { 0, 0, 0 };
Brian Salomona6948702018-06-01 15:33:20 -04002141 region.imageExtent = { (uint32_t)(left + width), (uint32_t)(top + height), 1 };
egdaniel6fa0a912016-09-12 11:51:29 -07002142 } else {
Brian Salomona6948702018-06-01 15:33:20 -04002143 VkOffset3D offset = { left, top, 0 };
egdaniel6fa0a912016-09-12 11:51:29 -07002144 region.imageOffset = offset;
2145 region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 };
2146 }
2147
2148 size_t transBufferRowBytes = bpp * region.imageExtent.width;
Greg Daniel386a9b62018-07-03 10:52:30 -04002149 size_t imageRows = region.imageExtent.height;
Brian Salomon12d22642019-01-29 14:38:50 -05002150 auto transferBuffer = sk_sp<GrVkTransferBuffer>(
Greg Daniel3cdfa092018-02-26 16:14:10 -05002151 static_cast<GrVkTransferBuffer*>(this->createBuffer(transBufferRowBytes * imageRows,
Brian Salomonae64c192019-02-05 09:41:37 -05002152 GrGpuBufferType::kXferGpuToCpu,
Brian Salomon12d22642019-01-29 14:38:50 -05002153 kStream_GrAccessPattern)
2154 .release()));
egdaniel6fa0a912016-09-12 11:51:29 -07002155
2156 // Copy the image to a buffer so we can map it to cpu memory
jvanverthdb379092016-07-07 11:18:46 -07002157 region.bufferOffset = transferBuffer->offset();
egdaniel88e8aef2016-06-27 14:34:55 -07002158 region.bufferRowLength = 0; // Forces RowLength to be width. We handle the rowBytes below.
Greg Daniel164a9f02016-02-22 09:56:40 -05002159 region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images.
2160 region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
Greg Daniel164a9f02016-02-22 09:56:40 -05002161
2162 fCurrentCmdBuffer->copyImageToBuffer(this,
egdaniel66933552016-08-24 07:22:19 -07002163 image,
Greg Daniel164a9f02016-02-22 09:56:40 -05002164 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
Brian Salomon12d22642019-01-29 14:38:50 -05002165 transferBuffer.get(),
Greg Daniel164a9f02016-02-22 09:56:40 -05002166 1,
2167 &region);
2168
2169 // make sure the copy to buffer has finished
2170 transferBuffer->addMemoryBarrier(this,
2171 VK_ACCESS_TRANSFER_WRITE_BIT,
2172 VK_ACCESS_HOST_READ_BIT,
2173 VK_PIPELINE_STAGE_TRANSFER_BIT,
2174 VK_PIPELINE_STAGE_HOST_BIT,
2175 false);
2176
2177 // We need to submit the current command buffer to the Queue and make sure it finishes before
2178 // we can copy the data out of the buffer.
2179 this->submitCommandBuffer(kForce_SyncQueue);
Greg Daniel88fdee92018-02-24 22:41:50 +00002180 void* mappedMemory = transferBuffer->map();
Greg Daniele35a99e2018-03-02 11:44:22 -05002181 const GrVkAlloc& transAlloc = transferBuffer->alloc();
Greg Daniel81df0412018-05-31 13:13:33 -04002182 GrVkMemory::InvalidateMappedAlloc(this, transAlloc, 0, transAlloc.fSize);
Greg Daniel164a9f02016-02-22 09:56:40 -05002183
egdaniel6fa0a912016-09-12 11:51:29 -07002184 if (copyFromOrigin) {
2185 uint32_t skipRows = region.imageExtent.height - height;
2186 mappedMemory = (char*)mappedMemory + transBufferRowBytes * skipRows + bpp * left;
2187 }
2188
Brian Salomona6948702018-06-01 15:33:20 -04002189 SkRectMemcpy(buffer, rowBytes, mappedMemory, transBufferRowBytes, tightRowBytes, height);
Greg Daniel164a9f02016-02-22 09:56:40 -05002190
2191 transferBuffer->unmap();
Greg Daniel164a9f02016-02-22 09:56:40 -05002192 return true;
2193}
egdaniel066df7c2016-06-08 14:02:27 -07002194
egdaniel27bb2842016-07-07 11:58:35 -07002195// The RenderArea bounds we pass into BeginRenderPass must have a start x value that is a multiple
2196// of the granularity. The width must also be a multiple of the granularity or eaqual to the width
2197// the the entire attachment. Similar requirements for the y and height components.
2198void adjust_bounds_to_granularity(SkIRect* dstBounds, const SkIRect& srcBounds,
2199 const VkExtent2D& granularity, int maxWidth, int maxHeight) {
2200 // Adjust Width
egdanield5797b32016-09-20 12:57:45 -07002201 if ((0 != granularity.width && 1 != granularity.width)) {
2202 // Start with the right side of rect so we know if we end up going pass the maxWidth.
2203 int rightAdj = srcBounds.fRight % granularity.width;
2204 if (rightAdj != 0) {
2205 rightAdj = granularity.width - rightAdj;
2206 }
2207 dstBounds->fRight = srcBounds.fRight + rightAdj;
2208 if (dstBounds->fRight > maxWidth) {
2209 dstBounds->fRight = maxWidth;
2210 dstBounds->fLeft = 0;
2211 } else {
2212 dstBounds->fLeft = srcBounds.fLeft - srcBounds.fLeft % granularity.width;
2213 }
egdaniel27bb2842016-07-07 11:58:35 -07002214 } else {
egdanield5797b32016-09-20 12:57:45 -07002215 dstBounds->fLeft = srcBounds.fLeft;
2216 dstBounds->fRight = srcBounds.fRight;
egdaniel27bb2842016-07-07 11:58:35 -07002217 }
2218
2219 // Adjust height
egdanield5797b32016-09-20 12:57:45 -07002220 if ((0 != granularity.height && 1 != granularity.height)) {
2221 // Start with the bottom side of rect so we know if we end up going pass the maxHeight.
2222 int bottomAdj = srcBounds.fBottom % granularity.height;
2223 if (bottomAdj != 0) {
2224 bottomAdj = granularity.height - bottomAdj;
2225 }
2226 dstBounds->fBottom = srcBounds.fBottom + bottomAdj;
2227 if (dstBounds->fBottom > maxHeight) {
2228 dstBounds->fBottom = maxHeight;
2229 dstBounds->fTop = 0;
2230 } else {
2231 dstBounds->fTop = srcBounds.fTop - srcBounds.fTop % granularity.height;
2232 }
egdaniel27bb2842016-07-07 11:58:35 -07002233 } else {
egdanield5797b32016-09-20 12:57:45 -07002234 dstBounds->fTop = srcBounds.fTop;
2235 dstBounds->fBottom = srcBounds.fBottom;
egdaniel27bb2842016-07-07 11:58:35 -07002236 }
2237}
2238
Greg Daniel22bc8652017-03-22 15:45:43 -04002239void GrVkGpu::submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>& buffers,
egdaniel9cb63402016-06-23 08:37:05 -07002240 const GrVkRenderPass* renderPass,
2241 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002242 GrVkRenderTarget* target, GrSurfaceOrigin origin,
egdaniel9cb63402016-06-23 08:37:05 -07002243 const SkIRect& bounds) {
Greg Danielbe7fc462019-01-03 16:40:42 -05002244 SkASSERT (!target->wrapsSecondaryCommandBuffer());
egdaniele7d1b242016-07-01 08:06:45 -07002245 const SkIRect* pBounds = &bounds;
2246 SkIRect flippedBounds;
Robert Phillipsb0e93a22017-08-29 08:26:54 -04002247 if (kBottomLeft_GrSurfaceOrigin == origin) {
egdaniele7d1b242016-07-01 08:06:45 -07002248 flippedBounds = bounds;
2249 flippedBounds.fTop = target->height() - bounds.fBottom;
2250 flippedBounds.fBottom = target->height() - bounds.fTop;
2251 pBounds = &flippedBounds;
2252 }
2253
egdaniel27bb2842016-07-07 11:58:35 -07002254 // The bounds we use for the render pass should be of the granularity supported
2255 // by the device.
2256 const VkExtent2D& granularity = renderPass->granularity();
2257 SkIRect adjustedBounds;
2258 if ((0 != granularity.width && 1 != granularity.width) ||
2259 (0 != granularity.height && 1 != granularity.height)) {
2260 adjust_bounds_to_granularity(&adjustedBounds, *pBounds, granularity,
2261 target->width(), target->height());
2262 pBounds = &adjustedBounds;
2263 }
2264
Robert Phillips95214472017-08-08 18:00:03 -04002265#ifdef SK_DEBUG
2266 uint32_t index;
2267 bool result = renderPass->colorAttachmentIndex(&index);
2268 SkASSERT(result && 0 == index);
2269 result = renderPass->stencilAttachmentIndex(&index);
2270 if (result) {
2271 SkASSERT(1 == index);
2272 }
2273#endif
2274 VkClearValue clears[2];
2275 clears[0].color = colorClear->color;
Robert Phillips8c326e92017-08-10 13:50:17 -04002276 clears[1].depthStencil.depth = 0.0f;
2277 clears[1].depthStencil.stencil = 0;
Robert Phillips95214472017-08-08 18:00:03 -04002278
2279 fCurrentCmdBuffer->beginRenderPass(this, renderPass, clears, *target, *pBounds, true);
Greg Daniel22bc8652017-03-22 15:45:43 -04002280 for (int i = 0; i < buffers.count(); ++i) {
2281 fCurrentCmdBuffer->executeCommands(this, buffers[i]);
2282 }
Greg Daniel164a9f02016-02-22 09:56:40 -05002283 fCurrentCmdBuffer->endRenderPass(this);
egdaniel66933552016-08-24 07:22:19 -07002284
Brian Salomon1fabd512018-02-09 09:54:25 -05002285 this->didWriteToSurface(target, origin, &bounds);
Greg Daniel164a9f02016-02-22 09:56:40 -05002286}
egdaniel9cb63402016-06-23 08:37:05 -07002287
Robert Phillips5b5d84c2018-08-09 15:12:18 -04002288void GrVkGpu::submit(GrGpuCommandBuffer* buffer) {
2289 if (buffer->asRTCommandBuffer()) {
2290 SkASSERT(fCachedRTCommandBuffer.get() == buffer);
2291
2292 fCachedRTCommandBuffer->submit();
2293 fCachedRTCommandBuffer->reset();
2294 } else {
2295 SkASSERT(fCachedTexCommandBuffer.get() == buffer);
2296
2297 fCachedTexCommandBuffer->submit();
2298 fCachedTexCommandBuffer->reset();
2299 }
2300}
2301
Greg Daniel6be35232017-03-01 17:01:09 -05002302GrFence SK_WARN_UNUSED_RESULT GrVkGpu::insertFence() {
jvanverth84741b32016-09-30 08:39:02 -07002303 VkFenceCreateInfo createInfo;
2304 memset(&createInfo, 0, sizeof(VkFenceCreateInfo));
2305 createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
2306 createInfo.pNext = nullptr;
2307 createInfo.flags = 0;
2308 VkFence fence = VK_NULL_HANDLE;
Greg Daniel6be35232017-03-01 17:01:09 -05002309
2310 VK_CALL_ERRCHECK(CreateFence(this->device(), &createInfo, nullptr, &fence));
2311 VK_CALL(QueueSubmit(this->queue(), 0, nullptr, fence));
2312
2313 GR_STATIC_ASSERT(sizeof(GrFence) >= sizeof(VkFence));
jvanverth84741b32016-09-30 08:39:02 -07002314 return (GrFence)fence;
2315}
2316
Greg Daniel6be35232017-03-01 17:01:09 -05002317bool GrVkGpu::waitFence(GrFence fence, uint64_t timeout) {
2318 SkASSERT(VK_NULL_HANDLE != (VkFence)fence);
2319
2320 VkResult result = VK_CALL(WaitForFences(this->device(), 1, (VkFence*)&fence, VK_TRUE, timeout));
jvanverth84741b32016-09-30 08:39:02 -07002321 return (VK_SUCCESS == result);
2322}
2323
2324void GrVkGpu::deleteFence(GrFence fence) const {
Greg Daniel6be35232017-03-01 17:01:09 -05002325 VK_CALL(DestroyFence(this->device(), (VkFence)fence, nullptr));
2326}
2327
Greg Daniela5cb7812017-06-16 09:45:32 -04002328sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT GrVkGpu::makeSemaphore(bool isOwned) {
2329 return GrVkSemaphore::Make(this, isOwned);
Greg Daniel6be35232017-03-01 17:01:09 -05002330}
2331
Greg Daniel48661b82018-01-22 16:11:35 -05002332sk_sp<GrSemaphore> GrVkGpu::wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
2333 GrResourceProvider::SemaphoreWrapType wrapType,
2334 GrWrapOwnership ownership) {
2335 return GrVkSemaphore::MakeWrapped(this, semaphore.vkSemaphore(), wrapType, ownership);
Greg Daniela5cb7812017-06-16 09:45:32 -04002336}
2337
Greg Daniel858e12c2018-12-06 11:11:37 -05002338void GrVkGpu::insertSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002339 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2340
Greg Daniel48661b82018-01-22 16:11:35 -05002341 GrVkSemaphore::Resource* resource = vkSem->getResource();
2342 if (resource->shouldSignal()) {
Greg Daniel17b7c052018-01-09 13:55:33 -05002343 resource->ref();
2344 fSemaphoresToSignal.push_back(resource);
2345 }
Greg Daniel6be35232017-03-01 17:01:09 -05002346}
2347
Greg Daniel48661b82018-01-22 16:11:35 -05002348void GrVkGpu::waitSemaphore(sk_sp<GrSemaphore> semaphore) {
Greg Daniel6be35232017-03-01 17:01:09 -05002349 GrVkSemaphore* vkSem = static_cast<GrVkSemaphore*>(semaphore.get());
2350
Greg Daniel48661b82018-01-22 16:11:35 -05002351 GrVkSemaphore::Resource* resource = vkSem->getResource();
2352 if (resource->shouldWait()) {
2353 resource->ref();
2354 fSemaphoresToWaitOn.push_back(resource);
2355 }
jvanverth84741b32016-09-30 08:39:02 -07002356}
Brian Osman13dddce2017-05-09 13:19:50 -04002357
2358sk_sp<GrSemaphore> GrVkGpu::prepareTextureForCrossContextUsage(GrTexture* texture) {
2359 SkASSERT(texture);
2360 GrVkTexture* vkTexture = static_cast<GrVkTexture*>(texture);
2361 vkTexture->setImageLayout(this,
2362 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
2363 VK_ACCESS_SHADER_READ_BIT,
Greg Danielf7828d02018-10-09 12:01:32 -04002364 VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
Brian Osman13dddce2017-05-09 13:19:50 -04002365 false);
2366 this->submitCommandBuffer(kSkip_SyncQueue);
2367
2368 // The image layout change serves as a barrier, so no semaphore is needed
2369 return nullptr;
2370}
Greg Danielf5d87582017-12-18 14:48:15 -05002371
Greg Daniel64cc9aa2018-10-19 13:54:56 -04002372void GrVkGpu::addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable) {
2373 fDrawables.emplace_back(std::move(drawable));
2374}
2375
Greg Daniel7a82edf2018-12-04 10:54:34 -05002376uint32_t GrVkGpu::getExtraSamplerKeyForProgram(const GrSamplerState& samplerState,
2377 const GrBackendFormat& format) {
2378 const GrVkYcbcrConversionInfo* ycbcrInfo = format.getVkYcbcrConversionInfo();
2379 SkASSERT(ycbcrInfo);
2380 if (!ycbcrInfo->isValid()) {
2381 return 0;
2382 }
2383
2384 const GrVkSampler* sampler = this->resourceProvider().findOrCreateCompatibleSampler(
2385 samplerState, *ycbcrInfo);
2386
2387 return sampler->uniqueID();
2388}
2389
Greg Daniela870b462019-01-08 15:49:46 -05002390void GrVkGpu::storeVkPipelineCacheData() {
Robert Phillips9da87e02019-02-04 13:26:26 -05002391 if (this->getContext()->priv().getPersistentCache()) {
Greg Daniela870b462019-01-08 15:49:46 -05002392 this->resourceProvider().storePipelineCacheData();
2393 }
2394}