Copyright Disclaimer: During reprinting, please use hyperlinks to indicate the original source and author information of the article and this statement
Http://dreamhead.blogbus.com/logs/3233507.html
Gimp is based on the plug-in structure. The plug-in structure features a small amount of content in the core part, and more functions are implemented through the plug-in. Gimp is an image processing program that supports many image processing methods, all of which are in the form of plug-ins. The architecture is shown in:
Gimp provides a plug-in compilation tutorial on its developer website to guide new plug-ins:
How to Write a gimp plug-in, part 1, Part 2, part 3.
Let's take a look at how gimp implements the plug-in structure. The above tutorial provides a brief introduction to the plug-in structure.
First
Let's take a look at the form of gimp plug-in. To help build the gimp plug-in, gimp provides a tool-gimptool, which encapsulates the build plug-in process and saves
The Troubles of finding the corresponding software package. After running gimptool, we will be in the directory of the gimp plug-in (usually ~ /. Gimp-2.0/plug-ins. Real
It is a standard application, not a library. It can even run independently. Of course, if you don't know anything about it, it will tell you that I want to run under gimp. View one
Gimp plug-in, or even the tutorial mentioned above, you will find such a macro call: Main (). Looking at the source code, in $ gimp/libgimp/gimp. H, we found
It simplifies:
# Define main ()/
INT/
Main (INT argc, char * argv [])/
{/
Return gimp_main (& plug_in_info, argc, argv );/
}
As mentioned above, the plug-in is an application, which provides the best evidence. The running logic of the gimp plug-in is hidden in gimp_main ($ gimp/libgimp/gimp. C.
Next, let's take a look at how the main program of gimp deals with plug-ins.
Both
However, if the plug-in exists as an application, the main program calls the plug-in function to call another program to complete the work. To open the channel between the two
Plug_in_open ($ gimp/APP/plug-in/plug-in.c. As you can see, the main task of this Code is to set the relevant parameters for calling the plug-in program.
Parameter, followedG_spawn_asyncOpen the channel to the plug-in.G_spawn_asyncIs a function provided by glib to create a new process.
Connect
The next step is how to communicate with each other. Communication requires communication protocols. For the calling program, the simplest communication protocol is the parameters of the calling program. Common gimp plug-ins
You must specify four functions for initialization, exit, query, and running. The running part is the core part of the entire plug-in. Back to the implementation of gimp_main, we can easily find that the initial
The calling method used for processing and querying is the form of program parameters. This is the easiest way. Of course, it is not enough to solve all the problems, such as how to return the query results.
Actually, this is a process.
How to communicate with each other. In * nix, there are many ways to communicate between processes, and gimp chooses pipelines ). Return to plug_in_open, and open
Two pipelines are used for read and write respectively. Of course, it then uses the io_channel of glib to encapsulate the pipeline. Then, the file descriptor of the two pipelines (actually an integer) is called
Pass parameters to the plug-in program. After the plug-in program obtains the descriptor, it opens the pipeline at its own end, so the two ends establish a connection. Note that a pipe is half-duplex, that is, a pipe can only be read or written.
Generally, two descriptors are obtained when an MPS queue is created, one for reading and the other for writing. To achieve two-way communication between the main program and the plug-in, two pipelines are opened at the same time, that is, four descriptors are obtained.
Parameters are passed to the plug-in program, and the other two are left to the main program itself. The pipeline is the real communication channel between the two processes.
In the same discussion, the channel is opened,
The next step is the communication protocol. The communication protocol between the two is in binary mode, which sets the binary access mode when the pipeline is enabled. Regionalization or binary is a design decision,
The binary method is selected here, and the running timeliness rate may be an important factor. Whether to check the number of intermediate Flows in the same way as network communication is required for inter-process communication.
What should we do if necessary?
The communication protocol between the main program and the plug-in is abstracted as the message model. Therefore, the processing at both ends seems to be a typical message processing process: in a Large Loop
To process the message. For agent-side processing, see gimp_loop ($ gimp/libgimp/gimp. c). For main program-side processing, see
Plug_in_handle_message ($ gimp/APP/plug-in/plug-in-message.c ). In the message retrieval program
(Wire_read_msg, $ gimp/libgimpbase/gimpwire. c), we can see that it is also a process similar to message processing, first retrieve a message
Type. Then, find the corresponding processing function based on the message type and read the other part of the message. Compare the process of writing messages
(Wire_write_msg, $ gimp/libgimpbase/gimpwire. C), the entire message is clearly sent and received. From this, we can see that,
To expand the communication protocol, add a message type and add the corresponding processing function.
Message-level communication is still relatively low-level.
Gimp also provides function-level communication. We know that gimp provides many APIs for writing plug-ins, and some APIs need to interact with the main program to reflect their functions. For example, if you need a plug-in
For a pair of images, the plug-in itself does not have an image. From where it comes, it needs to call a function, which is actually obtained through interaction with the main program. With the help of existing message-level communication
For plug-in writers, everything is transparent. Like the layer-7 model of network protocols, a complete system and a clear design are built through layers. Communication with function call Methods
The scalability is greatly enhanced.
For the request of the corresponding plug-in, the main program should also be expressed. The main program has built a library for this purpose, which is the PDB on the architecture diagram (process data
Library). Every time a request is sent for the running process, the main program will find the corresponding function in PDB for calling (plug_in_handle_proc_run, $ gimp/APP
/Plug-in/plug-in-message.c ). The well-known PDB is actually a hash table ($ gimp/APP/PDB
/Procedural_db ).
The above briefly introduces the implementation of the gimp plug-in structure, which is some of the experiences of recently studying gimp, not necessarily completely accurate, the source code is the most
Good answer. There are many things worth learning in programs that span time, And gimp in their teenage years are also among them. By the way, the implementation of gimp is clear. Although it is implemented in C, the modules are clearly divided.
Chu. The language only limits expression ability, not thinking.