The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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> |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 19 | #include <sys/socket.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 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 Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 28 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <android_runtime/AndroidRuntime.h> |
| 30 | #include <utils/misc.h> |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | static int gPrevDur; |
| 37 | |
| 38 | static 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 Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | // ---------------------------------------------------------------------------- |
| 81 | |
| 82 | const char* const kClassPathName = "android/view/ViewRoot"; |
| 83 | |
| 84 | static JNINativeMethod gMethods[] = { |
| 85 | { "nativeShowFPS", "(Landroid/graphics/Canvas;I)V", |
Jeff Brown | 8e03b75 | 2010-06-13 19:16:55 -0700 | [diff] [blame] | 86 | (void*)android_view_ViewRoot_showFPS } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | int register_android_view_ViewRoot(JNIEnv* env) { |
| 90 | return AndroidRuntime::registerNativeMethods(env, |
| 91 | kClassPathName, gMethods, NELEM(gMethods)); |
| 92 | } |
| 93 | |
| 94 | }; |
| 95 | |