| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.policy; |
| 18 |
|
| 19 |
import android.graphics.Insets; |
| 20 |
import android.graphics.RecordingCanvas; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.graphics.RenderNode; |
| 23 |
import android.graphics.drawable.ColorDrawable; |
| 24 |
import android.graphics.drawable.Drawable; |
| 25 |
import android.os.Looper; |
| 26 |
import android.view.Choreographer; |
| 27 |
import android.view.ThreadedRenderer; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class BackdropFrameRenderer extends Thread implements Choreographer.FrameCallback { |
| 38 |
|
| 39 |
private DecorView mDecorView; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private final Rect mTargetRect = new Rect(); |
| 44 |
|
| 45 |
|
| 46 |
private ThreadedRenderer mRenderer; |
| 47 |
private RenderNode mFrameAndBackdropNode; |
| 48 |
private RenderNode mSystemBarBackgroundNode; |
| 49 |
|
| 50 |
private final Rect mOldTargetRect = new Rect(); |
| 51 |
private final Rect mNewTargetRect = new Rect(); |
| 52 |
|
| 53 |
private Choreographer mChoreographer; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private int mLastContentWidth; |
| 58 |
private int mLastContentHeight; |
| 59 |
private int mLastCaptionHeight; |
| 60 |
private int mLastXOffset; |
| 61 |
private int mLastYOffset; |
| 62 |
|
| 63 |
|
| 64 |
private boolean mReportNextDraw; |
| 65 |
|
| 66 |
private Drawable mCaptionBackgroundDrawable; |
| 67 |
private Drawable mUserCaptionBackgroundDrawable; |
| 68 |
private Drawable mResizingBackgroundDrawable; |
| 69 |
private ColorDrawable mStatusBarColor; |
| 70 |
private ColorDrawable mNavigationBarColor; |
| 71 |
private boolean mOldFullscreen; |
| 72 |
private boolean mFullscreen; |
| 73 |
private final Rect mOldSystemBarInsets = new Rect(); |
| 74 |
private final Rect mSystemBarInsets = new Rect(); |
| 75 |
private final Rect mTmpRect = new Rect(); |
| 76 |
|
| 77 |
public BackdropFrameRenderer(DecorView decorView, ThreadedRenderer renderer, Rect initialBounds, |
| 78 |
Drawable resizingBackgroundDrawable, Drawable captionBackgroundDrawable, |
| 79 |
Drawable userCaptionBackgroundDrawable, int statusBarColor, int navigationBarColor, |
| 80 |
boolean fullscreen, Insets systemBarInsets) { |
| 81 |
setName("ResizeFrame"); |
| 82 |
|
| 83 |
mRenderer = renderer; |
| 84 |
onResourcesLoaded(decorView, resizingBackgroundDrawable, captionBackgroundDrawable, |
| 85 |
userCaptionBackgroundDrawable, statusBarColor, navigationBarColor); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
mFrameAndBackdropNode = RenderNode.create("FrameAndBackdropNode", null); |
| 90 |
|
| 91 |
mRenderer.addRenderNode(mFrameAndBackdropNode, true); |
| 92 |
|
| 93 |
|
| 94 |
mTargetRect.set(initialBounds); |
| 95 |
mFullscreen = fullscreen; |
| 96 |
mOldFullscreen = fullscreen; |
| 97 |
mSystemBarInsets.set(systemBarInsets.toRect()); |
| 98 |
mOldSystemBarInsets.set(systemBarInsets.toRect()); |
| 99 |
|
| 100 |
|
| 101 |
start(); |
| 102 |
} |
| 103 |
|
| 104 |
void onResourcesLoaded(DecorView decorView, Drawable resizingBackgroundDrawable, |
| 105 |
Drawable captionBackgroundDrawableDrawable, Drawable userCaptionBackgroundDrawable, |
| 106 |
int statusBarColor, int navigationBarColor) { |
| 107 |
synchronized (this) { |
| 108 |
mDecorView = decorView; |
| 109 |
mResizingBackgroundDrawable = resizingBackgroundDrawable != null |
| 110 |
&& resizingBackgroundDrawable.getConstantState() != null |
| 111 |
? resizingBackgroundDrawable.getConstantState().newDrawable() |
| 112 |
: null; |
| 113 |
mCaptionBackgroundDrawable = captionBackgroundDrawableDrawable != null |
| 114 |
&& captionBackgroundDrawableDrawable.getConstantState() != null |
| 115 |
? captionBackgroundDrawableDrawable.getConstantState().newDrawable() |
| 116 |
: null; |
| 117 |
mUserCaptionBackgroundDrawable = userCaptionBackgroundDrawable != null |
| 118 |
&& userCaptionBackgroundDrawable.getConstantState() != null |
| 119 |
? userCaptionBackgroundDrawable.getConstantState().newDrawable() |
| 120 |
: null; |
| 121 |
if (mCaptionBackgroundDrawable == null) { |
| 122 |
mCaptionBackgroundDrawable = mResizingBackgroundDrawable; |
| 123 |
} |
| 124 |
if (statusBarColor != 0) { |
| 125 |
mStatusBarColor = new ColorDrawable(statusBarColor); |
| 126 |
addSystemBarNodeIfNeeded(); |
| 127 |
} else { |
| 128 |
mStatusBarColor = null; |
| 129 |
} |
| 130 |
if (navigationBarColor != 0) { |
| 131 |
mNavigationBarColor = new ColorDrawable(navigationBarColor); |
| 132 |
addSystemBarNodeIfNeeded(); |
| 133 |
} else { |
| 134 |
mNavigationBarColor = null; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private void addSystemBarNodeIfNeeded() { |
| 140 |
if (mSystemBarBackgroundNode != null) { |
| 141 |
return; |
| 142 |
} |
| 143 |
mSystemBarBackgroundNode = RenderNode.create("SystemBarBackgroundNode", null); |
| 144 |
mRenderer.addRenderNode(mSystemBarBackgroundNode, false); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setTargetRect(Rect newTargetBounds, boolean fullscreen, Rect systemBarInsets) { |
| 157 |
synchronized (this) { |
| 158 |
mFullscreen = fullscreen; |
| 159 |
mTargetRect.set(newTargetBounds); |
| 160 |
mSystemBarInsets.set(systemBarInsets); |
| 161 |
|
| 162 |
pingRenderLocked(false ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public void onConfigurationChange() { |
| 170 |
synchronized (this) { |
| 171 |
if (mRenderer != null) { |
| 172 |
|
| 173 |
mOldTargetRect.set(0, 0, 0, 0); |
| 174 |
pingRenderLocked(false ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void releaseRenderer() { |
| 184 |
synchronized (this) { |
| 185 |
if (mRenderer != null) { |
| 186 |
|
| 187 |
mRenderer.setContentDrawBounds(0, 0, 0, 0); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
mRenderer.removeRenderNode(mFrameAndBackdropNode); |
| 192 |
if (mSystemBarBackgroundNode != null) { |
| 193 |
mRenderer.removeRenderNode(mSystemBarBackgroundNode); |
| 194 |
} |
| 195 |
|
| 196 |
mRenderer = null; |
| 197 |
|
| 198 |
|
| 199 |
pingRenderLocked(false ); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
public void run() { |
| 206 |
try { |
| 207 |
Looper.prepare(); |
| 208 |
synchronized (this) { |
| 209 |
mChoreographer = Choreographer.getInstance(); |
| 210 |
} |
| 211 |
Looper.loop(); |
| 212 |
} finally { |
| 213 |
releaseRenderer(); |
| 214 |
} |
| 215 |
synchronized (this) { |
| 216 |
|
| 217 |
mChoreographer = null; |
| 218 |
Choreographer.releaseInstance(); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
@Override |
| 228 |
public void doFrame(long frameTimeNanos) { |
| 229 |
synchronized (this) { |
| 230 |
if (mRenderer == null) { |
| 231 |
reportDrawIfNeeded(); |
| 232 |
|
| 233 |
Looper.myLooper().quit(); |
| 234 |
return; |
| 235 |
} |
| 236 |
doFrameUncheckedLocked(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
private void doFrameUncheckedLocked() { |
| 241 |
mNewTargetRect.set(mTargetRect); |
| 242 |
if (!mNewTargetRect.equals(mOldTargetRect) |
| 243 |
|| mOldFullscreen != mFullscreen |
| 244 |
|| !mSystemBarInsets.equals(mOldSystemBarInsets) |
| 245 |
|| mReportNextDraw) { |
| 246 |
mOldFullscreen = mFullscreen; |
| 247 |
mOldTargetRect.set(mNewTargetRect); |
| 248 |
mOldSystemBarInsets.set(mSystemBarInsets); |
| 249 |
redrawLocked(mNewTargetRect, mFullscreen); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
boolean onContentDrawn(int xOffset, int yOffset, int xSize, int ySize) { |
| 264 |
synchronized (this) { |
| 265 |
final boolean firstCall = mLastContentWidth == 0; |
| 266 |
|
| 267 |
mLastContentWidth = xSize; |
| 268 |
mLastContentHeight = ySize - mLastCaptionHeight; |
| 269 |
mLastXOffset = xOffset; |
| 270 |
mLastYOffset = yOffset; |
| 271 |
|
| 272 |
|
| 273 |
mRenderer.setContentDrawBounds( |
| 274 |
mLastXOffset, |
| 275 |
mLastYOffset, |
| 276 |
mLastXOffset + mLastContentWidth, |
| 277 |
mLastYOffset + mLastCaptionHeight + mLastContentHeight); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
return firstCall |
| 282 |
&& (mLastCaptionHeight != 0 || !mDecorView.isShowingCaption()); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void onRequestDraw(boolean reportNextDraw) { |
| 287 |
synchronized (this) { |
| 288 |
mReportNextDraw = reportNextDraw; |
| 289 |
mOldTargetRect.set(0, 0, 0, 0); |
| 290 |
pingRenderLocked(true ); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
private void redrawLocked(Rect newBounds, boolean fullscreen) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
final int captionHeight = mDecorView.getCaptionHeight(); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
if (captionHeight != 0) { |
| 310 |
|
| 311 |
mLastCaptionHeight = captionHeight; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
if ((mLastCaptionHeight == 0 && mDecorView.isShowingCaption()) || |
| 317 |
mLastContentWidth == 0 || mLastContentHeight == 0) { |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
final int left = mLastXOffset + newBounds.left; |
| 324 |
final int top = mLastYOffset + newBounds.top; |
| 325 |
final int width = newBounds.width(); |
| 326 |
final int height = newBounds.height(); |
| 327 |
|
| 328 |
mFrameAndBackdropNode.setLeftTopRightBottom(left, top, left + width, top + height); |
| 329 |
|
| 330 |
|
| 331 |
RecordingCanvas canvas = mFrameAndBackdropNode.beginRecording(width, height); |
| 332 |
final Drawable drawable = mUserCaptionBackgroundDrawable != null |
| 333 |
? mUserCaptionBackgroundDrawable : mCaptionBackgroundDrawable; |
| 334 |
|
| 335 |
if (drawable != null) { |
| 336 |
drawable.setBounds(0, 0, left + width, top + mLastCaptionHeight); |
| 337 |
drawable.draw(canvas); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
if (mResizingBackgroundDrawable != null) { |
| 342 |
mResizingBackgroundDrawable.setBounds(0, mLastCaptionHeight, left + width, top + height); |
| 343 |
mResizingBackgroundDrawable.draw(canvas); |
| 344 |
} |
| 345 |
mFrameAndBackdropNode.endRecording(); |
| 346 |
|
| 347 |
drawColorViews(left, top, width, height, fullscreen); |
| 348 |
|
| 349 |
|
| 350 |
mRenderer.drawRenderNode(mFrameAndBackdropNode); |
| 351 |
|
| 352 |
reportDrawIfNeeded(); |
| 353 |
} |
| 354 |
|
| 355 |
private void drawColorViews(int left, int top, int width, int height, boolean fullscreen) { |
| 356 |
if (mSystemBarBackgroundNode == null) { |
| 357 |
return; |
| 358 |
} |
| 359 |
RecordingCanvas canvas = mSystemBarBackgroundNode.beginRecording(width, height); |
| 360 |
mSystemBarBackgroundNode.setLeftTopRightBottom(left, top, left + width, top + height); |
| 361 |
final int topInset = mSystemBarInsets.top; |
| 362 |
if (mStatusBarColor != null) { |
| 363 |
mStatusBarColor.setBounds(0, 0, left + width, topInset); |
| 364 |
mStatusBarColor.draw(canvas); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
if (mNavigationBarColor != null && fullscreen) { |
| 371 |
DecorView.getNavigationBarRect(width, height, mSystemBarInsets, mTmpRect, 1f); |
| 372 |
mNavigationBarColor.setBounds(mTmpRect); |
| 373 |
mNavigationBarColor.draw(canvas); |
| 374 |
} |
| 375 |
mSystemBarBackgroundNode.endRecording(); |
| 376 |
mRenderer.drawRenderNode(mSystemBarBackgroundNode); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
private void reportDrawIfNeeded() { |
| 381 |
if (mReportNextDraw) { |
| 382 |
if (mDecorView.isAttachedToWindow()) { |
| 383 |
mDecorView.getViewRootImpl().reportDrawFinish(); |
| 384 |
} |
| 385 |
mReportNextDraw = false; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
private void pingRenderLocked(boolean drawImmediate) { |
| 397 |
if (mChoreographer != null && !drawImmediate) { |
| 398 |
mChoreographer.postFrameCallback(this); |
| 399 |
} else { |
| 400 |
doFrameUncheckedLocked(); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
void setUserCaptionBackgroundDrawable(Drawable userCaptionBackgroundDrawable) { |
| 405 |
synchronized (this) { |
| 406 |
mUserCaptionBackgroundDrawable = userCaptionBackgroundDrawable; |
| 407 |
} |
| 408 |
} |
| 409 |
} |