aboutsummaryrefslogtreecommitdiff
path: root/host/vulkan/VkDecoderGlobalState.h
blob: 61cd1d0feab64a7b16f39105bfcdda55a22fd4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
// Copyright 2018 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once

#include <vulkan/vulkan.h>

#include <memory>
#include <optional>
#include <unordered_map>
#include <utility>
#include <vector>

#include "VkDecoderContext.h"
#include "VkQsriTimeline.h"
#include "VulkanDispatch.h"
#include "VulkanHandleMapping.h"
#include "aemu/base/AsyncResult.h"
#include "aemu/base/HealthMonitor.h"
#include "aemu/base/synchronization/Lock.h"
#include "gfxstream/host/Features.h"
#include "goldfish_vk_private_defs.h"
#include "cereal/common/goldfish_vk_transform.h"
#include "host-common/GfxstreamFatalError.h"
#include "utils/GfxApiLogger.h"
#include "vk_util.h"

using android::base::AutoLock;
using android::base::Lock;
using emugl::ABORT_REASON_OTHER;
using emugl::FatalError;
using emugl::HealthMonitor;

class VkDecoderSnapshot;

namespace android {
namespace base {
class BumpPool;
class Stream;
}  // namespace base
}  // namespace android

namespace gfxstream {
namespace vk {

// Class for tracking host-side state. Currently we only care about
// tracking VkDeviceMemory to make it easier to pass the right data
// from mapped pointers to the guest, but this could get more stuff
// added to it if for instance, we want to only expose a certain set
// of physical device capabilities, or do snapshots.

// This class may be autogenerated in the future.
// Currently, it works by interfacing with VkDecoder calling on_<apicall>
// functions.
class VkDecoderGlobalState {
   public:
    VkDecoderGlobalState();
    ~VkDecoderGlobalState();

    // There should only be one instance of VkDecoderGlobalState per process
    // Note: currently not thread-safe
    static VkDecoderGlobalState* get();

    // For testing only - destroys the global instance of VkDecoderGlobalState.
    static void reset();

    // Snapshot save/load
    bool snapshotsEnabled() const;

    const gfxstream::host::FeatureSet& getFeatures() const;

    // Whether to clean up VK instance.
    // bug 149997534
    bool vkCleanupEnabled() const;

    void save(android::base::Stream* stream);
    void load(android::base::Stream* stream, emugl::GfxApiLogger& gfxLogger,
              emugl::HealthMonitor<>* healthMonitor);

    // Lock/unlock of global state to serve as a global lock
    void lock();
    void unlock();

    // Sets the current created handles for snapshot load
    // which will override the effect of any new_boxed_*** calls.
    // They are consumed in sequence.
    // Returns number of bytes of |buffer|
    // that have been consumed for this purpose.
    size_t setCreatedHandlesForSnapshotLoad(const unsigned char* buffer);
    void clearCreatedHandlesForSnapshotLoad();

    VkResult on_vkEnumerateInstanceVersion(android::base::BumpPool* pool, uint32_t* pApiVersion);

    // Fast way to get dispatch tables associated with a Vulkan object.
    // VkInstance
    // VkPhysicalDevice
    // VkDevice
    // VkQueue
    // VkCommandBuffer

    VkResult on_vkCreateInstance(android::base::BumpPool* pool,
                                 const VkInstanceCreateInfo* pCreateInfo,
                                 const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);

    void on_vkDestroyInstance(android::base::BumpPool* pool, VkInstance instance,
                              const VkAllocationCallbacks* pAllocator);

    VkResult on_vkEnumeratePhysicalDevices(android::base::BumpPool* pool, VkInstance instance,
                                           uint32_t* physicalDeviceCount,
                                           VkPhysicalDevice* physicalDevices);

    // Override features
    void on_vkGetPhysicalDeviceFeatures(android::base::BumpPool* pool,
                                        VkPhysicalDevice physicalDevice,
                                        VkPhysicalDeviceFeatures* pFeatures);
    void on_vkGetPhysicalDeviceFeatures2(android::base::BumpPool* pool,
                                         VkPhysicalDevice physicalDevice,
                                         VkPhysicalDeviceFeatures2* pFeatures);
    void on_vkGetPhysicalDeviceFeatures2KHR(android::base::BumpPool* pool,
                                            VkPhysicalDevice physicalDevice,
                                            VkPhysicalDeviceFeatures2* pFeatures);

    // Override image format properties
    VkResult on_vkGetPhysicalDeviceImageFormatProperties(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice, VkFormat format,
        VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags,
        VkImageFormatProperties* pImageFormatProperties);
    VkResult on_vkGetPhysicalDeviceImageFormatProperties2(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
        VkImageFormatProperties2* pImageFormatProperties);
    VkResult on_vkGetPhysicalDeviceImageFormatProperties2KHR(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        const VkPhysicalDeviceImageFormatInfo2* pImageFormatInfo,
        VkImageFormatProperties2* pImageFormatProperties);

    // Override format features
    void on_vkGetPhysicalDeviceFormatProperties(android::base::BumpPool* pool,
                                                VkPhysicalDevice physicalDevice, VkFormat format,
                                                VkFormatProperties* pFormatProperties);
    void on_vkGetPhysicalDeviceFormatProperties2(android::base::BumpPool* pool,
                                                 VkPhysicalDevice physicalDevice, VkFormat format,
                                                 VkFormatProperties2* pFormatProperties);
    void on_vkGetPhysicalDeviceFormatProperties2KHR(android::base::BumpPool* pool,
                                                    VkPhysicalDevice physicalDevice,
                                                    VkFormat format,
                                                    VkFormatProperties2* pFormatProperties);

    // Override API version
    void on_vkGetPhysicalDeviceProperties(android::base::BumpPool* pool,
                                          VkPhysicalDevice physicalDevice,
                                          VkPhysicalDeviceProperties* pProperties);
    void on_vkGetPhysicalDeviceProperties2(android::base::BumpPool* pool,
                                           VkPhysicalDevice physicalDevice,
                                           VkPhysicalDeviceProperties2* pProperties);
    void on_vkGetPhysicalDeviceProperties2KHR(android::base::BumpPool* pool,
                                              VkPhysicalDevice physicalDevice,
                                              VkPhysicalDeviceProperties2* pProperties);

    // Override memory types advertised from host
    //
    void on_vkGetPhysicalDeviceMemoryProperties(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        VkPhysicalDeviceMemoryProperties* pMemoryProperties);
    void on_vkGetPhysicalDeviceMemoryProperties2(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        VkPhysicalDeviceMemoryProperties2* pMemoryProperties);
    void on_vkGetPhysicalDeviceMemoryProperties2KHR(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        VkPhysicalDeviceMemoryProperties2* pMemoryProperties);

    // Override supported device extension lists
    VkResult on_vkEnumerateDeviceExtensionProperties(android::base::BumpPool* pool,
                                                     VkPhysicalDevice physicalDevice,
                                                     const char* pLayerName,
                                                     uint32_t* pPropertyCount,
                                                     VkExtensionProperties* pProperties);

    VkResult on_vkCreateDevice(android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
                               const VkDeviceCreateInfo* pCreateInfo,
                               const VkAllocationCallbacks* pAllocator, VkDevice* pDevice);

    void on_vkGetDeviceQueue(android::base::BumpPool* pool, VkDevice device,
                             uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue);

    void on_vkGetDeviceQueue2(android::base::BumpPool* pool, VkDevice device,
                              const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue);

    void on_vkDestroyDevice(android::base::BumpPool* pool, VkDevice device,
                            const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateBuffer(android::base::BumpPool* pool, VkDevice device,
                               const VkBufferCreateInfo* pCreateInfo,
                               const VkAllocationCallbacks* pAllocator, VkBuffer* pBuffer);

    void on_vkDestroyBuffer(android::base::BumpPool* pool, VkDevice device, VkBuffer buffer,
                            const VkAllocationCallbacks* pAllocator);

    VkResult on_vkBindBufferMemory(android::base::BumpPool* pool, VkDevice device, VkBuffer buffer,
                                   VkDeviceMemory memory, VkDeviceSize memoryOffset);
    VkResult on_vkBindBufferMemory2(android::base::BumpPool* pool, VkDevice device,
                                    uint32_t bindInfoCount,
                                    const VkBindBufferMemoryInfo* pBindInfos);
    VkResult on_vkBindBufferMemory2KHR(android::base::BumpPool* pool, VkDevice device,
                                       uint32_t bindInfoCount,
                                       const VkBindBufferMemoryInfo* pBindInfos);

    VkResult on_vkCreateImage(android::base::BumpPool* pool, VkDevice device,
                              const VkImageCreateInfo* pCreateInfo,
                              const VkAllocationCallbacks* pAllocator, VkImage* pImage);

    void on_vkDestroyImage(android::base::BumpPool* pool, VkDevice device, VkImage image,
                           const VkAllocationCallbacks* pAllocator);

    VkResult on_vkBindImageMemory(android::base::BumpPool* pool, VkDevice device, VkImage image,
                                  VkDeviceMemory memory, VkDeviceSize memoryOffset);
    VkResult on_vkBindImageMemory2(android::base::BumpPool* pool, VkDevice device,
                                   uint32_t bindInfoCount, const VkBindImageMemoryInfo* pBindInfos);
    VkResult on_vkBindImageMemory2KHR(android::base::BumpPool* pool, VkDevice device,
                                      uint32_t bindInfoCount,
                                      const VkBindImageMemoryInfo* pBindInfos);

    VkResult on_vkCreateImageView(android::base::BumpPool* pool, VkDevice device,
                                  const VkImageViewCreateInfo* pCreateInfo,
                                  const VkAllocationCallbacks* pAllocator, VkImageView* pView);

    void on_vkDestroyImageView(android::base::BumpPool* pool, VkDevice device,
                               VkImageView imageView, const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateSampler(android::base::BumpPool* pool, VkDevice device,
                                const VkSamplerCreateInfo* pCreateInfo,
                                const VkAllocationCallbacks* pAllocator, VkSampler* pSampler);

    void on_vkDestroySampler(android::base::BumpPool* pool, VkDevice device, VkSampler sampler,
                             const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateDescriptorSetLayout(android::base::BumpPool* pool, VkDevice device,
                                            const VkDescriptorSetLayoutCreateInfo* pCreateInfo,
                                            const VkAllocationCallbacks* pAllocator,
                                            VkDescriptorSetLayout* pSetLayout);

    void on_vkDestroyDescriptorSetLayout(android::base::BumpPool* pool, VkDevice device,
                                         VkDescriptorSetLayout descriptorSetLayout,
                                         const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateDescriptorPool(android::base::BumpPool* pool, VkDevice device,
                                       const VkDescriptorPoolCreateInfo* pCreateInfo,
                                       const VkAllocationCallbacks* pAllocator,
                                       VkDescriptorPool* pDescriptorPool);

    void on_vkDestroyDescriptorPool(android::base::BumpPool* pool, VkDevice device,
                                    VkDescriptorPool descriptorPool,
                                    const VkAllocationCallbacks* pAllocator);

    VkResult on_vkResetDescriptorPool(android::base::BumpPool* pool, VkDevice device,
                                      VkDescriptorPool descriptorPool,
                                      VkDescriptorPoolResetFlags flags);

    VkResult on_vkAllocateDescriptorSets(android::base::BumpPool* pool, VkDevice device,
                                         const VkDescriptorSetAllocateInfo* pAllocateInfo,
                                         VkDescriptorSet* pDescriptorSets);

    VkResult on_vkFreeDescriptorSets(android::base::BumpPool* pool, VkDevice device,
                                     VkDescriptorPool descriptorPool, uint32_t descriptorSetCount,
                                     const VkDescriptorSet* pDescriptorSets);

    void on_vkUpdateDescriptorSets(android::base::BumpPool* pool, VkDevice device,
                                   uint32_t descriptorWriteCount,
                                   const VkWriteDescriptorSet* pDescriptorWrites,
                                   uint32_t descriptorCopyCount,
                                   const VkCopyDescriptorSet* pDescriptorCopies);

    VkResult on_vkCreateShaderModule(android::base::BumpPool* pool, VkDevice device,
                                     const VkShaderModuleCreateInfo* pCreateInfo,
                                     const VkAllocationCallbacks* pAllocator,
                                     VkShaderModule* pShaderModule);

    void on_vkDestroyShaderModule(android::base::BumpPool* pool, VkDevice device,
                                  VkShaderModule shaderModule,
                                  const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreatePipelineCache(android::base::BumpPool* pool, VkDevice device,
                                      const VkPipelineCacheCreateInfo* pCreateInfo,
                                      const VkAllocationCallbacks* pAllocator,
                                      VkPipelineCache* pPipelineCache);

    void on_vkDestroyPipelineCache(android::base::BumpPool* pool, VkDevice device,
                                   VkPipelineCache pipelineCache,
                                   const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateGraphicsPipelines(android::base::BumpPool* pool, VkDevice device,
                                          VkPipelineCache pipelineCache, uint32_t createInfoCount,
                                          const VkGraphicsPipelineCreateInfo* pCreateInfos,
                                          const VkAllocationCallbacks* pAllocator,
                                          VkPipeline* pPipelines);

    void on_vkDestroyPipeline(android::base::BumpPool* pool, VkDevice device, VkPipeline pipeline,
                              const VkAllocationCallbacks* pAllocator);

    void on_vkCmdCopyBufferToImage(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                   VkBuffer srcBuffer, VkImage dstImage,
                                   VkImageLayout dstImageLayout, uint32_t regionCount,
                                   const VkBufferImageCopy* pRegions,
                                   const VkDecoderContext& context);

    void on_vkCmdCopyImage(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                           VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
                           VkImageLayout dstImageLayout, uint32_t regionCount,
                           const VkImageCopy* pRegions);
    void on_vkCmdCopyImageToBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                   VkImage srcImage, VkImageLayout srcImageLayout,
                                   VkBuffer dstBuffer, uint32_t regionCount,
                                   const VkBufferImageCopy* pRegions);

    void on_vkCmdCopyBufferToImage2(android::base::BumpPool* pool,
                                    VkCommandBuffer commandBuffer,
                                    const VkCopyBufferToImageInfo2* pCopyBufferToImageInfo,
                                    const VkDecoderContext& context);

    void on_vkCmdCopyImage2(android::base::BumpPool* pool,
                           VkCommandBuffer commandBuffer,
                           const VkCopyImageInfo2* pCopyImageInfo);
    void on_vkCmdCopyImageToBuffer2(android::base::BumpPool* pool,
                                   VkCommandBuffer commandBuffer,
                                   const VkCopyImageToBufferInfo2* pCopyImageToBufferInfo);

    void on_vkCmdCopyBufferToImage2KHR(android::base::BumpPool* pool,
                                    VkCommandBuffer commandBuffer,
                                    const VkCopyBufferToImageInfo2KHR* pCopyBufferToImageInfo,
                                    const VkDecoderContext& context);

    void on_vkCmdCopyImage2KHR(android::base::BumpPool* pool,
                           VkCommandBuffer commandBuffer,
                           const VkCopyImageInfo2KHR* pCopyImageInfo);
    void on_vkCmdCopyImageToBuffer2KHR(android::base::BumpPool* pool,
                                   VkCommandBuffer commandBuffer,
                                   const VkCopyImageToBufferInfo2KHR* pCopyImageToBufferInfo);

    void on_vkGetImageMemoryRequirements(android::base::BumpPool* pool, VkDevice device,
                                         VkImage image, VkMemoryRequirements* pMemoryRequirements);

    void on_vkGetImageMemoryRequirements2(android::base::BumpPool* pool, VkDevice device,
                                          const VkImageMemoryRequirementsInfo2* pInfo,
                                          VkMemoryRequirements2* pMemoryRequirements);

    void on_vkGetImageMemoryRequirements2KHR(android::base::BumpPool* pool, VkDevice device,
                                             const VkImageMemoryRequirementsInfo2* pInfo,
                                             VkMemoryRequirements2* pMemoryRequirements);

    void on_vkGetBufferMemoryRequirements(android::base::BumpPool* pool, VkDevice device,
                                          VkBuffer buffer,
                                          VkMemoryRequirements* pMemoryRequirements);

    void on_vkGetBufferMemoryRequirements2(android::base::BumpPool* pool, VkDevice device,
                                           const VkBufferMemoryRequirementsInfo2* pInfo,
                                           VkMemoryRequirements2* pMemoryRequirements);

    void on_vkGetBufferMemoryRequirements2KHR(android::base::BumpPool* pool, VkDevice device,
                                              const VkBufferMemoryRequirementsInfo2* pInfo,
                                              VkMemoryRequirements2* pMemoryRequirements);

    void on_vkCmdPipelineBarrier(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                 VkPipelineStageFlags srcStageMask,
                                 VkPipelineStageFlags dstStageMask,
                                 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
                                 const VkMemoryBarrier* pMemoryBarriers,
                                 uint32_t bufferMemoryBarrierCount,
                                 const VkBufferMemoryBarrier* pBufferMemoryBarriers,
                                 uint32_t imageMemoryBarrierCount,
                                 const VkImageMemoryBarrier* pImageMemoryBarriers);

    // Do we need to wrap vk(Create|Destroy)Instance to
    // update our maps of VkDevices? Spec suggests no:
    // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkDestroyInstance.html
    // Valid Usage
    // All child objects created using instance
    // must have been destroyed prior to destroying instance
    //
    // This suggests that we should emulate the invalid behavior by
    // not destroying our own VkDevice maps on instance destruction.

    VkResult on_vkAllocateMemory(android::base::BumpPool* pool, VkDevice device,
                                 const VkMemoryAllocateInfo* pAllocateInfo,
                                 const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory);

    void on_vkFreeMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory,
                         const VkAllocationCallbacks* pAllocator);

    VkResult on_vkMapMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory,
                            VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags,
                            void** ppData);

    void on_vkUnmapMemory(android::base::BumpPool* pool, VkDevice device, VkDeviceMemory memory);

    uint8_t* getMappedHostPointer(VkDeviceMemory memory);
    VkDeviceSize getDeviceMemorySize(VkDeviceMemory memory);
    bool usingDirectMapping() const;

    struct HostFeatureSupport {
        bool supportsVulkan = false;
        bool supportsVulkan1_1 = false;
        bool useDeferredCommands = false;
        bool useCreateResourcesWithRequirements = false;
        uint32_t apiVersion = 0;
        uint32_t driverVersion = 0;
        uint32_t deviceID = 0;
        uint32_t vendorID = 0;
    };

    HostFeatureSupport getHostFeatureSupport() const;

    // VK_ANDROID_native_buffer
    VkResult on_vkGetSwapchainGrallocUsageANDROID(android::base::BumpPool* pool, VkDevice device,
                                                  VkFormat format, VkImageUsageFlags imageUsage,
                                                  int* grallocUsage);
    VkResult on_vkGetSwapchainGrallocUsage2ANDROID(
        android::base::BumpPool* pool, VkDevice device, VkFormat format,
        VkImageUsageFlags imageUsage, VkSwapchainImageUsageFlagsANDROID swapchainImageUsage,
        uint64_t* grallocConsumerUsage, uint64_t* grallocProducerUsage);
    VkResult on_vkAcquireImageANDROID(android::base::BumpPool* pool, VkDevice device, VkImage image,
                                      int nativeFenceFd, VkSemaphore semaphore, VkFence fence);
    VkResult on_vkQueueSignalReleaseImageANDROID(android::base::BumpPool* pool, VkQueue queue,
                                                 uint32_t waitSemaphoreCount,
                                                 const VkSemaphore* pWaitSemaphores, VkImage image,
                                                 int* pNativeFenceFd);

    // VK_GOOGLE_gfxstream
    VkResult on_vkMapMemoryIntoAddressSpaceGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                                  VkDeviceMemory memory, uint64_t* pAddress);
    VkResult on_vkGetMemoryHostAddressInfoGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                                 VkDeviceMemory memory, uint64_t* pAddress,
                                                 uint64_t* pSize, uint64_t* pHostmemId);
    VkResult on_vkGetBlobGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                VkDeviceMemory memory);

    VkResult on_vkFreeMemorySyncGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                       VkDeviceMemory memory,
                                       const VkAllocationCallbacks* pAllocator);

    VkResult on_vkAllocateCommandBuffers(android::base::BumpPool* pool, VkDevice device,
                                         const VkCommandBufferAllocateInfo* pAllocateInfo,
                                         VkCommandBuffer* pCommandBuffers);

    void on_vkCmdExecuteCommands(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                 uint32_t commandBufferCount,
                                 const VkCommandBuffer* pCommandBuffers);

    VkResult on_vkQueueSubmit(android::base::BumpPool* pool, VkQueue queue, uint32_t submitCount,
                              const VkSubmitInfo* pSubmits, VkFence fence);

    VkResult on_vkQueueSubmit2(android::base::BumpPool* pool, VkQueue queue, uint32_t submitCount,
                               const VkSubmitInfo2* pSubmits, VkFence fence);

    VkResult on_vkQueueWaitIdle(android::base::BumpPool* pool, VkQueue queue);

    VkResult on_vkResetCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                     VkCommandBufferResetFlags flags);

    void on_vkFreeCommandBuffers(android::base::BumpPool* pool, VkDevice device,
                                 VkCommandPool commandPool, uint32_t commandBufferCount,
                                 const VkCommandBuffer* pCommandBuffers);

    VkResult on_vkCreateCommandPool(android::base::BumpPool* pool, VkDevice device,
                                    const VkCommandPoolCreateInfo* pCreateInfo,
                                    const VkAllocationCallbacks* pAllocator,
                                    VkCommandPool* pCommandPool);

    void on_vkDestroyCommandPool(android::base::BumpPool* pool, VkDevice device,
                                 VkCommandPool commandPool,
                                 const VkAllocationCallbacks* pAllocator);

    VkResult on_vkResetCommandPool(android::base::BumpPool* pool, VkDevice device,
                                   VkCommandPool commandPool, VkCommandPoolResetFlags flags);

    void on_vkGetPhysicalDeviceExternalSemaphoreProperties(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
        VkExternalSemaphoreProperties* pExternalSemaphoreProperties);

    void on_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR(
        android::base::BumpPool* pool, VkPhysicalDevice physicalDevice,
        const VkPhysicalDeviceExternalSemaphoreInfo* pExternalSemaphoreInfo,
        VkExternalSemaphoreProperties* pExternalSemaphoreProperties);

    VkResult on_vkCreateSemaphore(android::base::BumpPool* pool, VkDevice device,
                                  const VkSemaphoreCreateInfo* pCreateInfo,
                                  const VkAllocationCallbacks* pAllocator, VkSemaphore* pSemaphore);
    VkResult on_vkImportSemaphoreFdKHR(android::base::BumpPool* pool, VkDevice device,
                                       const VkImportSemaphoreFdInfoKHR* pImportSemaphoreFdInfo);
    VkResult on_vkGetSemaphoreFdKHR(android::base::BumpPool* pool, VkDevice boxed_device,
                                    const VkSemaphoreGetFdInfoKHR* pGetFdInfo, int* pFd);
    void on_vkDestroySemaphore(android::base::BumpPool* pool, VkDevice boxed_device,
                               VkSemaphore semaphore, const VkAllocationCallbacks* pAllocator);

    VkResult on_vkCreateFence(android::base::BumpPool* pool, VkDevice device,
                              const VkFenceCreateInfo* pCreateInfo,
                              const VkAllocationCallbacks* pAllocator, VkFence* pFence);
    VkResult on_vkResetFences(android::base::BumpPool* pool, VkDevice device, uint32_t fenceCount,
                              const VkFence* pFences);
    void on_vkDestroyFence(android::base::BumpPool* pool, VkDevice device, VkFence fence,
                           const VkAllocationCallbacks* pAllocator);

    // Descriptor update templates
    VkResult on_vkCreateDescriptorUpdateTemplate(
        android::base::BumpPool* pool, VkDevice boxed_device,
        const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
        const VkAllocationCallbacks* pAllocator,
        VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);

    VkResult on_vkCreateDescriptorUpdateTemplateKHR(
        android::base::BumpPool* pool, VkDevice boxed_device,
        const VkDescriptorUpdateTemplateCreateInfo* pCreateInfo,
        const VkAllocationCallbacks* pAllocator,
        VkDescriptorUpdateTemplate* pDescriptorUpdateTemplate);

    void on_vkDestroyDescriptorUpdateTemplate(android::base::BumpPool* pool, VkDevice boxed_device,
                                              VkDescriptorUpdateTemplate descriptorUpdateTemplate,
                                              const VkAllocationCallbacks* pAllocator);

    void on_vkDestroyDescriptorUpdateTemplateKHR(
        android::base::BumpPool* pool, VkDevice boxed_device,
        VkDescriptorUpdateTemplate descriptorUpdateTemplate,
        const VkAllocationCallbacks* pAllocator);

    void on_vkUpdateDescriptorSetWithTemplateSizedGOOGLE(
        android::base::BumpPool* pool, VkDevice boxed_device, VkDescriptorSet descriptorSet,
        VkDescriptorUpdateTemplate descriptorUpdateTemplate, uint32_t imageInfoCount,
        uint32_t bufferInfoCount, uint32_t bufferViewCount, const uint32_t* pImageInfoEntryIndices,
        const uint32_t* pBufferInfoEntryIndices, const uint32_t* pBufferViewEntryIndices,
        const VkDescriptorImageInfo* pImageInfos, const VkDescriptorBufferInfo* pBufferInfos,
        const VkBufferView* pBufferViews);

    void on_vkUpdateDescriptorSetWithTemplateSized2GOOGLE(
        android::base::BumpPool* pool, VkDevice boxed_device, VkDescriptorSet descriptorSet,
        VkDescriptorUpdateTemplate descriptorUpdateTemplate, uint32_t imageInfoCount,
        uint32_t bufferInfoCount, uint32_t bufferViewCount, uint32_t inlineUniformBlockCount,
        const uint32_t* pImageInfoEntryIndices, const uint32_t* pBufferInfoEntryIndices,
        const uint32_t* pBufferViewEntryIndices, const VkDescriptorImageInfo* pImageInfos,
        const VkDescriptorBufferInfo* pBufferInfos, const VkBufferView* pBufferViews,
        const uint8_t* pInlineUniformBlockData);

    VkResult on_vkBeginCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                     const VkCommandBufferBeginInfo* pBeginInfo,
                                     const VkDecoderContext& context);
    void on_vkBeginCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
                                            VkCommandBuffer commandBuffer,
                                            const VkCommandBufferBeginInfo* pBeginInfo,
                                            const VkDecoderContext& context);
    VkResult on_vkEndCommandBuffer(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                   const VkDecoderContext& context);
    void on_vkEndCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
                                          VkCommandBuffer commandBuffer,
                                          const VkDecoderContext& context);
    void on_vkResetCommandBufferAsyncGOOGLE(android::base::BumpPool* pool,
                                            VkCommandBuffer commandBuffer,
                                            VkCommandBufferResetFlags flags);
    void on_vkCommandBufferHostSyncGOOGLE(android::base::BumpPool* pool,
                                          VkCommandBuffer commandBuffer, uint32_t needHostSync,
                                          uint32_t sequenceNumber);

    VkResult on_vkCreateImageWithRequirementsGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                                    const VkImageCreateInfo* pCreateInfo,
                                                    const VkAllocationCallbacks* pAllocator,
                                                    VkImage* pImage,
                                                    VkMemoryRequirements* pMemoryRequirements);
    VkResult on_vkCreateBufferWithRequirementsGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                                     const VkBufferCreateInfo* pCreateInfo,
                                                     const VkAllocationCallbacks* pAllocator,
                                                     VkBuffer* pBuffer,
                                                     VkMemoryRequirements* pMemoryRequirements);

    void on_vkCmdBindPipeline(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                              VkPipelineBindPoint pipelineBindPoint, VkPipeline pipeline);

    void on_vkCmdBindDescriptorSets(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                    VkPipelineBindPoint pipelineBindPoint, VkPipelineLayout layout,
                                    uint32_t firstSet, uint32_t descriptorSetCount,
                                    const VkDescriptorSet* pDescriptorSets,
                                    uint32_t dynamicOffsetCount, const uint32_t* pDynamicOffsets);

    VkResult on_vkCreateRenderPass(android::base::BumpPool* pool, VkDevice device,
                                   const VkRenderPassCreateInfo* pCreateInfo,
                                   const VkAllocationCallbacks* pAllocator,
                                   VkRenderPass* pRenderPass);
    VkResult on_vkCreateRenderPass2(android::base::BumpPool* pool, VkDevice device,
                                    const VkRenderPassCreateInfo2* pCreateInfo,
                                    const VkAllocationCallbacks* pAllocator,
                                    VkRenderPass* pRenderPass);
    VkResult on_vkCreateRenderPass2KHR(android::base::BumpPool* pool, VkDevice device,
                                       const VkRenderPassCreateInfo2KHR* pCreateInfo,
                                       const VkAllocationCallbacks* pAllocator,
                                       VkRenderPass* pRenderPass);
    void on_vkDestroyRenderPass(android::base::BumpPool* pool, VkDevice device,
                                VkRenderPass renderPass, const VkAllocationCallbacks* pAllocator);
    void on_vkCmdBeginRenderPass(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                 const VkRenderPassBeginInfo* pRenderPassBegin,
                                 VkSubpassContents contents);
    void on_vkCmdBeginRenderPass2(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                  const VkRenderPassBeginInfo* pRenderPassBegin,
                                  const VkSubpassBeginInfo* pSubpassBeginInfo);
    void on_vkCmdBeginRenderPass2KHR(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                     const VkRenderPassBeginInfo* pRenderPassBegin,
                                     const VkSubpassBeginInfo* pSubpassBeginInfo);

    VkResult on_vkCreateFramebuffer(android::base::BumpPool* pool, VkDevice device,
                                    const VkFramebufferCreateInfo* pCreateInfo,
                                    const VkAllocationCallbacks* pAllocator,
                                    VkFramebuffer* pFramebuffer);
    void on_vkDestroyFramebuffer(android::base::BumpPool* pool, VkDevice device,
                                 VkFramebuffer framebuffer,
                                 const VkAllocationCallbacks* pAllocator);

    void on_vkCmdCopyQueryPoolResults(android::base::BumpPool* pool, VkCommandBuffer commandBuffer,
                                      VkQueryPool queryPool, uint32_t firstQuery,
                                      uint32_t queryCount, VkBuffer dstBuffer,
                                      VkDeviceSize dstOffset, VkDeviceSize stride,
                                      VkQueryResultFlags flags);

    // VK_GOOGLE_gfxstream
    void on_vkQueueHostSyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                  uint32_t needHostSync, uint32_t sequenceNumber);
    void on_vkQueueSubmitAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                     uint32_t submitCount, const VkSubmitInfo* pSubmits,
                                     VkFence fence);
    void on_vkQueueSubmitAsync2GOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                      uint32_t submitCount, const VkSubmitInfo2* pSubmits,
                                      VkFence fence);
    void on_vkQueueWaitIdleAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue);
    void on_vkQueueBindSparseAsyncGOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                         uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
                                         VkFence fence);

    // VK_GOOGLE_gfxstream
    void on_vkGetLinearImageLayoutGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                         VkFormat format, VkDeviceSize* pOffset,
                                         VkDeviceSize* pRowPitchAlignment);
    void on_vkGetLinearImageLayout2GOOGLE(android::base::BumpPool* pool, VkDevice device,
                                          const VkImageCreateInfo* pCreateInfo,
                                          VkDeviceSize* pOffset, VkDeviceSize* pRowPitchAlignment);

    // VK_GOOGLE_gfxstream
    void on_vkQueueFlushCommandsGOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                       VkCommandBuffer commandBuffer, VkDeviceSize dataSize,
                                       const void* pData, const VkDecoderContext& context);
    void on_vkQueueFlushCommandsFromAuxMemoryGOOGLE(android::base::BumpPool* pool, VkQueue queue,
                                                    VkCommandBuffer commandBuffer,
                                                    VkDeviceMemory deviceMemory,
                                                    VkDeviceSize dataOffset, VkDeviceSize dataSize,
                                                    const VkDecoderContext& context);
    void on_vkQueueCommitDescriptorSetUpdatesGOOGLE(
        android::base::BumpPool* pool, VkQueue queue, uint32_t descriptorPoolCount,
        const VkDescriptorPool* pDescriptorPools, uint32_t descriptorSetCount,
        const VkDescriptorSetLayout* pDescriptorSetLayouts, const uint64_t* pDescriptorSetPoolIds,
        const uint32_t* pDescriptorSetWhichPool, const uint32_t* pDescriptorSetPendingAllocation,
        const uint32_t* pDescriptorWriteStartingIndices, uint32_t pendingDescriptorWriteCount,
        const VkWriteDescriptorSet* pPendingDescriptorWrites);
    void on_vkCollectDescriptorPoolIdsGOOGLE(android::base::BumpPool* pool, VkDevice device,
                                             VkDescriptorPool descriptorPool,
                                             uint32_t* pPoolIdCount, uint64_t* pPoolIds);
    VkResult on_vkQueueBindSparse(android::base::BumpPool* pool, VkQueue queue,
                                  uint32_t bindInfoCount, const VkBindSparseInfo* pBindInfo,
                                  VkFence fence);
    void on_vkQueueSignalReleaseImageANDROIDAsyncGOOGLE(android::base::BumpPool* pool,
                                                        VkQueue queue, uint32_t waitSemaphoreCount,
                                                        const VkSemaphore* pWaitSemaphores,
                                                        VkImage image);

    VkResult on_vkCreateSamplerYcbcrConversion(
        android::base::BumpPool* pool, VkDevice device,
        const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
        const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
    VkResult on_vkCreateSamplerYcbcrConversionKHR(
        android::base::BumpPool* pool, VkDevice device,
        const VkSamplerYcbcrConversionCreateInfo* pCreateInfo,
        const VkAllocationCallbacks* pAllocator, VkSamplerYcbcrConversion* pYcbcrConversion);
    void on_vkDestroySamplerYcbcrConversion(android::base::BumpPool* pool, VkDevice device,
                                            VkSamplerYcbcrConversion ycbcrConversion,
                                            const VkAllocationCallbacks* pAllocator);
    void on_vkDestroySamplerYcbcrConversionKHR(android::base::BumpPool* pool, VkDevice device,
                                               VkSamplerYcbcrConversion ycbcrConversion,
                                               const VkAllocationCallbacks* pAllocator);

    void on_DeviceLost();

    void DeviceLostHandler();

    void on_CheckOutOfMemory(VkResult result, uint32_t opCode, const VkDecoderContext& context,
                             std::optional<uint64_t> allocationSize = std::nullopt);

    // Fence waits
    VkResult waitForFence(VkFence boxed_fence, uint64_t timeout);

    VkResult getFenceStatus(VkFence boxed_fence);

    // Wait for present (vkQueueSignalReleaseImageANDROID). This explicitly
    // requires the image to be presented again versus how many times it's been
    // presented so far, so it ends up incrementing a "target present count"
    // for this image, and then waiting for the image to get vkQSRI'ed at least
    // that many times.
    AsyncResult registerQsriCallback(VkImage boxed_image, VkQsriTimeline::Callback callback);

    // Transformations
    void deviceMemoryTransform_tohost(VkDeviceMemory* memory, uint32_t memoryCount,
                                      VkDeviceSize* offset, uint32_t offsetCount,
                                      VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex,
                                      uint32_t typeIndexCount, uint32_t* typeBits,
                                      uint32_t typeBitsCount);
    void deviceMemoryTransform_fromhost(VkDeviceMemory* memory, uint32_t memoryCount,
                                        VkDeviceSize* offset, uint32_t offsetCount,
                                        VkDeviceSize* size, uint32_t sizeCount, uint32_t* typeIndex,
                                        uint32_t typeIndexCount, uint32_t* typeBits,
                                        uint32_t typeBitsCount);

    // Snapshot access
    VkDecoderSnapshot* snapshot();

#define DEFINE_TRANSFORMED_TYPE_PROTOTYPE(type)                \
    void transformImpl_##type##_tohost(const type*, uint32_t); \
    void transformImpl_##type##_fromhost(const type*, uint32_t);

    LIST_TRANSFORMED_TYPES(DEFINE_TRANSFORMED_TYPE_PROTOTYPE)

    // boxed handles
#define DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL(type)                                 \
    type new_boxed_##type(type underlying, VulkanDispatch* dispatch, bool ownDispatch); \
    void delete_##type(type boxed);                                                     \
    type unbox_##type(type boxed);                                                      \
    type unboxed_to_boxed_##type(type boxed);                                           \
    VulkanDispatch* dispatch_##type(type boxed);

#define DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL(type)  \
    type new_boxed_non_dispatchable_##type(type underlying); \
    void delete_##type(type boxed);                          \
    type unbox_##type(type boxed);                           \
    type unboxed_to_boxed_non_dispatchable_##type(type boxed);

    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_DISPATCHABLE_HANDLE_API_DECL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(DEFINE_BOXED_NON_DISPATCHABLE_HANDLE_API_DECL)

   private:
    class Impl;
    std::unique_ptr<Impl> mImpl;
};

#define MAKE_HANDLE_MAPPING_FOREACH(type_name, map_impl, map_to_u64_impl, map_from_u64_impl)       \
    void mapHandles_##type_name(type_name* handles, size_t count) override {                       \
        for (size_t i = 0; i < count; ++i) {                                                       \
            map_impl;                                                                              \
        }                                                                                          \
    }                                                                                              \
    void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s,             \
                                      size_t count) override {                                     \
        for (size_t i = 0; i < count; ++i) {                                                       \
            map_to_u64_impl;                                                                       \
        }                                                                                          \
    }                                                                                              \
    void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) \
        override {                                                                                 \
        for (size_t i = 0; i < count; ++i) {                                                       \
            map_from_u64_impl;                                                                     \
        }                                                                                          \
    }

#define BOXED_DISPATCHABLE_UNWRAP_IMPL(type_name)                                                  \
    MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
        type_name, if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { \
            handles[i] = (type_name) nullptr;                                                      \
        };                                                                                         \
        ,                                                                                          \
        if (handles[i]) {                                                                          \
            handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);                     \
        } else { handle_u64s[i] = 0; },                                                            \
        if (handle_u64s[i]) {                                                                      \
            handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]);         \
        } else { handles[i] = (type_name) nullptr; })

#define BOXED_NON_DISPATCHABLE_UNWRAP_IMPL(type_name)                                              \
    MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
        type_name, if (handles[i]) { handles[i] = m_state->unbox_##type_name(handles[i]); } else { \
            handles[i] = (type_name) nullptr;                                                      \
        };                                                                                         \
        ,                                                                                          \
        if (handles[i]) {                                                                          \
            handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);                     \
        } else { handle_u64s[i] = 0; },                                                            \
        if (handle_u64s[i]) {                                                                      \
            handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]);         \
        } else { handles[i] = (type_name) nullptr; })

class BoxedHandleUnwrapMapping : public VulkanHandleMapping {
   public:
    BoxedHandleUnwrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
    virtual ~BoxedHandleUnwrapMapping() {}
    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_UNWRAP_IMPL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_IMPL)
};

#define BOXED_DISPATCHABLE_WRAP_IMPL(type_name)                                                    \
    MAKE_HANDLE_MAPPING_FOREACH(                                                                   \
        type_name,                                                                                 \
        if (handles[i]) { handles[i] = m_state->unboxed_to_boxed_##type_name(handles[i]); } else { \
            handles[i] = (type_name) nullptr;                                                      \
        };                                                                                         \
        ,                                                                                          \
        if (handles[i]) {                                                                          \
            handle_u64s[i] = (uint64_t)m_state->unboxed_to_boxed_##type_name(handles[i]);          \
        } else { handle_u64s[i] = 0; },                                                            \
        if (handle_u64s[i]) {                                                                      \
            handles[i] =                                                                           \
                m_state->unboxed_to_boxed_##type_name((type_name)(uintptr_t)handle_u64s[i]);       \
        } else { handles[i] = (type_name) nullptr; })

#define BOXED_NON_DISPATCHABLE_WRAP_IMPL(type_name)                                           \
    MAKE_HANDLE_MAPPING_FOREACH(                                                              \
        type_name,                                                                            \
        if (handles[i]) {                                                                     \
            handles[i] = m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]);  \
        } else { handles[i] = (type_name) nullptr; };                                         \
        ,                                                                                     \
        if (handles[i]) {                                                                     \
            handle_u64s[i] =                                                                  \
                (uint64_t)m_state->unboxed_to_boxed_non_dispatchable_##type_name(handles[i]); \
        } else { handle_u64s[i] = 0; },                                                       \
        if (handle_u64s[i]) {                                                                 \
            handles[i] = m_state->unboxed_to_boxed_non_dispatchable_##type_name(              \
                (type_name)(uintptr_t)handle_u64s[i]);                                        \
        } else { handles[i] = (type_name) nullptr; })

class BoxedHandleWrapMapping : public VulkanHandleMapping {
   public:
    BoxedHandleWrapMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
    virtual ~BoxedHandleWrapMapping() {}
    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_WRAP_IMPL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_WRAP_IMPL)
};

// Not used, so we do not define.
#define BOXED_DISPATCHABLE_CREATE_IMPL(type_name)                                  \
    MAKE_HANDLE_MAPPING_FOREACH(type_name, (void)handles[i], (void)handle_u64s[i], \
                                (void)handles[i];)

// Not used, so we do not define.
#define BOXED_DISPATCHABLE_DESTROY_IMPL(type_name)                                 \
    MAKE_HANDLE_MAPPING_FOREACH(type_name, (void)handles[i], (void)handle_u64s[i], \
                                (void)handles[i];)

// We only use the create/destroy mappings for non dispatchable handles.
#define BOXED_NON_DISPATCHABLE_CREATE_IMPL(type_name)                                    \
    MAKE_HANDLE_MAPPING_FOREACH(                                                         \
        type_name, handles[i] = new_boxed_non_dispatchable_##type_name(handles[i]);      \
        , handle_u64s[i] = (uint64_t)new_boxed_non_dispatchable_##type_name(handles[i]), \
        handles[i] = (type_name)new_boxed_non_dispatchable_##type_name(                  \
            (type_name)(uintptr_t)handle_u64s[i]);)

#define BOXED_NON_DISPATCHABLE_DESTROY_IMPL(type_name)                                           \
    MAKE_HANDLE_MAPPING_FOREACH(type_name, delete_##type_name(handles[i]), (void)handle_u64s[i]; \
                                delete_##type_name(handles[i]), (void)handles[i];                \
                                delete_##type_name((type_name)handle_u64s[i]))

#define BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL(type_name)                           \
    MAKE_HANDLE_MAPPING_FOREACH(                                                           \
        type_name,                                                                         \
        if (handles[i]) {                                                                  \
            auto boxed = handles[i];                                                       \
            handles[i] = m_state->unbox_##type_name(handles[i]);                           \
            delete_##type_name(boxed);                                                     \
        } else { handles[i] = (type_name) nullptr; };                                      \
        ,                                                                                  \
        if (handles[i]) {                                                                  \
            auto boxed = handles[i];                                                       \
            handle_u64s[i] = (uint64_t)m_state->unbox_##type_name(handles[i]);             \
            delete_##type_name(boxed);                                                     \
        } else { handle_u64s[i] = 0; },                                                    \
        if (handle_u64s[i]) {                                                              \
            auto boxed = (type_name)(uintptr_t)handle_u64s[i];                             \
            handles[i] = m_state->unbox_##type_name((type_name)(uintptr_t)handle_u64s[i]); \
            delete_##type_name(boxed);                                                     \
        } else { handles[i] = (type_name) nullptr; })

class BoxedHandleCreateMapping : public VulkanHandleMapping {
   public:
    BoxedHandleCreateMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
    virtual ~BoxedHandleCreateMapping() {}
    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_CREATE_IMPL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_CREATE_IMPL)
};

class BoxedHandleDestroyMapping : public VulkanHandleMapping {
   public:
    BoxedHandleDestroyMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
    virtual ~BoxedHandleDestroyMapping() {}
    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_DESTROY_IMPL)
};

class BoxedHandleUnwrapAndDeleteMapping : public VulkanHandleMapping {
   public:
    BoxedHandleUnwrapAndDeleteMapping(VkDecoderGlobalState* state) : VulkanHandleMapping(state) {}
    virtual ~BoxedHandleUnwrapAndDeleteMapping() {}
    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(BOXED_DISPATCHABLE_DESTROY_IMPL)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(BOXED_NON_DISPATCHABLE_UNWRAP_AND_DELETE_IMPL)
};

#define HANDLE_MAPPING_DECLS(type_name)                                                            \
    void mapHandles_##type_name(type_name* handles, size_t count) override;                        \
    void mapHandles_##type_name##_u64(const type_name* handles, uint64_t* handle_u64s,             \
                                      size_t count) override;                                      \
    void mapHandles_u64_##type_name(const uint64_t* handle_u64s, type_name* handles, size_t count) \
        override;

class BoxedHandleUnwrapAndDeletePreserveBoxedMapping : public VulkanHandleMapping {
   public:
    BoxedHandleUnwrapAndDeletePreserveBoxedMapping(VkDecoderGlobalState* state)
        : VulkanHandleMapping(state) {}
    void setup(android::base::BumpPool* pool, uint64_t** bufPtr);
    virtual ~BoxedHandleUnwrapAndDeletePreserveBoxedMapping() {}

    GOLDFISH_VK_LIST_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)
    GOLDFISH_VK_LIST_NON_DISPATCHABLE_HANDLE_TYPES(HANDLE_MAPPING_DECLS)

   private:
    void allocPreserve(size_t count);

    android::base::BumpPool* mPool = nullptr;
    uint64_t** mPreserveBufPtr = nullptr;
};

}  // namespace vk
}  // namespace gfxstream