1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef DAWNNATIVE_METAL_TEXTUREMTL_H_ 16 #define DAWNNATIVE_METAL_TEXTUREMTL_H_ 17 18 #include "dawn_native/Texture.h" 19 20 #include "common/NSRef.h" 21 #include "dawn_native/DawnNative.h" 22 23 #include <IOSurface/IOSurfaceRef.h> 24 #import <Metal/Metal.h> 25 26 namespace dawn_native { namespace metal { 27 28 class CommandRecordingContext; 29 class Device; 30 31 MTLPixelFormat MetalPixelFormat(wgpu::TextureFormat format); 32 MaybeError ValidateIOSurfaceCanBeWrapped(const DeviceBase* device, 33 const TextureDescriptor* descriptor, 34 IOSurfaceRef ioSurface, 35 uint32_t plane); 36 37 class Texture final : public TextureBase { 38 public: 39 static ResultOrError<Ref<Texture>> Create(Device* device, 40 const TextureDescriptor* descriptor); 41 static ResultOrError<Ref<Texture>> CreateFromIOSurface( 42 Device* device, 43 const ExternalImageDescriptor* descriptor, 44 IOSurfaceRef ioSurface, 45 uint32_t plane); 46 static Ref<Texture> CreateWrapping(Device* device, 47 const TextureDescriptor* descriptor, 48 NSPRef<id<MTLTexture>> wrapped); 49 50 id<MTLTexture> GetMTLTexture(); 51 52 void EnsureSubresourceContentInitialized(CommandRecordingContext* commandContext, 53 const SubresourceRange& range); 54 55 private: 56 using TextureBase::TextureBase; 57 ~Texture() override; 58 59 NSRef<MTLTextureDescriptor> CreateMetalTextureDescriptor() const; 60 61 MaybeError InitializeAsInternalTexture(const TextureDescriptor* descriptor); 62 MaybeError InitializeFromIOSurface(const ExternalImageDescriptor* descriptor, 63 const TextureDescriptor* textureDescriptor, 64 IOSurfaceRef ioSurface, 65 uint32_t plane); 66 void InitializeAsWrapping(const TextureDescriptor* descriptor, 67 NSPRef<id<MTLTexture>> wrapped); 68 69 void DestroyImpl() override; 70 71 MaybeError ClearTexture(CommandRecordingContext* commandContext, 72 const SubresourceRange& range, 73 TextureBase::ClearValue clearValue); 74 75 NSPRef<id<MTLTexture>> mMtlTexture; 76 MTLTextureUsage mMtlUsage; 77 }; 78 79 class TextureView final : public TextureViewBase { 80 public: 81 static ResultOrError<Ref<TextureView>> Create(TextureBase* texture, 82 const TextureViewDescriptor* descriptor); 83 84 id<MTLTexture> GetMTLTexture(); 85 86 private: 87 using TextureViewBase::TextureViewBase; 88 MaybeError Initialize(const TextureViewDescriptor* descriptor); 89 90 NSPRef<id<MTLTexture>> mMtlTextureView; 91 }; 92 93 }} // namespace dawn_native::metal 94 95 #endif // DAWNNATIVE_METAL_TEXTUREMTL_H_ 96