How does a computer draw the screen?
How does a computer draw anything to the screen at the lowest level (nothing about external libraries like X11)? Are t开发者_JS百科here supposed to be assembly commands that do this? How exactly does the CPU control what appears on the screen?
Fundamentally the same way it reads from the harddrive, or plays a sound effect.
By writing certain data to specific memory addresses which are mapped by the memory controller to the external hardware in question (harddrive, GPU or sound card in these cases). When the hardware receives those writes, it interprets the data written as instructions about what to do.
The CPU is really kind of isolated from the rest of the system. All it really knows about is the memory bus. It can read and write data on that bus, and that's basically it. Some of these reads/writes go to memory, and others can be mapped to the control registers of various hardware, or to the device's memory if such exists, allowing the CPU to communicate with other devices.
A modern GPU has its own dedicated RAM, which it can load data into. So the CPU sends off instructions to the GPU, specifying where in main memory it can find the data used to generate the screen contents and what to do with it. Then the GPU loads that data from main memory into its own RAM, where it performs the necessary transformations and computations, before writing it into its frame buffer, which the montitor is constantly reading from.
Typically there is an area of memory called frame buffer in your video card. Writing a value there means to establish the color value of a pixel.
You can consider a frame buffer like a 2D array, where each bit represent a pixel on the screen. To represent colors are used different levels of buffer. Today a common frame buffer has 24 level (8 for each RGB color component) and allow the defenition of 2^24 possible colors.
Nowadays generally the access to the frame buffer occurs through the GPU for perforances issues: even if it possible for the CPU to perform this task, it's quite expensive.
There is a memory somewhere that is dedicated to the video screen. Each pixel on the screen has a certain amount of memory. Avoiding historical solutions like palettes and black and white, there is typically a certain number of bits for red, some bits for blue and some for green. When those colors are added you can get everything including white (I know they are not the primary colors used with paint for example, dont worry it works). Typically you will find 24 bit color because it is easy to manage 8 bits, a byte, of red, a byte of green and a byte of blue, and wire up memory, etc.
The video hardware is constantly reading this memory and using whatever technology to send signals to the monitor that essentially tell it the amount of red, green, and blue to light up for a particular pixel, constantly scanning the screen with up to date information.
A couple of common techniques for using this memory are to either use something called dual ported, where two entities are able to read/write the memory. Or to have two memories and swap them. In either case there is another entity, a processor, either the CPU itself, or these days many little graphics processors in the video card/chip. In either case some software runs and these compute the location and color of all the pixels and write them to video memory, depending on the mechanism used the video/monitor side of the memory takes the changes when it makes sense. you dont want to have only some of the pixels of the letters you are typing to show up on one pass across the screen then have the rest show up on the next pass, might get a flicker the user notices.
The video memory is typically mapped like system memory, the processor would likely use the same instructions it uses to access non-video, system, memory. Certainly the gpus might, but a cpu normally does not have peripheral specific instructions, uart, gpio, video, usb, storage, etc. The peripherals are usually mapped as certain addresses in the cpu's memory space and normal memory read/write instructions are used.
Because of the specialized types of algorithms, and more importantly the sheer number of computations needed for video games the video cards use many, many, specialized, simple, graphics processors, these might, yesterday, today, or tomorrow, have video memory specific instructions for performance or size or power or other reasons. What probably will not change is the model of having framebuffer memory, a memory that holds a screens worth of pixels that the video hardware uses to create the signals to send to the monitor so that it can draw the screen.
精彩评论