@@ -241,11 +241,12 @@ static VmaAllocationCreateFlags ToVmaAllocationCreateFlags(StorageMode mode,
241241
242242class AllocatedTextureSourceVK final : public TextureSourceVK {
243243 public:
244- AllocatedTextureSourceVK (const TextureDescriptor& desc,
244+ AllocatedTextureSourceVK (std::weak_ptr<ResourceManagerVK> resource_manager,
245+ const TextureDescriptor& desc,
245246 VmaAllocator allocator,
246247 vk::Device device,
247248 bool supports_memoryless_textures)
248- : TextureSourceVK(desc) {
249+ : TextureSourceVK(desc), resource_(std::move(resource_manager)) {
249250 TRACE_EVENT0 (" impeller" , " CreateDeviceTexture" );
250251 vk::ImageCreateInfo image_info;
251252 image_info.flags = ToVKImageCreateFlags (desc.type );
@@ -305,12 +306,10 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
305306 }
306307 }
307308
308- image_ = vk::Image{vk_image};
309- allocator_ = allocator;
310- allocation_ = allocation;
309+ auto image = vk::Image{vk_image};
311310
312311 vk::ImageViewCreateInfo view_info = {};
313- view_info.image = image_ ;
312+ view_info.image = image ;
314313 view_info.viewType = ToVKImageViewType (desc.type );
315314 view_info.format = image_info.format ;
316315 view_info.subresourceRange .aspectMask = ToVKImageAspectFlags (desc.format );
@@ -332,34 +331,65 @@ class AllocatedTextureSourceVK final : public TextureSourceVK {
332331 << vk::to_string (result);
333332 return ;
334333 }
335- image_view_ = std::move (image_view);
336-
334+ resource_. Reset (
335+ ImageResource (image, allocator, allocation, std::move (image_view)));
337336 is_valid_ = true ;
338337 }
339338
340- ~AllocatedTextureSourceVK () {
341- TRACE_EVENT0 (" impeller" , " DestroyDeviceTexture" );
342- image_view_.reset ();
343- if (image_) {
344- ::vmaDestroyImage (
345- allocator_, //
346- static_cast <typename decltype (image_)::NativeType>(image_), //
347- allocation_ //
348- );
349- }
350- }
339+ ~AllocatedTextureSourceVK () = default ;
351340
352341 bool IsValid () const { return is_valid_; }
353342
354- vk::Image GetImage () const override { return image_ ; }
343+ vk::Image GetImage () const override { return resource_-> image ; }
355344
356- vk::ImageView GetImageView () const override { return image_view_.get (); }
345+ vk::ImageView GetImageView () const override {
346+ return resource_->image_view .get ();
347+ }
357348
358349 private:
359- vk::Image image_ = {};
360- VmaAllocator allocator_ = {};
361- VmaAllocation allocation_ = {};
362- vk::UniqueImageView image_view_;
350+ struct ImageResource {
351+ vk::Image image = {};
352+ VmaAllocator allocator = {};
353+ VmaAllocation allocation = {};
354+ vk::UniqueImageView image_view;
355+
356+ ImageResource () = default ;
357+
358+ ImageResource (vk::Image p_image,
359+ VmaAllocator p_allocator,
360+ VmaAllocation p_allocation,
361+ vk::UniqueImageView p_image_view)
362+ : image(p_image),
363+ allocator (p_allocator),
364+ allocation(p_allocation),
365+ image_view(std::move(p_image_view)) {}
366+
367+ ImageResource (ImageResource&& o) {
368+ std::swap (image, o.image );
369+ std::swap (allocator, o.allocator );
370+ std::swap (allocation, o.allocation );
371+ std::swap (image_view, o.image_view );
372+ }
373+
374+ ~ImageResource () {
375+ if (!image) {
376+ return ;
377+ }
378+ TRACE_EVENT0 (" impeller" , " DestroyDeviceTexture" );
379+ image_view.reset ();
380+ if (image) {
381+ ::vmaDestroyImage (
382+ allocator, //
383+ static_cast <typename decltype (image)::NativeType>(image), //
384+ allocation //
385+ );
386+ }
387+ }
388+
389+ FML_DISALLOW_COPY_AND_ASSIGN (ImageResource);
390+ };
391+
392+ UniqueResourceVKT<ImageResource> resource_;
363393 bool is_valid_ = false ;
364394
365395 FML_DISALLOW_COPY_AND_ASSIGN (AllocatedTextureSourceVK);
@@ -376,11 +406,16 @@ std::shared_ptr<Texture> AllocatorVK::OnCreateTexture(
376406 if (!device_holder) {
377407 return nullptr ;
378408 }
409+ auto context = context_.lock ();
410+ if (!context) {
411+ return nullptr ;
412+ }
379413 auto source = std::make_shared<AllocatedTextureSourceVK>(
380- desc, //
381- allocator_, //
382- device_holder->GetDevice (), //
383- supports_memoryless_textures_ //
414+ ContextVK::Cast (*context).GetResourceManager (), //
415+ desc, //
416+ allocator_, //
417+ device_holder->GetDevice (), //
418+ supports_memoryless_textures_ //
384419 );
385420 if (!source->IsValid ()) {
386421 return nullptr ;
0 commit comments