Title: [284122] trunk/Source
Revision
284122
Author
[email protected]
Date
2021-10-13 13:35:45 -0700 (Wed, 13 Oct 2021)

Log Message

Support both VK and VKC-prefixed SPI when softlinking VisionKitCore classes
https://bugs.webkit.org/show_bug.cgi?id=231683
rdar://83744729

Reviewed by Tim Horton.

Source/WebCore/PAL:

See WebKit/ChangeLog for more details.

* pal/cocoa/VisionKitCoreSoftLink.h:
* pal/cocoa/VisionKitCoreSoftLink.mm:

Source/WebKit:

Several SPI classes in VisionKitCore are going to be renamed to be prefixed with VKC- rather than VK-, with a
`@compatibility_alias` for the existing SPI names. Refactor Live Text support in WebKit to be both binary and
source compatible with this future version of VisionKitCore, by checking for the existence of both class names
when softlinking VKImageAnalyzer and VKImageAnalyzerRequest. See below for more details.

* Platform/cocoa/TextRecognitionUtilities.h:
* Platform/cocoa/TextRecognitionUtilities.mm:
(WebKit::hasVisionKitCorePrefixedClasses):
(WebKit::createImageAnalyzer):
(WebKit::createImageAnalyzerRequest):

Instead of calling into PAL softlinking functions to allocate VKImageAnalyzer, put this logic behind a helper
function in TextRecognitionUtilities, which knows how to check for either class.

* UIProcess/Cocoa/WebViewImpl.mm:
(WebKit::WebViewImpl::ensureImageAnalyzer):
(WebKit::createImageAnalyzerRequest):
(WebKit::WebViewImpl::requestTextRecognition):
(WebKit::WebViewImpl::computeHasImageAnalysisResults):
(WebKit::createImageAnalysisRequest): Deleted.

Use the helper functions declared in TextRecognitionUtilities.h.

* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView imageAnalyzer]):
(-[WKContentView createImageAnalyzerRequest:image:imageURL:]):
(-[WKContentView createImageAnalyzerRequest:image:]):
(-[WKContentView requestTextRecognition:imageData:completionHandler:]):
(-[WKContentView imageAnalysisGestureDidBegin:]):
(-[WKContentView imageAnalysisGestureDidTimeOut:]):
(-[WKContentView createImageAnalysisRequest:image:imageURL:]): Deleted.
(-[WKContentView createImageAnalysisRequest:image:]): Deleted.

Use the helper functions declared in TextRecognitionUtilities.h, instead of using PAL softlinking directly. We
also refactor this code to pass a CGImageRef instead of a UIImage when initializing the image analyzer request,
such that the helper functions can work across macOS and iOS.

Also rename `createImageAnalysisRequest` to `createImageAnalyzerRequest` for consistency with the class name.

Modified Paths

Diff

Modified: trunk/Source/WebCore/PAL/ChangeLog (284121 => 284122)


--- trunk/Source/WebCore/PAL/ChangeLog	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebCore/PAL/ChangeLog	2021-10-13 20:35:45 UTC (rev 284122)
@@ -1,3 +1,16 @@
+2021-10-13  Wenson Hsieh  <[email protected]>
+
+        Support both VK and VKC-prefixed SPI when softlinking VisionKitCore classes
+        https://bugs.webkit.org/show_bug.cgi?id=231683
+        rdar://83744729
+
+        Reviewed by Tim Horton.
+
+        See WebKit/ChangeLog for more details.
+
+        * pal/cocoa/VisionKitCoreSoftLink.h:
+        * pal/cocoa/VisionKitCoreSoftLink.mm:
+
 2021-10-13  Aditya Keerthi  <[email protected]>
 
         [macOS] Add support for accent-color

Modified: trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.h (284121 => 284122)


--- trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.h	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.h	2021-10-13 20:35:45 UTC (rev 284122)
@@ -33,5 +33,7 @@
 SOFT_LINK_FRAMEWORK_FOR_HEADER(PAL, VisionKitCore)
 SOFT_LINK_CLASS_FOR_HEADER(PAL, VKImageAnalyzer)
 SOFT_LINK_CLASS_FOR_HEADER(PAL, VKImageAnalyzerRequest)
+SOFT_LINK_CLASS_FOR_HEADER(PAL, VKCImageAnalyzer)
+SOFT_LINK_CLASS_FOR_HEADER(PAL, VKCImageAnalyzerRequest)
 
 #endif // HAVE(VK_IMAGE_ANALYSIS)

Modified: trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.mm (284121 => 284122)


--- trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.mm	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebCore/PAL/pal/cocoa/VisionKitCoreSoftLink.mm	2021-10-13 20:35:45 UTC (rev 284122)
@@ -31,7 +31,9 @@
 #import <wtf/SoftLinking.h>
 
 SOFT_LINK_PRIVATE_FRAMEWORK_FOR_SOURCE_WITH_EXPORT(PAL, VisionKitCore, PAL_EXPORT)
-SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT(PAL, VisionKitCore, VKImageAnalyzer, PAL_EXPORT)
-SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT(PAL, VisionKitCore, VKImageAnalyzerRequest, PAL_EXPORT)
+SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT_AND_IS_OPTIONAL(PAL, VisionKitCore, VKImageAnalyzer, PAL_EXPORT, true)
+SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT_AND_IS_OPTIONAL(PAL, VisionKitCore, VKImageAnalyzerRequest, PAL_EXPORT, true)
+SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT_AND_IS_OPTIONAL(PAL, VisionKitCore, VKCImageAnalyzer, PAL_EXPORT, true)
+SOFT_LINK_CLASS_FOR_SOURCE_WITH_EXPORT_AND_IS_OPTIONAL(PAL, VisionKitCore, VKCImageAnalyzerRequest, PAL_EXPORT, true)
 
 #endif // HAVE(VK_IMAGE_ANALYSIS)

Modified: trunk/Source/WebKit/ChangeLog (284121 => 284122)


--- trunk/Source/WebKit/ChangeLog	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/ChangeLog	2021-10-13 20:35:45 UTC (rev 284122)
@@ -1,3 +1,51 @@
+2021-10-13  Wenson Hsieh  <[email protected]>
+
+        Support both VK and VKC-prefixed SPI when softlinking VisionKitCore classes
+        https://bugs.webkit.org/show_bug.cgi?id=231683
+        rdar://83744729
+
+        Reviewed by Tim Horton.
+
+        Several SPI classes in VisionKitCore are going to be renamed to be prefixed with VKC- rather than VK-, with a
+        `@compatibility_alias` for the existing SPI names. Refactor Live Text support in WebKit to be both binary and
+        source compatible with this future version of VisionKitCore, by checking for the existence of both class names
+        when softlinking VKImageAnalyzer and VKImageAnalyzerRequest. See below for more details.
+
+        * Platform/cocoa/TextRecognitionUtilities.h:
+        * Platform/cocoa/TextRecognitionUtilities.mm:
+        (WebKit::hasVisionKitCorePrefixedClasses):
+        (WebKit::createImageAnalyzer):
+        (WebKit::createImageAnalyzerRequest):
+
+        Instead of calling into PAL softlinking functions to allocate VKImageAnalyzer, put this logic behind a helper
+        function in TextRecognitionUtilities, which knows how to check for either class.
+
+        * UIProcess/Cocoa/WebViewImpl.mm:
+        (WebKit::WebViewImpl::ensureImageAnalyzer):
+        (WebKit::createImageAnalyzerRequest):
+        (WebKit::WebViewImpl::requestTextRecognition):
+        (WebKit::WebViewImpl::computeHasImageAnalysisResults):
+        (WebKit::createImageAnalysisRequest): Deleted.
+
+        Use the helper functions declared in TextRecognitionUtilities.h.
+
+        * UIProcess/ios/WKContentViewInteraction.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView imageAnalyzer]):
+        (-[WKContentView createImageAnalyzerRequest:image:imageURL:]):
+        (-[WKContentView createImageAnalyzerRequest:image:]):
+        (-[WKContentView requestTextRecognition:imageData:completionHandler:]):
+        (-[WKContentView imageAnalysisGestureDidBegin:]):
+        (-[WKContentView imageAnalysisGestureDidTimeOut:]):
+        (-[WKContentView createImageAnalysisRequest:image:imageURL:]): Deleted.
+        (-[WKContentView createImageAnalysisRequest:image:]): Deleted.
+
+        Use the helper functions declared in TextRecognitionUtilities.h, instead of using PAL softlinking directly. We
+        also refactor this code to pass a CGImageRef instead of a UIImage when initializing the image analyzer request,
+        such that the helper functions can work across macOS and iOS.
+
+        Also rename `createImageAnalysisRequest` to `createImageAnalyzerRequest` for consistency with the class name.
+
 2021-10-13  Per Arne  <[email protected]>
 
         Restrict "darwin-notification-post" to a minimal set in the WP sandbox

Modified: trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.h (284121 => 284122)


--- trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.h	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.h	2021-10-13 20:35:45 UTC (rev 284122)
@@ -27,7 +27,8 @@
 
 #if ENABLE(IMAGE_ANALYSIS)
 
-@class VKImageAnalysis;
+#import <pal/spi/cocoa/VisionKitCoreSPI.h>
+#import <wtf/RetainPtr.h>
 
 namespace WebCore {
 struct TextRecognitionResult;
@@ -38,6 +39,10 @@
 bool isLiveTextAvailableAndEnabled();
 WebCore::TextRecognitionResult makeTextRecognitionResult(VKImageAnalysis *);
 
+// FIXME: Replace the return types of these helper functions with VKCImageAnalyzer and VKCImageAnalyzerRequest, respectively.
+RetainPtr<VKImageAnalyzer> createImageAnalyzer();
+RetainPtr<VKImageAnalyzerRequest> createImageAnalyzerRequest(CGImageRef, VKAnalysisTypes);
+
 }
 
 #endif // ENABLE(IMAGE_ANALYSIS)

Modified: trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm (284121 => 284122)


--- trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/Platform/cocoa/TextRecognitionUtilities.mm	2021-10-13 20:35:45 UTC (rev 284122)
@@ -35,6 +35,32 @@
 namespace WebKit {
 using namespace WebCore;
 
+static bool hasVisionKitCorePrefixedClasses()
+{
+    static bool result = false;
+    static std::once_flag onceFlag;
+    std::call_once(onceFlag, [&] {
+        result = !!PAL::getVKCImageAnalyzerClass();
+    });
+    return result;
+}
+
+RetainPtr<VKImageAnalyzer> createImageAnalyzer()
+{
+    if (hasVisionKitCorePrefixedClasses())
+        return adoptNS([static_cast<VKImageAnalyzer *>(PAL::allocVKCImageAnalyzerInstance()) init]);
+
+    return adoptNS([PAL::allocVKImageAnalyzerInstance() init]);
+}
+
+RetainPtr<VKImageAnalyzerRequest> createImageAnalyzerRequest(CGImageRef image, VKAnalysisTypes types)
+{
+    if (hasVisionKitCorePrefixedClasses())
+        return adoptNS([static_cast<VKImageAnalyzerRequest *>(PAL::allocVKCImageAnalyzerRequestInstance()) initWithCGImage:image orientation:VKImageOrientationUp requestType:types]);
+
+    return adoptNS([PAL::allocVKImageAnalyzerRequestInstance() initWithCGImage:image orientation:VKImageOrientationUp requestType:types]);
+}
+
 static FloatQuad floatQuad(VKQuad *quad)
 {
     return { quad.topLeft, quad.topRight, quad.bottomRight, quad.bottomLeft };

Modified: trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm (284121 => 284122)


--- trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/UIProcess/Cocoa/WebViewImpl.mm	2021-10-13 20:35:45 UTC (rev 284122)
@@ -190,15 +190,15 @@
 {
     if (!m_imageAnalyzer) {
         m_imageAnalyzerQueue = WorkQueue::create("WebKit image analyzer queue");
-        m_imageAnalyzer = adoptNS([PAL::allocVKImageAnalyzerInstance() init]);
+        m_imageAnalyzer = createImageAnalyzer();
         [m_imageAnalyzer setCallbackQueue:m_imageAnalyzerQueue->dispatchQueue()];
     }
     return m_imageAnalyzer.get();
 }
 
-static RetainPtr<VKImageAnalyzerRequest> createImageAnalysisRequest(CGImageRef image, const URL& imageURL, const URL& pageURL, VKAnalysisTypes types)
+static RetainPtr<VKImageAnalyzerRequest> createImageAnalyzerRequest(CGImageRef image, const URL& imageURL, const URL& pageURL, VKAnalysisTypes types)
 {
-    auto request = adoptNS([PAL::allocVKImageAnalyzerRequestInstance() initWithCGImage:image orientation:VKImageOrientationUp requestType:types]);
+    auto request = createImageAnalyzerRequest(image, types);
     [request setImageURL:imageURL];
     [request setPageURL:pageURL];
     return request;
@@ -218,7 +218,7 @@
     }
 
     auto cgImage = imageBitmap->makeCGImage();
-    auto request = createImageAnalysisRequest(cgImage.get(), imageURL, [NSURL _web_URLWithWTFString:m_page->currentURL()], VKAnalysisTypeText);
+    auto request = createImageAnalyzerRequest(cgImage.get(), imageURL, [NSURL _web_URLWithWTFString:m_page->currentURL()], VKAnalysisTypeText);
     auto startTime = MonotonicTime::now();
     [ensureImageAnalyzer() processRequest:request.get() progressHandler:nil completionHandler:makeBlockPtr([completion = WTFMove(completion), startTime] (VKImageAnalysis *analysis, NSError *) mutable {
         callOnMainRunLoop([completion = WTFMove(completion), result = makeTextRecognitionResult(analysis), startTime] () mutable {
@@ -237,7 +237,7 @@
 
     auto cgImage = imageBitmap.makeCGImage();
     auto analysisType = type == ImageAnalysisType::VisualSearch ? VKAnalysisTypeVisualSearch : VKAnalysisTypeText;
-    auto request = createImageAnalysisRequest(cgImage.get(), imageURL, [NSURL _web_URLWithWTFString:m_page->currentURL()], analysisType);
+    auto request = createImageAnalyzerRequest(cgImage.get(), imageURL, [NSURL _web_URLWithWTFString:m_page->currentURL()], analysisType);
     auto startTime = MonotonicTime::now();
     [ensureImageAnalyzer() processRequest:request.get() progressHandler:nil completionHandler:makeBlockPtr([completion = WTFMove(completion), startTime, analysisType] (VKImageAnalysis *analysis, NSError *) mutable {
         BOOL result = [analysis hasResultsForAnalysisTypes:analysisType];

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h (284121 => 284122)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2021-10-13 20:35:45 UTC (rev 284122)
@@ -109,7 +109,6 @@
 }
 
 @class QLPreviewController;
-@class VKImageAnalyzer;
 @class WebEvent;
 @class WebTextIndicatorLayer;
 @class WKActionSheetAssistant;
@@ -504,7 +503,7 @@
     BOOL _contextMenuWasTriggeredByImageAnalysisTimeout;
 #endif // USE(UICONTEXTMENU)
     BOOL _isProceedingWithTextSelectionInImage;
-    RetainPtr<VKImageAnalyzer> _imageAnalyzer;
+    RetainPtr<id> _imageAnalyzer;
 #if USE(QUICK_LOOK)
     RetainPtr<QLPreviewController> _visualSearchPreviewController;
     RetainPtr<UIImage> _visualSearchPreviewImage;

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (284121 => 284122)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-10-13 19:20:58 UTC (rev 284121)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2021-10-13 20:35:45 UTC (rev 284122)
@@ -10020,7 +10020,7 @@
 - (VKImageAnalyzer *)imageAnalyzer
 {
     if (!_imageAnalyzer)
-        _imageAnalyzer = adoptNS([PAL::allocVKImageAnalyzerInstance() init]);
+        _imageAnalyzer = WebKit::createImageAnalyzer();
     return _imageAnalyzer.get();
 }
 
@@ -10087,17 +10087,17 @@
     _elementPendingImageAnalysis = std::nullopt;
 }
 
-- (RetainPtr<VKImageAnalyzerRequest>)createImageAnalysisRequest:(VKAnalysisTypes)analysisTypes image:(UIImage *)image imageURL:(NSURL *)imageURL
+- (RetainPtr<VKImageAnalyzerRequest>)createImageAnalyzerRequest:(VKAnalysisTypes)analysisTypes image:(CGImageRef)image imageURL:(NSURL *)imageURL
 {
-    auto request = adoptNS([PAL::allocVKImageAnalyzerRequestInstance() initWithImage:image orientation:VKImageOrientationUp requestType:analysisTypes]);
+    auto request = WebKit::createImageAnalyzerRequest(image, analysisTypes);
     [request setImageURL:imageURL];
     [request setPageURL:[NSURL _web_URLWithWTFString:_page->currentURL()]];
     return request;
 }
 
-- (RetainPtr<VKImageAnalyzerRequest>)createImageAnalysisRequest:(VKAnalysisTypes)analysisTypes image:(UIImage *)image
+- (RetainPtr<VKImageAnalyzerRequest>)createImageAnalyzerRequest:(VKAnalysisTypes)analysisTypes image:(CGImageRef)image
 {
-    return [self createImageAnalysisRequest:analysisTypes image:image imageURL:_positionInformation.imageURL];
+    return [self createImageAnalyzerRequest:analysisTypes image:image imageURL:_positionInformation.imageURL];
 }
 
 #if USE(UICONTEXTMENU) && ENABLE(IMAGE_ANALYSIS_FOR_MACHINE_READABLE_CODES)
@@ -10139,13 +10139,7 @@
         return;
     }
 
-    auto image = adoptNS([[UIImage alloc] initWithCGImage:cgImage.get()]);
-    if (!image) {
-        completion({ });
-        return;
-    }
-
-    auto request = [self createImageAnalysisRequest:VKAnalysisTypeText image:image.get()];
+    auto request = [self createImageAnalyzerRequest:VKAnalysisTypeText image:cgImage.get()];
     [[self imageAnalyzer] processRequest:request.get() progressHandler:nil completionHandler:makeBlockPtr([completion = WTFMove(completion)] (VKImageAnalysis *result, NSError *) mutable {
         completion(WebKit::makeTextRecognitionResult(result));
     }).get()];
@@ -10210,12 +10204,6 @@
             return;
         }
 
-        auto image = adoptNS([[UIImage alloc] initWithCGImage:cgImage.get()]);
-        if (!image) {
-            [strongSelf _invokeAllActionsToPerformAfterPendingImageAnalysis:WebKit::ProceedWithTextSelectionInImage::No];
-            return;
-        }
-
         RELEASE_LOG(Images, "Image analysis preflight gesture initiated (request %" PRIu64 ").", requestIdentifier.toUInt64());
 
         strongSelf->_elementPendingImageAnalysis = information.imageElementContext;
@@ -10223,8 +10211,8 @@
         auto requestLocation = information.request.point;
         WebCore::ElementContext elementContext = *information.imageElementContext;
 
-        auto requestForTextSelection = [strongSelf createImageAnalysisRequest:VKAnalysisTypeText image:image.get()];
-        auto requestForContextMenu = [strongSelf createImageAnalysisRequest:VKAnalysisTypeVisualSearch | VKAnalysisTypeMachineReadableCode | VKAnalysisTypeAppClip image:image.get()];
+        auto requestForTextSelection = [strongSelf createImageAnalyzerRequest:VKAnalysisTypeText image:cgImage.get()];
+        auto requestForContextMenu = [strongSelf createImageAnalyzerRequest:VKAnalysisTypeVisualSearch | VKAnalysisTypeMachineReadableCode | VKAnalysisTypeAppClip image:cgImage.get()];
 
         auto textAnalysisStartTime = MonotonicTime::now();
         [[strongSelf imageAnalyzer] processRequest:requestForTextSelection.get() progressHandler:nil completionHandler:[requestIdentifier = WTFMove(requestIdentifier), weakSelf, elementContext, requestLocation, requestForContextMenu, gestureDeferralToken, textAnalysisStartTime] (VKImageAnalysis *result, NSError *error) mutable {
@@ -10320,16 +10308,12 @@
         if (!cgImage)
             return;
 
-        auto image = adoptNS([[UIImage alloc] initWithCGImage:cgImage.get()]);
-        if (!image)
-            return;
-
         RELEASE_LOG(Images, "Image analysis timeout gesture initiated.");
         // FIXME: We need to implement some way to cache image analysis results per element, so that we don't end up
         // making redundant image analysis requests for the same image data.
 
         auto visualSearchAnalysisStartTime = MonotonicTime::now();
-        auto requestForContextMenu = [strongSelf createImageAnalysisRequest:VKAnalysisTypeVisualSearch | VKAnalysisTypeMachineReadableCode | VKAnalysisTypeAppClip image:image.get()];
+        auto requestForContextMenu = [strongSelf createImageAnalyzerRequest:VKAnalysisTypeVisualSearch | VKAnalysisTypeMachineReadableCode | VKAnalysisTypeAppClip image:cgImage.get()];
         [[strongSelf imageAnalyzer] processRequest:requestForContextMenu.get() progressHandler:nil completionHandler:[weakSelf, location, visualSearchAnalysisStartTime] (VKImageAnalysis *result, NSError *error) {
             auto strongSelf = weakSelf.get();
             if (!strongSelf)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to