北国的春 发表于 2011-12-20 09:48

WebKit、Gecko使用图形库

<font class="Apple-style-span" color="#800000"><div class="multicntwrap"><div class="multicnt"><div><p class="tdep clearfix nbw-act fc06" style="zoom: 1; margin-top: 0px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-align: left; "><div style="text-align: -webkit-auto;"><span class="Apple-style-span" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; line-height: 22px; ">2008年11月30日 星期日 上午 01:20</span></div></p></div></div></div><div class="bct fc05 fc11 nbw-blog ztag js-fs2" style="line-height: 22px; font-size: 14px; text-align: left; word-wrap: break-word; margin-top: 15px; margin-right: 0px; margin-bottom: 15px; margin-left: 0px; padding-top: 5px; padding-right: 0px; padding-bottom: 5px; padding-left: 0px; overflow-x: hidden; overflow-y: hidden; font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><div align="left" style="line-height: 22px; "><div style="line-height: 22px; "><div style="line-height: 22px; "><div style="line-height: 22px; "><div style="line-height: 22px; "><div style="line-height: 22px; ">阅读了之后,觉得作为浏览器内核WebKit、Gecko,为了能高效美观的显示页面的内容,选择适当的图形库非常重要。如果图形库选择不当,往往会导致页面上显示的文字、图片不美观,看起来总让人觉得别扭,更为糟糕的是排列布局出现紊乱,简直无法阅览。<br style="line-height: 22px; "><br style="line-height: 22px; ">从浏览器发展的历史来看,IE系列浏览器的网页布局、文字图片显示的美观程度还是相当高的,也许这与Microsoft图形显示方面的功力相关,到目前为止linux桌面显示还是与传统的windows桌面显示有相当的差距。<br style="line-height: 22px; "><br style="line-height: 22px; ">相 比较Firefox1.5,Firefox3.0图形显示方面也有相当大的进步,这应该归功于完全采取Cario图形库来显示页面,目前应当完全达到了 IE6的显示效果。可见图形显示的好与坏,直接决定了一款浏览器的质量以及用户接受程度。那究竟什么是图形库?其主要功能是什么?目前WebKit、 Gecko可使用哪些图形库?它们在浏览器中是如何发挥其应有的作用呢?<br style="line-height: 22px; "><br style="line-height: 22px; ">一、图形库概述及其主要功能<br style="line-height: 22px; ">A graphics library is a program designed to aid in rendering computer graphics to a monitor. This typically involves providing optimized versions of functions that handle common rendering tasks.<br style="line-height: 22px; "><br style="line-height: 22px; ">This can be done purely in software and running on the CPU, common in embedded systems, or being hardware accelerated by a GPU, more common in PCs. By employing these functions, a program can assemble an image to be output to a monitor. This relieves the programmer of the task of creating and optimizing these functions, and allows them to focus on building the graphics program.<br style="line-height: 22px; "><br style="line-height: 22px; ">目前主要的图形库有:<br style="line-height: 22px; ">windows提供的GDI/GDI+、DirectX、OpenGL;<br style="line-height: 22px; ">支持X的有Cario、GTK、QT、OpenGL;<br style="line-height: 22px; ">其他的还有Skia(google提供)、Quartz 2D(apple提供)、wxWidget等;<br style="line-height: 22px; "><br style="line-height: 22px; ">一 般说来图形库只提供绘画图形,渲染文字、图片等,不管是2D还是3D,其往往不提供消息处理,简单的说来就是如何高效的在一块指定的画布上将线条、文字、 图片显示出来,其中往往涉及字体、颜色等;典型的图形库如GDI/GDI+、Cario、DirectX、Quartz 2D等;<br style="line-height: 22px; "><br style="line-height: 22px; ">而按钮、菜单、窗口等图形组件往往是基于图形库的基础上绘画出来的并有相对固定形状,同时一般具有消息处理功能;相关实现有GTK、QT、wxWidget、windows组件等;<br style="line-height: 22px; "><br style="line-height: 22px; ">其中GTK、QT、wxWidget、Skia等不仅提供图形组件,同时提供图形库的功能;而Cario则是一个纯粹的图形库,类似与Quartz 2D,目前GTK2则完全基于Cario来实现;<br style="line-height: 22px; "><br style="line-height: 22px; ">由 于浏览器页面元素的数量存在不确定性,将页面上的一些元素对应成图形组件可能导致一个页面使用组件过多的问题(早期的IE就曾出现使用GDI对象过多的现 象)。因此尽可能的将一个页面的所有元素显示在一个图形组件上,至于其显示交给图形库来处理,其消息响应交互交给DOM及原生窗口消息循环来完成。<br style="line-height: 22px; "><br style="line-height: 22px; ">从这里我们可以进一步的确认图形库在浏览器中的重要性,以及随着用户需求的增加及硬件的提升,浏览器中使用3D效果应该是一个大的方向。<br style="line-height: 22px; "><br style="line-height: 22px; ">二、Gecko中使用图形库Cario<br style="line-height: 22px; ">1、Cario概述<br style="line-height: 22px; ">Cairo is a 2D graphics library with support for multiple output devices. Currently supported output targets include the X Window System, Quartz, Win32, image buffers, PostScript, PDF, and SVG file output. Experimental backends include OpenGL (through glitz), XCB, BeOS, OS/2, and DirectFB.<br style="line-height: 22px; "><br style="line-height: 22px; ">Cairo is designed to produce consistent output on all output media while taking advantage of display hardware acceleration when available (eg. through the X Render Extension).<br style="line-height: 22px; "><br style="line-height: 22px; ">其主要优点在于其在X、Win32、Quartz的基础上统一了图形库的操作方式,同时支持PS、PDF、SVG、PNG/JPEG等图像格式的输出,极大的方便页面的再次利用,在glitz的支持下支持部分3D效果。<br style="line-height: 22px; "><br style="line-height: 22px; ">2、Cario在Gecko中的使用<br style="line-height: 22px; ">首先提供一个gfxASurface抽象类,代表一块可以作画的画布;提供一个gfxContext类,它用来管理究竟如何作画,如画圆形、旋转,维护画布的状态、当前颜色、路径等,其往往需要一个gfxASurface子类实例来初始化;<br style="line-height: 22px; "><br style="line-height: 22px; ">其次不同的图形输出实现不同的gfxASurface子类如gfxWindowsSurface、gfxXlibSurface、gfxQuartzSurface、gfxGlitzSurface、gfxQuartzPDFSurface、gfxPSSurface等;<br style="line-height: 22px; "><br style="line-height: 22px; ">其次提供一个DeviceContextImpl类实现nsIDeviceContext接口,以描述指定原生Widget相关的字体属性及创建可以在该原生Widget上作画的nsIRenderingContext接口实现;<br style="line-height: 22px; "><br style="line-height: 22px; ">而nsThebesRenderingContext类实现了nsIRenderingContext接口,以供外部(如不同的DOM Node页面元素对应的不同Frame)在其上显示文字、图像、作图形等;<br style="line-height: 22px; "><br style="line-height: 22px; ">然后当解析、布局完DOM页面元素后需要画出不同的页面元素时则由DeviceContextImpl类实例来创建nsThebesRenderingContext类实现,并初始化它,其初始化代码如下:<br style="line-height: 22px; ">nsThebesRenderingContext::Init(nsIDeviceContext* aContext, nsIWidget *aWidget)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">nsThebesDeviceContext *thebesDC = static_castnsIRenderingContext接口究竟有哪些主要方法?<br style="line-height: 22px; ">// RenderingContext interface<br style="line-height: 22px; ">class nsIRenderingContext : public nsISupports<br style="line-height: 22px; ">{<br style="line-height: 22px; ">public:<br style="line-height: 22px; ">.........................................................................<br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Initialize the RenderingContext<br style="line-height: 22px; ">* @param aContext the device context to use.<br style="line-height: 22px; ">* @param aWidget the widget to hook up to<br style="line-height: 22px; ">* @result The result of the initialization, NS_Ok if no errors<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD Init(nsIDeviceContext* aContext, nsIWidget *aWidget) = 0;<br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Get the DeviceContext that this RenderingContext was initialized<br style="line-height: 22px; ">* with. This function addrefs the device context. Though it might<br style="line-height: 22px; ">* be better if it just returned it directly, without addrefing.<br style="line-height: 22px; ">* @result the device context<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD GetDeviceContext(nsIDeviceContext *&amp; aDeviceContext) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Sets the forground color for the RenderingContext<br style="line-height: 22px; ">* @param aColor The color to set the RenderingContext to<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD SetColor(nscolor aColor) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Get the forground color for the RenderingContext<br style="line-height: 22px; ">* @return The current forground color of the RenderingContext<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD GetColor(nscolor &amp;aColor) const = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Sets the font for the RenderingContext<br style="line-height: 22px; ">* @param aFont The font to use in the RenderingContext<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD SetFont(const nsFont&amp; aFont, nsIAtom* aLangGroup) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Sets the font for the RenderingContext<br style="line-height: 22px; ">* @param aFontMetric The font metrics representing the<br style="line-height: 22px; ">* font to use in the RenderingContext<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD SetFont(nsIFontMetrics *aFontMetrics) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Get the current fontmetrics for the RenderingContext<br style="line-height: 22px; ">* @return The current font of the RenderingContext<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD GetFontMetrics(nsIFontMetrics *&amp;aFontMetrics) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Add in a translate to the RenderingContext's transformation matrix<br style="line-height: 22px; ">* @param aX The horizontal translation<br style="line-height: 22px; ">* @param aY The vertical translation<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD Translate(nscoord aX, nscoord aY) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Add in a scale to the RenderingContext's transformation matrix<br style="line-height: 22px; ">* @param aX The horizontal scale<br style="line-height: 22px; ">* @param aY The vertical scale<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD Scale(float aSx, float aSy) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Draw a line<br style="line-height: 22px; ">* @param aXO starting horiztonal coord in twips<br style="line-height: 22px; ">* @param aY0 starting vertical coord in twips<br style="line-height: 22px; ">* @param aX1 end horiztonal coord in twips<br style="line-height: 22px; ">* @param aY1 end vertical coord in twips<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Draw a rectangle<br style="line-height: 22px; ">* @param aRect The rectangle to draw<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD DrawRect(const nsRect&amp; aRect) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/**<br style="line-height: 22px; ">* Draw a string in the RenderingContext<br style="line-height: 22px; ">* @param aString The string to draw<br style="line-height: 22px; ">* @param aLength The length of the aString<br style="line-height: 22px; ">* @param aX Horizontal starting point of baseline<br style="line-height: 22px; ">* @param aY Vertical starting point of baseline.<br style="line-height: 22px; ">* @param aSpacing inter-character spacing to apply<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD DrawString(const char *aString, PRUint32 aLength,<br style="line-height: 22px; ">nscoord aX, nscoord aY,<br style="line-height: 22px; ">const nscoord* aSpacing = nsnull) = 0;<br style="line-height: 22px; "><br style="line-height: 22px; ">/*<br style="line-height: 22px; ">* Tiles an image over an area<br style="line-height: 22px; ">* @param aImage Image to tile<br style="line-height: 22px; ">* @param aXImageStart x location where the origin (0,0) of the image starts<br style="line-height: 22px; ">* @param aYImageStart y location where the origin (0,0) of the image starts<br style="line-height: 22px; ">* @param aTargetRect area to draw to<br style="line-height: 22px; ">* @param aSubimageRect the subimage (in tile space) which we expect to<br style="line-height: 22px; ">* sample from; may be null to indicate that the whole image is<br style="line-height: 22px; ">* OK to sample from<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">NS_IMETHOD DrawTile(imgIContainer *aImage,<br style="line-height: 22px; ">nscoord aXImageStart, nscoord aYImageStart,<br style="line-height: 22px; ">const nsRect * aTargetRect,<br style="line-height: 22px; ">const nsIntRect * aSubimageRect) = 0;<br style="line-height: 22px; ">...............................................................................<br style="line-height: 22px; ">};<br style="line-height: 22px; "><br style="line-height: 22px; ">其中DrawString、DrawTile方法最常用,其分别对应如何显示文字及图像。<br style="line-height: 22px; ">针对图形库显示文字的基本原理可以参考及。<br style="line-height: 22px; "><br style="line-height: 22px; ">至于图形库如何显示不同格式的图像可参考如gif、jpeg、png等。<br style="line-height: 22px; "><br style="line-height: 22px; ">Gecko对Cario的使用还体现在对canvas标签的实现,具体可参考nsCanvasRenderingContext2D.cpp、nsHTMLCanvasElement.cpp等。<br style="line-height: 22px; "><br style="line-height: 22px; ">三、WebKit中使用图形库<br style="line-height: 22px; ">1、WebKit支持的图形库<br style="line-height: 22px; ">目 前WebKit支持的图形库包括Cairo、Gtk、Qt、Wx、Cg、Mac、Skia等,虽然不同的图形库能支持不同的平台,但其在不同平台上的显示 效果也不尽相同。至于在一个指定的平台上究竟使用何种库,则显示出很大的灵活性。就目前来看,在windows平台上可选的图形库有Cairo、Qt、 Wx、Cg、Skia等,其中阐述了Chrome关于图形库的选择。<br style="line-height: 22px; "><br style="line-height: 22px; ">其实从WebKit的角度来看,它通过提供一组与Gecko中nsIRenderingContext类似的公共图形接口,而不同的图形库则根据自身的不同实现了这些公共图形接口,以提供给WebCore元素使用,从而可以让WebKit支持不同的图形库。<br style="line-height: 22px; "><br style="line-height: 22px; ">2、WebKit支持不同图形库的实现<br style="line-height: 22px; ">在WebKit中提供了一个GraphicsContext类,其中包括所有的图形接口,完全类似nsIRenderingContext,针对不同平台的特性,其定义中包含一些不同平台特有的<br style="line-height: 22px; ">宏及元素定义。<br style="line-height: 22px; "><br style="line-height: 22px; ">在 目录webcore\platform\graphics\下的子目录Cairo、Cg、Gtk、Mac、Qt、Win、Wx分别提供了 GraphicsContext类部分方法的实现,而公共的实现则在webcore\platform\graphics \GraphicsContext.cpp中提供。<br style="line-height: 22px; "><br style="line-height: 22px; ">其中我们非常值得关注的方法有drawText与drawImage,其实现如下:<br style="line-height: 22px; ">void GraphicsContext::drawText(const TextRun&amp; run, const IntPoint&amp; point, int from, int to)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">if (paintingDisabled())<br style="line-height: 22px; ">return;<br style="line-height: 22px; "><br style="line-height: 22px; ">font().drawText(this, run, point, from, to);<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">void GraphicsContext::drawImage(Image* image, const FloatRect&amp; dest, const FloatRect&amp; src, CompositeOperator op, bool useLowQualityScale)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">if (paintingDisabled() || !image)<br style="line-height: 22px; ">return;<br style="line-height: 22px; "><br style="line-height: 22px; ">float tsw = src.width();<br style="line-height: 22px; ">float tsh = src.height();<br style="line-height: 22px; ">float tw = dest.width();<br style="line-height: 22px; ">float th = dest.height();<br style="line-height: 22px; "><br style="line-height: 22px; ">if (tsw == -1)<br style="line-height: 22px; ">tsw = image-&gt;width();<br style="line-height: 22px; ">if (tsh == -1)<br style="line-height: 22px; ">tsh = image-&gt;height();<br style="line-height: 22px; "><br style="line-height: 22px; ">if (tw == -1)<br style="line-height: 22px; ">tw = image-&gt;width();<br style="line-height: 22px; ">if (th == -1)<br style="line-height: 22px; ">th = image-&gt;height();<br style="line-height: 22px; "><br style="line-height: 22px; ">if (useLowQualityScale) {<br style="line-height: 22px; ">save();<br style="line-height: 22px; ">setUseLowQualityImageInterpolation(true);<br style="line-height: 22px; ">}<br style="line-height: 22px; ">image-&gt;draw(this, FloatRect(dest.location(), FloatSize(tw, th)), FloatRect(src.location(), FloatSize(tsw, tsh)), op);<br style="line-height: 22px; ">if (useLowQualityScale)<br style="line-height: 22px; ">restore();<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">最 终的实现转交给类Font、Image的方法drawText、draw来实现,而不同实现如Cairo、Cg、Gtk、Mac、Qt、Win、Wx则会 针对类Font、Image分别提供部分对应的实现,而公共的实现则在webcore\platform\graphics\Font.cpp及 Image.cpp中提供。<br style="line-height: 22px; "><br style="line-height: 22px; ">3、不同平台GraphicsContext实例创建及使用<br style="line-height: 22px; ">GraphicsContext 创建的时机往往在对应平台的WebView获得Paint消息事件时,进而将该GraphicsContext类实例传递给FrameView及其不同的 RenderObject实例,由不同的RenderObject实例来决定究竟如何来显示自身的内容,而GraphicsContext类实例提供了各 种的显示文字、图形、图像的方法以供RenderObject实例调用。其调用关系基本上与Gecko中的不同Frame对象使用 nsIRenderingContext接口方法类似。<br style="line-height: 22px; "><br style="line-height: 22px; ">创建GraphicsContext实例的示例如下:<br style="line-height: 22px; ">//Gtk<br style="line-height: 22px; ">static gboolean webkit_web_view_expose_event(GtkWidget* widget, GdkEventExpose* event)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">WebKitWebView* webView = WEBKIT_WEB_VIEW(widget);<br style="line-height: 22px; ">WebKitWebViewPrivate* priv = webView-&gt;priv;<br style="line-height: 22px; "><br style="line-height: 22px; ">Frame* frame = core(webView)-&gt;mainFrame();<br style="line-height: 22px; ">GdkRectangle clip;<br style="line-height: 22px; ">gdk_region_get_clipbox(event-&gt;region, &amp;clip);<br style="line-height: 22px; ">cairo_t* cr = gdk_cairo_create(event-&gt;window);<br style="line-height: 22px; ">GraphicsContext ctx(cr);<br style="line-height: 22px; ">ctx.setGdkExposeEvent(event);<br style="line-height: 22px; ">if (frame-&gt;contentRenderer() &amp;&amp; frame-&gt;view()) {<br style="line-height: 22px; ">frame-&gt;view()-&gt;layoutIfNeededRecursive();<br style="line-height: 22px; "><br style="line-height: 22px; ">if (priv-&gt;transparent) {<br style="line-height: 22px; ">cairo_save(cr);<br style="line-height: 22px; ">cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);<br style="line-height: 22px; ">cairo_paint(cr);<br style="line-height: 22px; ">cairo_restore(cr);<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">frame-&gt;view()-&gt;paint(&amp;ctx, clip);<br style="line-height: 22px; ">}<br style="line-height: 22px; ">cairo_destroy(cr);<br style="line-height: 22px; "><br style="line-height: 22px; ">return FALSE;<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">//win<br style="line-height: 22px; ">void WebView::paintIntoBackingStore(FrameView* frameView, HDC bitmapDC, const IntRect&amp; dirtyRect)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">LOCAL_GDI_COUNTER(0, __FUNCTION__);<br style="line-height: 22px; "><br style="line-height: 22px; ">RECT rect = dirtyRect;<br style="line-height: 22px; "><br style="line-height: 22px; ">#if FLASH_BACKING_STORE_REDRAW<br style="line-height: 22px; ">HDC dc = ::GetDC(m_viewWindow);<br style="line-height: 22px; ">OwnPtr yellowBrush = CreateSolidBrush(RGB(255, 255, 0));<br style="line-height: 22px; ">FillRect(dc, &amp;rect, yellowBrush.get());<br style="line-height: 22px; ">GdiFlush();<br style="line-height: 22px; ">Sleep(50);<br style="line-height: 22px; ">paintIntoWindow(bitmapDC, dc, dirtyRect);<br style="line-height: 22px; ">::ReleaseDC(m_viewWindow, dc);<br style="line-height: 22px; ">#endif<br style="line-height: 22px; "><br style="line-height: 22px; ">FillRect(bitmapDC, &amp;rect, (HBRUSH)GetStockObject(WHITE_BRUSH));<br style="line-height: 22px; ">if (frameView &amp;&amp; frameView-&gt;frame() &amp;&amp; frameView-&gt;frame()-&gt;contentRenderer()) {<br style="line-height: 22px; ">GraphicsContext gc(bitmapDC);<br style="line-height: 22px; ">gc.save();<br style="line-height: 22px; ">gc.clip(dirtyRect);<br style="line-height: 22px; ">frameView-&gt;paint(&amp;gc, dirtyRect);<br style="line-height: 22px; ">gc.restore();<br style="line-height: 22px; ">}<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">//wx<br style="line-height: 22px; ">void wxWebView::OnPaint(wxPaintEvent&amp; event)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">if (m_beingDestroyed || !m_impl-&gt;frame-&gt;view() || !m_impl-&gt;frame)<br style="line-height: 22px; ">return;<br style="line-height: 22px; "><br style="line-height: 22px; ">wxAutoBufferedPaintDC dc(this);<br style="line-height: 22px; "><br style="line-height: 22px; ">if (IsShown() &amp;&amp; m_impl-&gt;frame &amp;&amp; m_impl-&gt;frame-&gt;document()) {<br style="line-height: 22px; ">#if USE(WXGC)<br style="line-height: 22px; ">wxGCDC gcdc(dc);<br style="line-height: 22px; ">#endif<br style="line-height: 22px; "><br style="line-height: 22px; ">if (dc.IsOk()) {<br style="line-height: 22px; ">wxRect paintRect = GetUpdateRegion().GetBox();<br style="line-height: 22px; "><br style="line-height: 22px; ">WebCore::IntSize offset = m_impl-&gt;frame-&gt;view()-&gt;scrollOffset();<br style="line-height: 22px; ">#if USE(WXGC)<br style="line-height: 22px; ">gcdc.SetDeviceOrigin(-offset.width(), -offset.height());<br style="line-height: 22px; ">#endif<br style="line-height: 22px; ">dc.SetDeviceOrigin(-offset.width(), -offset.height());<br style="line-height: 22px; ">paintRect.Offset(offset.width(), offset.height());<br style="line-height: 22px; "><br style="line-height: 22px; ">#if USE(WXGC)<br style="line-height: 22px; ">WebCore::GraphicsContext* gc = new WebCore::GraphicsContext(&amp;gcdc);<br style="line-height: 22px; ">#else<br style="line-height: 22px; ">WebCore::GraphicsContext* gc = new WebCore::GraphicsContext((wxWindowDC*)&amp;dc);<br style="line-height: 22px; ">#endif<br style="line-height: 22px; ">if (gc &amp;&amp; m_impl-&gt;frame-&gt;contentRenderer()) {<br style="line-height: 22px; ">if (m_impl-&gt;frame-&gt;view()-&gt;needsLayout())<br style="line-height: 22px; ">m_impl-&gt;frame-&gt;view()-&gt;layout();<br style="line-height: 22px; "><br style="line-height: 22px; ">m_impl-&gt;frame-&gt;paint(gc, paintRect);<br style="line-height: 22px; ">}<br style="line-height: 22px; ">}<br style="line-height: 22px; ">}<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">//Qt<br style="line-height: 22px; ">void QWebFrame::render(QPainter *painter, const QRegion &amp;clip)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">if (!d-&gt;frame-&gt;view() || !d-&gt;frame-&gt;contentRenderer())<br style="line-height: 22px; ">return;<br style="line-height: 22px; "><br style="line-height: 22px; ">d-&gt;frame-&gt;view()-&gt;layoutIfNeededRecursive();<br style="line-height: 22px; "><br style="line-height: 22px; ">GraphicsContext ctx(painter);<br style="line-height: 22px; ">QVector vector = clip.rects();<br style="line-height: 22px; ">WebCore::FrameView* view = d-&gt;frame-&gt;view();<br style="line-height: 22px; ">for (int i = 0; i &lt;&gt;paint(&amp;ctx, vector.at(i));<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">/*!<br style="line-height: 22px; ">Render the frame into \a painter.<br style="line-height: 22px; ">*/<br style="line-height: 22px; ">void QWebFrame::render(QPainter *painter)<br style="line-height: 22px; ">{<br style="line-height: 22px; ">if (!d-&gt;frame-&gt;view() || !d-&gt;frame-&gt;contentRenderer())<br style="line-height: 22px; ">return;<br style="line-height: 22px; "><br style="line-height: 22px; ">d-&gt;frame-&gt;view()-&gt;layoutIfNeededRecursive();<br style="line-height: 22px; "><br style="line-height: 22px; ">GraphicsContext ctx(painter);<br style="line-height: 22px; ">WebCore::FrameView* view = d-&gt;frame-&gt;view();<br style="line-height: 22px; ">view-&gt;paint(&amp;ctx, view-&gt;frameGeometry());<br style="line-height: 22px; ">}<br style="line-height: 22px; "><br style="line-height: 22px; ">4、WebKit 3D Port实现<br style="line-height: 22px; ">在中提供了WebKit 对3D Port的支持与实现,其实现类似于Gtk+/Cairo图形库的实现,但其3D效果仅实现在Port层,没有对页面上的元素如文字、图像实现3D效果支持。<br style="line-height: 22px; "><br style="line-height: 22px; ">这里只是简单的了解WebKit中如何整合不同的图形库及其与WebCore的交互。要想更加深入的了解页面上的文字、图形、图像究竟是如何显示出来的,则需要更进一步的针对不同平台库进行学习与了解。<br style="line-height: 22px; "><br style="line-height: 22px; ">WebKit 中也支持canvas标签,该标签提供的接口与Gecko能提供的几乎一致,其具体实现可参考webcore\html \CanvasRenderingContext2D.cpp,结合GraphicsContext类的实现,应该能对canvas标签的实现有充分的理 解。<br style="line-height: 22px; "><br style="line-height: 22px; ">四、总结<br style="line-height: 22px; ">其实关于图形库及其使用的内容非常的多,而对浏览器内核来讲能对图形库进行高效使用也是非常重要的一部分,所以在这里所谈到的内容也许只是一些皮毛,但希望能在此开阔一下了解浏览器内核特别是图形库使用方面的思路。<br style="line-height: 22px; "><br style="line-height: 22px; ">五、参考资源<br style="line-height: 22px; "><br style="line-height: 22px; ">Wiki Cairo<br style="line-height: 22px; ">Cairo HomePage<br style="line-height: 22px; ">Wiki Qt<br style="line-height: 22px; ">Wiki GTK+<br style="line-height: 22px; ">Wiki wxWidgets<br style="line-height: 22px; ">Wiki GDI<br style="line-height: 22px; ">Wiki DirectX<br style="line-height: 22px; ">Wiki Quartz 2D<br style="line-height: 22px; ">Wiki OpenGL<br style="line-height: 22px; ">Wiki OpenGL ES<br style="line-height: 22px; "><br style="line-height: 22px; ">Wiki gif<br style="line-height: 22px; ">Wiki jpeg<br style="line-height: 22px; ">Wiki png<br style="line-height: 22px; "><br style="line-height: 22px; ">Clutter Toolkit</div></div></div></div></div></div></div></font>
页: [1]
查看完整版本: WebKit、Gecko使用图形库