blob: 2988ae8bd5aab13a3a5480a7860c22c3dcc4c094 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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>
Christopher Tatefa9e7c02010-05-06 12:07:10 -070019#include <sys/socket.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21#include <core/SkCanvas.h>
22#include <core/SkDevice.h>
23#include <core/SkPaint.h>
24#include <utils/SkGLCanvas.h>
25#include "GraphicsJNI.h"
26
27#include "jni.h"
Christopher Tatefa9e7c02010-05-06 12:07:10 -070028#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <android_runtime/AndroidRuntime.h>
30#include <utils/misc.h>
31
32// ----------------------------------------------------------------------------
33
34namespace android {
35
36static int gPrevDur;
37
38static void android_view_ViewRoot_showFPS(JNIEnv* env, jobject, jobject jcanvas,
39 jint dur) {
40 NPE_CHECK_RETURN_VOID(env, jcanvas);
41 SkCanvas* canvas = GraphicsJNI::getNativeCanvas(env, jcanvas);
42 const SkBitmap& bm = canvas->getDevice()->accessBitmap(false);
43 int height = bm.height();
44 SkScalar bot = SkIntToScalar(height);
45
46 if (height < 200) {
47 return;
48 }
49
50 SkMatrix m;
51 SkRect r;
52 SkPaint p;
53 char str[4];
54
55 dur = (gPrevDur + dur) >> 1;
56 gPrevDur = dur;
57
58 dur = 1000 / dur;
59 str[3] = (char)('0' + dur % 10); dur /= 10;
60 str[2] = (char)('0' + dur % 10); dur /= 10;
61 str[1] = (char)('0' + dur % 10); dur /= 10;
62 str[0] = (char)('0' + dur % 10);
63
64 m.reset();
65 r.set(0, bot-SkIntToScalar(10), SkIntToScalar(26), bot);
66 p.setAntiAlias(true);
67 p.setTextSize(SkIntToScalar(10));
68
69 canvas->save();
70 canvas->setMatrix(m);
71 canvas->clipRect(r, SkRegion::kReplace_Op);
72 p.setColor(SK_ColorWHITE);
73 canvas->drawPaint(p);
74 p.setColor(SK_ColorBLACK);
75 canvas->drawText(str, 4, SkIntToScalar(1), bot - SK_Scalar1, p);
76 canvas->restore();
77}
78
Christopher Tatefa9e7c02010-05-06 12:07:10 -070079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080// ----------------------------------------------------------------------------
81
82const char* const kClassPathName = "android/view/ViewRoot";
83
84static JNINativeMethod gMethods[] = {
85 { "nativeShowFPS", "(Landroid/graphics/Canvas;I)V",
Jeff Brown8e03b752010-06-13 19:16:55 -070086 (void*)android_view_ViewRoot_showFPS }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087};
88
89int register_android_view_ViewRoot(JNIEnv* env) {
90 return AndroidRuntime::registerNativeMethods(env,
91 kClassPathName, gMethods, NELEM(gMethods));
92}
93
94};
95