frameworks/base
修订版 | 2f4457484a13422a28698cd06ce2efb2906d2f5f (tree) |
---|---|
时间 | 2010-02-27 17:46:27 |
作者 | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
merge from donut
@@ -377,7 +377,7 @@ public class SyncStorageEngine extends Handler { | ||
377 | 377 | int i = mAuthorities.size(); |
378 | 378 | while (i > 0) { |
379 | 379 | i--; |
380 | - AuthorityInfo authority = mAuthorities.get(i); | |
380 | + AuthorityInfo authority = mAuthorities.valueAt(i); | |
381 | 381 | if (authority.authority.equals(providerName) |
382 | 382 | && authority.enabled) { |
383 | 383 | return true; |
@@ -399,7 +399,7 @@ public class SyncStorageEngine extends Handler { | ||
399 | 399 | int i = mAuthorities.size(); |
400 | 400 | while (i > 0) { |
401 | 401 | i--; |
402 | - AuthorityInfo authority = mAuthorities.get(i); | |
402 | + AuthorityInfo authority = mAuthorities.valueAt(i); | |
403 | 403 | if (authority.authority.equals(providerName)) { |
404 | 404 | authority.enabled = sync; |
405 | 405 | } |
@@ -548,7 +548,7 @@ public class SyncStorageEngine extends Handler { | ||
548 | 548 | mPendingOperations.clear(); |
549 | 549 | final int N = mSyncStatus.size(); |
550 | 550 | for (int i=0; i<N; i++) { |
551 | - mSyncStatus.get(i).pending = false; | |
551 | + mSyncStatus.valueAt(i).pending = false; | |
552 | 552 | } |
553 | 553 | writePendingOperationsLocked(); |
554 | 554 | } |
@@ -843,7 +843,7 @@ public class SyncStorageEngine extends Handler { | ||
843 | 843 | SyncStatusInfo best = null; |
844 | 844 | final int N = mSyncStatus.size(); |
845 | 845 | for (int i=0; i<N; i++) { |
846 | - SyncStatusInfo cur = mSyncStatus.get(i); | |
846 | + SyncStatusInfo cur = mSyncStatus.valueAt(i); | |
847 | 847 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
848 | 848 | if (ainfo != null && ainfo.authority.equals(authority)) { |
849 | 849 | if (best == null) { |
@@ -864,7 +864,7 @@ public class SyncStorageEngine extends Handler { | ||
864 | 864 | synchronized (mAuthorities) { |
865 | 865 | final int N = mSyncStatus.size(); |
866 | 866 | for (int i=0; i<N; i++) { |
867 | - SyncStatusInfo cur = mSyncStatus.get(i); | |
867 | + SyncStatusInfo cur = mSyncStatus.valueAt(i); | |
868 | 868 | AuthorityInfo ainfo = mAuthorities.get(cur.authorityId); |
869 | 869 | if (ainfo == null) { |
870 | 870 | continue; |
@@ -1131,7 +1131,7 @@ public class SyncStorageEngine extends Handler { | ||
1131 | 1131 | |
1132 | 1132 | final int N = mAuthorities.size(); |
1133 | 1133 | for (int i=0; i<N; i++) { |
1134 | - AuthorityInfo authority = mAuthorities.get(i); | |
1134 | + AuthorityInfo authority = mAuthorities.valueAt(i); | |
1135 | 1135 | out.startTag(null, "authority"); |
1136 | 1136 | out.attribute(null, "id", Integer.toString(authority.ident)); |
1137 | 1137 | out.attribute(null, "account", authority.account); |
@@ -1217,7 +1217,7 @@ public class SyncStorageEngine extends Handler { | ||
1217 | 1217 | SyncStatusInfo st = null; |
1218 | 1218 | while (i > 0) { |
1219 | 1219 | i--; |
1220 | - st = mSyncStatus.get(i); | |
1220 | + st = mSyncStatus.valueAt(i); | |
1221 | 1221 | if (st.authorityId == authority.ident) { |
1222 | 1222 | found = true; |
1223 | 1223 | break; |
@@ -1,16 +1,16 @@ | ||
1 | 1 | /** |
2 | 2 | ** Copyright 2007, The Android Open Source Project |
3 | 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 | |
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 | 7 | ** |
8 | - ** http://www.apache.org/licenses/LICENSE-2.0 | |
8 | + ** http://www.apache.org/licenses/LICENSE-2.0 | |
9 | 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 | |
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 | 14 | ** limitations under the License. |
15 | 15 | */ |
16 | 16 |
@@ -53,18 +53,18 @@ public: | ||
53 | 53 | MallocHelper() { |
54 | 54 | mData = 0; |
55 | 55 | } |
56 | - | |
56 | + | |
57 | 57 | ~MallocHelper() { |
58 | 58 | if (mData != 0) { |
59 | 59 | free(mData); |
60 | 60 | } |
61 | 61 | } |
62 | - | |
62 | + | |
63 | 63 | void* alloc(size_t size) { |
64 | 64 | mData = malloc(size); |
65 | 65 | return mData; |
66 | 66 | } |
67 | - | |
67 | + | |
68 | 68 | private: |
69 | 69 | void* mData; |
70 | 70 | }; |
@@ -88,17 +88,17 @@ int visibilityTest(float* pWS, float* pPositions, int positionsLength, | ||
88 | 88 | int result = POLY_CLIP_OUT; |
89 | 89 | float* pTransformed = 0; |
90 | 90 | int transformedIndexCount = 0; |
91 | - | |
91 | + | |
92 | 92 | if ( indexCount < 3 ) { |
93 | 93 | return POLY_CLIP_OUT; |
94 | 94 | } |
95 | - | |
95 | + | |
96 | 96 | // Find out how many vertices we need to transform |
97 | 97 | // We transform every vertex between the min and max indices, inclusive. |
98 | 98 | // This is OK for the data sets we expect to use with this function, but |
99 | 99 | // for other loads it might be better to use a more sophisticated vertex |
100 | 100 | // cache of some sort. |
101 | - | |
101 | + | |
102 | 102 | int minIndex = 65536; |
103 | 103 | int maxIndex = -1; |
104 | 104 | for(int i = 0; i < indexCount; i++) { |
@@ -110,18 +110,18 @@ int visibilityTest(float* pWS, float* pPositions, int positionsLength, | ||
110 | 110 | maxIndex = index; |
111 | 111 | } |
112 | 112 | } |
113 | - | |
113 | + | |
114 | 114 | if ( maxIndex * 3 > positionsLength) { |
115 | 115 | return -1; |
116 | 116 | } |
117 | - | |
117 | + | |
118 | 118 | transformedIndexCount = maxIndex - minIndex + 1; |
119 | 119 | pTransformed = (float*) mallocHelper.alloc(transformedIndexCount * 4 * sizeof(float)); |
120 | - | |
120 | + | |
121 | 121 | if (pTransformed == 0 ) { |
122 | 122 | return -2; |
123 | 123 | } |
124 | - | |
124 | + | |
125 | 125 | // Transform the vertices |
126 | 126 | { |
127 | 127 | const float* pSrc = pPositions + 3 * minIndex; |
@@ -130,9 +130,9 @@ int visibilityTest(float* pWS, float* pPositions, int positionsLength, | ||
130 | 130 | mx4transform(pSrc[0], pSrc[1], pSrc[2], 1.0f, pWS, pDst); |
131 | 131 | } |
132 | 132 | } |
133 | - | |
133 | + | |
134 | 134 | // Clip the triangles |
135 | - | |
135 | + | |
136 | 136 | Poly poly; |
137 | 137 | float* pDest = & poly.vert[0].sx; |
138 | 138 | for (int i = 0; i < indexCount; i += 3) { |
@@ -166,14 +166,14 @@ public: | ||
166 | 166 | mEnv->ReleasePrimitiveArrayCritical(mRef, mBase, mReleaseParam); |
167 | 167 | } |
168 | 168 | } |
169 | - | |
169 | + | |
170 | 170 | // We seperate the bounds check from the initialization because we want to |
171 | 171 | // be able to bounds-check multiple arrays, and we can't throw an exception |
172 | 172 | // after we've called GetPrimitiveArrayCritical. |
173 | - | |
173 | + | |
174 | 174 | // Return true if the bounds check succeeded |
175 | 175 | // Else instruct the runtime to throw an exception |
176 | - | |
176 | + | |
177 | 177 | bool check() { |
178 | 178 | if ( ! mRef) { |
179 | 179 | mEnv->ThrowNew(gIAEClass, "array == null"); |
@@ -190,9 +190,9 @@ public: | ||
190 | 190 | } |
191 | 191 | return true; |
192 | 192 | } |
193 | - | |
193 | + | |
194 | 194 | // Bind the array. |
195 | - | |
195 | + | |
196 | 196 | void bind() { |
197 | 197 | mBase = (T*) mEnv->GetPrimitiveArrayCritical(mRef, (jboolean *) 0); |
198 | 198 | mData = mBase + mOffset; |
@@ -201,10 +201,10 @@ public: | ||
201 | 201 | void commitChanges() { |
202 | 202 | mReleaseParam = 0; |
203 | 203 | } |
204 | - | |
204 | + | |
205 | 205 | T* mData; |
206 | 206 | int mLength; |
207 | - | |
207 | + | |
208 | 208 | private: |
209 | 209 | T* mBase; |
210 | 210 | JNIEnv* mEnv; |
@@ -226,29 +226,29 @@ inline float distance2(float x, float y, float z) { | ||
226 | 226 | inline float distance(float x, float y, float z) { |
227 | 227 | return sqrtf(distance2(x, y, z)); |
228 | 228 | } |
229 | - | |
229 | + | |
230 | 230 | static |
231 | 231 | void util_computeBoundingSphere(JNIEnv *env, jclass clazz, |
232 | 232 | jfloatArray positions_ref, jint positionsOffset, jint positionsCount, |
233 | 233 | jfloatArray sphere_ref, jint sphereOffset) { |
234 | 234 | FloatArrayHelper positions(env, positions_ref, positionsOffset, 0); |
235 | 235 | FloatArrayHelper sphere(env, sphere_ref, sphereOffset, 4); |
236 | - | |
236 | + | |
237 | 237 | bool checkOK = positions.check() && sphere.check(); |
238 | 238 | if (! checkOK) { |
239 | 239 | return; |
240 | 240 | } |
241 | - | |
241 | + | |
242 | 242 | positions.bind(); |
243 | 243 | sphere.bind(); |
244 | - | |
244 | + | |
245 | 245 | if ( positionsCount < 1 ) { |
246 | 246 | env->ThrowNew(gIAEClass, "positionsCount < 1"); |
247 | 247 | return; |
248 | 248 | } |
249 | - | |
249 | + | |
250 | 250 | const float* pSrc = positions.mData; |
251 | - | |
251 | + | |
252 | 252 | // find bounding box |
253 | 253 | float x0 = *pSrc++; |
254 | 254 | float x1 = x0; |
@@ -256,7 +256,7 @@ void util_computeBoundingSphere(JNIEnv *env, jclass clazz, | ||
256 | 256 | float y1 = y0; |
257 | 257 | float z0 = *pSrc++; |
258 | 258 | float z1 = z0; |
259 | - | |
259 | + | |
260 | 260 | for(int i = 1; i < positionsCount; i++) { |
261 | 261 | { |
262 | 262 | float x = *pSrc++; |
@@ -286,7 +286,7 @@ void util_computeBoundingSphere(JNIEnv *env, jclass clazz, | ||
286 | 286 | } |
287 | 287 | } |
288 | 288 | } |
289 | - | |
289 | + | |
290 | 290 | // Because we know our input meshes fit pretty well into bounding boxes, |
291 | 291 | // just take the diagonal of the box as defining our sphere. |
292 | 292 | float* pSphere = sphere.mData; |
@@ -297,7 +297,7 @@ void util_computeBoundingSphere(JNIEnv *env, jclass clazz, | ||
297 | 297 | *pSphere++ = y0 + dy * 0.5f; |
298 | 298 | *pSphere++ = z0 + dz * 0.5f; |
299 | 299 | *pSphere++ = distance(dx, dy, dz) * 0.5f; |
300 | - | |
300 | + | |
301 | 301 | sphere.commitChanges(); |
302 | 302 | } |
303 | 303 |
@@ -344,7 +344,7 @@ static void computeFrustum(const float* m, float* f) { | ||
344 | 344 | normalizePlane(f); |
345 | 345 | f+= 4; |
346 | 346 | |
347 | - // left | |
347 | + // left | |
348 | 348 | f[0] = m3 + m[0]; |
349 | 349 | f[1] = m7 + m[4]; |
350 | 350 | f[2] = m11 + m[8]; |
@@ -396,20 +396,20 @@ int util_frustumCullSpheres(JNIEnv *env, jclass clazz, | ||
396 | 396 | FloatArrayHelper mvp(env, mvp_ref, mvpOffset, 16); |
397 | 397 | FloatArrayHelper spheres(env, spheres_ref, spheresOffset, spheresCount * 4); |
398 | 398 | IntArrayHelper results(env, results_ref, resultsOffset, resultsCapacity); |
399 | - | |
399 | + | |
400 | 400 | bool initializedOK = mvp.check() && spheres.check() && results.check(); |
401 | 401 | if (! initializedOK) { |
402 | 402 | return -1; |
403 | 403 | } |
404 | - | |
404 | + | |
405 | 405 | mvp.bind(); |
406 | 406 | spheres.bind(); |
407 | 407 | results.bind(); |
408 | - | |
408 | + | |
409 | 409 | computeFrustum(mvp.mData, frustum); |
410 | - | |
410 | + | |
411 | 411 | // Cull the spheres |
412 | - | |
412 | + | |
413 | 413 | pSphere = spheres.mData; |
414 | 414 | pResults = results.mData; |
415 | 415 | outputCount = 0; |
@@ -436,27 +436,27 @@ int util_visibilityTest(JNIEnv *env, jclass clazz, | ||
436 | 436 | jfloatArray ws_ref, jint wsOffset, |
437 | 437 | jfloatArray positions_ref, jint positionsOffset, |
438 | 438 | jcharArray indices_ref, jint indicesOffset, jint indexCount) { |
439 | - | |
439 | + | |
440 | 440 | FloatArrayHelper ws(env, ws_ref, wsOffset, 16); |
441 | 441 | FloatArrayHelper positions(env, positions_ref, positionsOffset, 0); |
442 | 442 | UnsignedShortArrayHelper indices(env, indices_ref, indicesOffset, 0); |
443 | - | |
443 | + | |
444 | 444 | bool checkOK = ws.check() && positions.check() && indices.check(); |
445 | 445 | if (! checkOK) { |
446 | 446 | // Return value will be ignored, because an exception has been thrown. |
447 | 447 | return -1; |
448 | 448 | } |
449 | - | |
449 | + | |
450 | 450 | if (indices.mLength < indexCount) { |
451 | 451 | env->ThrowNew(gIAEClass, "length < offset + indexCount"); |
452 | 452 | // Return value will be ignored, because an exception has been thrown. |
453 | 453 | return -1; |
454 | 454 | } |
455 | - | |
455 | + | |
456 | 456 | ws.bind(); |
457 | 457 | positions.bind(); |
458 | 458 | indices.bind(); |
459 | - | |
459 | + | |
460 | 460 | return visibilityTest(ws.mData, |
461 | 461 | positions.mData, positions.mLength, |
462 | 462 | indices.mData, indexCount); |
@@ -496,19 +496,19 @@ void util_multiplyMM(JNIEnv *env, jclass clazz, | ||
496 | 496 | FloatArrayHelper resultMat(env, result_ref, resultOffset, 16); |
497 | 497 | FloatArrayHelper lhs(env, lhs_ref, lhsOffset, 16); |
498 | 498 | FloatArrayHelper rhs(env, rhs_ref, rhsOffset, 16); |
499 | - | |
499 | + | |
500 | 500 | bool checkOK = resultMat.check() && lhs.check() && rhs.check(); |
501 | - | |
501 | + | |
502 | 502 | if ( !checkOK ) { |
503 | 503 | return; |
504 | 504 | } |
505 | - | |
505 | + | |
506 | 506 | resultMat.bind(); |
507 | 507 | lhs.bind(); |
508 | 508 | rhs.bind(); |
509 | - | |
509 | + | |
510 | 510 | multiplyMM(resultMat.mData, lhs.mData, rhs.mData); |
511 | - | |
511 | + | |
512 | 512 | resultMat.commitChanges(); |
513 | 513 | } |
514 | 514 |
@@ -527,19 +527,19 @@ void util_multiplyMV(JNIEnv *env, jclass clazz, | ||
527 | 527 | FloatArrayHelper resultV(env, result_ref, resultOffset, 4); |
528 | 528 | FloatArrayHelper lhs(env, lhs_ref, lhsOffset, 16); |
529 | 529 | FloatArrayHelper rhs(env, rhs_ref, rhsOffset, 4); |
530 | - | |
530 | + | |
531 | 531 | bool checkOK = resultV.check() && lhs.check() && rhs.check(); |
532 | - | |
532 | + | |
533 | 533 | if ( !checkOK ) { |
534 | 534 | return; |
535 | 535 | } |
536 | - | |
536 | + | |
537 | 537 | resultV.bind(); |
538 | 538 | lhs.bind(); |
539 | 539 | rhs.bind(); |
540 | - | |
540 | + | |
541 | 541 | multiplyMV(resultV.mData, lhs.mData, rhs.mData); |
542 | - | |
542 | + | |
543 | 543 | resultV.commitChanges(); |
544 | 544 | } |
545 | 545 |
@@ -550,7 +550,7 @@ static jfieldID nativeBitmapID = 0; | ||
550 | 550 | void nativeUtilsClassInit(JNIEnv *env, jclass clazz) |
551 | 551 | { |
552 | 552 | jclass bitmapClass = env->FindClass("android/graphics/Bitmap"); |
553 | - nativeBitmapID = env->GetFieldID(bitmapClass, "mNativeBitmap", "I"); | |
553 | + nativeBitmapID = env->GetFieldID(bitmapClass, "mNativeBitmap", "I"); | |
554 | 554 | } |
555 | 555 | |
556 | 556 | static int checkFormat(SkBitmap::Config config, int format, int type) |
@@ -653,7 +653,7 @@ static jint util_texImage2D(JNIEnv *env, jclass clazz, | ||
653 | 653 | } |
654 | 654 | int err = checkFormat(config, internalformat, type); |
655 | 655 | if (err) |
656 | - return err; | |
656 | + return err; | |
657 | 657 | bitmap.lockPixels(); |
658 | 658 | const int w = bitmap.width(); |
659 | 659 | const int h = bitmap.height(); |
@@ -665,14 +665,15 @@ static jint util_texImage2D(JNIEnv *env, jclass clazz, | ||
665 | 665 | } |
666 | 666 | const size_t size = bitmap.getSize(); |
667 | 667 | const size_t palette_size = 256*sizeof(SkPMColor); |
668 | - void* const data = malloc(size + palette_size); | |
668 | + const size_t imageSize = size + palette_size; | |
669 | + void* const data = malloc(imageSize); | |
669 | 670 | if (data) { |
670 | 671 | void* const pixels = (char*)data + palette_size; |
671 | 672 | SkColorTable* ctable = bitmap.getColorTable(); |
672 | 673 | memcpy(data, ctable->lockColors(), ctable->count() * sizeof(SkPMColor)); |
673 | 674 | memcpy(pixels, p, size); |
674 | 675 | ctable->unlockColors(false); |
675 | - glCompressedTexImage2D(target, level, internalformat, w, h, border, 0, data); | |
676 | + glCompressedTexImage2D(target, level, internalformat, w, h, border, imageSize, data); | |
676 | 677 | free(data); |
677 | 678 | } else { |
678 | 679 | err = -1; |
@@ -700,7 +701,7 @@ static jint util_texSubImage2D(JNIEnv *env, jclass clazz, | ||
700 | 701 | } |
701 | 702 | int err = checkFormat(config, format, type); |
702 | 703 | if (err) |
703 | - return err; | |
704 | + return err; | |
704 | 705 | bitmap.lockPixels(); |
705 | 706 | const int w = bitmap.width(); |
706 | 707 | const int h = bitmap.height(); |
@@ -723,22 +724,22 @@ lookupClasses(JNIEnv* env) { | ||
723 | 724 | } |
724 | 725 | |
725 | 726 | static JNINativeMethod gMatrixMethods[] = { |
726 | - { "multiplyMM", "([FI[FI[FI)V", (void*)util_multiplyMM }, | |
727 | - { "multiplyMV", "([FI[FI[FI)V", (void*)util_multiplyMV }, | |
727 | + { "multiplyMM", "([FI[FI[FI)V", (void*)util_multiplyMM }, | |
728 | + { "multiplyMV", "([FI[FI[FI)V", (void*)util_multiplyMV }, | |
728 | 729 | }; |
729 | 730 | |
730 | 731 | static JNINativeMethod gVisiblityMethods[] = { |
731 | - { "computeBoundingSphere", "([FII[FI)V", (void*)util_computeBoundingSphere }, | |
732 | + { "computeBoundingSphere", "([FII[FI)V", (void*)util_computeBoundingSphere }, | |
732 | 733 | { "frustumCullSpheres", "([FI[FII[III)I", (void*)util_frustumCullSpheres }, |
733 | - { "visibilityTest", "([FI[FI[CII)I", (void*)util_visibilityTest }, | |
734 | + { "visibilityTest", "([FI[FI[CII)I", (void*)util_visibilityTest }, | |
734 | 735 | }; |
735 | 736 | |
736 | 737 | static JNINativeMethod gUtilsMethods[] = { |
737 | 738 | {"nativeClassInit", "()V", (void*)nativeUtilsClassInit }, |
738 | - { "native_getInternalFormat", "(Landroid/graphics/Bitmap;)I", (void*) util_getInternalFormat }, | |
739 | - { "native_getType", "(Landroid/graphics/Bitmap;)I", (void*) util_getType }, | |
740 | - { "native_texImage2D", "(IIILandroid/graphics/Bitmap;II)I", (void*)util_texImage2D }, | |
741 | - { "native_texSubImage2D", "(IIIILandroid/graphics/Bitmap;II)I", (void*)util_texSubImage2D }, | |
739 | + { "native_getInternalFormat", "(Landroid/graphics/Bitmap;)I", (void*) util_getInternalFormat }, | |
740 | + { "native_getType", "(Landroid/graphics/Bitmap;)I", (void*) util_getType }, | |
741 | + { "native_texImage2D", "(IIILandroid/graphics/Bitmap;II)I", (void*)util_texImage2D }, | |
742 | + { "native_texSubImage2D", "(IIIILandroid/graphics/Bitmap;II)I", (void*)util_texSubImage2D }, | |
742 | 743 | }; |
743 | 744 | |
744 | 745 | typedef struct _ClassRegistrationInfo { |
@@ -112,7 +112,7 @@ void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2) | ||
112 | 112 | } |
113 | 113 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
114 | 114 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
115 | - mCameraJObjectWeak, msgType, ext1, ext2); | |
115 | + mCameraJObjectWeak, msgType, ext1, ext2, NULL); | |
116 | 116 | } |
117 | 117 | |
118 | 118 | void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType) |
@@ -32,6 +32,7 @@ | ||
32 | 32 | #include <sys/errno.h> |
33 | 33 | #include <sys/resource.h> |
34 | 34 | #include <sys/types.h> |
35 | +#include <cutils/sched_policy.h> | |
35 | 36 | #include <dirent.h> |
36 | 37 | #include <fcntl.h> |
37 | 38 | #include <grp.h> |
@@ -50,13 +51,7 @@ pid_t gettid() { return syscall(__NR_gettid);} | ||
50 | 51 | #undef __KERNEL__ |
51 | 52 | #endif |
52 | 53 | |
53 | -/* | |
54 | - * List of cgroup names which map to ANDROID_TGROUP_ values in Thread.h | |
55 | - * and Process.java | |
56 | - * These names are used to construct the path to the cgroup control dir | |
57 | - */ | |
58 | - | |
59 | -static const char *cgroup_names[] = { NULL, "bg_non_interactive", "fg_boost" }; | |
54 | +#define POLICY_DEBUG 0 | |
60 | 55 | |
61 | 56 | using namespace android; |
62 | 57 |
@@ -194,28 +189,6 @@ jint android_os_Process_getGidForName(JNIEnv* env, jobject clazz, jstring name) | ||
194 | 189 | return -1; |
195 | 190 | } |
196 | 191 | |
197 | -static int add_pid_to_cgroup(int pid, int grp) | |
198 | -{ | |
199 | - int fd; | |
200 | - char path[255]; | |
201 | - char text[64]; | |
202 | - | |
203 | - sprintf(path, "/dev/cpuctl/%s/tasks", | |
204 | - (cgroup_names[grp] ? cgroup_names[grp] : "")); | |
205 | - | |
206 | - if ((fd = open(path, O_WRONLY)) < 0) | |
207 | - return -1; | |
208 | - | |
209 | - sprintf(text, "%d", pid); | |
210 | - if (write(fd, text, strlen(text)) < 0) { | |
211 | - close(fd); | |
212 | - return -1; | |
213 | - } | |
214 | - | |
215 | - close(fd); | |
216 | - return 0; | |
217 | -} | |
218 | - | |
219 | 192 | void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint grp) |
220 | 193 | { |
221 | 194 | if (grp > ANDROID_TGROUP_MAX || grp < 0) { |
@@ -223,10 +196,9 @@ void android_os_Process_setThreadGroup(JNIEnv* env, jobject clazz, int pid, jint | ||
223 | 196 | return; |
224 | 197 | } |
225 | 198 | |
226 | - if (add_pid_to_cgroup(pid, grp)) { | |
227 | - // If the thread exited on us, don't generate an exception | |
228 | - if (errno != ESRCH && errno != ENOENT) | |
229 | - signalExceptionForGroupError(env, clazz, errno); | |
199 | + if (set_sched_policy(pid, (grp == ANDROID_TGROUP_BG_NONINTERACT) ? | |
200 | + SP_BACKGROUND : SP_FOREGROUND)) { | |
201 | + signalExceptionForGroupError(env, clazz, errno); | |
230 | 202 | } |
231 | 203 | } |
232 | 204 |
@@ -242,6 +214,26 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin | ||
242 | 214 | return; |
243 | 215 | } |
244 | 216 | |
217 | +#if POLICY_DEBUG | |
218 | + char cmdline[32]; | |
219 | + int fd; | |
220 | + | |
221 | + strcpy(cmdline, "unknown"); | |
222 | + | |
223 | + sprintf(proc_path, "/proc/%d/cmdline", pid); | |
224 | + fd = open(proc_path, O_RDONLY); | |
225 | + if (fd >= 0) { | |
226 | + int rc = read(fd, cmdline, sizeof(cmdline)-1); | |
227 | + cmdline[rc] = 0; | |
228 | + close(fd); | |
229 | + } | |
230 | + | |
231 | + if (grp == ANDROID_TGROUP_BG_NONINTERACT) { | |
232 | + LOGD("setProcessGroup: vvv pid %d (%s)", pid, cmdline); | |
233 | + } else { | |
234 | + LOGD("setProcessGroup: ^^^ pid %d (%s)", pid, cmdline); | |
235 | + } | |
236 | +#endif | |
245 | 237 | sprintf(proc_path, "/proc/%d/task", pid); |
246 | 238 | if (!(d = opendir(proc_path))) { |
247 | 239 | // If the process exited on us, don't generate an exception |
@@ -271,13 +263,10 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin | ||
271 | 263 | continue; |
272 | 264 | } |
273 | 265 | |
274 | - if (add_pid_to_cgroup(t_pid, grp)) { | |
275 | - // If the thread exited on us, ignore it and keep going | |
276 | - if (errno != ESRCH && errno != ENOENT) { | |
277 | - signalExceptionForGroupError(env, clazz, errno); | |
278 | - closedir(d); | |
279 | - return; | |
280 | - } | |
266 | + if (set_sched_policy(t_pid, (grp == ANDROID_TGROUP_BG_NONINTERACT) ? | |
267 | + SP_BACKGROUND : SP_FOREGROUND)) { | |
268 | + signalExceptionForGroupError(env, clazz, errno); | |
269 | + break; | |
281 | 270 | } |
282 | 271 | } |
283 | 272 | closedir(d); |
@@ -286,10 +275,17 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin | ||
286 | 275 | void android_os_Process_setThreadPriority(JNIEnv* env, jobject clazz, |
287 | 276 | jint pid, jint pri) |
288 | 277 | { |
278 | + int rc = 0; | |
279 | + | |
289 | 280 | if (pri >= ANDROID_PRIORITY_BACKGROUND) { |
290 | - add_pid_to_cgroup(pid, ANDROID_TGROUP_BG_NONINTERACT); | |
281 | + rc = set_sched_policy(pid, SP_BACKGROUND); | |
291 | 282 | } else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) { |
292 | - add_pid_to_cgroup(pid, ANDROID_TGROUP_DEFAULT); | |
283 | + rc = set_sched_policy(pid, SP_FOREGROUND); | |
284 | + } | |
285 | + | |
286 | + if (rc) { | |
287 | + signalExceptionForGroupError(env, clazz, errno); | |
288 | + return; | |
293 | 289 | } |
294 | 290 | |
295 | 291 | if (setpriority(PRIO_PROCESS, pid, pri) < 0) { |
@@ -18,6 +18,7 @@ | ||
18 | 18 | |
19 | 19 | #include <utils/Binder.h> |
20 | 20 | #include <utils/BpBinder.h> |
21 | +#include <cutils/sched_policy.h> | |
21 | 22 | #include <utils/Debug.h> |
22 | 23 | #include <utils/Log.h> |
23 | 24 | #include <utils/TextOutput.h> |
@@ -426,6 +427,21 @@ void IPCThreadState::joinThreadPool(bool isMain) | ||
426 | 427 | result = executeCommand(cmd); |
427 | 428 | } |
428 | 429 | |
430 | + // After executing the command, ensure that the thread is returned to the | |
431 | + // default cgroup and priority before rejoining the pool. This is a failsafe | |
432 | + // in case the command implementation failed to properly restore the thread's | |
433 | + // scheduling parameters upon completion. | |
434 | + int my_id; | |
435 | +#ifdef HAVE_GETTID | |
436 | + my_id = gettid(); | |
437 | +#else | |
438 | + my_id = getpid(); | |
439 | +#endif | |
440 | + if (!set_sched_policy(my_id, SP_FOREGROUND)) { | |
441 | + // success; reset the priority as well | |
442 | + setpriority(PRIO_PROCESS, my_id, ANDROID_PRIORITY_NORMAL); | |
443 | + } | |
444 | + | |
429 | 445 | // Let this thread exit the thread pool if it is no longer |
430 | 446 | // needed and it is not the main process thread. |
431 | 447 | if(result == TIMED_OUT && !isMain) { |