1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package android.webkit; 17 18import android.graphics.Point; 19import android.webkit.WebViewCore.DrawData; 20 21import java.io.DataInputStream; 22import java.io.DataOutputStream; 23import java.io.IOException; 24import java.io.InputStream; 25import java.io.OutputStream; 26 27/** 28 * @hide 29 */ 30class 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