aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/visualbench/src/main/jni/main.cpp
blob: 2ea6b76b39220cf003f3c82a31d740c368ed67ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 */

#include <android_native_app_glue.h>

#include "SkApplication.h"
#include "VisualBench.h"

/**
 * Shared state for our app.
 */
enum State {
    kInit_State,
    kAnimate_State,
    kDestroyRequested_State,
    kFinished_State,
};

struct VisualBenchState {
    VisualBenchState() : fApp(NULL), fWindow(NULL), fState(kInit_State) {}
    struct android_app* fApp;
    SkOSWindow* fWindow;
    SkTArray<SkString> fFlags;
    State fState;
};

static void handle_cmd(struct android_app* app, int32_t cmd) {
    struct VisualBenchState* state = (struct VisualBenchState*)app->userData;
    switch (cmd) {
        case APP_CMD_INIT_WINDOW:
            // The window is being shown, get it ready.
            if (state->fApp->window != NULL && kInit_State == state->fState) {
                // drain any events that occurred before |window| was assigned.
                while (SkEvent::ProcessEvent());

                // Start normal Skia sequence
                application_init();

                SkTArray<const char*> args;
                args.push_back("VisualBench");
                for (int i = 0; i < state->fFlags.count(); i++) {
                    SkDebugf(state->fFlags[i].c_str());
                    args.push_back(state->fFlags[i].c_str());
                }

                state->fWindow = create_sk_window((void*)state->fApp->window,
                                                  args.count(),
                                                  const_cast<char**>(args.begin()));
                state->fWindow->forceInvalAll();
                state->fState = kAnimate_State;
            }
            break;
        case APP_CMD_TERM_WINDOW:
            state->fState = kDestroyRequested_State;
            break;
    }
}

void android_main(struct android_app* state) {
    struct VisualBenchState visualBenchState;

    // Make sure glue isn't stripped.
    app_dummy();

    state->userData = &visualBenchState;
    state->onAppCmd = handle_cmd;
    visualBenchState.fApp = state;

    // Get command line arguments
    JavaVM* jvm = state->activity->vm;
    JNIEnv *env;
    jvm->AttachCurrentThread(&env, 0);

    jobject me = state->activity->clazz;

    jclass acl = env->GetObjectClass(me); //class pointer of NativeActivity
    jmethodID giid = env->GetMethodID(acl, "getIntent", "()Landroid/content/Intent;");
    jobject intent = env->CallObjectMethod(me, giid); //Got our intent

    jclass icl = env->GetObjectClass(intent); //class pointer of Intent
    jmethodID gseid = env->GetMethodID(icl, "getStringExtra",
                                       "(Ljava/lang/String;)Ljava/lang/String;");

    jstring jsParam1 = (jstring)env->CallObjectMethod(intent, gseid,
                                                      env->NewStringUTF("cmdLineFlags"));
    if (jsParam1) {
        const char* flags = env->GetStringUTFChars(jsParam1, 0);
        SkStrSplit(flags, " ", &visualBenchState.fFlags);
        env->ReleaseStringUTFChars(jsParam1, flags);
    }
    jvm->DetachCurrentThread();

    while (1) {
        // Read all pending events.
        int ident;
        int events;
        struct android_poll_source* source;

        // We loop until all events are read, then continue to draw the next frame of animation.
        while ((ident=ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0) {
            // Process this event.
            if (source != NULL) {
                source->process(state, source);
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                return;
            }

        }

        if (visualBenchState.fWindow) {
            if (visualBenchState.fWindow->destroyRequested()) {
                visualBenchState.fState = kDestroyRequested_State;
            } else {
                visualBenchState.fWindow->update(NULL);
            }
        }

        if (kDestroyRequested_State == visualBenchState.fState) {
            SkDELETE(visualBenchState.fWindow);
            visualBenchState.fWindow = NULL;
            application_term();
            ANativeActivity_finish(state->activity);
            visualBenchState.fState = kFinished_State;
        }
    }
}