读书人

Android CRT Screen 电视机效果

发布时间: 2012-06-29 15:48:47 作者: rapoo

Android CRT Screen 电视效果

Android 2.3?对关屏进行了优化,增加了一种类似于关闭电视的效果?

Android系统默认使用的关屏效果是fade,要激活旧CRT效果,需要修改以下文件,关闭fade效果。

?

?

frameworks/base/core/res/res/values/config.xml

?

?

  1. ???<!--?If?this?is?true,?the?screen?will?fade?off.?-->????
  2. -????<bool?name="config_animateScreenLights">true</bool>????
  3. +????<bool?name="config_animateScreenLights">false</bool>??

?

?

为啥如此修改就可以激活旧CRT效果,让我们跟踪下代码,先贴上相关代码:

?

?

frameworks\base\services\java\com\Android\server\PowerManagerService.java

?

?

  1. ?475?????????????????mAnimationSetting?=?0;????
  2. ?476?????????????????if?(windowScale?>?0.5f)?{????
  3. ?477?????????????????????mAnimationSetting?|=?ANIM_SETTING_OFF;????
  4. ?478?????????????????}????
  5. ????
  6. ????
  7. 2284?????????public?void?run()?{????
  8. 2285?????????????if?(mAnimateScreenLights)?{????
  9. 2286?????????????????synchronized?(mLocks)?{????
  10. 2287?????????????????????long?now?=?SystemClock.uptimeMillis();????
  11. 2288?????????????????????boolean?more?=?mScreenBrightness.stepLocked();????
  12. 2289?????????????????????if?(more)?{????
  13. 2290?????????????????????????mScreenOffHandler.postAtTime(this,?now+(1000/60));????
  14. 2291?????????????????????}????
  15. 2292?????????????????}????
  16. 2293?????????????}?else?{????
  17. 2294?????????????????synchronized?(mLocks)?{????
  18. 2295?????????????????????//?we're?turning?off????
  19. 2296?????????????????????final?boolean?animate?=?animating?&&?targetValue?==?Power.BRIGHTNESS_OFF;????
  20. 2297?????????????????????if?(animate)?{????
  21. 2298?????????????????????????//?It's?pretty?scary?to?hold?mLocks?for?this?long,?and?we?should????
  22. 2299?????????????????????????//?redesign?this,?but?it?works?for?now.????
  23. 2300?????????????????????????nativeStartSurfaceFlingerAnimation(????
  24. 2301?????????????????????????????????mScreenOffReason?==?WindowManagerPolicy.OFF_BECAUSE_OF_PROX_SENSOR????
  25. 2302???????????????????????????????????0?:?mAnimationSetting);????
  26. 2303?????????????????????}????
  27. 2304?????????????????????mScreenBrightness.jumpToTargetLocked();????
  28. 2305?????????????????}????
  29. 2306?????????????}????
  30. 2307?????????}????
  31. 2308?????}??

?

frameworks/base/services/jni/com_Android_server_PowerManagerService.cpp

?

?

  1. <pre?class="cpp"?name="code">????131?static?void?Android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation(JNIEnv*?env,????
  2. ????132?????????jobject?obj,?jint?mode)?{????
  3. ????133?????sp<ISurfaceComposer>?s(ComposerService::getComposerService());????
  4. ????134?????s->turnElectronBeamOff(mode);????
  5. ????135?}??

?

?

?

frameworks/base/services/surfaceflinger/SurfaceFlinger.cpp

?

  1. 2385?status_t?SurfaceFlinger::turnElectronBeamOff(int32_t?mode)????
  2. 2386?{????
  3. 2387?????class?MessageTurnElectronBeamOff?:?public?MessageBase?{????
  4. 2388?????????SurfaceFlinger*?flinger;????
  5. 2389?????????int32_t?mode;????
  6. 2390?????????status_t?result;????
  7. 2391?????public:????
  8. 2392?????????MessageTurnElectronBeamOff(SurfaceFlinger*?flinger,?int32_t?mode)????
  9. 2393?????????????:?flinger(flinger),?mode(mode),?result(PERMISSION_DENIED)?{????
  10. 2394?????????}????
  11. 2395?????????status_t?getResult()?const?{????
  12. 2396?????????????return?result;????
  13. 2397?????????}????
  14. 2398?????????virtual?bool?handler()?{????
  15. 2399?????????????Mutex::Autolock?_l(flinger->mStateLock);????
  16. 2400?????????????result?=?flinger->turnElectronBeamOffImplLocked(mode);????
  17. 2401?????????????return?true;????
  18. 2402?????????}????
  19. 2403?????};????
  20. 2404????
  21. 2405?????sp<MessageBase>?msg?=?new?MessageTurnElectronBeamOff(this,?mode);????
  22. 2406?????status_t?res?=?postMessageSync(msg);????
  23. 2407?????if?(res?==?NO_ERROR)?{????
  24. 2408?????????res?=?static_cast<MessageTurnElectronBeamOff*>(?msg.get()?)->getResult();????
  25. 2409????
  26. 2410?????????//?work-around:?when?the?power-manager?calls?us?we?activate?the????
  27. 2411?????????//?animation.?eventually,?the?"on"?animation?will?be?called????
  28. 2412?????????//?by?the?power-manager?itself????
  29. 2413?????????mElectronBeamAnimationMode?=?mode;????
  30. 2414?????}????
  31. 2415?????return?res;????
  32. 2416?}????
  33. ????
  34. ????
  35. 2363?status_t?SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t?mode)????
  36. 2364?{????
  37. 2365?????DisplayHardware&?hw(graphicPlane(0).editDisplayHardware());????
  38. 2366?????if?(!hw.canDraw())?{????
  39. 2367?????????//?we're?already?off????
  40. 2368?????????return?NO_ERROR;????
  41. 2369?????}????
  42. 2370?????if?(mode?&?ISurfaceComposer::eElectronBeamAnimationOff)?{????
  43. 2371?????????electronBeamOffAnimationImplLocked();????
  44. 2372?????}????
  45. 2373????
  46. 2374?????//?always?clear?the?whole?screen?at?the?end?of?the?animation????
  47. 2375?????glClearColor(0,0,0,1);????
  48. 2376?????glDisable(GL_SCISSOR_TEST);????
  49. 2377?????glClear(GL_COLOR_BUFFER_BIT);????
  50. 2378?????glEnable(GL_SCISSOR_TEST);????
  51. 2379?????hw.flip(?Region(hw.bounds())?);????
  52. 2380????
  53. 2381?????hw.setCanDraw(false);????
  54. 2382?????return?NO_ERROR;????
  55. 2383?}????
  56. ????
  57. 2073?status_t?SurfaceFlinger::electronBeamOffAnimationImplLocked()????
  58. 2074?{????
  59. 2075?????status_t?result?=?PERMISSION_DENIED;????
  60. 2076????
  61. 2077?????if?(!GLExtensions::getInstance().haveFramebufferObject())????
  62. 2078?????????return?INVALID_OPERATION;????
  63. 2079????
  64. 2080?????//?get?screen?geometry????
  65. 2081?????const?DisplayHardware&?hw(graphicPlane(0).displayHardware());????
  66. 2082?????const?uint32_t?hw_w?=?hw.getWidth();????
  67. 2083?????const?uint32_t?hw_h?=?hw.getHeight();????
  68. 2084?????const?Region?screenBounds(hw.bounds());????
  69. 2085????
  70. 2086?????GLfloat?u,?v;????
  71. 2087?????GLuint?tname;????
  72. 2088?????result?=?renderScreenToTextureLocked(0,?&tname,?&u,?&v);????
  73. 2089?????if?(result?!=?NO_ERROR)?{????
  74. 2090?????????return?result;????
  75. 2091?????}????
  76. 2092????
  77. 2093?????GLfloat?vtx[8];????
  78. 2094?????const?GLfloat?texCoords[4][2]?=?{?{0,v},?{0,0},?{u,0},?{u,v}?};????
  79. 2095?????glEnable(GL_TEXTURE_2D);????
  80. 2096?????glBindTexture(GL_TEXTURE_2D,?tname);????
  81. 2097?????glTexEnvx(GL_TEXTURE_ENV,?GL_TEXTURE_ENV_MODE,?GL_REPLACE);????
  82. 2098?????glTexParameterx(GL_TEXTURE_2D,?GL_TEXTURE_MAG_FILTER,?GL_LINEAR);????
  83. 2099?????glTexParameterx(GL_TEXTURE_2D,?GL_TEXTURE_MIN_FILTER,?GL_LINEAR);????
  84. 2100?????glTexCoordPointer(2,?GL_FLOAT,?0,?texCoords);????
  85. 2101?????glEnableClientState(GL_TEXTURE_COORD_ARRAY);????
  86. 2102?????glVertexPointer(2,?GL_FLOAT,?0,?vtx);????
  87. 2103????
  88. 2104?????class?s_curve_interpolator?{????
  89. 2105?????????const?float?nbFrames,?s,?v;????
  90. 2106?????public:????
  91. 2107?????????s_curve_interpolator(int?nbFrames,?float?s)????
  92. 2108?????????:?nbFrames(1.0f?/?(nbFrames-1)),?s(s),????
  93. 2109???????????v(1.0f?+?expf(-s?+?0.5f*s))?{????
  94. 2110?????????}????
  95. 2111?????????float?operator()(int?f)?{????
  96. 2112?????????????const?float?x?=?f?*?nbFrames;????
  97. 2113?????????????return?((1.0f/(1.0f?+?expf(-x*s?+?0.5f*s)))?-?0.5f)?*?v?+?0.5f;????
  98. 2114?????????}????
  99. 2115?????};????
  100. 2116????
  101. 2117?????class?v_stretch?{????
  102. 2118?????????const?GLfloat?hw_w,?hw_h;????
  103. 2119?????public:????
  104. 2120?????????v_stretch(uint32_t?hw_w,?uint32_t?hw_h)????
  105. 2121?????????:?hw_w(hw_w),?hw_h(hw_h)?{????
  106. 2122?????????}????
  107. 2123?????????void?operator()(GLfloat*?vtx,?float?v)?{????
  108. 2124?????????????const?GLfloat?w?=?hw_w?+?(hw_w?*?v);????
  109. 2125?????????????const?GLfloat?h?=?hw_h?-?(hw_h?*?v);????
  110. 2126?????????????const?GLfloat?x?=?(hw_w?-?w)?*?0.5f;????
  111. 2127?????????????const?GLfloat?y?=?(hw_h?-?h)?*?0.5f;????
  112. 2128?????????????vtx[0]?=?x;?????????vtx[1]?=?y;????
  113. 2129?????????????vtx[2]?=?x;?????????vtx[3]?=?y?+?h;????
  114. 2130?????????????vtx[4]?=?x?+?w;?????vtx[5]?=?y?+?h;????
  115. 2131?????????????vtx[6]?=?x?+?w;?????vtx[7]?=?y;????
  116. 2132?????????}????
  117. 2133?????};????
  118. 2134????
  119. 2135?????class?h_stretch?{????
  120. 2136?????????const?GLfloat?hw_w,?hw_h;????
  121. 2137?????public:????
  122. 2138?????????h_stretch(uint32_t?hw_w,?uint32_t?hw_h)????
  123. 2139?????????:?hw_w(hw_w),?hw_h(hw_h)?{????
  124. 2140?????????}????
  125. 2141?????????void?operator()(GLfloat*?vtx,?float?v)?{????
  126. 2142?????????????const?GLfloat?w?=?hw_w?-?(hw_w?*?v);????
  127. 2143?????????????const?GLfloat?h?=?1.0f;????
  128. 2144?????????????const?GLfloat?x?=?(hw_w?-?w)?*?0.5f;????
  129. 2145?????????????const?GLfloat?y?=?(hw_h?-?h)?*?0.5f;????
  130. 2146?????????????vtx[0]?=?x;?????????vtx[1]?=?y;????
  131. 2147?????????????vtx[2]?=?x;?????????vtx[3]?=?y?+?h;????
  132. 2148?????????????vtx[4]?=?x?+?w;?????vtx[5]?=?y?+?h;????
  133. 2149?????????????vtx[6]?=?x?+?w;?????vtx[7]?=?y;????
  134. 2150?????????}????
  135. 2151?????};????
  136. 2152????
  137. 2153?????//?the?full?animation?is?24?frames????
  138. 2154?????const?int?nbFrames?=?12;????
  139. 2155?????s_curve_interpolator?itr(nbFrames,?7.5f);????
  140. 2156?????s_curve_interpolator?itg(nbFrames,?8.0f);????
  141. 2157?????s_curve_interpolator?itb(nbFrames,?8.5f);????
  142. 2158????
  143. 2159?????v_stretch?vverts(hw_w,?hw_h);????
  144. 2160?????glEnable(GL_BLEND);????
  145. 2161?????glBlendFunc(GL_ONE,?GL_ONE);????
  146. 2162?????for?(int?i=0?;?i<nbFrames?;?i++)?{????
  147. 2163?????????float?x,?y,?w,?h;????
  148. 2164?????????const?float?vr?=?itr(i);????
  149. 2165?????????const?float?vg?=?itg(i);????
  150. 2166?????????const?float?vb?=?itb(i);????
  151. 2167????
  152. 2168?????????//?clear?screen????
  153. 2169?????????glColorMask(1,1,1,1);????
  154. 2170?????????glClear(GL_COLOR_BUFFER_BIT);????
  155. 2171?????????glEnable(GL_TEXTURE_2D);????
  156. 2172????
  157. 2173?????????//?draw?the?red?plane????
  158. 2174?????????vverts(vtx,?vr);????
  159. 2175?????????glColorMask(1,0,0,1);????
  160. 2176?????????glDrawArrays(GL_TRIANGLE_FAN,?0,?4);????
  161. 2177????
  162. 2178?????????//?draw?the?green?plane????
  163. 2179?????????vverts(vtx,?vg);????
  164. 2180?????????glColorMask(0,1,0,1);????
  165. 2181?????????glDrawArrays(GL_TRIANGLE_FAN,?0,?4);????
  166. 2182????
  167. 2183?????????//?draw?the?blue?plane????
  168. 2184?????????vverts(vtx,?vb);????
  169. 2185?????????glColorMask(0,0,1,1);????
  170. 2186?????????glDrawArrays(GL_TRIANGLE_FAN,?0,?4);????
  171. 2187????
  172. 2188?????????//?draw?the?white?highlight?(we?use?the?last?vertices)????
  173. 2189?????????glDisable(GL_TEXTURE_2D);????
  174. 2190?????????glColorMask(1,1,1,1);????
  175. 2191?????????glColor4f(vg,?vg,?vg,?1);????
  176. 2192?????????glDrawArrays(GL_TRIANGLE_FAN,?0,?4);????
  177. 2193?????????hw.flip(screenBounds);????
  178. 2194?????}????
  179. 2195????
  180. 2196?????h_stretch?hverts(hw_w,?hw_h);????
  181. 2197?????glDisable(GL_BLEND);????
  182. 2198?????glDisable(GL_TEXTURE_2D);????
  183. 2199?????glColorMask(1,1,1,1);????
  184. 2200?????for?(int?i=0?;?i<nbFrames?;?i++)?{????
  185. 2201?????????const?float?v?=?itg(i);????
  186. 2202?????????hverts(vtx,?v);????
  187. 2203?????????glClear(GL_COLOR_BUFFER_BIT);????
  188. 2204?????????glColor4f(1-v,?1-v,?1-v,?1);????
  189. 2205?????????glDrawArrays(GL_TRIANGLE_FAN,?0,?4);????
  190. 2206?????????hw.flip(screenBounds);????
  191. 2207?????}????
  192. 2208????
  193. 2209?????glColorMask(1,1,1,1);????
  194. 2210?????glEnable(GL_SCISSOR_TEST);????
  195. 2211?????glDisableClientState(GL_TEXTURE_COORD_ARRAY);????
  196. 2212?????glDeleteTextures(1,?&tname);????
  197. 2213?????return?NO_ERROR;????
  198. 2214?}????
  199. 2215??

?

?

?

OK,看完代码回来,首先是PowerManagerService.java中

mAnimationSetting如果标记为ANIM_SETTING_OFF,则打开旧CRT动画。

?

下面关屏动作run()中,因为我们将config_animateScreenLights置为false,因此mAnimateScreenLights为fasle

分支进入else,执行nativeStartSurfaceFlingerAnimation()函数。

?

nativeStartSurfaceFlingerAnimation()函数是一个JNI调用,在com_Android_server_PowerManagerService.cpp文件中,

对应surfaceflinger的s->turnElectronBeamOff(mode)函数。

?

好的,现在跳入SurfaceFlinger.cpp函数,具体调用顺序是:

turnElectronBeamOff()

|

turnElectronBeamOffImplLocked()

|

electronBeamOffAnimationImplLocked()

?

electronBeamOffAnimationImplLocked()函数将调用openGL绘值旧CRT关屏效果,大概有24帧。

?

当然,该函数有许多限制,不符合就会半途退出,你就看不到动画效果啦。

读书人网 >Android

热点推荐