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

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: Merge 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
« no previous file with comments | « tools/viewer/sk_app/android/surface_glue_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/looper.h>
16 #include <android/native_window_jni.h>
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 = 1;
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::paintIfNeeded() {
48 if (fNativeWindow && fWindow) {
49 fWindow->onPaint();
50 }
51 }
52
53 void SkiaAndroidApp::postMessage(const Message& message) {
54 auto writeSize = write(fPipes[1], &message, sizeof(message));
55 SkASSERT(writeSize == sizeof(message));
56 }
57
58 void SkiaAndroidApp::readMessage(Message* message) {
59 auto readSize = read(fPipes[0], message, sizeof(Message));
60 SkASSERT(readSize == sizeof(Message));
61 }
62
63 static int message_callback(int fd, int events, void* data) {
64 auto skiaAndroidApp = (SkiaAndroidApp*)data;
65 Message message;
66 skiaAndroidApp->readMessage(&message);
67 SkDebugf("message_callback %d", message.fType);
68 SkASSERT(message.fType != kUndefined);
69
70 switch (message.fType) {
71 case kDestroyApp: {
72 delete skiaAndroidApp;
73 pthread_exit(nullptr);
74 return 0;
75 }
76 case kContentInvalidated: {
77 skiaAndroidApp->paintIfNeeded();
78 break;
79 }
80 case kSurfaceCreated: {
81 SkASSERT(!skiaAndroidApp->fNativeWindow && message.fNativeWindow);
82 skiaAndroidApp->fNativeWindow = message.fNativeWindow;
83 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
84 window_android->initDisplay(skiaAndroidApp->fNativeWindow);
85 skiaAndroidApp->paintIfNeeded();
86 break;
87 }
88 case kSurfaceChanged: {
89 SkASSERT(message.fNativeWindow == skiaAndroidApp->fNativeWindow &&
90 message.fNativeWindow);
91 int width = ANativeWindow_getWidth(skiaAndroidApp->fNativeWindow);
92 int height = ANativeWindow_getHeight(skiaAndroidApp->fNativeWindow);
93 skiaAndroidApp->fWindow->onResize(width, height);
94 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
95 window_android->setContentRect(0, 0, width, height);
96 skiaAndroidApp->paintIfNeeded();
97 break;
98 }
99 case kSurfaceDestroyed: {
100 if (skiaAndroidApp->fNativeWindow) {
101 auto window_android = (Window_android*)skiaAndroidApp->fWindow;
102 window_android->onDisplayDestroyed();
103 ANativeWindow_release(skiaAndroidApp->fNativeWindow);
104 skiaAndroidApp->fNativeWindow = nullptr;
105 }
106 break;
107 }
108 default: {
109 // do nothing
110 }
111 }
112
113 return 1; // continue receiving callbacks
114 }
115
116 void* pthread_main(void* arg) {
117 SkDebugf("pthread_main begins");
118
119 auto skiaAndroidApp = (SkiaAndroidApp*)arg;
120
121 ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
122 pipe(skiaAndroidApp->fPipes);
123 ALooper_addFd(looper, skiaAndroidApp->fPipes[0], LOOPER_ID_MESSAGEPIPE, ALOO PER_EVENT_INPUT,
124 message_callback, skiaAndroidApp);
125
126 int ident;
127 int events;
128 struct android_poll_source* source;
129
130 skiaAndroidApp->fApp = Application::Create(0, nullptr, skiaAndroidApp);
131
132 while ((ident = ALooper_pollAll(-1, nullptr, &events, (void**)&source)) >= 0 ) {
133 SkDebugf("ALooper_pollAll ident=%d", ident);
134 }
135
136 return nullptr;
137 }
138
139 extern "C" // extern "C" is needed for JNI (although the method itself is in C+ +)
140 JNIEXPORT jlong JNICALL
141 Java_org_skia_viewer_ViewerApplication_createNativeApp(JNIEnv* env, jobject activity) {
142 SkiaAndroidApp* skiaAndroidApp = new SkiaAndroidApp;
143 return (jlong)((size_t)skiaAndroidApp);
144 }
145
146 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerApplication_destroy NativeApp(
147 JNIEnv* env, jobject activity, jlong handle) {
148 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
149 skiaAndroidApp->postMessage(Message(kDestroyApp));
150 }
151
152 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC reated(
153 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
154 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
155 Message message(kSurfaceCreated);
156 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
157 skiaAndroidApp->postMessage(message);
158 }
159
160 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceC hanged(
161 JNIEnv* env, jobject activity, jlong handle, jobject surface) {
162 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
163 Message message(kSurfaceChanged);
164 message.fNativeWindow = ANativeWindow_fromSurface(env, surface);
165 skiaAndroidApp->postMessage(message);
166 }
167
168 extern "C" JNIEXPORT void JNICALL Java_org_skia_viewer_ViewerActivity_onSurfaceD estroyed(
169 JNIEnv* env, jobject activity, jlong handle) {
170 auto skiaAndroidApp = (SkiaAndroidApp*)handle;
171 skiaAndroidApp->postMessage(Message(kSurfaceDestroyed));
172 }
173
174 } // namespace sk_app
OLDNEW
« no previous file with comments | « tools/viewer/sk_app/android/surface_glue_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698