-
Notifications
You must be signed in to change notification settings - Fork 120
Allow attaching an IWICBitmapSource
instance directly.
#1266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+194
−110
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bc69f42
Allow attaching an `IWICBitmapSource` instance directly.
grynspan d1d2870
Add comment
grynspan bb2c325
Add remaining subclasses
grynspan 7d5ccd5
Reorder some code and add comments
grynspan 022df4f
IWICBitmapSourceTransform2 is conditionally available on newer SDKs o…
grynspan 2dbbec4
Actually it's not even documented, so let's skip it
grynspan 4a60e74
Add comment about IUnknown casts
grynspan 35795bd
Add specific IWICBitmapSource implementation to avoid QueryInterface …
grynspan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
Sources/Overlays/_Testing_WinSDK/Attachments/IWICBitmap+AttachableAsIWICBitmap.swift
This file was deleted.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
.../Overlays/_Testing_WinSDK/Attachments/IWICBitmapSource+AttachableAsIWICBitmapSource.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
// | ||
|
||
#if os(Windows) | ||
import Testing | ||
|
||
public import WinSDK | ||
|
||
/// A protocol that identifies a type as a COM subclass of `IWICBitmapSource`. | ||
/// | ||
/// Because COM class inheritance is not visible in Swift, we must manually | ||
/// apply conformances to this protocol to each COM type that inherits from | ||
/// `IWICBitmapSource`. | ||
/// | ||
/// Because this protocol is not `public`, we must also explicitly restate | ||
/// conformance to the public protocol `_AttachableByAddressAsIWICBitmapSource` | ||
/// even though this protocol refines that one. This protocol refines | ||
/// `_AttachableByAddressAsIWICBitmapSource` because otherwise the compiler will | ||
/// not allow us to declare `public` members in its extension that provides the | ||
/// implementation of `_AttachableByAddressAsIWICBitmapSource` below. | ||
/// | ||
/// This protocol is not part of the public interface of the testing library. It | ||
/// allows us to reuse code across all subclasses of `IWICBitmapSource`. | ||
protocol IWICBitmapSourceProtocol: _AttachableByAddressAsIWICBitmapSource {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmapSource: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmap: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmapClipper: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmapFlipRotator: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmapFrameDecode: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICBitmapScaler: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICColorTransform: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICFormatConverter: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
@_spi(Experimental) | ||
extension IWICPlanarFormatConverter: _AttachableByAddressAsIWICBitmapSource, IWICBitmapSourceProtocol {} | ||
|
||
// MARK: - Upcasting conveniences | ||
|
||
extension UnsafeMutablePointer where Pointee: IWICBitmapSourceProtocol { | ||
/// Upcast this WIC bitmap to a WIC bitmap source (its parent type). | ||
/// | ||
/// - Returns: `self`, cast to the parent type via `QueryInterface()`. The | ||
/// caller is responsible for releasing the resulting object. | ||
/// | ||
/// - Throws: Any error that occurs while calling `QueryInterface()`. In | ||
/// practice, this function is not expected to throw an error as it should | ||
/// always be possible to cast a valid instance of `IWICBitmap` to | ||
/// `IWICBitmapSource`. | ||
/// | ||
/// - Important: This function consumes a reference to `self` even if the cast | ||
/// fails. | ||
consuming func cast(to _: IWICBitmapSource.Type) throws -> UnsafeMutablePointer<IWICBitmapSource> { | ||
try self.withMemoryRebound(to: IUnknown.self, capacity: 1) { `self` in | ||
defer { | ||
_ = self.pointee.lpVtbl.pointee.Release(self) | ||
} | ||
|
||
return try withUnsafePointer(to: IID_IWICBitmapSource) { IID_IWICBitmapSource in | ||
var bitmapSource: UnsafeMutableRawPointer? | ||
let rQuery = self.pointee.lpVtbl.pointee.QueryInterface(self, IID_IWICBitmapSource, &bitmapSource) | ||
guard rQuery == S_OK, let bitmapSource else { | ||
throw ImageAttachmentError.queryInterfaceFailed(IWICBitmapSource.self, rQuery) | ||
} | ||
return bitmapSource.assumingMemoryBound(to: IWICBitmapSource.self) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - _AttachableByAddressAsIWICBitmapSource implementation | ||
|
||
extension IWICBitmapSourceProtocol { | ||
public static func _copyAttachableIWICBitmapSource( | ||
from imageAddress: UnsafeMutablePointer<Self>, | ||
using factory: UnsafeMutablePointer<IWICImagingFactory> | ||
) throws -> UnsafeMutablePointer<IWICBitmapSource> { | ||
try _copyAttachableValue(at: imageAddress).cast(to: IWICBitmapSource.self) | ||
} | ||
|
||
public static func _copyAttachableValue(at imageAddress: UnsafeMutablePointer<Self>) -> UnsafeMutablePointer<Self> { | ||
imageAddress.withMemoryRebound(to: IUnknown.self, capacity: 1) { imageAddress in | ||
_ = imageAddress.pointee.lpVtbl.pointee.AddRef(imageAddress) | ||
} | ||
return imageAddress | ||
} | ||
|
||
public static func _deinitializeAttachableValue(at imageAddress: UnsafeMutablePointer<Self>) { | ||
imageAddress.withMemoryRebound(to: IUnknown.self, capacity: 1) { imageAddress in | ||
_ = imageAddress.pointee.lpVtbl.pointee.Release(imageAddress) | ||
} | ||
} | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.