UIKit、Core Graphics、Core Animation、OpenGL 的哪些部分允许在非主线程上使用?

发布于 2024-10-09 18:28:00 字数 712 浏览 0 评论 0 原文

在我基于 OpenGL-ES 1.1 的应用程序中,我使用 CALayer 作为 OpenGL 纹理的源。这些 CALayer 由 CGImage 和通过 CoreGraphics 渲染的文本组成。另一个 OpenGL 纹理源是使用 -[CALAyer renderInContext:]UIGraphicsGetImageFromCurrentImageContext 拍摄的 UIView 屏幕截图。目前,我完全在主线程上运行。

后一种情况尤其糟糕,因为它会在创建 UIView 及其屏幕截图的整个过程中停止 OpenGL 渲染。

现在我正在考虑将 OpenGL 代码移动到一个单独的线程中,希望能够绕过这个阻塞。理想情况下,屏幕截图将在与 OpenGL 渲染不同的线程(如果需要的话主线程)上进行。

我无法在文档中找到关于什么需要在主线程上运行以及什么不需要的完整内容。我在 iOS 4 发行说明,以及特定 UIKit 方法中的一些评论,但我缺少完整的图片。

该代码在 iOS 4.x 或更高版本上运行。

In my OpenGL-ES 1.1 based app, I'm using CALayers as a source for OpenGL textures. Those CALayers comprise of CGImages and text rendered through CoreGraphics. Another OpenGL texture source is a screenshot of a UIView taken using -[CALAyer renderInContext:] and UIGraphicsGetImageFromCurrentImageContext. Currently, I'm running completely on the main thread.

The latter case in particular is pretty bad because it halts the OpenGL rendering for the whole time it takes to create the UIView and its screenshot.

Now I'm considering to move the OpenGL code into a separate thread hoping to get around this blocking. Ideally, the screenshot would be taken on a different thread (main thread if needed) than the OpenGL rendering.

I wasn't able to find a complete coverage in the documentation about what requires running on the main thread and what not. I found some comments in the iOS 4 release notes, and some comments in specific UIKit methods but I'm missing the complete picture.

The code runs on iOS 4.x or higher.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2024-10-16 18:28:00

您可以在后台线程上使用 OpenGL ES 进行绘图,只要您不尝试同时从另一个线程访问 OpenGL 上下文即可。请参阅 Apple 的技术问答 QA1612 了解更多信息这。

我在从后台线程更新 CALayer 内容时遇到了许多问题,因此我在主线程上对图层进行任何操作。不管怎样,核心动画在后台线程上触发它的动画。

我从未从后台线程更新过与 UIKit 相关的任何内容,但 UIKit 中绘图的某些方面在 4.0 中已实现线程安全。 David Duncan 在此处评论了该绘图上下文现在是线程安全的。

在你的情况下,我不会看到在后台线程上运行 OpenGL ES 渲染的问题(也许在 GCD 中使用串行调度队列来防止同时从多个线程访问上下文),然后在另一个线程上进行图像抓取。

You can do drawing with OpenGL ES on a background thread, as long as you do don't try to access the OpenGL context from another thread at the same time. See Apple's Technical Q&A QA1612 for a little more on this.

I have run into a number of issues with updating CALayer content from a background thread, so I do any work with layers on the main thread. Core Animation fires off its animations on a background thread, anyway.

I'd never updated anything related to UIKit from a background thread, but some aspects of drawing in UIKit were made threadsafe in 4.0. David Duncan comments here that drawing to a context is now threadsafe.

In your case, I wouldn't see a problem with running the OpenGL ES rendering on a background thread (perhaps using a serial dispatch queue in GCD to prevent access to the context from multiple threads at once) and then doing your image grabs on another.

初雪 2024-10-16 18:28:00

Core Animation 通常是线程安全的,但 UIKit 和 OpenGL ES(至少在 iOS 上)不是线程安全的。 UIKit 必须只能在主线程上使用,而 OpenGL ES 必须一致地在单个线程(通常是主线程)上使用。

Core Animation is generally thread safe, but UIKit and OpenGL ES (on iOS, at least) are not thread-safe. UIKit must only be used on the main thread, and OpenGL ES must be consistently used on a single thread (usually the main thread.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文