Gacui and Design pattern (ii) rendering system

Source: Internet
Author: User
Tags range resource

The code for all parts of the rendering can be found under the \libraries\gacui\source\graphicselement directory after the http://gac.codeplex.com is downloaded.

The main idea of the entire rendering system is that the entities (Iguigraphicselement) and the renderer (iguigraphicsrenderer) are separated and the granularity is based on the performance requirements. Why is it so designed? In the preface, it is said that different rendering devices, such as GDI and DirectX, need the same methods of rendering strategies and cache resources. So in order for the renderer of each render device to fully customize the rendering strategy, the design is made.

But how exactly do you do that? In Gacui, you can first use Getguigraphicsresourcemanager to get a global resource Manager (Guigraphicsresourcemanager) object. The main purpose of this object is to register a variety of factory objects that create entities and renderer. To allow the entire rendering system to run, we first need to register all kinds of Iguigraphicselementfactory factory. Each metafile factory has its own global name. This allows you to take a metafile factory object from the resource manager by using the name of the entity after you have taken a diagram of the factory registration Gold Explorer.

Second, because at the time of running, each object in the entity will save a special renderer object for this object, the renderer of the specific rendering device can cache some resources in the object of the renderer, and it can achieve the purpose of special resources for a certain entity cache. So in order to create the appropriate renderer object for the metafile, we also need to associate the name of the metafile factory with a renderer factory (iguigraphicsrendererfactory). Once this step is complete, we can use the following code to associate the correct renderer object with an entity:

iguigraphicselement* element = xxxx;

iguigraphicselementfactory* elementfactory = Element->getfactory ();

iguigraphicsrendererfactory* rendererfactory = Getguigraphicsresourcemanager ()

->getrendererfactory (Elementfactory->getelementtypename ());

iguigraphicsrenderer* renderer = rendererfactory->create ();

Renderer->initialize (Element);

So we construct the corresponding Iguigraphicsrenderer object from a Iguigraphicselement object and associate the renderer object with the entity object. After this step is complete, the renderer object begins to have the resources needed to cache the associated entity objects. Then we only need to tell the object of the renderer objects to the object, then the object can be updated in their own time, by calling Renderer->onelementstatechanged () properly notify the Renderer object, but also can be used renderer- >getminsize () shows the smallest rectangle size required for this entity. Why does the size have to be calculated by the renderer? Mainly because the specific rendering is the renderer to control, so the size of course also need to let the rendering of its calculation, one example is the text rendering.

Then there is the question of how to plan the entities. Currently gacui all the entities shown below:

Gui3dborderelement

Gui3dsplitterelement

Guigradientbackgroundelement

Guiimageframeelement

Guipolygonelement

Guiroundborderelement

Guisolidbackgroundelement

Guisolidborderelement

Guisolidlabelelement

Guicolorizedtextelement

As we can see, most of the entities are simple. Guisolidlabelelement is a little more complicated, with a set of such things as automatic line-wrapping and ellipsis. And the most complicated is guicolorizedtextelement, which, after saving the text in rows, also assigns a buffer of color to each character by line, and then implements the operation of the buffer allocation release update when the string is modified. Why not design a guicharelement, but make these two things? Because in general, the renderer supports a one-time rendering of complex text, if we put every word characters design into an entity, let the typesetting engine to render the string, performance is low, the effect may not be as good as rendering the renderer itself. A typical example of this is the OpenType technology supported by Windows that can be connected to a pen. Another reason is that when developing a shaded text box, if all of the rendering process is not included in an entity, but scattered in the words Fu Tu words, then update the text and color, it is undoubtedly very wasteful memory, and the operation is very troublesome, for flexibility to sacrifice too much performance, outweigh the gains.

Having finished the primitives and renderer, the last one to introduce is the render target object (Iguigraphicsrendertarget). Although the render target can point to many different places, in general, the render target points to the client area of a window. Although it seems only natural to design, it is actually necessary to have such an object because resources such as a brush created by DIRECT2D's render target cannot be used directly on another render target, and when render When Target is dead, those resources are wiped out, recreated render target, and recreating the resource. This step as a bug registered in the gacui inside, not yet implemented, so now direct2d rendering, the window to minimize and then open, sometimes black.

Another function of rendering the target object is to compute the clipping. In rendering, a child entity cannot exceed the rectangular range of the parent typesetting object when the entity that binds the typesetting object that forms the parent-child relationship. And since a large number of objects may be in an invisible position, so the peripheral driver renders the code to stop rendering the invisible subtree and speed up the rendering process when the render object is completely clip (for example, in a container with a scroll bar, a button that is invisible because of the scroll bar's relationship). and the various rendering devices also need to be processed similar to the one in which only the top half of the text can see such a situation. So the typesetting object can render the target object by providing his own rectangular range, so that the render object will compute the visible rectangular range to match the whole rendering process. Since a part of the renderer needs a resource from the render target object, Iguigraphicsrenderer also has a function called SetRenderTarget, which is used when the render object changes For example, because the window minimizes the direct2d of the render target, it needs to be recreated, notifying each entity-bound renderer that the entire render target object has been replaced and some resources may have to be recreated.

Of course, what needs to be proposed here is that, in the Gacui GDI and DIRECT2D renderer implementations, there are some resources that rely on reference to count the global cache. For example, two of the same color rectangles rendered in the same render target object will not be created two times by the specific brushes he uses internally. Although GDI's and DIRECT2D's policies are different, GDI brushes are global, and direct2d brushes are valid for only one render target, GACUI provides a common resource cache algorithm template, making it easier to implement similar functions.

About the content of the rendering system here, the next article will be specific to the content of the typesetting object.

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.