UIView drawing rendering mechanism and optimization method

Source: Internet
Author: User


App page optimization for small series has been a difficult problem, has been constantly learning and summary, found that the app page optimization can not be separated from the view of the drawing and rendering mechanism. Online has a lot of wonderful blog, small compilation of the previous N more Daniel research results, at the same time combined with some of their own problems, organized the blog.  try to discuss with you the following questions: What is the relationship between the view drawing rendering mechanism and runloop.  What causes the so-called list to lag behind. We often draw code in the DrawRect method, but the method is called when it is invoked. In the DrawRect method, why the first line of code tends to get the context of the graph. Layer Agent must be a view, can be a VC, why Calayerdelegate can not actively follow. View drawing mechanism and CPU relationship. The relationship between the view rendering mechanism and the GPU.  All fillets are a waste of performance. is the off-screen rendering very NB? Those drawing APIs are the classes that provide how I systematically learn it. How to optimize the CPU/GPU usage rate. view Draw rendering mechanism and runloop what relationship. code example

@implementation Viewcontroller

-(void) viewdidload {
    [super viewdidload];
    Zyyview *view = [[Zyyview alloc] init];
    View.backgroundcolor = [Uicolor whitecolor];
    View.bounds = CGRectMake (0, 0,.);
    View.center = Cgpointmake (+);
    [Self.view Addsubview:view];
}

@end
1 2 3 4 5 6 7 8 9 10 11 12
@implementation Zyyview

-(void) DrawRect: (cgrect) rect {
    Cgcontextref con = uigraphicsgetcurrentcontext ();
    Cgcontextaddellipseinrect (Con, CGRectMake (0,0,100,200));
    Cgcontextsetrgbfillcolor (Con, 0, 0, 1, 1);
    Cgcontextfillpath (con);
}

@end
1 2 3 4 5 6 7 8 9 10 Stack Show

underlying principle

When manipulating the UI, such as changing the Frame, updating the Uiview/calayer hierarchy, or manually invoking the Uiview/calayer Setneedslayout/setneedsdisplay method, this uiview/ The Calayer is marked for processing and is committed to a global container.
Apple has registered a Observer listener beforewaiting (going into hibernation) and exit (about to exit the loop) event, callback to execute a long function:
_ZN2CA11TRANSACTION17OBSERVER_CALLBACKEP19__CFRUNLOOPOBSERVERMPV (). This function iterates through all the pending uiview/calayer to perform the actual drawing and adjustment, and updates the UI interface.
The call stack inside this function is probably the same:

_ZN2CA11TRANSACTION17OBSERVER_CALLBACKEP19__CFRUNLOOPOBSERVERMPV ()  
Quartzcore:ca::transaction::o bserver_callback: 
Ca::transaction::commit ();  
Ca::context::commit_transaction ();  
CA: : layer::layout_and_display_if_needed ();  
ca::layer::layout_if_needed ();  
[Calayer layoutsublayers]; 
[UIView layoutsubviews]; 
Ca::layer::d isplay_if_needed ();  
[Calayer display]; 
[UIView drawrect];

We take a screenshot of the stack information above, and see Balabala a lot of call stack information, which is actually a function of the sins.  How can not understand, then directly look at the following flowchart it. Flowchart Program start Uiapplicationmain () main thread: I am the UI thread can't stop, runloop come with me. Mainrunloop Create and run Mainrunloop: I want to sleep, observer, do you have something? Observer: I'll check _ZN2CA11TRANSACTION17OBSERVER_CALLBACKEP19__CFRUNLOOPOBSERVERMPV () I'll see if there are any objects in the layer tree that are pending. CPU: I'm updating the layer tree and I'll give it to core Animation. Core Animation: Sends the pending Layer object through the IPC to the render service process GPU: The render service process starts rendering the work Gpu:compositing\offscreen Rendering Show to the screen tell Runloop to let it sleep. There's something, I'm calling you observer. Yesno the so-called List of stalling, what is the reason for the cause.

iOS Mainrunloop is a 60fps callback, that is, every 16.7ms will draw a screen, this time period to complete the view of the buffer creation, the view content of the drawing (if rewritten drawrect), the work of these CPUs. This buffer is then given to GPU rendering, which includes multiple view stitching (compositing), texture rendering (Texture), and so on, which is eventually displayed on the screen. The whole process is the flowchart we drew above. Therefore, if you do not complete these operations in 16.7ms, for example, the CPU does too much work, or view hierarchy too much, the picture is too large, causing the GPU pressure is too large, it will lead to "card" phenomenon, that is, drop frames We often draw code in the DrawRect method, But the method is called when the call is invoked. Product Drawing Requirements

First we assume that there is a requirement: to implement the following ellipse effect:
code Example

@implementation Viewcontroller

-(void) viewdidload {
    [super viewdidload];
    Zyyview *view = [[Zyyview alloc] init];
    View.backgroundcolor = [Uicolor whitecolor];
    View.bounds = CGRectMake (0, 0,.);
    View.center = Cgpointmake (+);
    [Self.view Addsubview:view];
}

@end
1 2 3 4 5 6 7 8 9 10 11 12
@implementation Zyyview

-(void) DrawRect: (cgrect) rect {
    Cgcontextref con = uigraphicsgetcurrentcontext ();
    Cgcontextaddellipseinrect (Con, CGRectMake (0,0,100,200));
    Cgcontextsetrgbfillcolor (Con, 0, 0, 1, 1);
    Cgcontextfillpath (con);
}

@end
1 2 3 4 5 6 7 8 9 10 Stack Show

underlying principle

1. Before the [Zyyview DrawRect:] method, the [UIView (Calayerdelegate) Drawlayer:incontext:] and [Calayer Drawincontext:] are called first.
2, if [Self.view Addsubview:view]; DrawRect is not executed if it is written off. Be sure DrawRect
method is triggered by the Addsubview function. Flowchart [Self.view Addsubview:view] [Calayer Drawincontext:] [UIView (Calayerdelegate) Drawlayer:incontext:] [Zyyview DrawRect:] why the first line of code in the DrawRect method always gets the context of the graph

code example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.