| 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview; |
| 6 | |
| 7 | import android.graphics.Canvas; |
| 8 | import android.graphics.Picture; |
| 9 | import android.graphics.Rect; |
| 10 | |
| 11 | import org.chromium.base.CalledByNative; |
| 12 | import org.chromium.base.JNINamespace; |
| 13 | import org.chromium.content.common.CleanupReference; |
| 14 | |
| 15 | import java.io.OutputStream; |
| 16 | |
| 17 | // A simple wrapper around a SkPicture, that allows final rendering to be performed using the |
| 18 | // chromium skia library. |
| 19 | @JNINamespace("android_webview") |
| 20 | class AwPicture extends Picture { |
| 21 | |
| 22 | private int mNativeAwPicture; |
| 23 | |
| 24 | // There is no explicit destroy method on Picture base-class, so cleanup is always |
| 25 | // handled via the CleanupReference. |
| 26 | private static final class DestroyRunnable implements Runnable { |
| 27 | private int mNativeAwPicture; |
| 28 | private DestroyRunnable(int nativeAwPicture) { |
| 29 | mNativeAwPicture = nativeAwPicture; |
| 30 | } |
| 31 | @Override |
| 32 | public void run() { |
| 33 | nativeDestroy(mNativeAwPicture); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | private CleanupReference mCleanupReference; |
| 38 | |
| 39 | /** |
| 40 | * @param nativeAwPicture is an instance of the AwPicture native class. Ownership is |
| 41 | * taken by this java instance. |
| 42 | */ |
| 43 | AwPicture(int nativeAwPicture) { |
| 44 | mNativeAwPicture = nativeAwPicture; |
| 45 | mCleanupReference = new CleanupReference(this, new DestroyRunnable(nativeAwPicture)); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public Canvas beginRecording(int width, int height) { |
| 50 | unsupportedOperation(); |
| 51 | return null; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public void endRecording() { |
| 56 | // Intentional no-op. The native picture ended recording prior to java c'tor call. |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public int getWidth() { |
| 61 | return nativeGetWidth(mNativeAwPicture); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int getHeight() { |
| 66 | return nativeGetHeight(mNativeAwPicture); |
| 67 | } |
| 68 | |
| 69 | // Effectively a local variable of draw(), but held as a member to avoid GC churn. |
| 70 | private Rect mClipBoundsTemporary = new Rect(); |
| 71 | |
| 72 | @Override |
| 73 | public void draw(Canvas canvas) { |
| 74 | canvas.getClipBounds(mClipBoundsTemporary); |
| 75 | nativeDraw(mNativeAwPicture, canvas, |
| 76 | mClipBoundsTemporary.left, mClipBoundsTemporary.top, |
| 77 | mClipBoundsTemporary.right, mClipBoundsTemporary.bottom); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void writeToStream(OutputStream stream) { |
| 82 | unsupportedOperation(); |
| 83 | } |
| 84 | |
| 85 | private void unsupportedOperation() { |
| 86 | throw new IllegalStateException("Unsupported in AwPicture"); |
| 87 | } |
| 88 | |
| 89 | private static native void nativeDestroy(int nativeAwPicture); |
| 90 | private native int nativeGetWidth(int nativeAwPicture); |
| 91 | private native int nativeGetHeight(int nativeAwPicture); |
| 92 | private native void nativeDraw(int nativeAwPicture, Canvas canvas, |
| 93 | int left, int top, int right, int bottom); |
| 94 | } |
| 95 | |