Skip to content

Commit bbf1bc8

Browse files
Romain GuyAndroid (Google) Code Review
authored andcommitted
Merge "Add optional metadata to initiliaze the render threat."
2 parents 6baed6c + 211370f commit bbf1bc8

File tree

4 files changed

+80
-46
lines changed

4 files changed

+80
-46
lines changed

core/java/android/view/ViewRootImpl.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.content.ComponentCallbacks;
2424
import android.content.ComponentCallbacks2;
2525
import android.content.Context;
26+
import android.content.pm.ApplicationInfo;
2627
import android.content.pm.PackageManager;
2728
import android.content.res.CompatibilityInfo;
2829
import android.content.res.Configuration;
@@ -82,7 +83,6 @@
8283

8384
import java.io.IOException;
8485
import java.io.OutputStream;
85-
import java.io.PrintWriter;
8686
import java.lang.ref.WeakReference;
8787
import java.util.ArrayList;
8888
import java.util.List;
@@ -140,6 +140,10 @@ public final class ViewRootImpl extends Handler implements ViewParent,
140140
static final ArrayList<ComponentCallbacks> sConfigCallbacks
141141
= new ArrayList<ComponentCallbacks>();
142142

143+
private static boolean sUseRenderThread = false;
144+
private static boolean sRenderThreadQueried = false;
145+
private static final Object[] sRenderThreadQueryLock = new Object[0];
146+
143147
long mLastTrackballTime = 0;
144148
final TrackballAxis mTrackballAxisX = new TrackballAxis();
145149
final TrackballAxis mTrackballAxisY = new TrackballAxis();
@@ -381,6 +385,31 @@ public ViewRootImpl(Context context) {
381385
mChoreographer = Choreographer.getInstance();
382386
}
383387

388+
/**
389+
* @return True if the application requests the use of a separate render thread,
390+
* false otherwise
391+
*/
392+
private static boolean isRenderThreadRequested(Context context) {
393+
synchronized (sRenderThreadQueryLock) {
394+
if (!sRenderThreadQueried) {
395+
final PackageManager packageManager = context.getPackageManager();
396+
final String packageName = context.getApplicationInfo().packageName;
397+
try {
398+
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName,
399+
PackageManager.GET_META_DATA);
400+
if (applicationInfo.metaData != null) {
401+
sUseRenderThread = applicationInfo.metaData.getBoolean(
402+
"android.graphics.renderThread", false);
403+
}
404+
} catch (PackageManager.NameNotFoundException e) {
405+
} finally {
406+
sRenderThreadQueried = true;
407+
}
408+
}
409+
return sUseRenderThread;
410+
}
411+
}
412+
384413
public static void addFirstDrawHandler(Runnable callback) {
385414
synchronized (sFirstDrawHandlers) {
386415
if (!sFirstDrawComplete) {
@@ -451,7 +480,7 @@ public void setView(View view, WindowManager.LayoutParams attrs, View panelParen
451480

452481
// If the application owns the surface, don't enable hardware acceleration
453482
if (mSurfaceHolder == null) {
454-
enableHardwareAcceleration(attrs);
483+
enableHardwareAcceleration(mView.getContext(), attrs);
455484
}
456485

457486
boolean restore = false;
@@ -611,7 +640,7 @@ void destroyHardwareLayers() {
611640
}
612641
}
613642

614-
private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
643+
private void enableHardwareAcceleration(Context context, WindowManager.LayoutParams attrs) {
615644
mAttachInfo.mHardwareAccelerated = false;
616645
mAttachInfo.mHardwareAccelerationRequested = false;
617646

@@ -644,20 +673,27 @@ private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
644673
if (!HardwareRenderer.sRendererDisabled || (HardwareRenderer.sSystemRendererDisabled
645674
&& forceHwAccelerated)) {
646675
// Don't enable hardware acceleration when we're not on the main thread
647-
if (!HardwareRenderer.sSystemRendererDisabled
648-
&& Looper.getMainLooper() != Looper.myLooper()) {
649-
Log.w(HardwareRenderer.LOG_TAG, "Attempting to initialize hardware "
676+
if (!HardwareRenderer.sSystemRendererDisabled &&
677+
Looper.getMainLooper() != Looper.myLooper()) {
678+
Log.w(HardwareRenderer.LOG_TAG, "Attempting to initialize hardware "
650679
+ "acceleration outside of the main thread, aborting");
651680
return;
652681
}
653682

654-
final boolean translucent = attrs.format != PixelFormat.OPAQUE;
683+
boolean renderThread = isRenderThreadRequested(context);
684+
if (renderThread) {
685+
Log.i(HardwareRenderer.LOG_TAG, "Render threat initiated");
686+
}
687+
655688
if (mAttachInfo.mHardwareRenderer != null) {
656689
mAttachInfo.mHardwareRenderer.destroy(true);
657-
}
690+
}
691+
692+
final boolean translucent = attrs.format != PixelFormat.OPAQUE;
658693
mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
659694
mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareAccelerationRequested
660695
= mAttachInfo.mHardwareRenderer != null;
696+
661697
} else if (fakeHwAccelerated) {
662698
// The window had wanted to use hardware acceleration, but this
663699
// is not allowed in its process. By setting this flag, it can
@@ -3444,11 +3480,11 @@ public void handleDispatchSystemUiVisibilityChanged(SystemUiVisibilityInfo args)
34443480
if (args.localChanges != 0) {
34453481
if (mAttachInfo != null) {
34463482
mAttachInfo.mSystemUiVisibility =
3447-
(mAttachInfo.mSystemUiVisibility&~args.localChanges)
3448-
| (args.localValue&args.localChanges);
3483+
(mAttachInfo.mSystemUiVisibility & ~args.localChanges) |
3484+
(args.localValue & args.localChanges);
3485+
mAttachInfo.mRecomputeGlobalAttributes = true;
34493486
}
34503487
mView.updateLocalSystemUiVisibility(args.localValue, args.localChanges);
3451-
mAttachInfo.mRecomputeGlobalAttributes = true;
34523488
scheduleTraversals();
34533489
}
34543490
mView.dispatchSystemUiVisibilityChanged(args.globalVisibility);
@@ -3602,7 +3638,7 @@ public void debug() {
36023638
mView.debug();
36033639
}
36043640

3605-
public void dumpGfxInfo(PrintWriter pw, int[] info) {
3641+
public void dumpGfxInfo(int[] info) {
36063642
if (mView != null) {
36073643
getGfxInfo(mView, info);
36083644
} else {
@@ -3714,7 +3750,7 @@ public void dispatchResized(int w, int h, Rect coveredInsets,
37143750
* Represents a pending input event that is waiting in a queue.
37153751
*
37163752
* Input events are processed in serial order by the timestamp specified by
3717-
* {@link InputEvent#getEventTime()}. In general, the input dispatcher delivers
3753+
* {@link InputEvent#getEventTimeNano()}. In general, the input dispatcher delivers
37183754
* one input event to the application at a time and waits for the application
37193755
* to finish handling it before delivering the next one.
37203756
*
@@ -3723,7 +3759,7 @@ public void dispatchResized(int w, int h, Rect coveredInsets,
37233759
* needing a queue on the application's side.
37243760
*/
37253761
private static final class QueuedInputEvent {
3726-
public static final int FLAG_DELIVER_POST_IME = 1 << 0;
3762+
public static final int FLAG_DELIVER_POST_IME = 1;
37273763

37283764
public QueuedInputEvent mNext;
37293765

@@ -4842,7 +4878,7 @@ public void findAccessibilityNodeInfosByTextUiThread(Message message) {
48424878
mPool.release(args);
48434879
List<AccessibilityNodeInfo> infos = null;
48444880
try {
4845-
View target = null;
4881+
View target;
48464882
if (accessibilityViewId != View.NO_ID) {
48474883
target = findViewByAccessibilityId(accessibilityViewId);
48484884
} else {

core/java/android/view/WindowManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ public void dumpGfxInfo(FileDescriptor fd) {
490490

491491
for (int i = 0; i < count; i++) {
492492
ViewRootImpl root = mRoots[i];
493-
root.dumpGfxInfo(pw, info);
493+
root.dumpGfxInfo(info);
494494

495495
String name = root.getClass().getName() + '@' +
496496
Integer.toHexString(hashCode());

libs/hwui/OpenGLRenderer.cpp

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
754754

755755
// TODO: See LayerRenderer.cpp::generateMesh() for important
756756
// information about this implementation
757-
if (!layer->region.isEmpty()) {
757+
if (CC_LIKELY(!layer->region.isEmpty())) {
758758
size_t count;
759759
const android::Rect* rects = layer->region.getArray(&count);
760760

@@ -1398,7 +1398,7 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint
13981398
if (!texture) return;
13991399
const AutoTexture autoCleanup(texture);
14001400

1401-
if (bitmap->getConfig() == SkBitmap::kA8_Config) {
1401+
if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
14021402
drawAlphaBitmap(texture, left, top, paint);
14031403
} else {
14041404
drawTextureRect(left, top, right, bottom, texture, paint);
@@ -1454,9 +1454,9 @@ void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHei
14541454
float bottom = FLT_MIN;
14551455

14561456
#if RENDER_LAYERS_AS_REGIONS
1457-
bool hasActiveLayer = hasLayer();
1457+
const bool hasActiveLayer = hasLayer();
14581458
#else
1459-
bool hasActiveLayer = false;
1459+
const bool hasActiveLayer = false;
14601460
#endif
14611461

14621462
// TODO: Support the colors array
@@ -1541,7 +1541,7 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
15411541

15421542
texture->setWrap(GL_CLAMP_TO_EDGE, true);
15431543

1544-
if (mSnapshot->transform->isPureTranslate()) {
1544+
if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
15451545
const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
15461546
const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
15471547

@@ -1587,7 +1587,7 @@ void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int
15871587
const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
15881588
right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
15891589

1590-
if (mesh && mesh->verticesCount > 0) {
1590+
if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
15911591
const bool pureTranslate = mSnapshot->transform->isPureTranslate();
15921592
#if RENDER_LAYERS_AS_REGIONS
15931593
// Mark the current layer dirty where we are going to draw the patch
@@ -1597,7 +1597,7 @@ void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int
15971597
const size_t count = mesh->quads.size();
15981598
for (size_t i = 0; i < count; i++) {
15991599
const Rect& bounds = mesh->quads.itemAt(i);
1600-
if (pureTranslate) {
1600+
if (CC_LIKELY(pureTranslate)) {
16011601
const float x = (int) floorf(bounds.left + offsetX + 0.5f);
16021602
const float y = (int) floorf(bounds.top + offsetY + 0.5f);
16031603
dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
@@ -1609,7 +1609,7 @@ void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int
16091609
}
16101610
#endif
16111611

1612-
if (pureTranslate) {
1612+
if (CC_LIKELY(pureTranslate)) {
16131613
const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
16141614
const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
16151615

@@ -1637,7 +1637,7 @@ void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom
16371637
float inverseScaleX = 1.0f;
16381638
float inverseScaleY = 1.0f;
16391639
// The quad that we use needs to account for scaling.
1640-
if (!mSnapshot->transform->isPureTranslate()) {
1640+
if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
16411641
Matrix4 *mat = mSnapshot->transform;
16421642
float m00 = mat->data[Matrix4::kScaleX];
16431643
float m01 = mat->data[Matrix4::kSkewY];
@@ -1743,16 +1743,16 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
17431743
// The quad that we use for AA and hairlines needs to account for scaling. For hairlines
17441744
// the line on the screen should always be one pixel wide regardless of scale. For
17451745
// AA lines, we only want one pixel of translucent boundary around the quad.
1746-
if (!mSnapshot->transform->isPureTranslate()) {
1746+
if (CC_UNLIKELY(!mSnapshot->transform->isPureTranslate())) {
17471747
Matrix4 *mat = mSnapshot->transform;
17481748
float m00 = mat->data[Matrix4::kScaleX];
17491749
float m01 = mat->data[Matrix4::kSkewY];
17501750
float m02 = mat->data[2];
17511751
float m10 = mat->data[Matrix4::kSkewX];
17521752
float m11 = mat->data[Matrix4::kScaleX];
17531753
float m12 = mat->data[6];
1754-
float scaleX = sqrt(m00*m00 + m01*m01);
1755-
float scaleY = sqrt(m10*m10 + m11*m11);
1754+
float scaleX = sqrtf(m00 * m00 + m01 * m01);
1755+
float scaleY = sqrtf(m10 * m10 + m11 * m11);
17561756
inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0;
17571757
inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0;
17581758
if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) {
@@ -1770,11 +1770,7 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
17701770
setupDrawColor(paint->getColor(), alpha);
17711771
setupDrawColorFilter();
17721772
setupDrawShader();
1773-
if (isAA) {
1774-
setupDrawBlending(true, mode);
1775-
} else {
1776-
setupDrawBlending(mode);
1777-
}
1773+
setupDrawBlending(isAA, mode);
17781774
setupDrawProgram();
17791775
setupDrawModelViewIdentity(true);
17801776
setupDrawColorUniforms();
@@ -1792,7 +1788,7 @@ void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
17921788
Vertex* vertices = &lines[0];
17931789
AAVertex wLines[verticesCount];
17941790
AAVertex* aaVertices = &wLines[0];
1795-
if (!isAA) {
1791+
if (CC_UNLIKELY(!isAA)) {
17961792
setupDrawVertices(vertices);
17971793
} else {
17981794
void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset;
@@ -2152,9 +2148,9 @@ void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
21522148
Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
21532149

21542150
#if RENDER_LAYERS_AS_REGIONS
2155-
bool hasActiveLayer = hasLayer();
2151+
const bool hasActiveLayer = hasLayer();
21562152
#else
2157-
bool hasActiveLayer = false;
2153+
const bool hasActiveLayer = false;
21582154
#endif
21592155

21602156
if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
@@ -2201,7 +2197,7 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
22012197
const float oldX = x;
22022198
const float oldY = y;
22032199
const bool pureTranslate = mSnapshot->transform->isPureTranslate();
2204-
if (pureTranslate) {
2200+
if (CC_LIKELY(pureTranslate)) {
22052201
x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
22062202
y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
22072203
}
@@ -2218,7 +2214,7 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
22182214
SkXfermode::Mode mode;
22192215
getAlphaAndMode(paint, &alpha, &mode);
22202216

2221-
if (mHasShadow) {
2217+
if (CC_UNLIKELY(mHasShadow)) {
22222218
mCaches.activeTexture(0);
22232219

22242220
mCaches.dropShadowCache.setFontRenderer(fontRenderer);
@@ -2277,9 +2273,9 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
22772273
Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
22782274

22792275
#if RENDER_LAYERS_AS_REGIONS
2280-
bool hasActiveLayer = hasLayer();
2276+
const bool hasActiveLayer = hasLayer();
22812277
#else
2282-
bool hasActiveLayer = false;
2278+
const bool hasActiveLayer = false;
22832279
#endif
22842280

22852281
if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
@@ -2326,7 +2322,7 @@ void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
23262322
layer->setAlpha(alpha, mode);
23272323

23282324
#if RENDER_LAYERS_AS_REGIONS
2329-
if (!layer->region.isEmpty()) {
2325+
if (CC_LIKELY(!layer->region.isEmpty())) {
23302326
if (layer->region.isRect()) {
23312327
composeLayerRect(layer, layer->regionRect);
23322328
} else if (layer->mesh) {
@@ -2342,7 +2338,7 @@ void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
23422338
setupDrawPureColorUniforms();
23432339
setupDrawColorFilterUniforms();
23442340
setupDrawTexture(layer->getTexture());
2345-
if (mSnapshot->transform->isPureTranslate()) {
2341+
if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
23462342
x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
23472343
y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
23482344

@@ -2502,7 +2498,7 @@ void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float
25022498
break;
25032499
}
25042500

2505-
if (underlineWidth > 0.0f) {
2501+
if (CC_LIKELY(underlineWidth > 0.0f)) {
25062502
const float textSize = paintCopy.getTextSize();
25072503
const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
25082504

@@ -2571,7 +2567,7 @@ void OpenGLRenderer::drawTextureRect(float left, float top, float right, float b
25712567

25722568
texture->setWrap(GL_CLAMP_TO_EDGE, true);
25732569

2574-
if (mSnapshot->transform->isPureTranslate()) {
2570+
if (CC_LIKELY(mSnapshot->transform->isPureTranslate())) {
25752571
const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
25762572
const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
25772573

@@ -2631,8 +2627,8 @@ void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
26312627
// the blending, turn blending off here
26322628
// If the blend mode cannot be implemented using shaders, fall
26332629
// back to the default SrcOver blend mode instead
2634-
if (mode > SkXfermode::kScreen_Mode) {
2635-
if (mCaches.extensions.hasFramebufferFetch()) {
2630+
if CC_UNLIKELY((mode > SkXfermode::kScreen_Mode)) {
2631+
if (CC_UNLIKELY(mCaches.extensions.hasFramebufferFetch())) {
26362632
description.framebufferMode = mode;
26372633
description.swapSrcDst = swapSrcDst;
26382634

tests/HwAccelerationTest/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
android:label="HwUi"
3131
android:hardwareAccelerated="true">
3232

33+
<meta-data android:name="android.graphics.renderThread" android:value="true" />
34+
3335
<activity
3436
android:name="PaintDrawFilterActivity"
3537
android:label="_DrawFilter">

0 commit comments

Comments
 (0)