摘要:將操作記錄為一列。在列表上進行操作被稱為光柵化。能夠運行產生位圖的命令加速光柵化注意到此時像素并不在屏幕上。因此,線程將劃分為。根據與的距離確定的優先級。被包裝在一個中,該對象被提交給瀏覽器進程。
This talk is about how Chrome turns web content into pixels. The entire process is called "rendering".
We"ll describe what we mean by content, and what we mean by pixels, and then we"ll explain the magic in between.
這個演講主要介紹chrome是如何把web content轉成像素點的,整個過程被稱為“rendering”。
我們首先描述什么叫做content,然后再介紹pixels,然后解釋其中的魔法。
Chrome"s architecture is constantly evolving. This talk connects high-level concepts (which change slowly) to specific classes (which change frequently).
The details are based primarily on what is currently shipping in the canary channel (M69), but a few of the biggest future refactorings are mentioned in passing.
chrome的架構在不斷發展。這個演講將高級概念(變化緩慢)與特定類(經常變化)聯系起來。細節基于canary(M69),一些比較大的未來的重構也會被提到。
"Content" is the generic term in Chromium for all of the code inside a webpage or the frontend of a web application.
It"s made of text, markup (surrounding the text), styles (defining how markup is rendered), and script (which can modify all of the above dynamically).
There are other kinds of content, which we won"t cover here.
"Content"是最通用的Chromium的術語,指的是網頁內部或者是網絡應用的前端代碼。它由text,markup(環繞于文本),style(決定標記如何渲染),以及script(能夠動態地改變上述提到的內容)。同時也有其他的content,但是我們在這里暫時不做介紹。
A real webpage is just thousands of lines of HTML, CSS, and JavaScript delivered in plain text over the network.
There"s no notion of compilation or packaging as you might find on other kinds of software platforms - the webpage"s source code is the input to the renderer.
一個真實的網頁由成千行的純文本的HTML,CSS以及JavaScript通過網絡提供。你可能在其他軟件平臺找不到compilation或者packaging的概念,網頁的源代碼是renderer的輸入。
Architecturally, the "content" namespace in the Chromium C++ codebase is responsible for everything in the red box.
Contrast with tab strip, address bar, navigation buttons, menus, etc. which live outside of "content".
Key to Chrome"s security model: rendering happens in a sandboxed process.
從架構上說,”content“在Chromium C++ 代碼的命名空間負責紅框中的所有內容。
對比而言,標簽條,地址欄,導航按鈕,菜單,在”content“之外。
Chrome安全模型的關鍵是,在沙盒進程中進行渲染。
At the other end of the pipeline we have to get pixels onto the screen using the graphics libraries provided by the underlying operating system.
On most platforms today, that"s a standardized API called "OpenGL". On Windows there"s an extra translation into DirectX. In the future, we may support newer APIs such as Vulkan.
These libraries provide low-level graphics primitives like "textures" and "shaders", and let you do things like "draw a polygon at these coordinates into a buffer of virtual pixels". But obviously they don"t understand anything about the web or HTML or CSS.
在管道的另一邊,我們要利用操作系統下提供的圖形庫把像素點放置在屏幕上。現今,在大多數的平臺上,有一個標準API叫做”OpenGL”。在Windows上,有額外到DirectX的轉換。在將來,我們會支持新的API(類似Vulkan)。
這些庫提供了低級圖形基元(primitive),類似“紋理(texture)”以及“著色器(shader)”,讓你能夠做類似“在這些坐標處繪制一個多邊形到虛擬像素緩沖區”這樣的操作。但是顯然他們并不理解任何和HTML和CSS相關的事情。
So the goal of rendering can be stated as: turn HTML / CSS / JavaScript into the right OpenGL calls to display the pixels.
But keep in mind a second goal as we describe the pipeline: We also want the right intermediate data structures to update the rendering efficiently after it"s produced, and answer queries about it from script or other parts of the system.
所以渲染的目標可以描述為:將HTML/CSS/JavaScript轉化為正確的OpenGL的調用來展示像素。
但是要記住,我們描述這個管道的第二個目標是:我們同樣想知道正確的中間數據生成后有效地更新,并且能在腳本或者系統的其他部分回答有關它的查詢。
We break the pipeline into multiple "lifecycle stages", generating those intermediate outputs.
We"ll first describe each stage of a working pipeline, then come back to the notion of efficient updating and introduce some concepts for optimization.
我們將管道分成多個“生命周期階段”,產生中間輸出。我們首先描述工作流水線的每個階段,然后回到有效更新的概念,再介紹優化相關的概念。
HTML tags impose a semantically meaningful hierarchical structure on the document. For example, amay contain two paragraphs, each with text. So the first step is to parse those tags to build an object model that reflects this structure.HTML標簽在文檔上強加了語義上有意義的層次結構。 例如,
可能包含兩個段落,每個段落都帶有文本。 因此,第一步是解析這些標簽以構建反映此結構的對象模型。If you"ve taken computer science classes you may recognize this as a "tree".如果你上過計算機科學的課程,那么你就能認出來它是一棵樹。
The DOM serves double duty as both the internal representation of the page, and the API exposed to script for querying or modifying the rendering.
The JavaScript engine (V8) exposes DOM web APIs as thin wrappers around the real DOM tree through a system called "bindings".DOM具有雙重功能,它既能作為頁面的內部展示,也能作為查詢和更改呈現的腳本的公開API。Javascript引擎(V8)通過一個系統叫做“bindings”把真實的DOM tree封裝成一個輕量的wrapper。
Having built the DOM tree, the next step is to process the CSS styles.
A CSS selector selects a subset of DOM elements that its property declarations should apply to.構建了DOM tree以后,下一步是處理CSS styles。一個CSS選擇器選擇它屬性聲明了應當應用的DOM元素的子集。
Style properties are the knobs by which web authors can influence the rendering of DOM elements.
There are hundreds of style properties.作者可以通過Style屬性影響DOM元素的渲染。style屬性有幾百個。
Furthermore, it"s not trivial to determine which elements a style rule selects.
Some elements may be selected by more than one rule, with conflicting declarations for a particular style property.此外,確定樣式規則選擇哪些元素并非易事。
某些元素可能由多個規則選擇,并且具有特定樣式屬性的沖突聲明。The layout stage runs after the style recalc stage.
First, the layout tree is constructed. Then, we walk the layout tree, filling in the geometry data, and processing side effects of the geometry.layout階段在style重計算階段以后進行。首先,layout tree構建,然后我們遍歷這個layout tree,填充幾何數據,然后處理幾何產生的副作用。
Today, layout objects contain both inputs and outputs of the layout stage, without a clean separation between them.
For example, the LayoutObject acquires ownership of its element"s ComputedStyle object.
A new layout system called LayoutNG is expected to simplify the architecture, and make it easier to build new layout algorithms.如今,layout對象包括layout階段的輸入和輸出,它們之間的區分沒有那么清晰。
比如,LayoutObject獲取它元素的 ComputedStyle 對象的所有權。一個新的layout系統叫做LayoutNG,被期望用來簡化架構,構建新的更簡單的layout算法。Now that we understand the geometry of our layout objects, it"s time to paint them.
Paint records paint operations into a list of display items.
A paint operation might be something like "draw a rectangle at these coordinates, in this color".
There may be multiple display items for each layout object, corresponding to different parts of its visual appearance, like the background, foreground, outline, etc.
This is just a recording that can be played back later. We"ll see why that"s useful in a bit.現在我們理解了layout object的幾何形狀,接下來就是繪制它們。
paint將paint操作記錄為一列display item。一個paint操作類似于“在這些坐標用這個顏色繪制一個矩形”。對于每個layout object也許有多個display item,對應視覺外觀的不同部分,比如背景,前景,輪廓等。~~~
It"s important to paint elements in the right order, so that they stack correctly when they overlap.
The order can be controlled by style.
以正確的順序paint element非常重要,只有這樣才能在元素覆蓋的時候正常顯示。這個順序能被style所控制。It"s even possible for an element to be partly in front of and partly behind another element.
That"s because paint runs in multiple phases, and each paint phase does its own traversal of a subtree.一個元素部分在另外一個元素的前面或者后面是可能的。因為paint在不同階段運行,每次的paint階段做了自己子樹的遍歷。
The paint operations in the display item list are executed by a process called rasterization.
Each cell in the resulting bitmap holds values for four color channels.在display item列表上進行paint操作被稱為rasterization(光柵化)。位圖上的每個像素點存著四個顏色通道的值。
The rastered bitmap is stored in memory, typically GPU memory referenced by an OpenGL texture object.
The GPU can also run the commands that produce the bitmap ("accelerated rasterization").
Note that these pixels are not yet on the screen!光柵化的位圖在內存中存儲,通常是被OpenGL紋理對象引用的GPU內存。
GPU能夠運行產生位圖的命令(“加速光柵化”)
注意到此時像素并不在屏幕上。Rasterization issues OpenGL calls through a library called Skia. Skia provides a layer of abstraction around the hardware, and understands more complex things like paths and Bezier curves.
Skia is open-source and maintained by Google. It ships in the Chrome binary but lives in a separate code repository. It"s also used by other products such as the Android OS.
Skia"s GPU-accelerated codepath builds its own buffer of drawing operations, which is flushed at the end of the raster task.Rasterization通過一個Skia的庫產生OpenGL調用,Skia圍繞硬件提供了一層抽象,能理解類似路徑和貝塞爾曲線等更復雜的事情。
Skia開源且由谷歌維護。它附帶在chrome二進制文件中,但是屬于一個多帶帶的代碼庫,同時也被其他產品,比如安卓系統使用。
Skia的GPU加速代碼路徑構建了它獨立的繪制操作緩沖區,它會在光柵化任務結束的時候刷新。Recall that the renderer process is sandboxed, so it can"t make system calls directly.
GL calls issued by Skia are actually proxied into a different process using a "command buffer".
The GPU process receives the command buffer and issues the "real" GL calls through a set of function pointers.
Besides escaping the renderer sandbox, isolating GL in the GPU process protects us from unstable or insecure graphics drivers.回顧之前說的渲染進程在沙盒中,所以并不能直接進行系統調用。
Skia發起的GL調用使用“command buffer”被代理給了另一個不同的進程。
GPU進程接受command buffer然后通過函數指針發起“真正”的GL調用。
逃離沙盒進程,GPU進程中獨立的GL將從不穩定和不安全的圖形驅動中保護我們。Those GL function pointers are initialized by dynamic lookup from the system"s shared OpenGL library - or the ANGLE library on Windows.
ANGLE is another library built by Google; its job is to translate OpenGL to DirectX, which is Microsoft"s API for accelerated graphics on Windows.
There are also OpenGL drivers for Windows, but historically they have not been very high quality.這些GL函數指針通過系統的共享OpenGL庫(或Windows上的ANGLE庫)進行動態查找來初始化。
ANGLE是Google建立的另一個庫; 它的工作是將OpenGL轉換為DirectX,這是微軟在Windows上加速圖形的API。
Windows也有OpenGL驅動程序,但從歷史上看,它們的質量并不高。Moving raster to the GPU process will improve performance.
It"s also needed to support Vulkan.
把光柵化移動到GPU進程能夠改善性能。它同時需要支持VulkanWe have now gone all the way from content to pixels in memory.
But note that the rendering is not static.
Running the full pipeline is expensive
現在我們已經走完在內存中從content到pixels的全程了。但是渲染本身不是靜態的,走完管道全程總是代價昂貴的。Change is modelled as animation frames.
Each frame is a complete rendering of the state of the content at a particular point in time.Certain style properties cause a layer to be created for a layout object.
If a layout object doesn"t have a layer, it paints into the layer of the nearest ancestor that has one.
特定的樣式屬性會為layout對象創建layer。如果一個layout對象本身并沒有layer,那么它將被繪制到最近的祖先節點上。Building the layer tree is a new lifecycle stage on the main thread. Today, this happens before paint, and each layer is painted separately.
構建layer tree是主線程上的新生命周期階段。如今,這發生在paint之前,每層都是多帶帶paint的。
After paint is finished, the commit updates a copy of the layer tree on the compositor thread, to match the state of the tree on the main thread.paint結束后,commit將更新compositor線程上的layer tree的拷貝,以匹配主線程上tree的狀態。
Recall: raster is the step after paint, which turns paint ops into bitmaps.
Layers can be large - rastering the whole layer is expensive, and unnecessary if only part of it is visible.
So the compositor thread divides the layer into tiles.
Tiles are the unit of raster work. Tiles are rastered with a pool of dedicated raster threads. Tiles are prioritized based on their distance from the viewport.
(Not shown: a layer actually has multiple tilings for different resolutions.)回想一下:raster在paint之后,它將paint ops轉換為位圖。
layer可能很大 - 整個layer都光柵化的成本很高,如果只有部分可見則不必要全部光柵化。
因此,compositor線程將layer劃分為tiles。
tiles是raster work的基本單位。 專門的raster線程池對tiles進行光柵化。 根據tiles與viewport的距離確定tiles的優先級。
(未顯示:layer實際上有不同分辨率的多個tilings。)Once all the tiles are rastered, the compositor thread generates "draw quads". A quad is like a command to draw a tile in a particular location on the screen, taking into account all the transformations applied by the layer tree. Each quad references the tile"s rastered output in memory (remember, no pixels are on the screen yet).
The quads are wrapped up in a compositor frame object which gets submitted to the browser process.一旦所有tiles都被光柵化,compositor線程就會生成“draw quads”。 quads類似一個命令,考慮了圖層樹應用的所有變換在屏幕上的特定位置繪制tile。 每個quads在內存中引用了tile的光柵輸出(請記住,屏幕上還沒有像素)。
quads被包裝在一個compositor frame object中,該對象被提交給瀏覽器進程。The compositor thread has two copies of the tree, so that it can raster tiles from a new commit while drawing the previous commit.合成器線程有兩個樹的副本,因此它可以在繪制上一次commit時,對新的commit進行光柵化tiles。
The browser process runs a component called the display compositor, inside a service called "viz" (short for visuals).
The display compositor aggregates compositor frames submitted from all the renderer processes, along with frames from the browser UI outside of the WebContents. Then it issues the GL calls to draw the quad resources, which go to the GPU process just like the GL calls from the raster workers.
On most platforms the display compositor"s output is double-buffered, so the quads draw into a backbuffer, and a "swap" command makes it visible.
(On OS X we do something a little different with CoreAnimation.)
Finally our pixels are on the screen. :)瀏覽器進程在名為“viz”(visual的簡稱)的service中運行一個名為display compositor的組件。
display compositor聚合從所有renderer進程提交的compositor frame,以及來自WebContents外部的browser UI的frame。 然后它發起GL調用來繪制quad資源,這些資源就像來自raster worker的GL調用一樣進入GPU進程。
在大多數平臺上,display compositor的輸出是雙緩沖的,因此quads繪制到backbuffer,“swap”命令使其可見。
(在OS X上,我們使用CoreAnimation做了一些不同的事情。)
最后我們的像素在屏幕上。:)文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/97623.html
相關文章
圖像格式PPM/PGM/PBM剖析及代碼實現 -- 視頻和圖像編程基礎之一
摘要:可移植像素圖格式,灰度圖格式,位圖格式的介紹簡介可移植像素圖格式,可移植灰度圖格式和可移植位圖格式是便于跨平臺的圖像格式。每個文件的開頭兩個字節碼作為文件描述符,指出具體格式和編碼形式。 可移植像素圖格式 PPM,灰度圖格式 PGM,位圖格式 PBM 的介紹 簡介 可移植像素圖格式(PPM),可移植灰度圖格式(PGM)和可移植位圖格式(PBM)是便于跨平臺的圖像格式。有時候也被統稱為 ...
[Leetcode] Game of Life 生命游戲
摘要:思路普通解法,遍歷每一個細胞求值,用一個的矩陣存放結果。求值過程,稍微分析一下可知,其實就是按照以下的矩陣進行結果是可數的。 According to the Wikipedias article: The Game of Life, also knownsimply as Life, is a cellular automaton devised by the Britishmath...
[LeetCode] 289. Game of Life
Problem According to the Wikipedias article: The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. Given a board with m ...
頁面基礎布局相關知識 --- 居中&經典布局
摘要:子元素固定高寬子元素不定高寬缺點需要設置子元素寬度包括百分比等非固定寬度也可以原理是的縮寫,意為彈性布局,用來為盒狀模型提供最大的靈活性。路途未完,行囊已空衣裳破裂污損,人已精疲力竭。拋棄顏色形狀等干擾代碼實際布局 前言 PS: 這些只是個人學習,僅供思路參考,可能會有缺陷,并且都在chrome中測試,不一定適用其他瀏覽器,而且不考慮IE的,切記!! PS: 2018/03/23修改,...
發表評論
0條評論
閱讀 2929·2021-11-24 09:39
閱讀 3612·2021-11-22 13:54
閱讀 3415·2021-11-16 11:45
閱讀 2444·2021-09-09 09:33
閱讀 3202·2019-08-30 15:55
閱讀 1297·2019-08-29 15:40
閱讀 926·2019-08-29 15:19
閱讀 3402·2019-08-29 15:14