aboutsummaryrefslogtreecommitdiffhomepage
path: root/platform_tools/android/apps/sample_app/src/main/jni/com_skia_SkiaSampleRenderer.cpp
blob: e5c6660921c8f8d27c2f59725b30a3ff29a3888e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright 2011 Skia
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "com_skia_SkiaSampleRenderer.h"

#include "SampleApp.h"
#include "SkApplication.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkEvent.h"
#include "SkWindow.h"

#include <jni.h>
#include "AndroidKeyToSkKey.h"


///////////////////////////////////////////
///////////////// Globals /////////////////
///////////////////////////////////////////

struct ActivityGlue {
    JNIEnv* m_env;
    jweak m_obj;
    jmethodID m_setTitle;
    jmethodID m_setSlideList;
    ActivityGlue() {
        m_env = nullptr;
        m_obj = nullptr;
        m_setTitle = nullptr;
        m_setSlideList = nullptr;
    }
} gActivityGlue;

struct WindowGlue {
    jweak m_obj;
    jmethodID m_inval;
    jmethodID m_queueSkEvent;
    jmethodID m_startTimer;
    jmethodID m_getMSAASampleCount;
    WindowGlue() {
        m_obj = nullptr;
        m_inval = nullptr;
        m_queueSkEvent = nullptr;
        m_startTimer = nullptr;
        m_getMSAASampleCount = nullptr;
    }
} gWindowGlue;

SampleWindow* gWindow;

///////////////////////////////////////////
///////////// SkOSWindow impl /////////////
///////////////////////////////////////////

SkOSWindow::SkOSWindow(void*) : fDestroyRequested(false) {
}

SkOSWindow::~SkOSWindow() {
}

bool SkOSWindow::attach(SkBackEndTypes /* attachType */, int /*msaaSampleCount*/, AttachmentInfo* info)
{
    JNIEnv* env = gActivityGlue.m_env;
    if (!env || !gWindowGlue.m_getMSAASampleCount || !gWindowGlue.m_obj) {
        return false;
    }
    if (env->IsSameObject(gWindowGlue.m_obj, nullptr)) {
        SkDebugf("ERROR: The JNI WeakRef to the Window is invalid");
        return false;
    }
    info->fSampleCount = env->CallIntMethod(gWindowGlue.m_obj, gWindowGlue.m_getMSAASampleCount);

    // This is the value requested in SkiaSampleView.java.
    info->fStencilBits = 8;
    return true;
}

void SkOSWindow::release() {
}

void SkOSWindow::present() {
}

void SkOSWindow::closeWindow() {
}

void SkOSWindow::setVsync(bool) {
}

void SkOSWindow::onSetTitle(const char title[])
{
    JNIEnv* env = gActivityGlue.m_env;
    if (!env) {
        return;
    }
    if (env->IsSameObject(gActivityGlue.m_obj, nullptr)) {
        SkDebugf("ERROR: The JNI WeakRef to the Activity is invalid");
        return;
    }

    jstring string = env->NewStringUTF(title);
    env->CallVoidMethod(gActivityGlue.m_obj, gActivityGlue.m_setTitle, string);
    env->DeleteLocalRef(string);
}

void SkOSWindow::onHandleInval(const SkIRect& rect)
{
    JNIEnv* env = gActivityGlue.m_env;
    if (!env || !gWindowGlue.m_inval || !gWindowGlue.m_obj) {
        return;
    }
    if (env->IsSameObject(gWindowGlue.m_obj, nullptr)) {
        SkDebugf("ERROR: The JNI WeakRef to the Window is invalid");
        return;
    }
    env->CallVoidMethod(gWindowGlue.m_obj, gWindowGlue.m_inval);
}

///////////////////////////////////////////
/////////////// SkEvent impl //////////////
///////////////////////////////////////////

void SkEvent::SignalQueueTimer(SkMSec ms)
{
    JNIEnv* env = gActivityGlue.m_env;
    if (!env || !gWindowGlue.m_startTimer || !gWindowGlue.m_obj || !ms) {
        return;
    }
    if (env->IsSameObject(gWindowGlue.m_obj, nullptr)) {
        SkDebugf("ERROR: The JNI WeakRef to the Window is invalid");
        return;
    }
    env->CallVoidMethod(gWindowGlue.m_obj,
            gWindowGlue.m_startTimer, ms);
}

void SkEvent::SignalNonEmptyQueue()
{
    JNIEnv* env = gActivityGlue.m_env;
    if (!env || !gWindowGlue.m_queueSkEvent || !gWindowGlue.m_obj) {
        return;
    }
    if (env->IsSameObject(gWindowGlue.m_obj, nullptr)) {
        SkDebugf("ERROR: The JNI WeakRef to the Window is invalid");
        return;
    }
    env->CallVoidMethod(gWindowGlue.m_obj, gWindowGlue.m_queueSkEvent);
}

///////////////////////////////////////////
////////////////// JNI ////////////////////
///////////////////////////////////////////

static jmethodID GetJMethod(JNIEnv* env, jclass clazz, const char name[],
        const char signature[])
{
    jmethodID m = env->GetMethodID(clazz, name, signature);
    if (!m) SkDebugf("Could not find Java method %s\n", name);
    return m;
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_init(JNIEnv* env,
        jobject thiz, jobject jsampleActivity, jstring cmdLineFlags, jint msaaSampleCount)
{
    // setup jni hooks to the java activity
    gActivityGlue.m_env = env;
    jclass clazz = env->FindClass("com/skia/SkiaSampleActivity");
    gActivityGlue.m_obj = env->NewWeakGlobalRef(jsampleActivity);
    gActivityGlue.m_setTitle = GetJMethod(env, clazz, "setTitle", "(Ljava/lang/CharSequence;)V");
    gActivityGlue.m_setSlideList = GetJMethod(env, clazz, "setSlideList", "([Ljava/lang/String;)V");
    env->DeleteLocalRef(clazz);

    // setup jni hooks to the java renderer
    clazz = env->FindClass("com/skia/SkiaSampleRenderer");
    gWindowGlue.m_obj = env->NewWeakGlobalRef(thiz);
    gWindowGlue.m_inval = GetJMethod(env, clazz, "requestRender", "()V");
    gWindowGlue.m_queueSkEvent = GetJMethod(env, clazz, "queueSkEvent", "()V");
    gWindowGlue.m_startTimer = GetJMethod(env, clazz, "startTimer", "(I)V");
    gWindowGlue.m_getMSAASampleCount = GetJMethod(env, clazz, "getMSAASampleCount", "()I");
    env->DeleteLocalRef(clazz);

    application_init();

    const char* flags = env->GetStringUTFChars(cmdLineFlags, JNI_FALSE);
    SkTArray<SkString> flagEntries;
    SkStrSplit(flags, " ", &flagEntries);

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

    SkString msaaSampleCountString;
    if (msaaSampleCount > 0) {
        args.push_back("--msaa");
        msaaSampleCountString.appendS32(static_cast<uint32_t>(msaaSampleCount));
        args.push_back(msaaSampleCountString.c_str());
    }

    if (gWindow) {
        SkDebugf("The sample window already exists.");
    } else {
        gWindow = new SampleWindow(nullptr, args.count(), const_cast<char**>(args.begin()), nullptr);
    }

    // cleanup the command line flags
    env->ReleaseStringUTFChars(cmdLineFlags, flags);

    // send the list of slides up to the activity
    const int slideCount = gWindow->sampleCount();
    jobjectArray slideList = env->NewObjectArray(slideCount, env->FindClass("java/lang/String"), env->NewStringUTF(""));
    for (int i = 0; i < slideCount; i++) {
        jstring slideTitle = env->NewStringUTF(gWindow->getSampleTitle(i).c_str());
        env->SetObjectArrayElement(slideList, i, slideTitle);
        env->DeleteLocalRef(slideTitle);
    }
    env->CallVoidMethod(gActivityGlue.m_obj, gActivityGlue.m_setSlideList, slideList);
    env->DeleteLocalRef(slideList);
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_term(JNIEnv* env,
        jobject thiz)
{
    delete gWindow;
    gWindow = nullptr;
    application_term();
    if (gWindowGlue.m_obj) {
        env->DeleteWeakGlobalRef(gWindowGlue.m_obj);
        gWindowGlue.m_obj = nullptr;
    }
    if (gActivityGlue.m_obj) {
        env->DeleteWeakGlobalRef(gActivityGlue.m_obj);
        gActivityGlue.m_obj = nullptr;
    }
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_draw(
        JNIEnv* env, jobject thiz)
{
    if (!gWindow) return;
    gWindow->update(nullptr);
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_updateSize(JNIEnv* env,
        jobject thiz, jint w, jint h)
{
    gWindow->resize(w, h);
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_handleClick(JNIEnv* env,
        jobject thiz, jint owner, jfloat x, jfloat y, jint jstate)
{
    SkView::Click::State state;
    switch(jstate) {
        case 0:     // MotionEvent.ACTION_DOWN
            state = SkView::Click::kDown_State;
            break;
        case 1:     // MotionEvent.ACTION_UP
        case 3:     // MotionEvent.ACTION_CANCEL
            state = SkView::Click::kUp_State;
            break;
        case 2:     // MotionEvent.ACTION_MOVE
            state = SkView::Click::kMoved_State;
            break;
        default:
            SkDebugf("motion event ignored\n");
            return;
    }
    gWindow->handleClick(x, y, state, reinterpret_cast<void*>(owner));
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_nextSample(
        JNIEnv* env, jobject thiz)
{
    gWindow->nextSample();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_previousSample(
        JNIEnv* env, jobject thiz)
{
    gWindow->previousSample();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_goToSample(
        JNIEnv* env, jobject thiz, jint position)
{
    gWindow->goToSample(position);
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_toggleRenderingMode(
        JNIEnv* env, jobject thiz)
{
    gWindow->toggleRendering();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_showOverview(
        JNIEnv* env, jobject thiz)
{
    gWindow->showOverview();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_toggleSlideshow(
        JNIEnv* env, jobject thiz)
{
    gWindow->toggleSlideshow();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_toggleFPS(
        JNIEnv* env, jobject thiz)
{
    gWindow->toggleFPS();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_toggleTiling(
        JNIEnv* env, jobject thiz)
{
    gWindow->handleChar('t');
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_toggleBBox(
        JNIEnv* env, jobject thiz)
{
    gWindow->handleChar('b');
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_processSkEvent(
        JNIEnv* env, jobject thiz)
{
    if (SkEvent::ProcessEvent()) {
        SkEvent::SignalNonEmptyQueue();
    }
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_serviceQueueTimer(
        JNIEnv* env, jobject thiz)
{
    SkEvent::ServiceQueueTimer();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_saveToPDF(
        JNIEnv* env, jobject thiz)
{
    gWindow->saveToPdf();
}

JNIEXPORT void JNICALL Java_com_skia_SkiaSampleRenderer_postInval(
        JNIEnv* env, jobject thiz)
{
    gWindow->postInvalDelay();
}