Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: tools/viewer/sk_app/android/surface_glue_android.cpp

Issue 1952323004: Initial commit of our new Android app to demo Skia. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Clang Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "surface_glue_android.h"
9
10 #include <jni.h>
11 #include <pthread.h>
12 #include <stdio.h>
13 #include <unistd.h>
14
15 #include <android/native_window_jni.h>
16 #include <android_native_app_glue.h> // for LOOPER_ID_USER
17
18 #include "../Application.h"
19 #include "SkTypes.h"
20 #include "SkUtils.h"
21 #include "Window_android.h"
22
23 namespace sk_app {
24
25 static const int LOOPER_ID_MESSAGEPIPE = LOOPER_ID_USER;
26
27 void* pthread_main(void* arg);
28
29 SkiaAndroidApp::SkiaAndroidApp() {
30 fNativeWindow = nullptr;
31 pthread_create(&fThread, nullptr, pthread_main, this);
32 }
33
34 SkiaAndroidApp::~SkiaAndroidApp() {
35 if (fWindow) {
36 fWindow->detach();
37 }
38 if (fNativeWindow) {
39 ANativeWindow_release(fNativeWindow);
40 fNativeWindow = nullptr;
41 }
42 if (fApp) {
43 delete fApp;
44 }
45 }
46
47 void SkiaAndroidApp::postMessage(const Message& message) {
48 auto writeSize = write(fPipes[1], &message, sizeof(message));
49 SkASSERT(writeSize == sizeof(message));
50 }
51
52 void SkiaAndroidApp::readMessage(Message* message) {
53 auto readSize = read(fPipes[0], message, sizeof(Message));
54 SkASSERT(readSize == sizeof(Message));
55 }
56
57 static int message_callback(int fd, int events, void* data) {
58 auto skiaAndroidApp = (SkiaAndroidApp*)data;
59 Message message;
60 skiaAndroidApp->readMessage(&message);
61 SkDebugf("message_callback %d", message.fType);
62 SkASSERT(message.fType != kUndefined);
63
64 switch (message.fType) {
65 case kDestroyApp: {
66 delete skiaAndroidApp;
67 pthread_exit(nullptr);
68 return 0;
69 }
70 case kSurfaceCreated: {
71 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow);
72 skiaAndroidApp->fNativeWindow = message.fNativeWindow;
73 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
74 window_android->initDisplay(skiaAndroidApp->fNativeWindow);
75 window_android->paintIfNeeded();
76 break;
77 }
78 case kSurfaceChanged: {
79 SkASSERT(message.fNativeWindow == skiaAndroidApp->fNativeWindow &&
80 message.fNativeWindow);
81 int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow);
82 int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow);
83 skiaAndroidApp->fWindow->onResize(width, height);
84 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
85 window_android->setContentRect(0, 0, width, height);
86 window_android->paintIfNeeded();
87 break;
88 }
89 case kSurfaceDestroyed: {
90 if (skiaAndroidApp->fNativeWindow) {
91 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
92 window_android->onDisplayDestroyed();
93 ANativeWindow_release(skiaAndroidApp->fNativeWindow);
94 skiaAndroidApp->fNativeWindow = nullptr;
95 }
96 break;
97 }
98 default: {
99 // do nothing
100 }
101 }
102
103 return 1; // continue receiving callbacks
104 }
105
106 void* pthread_main(void* arg) {
107 SkDebugf("pthread_main begins");
108
109 auto skiaAndroidApp = (SkiaAndroidApp*)arg;
110
111 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
112 pipe(skiaAndroidApp->fPipes);
113 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT,
114 message_callback, skiaAndroidApp);
115
116 int ident;
117 int events;
118 struct android_poll_source* source;
119
120 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp);
121
122 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) {
123 SkDebugf("ALooper_pollAll ident=%d", ident);
124 }
125
126 return nullptr;
127 }
128
129 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +)
130 JNIEXPORT jlong JNICALL
131 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject activity) {
132 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp;
133 return (jlong)((size_t)skiaAndroidApp);
134 }
135
136 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp(
137 JNIEnv* env, jobject activity, jlong handle) {
138 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
139 skiaAndroidApp->postMessage(Message(kDestroyApp));
140 }
141
142 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated(
143 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
144 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
145 Message message(kSurfaceCreated);
146 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
147 skiaAndroidApp->postMessage(message);
148 }
149
150 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC hanged(
151 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
152 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
153 Message message(kSurfaceChanged);
154 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
155 skiaAndroidApp->postMessage(message);
156 }
157
158 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceD estroyed(
159 JNIEnv* env, jobject activity, jlong handle) {
160 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
161 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed));
162 }
163
164 } // namespace sk_app
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698