Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 898fd94f authored by Tyler Freeman's avatar Tyler Freeman Committed by Android (Google) Code Review
Browse files

Merge changes from topic "forcedark-ignoreoutoutrendernode" into main

* changes:
  fix(force invert): don't let devs opt-out of force dark when force invert is on
  refactor(force invert): use enum/intdef for force dark type in setForceDark() functions
parents f5276474 33567749
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ import android.content.res.TypedArray;
import android.graphics.BLASTBufferQueue;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ForceDarkType;
import android.graphics.FrameInfo;
import android.graphics.HardwareRenderer;
import android.graphics.HardwareRenderer.FrameDrawingCallback;
@@ -1796,7 +1797,7 @@ public final class ViewRootImpl implements ViewParent,

    /** Returns true if force dark should be enabled according to various settings */
    @VisibleForTesting
    public boolean isForceDarkEnabled() {
    public @ForceDarkType.ForceDarkTypeDef int determineForceDarkType() {
        if (forceInvertColor()) {
            boolean isForceInvertEnabled = Settings.Secure.getIntForUser(
                    mContext.getContentResolver(),
@@ -1808,7 +1809,7 @@ public final class ViewRootImpl implements ViewParent,
            // for dark mode in configuration.uiMode. Instead, we assume that the force invert
            // setting will be enabled at the same time dark theme is in the Settings app.
            if (isForceInvertEnabled) {
                return true;
                return ForceDarkType.FORCE_INVERT_COLOR_DARK;
            }
        }

@@ -1822,12 +1823,12 @@ public final class ViewRootImpl implements ViewParent,
                    && a.getBoolean(R.styleable.Theme_forceDarkAllowed, forceDarkAllowedDefault);
            a.recycle();
        }
        return useAutoDark;
        return useAutoDark ? ForceDarkType.FORCE_DARK : ForceDarkType.NONE;
    }

    private void updateForceDarkMode() {
        if (mAttachInfo.mThreadedRenderer == null) return;
        if (mAttachInfo.mThreadedRenderer.setForceDark(isForceDarkEnabled())) {
        if (mAttachInfo.mThreadedRenderer.setForceDark(determineForceDarkType())) {
            // TODO: Don't require regenerating all display lists to apply this setting
            invalidateWorld(mView);
        }
+6 −4
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import static org.junit.Assume.assumeTrue;
import android.app.Instrumentation;
import android.app.UiModeManager;
import android.content.Context;
import android.graphics.ForceDarkType;
import android.hardware.display.DisplayManagerGlobal;
import android.os.Binder;
import android.os.SystemProperties;
@@ -593,7 +594,7 @@ public class ViewRootImplTest {
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isFalse();
        assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE);
    }

    @Test
@@ -613,7 +614,8 @@ public class ViewRootImplTest {
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isTrue();
        assertThat(mViewRootImpl.determineForceDarkType())
                .isEqualTo(ForceDarkType.FORCE_INVERT_COLOR_DARK);
    }

    @Test
@@ -634,7 +636,7 @@ public class ViewRootImplTest {
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isFalse();
        assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE);
    }

    @Test
@@ -654,7 +656,7 @@ public class ViewRootImplTest {
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isTrue();
        assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.FORCE_DARK);
    }

    private boolean setForceDarkSysProp(boolean isForceDarkEnabled) {
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.graphics;

import android.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * The style of force dark to use in {@link HardwareRenderer}.
 *
 * You must keep this in sync with the C++ enum ForceDarkType in
 * frameworks/base/libs/hwui/utils/ForceDark.h
 *
 * @hide
 */
public class ForceDarkType {
    /**
     * Force dark disabled: normal, default operation.
     *
     * @hide
     */
    public static final int NONE = 0;

    /**
     * Use force dark
     * @hide
     */
    public static final int FORCE_DARK = 1;

    /**
     * Force force-dark. {@see Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED}
     * @hide */
    public static final int FORCE_INVERT_COLOR_DARK = 2;

    /** @hide */
    @IntDef({
        NONE,
        FORCE_DARK,
        FORCE_INVERT_COLOR_DARK,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ForceDarkTypeDef {}

}
+6 −6
Original line number Diff line number Diff line
@@ -182,7 +182,7 @@ public class HardwareRenderer {
    /** @hide */
    protected RenderNode mRootNode;
    private boolean mOpaque = true;
    private boolean mForceDark = false;
    private int mForceDark = ForceDarkType.NONE;
    private @ActivityInfo.ColorMode int mColorMode = ActivityInfo.COLOR_MODE_DEFAULT;
    private float mDesiredSdrHdrRatio = 1f;

@@ -571,10 +571,10 @@ public class HardwareRenderer {
     * Whether or not the force-dark feature should be used for this renderer.
     * @hide
     */
    public boolean setForceDark(boolean enable) {
        if (mForceDark != enable) {
            mForceDark = enable;
            nSetForceDark(mNativeProxy, enable);
    public boolean setForceDark(@ForceDarkType.ForceDarkTypeDef int type) {
        if (mForceDark != type) {
            mForceDark = type;
            nSetForceDark(mNativeProxy, type);
            return true;
        }
        return false;
@@ -1597,7 +1597,7 @@ public class HardwareRenderer {

    private static native void nAllocateBuffers(long nativeProxy);

    private static native void nSetForceDark(long nativeProxy, boolean enabled);
    private static native void nSetForceDark(long nativeProxy, int type);

    private static native void nSetDisplayDensityDpi(int densityDpi);

+10 −4
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
#ifdef __ANDROID__
#include "include/gpu/ganesh/SkImageGanesh.h"
#endif
#include "utils/ForceDark.h"
#include "utils/MathUtils.h"
#include "utils/StringUtils.h"

@@ -403,16 +404,21 @@ void RenderNode::syncDisplayList(TreeObserver& observer, TreeInfo* info) {
    deleteDisplayList(observer, info);
    mDisplayList = std::move(mStagingDisplayList);
    if (mDisplayList) {
        WebViewSyncData syncData {
            .applyForceDark = info && !info->disableForceDark
        };
        WebViewSyncData syncData{.applyForceDark = shouldEnableForceDark(info)};
        mDisplayList.syncContents(syncData);
        handleForceDark(info);
    }
}

inline bool RenderNode::shouldEnableForceDark(TreeInfo* info) {
    return CC_UNLIKELY(
            info &&
            (!info->disableForceDark ||
             info->forceDarkType == android::uirenderer::ForceDarkType::FORCE_INVERT_COLOR_DARK));
}

void RenderNode::handleForceDark(android::uirenderer::TreeInfo *info) {
    if (CC_LIKELY(!info || info->disableForceDark)) {
    if (!shouldEnableForceDark(info)) {
        return;
    }
    auto usage = usageHint();
Loading