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.
5 package org.chromium.android_webview;
7 import android.graphics.Canvas;
8 import android.graphics.Picture;
9 import android.graphics.Rect;
11 import org.chromium.base.CalledByNative;
12 import org.chromium.base.JNINamespace;
13 import org.chromium.content.common.CleanupReference;
15 import java.io.OutputStream;
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 {
22 private int mNativeAwPicture;
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;
33 nativeDestroy(mNativeAwPicture);
37 private CleanupReference mCleanupReference;
40 * @param nativeAwPicture is an instance of the AwPicture native class. Ownership is
41 * taken by this java instance.
43 AwPicture(int nativeAwPicture) {
44 mNativeAwPicture = nativeAwPicture;
45 mCleanupReference = new CleanupReference(this, new DestroyRunnable(nativeAwPicture));
49 public Canvas beginRecording(int width, int height) {
50 unsupportedOperation();
55 public void endRecording() {
56 // Intentional no-op. The native picture ended recording prior to java c'tor call.
60 public int getWidth() {
61 return nativeGetWidth(mNativeAwPicture);
65 public int getHeight() {
66 return nativeGetHeight(mNativeAwPicture);
69 // Effectively a local variable of draw(), but held as a member to avoid GC churn.
70 private Rect mClipBoundsTemporary = new Rect();
73 public void draw(Canvas canvas) {
74 canvas.getClipBounds(mClipBoundsTemporary);
75 nativeDraw(mNativeAwPicture, canvas,
76 mClipBoundsTemporary.left, mClipBoundsTemporary.top,
77 mClipBoundsTemporary.right, mClipBoundsTemporary.bottom);
81 public void writeToStream(OutputStream stream) {
82 unsupportedOperation();
85 private void unsupportedOperation() {
86 throw new IllegalStateException("Unsupported in AwPicture");
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);