のねのBlog

パソコンの問題や、ソフトウェアの開発で起きた問題など書いていきます。よろしくお願いします^^。

JB43R11 saveViewState その3

その3

7:frameworks/base/core/java/android/webkit/WebViewCore.java

   2255     private void saveViewState(OutputStream stream,
   2256             ValueCallback<Boolean> callback) {
   2257         // TODO: Create a native method to do this better without overloading
   2258         // the draw path (and fix saving <canvas>)
   2259         DrawData draw = new DrawData(); <=8
   2260         if (DebugFlags.WEB_VIEW_CORE) Log.v(LOGTAG, "saveViewState start");
   2261         draw.mBaseLayer = nativeRecordContent(mNativeClass, draw.mContentSize);<==Native
   2262         boolean result = false;
   2263         try {
   2264             result = ViewStateSerializer.serializeViewState(stream, draw); <==あ
   2265         } catch (Throwable t) {
   2266             Log.w(LOGTAG, "Failed to save view state", t);
   2267         }
   2268         callback.onReceiveValue(result);
   2269         if (draw.mBaseLayer != 0) {
   2270             if (mDrawIsScheduled) {
   2271                 mDrawIsScheduled = false;
   2272                 mEventHub.removeMessages(EventHub.WEBKIT_DRAW);
   2273             }
   2274             mLastDrawData = draw;
   2275             webkitDraw(draw);
   2276         }
   2277     }

あ:frameworks/base/core/java/android/webkit/ViewStateSerializer.java

     30 class ViewStateSerializer {
     31 
     32     private static final int WORKING_STREAM_STORAGE = 16 * 1024;
     33 
     34     // VERSION = 1 was for pictures encoded using a previous copy of libskia
     35     static final int VERSION = 2;
     36 
     37     static boolean serializeViewState(OutputStream stream, DrawData draw) <==
     38             throws IOException {
     39         int baseLayer = draw.mBaseLayer;
     40         if (baseLayer == 0) {
     41             return false;
     42         }
     43         DataOutputStream dos = new DataOutputStream(stream);
     44         dos.writeInt(VERSION);
     45         dos.writeInt(draw.mContentSize.x);
     46         dos.writeInt(draw.mContentSize.y);
     47         return nativeSerializeViewState(baseLayer, dos, <==いへ
     48                 new byte[WORKING_STREAM_STORAGE]);
     49     }
     50 
     51     static DrawData deserializeViewState(InputStream stream)
     52             throws IOException {
     53         DataInputStream dis = new DataInputStream(stream);
     54         int version = dis.readInt();
     55         if (version > VERSION) {
     56             throw new IOException("Unexpected version: " + version);
     57         }
     58         int contentWidth = dis.readInt();
     59         int contentHeight = dis.readInt();
     60         int baseLayer = nativeDeserializeViewState(version, dis,
     61                 new byte[WORKING_STREAM_STORAGE]);
     62 
     63         final WebViewCore.DrawData draw = new WebViewCore.DrawData();
     64         draw.mViewState = new WebViewCore.ViewState();
     65         draw.mContentSize = new Point(contentWidth, contentHeight);
     66         draw.mBaseLayer = baseLayer;
     67         stream.close();
     68         return draw;
     69     }
     70 
     71     public static void dumpLayerHierarchy(int baseLayer, OutputStream out, int level) {
     72         nativeDumpLayerHierarchy(baseLayer, level, out,
     73                 new byte[WORKING_STREAM_STORAGE]);
     74     }
     75 
     76 
     77     private static native void nativeDumpLayerHierarchy(int baseLayer, int level,
     78             OutputStream out, byte[] storage);
     79 
     80     private static native boolean nativeSerializeViewState(int baseLayer,
     81             OutputStream stream, byte[] storage);
     82 
     83     // Returns a pointer to the BaseLayer
     84     private static native int nativeDeserializeViewState(int version,
     85             InputStream stream, byte[] storage);
     86 
     87     private ViewStateSerializer() {}
     88 }
     89 

う:external/webkit/Source/WebKit/android/jni/ViewStateSerializer.cpp

    124 static bool nativeSerializeViewState(JNIEnv* env, jobject, jint jbaseLayer,
    125                                      jobject jstream, jbyteArray jstorage)
    126 {
    127     BaseLayerAndroid* baseLayer = (BaseLayerAndroid*) jbaseLayer;
    128     if (!baseLayer)
    129         return false;
    130 
    131     SkWStream *stream = CreateJavaOutputStreamAdaptor(env, jstream, jstorage);
    132 #if USE(ACCELERATED_COMPOSITING)
    133     stream->write32(baseLayer->getBackgroundColor().rgb());
    134 #else
    135     stream->write32(0);
    136 #endif
    137     if (!stream)
    138         return false;
    139     if (baseLayer->content())
    140         baseLayer->content()->serialize(stream);
    141     else
    142         return false;
    143     int childCount = baseLayer->countChildren();
    144     ALOGV("BaseLayer has %d child(ren)", childCount);
    145     stream->write32(childCount);
    146     for (int i = 0; i < childCount; i++) {
    147         LayerAndroid* layer = static_cast<LayerAndroid*>(baseLayer->getChild(i));
    148         serializeLayer(layer, stream); <==うへ
    149     }
    150     delete stream;
    151     return true;
    152 }
    154 static BaseLayerAndroid* nativeDeserializeViewState(JNIEnv* env, jobject, jint version,
    155                                                     jobject jstream, jbyteArray jstorage)
    156 {
    157     SkStream* javaStream = CreateJavaInputStreamAdaptor(env, jstream, jstorage);
    158     if (!javaStream)
    159         return 0;
    160 
    161     // read everything into memory so that we can get the offset into the stream
    162     // when necessary. This is needed for the LegacyPictureLayerContent.
    163     SkDynamicMemoryWStream tempStream;
    164     const int bufferSize = 256*1024; // 256KB
    165     uint8_t buffer[bufferSize];
    166     int bytesRead = 0;
    167 
    168     do {
    169       bytesRead = javaStream->read(buffer, bufferSize);
    170       tempStream.write(buffer, bytesRead);
    171     } while (bytesRead != 0);
    172 
    173     SkMemoryStream stream;
    174     stream.setData(tempStream.copyToData())->unref();
    175 
    176     // clean up the javaStream now that we have everything in memory
    177     delete javaStream;
    178 
    179     Color color = stream.readU32();
    180 
    181 
    182 
    183     LayerContent* content;
    184     if (version == 1) {
    185         content = new LegacyPictureLayerContent(&stream);
    186     } else {
    187         SkPicture* picture = new SkPicture(&stream);
    188         content = new PictureLayerContent(picture);
    189         SkSafeUnref(picture);
    190     }
    191 
    192     BaseLayerAndroid* layer = new BaseLayerAndroid(content);
    193     layer->setBackgroundColor(color);
    194 
    195     SkRegion dirtyRegion;
    196     dirtyRegion.setRect(0, 0, content->width(), content->height());
    197     layer->markAsDirty(dirtyRegion);
    198 
    199     SkSafeUnref(content);
    200     int childCount = stream.readS32();
    201     for (int i = 0; i < childCount; i++) {
    202         LayerAndroid* childLayer = deserializeLayer(version, &stream);
    203         if (childLayer)
    204             layer->addChild(childLayer);
    205     }
    206     return layer;
    207 }

う:external/webkit/Source/WebKit/android/jni/ViewStateSerializer.cpp

    333 void serializeLayer(LayerAndroid* layer, SkWStream* stream)
    334 {
    335     if (!layer) {
    336         ALOGV("NULL layer!");
    337         stream->write8(LTNone);
    338         return;
    339     }
    340     if (layer->isMedia() || layer->isVideo()) {
    341         ALOGV("Layer isn't supported for serialization: isMedia: %s, isVideo: %s",
    342              layer->isMedia() ? "true" : "false",
    343              layer->isVideo() ? "true" : "false");
    344         stream->write8(LTNone);
    345         return;
    346     }
    347     LayerTypes type = LTLayerAndroid;
    348     if (layer->contentIsScrollable())
    349         type = LTScrollableLayerAndroid;
    350     stream->write8(type);
    351 
    352     // Start with Layer fields
    353     stream->writeBool(layer->shouldInheritFromRootTransform());
    354     stream->writeScalar(layer->getOpacity());
    355     stream->writeScalar(layer->getSize().width());
    356     stream->writeScalar(layer->getSize().height());
    357     stream->writeScalar(layer->getPosition().x());
    358     stream->writeScalar(layer->getPosition().y());
    359     stream->writeScalar(layer->getAnchorPoint().x());
    360     stream->writeScalar(layer->getAnchorPoint().y());
    361     writeMatrix(stream, layer->getMatrix());
    362     writeMatrix(stream, layer->getChildrenMatrix());
    363 
    364     // Next up, LayerAndroid fields
    365     stream->writeBool(layer->m_haveClip);
    366     stream->writeBool(layer->isPositionFixed());
    367     stream->writeBool(layer->m_backgroundColorSet);
    368     stream->writeBool(layer->isIFrame());
    369 
    370     // With the current LayerAndroid hierarchy, LayerAndroid doesn't have
    371     // those fields anymore. Let's keep the current serialization format for
    372     // now and output blank fields... not great, but probably better than
    373     // dealing with multiple versions.
    374     if (layer->fixedPosition()) {
    375         FixedPositioning* fixedPosition = layer->fixedPosition();
    376         writeSkLength(stream, fixedPosition->m_fixedLeft);
    377         writeSkLength(stream, fixedPosition->m_fixedTop);
    378         writeSkLength(stream, fixedPosition->m_fixedRight);
    379         writeSkLength(stream, fixedPosition->m_fixedBottom);
    380         writeSkLength(stream, fixedPosition->m_fixedMarginLeft);
    381         writeSkLength(stream, fixedPosition->m_fixedMarginTop);
    382         writeSkLength(stream, fixedPosition->m_fixedMarginRight);
    383         writeSkLength(stream, fixedPosition->m_fixedMarginBottom);
    384         writeSkRect(stream, fixedPosition->m_fixedRect);
    385         stream->write32(fixedPosition->m_renderLayerPos.x());
    386         stream->write32(fixedPosition->m_renderLayerPos.y());
    387     } else {
    388         SkLength length;
    389         SkRect rect;
    390         writeSkLength(stream, length); // fixedLeft
    391         writeSkLength(stream, length); // fixedTop
    392         writeSkLength(stream, length); // fixedRight
    393         writeSkLength(stream, length); // fixedBottom
    394         writeSkLength(stream, length); // fixedMarginLeft
    395         writeSkLength(stream, length); // fixedMarginTop
    396         writeSkLength(stream, length); // fixedMarginRight
    397         writeSkLength(stream, length); // fixedMarginBottom
    398         writeSkRect(stream, rect);     // fixedRect
    399         stream->write32(0);            // renderLayerPos.x()
    400         stream->write32(0);            // renderLayerPos.y()
    401     }
    402 
    403     stream->writeBool(layer->m_backfaceVisibility);
    404     stream->writeBool(layer->m_visible);
    405     stream->write32(layer->m_backgroundColor);
    406     stream->writeBool(layer->m_preserves3D);
    407     stream->writeScalar(layer->m_anchorPointZ);
    408     stream->writeScalar(layer->m_drawOpacity);
    409     bool hasContentsImage = layer->m_imageCRC != 0;
    410     stream->writeBool(hasContentsImage);
    411     if (hasContentsImage) {
    412         SkOrderedWriteBuffer buffer(1024);
    413         buffer.setFlags(SkFlattenableWriteBuffer::kCrossProcess_Flag);
    414         ImageTexture* imagetexture =
    415                 ImagesManager::instance()->retainImage(layer->m_imageCRC);
    416         if (imagetexture && imagetexture->bitmap())
    417             imagetexture->bitmap()->flatten(buffer);
    418         ImagesManager::instance()->releaseImage(layer->m_imageCRC);
    419         stream->write32(buffer.size());
    420         buffer.writeToStream(stream);
    421     }
    422     bool hasRecordingPicture = layer->m_content != 0 && !layer->m_content->isEmpty();
    423     stream->writeBool(hasRecordingPicture);
    424     if (hasRecordingPicture)
    425         layer->m_content->serialize(stream);
    426     // TODO: support m_animations (maybe?)
    427     stream->write32(0); // placeholder for m_animations.size();
    428     writeTransformationMatrix(stream, layer->m_transform);
    429     writeTransformationMatrix(stream, layer->m_childrenTransform);
    430     if (type == LTScrollableLayerAndroid) {
    431         ScrollableLayerAndroid* scrollableLayer =
    432                 static_cast<ScrollableLayerAndroid*>(layer);
    433         stream->writeScalar(scrollableLayer->m_scrollLimits.fLeft);
    434         stream->writeScalar(scrollableLayer->m_scrollLimits.fTop);
    435         stream->writeScalar(scrollableLayer->m_scrollLimits.width());
    436         stream->writeScalar(scrollableLayer->m_scrollLimits.height());
    437     }
    438     int childCount = layer->countChildren();
    439     stream->write32(childCount);
    440     for (int i = 0; i < childCount; i++)
    441         serializeLayer(layer->getChild(i), stream);
    442 }
    443 

============================================================

    444 LayerAndroid* deserializeLayer(int version, SkMemoryStream* stream)
    445 {
    446     int type = stream->readU8();
    447     if (type == LTNone)
    448         return 0;
    449     // Cast is to disambiguate between ctors.
    450     LayerAndroid *layer;
    451     if (type == LTLayerAndroid)
    452         layer = new LayerAndroid((RenderLayer*) 0);
    453     else if (type == LTScrollableLayerAndroid)
    454         layer = new ScrollableLayerAndroid((RenderLayer*) 0);
    455     else {
    456         ALOGV("Unexpected layer type: %d, aborting!", type);
    457         return 0;
    458     }
    459 
    460     // Layer fields
    461     layer->setShouldInheritFromRootTransform(stream->readBool());
    462     layer->setOpacity(stream->readScalar());
    463     layer->setSize(stream->readScalar(), stream->readScalar());
    464     layer->setPosition(stream->readScalar(), stream->readScalar());
    465     layer->setAnchorPoint(stream->readScalar(), stream->readScalar());
    466     layer->setMatrix(readMatrix(stream));
    467     layer->setChildrenMatrix(readMatrix(stream));
    468 
    469     // LayerAndroid fields
    470     layer->m_haveClip = stream->readBool();
    471 
    472     // Keep the legacy serialization/deserialization format...
    473     bool isFixed = stream->readBool();
    474 
    475     layer->m_backgroundColorSet = stream->readBool();
    476 
    477     bool isIframe = stream->readBool();
    478     // If we are a scrollable layer android, we are an iframe content
    479     if (isIframe && type == LTScrollableLayerAndroid) {
    480          IFrameContentLayerAndroid* iframeContent = new IFrameContentLayerAndroid(*layer);
    481          layer->unref();
    482          layer = iframeContent;
    483     } else if (isIframe) { // otherwise we are just the iframe (we use it to compute offset)
    484          IFrameLayerAndroid* iframe = new IFrameLayerAndroid(*layer);
    485          layer->unref();
    486          layer = iframe;
    487     }
    488 
    489     if (isFixed) {
    490         FixedPositioning* fixedPosition = new FixedPositioning(layer);
    491 
    492         fixedPosition->m_fixedLeft = readSkLength(stream);
    493         fixedPosition->m_fixedTop = readSkLength(stream);
    494         fixedPosition->m_fixedRight = readSkLength(stream);
    495         fixedPosition->m_fixedBottom = readSkLength(stream);
    496         fixedPosition->m_fixedMarginLeft = readSkLength(stream);
    497         fixedPosition->m_fixedMarginTop = readSkLength(stream);
    498         fixedPosition->m_fixedMarginRight = readSkLength(stream);
    499         fixedPosition->m_fixedMarginBottom = readSkLength(stream);
    500         fixedPosition->m_fixedRect = readSkRect(stream);
    501         fixedPosition->m_renderLayerPos.setX(stream->readS32());
    502         fixedPosition->m_renderLayerPos.setY(stream->readS32());
    503 
    504         layer->setFixedPosition(fixedPosition);
    505     } else {
    506         // Not a fixed element, bypass the values in the stream
    507         readSkLength(stream); // fixedLeft
    508         readSkLength(stream); // fixedTop
    509         readSkLength(stream); // fixedRight
    510         readSkLength(stream); // fixedBottom
    511         readSkLength(stream); // fixedMarginLeft
    512         readSkLength(stream); // fixedMarginTop
    513         readSkLength(stream); // fixedMarginRight
    514         readSkLength(stream); // fixedMarginBottom
    515         readSkRect(stream);   // fixedRect
    516         stream->readS32();    // renderLayerPos.x()
    517         stream->readS32();    // renderLayerPos.y()
    518     }
    519 
    520     layer->m_backfaceVisibility = stream->readBool();
    521     layer->m_visible = stream->readBool();
    522     layer->m_backgroundColor = stream->readU32();
    523     layer->m_preserves3D = stream->readBool();
    524     layer->m_anchorPointZ = stream->readScalar();
    525     layer->m_drawOpacity = stream->readScalar();
    526     bool hasContentsImage = stream->readBool();
    527     if (hasContentsImage) {
    528         int size = stream->readU32();
    529         SkAutoMalloc storage(size);
    530         stream->read(storage.get(), size);
    531         SkOrderedReadBuffer buffer(storage.get(), size);
    532         SkBitmap contentsImage;
    533         contentsImage.unflatten(buffer);
    534         SkBitmapRef* imageRef = new SkBitmapRef(contentsImage);
    535         layer->setContentsImage(imageRef);
    536         delete imageRef;
    537     }
    538     bool hasRecordingPicture = stream->readBool();
    539     if (hasRecordingPicture) {
    540       LayerContent* content;
    541         if (version == 1) {
    542             content = new LegacyPictureLayerContent(stream);
    543         } else {
    544             SkPicture* picture = new SkPicture(stream);
    545             content = new PictureLayerContent(picture);
    546             SkSafeUnref(picture);
    547         }
    548         layer->setContent(content);
    549         SkSafeUnref(content);
    550     }
    551     int animationCount = stream->readU32(); // TODO: Support (maybe?)
    552     readTransformationMatrix(stream, layer->m_transform);
    553     readTransformationMatrix(stream, layer->m_childrenTransform);
    554     if (type == LTScrollableLayerAndroid) {
    555         ScrollableLayerAndroid* scrollableLayer =
    556                 static_cast<ScrollableLayerAndroid*>(layer);
    557         scrollableLayer->m_scrollLimits.set(
    558                 stream->readScalar(),
    559                 stream->readScalar(),
    560                 stream->readScalar(),
    561                 stream->readScalar());
    562     }
    563     int childCount = stream->readU32();
    564     for (int i = 0; i < childCount; i++) {
    565         LayerAndroid *childLayer = deserializeLayer(version, stream);
    566         if (childLayer)
    567             layer->addChild(childLayer);
    568     }
    569     ALOGV("Created layer with id %d", layer->uniqueId());
    570     return layer;
    571 }