1 /*
2 * Copyright (C) 2008 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 */
16
17 #include <stdio.h>
18 #include <assert.h>
19
20 #include <core/SkCanvas.h>
21 #include <core/SkDevice.h>
22 #include <core/SkPaint.h>
23 #include <utils/SkGLCanvas.h>
24 #include "GraphicsJNI.h"
25
26 #include "jni.h"
27 #include <android_runtime/AndroidRuntime.h>
28 #include <utils/misc.h>
29
30 // ----------------------------------------------------------------------------
31
32 namespace android {
33
34 static int gPrevDur;
35
android_view_ViewRoot_showFPS(JNIEnv * env,jobject,jobject jcanvas,jint dur)36 static void android_view_ViewRoot_showFPS(JNIEnv* env, jobject, jobject jcanvas,
37 jint dur) {
38 NPE_CHECK_RETURN_VOID(env, jcanvas);
39 SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
40 const SkBitmap& bm = canvas->getDevice()->accessBitmap(false);
41 int height = bm.height();
42 SkScalar bot = SkIntToScalar(height);
43
44 if (height < 200) {
45 return;
46 }
47
48 SkMatrix m;
49 SkRect r;
50 SkPaint p;
51 char str[4];
52
53 dur = (gPrevDur + dur) >> 1;
54 gPrevDur = dur;
55
56 dur = 1000 / dur;
57 str[3] = (char)('0' + dur % 10); dur /= 10;
58 str[2] = (char)('0' + dur % 10); dur /= 10;
59 str[1] = (char)('0' + dur % 10); dur /= 10;
60 str[0] = (char)('0' + dur % 10);
61
62 m.reset();
63 r.set(0, bot-SkIntToScalar(10), SkIntToScalar(26), bot);
64 p.setAntiAlias(true);
65 p.setTextSize(SkIntToScalar(10));
66
67 canvas->save();
68 canvas->setMatrix(m);
69 canvas->clipRect(r, SkRegion::kReplace_Op);
70 p.setColor(SK_ColorWHITE);
71 canvas->drawPaint(p);
72 p.setColor(SK_ColorBLACK);
73 canvas->drawText(str, 4, SkIntToScalar(1), bot - SK_Scalar1, p);
74 canvas->restore();
75 }
76
android_view_ViewRoot_abandonGlCaches(JNIEnv * env,jobject)77 static void android_view_ViewRoot_abandonGlCaches(JNIEnv* env, jobject) {
78 SkGLCanvas::AbandonAllTextures();
79 }
80
81 // ----------------------------------------------------------------------------
82
83 const char* const kClassPathName = "android/view/ViewRoot";
84
85 static JNINativeMethod gMethods[] = {
86 { "nativeShowFPS", "(Landroid/graphics/Canvas;I)V",
87 (void*)android_view_ViewRoot_showFPS },
88 { "nativeAbandonGlCaches", "()V",
89 (void*)android_view_ViewRoot_abandonGlCaches }
90 };
91
register_android_view_ViewRoot(JNIEnv * env)92 int register_android_view_ViewRoot(JNIEnv* env) {
93 return AndroidRuntime::registerNativeMethods(env,
94 kClassPathName, gMethods, NELEM(gMethods));
95 }
96
97 };
98
99