6f245bce7bfdff39866c02130c0659a441f8687a
[platform/framework/web/crosswalk.git] / src / android_webview / java / src / org / chromium / android_webview / AwPicture.java
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.JNINamespace;
12 import org.chromium.content.common.CleanupReference;
13
14 import java.io.OutputStream;
15
16 // A simple wrapper around a SkPicture, that allows final rendering to be performed using the
17 // chromium skia library.
18 @JNINamespace("android_webview")
19 class AwPicture extends Picture {
20
21     private long mNativeAwPicture;
22
23     // There is no explicit destroy method on Picture base-class, so cleanup is always
24     // handled via the CleanupReference.
25     private static final class DestroyRunnable implements Runnable {
26         private long mNativeAwPicture;
27         private DestroyRunnable(long nativeAwPicture) {
28             mNativeAwPicture = nativeAwPicture;
29         }
30         @Override
31         public void run() {
32             nativeDestroy(mNativeAwPicture);
33         }
34     }
35
36     private CleanupReference mCleanupReference;
37
38     /**
39      * @param nativeAwPicture is an instance of the AwPicture native class. Ownership is
40      *                        taken by this java instance.
41      */
42     AwPicture(long nativeAwPicture) {
43         mNativeAwPicture = nativeAwPicture;
44         mCleanupReference = new CleanupReference(this, new DestroyRunnable(nativeAwPicture));
45     }
46
47     @Override
48     public Canvas beginRecording(int width, int height) {
49         unsupportedOperation();
50         return null;
51     }
52
53     @Override
54     public void endRecording() {
55         // Intentional no-op. The native picture ended recording prior to java c'tor call.
56     }
57
58     @Override
59     public int getWidth() {
60         return nativeGetWidth(mNativeAwPicture);
61     }
62
63     @Override
64     public int getHeight() {
65         return nativeGetHeight(mNativeAwPicture);
66     }
67
68     // Effectively a local variable of draw(), but held as a member to avoid GC churn.
69     private Rect mClipBoundsTemporary = new Rect();
70
71     @Override
72     public void draw(Canvas canvas) {
73         canvas.getClipBounds(mClipBoundsTemporary);
74         nativeDraw(mNativeAwPicture, canvas,
75                 mClipBoundsTemporary.left, mClipBoundsTemporary.top,
76                 mClipBoundsTemporary.right, mClipBoundsTemporary.bottom);
77     }
78
79     @Override
80     @SuppressWarnings("deprecation")
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(long nativeAwPicture);
90     private native int nativeGetWidth(long nativeAwPicture);
91     private native int nativeGetHeight(long nativeAwPicture);
92     private native void nativeDraw(long nativeAwPicture, Canvas canvas,
93             int left, int top, int right, int bottom);
94 }
95