|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +#if os(Windows) |
| 12 | +@_spi(Experimental) import Testing |
| 13 | + |
| 14 | +public import WinSDK |
| 15 | + |
| 16 | +/// A protocol describing images that can be converted to instances of |
| 17 | +/// ``Testing/Attachment``. |
| 18 | +/// |
| 19 | +/// Instances of types conforming to this protocol do not themselves conform to |
| 20 | +/// ``Testing/Attachable``. Instead, the testing library provides additional |
| 21 | +/// initializers on ``Testing/Attachment`` that take instances of such types and |
| 22 | +/// handle converting them to image data when needed. |
| 23 | +/// |
| 24 | +/// The following system-provided image types conform to this protocol and can |
| 25 | +/// be attached to a test: |
| 26 | +/// |
| 27 | +/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps) |
| 28 | +/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons) |
| 29 | +/// - [`IWICBitmap`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmap) |
| 30 | +/// |
| 31 | +/// You do not generally need to add your own conformances to this protocol. If |
| 32 | +/// you have an image in another format that needs to be attached to a test, |
| 33 | +/// first convert it to an instance of one of the types above. |
| 34 | +@_spi(Experimental) |
| 35 | +public protocol _AttachableByAddressAsIWICBitmap { |
| 36 | + /// Create a WIC bitmap representing an instance of this type at the given |
| 37 | + /// address. |
| 38 | + /// |
| 39 | + /// - Parameters: |
| 40 | + /// - imageAddress: The address of the instance of this type. |
| 41 | + /// - factory: A WIC imaging factory that can be used to create additional |
| 42 | + /// WIC objects. |
| 43 | + /// |
| 44 | + /// - Returns: A pointer to a new WIC bitmap representing this image. The |
| 45 | + /// caller is responsible for releasing this image when done with it. |
| 46 | + /// |
| 47 | + /// - Throws: Any error that prevented the creation of the WIC bitmap. |
| 48 | + /// |
| 49 | + /// This function is not part of the public interface of the testing library. |
| 50 | + /// It may be removed in a future update. |
| 51 | + static func _copyAttachableIWICBitmap( |
| 52 | + from imageAddress: UnsafeMutablePointer<Self>, |
| 53 | + using factory: UnsafeMutablePointer<IWICImagingFactory> |
| 54 | + ) throws -> UnsafeMutablePointer<IWICBitmap> |
| 55 | + |
| 56 | + /// Make a copy of the instance of this type at the given address. |
| 57 | + /// |
| 58 | + /// - Parameters: |
| 59 | + /// - imageAddress: The address of the instance of this type that should be |
| 60 | + /// copied. |
| 61 | + /// |
| 62 | + /// - Returns: A copy of `imageAddress`, or `imageAddress` if this type does |
| 63 | + /// not support a copying operation. |
| 64 | + /// |
| 65 | + /// - Throws: Any error that prevented copying the value at `imageAddress`. |
| 66 | + /// |
| 67 | + /// The testing library uses this function to take ownership of image |
| 68 | + /// resources that test authors pass to it. If possible, make a copy of or add |
| 69 | + /// a reference to the value at `imageAddress`. If this type does not support |
| 70 | + /// making copies, return `imageAddress` verbatim. |
| 71 | + /// |
| 72 | + /// This function is not part of the public interface of the testing library. |
| 73 | + /// It may be removed in a future update. |
| 74 | + static func _copyAttachableValue(at imageAddress: UnsafeMutablePointer<Self>) throws -> UnsafeMutablePointer<Self> |
| 75 | + |
| 76 | + /// Manually deinitialize any resources at the given address. |
| 77 | + /// |
| 78 | + /// - Parameters: |
| 79 | + /// - imageAddress: The address of the instance of this type. |
| 80 | + /// |
| 81 | + /// The implementation of this function is responsible for balancing a |
| 82 | + /// previous call to `_copyAttachableValue(at:)` by cleaning up any resources |
| 83 | + /// (such as handles or COM objects) associated with the value at |
| 84 | + /// `imageAddress`. The testing library automatically invokes this function as |
| 85 | + /// needed. If `_copyAttachableValue(at:)` threw an error, the testing library |
| 86 | + /// does not call this function. |
| 87 | + /// |
| 88 | + /// This function is not responsible for releasing the image returned from |
| 89 | + /// `_copyAttachableIWICBitmap(from:using:)`. |
| 90 | + /// |
| 91 | + /// This function is not part of the public interface of the testing library. |
| 92 | + /// It may be removed in a future update. |
| 93 | + static func _deinitializeAttachableValue(at imageAddress: UnsafeMutablePointer<Self>) |
| 94 | +} |
| 95 | + |
| 96 | +/// A protocol describing images that can be converted to instances of |
| 97 | +/// ``Testing/Attachment``. |
| 98 | +/// |
| 99 | +/// Instances of types conforming to this protocol do not themselves conform to |
| 100 | +/// ``Testing/Attachable``. Instead, the testing library provides additional |
| 101 | +/// initializers on ``Testing/Attachment`` that take instances of such types and |
| 102 | +/// handle converting them to image data when needed. |
| 103 | +/// |
| 104 | +/// The following system-provided image types conform to this protocol and can |
| 105 | +/// be attached to a test: |
| 106 | +/// |
| 107 | +/// - [`HBITMAP`](https://learn.microsoft.com/en-us/windows/win32/gdi/bitmaps) |
| 108 | +/// - [`HICON`](https://learn.microsoft.com/en-us/windows/win32/menurc/icons) |
| 109 | +/// - [`IWICBitmap`](https://learn.microsoft.com/en-us/windows/win32/api/wincodec/nn-wincodec-iwicbitmap) |
| 110 | +/// |
| 111 | +/// You do not generally need to add your own conformances to this protocol. If |
| 112 | +/// you have an image in another format that needs to be attached to a test, |
| 113 | +/// first convert it to an instance of one of the types above. |
| 114 | +@_spi(Experimental) |
| 115 | +public protocol AttachableAsIWICBitmap { |
| 116 | + /// Create a WIC bitmap representing an instance of this type. |
| 117 | + /// |
| 118 | + /// - Parameters: |
| 119 | + /// - factory: A WIC imaging factory that can be used to create additional |
| 120 | + /// WIC objects. |
| 121 | + /// |
| 122 | + /// - Returns: A pointer to a new WIC bitmap representing this image. The |
| 123 | + /// caller is responsible for releasing this image when done with it. |
| 124 | + /// |
| 125 | + /// - Throws: Any error that prevented the creation of the WIC bitmap. |
| 126 | + /// |
| 127 | + /// This function is not part of the public interface of the testing library. |
| 128 | + /// It may be removed in a future update. |
| 129 | + borrowing func _copyAttachableIWICBitmap( |
| 130 | + using factory: UnsafeMutablePointer<IWICImagingFactory> |
| 131 | + ) throws -> UnsafeMutablePointer<IWICBitmap> |
| 132 | + |
| 133 | + /// Make a copy of this instance. |
| 134 | + /// |
| 135 | + /// - Returns: A copy of `self`, or `self` if this type does not support a |
| 136 | + /// copying operation. |
| 137 | + /// |
| 138 | + /// - Throws: Any error that prevented copying this value. |
| 139 | + /// |
| 140 | + /// The testing library uses this function to take ownership of image |
| 141 | + /// resources that test authors pass to it. If possible, make a copy of or add |
| 142 | + /// a reference to `self`. If this type does not support making copies, return |
| 143 | + /// `self` verbatim. |
| 144 | + /// |
| 145 | + /// This function is not part of the public interface of the testing library. |
| 146 | + /// It may be removed in a future update. |
| 147 | + func _copyAttachableValue() throws -> Self |
| 148 | + |
| 149 | + /// Manually deinitialize any resources associated with this image. |
| 150 | + /// |
| 151 | + /// The implementation of this function cleans up any resources (such as |
| 152 | + /// handles or COM objects) associated with this image. The testing library |
| 153 | + /// automatically invokes this function as needed. |
| 154 | + /// |
| 155 | + /// This function is not responsible for releasing the image returned from |
| 156 | + /// `_copyAttachableIWICBitmap(using:)`. |
| 157 | + /// |
| 158 | + /// This function is not part of the public interface of the testing library. |
| 159 | + /// It may be removed in a future update. |
| 160 | + func _deinitializeAttachableValue() |
| 161 | +} |
| 162 | + |
| 163 | +extension AttachableAsIWICBitmap { |
| 164 | + /// Create a WIC bitmap representing an instance of this type and return it as |
| 165 | + /// an instance of `IWICBitmapSource`. |
| 166 | + /// |
| 167 | + /// - Parameters: |
| 168 | + /// - factory: A WIC imaging factory that can be used to create additional |
| 169 | + /// WIC objects. |
| 170 | + /// |
| 171 | + /// - Returns: A pointer to a new WIC bitmap representing this image. The |
| 172 | + /// caller is responsible for releasing this image when done with it. |
| 173 | + /// |
| 174 | + /// - Throws: Any error that prevented the creation of the WIC bitmap. |
| 175 | + /// |
| 176 | + /// This function is a convenience over `_copyAttachableIWICBitmap(using:)` |
| 177 | + /// that casts the result of that function to `IWICBitmapSource` (as needed |
| 178 | + /// by WIC when it encodes the image.) |
| 179 | + borrowing func copyAttachableIWICBitmapSource( |
| 180 | + using factory: UnsafeMutablePointer<IWICImagingFactory> |
| 181 | + ) throws -> UnsafeMutablePointer<IWICBitmapSource> { |
| 182 | + let bitmap = try _copyAttachableIWICBitmap(using: factory) |
| 183 | + defer { |
| 184 | + _ = bitmap.pointee.lpVtbl.pointee.Release(bitmap) |
| 185 | + } |
| 186 | + |
| 187 | + return try withUnsafePointer(to: IID_IWICBitmapSource) { IID_IWICBitmapSource in |
| 188 | + var bitmapSource: UnsafeMutableRawPointer? |
| 189 | + let rQuery = bitmap.pointee.lpVtbl.pointee.QueryInterface(bitmap, IID_IWICBitmapSource, &bitmapSource) |
| 190 | + guard rQuery == S_OK, let bitmapSource else { |
| 191 | + throw ImageAttachmentError.queryInterfaceFailed(IWICBitmapSource.self, rQuery) |
| 192 | + } |
| 193 | + return bitmapSource.assumingMemoryBound(to: IWICBitmapSource.self) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | +#endif |
0 commit comments