Upstream version 9.37.195.0
[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
10 import org.chromium.base.JNINamespace;
11 import org.chromium.content.common.CleanupReference;
12
13 import java.io.OutputStream;
14
15 // A simple wrapper around a SkPicture, that allows final rendering to be performed using the
16 // chromium skia library.
17 @JNINamespace("android_webview")
18 class AwPicture extends Picture {
19
20     private long mNativeAwPicture;
21
22     // There is no explicit destroy method on Picture base-class, so cleanup is always
23     // handled via the CleanupReference.
24     private static final class DestroyRunnable implements Runnable {
25         private long mNativeAwPicture;
26         private DestroyRunnable(long nativeAwPicture) {
27             mNativeAwPicture = nativeAwPicture;
28         }
29         @Override
30         public void run() {
31             nativeDestroy(mNativeAwPicture);
32         }
33     }
34
35     private CleanupReference mCleanupReference;
36
37     /**
38      * @param nativeAwPicture is an instance of the AwPicture native class. Ownership is
39      *                        taken by this java instance.
40      */
41     AwPicture(long nativeAwPicture) {
42         mNativeAwPicture = nativeAwPicture;
43         mCleanupReference = new CleanupReference(this, new DestroyRunnable(nativeAwPicture));
44     }
45
46     @Override
47     public Canvas beginRecording(int width, int height) {
48         unsupportedOperation();
49         return null;
50     }
51
52     @Override
53     public void endRecording() {
54         // Intentional no-op. The native picture ended recording prior to java c'tor call.
55     }
56
57     @Override
58     public int getWidth() {
59         return nativeGetWidth(mNativeAwPicture);
60     }
61
62     @Override
63     public int getHeight() {
64         return nativeGetHeight(mNativeAwPicture);
65     }
66
67     @Override
68     public void draw(Canvas canvas) {
69         nativeDraw(mNativeAwPicture, canvas);
70     }
71
72     @Override
73     @SuppressWarnings("deprecation")
74     public void writeToStream(OutputStream stream) {
75         unsupportedOperation();
76     }
77
78     private void unsupportedOperation() {
79         throw new IllegalStateException("Unsupported in AwPicture");
80     }
81
82     private static native void nativeDestroy(long nativeAwPicture);
83     private native int nativeGetWidth(long nativeAwPicture);
84     private native int nativeGetHeight(long nativeAwPicture);
85     private native void nativeDraw(long nativeAwPicture, Canvas canvas);
86 }
87