UIWebTiledView是UIWebBrowserView->UIWebDocumentView->UIWebTiledView继承链中的老爷子,但这三个类的职能却是MVC的关系,UIWebBrowserView是Controller,UIWebDocumentView是Model,UIWebTiledView是View。(这也是我第一次见到这样的设计,很有意思)
它的声明为: (代码好长,点此跳过)
@interface UIWebTiledView : UIView { WAKWindow *_wakWindow; int _inGestureType; int _tilingArea; BOOL _didFirstTileLayout; BOOL _layoutTilesInMainThread; BOOL _tilingModeIsLocked; BOOL _allowsPaintingAndScriptsWhilePanning; } - (id)wakWindow; - (void)setLayoutTilesInMainThread:(BOOL)arg1; - (BOOL)layoutTilesInMainThread; - (void)dumpTiles; - (void)removeForegroundTiles; - (void)removeAllTiles; - (void)removeAllNonVisibleTiles; - (BOOL)keepsZoomedOutTiles; - (void)setKeepsZoomedOutTiles:(BOOL)arg1; - (BOOL)tilesOpaque; - (void)setTilesOpaque:(BOOL)arg1; - (BOOL)tileDrawingEnabled; - (void)setTileDrawingEnabled:(BOOL)arg1; - (BOOL)logsTilingChanges; - (void)setLogsTilingChanges:(BOOL)arg1; - (BOOL)isTilingEnabled; - (void)setTilingEnabled:(BOOL)arg1; - (unsigned int)adjustedMaxTileCount; - (unsigned int)maxTileCount; - (void)setMaxTileCount:(unsigned int)arg1; - (BOOL)drawsGrid; - (void)setDrawsGrid:(BOOL)arg1; - (struct CGSize)tileSize; - (void)setTileSize:(struct CGSize)arg1; - (void)setTransform:(struct CGAffineTransform)arg1; - (void)setNeedsDisplay; - (void)setNeedsDisplayInRect:(struct CGRect)arg1; - (void)_didScroll; - (void)setNeedsLayout; - (void)setAllowsPaintingAndScriptsWhilePanning:(BOOL)arg1; - (BOOL)allowsPaintingAndScriptsWhilePanning; - (void)unlockTilingMode; - (void)lockTilingMode; - (int)tilingArea; - (void)setTilingArea:(int)arg1; - (void)setInGesture:(int)arg1; - (void)updateTilingMode; - (void)willMoveToWindow:(id)arg1; - (void)_screenChanged:(id)arg1; - (void)layoutSubviews; - (void)layoutTilesNowForRect:(struct CGRect)arg1; - (void)layoutTilesNowOnWebThread; - (void)layoutTilesNow; - (struct CGRect)visibleRect; - (void)setWAKWindow:(id)arg1; - (struct WKWindow *)wkWindow; - (void)dealloc; - (id)initWithFrame:(struct CGRect)arg1; - (void)_updateForScreen:(id)arg1; @end
可以看到好多名字带Tile这个词,也说明了此类专门负责绘制,而tile是分块渲染的意思,这是为了更好地利用GPU和显存。tile rendering大致意思是把屏幕分成几块区域,按照一定顺序来并行绘制这些区域,而不是一整块屏幕一起绘制,详细的信息可参考wiki:http://en.wikipedia.org/wiki/Tiled_rendering。iDevice都是用PowerVR的显卡,它正是分块渲染技术的先锋之一,也可见iOS的设计有针对硬件做优化,非常用心。
从函数和变量名看,UIWebTiledView有这些作用:
- 确定分块的排版是在主线程还是webThread
- 设置需要重排版、重绘,发起分块排版
- 确定分块模式
- 设置绘图区大小和transform等。
WAKWindow是UIWebTiledView的组合对象之一,其声明如下:(代码好长,点此跳过)
@interface WAKWindow : WAKResponder { struct WKWindow *_wkWindow; CALayer *_hostLayer; struct TileCache *_tileCache; struct CGRect _cachedVisibleRect; CALayer *_rootLayer; struct CGSize _screenSize; struct CGSize _availableScreenSize; float _screenScale; struct CGRect _frame; BOOL _useOrientationDependentFontAntialiasing; } + (id)currentEvent; + (BOOL)hasLandscapeOrientation; + (void)setOrientationProvider:(id)arg1; + (id)_wrapperForWindowRef:(struct WKWindow *)arg1; @property(nonatomic) BOOL useOrientationDependentFontAntialiasing; // @synthesize useOrientationDependentFontAntialiasing=_useOrientationDependentFontAntialiasing; - (id).cxx_construct; - (id)recursiveDescription; - (id)description; - (void)dumpTiles; - (void)setAcceleratedDrawingEnabled:(BOOL)arg1; - (void)setTilePaintCountsVisible:(BOOL)arg1; - (void)setTileBordersVisible:(BOOL)arg1; - (id)hostLayer; - (void)didRotate; - (void)willRotate; - (void)displayRect:(struct CGRect)arg1; - (BOOL)hasPendingDraw; - (BOOL)keepsZoomedOutTiles; - (void)setKeepsZoomedOutTiles:(BOOL)arg1; - (float)currentTileScale; - (void)setCurrentTileScale:(float)arg1; - (float)zoomedOutTileScale; - (void)setZoomedOutTileScale:(float)arg1; - (int)tilingDirection; - (void)setTilingDirection:(int)arg1; - (int)tilingMode; - (void)setTilingMode:(int)arg1; - (void)removeForegroundTiles; - (void)removeAllTiles; - (void)removeAllNonVisibleTiles; - (struct CGRect)visibleRect; - (void)setTilesOpaque:(BOOL)arg1; - (BOOL)tilesOpaque; - (void)setNeedsDisplayInRect:(struct CGRect)arg1; - (void)setNeedsDisplay; - (void)layoutTilesNowForRect:(struct CGRect)arg1; - (void)layoutTilesNow; - (void)layoutTiles; - (void)sendMouseMoveEvent:(id)arg1 contentChange:(int *)arg2; - (void)sendEventSynchronously:(id)arg1; - (void)sendEvent:(id)arg1; - (struct WKWindow *)_windowRef; - (id)rootLayer; - (void)setRootLayer:(id)arg1; - (float)screenScale; - (void)setScreenScale:(float)arg1; - (struct CGSize)availableScreenSize; - (void)setAvailableScreenSize:(struct CGSize)arg1; - (struct CGSize)screenSize; - (void)setScreenSize:(struct CGSize)arg1; - (void)setContentRect:(struct CGRect)arg1; - (struct CGRect)frame; - (void)setFrame:(struct CGRect)arg1 display:(BOOL)arg2; - (BOOL)makeFirstResponder:(id)arg1; - (int)keyViewSelectionDirection; - (void)makeKeyWindow; - (BOOL)isKeyWindow; - (struct CGPoint)convertScreenToBase:(struct CGPoint)arg1; - (struct CGPoint)convertBaseToScreen:(struct CGPoint)arg1; - (id)_newFirstResponderAfterResigning; - (BOOL)makeViewFirstResponder:(id)arg1; - (id)firstResponder; - (void)close; - (id)contentView; - (void)setContentView:(id)arg1; - (void)dealloc; - (id)initWithFrame:(struct CGRect)arg1; - (id)initWithLayer:(id)arg1; @end
根据之前《WebCore::Widget浅探》的研究,WAKWindow是模仿NSWindow的,不过也有iOS的特性。它的成员变量TileCache是对分块做缓存,可重复利用。每块Tile都会对应一个CALayer对象,所以也有成员变量叫rootLayer。 hostLayer则是放置用作硬件加速的layer的(这个不深入讲了)。
总结之:UIWebTiledView和WAKWindow共同负责页面的绘制,包括分块、排版、提供CALayer的context(绘图上下文)、缓存Layer、交互(缩放、滚动等)。WAKWindow还兼做分派用户操作信息的工作(sendEvent)。