The nature of a good programmer is curiosity, how the software works, how the screen is displayed, and why desktop forms can be so human-dragged by the mouse? If you often have such problems in your mind, congratulations, you are a potential programmer.
I am in the University of automation major, belongs to the electronic class, and the computer is quite interested in the first time to see this thing, I think this thing is too magical (in fact, as long as you see the screen of things, all feel very magical). Hardware + software In-depth let me directly through the understanding of this mysterious machine of the two-pulse. Software I'm not the best, the hardware actually I am very much to take a break. But looking at software through hardware I have a little say in it. The following related content is a small personal perceptual knowledge, hope to have some inspiration for everyone. Since the individual has done about 2 years of Android since graduation, so take Android to see it.
Android belongs to the operating system (hereinafter referred to as OS), running on the hardware, OS can be equipped with App,app based on the OS burst API written. When you write an Android program, you see that the program refers to Android.jar (which is under "Ad directory \sdk\platforms\android-17", about 18.2M), and most of the APIs used in the program are here (e.g. activity), But a strange phenomenon, the compiled apk does not have this thing, a simple demo-compiled apk only a few m. However, if you reference a third-party class library (such as importing a jar package in Libs, referencing an external package), the jar package is included in the compiled apk and the volume increases. This is because the code in Android.jar already exists in the operating system (not necessarily called Android.jar, and scattered in multiple jars), while writing code android.jar the main role is to do compile-time inspection, support code hints, jump to the source, Android.jar is a programming model.
OK, after writing the minimum demo, you can run on the Android phone, a small piece of code can display a very rich interface, buttons, click Processing. But this little piece of code out of the OS is not working, the simple effect behind the pang system is supporting.
For example, the layout file written by XML, the runtime will be translated into Java, class, attribute, structure level one by one corresponding (so the XML write things can generally be completely replaced by Java). The view that is displayed is mostly based on View,view.draw, which is responsible for all the drawing of the view, the default background of the button you see, and the effect of the click is the PNG image that comes with the system. But View.draw in the end is who call it, if interrupt point debugging, will find the level is very, from the outward tune, very easy to see Halo (Breakpoint find mechanism this thing to long-tempered, cold day). Breakpoints can see the final initiator is choreographer (this thing is responsible for "drawing/event" and so on "initiation"), that choreographer is driven by WHO to "start drawing", Activitythread, Activitythread is the source of the APK's operation at each frame. Understanding GUI programming students know that the program is not working, but can respond to click, is because the app has been watching you, this kind of monitoring called "message loop" (plainly, is a dead loop), this cycle has been judged there is no message (you can simply interpret it as a flag bit) arrival (such as Click) If there is a corresponding handler function on the branch callback.
How Ok,choreographer is driven is a clear step, with someone casting a "draw message" to Activitythread's message loop. But where did the news come from? The first time the program runs, it will take the initiative to throw one (you can pretend to throw in the program initialization), run, all kinds of clicks, animations will throw (something like view.invalidate), triggering the redraw.
OK, the source of the draw trigger is probably traced to a certain extent. But how does the view appear on the screen, although View.draw can clearly see what is written in canvas (such as the most commonly used canvas.drawbitmap), but how does the canvas's draw actually go to the screen? This section of the root I have not explored the source, but after all, arm, manually drive the display, here only to make a rough guess, the rationale should not go.
The basic idea is that the app draws →os→ drive → hardware, haha, seemingly all hardware-related final mechanisms are like this. Drivers are directly involved with the hardware, generally compiled or C-written, directly control the hardware interface, generally referred to as HAL (Hardware abstraction layer). Os function Intermediate Bridge. The canvas contains a piece of bitmap (corresponding to a piece of memory), and the various draw of the canvas forms a pixel matrix (a description of the screen pixels) that exists within the bitmap and is passed to the driver through the OS, driving the screen directly. The details of the code, the mechanism is quite cumbersome. But in theory, the image description in memory, basically can show out, that bitmap this piece of memory is how to display it?
There is a piece of memory called "Video Memory" (is not very familiar with, desktop memory can generally be placed in the video card, in fact, which is a block of memory), as the name implies, video memory is used to display the RAM. The rationale is that the driver scans the video memory with two for (rows and columns), reads each pixel, and sets a single pixel signal to a point on the corresponding screen. Two for is a face. But how does this single pixel signal be set to a single point on the screen? Here we can simplify the single point to "light bulb", only two colors (white + black), the bulb is connected to the motherboard of an IO interface, IO port power, light on. The light is black when it is broken. Driven code according to the memory of the single point of data, control the IO interface on the power off.
OK, more complicated, now the screen color is RGB (0xffffff table white, ARGB alpha is only used to give multiple layers of the operation, the final display or RGB). A screen point is actually made up of three dots (red/green/Blue) (the TV closer to your home will look more clearly). The strength of each point from not light to the brightest class, with a byte (0x00~0xff, that is, 0~255, a total of 256 levels), a point of 8 IO interface can be controlled, the driver control 8x3 IO Port can display a point of all colors.
is not a lot clearer. But a screen with so many points (1024x768=69632), the chip is small, but there is no way to have so many interfaces it. Played the embedded students know, generally after a display, should not be more than 50 lines (should be less than this, remember is not very clear). Just said that the drive is usually drawn with two for a point of the sweep of a point, so theoretically leak a point of the interface (8X3) can be (actually slightly different), but how the screen know this point is drawn at which the actual point? The motherboard, the screen through the agreed-upon timing synchronization is OK ( Add a few synchronous signal interfaces to indicate which row to which column.
This time probably roaming here, Chase is not to die brick niu Jiao Jian, but step by step to verify their own ideas, exercise their left brain. Wirelessly structure the best source is Andorid-os, that is the cohesion of how much thinking crystallization.