webkit 图片解码与缓存

  从之前的文章中我们知道,image是一种派生资源,它下载之后会有memory cache机制进行缓存,而image对应的缓存类为CacheImage。CacheImage有一个成员SharedBuffer存储的是图片元数据,也就是没解码前的数据(由loader传递而来)。还有一个Image对象(实际是个BitmapImage),该对象在GraphicsContext绘制时使用。看如下堆栈:

#4  WebCore::BitmapImage::frameDurationAtIndex (this=0x4daf1a78, index=0) at common/external/webkit/Source/WebCore/platform/graphics/BitmapImage.cpp:264
#5  WebCore::BitmapImage::startAnimation 
#6  WebCore::BitmapImage::startAnimation 
#7  WebCore::BitmapImage::draw       ImageAndroid.cpp:180
#8  WebCore::Image::drawTiled 
#9  WebCore::GraphicsContext::drawTiledImage 

  BitmapImage在绘制时会去找,这个图片是不是已在m_frames对象中被缓存,如果被缓存则直接使用,如果没有则创建缓存。那么缓存是怎么创建的呢?

 #1  0x5ae11e2a in WebCore::ImageSource::createFrameAtIndex   ImageSourceAndroid.cpp:389
#2  0x5ae01912 in WebCore::BitmapImage::cacheFrame 

  可见在ImageSource中被创建,注意android的实现在ImageSourceAndroid.cpp中。可见解码后的数据应该存在ImageSource中。实际上ImageSource有一个NativeImageSourcePtr对象,该对象中又有一个PrivateAndroidImageSoureRec对象存储着解码后的SkBitmap。其中解码过程如下:

#0  WebCore::ImageSource::setData   ImageSourceAndroid.cpp:243
#1  WebCore::BitmapImage::dataChanged 
#2  WebCore::Image::setData
#3  WebCore::CachedImage::data 
#4  WebCore::CachedImage::data 
#5  WebCore::CachedResourceRequest::didFinishLoading 

  这个setData的过程就是将前述的SharedBuffer元数据传递的过程,最终在ImageSource::setData(android实现在ImageSourceAndroid.cpp中)中调用Skia库相应的解码类来进行解码。

  这个过程有一点乱,我们以一个类图来梳理一下:

webkit 图片解码与缓存_第1张图片

你可能感兴趣的:(webkit 图片解码与缓存)