Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
It should be noted that, in the embodiments of the present invention, words such as "exemplary" or "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design described as "exemplary" or "e.g.," an embodiment of the present invention is not necessarily to be construed as preferred or advantageous over other embodiments or designs. Rather, use of the word "exemplary" or "such as" is intended to present concepts related in a concrete fashion. "of", "corresponding" and "corresponding" may sometimes be used in combination, it being noted that the intended meaning is consistent when no distinction is made. The 'A and/or B' in the scheme refers to three conditions of a protection scheme 'A' or a protection scheme 'B' or protection schemes 'A and B'.
The invention relates to Web front-end development based on HTML Canvas, and describes a method for optimized image rendering in a UI. According to the scheme, in the intelligent television UI based on the HTML Canvas, a plurality of pictures are changed into a combined image in an image drawing mode by using the hidden Canvas, and then the combined image is drawn into the UI interface, so that the drawing efficiency of the UI on some complex scenes or elements is improved. Taking fig. 1 as an example, the level of interaction required for the user in the prior art is Layer3, and Layer1 and Layer2 are completely static when operating on components in Layer 3. For such a completely static hierarchy in the solution provided by the embodiments of the present application, we can consider whether it can be cached, and how the cached image can be drawn into the UI interface. Compared with a mode of sequentially drawing a plurality of pictures, the scheme described by the invention has the advantages that: complex UI scenes or elements are already inherently more performance consuming, and performing a large number of image rendering computations at the same time increases the CPU/GPU burden. In an embedded system device with relatively limited hardware performance resources, such as a smart television, the phenomena of unsmooth UI, slow response and the like are often caused, and the user experience is influenced. The optimized image drawing mode adopted by the invention can reduce the performance consumed by image rendering aiming at the complex UI scene and ensure the fluency of the UI to the maximum extent.
The protection scheme of the present invention is specifically illustrated with reference to the following examples:
referring to fig. 4, an embodiment of the present invention provides a method for image drawing in a UI, including the steps of:
101. and determining a UI container node to be drawn in the rendering tree.
102. And drawing an object of the hidden graphic container Canvas corresponding to the UI container node to be drawn on the user interface UI.
And the hidden Canvas is pre-drawn with the contents of the UI container nodes to be drawn. Before the method is executed, a hidden graphic container Canvas needs to be created for a UI user interface container node to be drawn in advance. The types of the UI containers comprise UI hierarchies and UI components; specifically, a Canvas tag may be created in the memory by using the document element method, and the size of the Canvas tag is equal to the size of the UI container node object to be drawn. This Canvas tag object is pointed to by this. In the API provided by the Canvas context, the interface for image drawing is context. The img parameter of this method, i.e. the object that needs to be rendered, may be passed in img tags, video tags or canvas tags. That is, the contents of one HTML5Canvas may be rendered in another HTML5 Canvas. With this feature, a hidden Canvas can be created in memory for a static UI container node.
According to the method for drawing the image in the user interface UI, firstly, a UI container node to be drawn is determined in a rendering tree; and then drawing the object of the hidden graphic container Canvas corresponding to the UI container node to be drawn on the user interface UI. Because the content of the UI container node to be rendered is pre-rendered in the hidden Canvas, when the UI performs traversal of the rendering tree and renders to the UI user interface container node to be rendered, the content of the UI user interface container node is already rendered in one hidden Canvas, so the hidden Canvas can be directly rendered on the UI. Therefore, a large amount of drawing processes for the UI container nodes to be drawn are avoided, the performance of image rendering consumption is reduced, and the UI fluency is improved.
A hidden Canvas is created in a memory for a certain static UI container node, a cache () method and an uncache () method are added for container types such as UI hierarchies and components, and an attribute cached is added. After the cache method is called, all elements in the UI container node hierarchy are drawn into a hidden Canvas, and the cached attribute points to the hidden Canvas. And the uncache method will leave the cached attribute empty. Thus, when the UI performs the whole rendering, if the cached attribute of the UI container node to be drawn is not empty, it indicates that the hierarchy or component has been drawn into the hidden Canvas, which is equivalent to performing the pre-rendering. At this time, the UI may draw the hidden Canvas (i.e., the object pointed by the cached attribute) directly without traversing its child nodes. Based on the above principle, before the step 101, the contents of the UI container node to be rendered need to be pre-rendered in a hidden Canvas, which refers to the flow shown in fig. 5, specifically as follows:
201. and judging whether the child node exists in the UI container node to be drawn.
Before step 201, a hidden graphic container Canvas needs to be created in advance for a UI user interface container node to be rendered. Specifically, a Canvas label is created in the memory by using the document. This Canvas tag object is pointed to by this.
202. And when determining that the UI container node to be drawn contains the child node, judging whether the cached attribute of the UI container node to be drawn is empty.
203. And when the cached attribute is determined not to be empty, drawing the hidden Canvas object pointed by the cached attribute in the UI container node onto the hidden Canvas.
If not, indicating that the UI container node is cached, calling context.
204. And when determining that the UI container node to be drawn does not contain the child node, determining whether the UI container node to be drawn contains the picture or the character.
205. And when the UI container node to be drawn is determined to be a picture, drawing the picture into the hidden Canvas.
206. When the predetermined UI container node is determined to be a text, the text is drawn into a hidden Canvas.
In step 204 and 206, if the UI container node has no child node, it indicates that the object in the UI container node is a basic picture element or text, and is not a container type, so it is impossible to have a cached attribute. Therefore, a context.
207. When the cached attribute is determined to be empty, the contents of the child nodes of the predetermined UI container node are drawn in another hidden Canvas.
And the uncache method is realized by setting the cached attribute of the container object to be null. At this point, the pointer to the hidden Canvas is cut off, and the hidden Canvas will be destroyed at a suitable time by the garbage collection mechanism of the browser JavaScript.
The steps 202 and 207 are executed circularly to traverse all child nodes of the UI container node, specifically, for the child nodes, create hidden Canvas corresponding to the object size of each child node, and respectively draw the child nodes into the corresponding hidden Canvas.
208. And traversing nodes in the rendering tree to which the UI container nodes to be drawn belong, and drawing all the UI container nodes in the rendering tree to the corresponding hidden Canvas.
After the steps 201 to 208 are completed, all UI container nodes in the rendering tree can be rendered and drawn into the UI, and when the UI is rendered as a whole, if the cached attribute of the UI container node to be drawn is not empty, it is indicated that the hierarchy or component is already drawn into the hidden Canvas, which is equivalent to pre-rendering. At this time, the UI may draw the hidden Canvas (i.e., the Canvas pointed by the cached attribute) directly without traversing its child nodes.
As per the scenario illustrated in fig. 3, the UI component is composed of multiple elements, and the conventional approach requires that each element inside it be rendered separately at each refresh. The rendering tree is shown in fig. 6, and List items (List items) include: background (Background), Icon (Icon), Title Text (Title Text), detailed Text (Detail Text), and Arrow (Arrow). However, from the perspective of the component, its internal elements are fixed and static, so the cache object of the component can be called and cached. Therefore, when the component is rendered, only one rendering process is needed to complete the drawing of the component. In actual application, a plurality of components are provided, and the hidden Canvas cache mode can greatly reduce the burden of a rendering engine and improve the fluency of a UI. In another example of the scenario illustrated in fig. 2, when the UI needs to render one frame, Layer1 and Layer2 in the UI are in a static state, and the operation of the user only works on Layer 3. In this scenario, we can call cache methods to Layer1 and Layer2, caching both levels to the hidden Canvas. Then the rendering tree that the UI needs to traverse becomes as shown in fig. 7, and the flow of the UI rendering interface is substantially the same as the flow shown in fig. 5, and is not described herein again. When the UI is used for user interaction or animation effect processing, in order to provide smooth experience for the user, the refreshing time of the UI per second is at least 24 frames. If the entire tree structure is rendered in a conventional manner, all nodes need to be rendered for each frame. As is evident from comparing fig. 7 with fig. 2, the number of nodes to be rendered per frame is greatly reduced. At this point, the rendering engine can render more frames per second, making the user experience more fluent. In addition, the HTML5Canvas is not very powerful for processing text, and rendering text is very resource-intensive compared to pictures. The characters are drawn into the hidden Canvas, namely the characters are converted into pictures, and then the hidden Canvas is drawn on the UI interface, so that the weak term of text rendering of the Canvas can be avoided, and the rendering speed is increased.
By the steps, the picture and the character elements are pre-rendered by hiding the Canvas by utilizing the characteristics of the API provided by the Canvas, so that the function similar to image caching is realized. In the process of rendering each frame of the UI, repeated rendering of a large number of static elements is avoided, and the consumption of the UI on performance is reduced. Particularly in a relatively complex scene, the drawing mode can effectively solve the Kanton phenomenon of the UI, and brings good interactive experience to the user.
Referring to fig. 8, an embodiment of the present invention provides an apparatus for image drawing in a UI, including:
a traversal unit 81, configured to determine a UI container node to be drawn in the rendering tree;
and the processing unit 82 is configured to draw an object of a hidden graphic container Canvas corresponding to the UI container node to be drawn, which is determined by the traversal unit, on the user interface UI, where the content of the UI container node to be drawn is pre-drawn in the hidden Canvas.
Optionally, the processing unit 82 is further configured to create the hidden Canvas for the UI container node to be drawn; and drawing the content of the UI container node to be drawn in the hidden Canvas.
Optionally, the processing unit 82 is specifically configured to determine whether a child node exists in the UI container node to be drawn; when the UI container node to be drawn is determined to contain the child node, judging whether the cache cached attribute of the UI container node to be drawn is empty; and when the cached attribute is determined not to be empty, drawing the hidden Canvas object pointed by the cached attribute in the UI container node to be drawn onto the hidden Canvas.
Optionally, the processing unit 82 is specifically configured to, when it is determined that the UI container node to be rendered does not include a child node, determine that a component of the UI container node to be rendered is a picture or a character; and when the UI container node to be drawn is determined to be a picture or a character, drawing the picture or the character into the hidden Canvas.
Optionally, the processing unit 82 is specifically configured to, when it is determined that the cached attribute is empty, draw the content of the child node of the UI container node to be drawn on another hidden Canvas.
According to the device for drawing the image in the user interface UI, firstly, the UI container node to be drawn is determined in the rendering tree; and then drawing the object of the hidden graphic container Canvas corresponding to the UI container node to be drawn on the user interface UI. Because the content of the UI container node to be rendered is pre-rendered in the hidden Canvas, when the UI performs traversal of the rendering tree and renders to the UI user interface container node to be rendered, the content of the UI user interface container node is already rendered in one hidden Canvas, so the hidden Canvas can be directly rendered on the UI. Therefore, a large amount of drawing processes for the UI container nodes to be drawn are avoided, the performance of image rendering consumption is reduced, and the UI fluency is improved.
An embodiment of the present invention further provides a television, including the apparatus for drawing an image in a UI provided in any of the above embodiments.
It should be noted that, in a specific implementation process, each step executed by the apparatus for drawing an image in a UI in the method flows shown in fig. 4 and 5 may be implemented by a processor in a hardware form executing a computer execution instruction in a software form stored in a memory, and is not described herein again to avoid repetition. The program corresponding to the action executed by the image rendering device in the UI may be stored in the memory of the image rendering device in the UI in a software form, so that the processor can call and execute the operations corresponding to the above modules.
The memory above may include volatile memory (volatile memory), such as random-access memory (RAM); a non-volatile memory (non-volatile memory) such as a read-only memory (ROM), a flash memory (flash memory), a hard disk (HDD) or a solid-state drive (SSD); combinations of the above categories of memory may also be included.
The processor in the above-provided apparatus may be a single processor or may be a collective term for a plurality of processing elements. For example, the processor may be a central processing unit (CPU; other general purpose processors, Digital Signal Processor (DSP), Application Specific Integrated Circuit (ASIC), field-programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic, discrete hardware components, etc.
It can be clearly understood by those skilled in the art that, for convenience and brevity of description, the specific working processes of the above-described apparatuses and modules may refer to the corresponding processes in the foregoing method embodiments, and are not described herein again.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. For example, the above-described apparatus embodiments are merely illustrative, and for example, the division of the modules is merely a logical division, and in actual implementation, there may be other divisions, for example, multiple modules or components may be combined or integrated into another system, or some features may be omitted, or not implemented. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
The modules described as separate parts may or may not be physically separate, and parts displayed as modules may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be physically included alone, or two or more units may be integrated into one unit. The integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
The above embodiments are only used for illustrating the technical solutions of the present application, and not for limiting the same; although the present application has been described in detail with reference to the foregoing embodiments, it should be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; and such modifications or substitutions do not depart from the spirit and scope of the corresponding technical solutions in the embodiments of the present application.