Merge changes from topic "biometric-prompt-service"

* changes:
  Add BiometricPromptService
  Remove common biometric directory
diff --git a/Android.mk b/Android.mk
index 84b708e..988c009 100644
--- a/Android.mk
+++ b/Android.mk
@@ -322,131 +322,34 @@
 	( unzip -qo $< -d $(OUT_DOCS)/offline-sdk && touch -f $@ ) || exit 1
 
 # ==== hiddenapi lists =======================================
-include $(CLEAR_VARS)
-
-# File names of final API lists
-LOCAL_WHITELIST := $(INTERNAL_PLATFORM_HIDDENAPI_WHITELIST)
-LOCAL_LIGHT_GREYLIST := $(INTERNAL_PLATFORM_HIDDENAPI_LIGHT_GREYLIST)
-LOCAL_DARK_GREYLIST := $(INTERNAL_PLATFORM_HIDDENAPI_DARK_GREYLIST)
-LOCAL_BLACKLIST := $(INTERNAL_PLATFORM_HIDDENAPI_BLACKLIST)
-
-# File names of source files we will use to generate the final API lists.
-LOCAL_SRC_GREYLIST := frameworks/base/config/hiddenapi-light-greylist.txt
-LOCAL_SRC_VENDOR_LIST := frameworks/base/config/hiddenapi-vendor-list.txt
-LOCAL_SRC_FORCE_BLACKLIST := frameworks/base/config/hiddenapi-force-blacklist.txt
-LOCAL_SRC_PUBLIC_API := $(INTERNAL_PLATFORM_HIDDENAPI_PUBLIC_LIST)
-LOCAL_SRC_PRIVATE_API := $(INTERNAL_PLATFORM_HIDDENAPI_PRIVATE_LIST)
-LOCAL_SRC_REMOVED_API := $(INTERNAL_PLATFORM_REMOVED_DEX_API_FILE)
-
-LOCAL_SRC_ALL := \
-	$(LOCAL_SRC_GREYLIST) \
-	$(LOCAL_SRC_VENDOR_LIST) \
-	$(LOCAL_SRC_FORCE_BLACKLIST) \
-	$(LOCAL_SRC_PUBLIC_API) \
-	$(LOCAL_SRC_PRIVATE_API) \
-	$(LOCAL_SRC_REMOVED_API)
-
-define assert-has-no-overlap
-if [ ! -z "`comm -12 <(sort $(1)) <(sort $(2))`" ]; then \
-	echo "$(1) and $(2) should not overlap" 1>&2; \
-	comm -12 <(sort $(1)) <(sort $(2)) 1>&2; \
-	exit 1; \
-fi
-endef
-
-define assert-is-subset
-if [ ! -z "`comm -23 <(sort $(1)) <(sort $(2))`" ]; then \
-	echo "$(1) must be a subset of $(2)" 1>&2; \
-	comm -23 <(sort $(1)) <(sort $(2)) 1>&2; \
-	exit 1; \
-fi
-endef
-
-define assert-has-no-duplicates
-if [ ! -z "`sort $(1) | uniq -D`" ]; then \
-	echo "$(1) has duplicate entries" 1>&2; \
-	sort $(1) | uniq -D 1>&2; \
-	exit 1; \
-fi
-endef
-
-# The following rules build API lists in the build folder.
-# By not using files from the source tree, ART buildbots can mock these lists
-# or have alternative rules for building them. Other rules in the build system
-# should depend on the files in the build folder.
-
-# Merge whitelist from:
-# (1) public API stubs
-# (2) whitelist entries generated by class2greylist (PRIVATE_WHITELIST_INPUTS)
-$(LOCAL_WHITELIST): $(LOCAL_SRC_PUBLIC_API)
-	sort $(LOCAL_SRC_PUBLIC_API) $(PRIVATE_WHITELIST_INPUTS) > $@
-	$(call assert-has-no-duplicates,$@)
-
-# Merge light greylist from multiple files:
-#  (1) manual greylist LOCAL_SRC_GREYLIST
-#  (2) list of usages from vendor apps LOCAL_SRC_VENDOR_LIST
-#  (3) list of removed APIs in LOCAL_SRC_REMOVED_API
-#      @removed does not imply private in Doclava. We must take the subset also
-#      in LOCAL_SRC_PRIVATE_API.
-#  (4) list of serialization APIs
-#      Automatically adds all methods which match the signatures in
-#      REGEX_SERIALIZATION. These are greylisted in order to allow applications
-#      to write their own serializers.
-#  (5) greylist entries generated by class2greylist (PRIVATE_GREYLIST_INPUTS)
-$(LOCAL_LIGHT_GREYLIST): REGEX_SERIALIZATION := \
-    "readObject\(Ljava/io/ObjectInputStream;\)V" \
-    "readObjectNoData\(\)V" \
-    "readResolve\(\)Ljava/lang/Object;" \
-    "serialVersionUID:J" \
-    "serialPersistentFields:\[Ljava/io/ObjectStreamField;" \
-    "writeObject\(Ljava/io/ObjectOutputStream;\)V" \
-    "writeReplace\(\)Ljava/lang/Object;"
-$(LOCAL_LIGHT_GREYLIST): $(LOCAL_SRC_ALL) $(LOCAL_WHITELIST)
-	sort $(LOCAL_SRC_GREYLIST) $(LOCAL_SRC_VENDOR_LIST) $(PRIVATE_GREYLIST_INPUTS) \
-	     <(grep -E "\->("$(subst $(space),"|",$(REGEX_SERIALIZATION))")$$" \
-	               $(LOCAL_SRC_PRIVATE_API)) \
-	     <(comm -12 <(sort $(LOCAL_SRC_REMOVED_API)) <(sort $(LOCAL_SRC_PRIVATE_API))) \
-	     > $@
-	$(call assert-has-no-duplicates,$@)
-	$(call assert-is-subset,$@,$(LOCAL_SRC_PRIVATE_API))
-	$(call assert-has-no-overlap,$@,$(LOCAL_WHITELIST))
-	$(call assert-has-no-overlap,$@,$(LOCAL_SRC_FORCE_BLACKLIST))
-
-# Generate dark greylist as remaining classes and class members in the same
-# package as classes listed in the light greylist.
-# The algorithm is as follows:
-#   (1) extract the class descriptor from each entry in LOCAL_LIGHT_GREYLIST
-#   (2) strip everything after the last forward-slash,
-#       e.g. 'Lpackage/subpackage/class$inner;' turns into 'Lpackage/subpackage/'
-#   (3) insert all entries from LOCAL_SRC_PRIVATE_API which begin with the package
-#       name but do not contain another forward-slash in the class name, e.g.
-#       matching '^Lpackage/subpackage/[^/;]*;'
-#   (4) subtract entries shared with LOCAL_LIGHT_GREYLIST
-$(LOCAL_DARK_GREYLIST): $(LOCAL_SRC_ALL) $(LOCAL_WHITELIST) $(LOCAL_LIGHT_GREYLIST)
-	comm -13 <(sort $(LOCAL_WHITELIST) $(LOCAL_LIGHT_GREYLIST) $(LOCAL_SRC_FORCE_BLACKLIST)) \
-	         <(cat $(LOCAL_WHITELIST) $(LOCAL_LIGHT_GREYLIST) | \
-	               sed 's/\->.*//' | sed 's/\(.*\/\).*/\1/' | sort | uniq | \
-	               while read PKG_NAME; do \
-	                   grep -E "^$${PKG_NAME}[^/;]*;" $(LOCAL_SRC_PRIVATE_API); \
-	               done | sort | uniq) \
-	         > $@
-	$(call assert-is-subset,$@,$(LOCAL_SRC_PRIVATE_API))
-	$(call assert-has-no-duplicates,$@)
-	$(call assert-has-no-overlap,$@,$(LOCAL_WHITELIST))
-	$(call assert-has-no-overlap,$@,$(LOCAL_LIGHT_GREYLIST))
-	$(call assert-has-no-overlap,$@,$(LOCAL_SRC_FORCE_BLACKLIST))
-
-# Generate blacklist as private API minus (light greylist plus dark greylist).
-$(LOCAL_BLACKLIST): $(LOCAL_SRC_ALL) $(LOCAL_WHITELIST) $(LOCAL_LIGHT_GREYLIST) $(LOCAL_DARK_GREYLIST)
-	comm -13 <(sort $(LOCAL_WHITELIST) $(LOCAL_LIGHT_GREYLIST) $(LOCAL_DARK_GREYLIST)) \
-	         <(sort $(LOCAL_SRC_PRIVATE_API)) \
-	         > $@
-	$(call assert-is-subset,$@,$(LOCAL_SRC_PRIVATE_API))
-	$(call assert-has-no-duplicates,$@)
-	$(call assert-has-no-overlap,$@,$(LOCAL_WHITELIST))
-	$(call assert-has-no-overlap,$@,$(LOCAL_LIGHT_GREYLIST))
-	$(call assert-has-no-overlap,$@,$(LOCAL_DARK_GREYLIST))
-	$(call assert-is-subset,$(LOCAL_SRC_FORCE_BLACKLIST),$@)
+$(INTERNAL_PLATFORM_HIDDENAPI_WHITELIST): \
+    .KATI_IMPLICIT_OUTPUTS := \
+        $(INTERNAL_PLATFORM_HIDDENAPI_LIGHT_GREYLIST) \
+        $(INTERNAL_PLATFORM_HIDDENAPI_DARK_GREYLIST) \
+        $(INTERNAL_PLATFORM_HIDDENAPI_BLACKLIST)
+$(INTERNAL_PLATFORM_HIDDENAPI_WHITELIST): \
+    frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py \
+    frameworks/base/config/hiddenapi-light-greylist.txt \
+    frameworks/base/config/hiddenapi-vendor-list.txt \
+    frameworks/base/config/hiddenapi-force-blacklist.txt \
+    $(INTERNAL_PLATFORM_HIDDENAPI_PUBLIC_LIST) \
+    $(INTERNAL_PLATFORM_HIDDENAPI_PRIVATE_LIST) \
+    $(INTERNAL_PLATFORM_REMOVED_DEX_API_FILE)
+	frameworks/base/tools/hiddenapi/generate_hiddenapi_lists.py \
+	    --input-public $(INTERNAL_PLATFORM_HIDDENAPI_PUBLIC_LIST) \
+	    --input-private $(INTERNAL_PLATFORM_HIDDENAPI_PRIVATE_LIST) \
+	    --input-whitelists $(PRIVATE_WHITELIST_INPUTS) \
+	    --input-greylists \
+	        frameworks/base/config/hiddenapi-light-greylist.txt \
+	        frameworks/base/config/hiddenapi-vendor-list.txt \
+	        <(comm -12 <(sort $(INTERNAL_PLATFORM_REMOVED_DEX_API_FILE)) \
+	                   $(INTERNAL_PLATFORM_HIDDENAPI_PRIVATE_LIST)) \
+	        $(PRIVATE_GREYLIST_INPUTS) \
+	    --input-blacklists frameworks/base/config/hiddenapi-force-blacklist.txt \
+	    --output-whitelist $(INTERNAL_PLATFORM_HIDDENAPI_WHITELIST) \
+	    --output-light-greylist $(INTERNAL_PLATFORM_HIDDENAPI_LIGHT_GREYLIST) \
+	    --output-dark-greylist $(INTERNAL_PLATFORM_HIDDENAPI_DARK_GREYLIST) \
+	    --output-blacklist $(INTERNAL_PLATFORM_HIDDENAPI_BLACKLIST)
 
 # Include subdirectory makefiles
 # ============================================================
diff --git a/api/current.txt b/api/current.txt
index 4d692ee..7cacc8e 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -3762,7 +3762,7 @@
     method public boolean onSearchRequested(android.view.SearchEvent);
     method public boolean onSearchRequested();
     method protected void onStart();
-    method public void onStateNotSaved();
+    method public deprecated void onStateNotSaved();
     method protected void onStop();
     method protected void onTitleChanged(java.lang.CharSequence, int);
     method public boolean onTouchEvent(android.view.MotionEvent);
@@ -37309,7 +37309,7 @@
     field public static final int RESULT_SMS_HANDLED = 1; // 0x1
     field public static final int RESULT_SMS_OUT_OF_MEMORY = 3; // 0x3
     field public static final int RESULT_SMS_UNSUPPORTED = 4; // 0x4
-    field public static final java.lang.String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
+    field public static final deprecated java.lang.String SECRET_CODE_ACTION = "android.provider.Telephony.SECRET_CODE";
     field public static final java.lang.String SIM_FULL_ACTION = "android.provider.Telephony.SIM_FULL";
     field public static final java.lang.String SMS_CB_RECEIVED_ACTION = "android.provider.Telephony.SMS_CB_RECEIVED";
     field public static final java.lang.String SMS_DELIVER_ACTION = "android.provider.Telephony.SMS_DELIVER";
@@ -41870,6 +41870,8 @@
     field public static final java.lang.String KEY_CALL_FORWARDING_BLOCKS_WHILE_ROAMING_STRING_ARRAY = "call_forwarding_blocks_while_roaming_string_array";
     field public static final java.lang.String KEY_CARRIER_ALLOW_TURNOFF_IMS_BOOL = "carrier_allow_turnoff_ims_bool";
     field public static final java.lang.String KEY_CARRIER_DATA_CALL_PERMANENT_FAILURE_STRINGS = "carrier_data_call_permanent_failure_strings";
+    field public static final java.lang.String KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT = "carrier_default_wfc_ims_mode_int";
+    field public static final java.lang.String KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT = "carrier_default_wfc_ims_roaming_mode_int";
     field public static final java.lang.String KEY_CARRIER_FORCE_DISABLE_ETWS_CMAS_TEST_BOOL = "carrier_force_disable_etws_cmas_test_bool";
     field public static final java.lang.String KEY_CARRIER_IMS_GBA_REQUIRED_BOOL = "carrier_ims_gba_required_bool";
     field public static final java.lang.String KEY_CARRIER_INSTANT_LETTERING_AVAILABLE_BOOL = "carrier_instant_lettering_available_bool";
@@ -42752,6 +42754,7 @@
     field public static final java.lang.String ACTION_CONFIGURE_VOICEMAIL = "android.telephony.action.CONFIGURE_VOICEMAIL";
     field public static final java.lang.String ACTION_PHONE_STATE_CHANGED = "android.intent.action.PHONE_STATE";
     field public static final java.lang.String ACTION_RESPOND_VIA_MESSAGE = "android.intent.action.RESPOND_VIA_MESSAGE";
+    field public static final java.lang.String ACTION_SECRET_CODE = "android.telephony.action.SECRET_CODE";
     field public static final java.lang.String ACTION_SHOW_VOICEMAIL_NOTIFICATION = "android.telephony.action.SHOW_VOICEMAIL_NOTIFICATION";
     field public static final java.lang.String ACTION_SUBSCRIPTION_CARRIER_IDENTITY_CHANGED = "android.telephony.action.SUBSCRIPTION_CARRIER_IDENTITY_CHANGED";
     field public static final int APPTYPE_CSIM = 4; // 0x4
@@ -53018,7 +53021,15 @@
   public final class Magnifier {
     ctor public Magnifier(android.view.View);
     method public void dismiss();
+    method public float getCornerRadius();
+    method public int getDefaultHorizontalSourceToMagnifierOffset();
+    method public int getDefaultVerticalSourceToMagnifierOffset();
+    method public float getElevation();
     method public int getHeight();
+    method public android.graphics.Point getPosition();
+    method public int getSourceHeight();
+    method public android.graphics.Point getSourcePosition();
+    method public int getSourceWidth();
     method public int getWidth();
     method public float getZoom();
     method public void show(float, float);
@@ -53026,6 +53037,16 @@
     method public void update();
   }
 
+  public static class Magnifier.Builder {
+    ctor public Magnifier.Builder(android.view.View);
+    method public android.widget.Magnifier build();
+    method public android.widget.Magnifier.Builder setCornerRadius(float);
+    method public android.widget.Magnifier.Builder setDefaultSourceToMagnifierOffset(int, int);
+    method public android.widget.Magnifier.Builder setElevation(float);
+    method public android.widget.Magnifier.Builder setSize(int, int);
+    method public android.widget.Magnifier.Builder setZoom(float);
+  }
+
   public class MediaController extends android.widget.FrameLayout {
     ctor public MediaController(android.content.Context, android.util.AttributeSet);
     ctor public MediaController(android.content.Context, boolean);
diff --git a/api/system-current.txt b/api/system-current.txt
index 3d8c494..4b8276d 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -213,11 +213,24 @@
     field public static final int userRestriction = 16844164; // 0x1010584
   }
 
+  public static final class R.bool {
+    field public static final int config_sendPackageName = 17891328; // 0x1110000
+  }
+
   public static final class R.raw {
     field public static final int loaderror = 17825792; // 0x1100000
     field public static final int nodomain = 17825793; // 0x1100001
   }
 
+  public static final class R.string {
+    field public static final int config_feedback_intent_extra_key = 17039391; // 0x104001f
+    field public static final int config_feedback_intent_name_key = 17039392; // 0x1040020
+    field public static final int config_help_intent_extra_key = 17039389; // 0x104001d
+    field public static final int config_help_intent_name_key = 17039390; // 0x104001e
+    field public static final int config_help_package_name_key = 17039387; // 0x104001b
+    field public static final int config_help_package_name_value = 17039388; // 0x104001c
+  }
+
   public static final class R.style {
     field public static final int Theme_Leanback_FormWizard = 16974544; // 0x10302d0
   }
@@ -358,9 +371,11 @@
     method public final void attachBaseContext(android.content.Context);
     method public final android.os.IBinder onBind(android.content.Intent);
     method public deprecated void onGetInstantAppIntentFilter(int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
-    method public void onGetInstantAppIntentFilter(android.content.Intent, int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
+    method public deprecated void onGetInstantAppIntentFilter(android.content.Intent, int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
+    method public void onGetInstantAppIntentFilter(android.content.Intent, int[], android.os.UserHandle, java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
     method public deprecated void onGetInstantAppResolveInfo(int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
-    method public void onGetInstantAppResolveInfo(android.content.Intent, int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
+    method public deprecated void onGetInstantAppResolveInfo(android.content.Intent, int[], java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
+    method public void onGetInstantAppResolveInfo(android.content.Intent, int[], android.os.UserHandle, java.lang.String, android.app.InstantAppResolverService.InstantAppResolutionCallback);
   }
 
   public static final class InstantAppResolverService.InstantAppResolutionCallback {
diff --git a/api/test-current.txt b/api/test-current.txt
index 3a9d6a4..40aa440 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -1598,7 +1598,6 @@
   public final class Magnifier {
     method public android.graphics.Bitmap getContent();
     method public static android.graphics.PointF getMagnifierDefaultSize();
-    method public android.graphics.Rect getWindowPositionOnScreen();
     method public void setOnOperationCompleteCallback(android.widget.Magnifier.Callback);
   }
 
diff --git a/config/boot-image-profile.txt b/config/boot-image-profile.txt
index 1f6cfff..cdcdc46 100644
--- a/config/boot-image-profile.txt
+++ b/config/boot-image-profile.txt
@@ -6645,7 +6645,6 @@
 HPLandroid/view/SurfaceControl$Transaction;->setAnimationTransaction()Landroid/view/SurfaceControl$Transaction;
 HPLandroid/view/SurfaceControl$Transaction;->setColor(Landroid/view/SurfaceControl;[F)Landroid/view/SurfaceControl$Transaction;
 HPLandroid/view/SurfaceControl$Transaction;->setEarlyWakeup()Landroid/view/SurfaceControl$Transaction;
-HPLandroid/view/SurfaceControl$Transaction;->setFinalCrop(Landroid/view/SurfaceControl;Landroid/graphics/Rect;)Landroid/view/SurfaceControl$Transaction;
 HPLandroid/view/SurfaceControl$Transaction;->setGeometryAppliesWithResize(Landroid/view/SurfaceControl;)Landroid/view/SurfaceControl$Transaction;
 HPLandroid/view/SurfaceControl$Transaction;->setMatrix(Landroid/view/SurfaceControl;Landroid/graphics/Matrix;[F)Landroid/view/SurfaceControl$Transaction;
 HPLandroid/view/SurfaceControl$Transaction;->setOpaque(Landroid/view/SurfaceControl;Z)Landroid/view/SurfaceControl$Transaction;
diff --git a/config/hiddenapi-light-greylist.txt b/config/hiddenapi-light-greylist.txt
index 6f06edc..045814a 100644
--- a/config/hiddenapi-light-greylist.txt
+++ b/config/hiddenapi-light-greylist.txt
@@ -214,14 +214,6 @@
 Landroid/app/PackageInstallObserver;-><init>()V
 Landroid/app/ResourcesManager$ActivityResources;-><init>()V
 Landroid/app/ResourcesManager;-><init>()V
-Landroid/app/servertransaction/ActivityResultItem;->mResultInfoList:Ljava/util/List;
-Landroid/app/servertransaction/ClientTransaction;->getActivityToken()Landroid/os/IBinder;
-Landroid/app/servertransaction/ClientTransaction;->getCallbacks()Ljava/util/List;
-Landroid/app/servertransaction/ClientTransaction;->getLifecycleStateRequest()Landroid/app/servertransaction/ActivityLifecycleItem;
-Landroid/app/servertransaction/ClientTransaction;->mActivityCallbacks:Ljava/util/List;
-Landroid/app/servertransaction/LaunchActivityItem;->mInfo:Landroid/content/pm/ActivityInfo;
-Landroid/app/servertransaction/LaunchActivityItem;->mIntent:Landroid/content/Intent;
-Landroid/app/servertransaction/NewIntentItem;->mIntents:Ljava/util/List;
 Landroid/app/TaskStackListener;-><init>()V
 Landroid/app/trust/ITrustManager$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
 Landroid/app/UiAutomationConnection;-><init>()V
@@ -426,8 +418,6 @@
 Landroid/database/IContentObserver$Stub;-><init>()V
 Landroid/database/IContentObserver$Stub;->asInterface(Landroid/os/IBinder;)Landroid/database/IContentObserver;
 Landroid/database/IContentObserver;->onChange(ZLandroid/net/Uri;I)V
-Landroid/hardware/biometrics/BiometricConstants;->BIOMETRIC_ERROR_VENDOR_BASE:I
-Landroid/hardware/biometrics/BiometricFingerprintConstants;->FINGERPRINT_ERROR_VENDOR_BASE:I
 Landroid/hardware/display/IDisplayManager$Stub;->asInterface(Landroid/os/IBinder;)Landroid/hardware/display/IDisplayManager;
 Landroid/hardware/display/IDisplayManager;->getDisplayInfo(I)Landroid/view/DisplayInfo;
 Landroid/hardware/fingerprint/IFingerprintService$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
@@ -765,8 +755,6 @@
 Landroid/os/Handler;->mCallback:Landroid/os/Handler$Callback;
 Landroid/os/Handler;->mLooper:Landroid/os/Looper;
 Landroid/os/Handler;->mMessenger:Landroid/os/IMessenger;
-Landroid/os/health/SystemHealthManager;-><init>()V
-Landroid/os/health/SystemHealthManager;->from(Landroid/content/Context;)Landroid/os/health/SystemHealthManager;
 Landroid/os/HwParcel;-><init>(Z)V
 Landroid/os/HwRemoteBinder;-><init>()V
 Landroid/os/IBatteryPropertiesRegistrar$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
@@ -950,98 +938,10 @@
 Landroid/os/SharedMemory;->getFd()I
 Landroid/os/ShellCommand;->peekNextArg()Ljava/lang/String;
 Landroid/os/StatFs;->mStat:Landroid/system/StructStatVfs;
-Landroid/os/storage/DiskInfo;-><init>(Landroid/os/Parcel;)V
-Landroid/os/storage/DiskInfo;->CREATOR:Landroid/os/Parcelable$Creator;
-Landroid/os/storage/DiskInfo;->flags:I
-Landroid/os/storage/DiskInfo;->getDescription()Ljava/lang/String;
-Landroid/os/storage/DiskInfo;->isAdoptable()Z
-Landroid/os/storage/DiskInfo;->isDefaultPrimary()Z
-Landroid/os/storage/DiskInfo;->isSd()Z
-Landroid/os/storage/DiskInfo;->isUsb()Z
-Landroid/os/storage/DiskInfo;->label:Ljava/lang/String;
-Landroid/os/storage/DiskInfo;->size:J
 Landroid/os/storage/IObbActionListener$Stub;-><init>()V
 Landroid/os/storage/IObbActionListener$Stub;->asInterface(Landroid/os/IBinder;)Landroid/os/storage/IObbActionListener;
 Landroid/os/storage/IStorageManager$Stub$Proxy;-><init>(Landroid/os/IBinder;)V
 Landroid/os/storage/IStorageManager$Stub;->asInterface(Landroid/os/IBinder;)Landroid/os/storage/IStorageManager;
-Landroid/os/storage/StorageEventListener;->onDiskDestroyed(Landroid/os/storage/DiskInfo;)V
-Landroid/os/storage/StorageEventListener;->onDiskScanned(Landroid/os/storage/DiskInfo;I)V
-Landroid/os/storage/StorageEventListener;->onStorageStateChanged(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
-Landroid/os/storage/StorageEventListener;->onUsbMassStorageConnectionChanged(Z)V
-Landroid/os/storage/StorageEventListener;->onVolumeForgotten(Ljava/lang/String;)V
-Landroid/os/storage/StorageEventListener;->onVolumeRecordChanged(Landroid/os/storage/VolumeRecord;)V
-Landroid/os/storage/StorageEventListener;->onVolumeStateChanged(Landroid/os/storage/VolumeInfo;II)V
-Landroid/os/storage/StorageManager;-><init>(Landroid/content/Context;Landroid/os/Looper;)V
-Landroid/os/storage/StorageManager;->CRYPT_TYPE_DEFAULT:I
-Landroid/os/storage/StorageManager;->CRYPT_TYPE_PASSWORD:I
-Landroid/os/storage/StorageManager;->disableUsbMassStorage()V
-Landroid/os/storage/StorageManager;->enableUsbMassStorage()V
-Landroid/os/storage/StorageManager;->ENCRYPTION_STATE_NONE:I
-Landroid/os/storage/StorageManager;->findDiskById(Ljava/lang/String;)Landroid/os/storage/DiskInfo;
-Landroid/os/storage/StorageManager;->findEmulatedForPrivate(Landroid/os/storage/VolumeInfo;)Landroid/os/storage/VolumeInfo;
-Landroid/os/storage/StorageManager;->findVolumeByUuid(Ljava/lang/String;)Landroid/os/storage/VolumeInfo;
-Landroid/os/storage/StorageManager;->format(Ljava/lang/String;)V
-Landroid/os/storage/StorageManager;->getBestVolumeDescription(Landroid/os/storage/VolumeInfo;)Ljava/lang/String;
-Landroid/os/storage/StorageManager;->getDisks()Ljava/util/List;
-Landroid/os/storage/StorageManager;->getPrimaryPhysicalVolume()Landroid/os/storage/VolumeInfo;
-Landroid/os/storage/StorageManager;->getStorageBytesUntilLow(Ljava/io/File;)J
-Landroid/os/storage/StorageManager;->getStorageFullBytes(Ljava/io/File;)J
-Landroid/os/storage/StorageManager;->getStorageLowBytes(Ljava/io/File;)J
-Landroid/os/storage/StorageManager;->getStorageVolume([Landroid/os/storage/StorageVolume;Ljava/io/File;)Landroid/os/storage/StorageVolume;
-Landroid/os/storage/StorageManager;->getVolumeList(II)[Landroid/os/storage/StorageVolume;
-Landroid/os/storage/StorageManager;->getVolumePaths()[Ljava/lang/String;
-Landroid/os/storage/StorageManager;->getVolumes()Ljava/util/List;
-Landroid/os/storage/StorageManager;->getVolumeState(Ljava/lang/String;)Ljava/lang/String;
-Landroid/os/storage/StorageManager;->isFileEncryptedNativeOnly()Z
-Landroid/os/storage/StorageManager;->isUsbMassStorageConnected()Z
-Landroid/os/storage/StorageManager;->isUsbMassStorageEnabled()Z
-Landroid/os/storage/StorageManager;->partitionPublic(Ljava/lang/String;)V
-Landroid/os/storage/StorageManager;->unmount(Ljava/lang/String;)V
-Landroid/os/storage/StorageVolume;->allowMassStorage()Z
-Landroid/os/storage/StorageVolume;->getFatVolumeId()I
-Landroid/os/storage/StorageVolume;->getMaxFileSize()J
-Landroid/os/storage/StorageVolume;->getOwner()Landroid/os/UserHandle;
-Landroid/os/storage/StorageVolume;->getPathFile()Ljava/io/File;
-Landroid/os/storage/StorageVolume;->getUserLabel()Ljava/lang/String;
-Landroid/os/storage/StorageVolume;->mDescription:Ljava/lang/String;
-Landroid/os/storage/StorageVolume;->mId:Ljava/lang/String;
-Landroid/os/storage/StorageVolume;->mPath:Ljava/io/File;
-Landroid/os/storage/StorageVolume;->mPrimary:Z
-Landroid/os/storage/StorageVolume;->mRemovable:Z
-Landroid/os/storage/VolumeInfo;-><init>(Landroid/os/Parcel;)V
-Landroid/os/storage/VolumeInfo;->buildBrowseIntent()Landroid/content/Intent;
-Landroid/os/storage/VolumeInfo;->buildStableMtpStorageId(Ljava/lang/String;)I
-Landroid/os/storage/VolumeInfo;->buildStorageVolume(Landroid/content/Context;IZ)Landroid/os/storage/StorageVolume;
-Landroid/os/storage/VolumeInfo;->CREATOR:Landroid/os/Parcelable$Creator;
-Landroid/os/storage/VolumeInfo;->disk:Landroid/os/storage/DiskInfo;
-Landroid/os/storage/VolumeInfo;->fsLabel:Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->fsUuid:Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getDescription()Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getDisk()Landroid/os/storage/DiskInfo;
-Landroid/os/storage/VolumeInfo;->getDiskId()Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getEnvironmentForState(I)Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getFsUuid()Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getInternalPath()Ljava/io/File;
-Landroid/os/storage/VolumeInfo;->getInternalPathForUser(I)Ljava/io/File;
-Landroid/os/storage/VolumeInfo;->getMountUserId()I
-Landroid/os/storage/VolumeInfo;->getPath()Ljava/io/File;
-Landroid/os/storage/VolumeInfo;->getPathForUser(I)Ljava/io/File;
-Landroid/os/storage/VolumeInfo;->getState()I
-Landroid/os/storage/VolumeInfo;->getType()I
-Landroid/os/storage/VolumeInfo;->internalPath:Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->isMountedReadable()Z
-Landroid/os/storage/VolumeInfo;->isMountedWritable()Z
-Landroid/os/storage/VolumeInfo;->isPrimary()Z
-Landroid/os/storage/VolumeInfo;->isPrimaryPhysical()Z
-Landroid/os/storage/VolumeInfo;->isVisible()Z
-Landroid/os/storage/VolumeInfo;->isVisibleForWrite(I)Z
-Landroid/os/storage/VolumeInfo;->path:Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->state:I
-Landroid/os/storage/VolumeInfo;->type:I
-Landroid/os/storage/VolumeInfo;->TYPE_EMULATED:I
-Landroid/os/storage/VolumeInfo;->TYPE_PUBLIC:I
-Landroid/os/storage/VolumeRecord;-><init>(Landroid/os/Parcel;)V
-Landroid/os/storage/VolumeRecord;->CREATOR:Landroid/os/Parcelable$Creator;
 Landroid/os/StrictMode$Span;->finish()V
 Landroid/os/StrictMode$ThreadPolicy;->mask:I
 Landroid/os/StrictMode$VmPolicy$Builder;->mMask:I
@@ -1548,17 +1448,6 @@
 Landroid/system/StructTimeval;->fromMillis(J)Landroid/system/StructTimeval;
 Landroid/telephony/CarrierMessagingServiceManager;-><init>()V
 Landroid/telephony/TelephonyManager$MultiSimVariants;->values()[Landroid/telephony/TelephonyManager$MultiSimVariants;
-Landroid/transition/ChangeBounds;->BOTTOM_RIGHT_ONLY_PROPERTY:Landroid/util/Property;
-Landroid/transition/ChangeBounds;->POSITION_PROPERTY:Landroid/util/Property;
-Landroid/transition/Scene;->mEnterAction:Ljava/lang/Runnable;
-Landroid/transition/Scene;->mExitAction:Ljava/lang/Runnable;
-Landroid/transition/Scene;->setCurrentScene(Landroid/view/View;Landroid/transition/Scene;)V
-Landroid/transition/Transition;->cancel()V
-Landroid/transition/Transition;->end()V
-Landroid/transition/Transition;->getRunningAnimators()Landroid/util/ArrayMap;
-Landroid/transition/TransitionManager;->getRunningTransitions()Landroid/util/ArrayMap;
-Landroid/transition/TransitionManager;->sPendingTransitions:Ljava/util/ArrayList;
-Landroid/transition/TransitionManager;->sRunningTransitions:Ljava/lang/ThreadLocal;
 Landroid/util/Singleton;-><init>()V
 Landroid/view/accessibility/IAccessibilityInteractionConnectionCallback;->setFindAccessibilityNodeInfoResult(Landroid/view/accessibility/AccessibilityNodeInfo;I)V
 Landroid/view/accessibility/IAccessibilityInteractionConnectionCallback;->setFindAccessibilityNodeInfosResult(Ljava/util/List;I)V
diff --git a/config/hiddenapi-vendor-list.txt b/config/hiddenapi-vendor-list.txt
index 54cc07a..02719e3f8 100644
--- a/config/hiddenapi-vendor-list.txt
+++ b/config/hiddenapi-vendor-list.txt
@@ -113,14 +113,7 @@
 Landroid/os/RegistrantList;->removeCleared()V
 Landroid/os/RemoteException;->rethrowFromSystemServer()Ljava/lang/RuntimeException;
 Landroid/os/ServiceSpecificException;->errorCode:I
-Landroid/os/storage/DiskInfo;->getId()Ljava/lang/String;
 Landroid/os/storage/StorageEventListener;-><init>()V
-Landroid/os/storage/StorageManager;->findVolumeById(Ljava/lang/String;)Landroid/os/storage/VolumeInfo;
-Landroid/os/storage/StorageManager;->from(Landroid/content/Context;)Landroid/os/storage/StorageManager;
-Landroid/os/storage/StorageManager;->registerListener(Landroid/os/storage/StorageEventListener;)V
-Landroid/os/storage/StorageManager;->unregisterListener(Landroid/os/storage/StorageEventListener;)V
-Landroid/os/storage/StorageVolume;->getId()Ljava/lang/String;
-Landroid/os/storage/VolumeInfo;->getId()Ljava/lang/String;
 Landroid/os/SystemProperties;->reportSyspropChanged()V
 Landroid/os/SystemService;->start(Ljava/lang/String;)V
 Landroid/os/SystemService;->stop(Ljava/lang/String;)V
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 8d5b96b..6d7011a 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -1349,7 +1349,12 @@
      * to give the activity a hint that its state is no longer saved -- it will generally
      * be called after {@link #onSaveInstanceState} and prior to the activity being
      * resumed/started again.
+     *
+     * @deprecated starting with {@link android.os.Build.VERSION_CODES#P} onSaveInstanceState is
+     * called after {@link #onStop}, so this hint isn't accurate anymore: you should consider your
+     * state not saved in between {@code onStart} and {@code onStop} callbacks inclusively.
      */
+    @Deprecated
     public void onStateNotSaved() {
     }
 
diff --git a/core/java/android/app/IInstantAppResolver.aidl b/core/java/android/app/IInstantAppResolver.aidl
index ae20057..7318762 100644
--- a/core/java/android/app/IInstantAppResolver.aidl
+++ b/core/java/android/app/IInstantAppResolver.aidl
@@ -22,9 +22,9 @@
 /** @hide */
 oneway interface IInstantAppResolver {
     void getInstantAppResolveInfoList(in Intent sanitizedIntent, in int[] hostDigestPrefix,
-            String token, int sequence, IRemoteCallback callback);
+            int userId, String token, int sequence, IRemoteCallback callback);
 
     void getInstantAppIntentFilterList(in Intent sanitizedIntent, in int[] hostDigestPrefix,
-            String token, IRemoteCallback callback);
+            int userId, String token, IRemoteCallback callback);
 
 }
diff --git a/core/java/android/app/InstantAppResolverService.java b/core/java/android/app/InstantAppResolverService.java
index 58d0aaf..8f83ee3 100644
--- a/core/java/android/app/InstantAppResolverService.java
+++ b/core/java/android/app/InstantAppResolverService.java
@@ -28,6 +28,7 @@
 import android.os.Looper;
 import android.os.Message;
 import android.os.RemoteException;
+import android.os.UserHandle;
 import android.util.Log;
 import android.util.Slog;
 
@@ -56,8 +57,8 @@
      * Called to retrieve resolve info for instant applications immediately.
      *
      * @param digestPrefix The hash prefix of the instant app's domain.
-     * @deprecated should implement {@link #onGetInstantAppResolveInfo(Intent, int[], String,
-     *             InstantAppResolutionCallback)}
+     * @deprecated Should implement {@link #onGetInstantAppResolveInfo(Intent, int[], UserHandle,
+     *             String, InstantAppResolutionCallback)}.
      */
     @Deprecated
     public void onGetInstantAppResolveInfo(
@@ -70,8 +71,8 @@
      * sources.
      *
      * @param digestPrefix The hash prefix of the instant app's domain.
-     * @deprecated should implement {@link #onGetInstantAppIntentFilter(Intent, int[], String,
-     *             InstantAppResolutionCallback)}
+     * @deprecated Should implement {@link #onGetInstantAppIntentFilter(Intent, int[], UserHandle,
+     *             String, InstantAppResolutionCallback)}.
      */
     @Deprecated
     public void onGetInstantAppIntentFilter(
@@ -86,7 +87,6 @@
      * result in a subsequent call to
      * {@link #onGetInstantAppIntentFilter(Intent, int[], String, InstantAppResolutionCallback)}
      *
-     *
      * @param sanitizedIntent The sanitized {@link Intent} used for resolution. A sanitized Intent
      *                        is an intent with potential PII removed from the original intent.
      *                        Fields removed include extras and the host + path of the data, if
@@ -100,7 +100,11 @@
      * @param callback The {@link InstantAppResolutionCallback} to provide results to.
      *
      * @see InstantAppResolveInfo
+     *
+     * @deprecated Should implement {@link #onGetInstantAppResolveInfo(Intent, int[], UserHandle,
+     *             String, InstantAppResolutionCallback)}.
      */
+    @Deprecated
     public void onGetInstantAppResolveInfo(Intent sanitizedIntent, int[] hostDigestPrefix,
             String token, InstantAppResolutionCallback callback) {
         // if not overridden, forward to old methods and filter out non-web intents
@@ -125,8 +129,12 @@
      *              InstantAppResolutionCallback)}
      *              and provided to the currently visible installer via
      *              {@link Intent#EXTRA_INSTANT_APP_TOKEN}.
-     * @param callback The {@link InstantAppResolutionCallback} to provide results to
+     * @param callback The {@link InstantAppResolutionCallback} to provide results to.
+     *
+     * @deprecated Should implement {@link #onGetInstantAppIntentFilter(Intent, int[], UserHandle,
+     *             String, InstantAppResolutionCallback)}.
      */
+    @Deprecated
     public void onGetInstantAppIntentFilter(Intent sanitizedIntent, int[] hostDigestPrefix,
             String token, InstantAppResolutionCallback callback) {
         Log.e(TAG, "New onGetInstantAppIntentFilter is not overridden");
@@ -139,6 +147,55 @@
     }
 
     /**
+     * Called to retrieve resolve info for instant applications immediately. The response will be
+     * ignored if not provided within a reasonable time. {@link InstantAppResolveInfo}s provided
+     * in response to this method may be partial to request a second phase of resolution which will
+     * result in a subsequent call to {@link #onGetInstantAppIntentFilter(Intent, int[], UserHandle,
+     * String, InstantAppResolutionCallback)}
+     *
+     * @param sanitizedIntent The sanitized {@link Intent} used for resolution. A sanitized Intent
+     *                        is an intent with potential PII removed from the original intent.
+     *                        Fields removed include extras and the host + path of the data, if
+     *                        defined.
+     * @param hostDigestPrefix The hash prefix of the instant app's domain.
+     * @param userHandle The user for which to resolve the instant app.
+     * @param token A unique identifier that will be provided in calls to {@link
+     *              #onGetInstantAppIntentFilter(Intent, int[], UserHandle, String,
+     *              InstantAppResolutionCallback)} and provided to the installer via {@link
+     *              Intent#EXTRA_INSTANT_APP_TOKEN} to tie a single launch together.
+     * @param callback The {@link InstantAppResolutionCallback} to provide results to.
+     *
+     * @see InstantAppResolveInfo
+     */
+    public void onGetInstantAppResolveInfo(Intent sanitizedIntent, int[] hostDigestPrefix,
+            UserHandle userHandle, String token, InstantAppResolutionCallback callback) {
+        // If not overridden, forward to the old method.
+        onGetInstantAppResolveInfo(sanitizedIntent, hostDigestPrefix, token, callback);
+    }
+
+    /**
+     * Called to retrieve intent filters for potentially matching instant applications. Unlike
+     * {@link #onGetInstantAppResolveInfo(Intent, int[], UserHandle, String,
+     * InstantAppResolutionCallback)}, the response may take as long as necessary to respond. All
+     * {@link InstantAppResolveInfo}s provided in response to this method must be completely
+     * populated.
+     *
+     * @param sanitizedIntent The sanitized {@link Intent} used for resolution.
+     * @param hostDigestPrefix The hash prefix of the instant app's domain or null if no host is
+     *                         defined.
+     * @param userHandle The user for which to resolve the instant app.
+     * @param token A unique identifier that was provided in {@link #onGetInstantAppResolveInfo(
+     *              Intent, int[], UserHandle, String, InstantAppResolutionCallback)} and provided
+     *              to the currently visible installer via {@link Intent#EXTRA_INSTANT_APP_TOKEN}.
+     * @param callback The {@link InstantAppResolutionCallback} to provide results to.
+     */
+    public void onGetInstantAppIntentFilter(Intent sanitizedIntent, int[] hostDigestPrefix,
+            UserHandle userHandle, String token, InstantAppResolutionCallback callback) {
+        // If not overridden, forward to the old method.
+        onGetInstantAppIntentFilter(sanitizedIntent, hostDigestPrefix, token, callback);
+    }
+
+    /**
      * Returns a {@link Looper} to perform service operations on.
      */
     Looper getLooper() {
@@ -156,30 +213,32 @@
         return new IInstantAppResolver.Stub() {
             @Override
             public void getInstantAppResolveInfoList(Intent sanitizedIntent, int[] digestPrefix,
-                    String token, int sequence, IRemoteCallback callback) {
+                    int userId, String token, int sequence, IRemoteCallback callback) {
                 if (DEBUG_INSTANT) {
                     Slog.v(TAG, "[" + token + "] Phase1 called; posting");
                 }
                 final SomeArgs args = SomeArgs.obtain();
                 args.arg1 = callback;
                 args.arg2 = digestPrefix;
-                args.arg3 = token;
-                args.arg4 = sanitizedIntent;
+                args.arg3 = userId;
+                args.arg4 = token;
+                args.arg5 = sanitizedIntent;
                 mHandler.obtainMessage(ServiceHandler.MSG_GET_INSTANT_APP_RESOLVE_INFO,
                         sequence, 0, args).sendToTarget();
             }
 
             @Override
             public void getInstantAppIntentFilterList(Intent sanitizedIntent,
-                    int[] digestPrefix, String token, IRemoteCallback callback) {
+                    int[] digestPrefix, int userId, String token, IRemoteCallback callback) {
                 if (DEBUG_INSTANT) {
                     Slog.v(TAG, "[" + token + "] Phase2 called; posting");
                 }
                 final SomeArgs args = SomeArgs.obtain();
                 args.arg1 = callback;
                 args.arg2 = digestPrefix;
-                args.arg3 = token;
-                args.arg4 = sanitizedIntent;
+                args.arg3 = userId;
+                args.arg4 = token;
+                args.arg5 = sanitizedIntent;
                 mHandler.obtainMessage(ServiceHandler.MSG_GET_INSTANT_APP_INTENT_FILTER,
                         callback).sendToTarget();
             }
@@ -224,14 +283,16 @@
                     final SomeArgs args = (SomeArgs) message.obj;
                     final IRemoteCallback callback = (IRemoteCallback) args.arg1;
                     final int[] digestPrefix = (int[]) args.arg2;
-                    final String token = (String) args.arg3;
-                    final Intent intent = (Intent) args.arg4;
+                    final int userId = (int) args.arg3;
+                    final String token = (String) args.arg4;
+                    final Intent intent = (Intent) args.arg5;
                     final int sequence = message.arg1;
                     if (DEBUG_INSTANT) {
                         Slog.d(TAG, "[" + token + "] Phase1 request;"
-                                + " prefix: " + Arrays.toString(digestPrefix));
+                                + " prefix: " + Arrays.toString(digestPrefix)
+                                + ", userId: " + userId);
                     }
-                    onGetInstantAppResolveInfo(intent, digestPrefix, token,
+                    onGetInstantAppResolveInfo(intent, digestPrefix, UserHandle.of(userId), token,
                             new InstantAppResolutionCallback(sequence, callback));
                 } break;
 
@@ -239,13 +300,15 @@
                     final SomeArgs args = (SomeArgs) message.obj;
                     final IRemoteCallback callback = (IRemoteCallback) args.arg1;
                     final int[] digestPrefix = (int[]) args.arg2;
-                    final String token = (String) args.arg3;
-                    final Intent intent = (Intent) args.arg4;
+                    final int userId = (int) args.arg3;
+                    final String token = (String) args.arg4;
+                    final Intent intent = (Intent) args.arg5;
                     if (DEBUG_INSTANT) {
                         Slog.d(TAG, "[" + token + "] Phase2 request;"
-                                + " prefix: " + Arrays.toString(digestPrefix));
+                                + " prefix: " + Arrays.toString(digestPrefix)
+                                + ", userId: " + userId);
                     }
-                    onGetInstantAppIntentFilter(intent, digestPrefix, token,
+                    onGetInstantAppIntentFilter(intent, digestPrefix, UserHandle.of(userId), token,
                             new InstantAppResolutionCallback(-1 /*sequence*/, callback));
                 } break;
 
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index aa021a2..7ef9b54 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -3896,6 +3896,11 @@
 
     /**
      * Installs the given certificate as a user CA.
+     * <p>
+     * Inserted user CAs aren't automatically trusted by apps in Android 7.0 (API level 24) and
+     * higher. App developers can change the default behavior for an app by adding a
+     * <a href="{@docRoot}training/articles/security-config.html">Security Configuration
+     * File</a> to the app manifest file.
      *
      * The caller must be a profile or device owner on that user, or a delegate package given the
      * {@link #DELEGATION_CERT_INSTALL} scope via {@link #setDelegatedScopes}; otherwise a
diff --git a/core/java/android/app/servertransaction/ActivityResultItem.java b/core/java/android/app/servertransaction/ActivityResultItem.java
index e57f585..bab17dc 100644
--- a/core/java/android/app/servertransaction/ActivityResultItem.java
+++ b/core/java/android/app/servertransaction/ActivityResultItem.java
@@ -18,6 +18,7 @@
 
 import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
 
+import android.annotation.UnsupportedAppUsage;
 import android.app.ClientTransactionHandler;
 import android.app.ResultInfo;
 import android.os.IBinder;
@@ -34,6 +35,7 @@
  */
 public class ActivityResultItem extends ClientTransactionItem {
 
+    @UnsupportedAppUsage
     private List<ResultInfo> mResultInfoList;
 
     /* TODO(b/78294732)
diff --git a/core/java/android/app/servertransaction/ClientTransaction.java b/core/java/android/app/servertransaction/ClientTransaction.java
index 2a33342..f1d87ac 100644
--- a/core/java/android/app/servertransaction/ClientTransaction.java
+++ b/core/java/android/app/servertransaction/ClientTransaction.java
@@ -17,6 +17,7 @@
 package android.app.servertransaction;
 
 import android.annotation.Nullable;
+import android.annotation.UnsupportedAppUsage;
 import android.app.ClientTransactionHandler;
 import android.app.IApplicationThread;
 import android.os.IBinder;
@@ -43,6 +44,7 @@
 public class ClientTransaction implements Parcelable, ObjectPoolItem {
 
     /** A list of individual callbacks to a client. */
+    @UnsupportedAppUsage
     private List<ClientTransactionItem> mActivityCallbacks;
 
     /**
@@ -75,18 +77,21 @@
 
     /** Get the list of callbacks. */
     @Nullable
+    @UnsupportedAppUsage
     List<ClientTransactionItem> getCallbacks() {
         return mActivityCallbacks;
     }
 
     /** Get the target activity. */
     @Nullable
+    @UnsupportedAppUsage
     public IBinder getActivityToken() {
         return mActivityToken;
     }
 
     /** Get the target state lifecycle request. */
     @VisibleForTesting
+    @UnsupportedAppUsage
     public ActivityLifecycleItem getLifecycleStateRequest() {
         return mLifecycleStateRequest;
     }
diff --git a/core/java/android/app/servertransaction/LaunchActivityItem.java b/core/java/android/app/servertransaction/LaunchActivityItem.java
index 6bae359..ed793bf 100644
--- a/core/java/android/app/servertransaction/LaunchActivityItem.java
+++ b/core/java/android/app/servertransaction/LaunchActivityItem.java
@@ -18,6 +18,7 @@
 
 import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
 
+import android.annotation.UnsupportedAppUsage;
 import android.app.ActivityThread.ActivityClientRecord;
 import android.app.ClientTransactionHandler;
 import android.app.ProfilerInfo;
@@ -45,8 +46,10 @@
  */
 public class LaunchActivityItem extends ClientTransactionItem {
 
+    @UnsupportedAppUsage
     private Intent mIntent;
     private int mIdent;
+    @UnsupportedAppUsage
     private ActivityInfo mInfo;
     private Configuration mCurConfig;
     private Configuration mOverrideConfig;
diff --git a/core/java/android/app/servertransaction/NewIntentItem.java b/core/java/android/app/servertransaction/NewIntentItem.java
index e5ce3b0..4f28cd2 100644
--- a/core/java/android/app/servertransaction/NewIntentItem.java
+++ b/core/java/android/app/servertransaction/NewIntentItem.java
@@ -16,6 +16,7 @@
 
 package android.app.servertransaction;
 
+import android.annotation.UnsupportedAppUsage;
 import android.app.ClientTransactionHandler;
 import android.os.IBinder;
 import android.os.Parcel;
@@ -33,6 +34,7 @@
  */
 public class NewIntentItem extends ClientTransactionItem {
 
+    @UnsupportedAppUsage
     private List<ReferrerIntent> mIntents;
     private boolean mPause;
 
diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java
index 3bfb1c5..a882434 100644
--- a/core/java/android/content/ContentResolver.java
+++ b/core/java/android/content/ContentResolver.java
@@ -2130,7 +2130,7 @@
                     uri, observer == null ? null : observer.getContentObserver(),
                     observer != null && observer.deliverSelfNotifications(),
                     syncToNetwork ? NOTIFY_SYNC_TO_NETWORK : 0,
-                    userHandle, mTargetSdkVersion);
+                    userHandle, mTargetSdkVersion, mContext.getPackageName());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -2147,7 +2147,7 @@
             getContentService().notifyChange(
                     uri, observer == null ? null : observer.getContentObserver(),
                     observer != null && observer.deliverSelfNotifications(), flags,
-                    userHandle, mTargetSdkVersion);
+                    userHandle, mTargetSdkVersion, mContext.getPackageName());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -2320,7 +2320,9 @@
                 .syncOnce()     // Immediate sync.
                 .build();
         try {
-            getContentService().syncAsUser(request, userId);
+            // Note ActivityThread.currentPackageName() may not be accurate in a shared process
+            // case, but it's only for debugging.
+            getContentService().syncAsUser(request, userId, ActivityThread.currentPackageName());
         } catch(RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -2332,7 +2334,9 @@
      */
     public static void requestSync(SyncRequest request) {
         try {
-            getContentService().sync(request);
+            // Note ActivityThread.currentPackageName() may not be accurate in a shared process
+            // case, but it's only for debugging.
+            getContentService().sync(request, ActivityThread.currentPackageName());
         } catch(RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
diff --git a/core/java/android/content/IContentService.aidl b/core/java/android/content/IContentService.aidl
index dc17666..a55dd31 100644
--- a/core/java/android/content/IContentService.aidl
+++ b/core/java/android/content/IContentService.aidl
@@ -53,14 +53,14 @@
      */
     void notifyChange(in Uri uri, IContentObserver observer,
             boolean observerWantsSelfNotifications, int flags,
-            int userHandle, int targetSdkVersion);
+            int userHandle, int targetSdkVersion, String callingPackage);
 
-    void requestSync(in Account account, String authority, in Bundle extras);
+    void requestSync(in Account account, String authority, in Bundle extras, String callingPackage);
     /**
      * Start a sync given a request.
      */
-    void sync(in SyncRequest request);
-    void syncAsUser(in SyncRequest request, int userId);
+    void sync(in SyncRequest request, String callingPackage);
+    void syncAsUser(in SyncRequest request, int userId, String callingPackage);
     void cancelSync(in Account account, String authority, in ComponentName cname);
     void cancelSyncAsUser(in Account account, String authority, in ComponentName cname, int userId);
 
diff --git a/core/java/android/content/pm/ApplicationInfo.java b/core/java/android/content/pm/ApplicationInfo.java
index b7a5352..1108f93 100644
--- a/core/java/android/content/pm/ApplicationInfo.java
+++ b/core/java/android/content/pm/ApplicationInfo.java
@@ -1009,6 +1009,13 @@
     public String appComponentFactory;
 
     /**
+     * Indicates whether this package requires access to non-SDK APIs. Only system apps
+     * and tests are allowed to use this property.
+     * @hide
+     */
+    public boolean usesNonSdkApi;
+
+    /**
      * The category of this app. Categories are used to cluster multiple apps
      * together into meaningful groups, such as when summarizing battery,
      * network, or disk usage. Apps should only define this value when they fit
@@ -1712,8 +1719,13 @@
     }
 
     private boolean isAllowedToUseHiddenApis() {
-        return isSignedWithPlatformKey()
-            || (isPackageWhitelistedForHiddenApis() && (isSystemApp() || isUpdatedSystemApp()));
+        if (isSignedWithPlatformKey()) {
+            return true;
+        } else if (isSystemApp() || isUpdatedSystemApp()) {
+            return usesNonSdkApi || isPackageWhitelistedForHiddenApis();
+        } else {
+            return false;
+        }
     }
 
     /**
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index 83757c4..8b058dc 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -3659,6 +3659,9 @@
             ai.appComponentFactory = buildClassName(ai.packageName, factory, outError);
         }
 
+        ai.usesNonSdkApi = sa.getBoolean(
+                com.android.internal.R.styleable.AndroidManifestApplication_usesNonSdkApi, false);
+
         if (outError[0] == null) {
             CharSequence pname;
             if (owner.applicationInfo.targetSdkVersion >= Build.VERSION_CODES.FROYO) {
diff --git a/core/java/android/hardware/biometrics/BiometricConstants.java b/core/java/android/hardware/biometrics/BiometricConstants.java
index 3612e9d..6150be3 100644
--- a/core/java/android/hardware/biometrics/BiometricConstants.java
+++ b/core/java/android/hardware/biometrics/BiometricConstants.java
@@ -16,6 +16,8 @@
 
 package android.hardware.biometrics;
 
+import android.annotation.UnsupportedAppUsage;
+
 
 /**
  * Interface containing all of the biometric modality agnostic constants.
@@ -119,6 +121,7 @@
     /**
      * @hide
      */
+    @UnsupportedAppUsage
     int BIOMETRIC_ERROR_VENDOR_BASE = 1000;
 
     //
diff --git a/core/java/android/hardware/biometrics/BiometricFingerprintConstants.java b/core/java/android/hardware/biometrics/BiometricFingerprintConstants.java
index d583d78..041b2e6 100644
--- a/core/java/android/hardware/biometrics/BiometricFingerprintConstants.java
+++ b/core/java/android/hardware/biometrics/BiometricFingerprintConstants.java
@@ -16,6 +16,7 @@
 
 package android.hardware.biometrics;
 
+import android.annotation.UnsupportedAppUsage;
 import android.hardware.fingerprint.FingerprintManager;
 
 /**
@@ -120,6 +121,7 @@
     /**
      * @hide
      */
+    @UnsupportedAppUsage
     public static final int FINGERPRINT_ERROR_VENDOR_BASE = 1000;
 
     //
diff --git a/core/java/android/os/StatsLogEventWrapper.java b/core/java/android/os/StatsLogEventWrapper.java
index d4d3dc8..7b3ea57 100644
--- a/core/java/android/os/StatsLogEventWrapper.java
+++ b/core/java/android/os/StatsLogEventWrapper.java
@@ -65,11 +65,17 @@
     public static final Parcelable.Creator<StatsLogEventWrapper> CREATOR = new
             Parcelable.Creator<StatsLogEventWrapper>() {
                 public StatsLogEventWrapper createFromParcel(Parcel in) {
-                    return new StatsLogEventWrapper(in);
+                    android.util.EventLog.writeEvent(0x534e4554, "112550251",
+                            android.os.Binder.getCallingUid(), "");
+                    // Purposefully leaving this method not implemented.
+                    throw new RuntimeException("Not implemented");
                 }
 
                 public StatsLogEventWrapper[] newArray(int size) {
-                    return new StatsLogEventWrapper[size];
+                    android.util.EventLog.writeEvent(0x534e4554, "112550251",
+                            android.os.Binder.getCallingUid(), "");
+                    // Purposefully leaving this method not implemented.
+                    throw new RuntimeException("Not implemented");
                 }
             };
 
@@ -120,10 +126,6 @@
         mStorage.write(bytes, 0, bytes.length);
     }
 
-    private StatsLogEventWrapper(Parcel in) {
-        readFromParcel(in);
-    }
-
     /**
      * Writes the stored fields to a byte array. Will first write a new-line character to denote
      * END_LIST before writing contents to byte array.
@@ -134,13 +136,6 @@
     }
 
     /**
-     * Not implemented.
-     */
-    public void readFromParcel(Parcel in) {
-        // Not needed since this java class is for sending to statsd only.
-    }
-
-    /**
      * Boilerplate for Parcel.
      */
     public int describeContents() {
diff --git a/core/java/android/os/health/SystemHealthManager.java b/core/java/android/os/health/SystemHealthManager.java
index bba4cd1..f303674 100644
--- a/core/java/android/os/health/SystemHealthManager.java
+++ b/core/java/android/os/health/SystemHealthManager.java
@@ -17,6 +17,7 @@
 package android.os.health;
 
 import android.annotation.SystemService;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.os.BatteryStats;
 import android.os.Process;
@@ -49,6 +50,7 @@
      * Construct a new SystemHealthManager object.
      * @hide
      */
+    @UnsupportedAppUsage
     public SystemHealthManager() {
         this(IBatteryStats.Stub.asInterface(ServiceManager.getService(BatteryStats.SERVICE_NAME)));
     }
@@ -63,6 +65,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public static SystemHealthManager from(Context context) {
         return (SystemHealthManager)context.getSystemService(Context.SYSTEM_HEALTH_SERVICE);
     }
diff --git a/core/java/android/os/storage/DiskInfo.java b/core/java/android/os/storage/DiskInfo.java
index d493cce..7b6c971 100644
--- a/core/java/android/os/storage/DiskInfo.java
+++ b/core/java/android/os/storage/DiskInfo.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.UnsupportedAppUsage;
 import android.content.res.Resources;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -50,8 +51,11 @@
     public static final int FLAG_USB = 1 << 3;
 
     public final String id;
+    @UnsupportedAppUsage
     public final int flags;
+    @UnsupportedAppUsage
     public long size;
+    @UnsupportedAppUsage
     public String label;
     /** Hacky; don't rely on this count */
     public int volumeCount;
@@ -62,6 +66,7 @@
         this.flags = flags;
     }
 
+    @UnsupportedAppUsage
     public DiskInfo(Parcel parcel) {
         id = parcel.readString();
         flags = parcel.readInt();
@@ -71,6 +76,7 @@
         sysPath = parcel.readString();
     }
 
+    @UnsupportedAppUsage
     public @NonNull String getId() {
         return id;
     }
@@ -94,6 +100,7 @@
         return true;
     }
 
+    @UnsupportedAppUsage
     public @Nullable String getDescription() {
         final Resources res = Resources.getSystem();
         if ((flags & FLAG_SD) != 0) {
@@ -124,18 +131,22 @@
         }
     }
 
+    @UnsupportedAppUsage
     public boolean isAdoptable() {
         return (flags & FLAG_ADOPTABLE) != 0;
     }
 
+    @UnsupportedAppUsage
     public boolean isDefaultPrimary() {
         return (flags & FLAG_DEFAULT_PRIMARY) != 0;
     }
 
+    @UnsupportedAppUsage
     public boolean isSd() {
         return (flags & FLAG_SD) != 0;
     }
 
+    @UnsupportedAppUsage
     public boolean isUsb() {
         return (flags & FLAG_USB) != 0;
     }
@@ -185,6 +196,7 @@
         return id.hashCode();
     }
 
+    @UnsupportedAppUsage
     public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
         @Override
         public DiskInfo createFromParcel(Parcel in) {
diff --git a/core/java/android/os/storage/StorageEventListener.java b/core/java/android/os/storage/StorageEventListener.java
index 4cf83fd..4aa0b08 100644
--- a/core/java/android/os/storage/StorageEventListener.java
+++ b/core/java/android/os/storage/StorageEventListener.java
@@ -16,6 +16,8 @@
 
 package android.os.storage;
 
+import android.annotation.UnsupportedAppUsage;
+
 /**
  * Used for receiving notifications from the StorageManager
  * 
@@ -26,6 +28,7 @@
      * Called when the detection state of a USB Mass Storage host has changed.
      * @param connected true if the USB mass storage is connected.
      */
+    @UnsupportedAppUsage
     public void onUsbMassStorageConnectionChanged(boolean connected) {
     }
 
@@ -35,21 +38,27 @@
      * @param oldState the old state as returned by {@link android.os.Environment#getExternalStorageState()}.
      * @param newState the old state as returned by {@link android.os.Environment#getExternalStorageState()}.
      */
+    @UnsupportedAppUsage
     public void onStorageStateChanged(String path, String oldState, String newState) {
     }
 
+    @UnsupportedAppUsage
     public void onVolumeStateChanged(VolumeInfo vol, int oldState, int newState) {
     }
 
+    @UnsupportedAppUsage
     public void onVolumeRecordChanged(VolumeRecord rec) {
     }
 
+    @UnsupportedAppUsage
     public void onVolumeForgotten(String fsUuid) {
     }
 
+    @UnsupportedAppUsage
     public void onDiskScanned(DiskInfo disk, int volumeCount) {
     }
 
+    @UnsupportedAppUsage
     public void onDiskDestroyed(DiskInfo disk) {
     }
 }
diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java
index 9724e8f..8d5017b 100644
--- a/core/java/android/os/storage/StorageManager.java
+++ b/core/java/android/os/storage/StorageManager.java
@@ -25,6 +25,7 @@
 import android.annotation.SuppressLint;
 import android.annotation.SystemApi;
 import android.annotation.SystemService;
+import android.annotation.UnsupportedAppUsage;
 import android.annotation.WorkerThread;
 import android.app.Activity;
 import android.app.ActivityThread;
@@ -232,6 +233,7 @@
     public static final int FSTRIM_FLAG_DEEP = IVold.FSTRIM_FLAG_DEEP_TRIM;
 
     /** @hide The volume is not encrypted. */
+    @UnsupportedAppUsage
     public static final int ENCRYPTION_STATE_NONE =
             IVold.ENCRYPTION_STATE_NONE;
 
@@ -448,6 +450,7 @@
 
     /** {@hide} */
     @Deprecated
+    @UnsupportedAppUsage
     public static StorageManager from(Context context) {
         return context.getSystemService(StorageManager.class);
     }
@@ -464,6 +467,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public StorageManager(Context context, Looper looper) throws ServiceNotFoundException {
         mContext = context;
         mResolver = context.getContentResolver();
@@ -478,6 +482,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public void registerListener(StorageEventListener listener) {
         synchronized (mDelegates) {
             final StorageEventListenerDelegate delegate = new StorageEventListenerDelegate(listener,
@@ -498,6 +503,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public void unregisterListener(StorageEventListener listener) {
         synchronized (mDelegates) {
             for (Iterator<StorageEventListenerDelegate> i = mDelegates.iterator(); i.hasNext();) {
@@ -520,6 +526,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public void enableUsbMassStorage() {
     }
 
@@ -529,6 +536,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public void disableUsbMassStorage() {
     }
 
@@ -539,6 +547,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public boolean isUsbMassStorageConnected() {
         return false;
     }
@@ -550,6 +559,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public boolean isUsbMassStorageEnabled() {
         return false;
     }
@@ -670,6 +680,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @NonNull List<DiskInfo> getDisks() {
         try {
             return Arrays.asList(mStorageManager.getDisks());
@@ -679,6 +690,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable DiskInfo findDiskById(String id) {
         Preconditions.checkNotNull(id);
         // TODO; go directly to service to make this faster
@@ -691,6 +703,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable VolumeInfo findVolumeById(String id) {
         Preconditions.checkNotNull(id);
         // TODO; go directly to service to make this faster
@@ -703,6 +716,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable VolumeInfo findVolumeByUuid(String fsUuid) {
         Preconditions.checkNotNull(fsUuid);
         // TODO; go directly to service to make this faster
@@ -736,6 +750,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable VolumeInfo findEmulatedForPrivate(VolumeInfo privateVol) {
         if (privateVol != null) {
             return findVolumeById(privateVol.getId().replace("private", "emulated"));
@@ -814,6 +829,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @NonNull List<VolumeInfo> getVolumes() {
         try {
             return Arrays.asList(mStorageManager.getVolumes(0));
@@ -847,6 +863,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable String getBestVolumeDescription(VolumeInfo vol) {
         if (vol == null) return null;
 
@@ -870,6 +887,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public @Nullable VolumeInfo getPrimaryPhysicalVolume() {
         final List<VolumeInfo> vols = getVolumes();
         for (VolumeInfo vol : vols) {
@@ -890,6 +908,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public void unmount(String volId) {
         try {
             mStorageManager.unmount(volId);
@@ -899,6 +918,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public void format(String volId) {
         try {
             mStorageManager.format(volId);
@@ -940,6 +960,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public void partitionPublic(String diskId) {
         try {
             mStorageManager.partitionPublic(diskId);
@@ -1069,6 +1090,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     private static @Nullable StorageVolume getStorageVolume(StorageVolume[] volumes, File file) {
         if (file == null) {
             return null;
@@ -1098,6 +1120,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public @NonNull String getVolumeState(String mountPoint) {
         final StorageVolume vol = getStorageVolume(new File(mountPoint));
         if (vol != null) {
@@ -1161,6 +1184,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public static @NonNull StorageVolume[] getVolumeList(int userId, int flags) {
         final IStorageManager storageManager = IStorageManager.Stub.asInterface(
                 ServiceManager.getService("mount"));
@@ -1194,6 +1218,7 @@
      * @hide
      */
     @Deprecated
+    @UnsupportedAppUsage
     public @NonNull String[] getVolumePaths() {
         StorageVolume[] volumes = getVolumeList();
         int count = volumes.length;
@@ -1233,6 +1258,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public long getStorageBytesUntilLow(File path) {
         return path.getUsableSpace() - getStorageFullBytes(path);
     }
@@ -1243,6 +1269,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public long getStorageLowBytes(File path) {
         final long lowPercent = Settings.Global.getInt(mResolver,
                 Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE, DEFAULT_THRESHOLD_PERCENTAGE);
@@ -1286,6 +1313,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     public long getStorageFullBytes(File path) {
         return Settings.Global.getLong(mResolver, Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES,
                 DEFAULT_FULL_THRESHOLD_BYTES);
@@ -1402,6 +1430,7 @@
      * @return true for file encrypted. (Implies isEncrypted() == true)
      *         false not encrypted or block encrypted
      */
+    @UnsupportedAppUsage
     public static boolean isFileEncryptedNativeOnly() {
         if (!isEncrypted()) {
             return false;
@@ -2062,8 +2091,10 @@
 
     /// Consts to match the password types in cryptfs.h
     /** @hide */
+    @UnsupportedAppUsage
     public static final int CRYPT_TYPE_PASSWORD = IVold.PASSWORD_TYPE_PASSWORD;
     /** @hide */
+    @UnsupportedAppUsage
     public static final int CRYPT_TYPE_DEFAULT = IVold.PASSWORD_TYPE_DEFAULT;
     /** @hide */
     public static final int CRYPT_TYPE_PATTERN = IVold.PASSWORD_TYPE_PATTERN;
diff --git a/core/java/android/os/storage/StorageVolume.java b/core/java/android/os/storage/StorageVolume.java
index 9880142..deff693 100644
--- a/core/java/android/os/storage/StorageVolume.java
+++ b/core/java/android/os/storage/StorageVolume.java
@@ -18,6 +18,7 @@
 
 import android.annotation.Nullable;
 import android.annotation.TestApi;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
@@ -77,11 +78,16 @@
 // user, but is now part of the public API.
 public final class StorageVolume implements Parcelable {
 
+    @UnsupportedAppUsage
     private final String mId;
+    @UnsupportedAppUsage
     private final File mPath;
     private final File mInternalPath;
+    @UnsupportedAppUsage
     private final String mDescription;
+    @UnsupportedAppUsage
     private final boolean mPrimary;
+    @UnsupportedAppUsage
     private final boolean mRemovable;
     private final boolean mEmulated;
     private final boolean mAllowMassStorage;
@@ -153,6 +159,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public String getId() {
         return mId;
     }
@@ -179,6 +186,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public File getPathFile() {
         return mPath;
     }
@@ -224,6 +232,7 @@
      * @return whether mass storage is allowed
      * @hide
      */
+    @UnsupportedAppUsage
     public boolean allowMassStorage() {
         return mAllowMassStorage;
     }
@@ -234,11 +243,13 @@
      * @return maximum file size
      * @hide
      */
+    @UnsupportedAppUsage
     public long getMaxFileSize() {
         return mMaxFileSize;
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public UserHandle getOwner() {
         return mOwner;
     }
@@ -255,6 +266,7 @@
      * parse or UUID is unknown.
      * @hide
      */
+    @UnsupportedAppUsage
     public int getFatVolumeId() {
         if (mFsUuid == null || mFsUuid.length() != 9) {
             return -1;
@@ -267,6 +279,7 @@
     }
 
     /** {@hide} */
+    @UnsupportedAppUsage
     public String getUserLabel() {
         return mDescription;
     }
diff --git a/core/java/android/os/storage/VolumeInfo.java b/core/java/android/os/storage/VolumeInfo.java
index 8c77502..afd38369 100644
--- a/core/java/android/os/storage/VolumeInfo.java
+++ b/core/java/android/os/storage/VolumeInfo.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Resources;
@@ -76,8 +77,10 @@
     /** Real volume representing internal emulated storage */
     public static final String ID_EMULATED_INTERNAL = "emulated";
 
+    @UnsupportedAppUsage
     public static final int TYPE_PUBLIC = IVold.VOLUME_TYPE_PUBLIC;
     public static final int TYPE_PRIVATE = IVold.VOLUME_TYPE_PRIVATE;
+    @UnsupportedAppUsage
     public static final int TYPE_EMULATED = IVold.VOLUME_TYPE_EMULATED;
     public static final int TYPE_ASEC = IVold.VOLUME_TYPE_ASEC;
     public static final int TYPE_OBB = IVold.VOLUME_TYPE_OBB;
@@ -148,16 +151,23 @@
 
     /** vold state */
     public final String id;
+    @UnsupportedAppUsage
     public final int type;
+    @UnsupportedAppUsage
     public final DiskInfo disk;
     public final String partGuid;
     public int mountFlags = 0;
     public int mountUserId = -1;
+    @UnsupportedAppUsage
     public int state = STATE_UNMOUNTED;
     public String fsType;
+    @UnsupportedAppUsage
     public String fsUuid;
+    @UnsupportedAppUsage
     public String fsLabel;
+    @UnsupportedAppUsage
     public String path;
+    @UnsupportedAppUsage
     public String internalPath;
 
     public VolumeInfo(String id, int type, DiskInfo disk, String partGuid) {
@@ -167,6 +177,7 @@
         this.partGuid = partGuid;
     }
 
+    @UnsupportedAppUsage
     public VolumeInfo(Parcel parcel) {
         id = parcel.readString();
         type = parcel.readInt();
@@ -186,6 +197,7 @@
         internalPath = parcel.readString();
     }
 
+    @UnsupportedAppUsage
     public static @NonNull String getEnvironmentForState(int state) {
         final String envState = sStateToEnvironment.get(state);
         if (envState != null) {
@@ -207,22 +219,27 @@
         return sDescriptionComparator;
     }
 
+    @UnsupportedAppUsage
     public @NonNull String getId() {
         return id;
     }
 
+    @UnsupportedAppUsage
     public @Nullable DiskInfo getDisk() {
         return disk;
     }
 
+    @UnsupportedAppUsage
     public @Nullable String getDiskId() {
         return (disk != null) ? disk.id : null;
     }
 
+    @UnsupportedAppUsage
     public int getType() {
         return type;
     }
 
+    @UnsupportedAppUsage
     public int getState() {
         return state;
     }
@@ -231,14 +248,17 @@
         return sStateToDescrip.get(state, 0);
     }
 
+    @UnsupportedAppUsage
     public @Nullable String getFsUuid() {
         return fsUuid;
     }
 
+    @UnsupportedAppUsage
     public int getMountUserId() {
         return mountUserId;
     }
 
+    @UnsupportedAppUsage
     public @Nullable String getDescription() {
         if (ID_PRIVATE_INTERNAL.equals(id) || ID_EMULATED_INTERNAL.equals(id)) {
             return Resources.getSystem().getString(com.android.internal.R.string.storage_internal);
@@ -249,22 +269,27 @@
         }
     }
 
+    @UnsupportedAppUsage
     public boolean isMountedReadable() {
         return state == STATE_MOUNTED || state == STATE_MOUNTED_READ_ONLY;
     }
 
+    @UnsupportedAppUsage
     public boolean isMountedWritable() {
         return state == STATE_MOUNTED;
     }
 
+    @UnsupportedAppUsage
     public boolean isPrimary() {
         return (mountFlags & MOUNT_FLAG_PRIMARY) != 0;
     }
 
+    @UnsupportedAppUsage
     public boolean isPrimaryPhysical() {
         return isPrimary() && (getType() == TYPE_PUBLIC);
     }
 
+    @UnsupportedAppUsage
     public boolean isVisible() {
         return (mountFlags & MOUNT_FLAG_VISIBLE) != 0;
     }
@@ -283,18 +308,22 @@
         return isVisibleForUser(userId);
     }
 
+    @UnsupportedAppUsage
     public boolean isVisibleForWrite(int userId) {
         return isVisibleForUser(userId);
     }
 
+    @UnsupportedAppUsage
     public File getPath() {
         return (path != null) ? new File(path) : null;
     }
 
+    @UnsupportedAppUsage
     public File getInternalPath() {
         return (internalPath != null) ? new File(internalPath) : null;
     }
 
+    @UnsupportedAppUsage
     public File getPathForUser(int userId) {
         if (path == null) {
             return null;
@@ -311,6 +340,7 @@
      * Path which is accessible to apps holding
      * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}.
      */
+    @UnsupportedAppUsage
     public File getInternalPathForUser(int userId) {
         if (path == null) {
             return null;
@@ -322,6 +352,7 @@
         }
     }
 
+    @UnsupportedAppUsage
     public StorageVolume buildStorageVolume(Context context, int userId, boolean reportUnmounted) {
         final StorageManager storage = context.getSystemService(StorageManager.class);
 
@@ -382,6 +413,7 @@
                 derivedFsUuid, envState);
     }
 
+    @UnsupportedAppUsage
     public static int buildStableMtpStorageId(String fsUuid) {
         if (TextUtils.isEmpty(fsUuid)) {
             return StorageVolume.STORAGE_ID_INVALID;
@@ -408,6 +440,7 @@
      * Build an intent to browse the contents of this volume. Only valid for
      * {@link #TYPE_EMULATED} or {@link #TYPE_PUBLIC}.
      */
+    @UnsupportedAppUsage
     public @Nullable Intent buildBrowseIntent() {
         return buildBrowseIntentForUser(UserHandle.myUserId());
     }
@@ -486,6 +519,7 @@
         return id.hashCode();
     }
 
+    @UnsupportedAppUsage
     public static final Creator<VolumeInfo> CREATOR = new Creator<VolumeInfo>() {
         @Override
         public VolumeInfo createFromParcel(Parcel in) {
diff --git a/core/java/android/os/storage/VolumeRecord.java b/core/java/android/os/storage/VolumeRecord.java
index 7b20223..bc2d55a 100644
--- a/core/java/android/os/storage/VolumeRecord.java
+++ b/core/java/android/os/storage/VolumeRecord.java
@@ -16,6 +16,7 @@
 
 package android.os.storage;
 
+import android.annotation.UnsupportedAppUsage;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.util.DebugUtils;
@@ -52,6 +53,7 @@
         this.fsUuid = Preconditions.checkNotNull(fsUuid);
     }
 
+    @UnsupportedAppUsage
     public VolumeRecord(Parcel parcel) {
         type = parcel.readInt();
         fsUuid = parcel.readString();
@@ -127,6 +129,7 @@
         return fsUuid.hashCode();
     }
 
+    @UnsupportedAppUsage
     public static final Creator<VolumeRecord> CREATOR = new Creator<VolumeRecord>() {
         @Override
         public VolumeRecord createFromParcel(Parcel in) {
diff --git a/core/java/android/transition/ChangeBounds.java b/core/java/android/transition/ChangeBounds.java
index 56af3ac..c822832 100644
--- a/core/java/android/transition/ChangeBounds.java
+++ b/core/java/android/transition/ChangeBounds.java
@@ -22,6 +22,7 @@
 import android.animation.ObjectAnimator;
 import android.animation.PropertyValuesHolder;
 import android.animation.RectEvaluator;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
@@ -108,6 +109,7 @@
                 }
             };
 
+    @UnsupportedAppUsage
     private static final Property<View, PointF> BOTTOM_RIGHT_ONLY_PROPERTY =
             new Property<View, PointF>(PointF.class, "bottomRight") {
                 @Override
@@ -142,6 +144,7 @@
                 }
             };
 
+    @UnsupportedAppUsage
     private static final Property<View, PointF> POSITION_PROPERTY =
             new Property<View, PointF>(PointF.class, "position") {
                 @Override
diff --git a/core/java/android/transition/Scene.java b/core/java/android/transition/Scene.java
index 2c858cd..1bdcff9 100644
--- a/core/java/android/transition/Scene.java
+++ b/core/java/android/transition/Scene.java
@@ -16,6 +16,7 @@
 
 package android.transition;
 
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.util.SparseArray;
 import android.view.LayoutInflater;
@@ -35,7 +36,9 @@
     private int mLayoutId = -1;
     private ViewGroup mSceneRoot;
     private View mLayout; // alternative to layoutId
+    @UnsupportedAppUsage
     Runnable mEnterAction;
+    @UnsupportedAppUsage
     Runnable mExitAction;
 
     /**
@@ -195,6 +198,7 @@
      *
      * @param view The view on which the current scene is being set
      */
+    @UnsupportedAppUsage
     static void setCurrentScene(View view, Scene scene) {
         view.setTagInternal(com.android.internal.R.id.current_scene, scene);
     }
diff --git a/core/java/android/transition/Transition.java b/core/java/android/transition/Transition.java
index 255a029..b79228d 100644
--- a/core/java/android/transition/Transition.java
+++ b/core/java/android/transition/Transition.java
@@ -20,6 +20,7 @@
 import android.animation.AnimatorListenerAdapter;
 import android.animation.TimeInterpolator;
 import android.annotation.Nullable;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Path;
@@ -848,6 +849,7 @@
         return false;
     }
 
+    @UnsupportedAppUsage
     private static ArrayMap<Animator, AnimationInfo> getRunningAnimators() {
         ArrayMap<Animator, AnimationInfo> runningAnimators = sRunningAnimators.get();
         if (runningAnimators == null) {
@@ -1915,6 +1917,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     protected void end() {
         --mNumInstances;
         if (mNumInstances == 0) {
@@ -1967,6 +1970,7 @@
      *
      * @hide
      */
+    @UnsupportedAppUsage
     protected void cancel() {
         int numAnimators = mCurrentAnimators.size();
         for (int i = numAnimators - 1; i >= 0; i--) {
diff --git a/core/java/android/transition/TransitionManager.java b/core/java/android/transition/TransitionManager.java
index 45134c0..f8e8762 100644
--- a/core/java/android/transition/TransitionManager.java
+++ b/core/java/android/transition/TransitionManager.java
@@ -17,6 +17,7 @@
 package android.transition;
 
 import android.annotation.TestApi;
+import android.annotation.UnsupportedAppUsage;
 import android.content.Context;
 import android.util.ArrayMap;
 import android.util.Log;
@@ -73,9 +74,11 @@
     ArrayMap<Scene, Transition> mSceneTransitions = new ArrayMap<Scene, Transition>();
     ArrayMap<Scene, ArrayMap<Scene, Transition>> mScenePairTransitions =
             new ArrayMap<Scene, ArrayMap<Scene, Transition>>();
+    @UnsupportedAppUsage
     private static ThreadLocal<WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>>>
             sRunningTransitions =
             new ThreadLocal<WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>>>();
+    @UnsupportedAppUsage
     private static ArrayList<ViewGroup> sPendingTransitions = new ArrayList<ViewGroup>();
 
 
@@ -207,6 +210,7 @@
         }
     }
 
+    @UnsupportedAppUsage
     private static ArrayMap<ViewGroup, ArrayList<Transition>> getRunningTransitions() {
         WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>> runningTransitions =
                 sRunningTransitions.get();
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index bcd5585..79eafa8 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -109,8 +109,6 @@
             int flags, int mask);
     private static native void nativeSetWindowCrop(long transactionObj, long nativeObject,
             int l, int t, int r, int b);
-    private static native void nativeSetFinalCrop(long transactionObj, long nativeObject,
-            int l, int t, int r, int b);
     private static native void nativeSetLayerStack(long transactionObj, long nativeObject,
             int layerStack);
 
@@ -961,13 +959,6 @@
         }
     }
 
-    public void setFinalCrop(Rect crop) {
-        checkNotReleased();
-        synchronized (SurfaceControl.class) {
-            sGlobalTransaction.setFinalCrop(this, crop);
-        }
-    }
-
     public void setLayerStack(int layerStack) {
         checkNotReleased();
         synchronized(SurfaceControl.class) {
@@ -1521,18 +1512,6 @@
         }
 
         @UnsupportedAppUsage
-        public Transaction setFinalCrop(SurfaceControl sc, Rect crop) {
-            sc.checkNotReleased();
-            if (crop != null) {
-                nativeSetFinalCrop(mNativeObject, sc.mNativeObject,
-                        crop.left, crop.top, crop.right, crop.bottom);
-            } else {
-                nativeSetFinalCrop(mNativeObject, sc.mNativeObject, 0, 0, 0, 0);
-            }
-
-            return this;
-        }
-
         public Transaction setLayerStack(SurfaceControl sc, int layerStack) {
             sc.checkNotReleased();
             nativeSetLayerStack(mNativeObject, sc.mNativeObject, layerStack);
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index 6319a8f..274dd2f 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -1168,12 +1168,6 @@
         }
 
         @Override
-        public void setFinalCrop(Rect crop) {
-            super.setFinalCrop(crop);
-            mBackgroundControl.setFinalCrop(crop);
-        }
-
-        @Override
         public void setLayerStack(int layerStack) {
             super.setLayerStack(layerStack);
             mBackgroundControl.setLayerStack(layerStack);
diff --git a/core/java/android/view/inputmethod/InputMethodManagerInternal.java b/core/java/android/view/inputmethod/InputMethodManagerInternal.java
deleted file mode 100644
index 276fab9..0000000
--- a/core/java/android/view/inputmethod/InputMethodManagerInternal.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2015 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.view.inputmethod;
-
-import android.content.ComponentName;
-
-/**
- * Input method manager local system service interface.
- *
- * @hide Only for use within the system server.
- */
-public interface InputMethodManagerInternal {
-    /**
-     * Called by the power manager to tell the input method manager whether it
-     * should start watching for wake events.
-     */
-    void setInteractive(boolean interactive);
-
-    /**
-     * Hides the current input method, if visible.
-     */
-    void hideCurrentInputMethod();
-
-    /**
-     * Switches to VR InputMethod defined in the packageName of {@param componentName}.
-     */
-    void startVrInputMethodNoCheck(ComponentName componentName);
-}
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 47ce90b..4428598 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -4898,23 +4898,23 @@
                     : controller.mEndHandle;
         }
 
-        private final Magnifier.Callback mHandlesVisibilityCallback = new Magnifier.Callback() {
-            @Override
-            public void onOperationComplete() {
-                final Point magnifierTopLeft = mMagnifierAnimator.mMagnifier.getWindowCoords();
-                if (magnifierTopLeft == null) {
-                    return;
-                }
-                final Rect magnifierRect = new Rect(magnifierTopLeft.x, magnifierTopLeft.y,
-                        magnifierTopLeft.x + mMagnifierAnimator.mMagnifier.getWidth(),
-                        magnifierTopLeft.y + mMagnifierAnimator.mMagnifier.getHeight());
-                setVisible(!handleOverlapsMagnifier(HandleView.this, magnifierRect));
-                final HandleView otherHandle = getOtherSelectionHandle();
-                if (otherHandle != null) {
-                    otherHandle.setVisible(!handleOverlapsMagnifier(otherHandle, magnifierRect));
-                }
+        private void updateHandlesVisibility() {
+            final Point magnifierTopLeft = mMagnifierAnimator.mMagnifier.getPosition();
+            if (magnifierTopLeft == null) {
+                return;
             }
-        };
+            final Rect surfaceInsets =
+                    mTextView.getViewRootImpl().mWindowAttributes.surfaceInsets;
+            magnifierTopLeft.offset(-surfaceInsets.left, -surfaceInsets.top);
+            final Rect magnifierRect = new Rect(magnifierTopLeft.x, magnifierTopLeft.y,
+                    magnifierTopLeft.x + mMagnifierAnimator.mMagnifier.getWidth(),
+                    magnifierTopLeft.y + mMagnifierAnimator.mMagnifier.getHeight());
+            setVisible(!handleOverlapsMagnifier(HandleView.this, magnifierRect));
+            final HandleView otherHandle = getOtherSelectionHandle();
+            if (otherHandle != null) {
+                otherHandle.setVisible(!handleOverlapsMagnifier(otherHandle, magnifierRect));
+            }
+        }
 
         protected final void updateMagnifier(@NonNull final MotionEvent event) {
             if (mMagnifierAnimator == null) {
@@ -4929,10 +4929,9 @@
                 mRenderCursorRegardlessTiming = true;
                 mTextView.invalidateCursorPath();
                 suspendBlink();
-                mMagnifierAnimator.mMagnifier
-                        .setOnOperationCompleteCallback(mHandlesVisibilityCallback);
 
                 mMagnifierAnimator.show(showPosInView.x, showPosInView.y);
+                updateHandlesVisibility();
             } else {
                 dismissMagnifier();
             }
@@ -4940,7 +4939,6 @@
 
         protected final void dismissMagnifier() {
             if (mMagnifierAnimator != null) {
-                mMagnifierAnimator.mMagnifier.setOnOperationCompleteCallback(null);
                 mMagnifierAnimator.dismiss();
                 mRenderCursorRegardlessTiming = false;
                 resumeBlink();
diff --git a/core/java/android/widget/Magnifier.java b/core/java/android/widget/Magnifier.java
index 5734171..f82b17f 100644
--- a/core/java/android/widget/Magnifier.java
+++ b/core/java/android/widget/Magnifier.java
@@ -17,8 +17,10 @@
 package android.widget;
 
 import android.annotation.FloatRange;
+import android.annotation.IntRange;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.Px;
 import android.annotation.TestApi;
 import android.annotation.UiThread;
 import android.content.Context;
@@ -74,7 +76,7 @@
     private final int mWindowWidth;
     // The height of the window containing the magnifier.
     private final int mWindowHeight;
-    // The zoom applied to the view region copied to the magnifier window.
+    // The zoom applied to the view region copied to the magnifier view.
     private final float mZoom;
     // The width of the content that will be copied to the magnifier.
     private final int mSourceWidth;
@@ -84,6 +86,10 @@
     private final float mWindowElevation;
     // The corner radius of the window containing the magnifier.
     private final float mWindowCornerRadius;
+    // The horizontal offset between the source and window coords when #show(float, float) is used.
+    private final int mDefaultHorizontalSourceToMagnifierOffset;
+    // The vertical offset between the source and window coords when #show(float, float) is used.
+    private final int mDefaultVerticalSourceToMagnifierOffset;
     // The parent surface for the magnifier surface.
     private SurfaceInfo mParentSurface;
     // The surface where the content will be copied from.
@@ -110,17 +116,27 @@
      * Initializes a magnifier.
      *
      * @param view the view for which this magnifier is attached
+     *
+     * @see Builder
      */
     public Magnifier(@NonNull View view) {
-        mView = Preconditions.checkNotNull(view);
-        final Context context = mView.getContext();
-        mWindowWidth = context.getResources().getDimensionPixelSize(R.dimen.magnifier_width);
-        mWindowHeight = context.getResources().getDimensionPixelSize(R.dimen.magnifier_height);
-        mWindowElevation = context.getResources().getDimension(R.dimen.magnifier_elevation);
-        mWindowCornerRadius = getDeviceDefaultDialogCornerRadius();
-        mZoom = context.getResources().getFloat(R.dimen.magnifier_zoom_scale);
+        this(new Builder(view));
+    }
+
+    private Magnifier(@NonNull Builder params) {
+        // Copy params from builder.
+        mView = params.mView;
+        mWindowWidth = params.mWidth;
+        mWindowHeight = params.mHeight;
+        mZoom = params.mZoom;
         mSourceWidth = Math.round(mWindowWidth / mZoom);
         mSourceHeight = Math.round(mWindowHeight / mZoom);
+        mWindowElevation = params.mElevation;
+        mWindowCornerRadius = params.mCornerRadius;
+        mDefaultHorizontalSourceToMagnifierOffset =
+                params.mHorizontalDefaultSourceToMagnifierOffset;
+        mDefaultVerticalSourceToMagnifierOffset =
+                params.mVerticalDefaultSourceToMagnifierOffset;
         // The view's surface coordinates will not be updated until the magnifier is first shown.
         mViewCoordinatesInSurface = new int[2];
     }
@@ -130,53 +146,43 @@
     }
 
     /**
-     * Returns the device default theme dialog corner radius attribute.
-     * We retrieve this from the device default theme to avoid
-     * using the values set in the custom application themes.
-     */
-    private float getDeviceDefaultDialogCornerRadius() {
-        final Context deviceDefaultContext =
-                new ContextThemeWrapper(mView.getContext(), R.style.Theme_DeviceDefault);
-        final TypedArray ta = deviceDefaultContext.obtainStyledAttributes(
-                new int[]{android.R.attr.dialogCornerRadius});
-        final float dialogCornerRadius = ta.getDimension(0, 0);
-        ta.recycle();
-        return dialogCornerRadius;
-    }
-
-    /**
-     * Shows the magnifier on the screen.
+     * Shows the magnifier on the screen. The method takes the coordinates of the center
+     * of the content source going to be magnified and copied to the magnifier. The coordinates
+     * are relative to the top left corner of the magnified view. The magnifier will be
+     * positioned such that its center will be at the default offset from the center of the source.
+     * The default offset can be specified using the method
+     * {@link Builder#setDefaultSourceToMagnifierOffset(int, int)}. If the offset should
+     * be different across calls to this method, you should consider to use method
+     * {@link #show(float, float, float, float)} instead.
      *
-     * @param sourceCenterX horizontal coordinate of the center point of the source rectangle that
-     *        will be magnified and copied to the magnifier, relative to the view.
-     *        The parameter is clamped such that the copy rectangle fits inside [0, view width].
-     * @param sourceCenterY vertical coordinate of the center point of the source rectangle that
-     *        will be magnified and copied to the magnifier, relative to the view.
-     *        The parameter is clamped such that the copy rectangle fits inside [0, view height].
+     * @param sourceCenterX horizontal coordinate of the source center, relative to the view
+     * @param sourceCenterY vertical coordinate of the source center, relative to the view
+     *
+     * @see Builder#setDefaultSourceToMagnifierOffset(int, int)
+     * @see Builder#getDefaultHorizontalSourceToMagnifierOffset()
+     * @see Builder#getDefaultVerticalSourceToMagnifierOffset()
+     * @see #show(float, float, float, float)
      */
     public void show(@FloatRange(from = 0) float sourceCenterX,
             @FloatRange(from = 0) float sourceCenterY) {
-        final int verticalOffset = mView.getContext().getResources()
-                .getDimensionPixelSize(R.dimen.magnifier_offset);
-        show(sourceCenterX, sourceCenterY, sourceCenterX, sourceCenterY - verticalOffset);
+        show(sourceCenterX, sourceCenterY,
+                sourceCenterX + mDefaultHorizontalSourceToMagnifierOffset,
+                sourceCenterY + mDefaultVerticalSourceToMagnifierOffset);
     }
 
     /**
-     * Shows the magnifier on the screen at a position
-     * that is independent from its content position.
+     * Shows the magnifier on the screen at a position that is independent from its content
+     * position. The first two arguments represent the coordinates of the center of the
+     * content source going to be magnified and copied to the magnifier. The last two arguments
+     * represent the coordinates of the center of the magnifier itself. All four coordinates
+     * are relative to the top left corner of the magnified view. If you consider using this
+     * method such that the offset between the source center and the magnifier center coordinates
+     * remains constant, you should consider using method {@link #show(float, float)} instead.
      *
-     * @param sourceCenterX horizontal coordinate of the center point of the source rectangle that
-     *        will be magnified and copied to the magnifier, relative to the view.
-     *        The parameter is clamped such that the copy rectangle fits inside [0, view width].
-     * @param sourceCenterY vertical coordinate of the center point of the source rectangle that
-     *        will be magnified and copied to the magnifier, relative to the view.
-     *        The parameter is clamped such that the copy rectangle fits inside [0, view height].
-     * @param magnifierCenterX horizontal coordinate of the center point of the magnifier window
-     *        relative to the view. As the magnifier can be arbitrarily positioned, this can be
-     *        negative or larger than the view width.
-     * @param magnifierCenterY vertical coordinate of the center point of the magnifier window
-     *        relative to the view. As the magnifier can be arbitrarily positioned, this can be
-     *        negative or larger than the view height.
+     * @param sourceCenterX horizontal coordinate of the source center relative to the view
+     * @param sourceCenterY vertical coordinate of the source center, relative to the view
+     * @param magnifierCenterX horizontal coordinate of the magnifier center, relative to the view
+     * @param magnifierCenterY vertical coordinate of the magnifier center, relative to the view
      */
     public void show(@FloatRange(from = 0) float sourceCenterX,
             @FloatRange(from = 0) float sourceCenterY,
@@ -240,8 +246,9 @@
     }
 
     /**
-     * Forces the magnifier to update its content. It uses the previous coordinates passed to
-     * {@link #show(float, float)}. This only happens if the magnifier is currently showing.
+     * Asks the magnifier to update its content. It uses the previous coordinates passed to
+     * {@link #show(float, float)} or {@link #show(float, float, float, float)}. The
+     * method only has effect if the magnifier is currently showing.
      */
     public void update() {
         if (mWindow != null) {
@@ -253,41 +260,138 @@
     }
 
     /**
-     * @return The width of the magnifier window, in pixels.
+     * @return the width of the magnifier window, in pixels
+     * @see Magnifier.Builder#setSize(int, int)
      */
+    @Px
     public int getWidth() {
         return mWindowWidth;
     }
 
     /**
-     * @return The height of the magnifier window, in pixels.
+     * @return the height of the magnifier window, in pixels
+     * @see Magnifier.Builder#setSize(int, int)
      */
+    @Px
     public int getHeight() {
         return mWindowHeight;
     }
 
     /**
-     * @return The zoom applied to the magnified view region copied to the magnifier window.
+     * @return the initial width of the content magnified and copied to the magnifier, in pixels
+     * @see Magnifier.Builder#setSize(int, int)
+     * @see Magnifier.Builder#setZoom(float)
+     */
+    @Px
+    public int getSourceWidth() {
+        return mSourceWidth;
+    }
+
+    /**
+     * @return the initial height of the content magnified and copied to the magnifier, in pixels
+     * @see Magnifier.Builder#setSize(int, int)
+     * @see Magnifier.Builder#setZoom(float)
+     */
+    @Px
+    public int getSourceHeight() {
+        return mSourceHeight;
+    }
+
+    /**
+     * Returns the zoom to be applied to the magnified view region copied to the magnifier.
      * If the zoom is x and the magnifier window size is (width, height), the original size
-     * of the content copied in the magnifier will be (width / x, height / x).
+     * of the content being magnified will be (width / x, height / x).
+     * @return the zoom applied to the content
+     * @see Magnifier.Builder#setZoom(float)
      */
     public float getZoom() {
         return mZoom;
     }
 
     /**
-     * @hide
+     * @return the elevation set for the magnifier window, in pixels
+     * @see Magnifier.Builder#setElevation(float)
+     */
+    @Px
+    public float getElevation() {
+        return mWindowElevation;
+    }
+
+    /**
+     * @return the corner radius of the magnifier window, in pixels
+     * @see Magnifier.Builder#setCornerRadius(float)
+     */
+    @Px
+    public float getCornerRadius() {
+        return mWindowCornerRadius;
+    }
+
+    /**
+     * Returns the horizontal offset, in pixels, to be applied to the source center position
+     * to obtain the magnifier center position when {@link #show(float, float)} is called.
+     * The value is ignored when {@link #show(float, float, float, float)} is used instead.
      *
-     * @return The top left coordinates of the magnifier, relative to the parent window.
+     * @return the default horizontal offset between the source center and the magnifier
+     * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int)
+     * @see Magnifier#show(float, float)
+     */
+    @Px
+    public int getDefaultHorizontalSourceToMagnifierOffset() {
+        return mDefaultHorizontalSourceToMagnifierOffset;
+    }
+
+    /**
+     * Returns the vertical offset, in pixels, to be applied to the source center position
+     * to obtain the magnifier center position when {@link #show(float, float)} is called.
+     * The value is ignored when {@link #show(float, float, float, float)} is used instead.
+     *
+     * @return the default vertical offset between the source center and the magnifier
+     * @see Magnifier.Builder#setDefaultSourceToMagnifierOffset(int, int)
+     * @see Magnifier#show(float, float)
+     */
+    @Px
+    public int getDefaultVerticalSourceToMagnifierOffset() {
+        return mDefaultVerticalSourceToMagnifierOffset;
+    }
+
+    /**
+     * Returns the top left coordinates of the magnifier, relative to the surface of the
+     * main application window. They will be determined by the coordinates of the last
+     * {@link #show(float, float)} or {@link #show(float, float, float, float)} call, adjusted
+     * to take into account any potential clamping behavior. The method can be used immediately
+     * after a #show call to find out where the magnifier will be positioned. However, the
+     * position of the magnifier will not be updated in the same frame due to the async
+     * copying of the content copying and of the magnifier rendering.
+     * The method will return {@code null} if #show has not yet been called, or if the last
+     * operation performed was a #dismiss.
+     *
+     * @return the top left coordinates of the magnifier
      */
     @Nullable
-    public Point getWindowCoords() {
+    public Point getPosition() {
         if (mWindow == null) {
             return null;
         }
-        final Rect surfaceInsets = mView.getViewRootImpl().mWindowAttributes.surfaceInsets;
-        return new Point(mWindow.mLastDrawContentPositionX - surfaceInsets.left,
-                mWindow.mLastDrawContentPositionY - surfaceInsets.top);
+        return new Point(getCurrentClampedWindowCoordinates());
+    }
+
+    /**
+     * Returns the top left coordinates of the magnifier source (i.e. the view region going to
+     * be magnified and copied to the magnifier), relative to the surface the content is copied
+     * from. The content will be copied:
+     * - if the magnified view is a {@link SurfaceView}, from the surface backing it
+     * - otherwise, from the surface of the main application window
+     * The method will return {@code null} if #show has not yet been called, or if the last
+     * operation performed was a #dismiss.
+     *
+     * @return the top left coordinates of the magnifier source
+     */
+    @Nullable
+    public Point getSourcePosition() {
+        if (mWindow == null) {
+            return null;
+        }
+        return new Point(mPixelCopyRequestRect.left, mPixelCopyRequestRect.top);
     }
 
     /**
@@ -750,6 +854,134 @@
         }
     }
 
+    /**
+     * Builder class for {@link Magnifier} objects.
+     */
+    public static class Builder {
+        private @NonNull View mView;
+        private @Px @IntRange(from = 0) int mWidth;
+        private @Px @IntRange(from = 0) int mHeight;
+        private float mZoom;
+        private @FloatRange(from = 0f) float mElevation;
+        private @FloatRange(from = 0f) float mCornerRadius;
+        private int mHorizontalDefaultSourceToMagnifierOffset;
+        private int mVerticalDefaultSourceToMagnifierOffset;
+
+        /**
+         * Construct a new builder for {@link Magnifier} objects.
+         * @param view the view this magnifier is attached to
+         */
+        public Builder(@NonNull View view) {
+            mView = Preconditions.checkNotNull(view);
+            applyDefaults();
+        }
+
+        private void applyDefaults() {
+            final Context context = mView.getContext();
+            final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Magnifier,
+                    R.attr.magnifierStyle, 0);
+            mWidth = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierWidth, 0);
+            mHeight = a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHeight, 0);
+            mElevation = a.getDimension(R.styleable.Magnifier_magnifierElevation, 0);
+            mCornerRadius = getDeviceDefaultDialogCornerRadius();
+            mZoom = a.getFloat(R.styleable.Magnifier_magnifierZoom, 0);
+            mHorizontalDefaultSourceToMagnifierOffset =
+                    a.getDimensionPixelSize(R.styleable.Magnifier_magnifierHorizontalOffset, 0);
+            mVerticalDefaultSourceToMagnifierOffset =
+                    a.getDimensionPixelSize(R.styleable.Magnifier_magnifierVerticalOffset, 0);
+            a.recycle();
+        }
+
+        /**
+         * Returns the device default theme dialog corner radius attribute.
+         * We retrieve this from the device default theme to avoid
+         * using the values set in the custom application themes.
+         */
+        private float getDeviceDefaultDialogCornerRadius() {
+            final Context deviceDefaultContext =
+                    new ContextThemeWrapper(mView.getContext(), R.style.Theme_DeviceDefault);
+            final TypedArray ta = deviceDefaultContext.obtainStyledAttributes(
+                    new int[]{android.R.attr.dialogCornerRadius});
+            final float dialogCornerRadius = ta.getDimension(0, 0);
+            ta.recycle();
+            return dialogCornerRadius;
+        }
+
+        /**
+         * Sets the size of the magnifier window, in pixels. Defaults to (100dp, 48dp).
+         * Note that the size of the content being magnified and copied to the magnifier
+         * will be computed as (window width / zoom, window height / zoom).
+         * @param width the window width to be set
+         * @param height the window height to be set
+         */
+        public Builder setSize(@Px @IntRange(from = 0) int width,
+                @Px @IntRange(from = 0) int height) {
+            Preconditions.checkArgumentPositive(width, "Width should be positive");
+            Preconditions.checkArgumentPositive(height, "Height should be positive");
+            mWidth = width;
+            mHeight = height;
+            return this;
+        }
+
+        /**
+         * Sets the zoom to be applied to the chosen content before being copied to the magnifier.
+         * A content of size (content_width, content_height) will be magnified to
+         * (content_width * zoom, content_height * zoom), which will coincide with the size
+         * of the magnifier. A zoom of 1 will translate to no magnification (the content will
+         * be just copied to the magnifier with no scaling). The zoom defaults to 1.25.
+         * @param zoom the zoom to be set
+         */
+        public Builder setZoom(@FloatRange(from = 0f) float zoom) {
+            Preconditions.checkArgumentPositive(zoom, "Zoom should be positive");
+            mZoom = zoom;
+            return this;
+        }
+
+        /**
+         * Sets the elevation of the magnifier window, in pixels. Defaults to 4dp.
+         * @param elevation the elevation to be set
+         */
+        public Builder setElevation(@Px @FloatRange(from = 0) float elevation) {
+            Preconditions.checkArgumentNonNegative(elevation, "Elevation should be non-negative");
+            mElevation = elevation;
+            return this;
+        }
+
+        /**
+         * Sets the corner radius of the magnifier window, in pixels.
+         * Defaults to the corner radius defined in the device default theme.
+         * @param cornerRadius the corner radius to be set
+         */
+        public Builder setCornerRadius(@Px @FloatRange(from = 0) float cornerRadius) {
+            Preconditions.checkArgumentNonNegative(cornerRadius,
+                    "Corner radius should be non-negative");
+            mCornerRadius = cornerRadius;
+            return this;
+        }
+
+        /**
+         * Sets an offset, in pixels, that should be added to the content source center to obtain
+         * the position of the magnifier window, when the {@link #show(float, float)}
+         * method is called. The offset is ignored when {@link #show(float, float, float, float)}
+         * is used. The offset can be negative, and it defaults to (0dp, -42dp).
+         * @param horizontalOffset the horizontal component of the offset
+         * @param verticalOffset the vertical component of the offset
+         */
+        public Builder setDefaultSourceToMagnifierOffset(@Px int horizontalOffset,
+                @Px int verticalOffset) {
+            mHorizontalDefaultSourceToMagnifierOffset = horizontalOffset;
+            mVerticalDefaultSourceToMagnifierOffset = verticalOffset;
+            return this;
+        }
+
+        /**
+         * Builds a {@link Magnifier} instance based on the configuration of this {@link Builder}.
+         */
+        public @NonNull Magnifier build() {
+            return new Magnifier(this);
+        }
+    }
+
     // The rest of the file consists of test APIs.
 
     /**
@@ -788,23 +1020,6 @@
     }
 
     /**
-     * @return the position of the magnifier window relative to the screen
-     *
-     * @hide
-     */
-    @TestApi
-    public Rect getWindowPositionOnScreen() {
-        final int[] viewLocationOnScreen = new int[2];
-        mView.getLocationOnScreen(viewLocationOnScreen);
-        final int[] viewLocationInSurface = new int[2];
-        mView.getLocationInSurface(viewLocationInSurface);
-
-        final int left = mWindowCoords.x + viewLocationOnScreen[0] - viewLocationInSurface[0];
-        final int top = mWindowCoords.y + viewLocationOnScreen[1] - viewLocationInSurface[1];
-        return new Rect(left, top, left + mWindowWidth, top + mWindowHeight);
-    }
-
-    /**
      * @return the size of the magnifier window in dp
      *
      * @hide
diff --git a/core/java/com/android/internal/util/Preconditions.java b/core/java/com/android/internal/util/Preconditions.java
index 91c76af..2c6a0e0 100644
--- a/core/java/com/android/internal/util/Preconditions.java
+++ b/core/java/com/android/internal/util/Preconditions.java
@@ -192,7 +192,7 @@
     }
 
     /**
-     * Ensures that that the argument numeric value is non-negative.
+     * Ensures that that the argument numeric value is non-negative (greater than or equal to 0).
      *
      * @param value a numeric int value
      * @param errorMessage the exception message to use if the check fails
@@ -209,7 +209,7 @@
     }
 
     /**
-     * Ensures that that the argument numeric value is non-negative.
+     * Ensures that that the argument numeric value is non-negative (greater than or equal to 0).
      *
      * @param value a numeric int value
      *
@@ -225,7 +225,7 @@
     }
 
     /**
-     * Ensures that that the argument numeric value is non-negative.
+     * Ensures that that the argument numeric value is non-negative (greater than or equal to 0).
      *
      * @param value a numeric long value
      * @return the validated numeric value
@@ -240,7 +240,7 @@
     }
 
     /**
-     * Ensures that that the argument numeric value is non-negative.
+     * Ensures that that the argument numeric value is non-negative (greater than or equal to 0).
      *
      * @param value a numeric long value
      * @param errorMessage the exception message to use if the check fails
@@ -256,7 +256,7 @@
     }
 
     /**
-     * Ensures that that the argument numeric value is positive.
+     * Ensures that that the argument numeric value is positive (greater than 0).
      *
      * @param value a numeric int value
      * @param errorMessage the exception message to use if the check fails
@@ -272,6 +272,36 @@
     }
 
     /**
+     * Ensures that the argument floating point value is non-negative (greater than or equal to 0).
+     * @param value a floating point value
+     * @param errorMessage the exteption message to use if the check fails
+     * @return the validated numeric value
+     * @throws IllegalArgumentException if {@code value} was negative
+     */
+    public static float checkArgumentNonNegative(final float value, final String errorMessage) {
+        if (value < 0) {
+            throw new IllegalArgumentException(errorMessage);
+        }
+
+        return value;
+    }
+
+    /**
+     * Ensures that the argument floating point value is positive (greater than 0).
+     * @param value a floating point value
+     * @param errorMessage the exteption message to use if the check fails
+     * @return the validated numeric value
+     * @throws IllegalArgumentException if {@code value} was not positive
+     */
+    public static float checkArgumentPositive(final float value, final String errorMessage) {
+        if (value <= 0) {
+            throw new IllegalArgumentException(errorMessage);
+        }
+
+        return value;
+    }
+
+    /**
      * Ensures that the argument floating point value is a finite number.
      *
      * <p>A finite number is defined to be both representable (that is, not NaN) and
diff --git a/core/java/com/android/internal/view/IInputMethodManager.aidl b/core/java/com/android/internal/view/IInputMethodManager.aidl
index 06726bd..4b004e2 100644
--- a/core/java/com/android/internal/view/IInputMethodManager.aidl
+++ b/core/java/com/android/internal/view/IInputMethodManager.aidl
@@ -43,9 +43,6 @@
     // TODO: We should change the return type from List to List<Parcelable>
     // Currently there is a bug that aidl doesn't accept List<Parcelable>
     List getShortcutInputMethodsAndSubtypes();
-    void addClient(in IInputMethodClient client,
-            in IInputContext inputContext, int uid, int pid);
-    void removeClient(in IInputMethodClient client);
 
     void finishInput(in IInputMethodClient client);
     boolean showSoftInput(in IInputMethodClient client, int flags,
diff --git a/core/jni/android/graphics/ColorFilter.cpp b/core/jni/android/graphics/ColorFilter.cpp
index 6ebf35c..3fcedd0 100644
--- a/core/jni/android/graphics/ColorFilter.cpp
+++ b/core/jni/android/graphics/ColorFilter.cpp
@@ -22,6 +22,8 @@
 #include "SkColorFilter.h"
 #include "SkColorMatrixFilter.h"
 
+#include <Caches.h>
+
 namespace android {
 
 using namespace uirenderer;
diff --git a/core/jni/android/graphics/Matrix.cpp b/core/jni/android/graphics/Matrix.cpp
index 755fcfb..f8bb77a 100644
--- a/core/jni/android/graphics/Matrix.cpp
+++ b/core/jni/android/graphics/Matrix.cpp
@@ -20,6 +20,7 @@
 #include "SkMatrix.h"
 #include "core_jni_helpers.h"
 
+#include <Caches.h>
 #include <jni.h>
 
 namespace android {
diff --git a/core/jni/android/graphics/Shader.cpp b/core/jni/android/graphics/Shader.cpp
index 68f5bef..cff7720 100644
--- a/core/jni/android/graphics/Shader.cpp
+++ b/core/jni/android/graphics/Shader.cpp
@@ -6,6 +6,7 @@
 #include "SkBlendMode.h"
 #include "core_jni_helpers.h"
 
+#include <Caches.h>
 #include <jni.h>
 
 using namespace android::uirenderer;
diff --git a/core/jni/android/graphics/SurfaceTexture.cpp b/core/jni/android/graphics/SurfaceTexture.cpp
index 3e464c6..d098a35 100644
--- a/core/jni/android/graphics/SurfaceTexture.cpp
+++ b/core/jni/android/graphics/SurfaceTexture.cpp
@@ -36,7 +36,6 @@
 #include "jni.h"
 #include <nativehelper/JNIHelp.h>
 #include <nativehelper/ScopedLocalRef.h>
-#include "surfacetexture/SurfaceTexture.h"
 
 // ----------------------------------------------------------------------------
 
@@ -81,10 +80,10 @@
 // ----------------------------------------------------------------------------
 
 static void SurfaceTexture_setSurfaceTexture(JNIEnv* env, jobject thiz,
-        const sp<SurfaceTexture>& surfaceTexture)
+        const sp<GLConsumer>& surfaceTexture)
 {
-    SurfaceTexture* const p =
-        (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
+    GLConsumer* const p =
+        (GLConsumer*)env->GetLongField(thiz, fields.surfaceTexture);
     if (surfaceTexture.get()) {
         surfaceTexture->incStrong((void*)SurfaceTexture_setSurfaceTexture);
     }
@@ -109,10 +108,10 @@
 }
 
 static void SurfaceTexture_setFrameAvailableListener(JNIEnv* env,
-        jobject thiz, sp<SurfaceTexture::FrameAvailableListener> listener)
+        jobject thiz, sp<GLConsumer::FrameAvailableListener> listener)
 {
-    SurfaceTexture::FrameAvailableListener* const p =
-        (SurfaceTexture::FrameAvailableListener*)
+    GLConsumer::FrameAvailableListener* const p =
+        (GLConsumer::FrameAvailableListener*)
             env->GetLongField(thiz, fields.frameAvailableListener);
     if (listener.get()) {
         listener->incStrong((void*)SurfaceTexture_setSurfaceTexture);
@@ -123,8 +122,8 @@
     env->SetLongField(thiz, fields.frameAvailableListener, (jlong)listener.get());
 }
 
-sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
-    return (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
+sp<GLConsumer> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
+    return (GLConsumer*)env->GetLongField(thiz, fields.surfaceTexture);
 }
 
 sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz) {
@@ -132,7 +131,7 @@
 }
 
 sp<ANativeWindow> android_SurfaceTexture_getNativeWindow(JNIEnv* env, jobject thiz) {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, thiz));
     sp<Surface> surfaceTextureClient(surfaceTexture != NULL ? new Surface(producer) : NULL);
     return surfaceTextureClient;
@@ -145,7 +144,7 @@
 
 // ----------------------------------------------------------------------------
 
-class JNISurfaceTextureContext : public SurfaceTexture::FrameAvailableListener
+class JNISurfaceTextureContext : public GLConsumer::FrameAvailableListener
 {
 public:
     JNISurfaceTextureContext(JNIEnv* env, jobject weakThiz, jclass clazz);
@@ -267,12 +266,12 @@
         consumer->setMaxBufferCount(1);
     }
 
-    sp<SurfaceTexture> surfaceTexture;
+    sp<GLConsumer> surfaceTexture;
     if (isDetached) {
-        surfaceTexture = new SurfaceTexture(consumer, GL_TEXTURE_EXTERNAL_OES,
+        surfaceTexture = new GLConsumer(consumer, GL_TEXTURE_EXTERNAL_OES,
                 true, !singleBufferMode);
     } else {
-        surfaceTexture = new SurfaceTexture(consumer, texName,
+        surfaceTexture = new GLConsumer(consumer, texName,
                 GL_TEXTURE_EXTERNAL_OES, true, !singleBufferMode);
     }
 
@@ -307,7 +306,7 @@
 
 static void SurfaceTexture_finalize(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     surfaceTexture->setFrameAvailableListener(0);
     SurfaceTexture_setFrameAvailableListener(env, thiz, 0);
     SurfaceTexture_setSurfaceTexture(env, thiz, 0);
@@ -316,13 +315,13 @@
 
 static void SurfaceTexture_setDefaultBufferSize(
         JNIEnv* env, jobject thiz, jint width, jint height) {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     surfaceTexture->setDefaultBufferSize(width, height);
 }
 
 static void SurfaceTexture_updateTexImage(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     status_t err = surfaceTexture->updateTexImage();
     if (err == INVALID_OPERATION) {
         jniThrowException(env, IllegalStateException, "Unable to update texture contents (see "
@@ -334,7 +333,7 @@
 
 static void SurfaceTexture_releaseTexImage(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     status_t err = surfaceTexture->releaseTexImage();
     if (err == INVALID_OPERATION) {
         jniThrowException(env, IllegalStateException, "Unable to release texture contents (see "
@@ -346,20 +345,20 @@
 
 static jint SurfaceTexture_detachFromGLContext(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     return surfaceTexture->detachFromContext();
 }
 
 static jint SurfaceTexture_attachToGLContext(JNIEnv* env, jobject thiz, jint tex)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     return surfaceTexture->attachToContext((GLuint)tex);
 }
 
 static void SurfaceTexture_getTransformMatrix(JNIEnv* env, jobject thiz,
         jfloatArray jmtx)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     float* mtx = env->GetFloatArrayElements(jmtx, NULL);
     surfaceTexture->getTransformMatrix(mtx);
     env->ReleaseFloatArrayElements(jmtx, mtx, 0);
@@ -367,19 +366,19 @@
 
 static jlong SurfaceTexture_getTimestamp(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     return surfaceTexture->getTimestamp();
 }
 
 static void SurfaceTexture_release(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     surfaceTexture->abandon();
 }
 
 static jboolean SurfaceTexture_isReleased(JNIEnv* env, jobject thiz)
 {
-    sp<SurfaceTexture> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, thiz));
     return surfaceTexture->isAbandoned();
 }
 
diff --git a/core/jni/android_os_Debug.cpp b/core/jni/android_os_Debug.cpp
index 26367c2..ccbe0ee 100644
--- a/core/jni/android_os_Debug.cpp
+++ b/core/jni/android_os_Debug.cpp
@@ -343,51 +343,47 @@
                 }
                 is_swappable = true;
             } else if (strncmp(name, "/dev/", 5) == 0) {
+                whichHeap = HEAP_UNKNOWN_DEV;
                 if (strncmp(name, "/dev/kgsl-3d0", 13) == 0) {
                     whichHeap = HEAP_GL_DEV;
-                } else if (strncmp(name, "/dev/ashmem", 11) == 0) {
-                    if (strncmp(name, "/dev/ashmem/dalvik-", 19) == 0) {
-                        whichHeap = HEAP_DALVIK_OTHER;
-                        if (strstr(name, "/dev/ashmem/dalvik-LinearAlloc") == name) {
-                            subHeap = HEAP_DALVIK_OTHER_LINEARALLOC;
-                        } else if ((strstr(name, "/dev/ashmem/dalvik-alloc space") == name) ||
-                                   (strstr(name, "/dev/ashmem/dalvik-main space") == name)) {
-                            // This is the regular Dalvik heap.
-                            whichHeap = HEAP_DALVIK;
-                            subHeap = HEAP_DALVIK_NORMAL;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-large object space") == name ||
-                                   strstr(name, "/dev/ashmem/dalvik-free list large object space")
-                                       == name) {
-                            whichHeap = HEAP_DALVIK;
-                            subHeap = HEAP_DALVIK_LARGE;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-non moving space") == name) {
-                            whichHeap = HEAP_DALVIK;
-                            subHeap = HEAP_DALVIK_NON_MOVING;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-zygote space") == name) {
-                            whichHeap = HEAP_DALVIK;
-                            subHeap = HEAP_DALVIK_ZYGOTE;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-indirect ref") == name) {
-                            subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-jit-code-cache") == name ||
-                                   strstr(name, "/dev/ashmem/dalvik-data-code-cache") == name) {
-                            subHeap = HEAP_DALVIK_OTHER_CODE_CACHE;
-                        } else if (strstr(name, "/dev/ashmem/dalvik-CompilerMetadata") == name) {
-                            subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
-                        } else {
-                            subHeap = HEAP_DALVIK_OTHER_ACCOUNTING;  // Default to accounting.
-                        }
-                    } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
-                        whichHeap = HEAP_CURSOR;
-                    } else if (strncmp(name, "/dev/ashmem/libc malloc", 23) == 0) {
-                        whichHeap = HEAP_NATIVE;
-                    } else {
-                        whichHeap = HEAP_ASHMEM;
-                    }
-                } else {
-                    whichHeap = HEAP_UNKNOWN_DEV;
+                } else if (strncmp(name, "/dev/ashmem/CursorWindow", 24) == 0) {
+                    whichHeap = HEAP_CURSOR;
+                } else if (strncmp(name, "/dev/ashmem", 11)) {
+                    whichHeap = HEAP_ASHMEM;
                 }
             } else if (strncmp(name, "[anon:", 6) == 0) {
                 whichHeap = HEAP_UNKNOWN;
+                if (strncmp(name, "[anon:dalvik-", 13) == 0) {
+                    whichHeap = HEAP_DALVIK_OTHER;
+                    if (strstr(name, "[anon:dalvik-LinearAlloc") == name) {
+                        subHeap = HEAP_DALVIK_OTHER_LINEARALLOC;
+                    } else if ((strstr(name, "[anon:dalvik-alloc space") == name) ||
+                               (strstr(name, "[anon:dalvik-main space") == name)) {
+                        // This is the regular Dalvik heap.
+                        whichHeap = HEAP_DALVIK;
+                        subHeap = HEAP_DALVIK_NORMAL;
+                    } else if (strstr(name, "[anon:dalvik-large object space") == name ||
+                               strstr(name, "[anon:dalvik-free list large object space")
+                                   == name) {
+                        whichHeap = HEAP_DALVIK;
+                        subHeap = HEAP_DALVIK_LARGE;
+                    } else if (strstr(name, "[anon:dalvik-non moving space") == name) {
+                        whichHeap = HEAP_DALVIK;
+                        subHeap = HEAP_DALVIK_NON_MOVING;
+                    } else if (strstr(name, "[anon:dalvik-zygote space") == name) {
+                        whichHeap = HEAP_DALVIK;
+                        subHeap = HEAP_DALVIK_ZYGOTE;
+                    } else if (strstr(name, "[anon:dalvik-indirect ref") == name) {
+                        subHeap = HEAP_DALVIK_OTHER_INDIRECT_REFERENCE_TABLE;
+                    } else if (strstr(name, "[anon:dalvik-jit-code-cache") == name ||
+                               strstr(name, "[anon:dalvik-data-code-cache") == name) {
+                        subHeap = HEAP_DALVIK_OTHER_CODE_CACHE;
+                    } else if (strstr(name, "[anon:dalvik-CompilerMetadata") == name) {
+                        subHeap = HEAP_DALVIK_OTHER_COMPILER_METADATA;
+                    } else {
+                        subHeap = HEAP_DALVIK_OTHER_ACCOUNTING;  // Default to accounting.
+                    }
+                }
             } else if (nameLen > 0) {
                 whichHeap = HEAP_UNKNOWN_MAP;
             } else if (start == prevEnd && prevHeap == HEAP_SO) {
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index 02d6adb..743b9f6 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -449,16 +449,6 @@
     transaction->setCrop_legacy(ctrl, crop);
 }
 
-static void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong transactionObj,
-        jlong nativeObject,
-        jint l, jint t, jint r, jint b) {
-    auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
-
-    SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
-    Rect crop(l, t, r, b);
-    transaction->setFinalCrop_legacy(ctrl, crop);
-}
-
 static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong transactionObj,
         jlong nativeObject, jint layerStack) {
     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
@@ -967,8 +957,6 @@
             (void*)nativeSetFlags },
     {"nativeSetWindowCrop", "(JJIIII)V",
             (void*)nativeSetWindowCrop },
-    {"nativeSetFinalCrop", "(JJIIII)V",
-            (void*)nativeSetFinalCrop },
     {"nativeSetLayerStack", "(JJI)V",
             (void*)nativeSetLayerStack },
     {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;",
diff --git a/core/jni/android_view_TextureLayer.cpp b/core/jni/android_view_TextureLayer.cpp
index 1ccb6a8..d3a447f 100644
--- a/core/jni/android_view_TextureLayer.cpp
+++ b/core/jni/android_view_TextureLayer.cpp
@@ -67,7 +67,8 @@
 static void TextureLayer_setSurfaceTexture(JNIEnv* env, jobject clazz,
         jlong layerUpdaterPtr, jobject surface) {
     DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerUpdaterPtr);
-    layer->setSurfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface));
+    sp<GLConsumer> surfaceTexture(SurfaceTexture_getSurfaceTexture(env, surface));
+    layer->setSurfaceTexture(surfaceTexture);
 }
 
 static void TextureLayer_updateSurfaceTexture(JNIEnv* env, jobject clazz,
diff --git a/core/jni/include/android_runtime/android_graphics_SurfaceTexture.h b/core/jni/include/android_runtime/android_graphics_SurfaceTexture.h
index 0ad2587..c534d4b 100644
--- a/core/jni/include/android_runtime/android_graphics_SurfaceTexture.h
+++ b/core/jni/include/android_runtime/android_graphics_SurfaceTexture.h
@@ -23,14 +23,14 @@
 
 namespace android {
 
+class GLConsumer;
 class IGraphicBufferProducer;
-class SurfaceTexture;
 
 extern sp<ANativeWindow> android_SurfaceTexture_getNativeWindow(JNIEnv* env, jobject thiz);
 extern bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz);
 
-/* Gets the underlying C++ SurfaceTexture object from a SurfaceTexture Java object. */
-extern sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz);
+/* Gets the underlying GLConsumer from a SurfaceTexture Java object. */
+extern sp<GLConsumer> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz);
 
 /* gets the producer end of the SurfaceTexture */
 extern sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz);
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 2f710bf..64a9e6d 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -759,6 +759,8 @@
         <attr name="contextPopupMenuStyle" format="reference" />
         <!-- Default StackView style. -->
         <attr name="stackViewStyle" format="reference" />
+        <!-- Magnifier style. -->
+        <attr name="magnifierStyle" format="reference" />
 
         <!-- Default style for the FragmentBreadCrumbs widget. This widget is deprecated
              starting in API level 21 ({@link android.os.Build.VERSION_CODES#.L}). -->
@@ -8921,4 +8923,13 @@
     </declare-styleable>
 
     <attr name="lockPatternStyle" format="reference" />
+
+    <declare-styleable name="Magnifier">
+        <attr name="magnifierWidth" format="dimension" />
+        <attr name="magnifierHeight" format="dimension" />
+        <attr name="magnifierZoom" format="float" />
+        <attr name="magnifierElevation" format="dimension" />
+        <attr name="magnifierVerticalOffset" format="dimension" />
+        <attr name="magnifierHorizontalOffset" format="dimension" />
+    </declare-styleable>
 </resources>
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 74663c9..3c0e51e 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -1388,6 +1388,8 @@
          instantiates items without it.-->
     <attr name="appComponentFactory" format="string" />
 
+    <attr name="usesNonSdkApi" format="boolean" />
+
     <!-- The <code>manifest</code> tag is the root of an
          <code>AndroidManifest.xml</code> file,
          describing the contents of an Android package (.apk) file.  One
@@ -1561,6 +1563,9 @@
 
         <attr name="appComponentFactory" />
 
+        <!-- Declares that this application should be invoked without non-SDK API enforcement -->
+        <attr name="usesNonSdkApi" />
+
     </declare-styleable>
     <!-- The <code>permission</code> tag declares a security permission that can be
          used to control access from other packages to specific components or
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 836e824..c8ad31f 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -1282,6 +1282,10 @@
          that can be set by the user. -->
     <integer name="config_screenBrightnessDoze">1</integer>
 
+    <!-- Delay that allows some content to arrive at the display before switching
+         from DOZE to ON. -->
+    <integer name="config_wakeUpDelayDoze">0</integer>
+
     <!-- Whether or not to skip the initial brightness ramps when the display transitions to
          STATE_ON. Setting this to true will skip the brightness ramp to the last stored active
          brightness value and will repeat for the following ramp if autobrightness is enabled. -->
@@ -3363,22 +3367,22 @@
     <bool name="config_sendPackageName">false</bool>
 
     <!-- Name for the set of keys associating package names -->
-    <string name="config_helpPackageNameKey" translatable="false"></string>
+    <string name="config_help_package_name_key" translatable="false"></string>
 
     <!-- Name for the set of values of package names -->
-    <string name="config_helpPackageNameValue" translatable="false"></string>
+    <string name="config_help_package_name_value" translatable="false"></string>
 
     <!-- Intent key for the package name keys -->
-    <string name="config_helpIntentExtraKey" translatable="false"></string>
+    <string name="config_help_intent_extra_key" translatable="false"></string>
 
     <!-- Intent key for package name values -->
-    <string name="config_helpIntentNameKey" translatable="false"></string>
+    <string name="config_help_intent_name_key" translatable="false"></string>
 
     <!-- Intent key for the package name keys -->
-    <string name="config_feedbackIntentExtraKey" translatable="false"></string>
+    <string name="config_feedback_intent_extra_key" translatable="false"></string>
 
     <!-- Intent key for package name values -->
-    <string name="config_feedbackIntentNameKey" translatable="false"></string>
+    <string name="config_feedback_intent_name_key" translatable="false"></string>
 
     <!-- The apps that need to be hidden when they are disabled -->
     <string-array name="config_hideWhenDisabled_packageNames"></string-array>
diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml
index 73cb59e..097ce44 100644
--- a/core/res/res/values/dimens.xml
+++ b/core/res/res/values/dimens.xml
@@ -564,8 +564,9 @@
     <dimen name="magnifier_width">100dp</dimen>
     <dimen name="magnifier_height">48dp</dimen>
     <dimen name="magnifier_elevation">4dp</dimen>
-    <dimen name="magnifier_offset">42dp</dimen>
-    <item type="dimen" format="float" name="magnifier_zoom_scale">1.25</item>
+    <dimen name="magnifier_vertical_offset">-42dp</dimen>
+    <dimen name="magnifier_horizontal_offset">0dp</dimen>
+    <item type="dimen" format="float" name="magnifier_zoom">1.25</item>
 
     <dimen name="chooser_grid_padding">0dp</dimen>
     <!-- Spacing around the background change frome service to non-service -->
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index e25bb79..fa31dce 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2911,6 +2911,11 @@
         <public name="supportsAmbientMode" />
     </public-group>
 
+    <public-group type="attr" first-id="0x0101058d">
+        <!-- @hide For use by platform and tools only. Developers should not specify this value. -->
+        <public name="usesNonSdkApi" />
+    </public-group>
+
     <public-group type="style" first-id="0x010302e2">
     </public-group>
 
@@ -2918,6 +2923,23 @@
     </public-group>
 
     <public-group type="string" first-id="0x0104001b">
+        <!-- @hide @SystemApi -->
+        <public name="config_help_package_name_key" />
+        <!-- @hide @SystemApi -->
+        <public name="config_help_package_name_value" />
+        <!-- @hide @SystemApi -->
+        <public name="config_help_intent_extra_key" />
+        <!-- @hide @SystemApi -->
+        <public name="config_help_intent_name_key" />
+        <!-- @hide @SystemApi -->
+        <public name="config_feedback_intent_extra_key" />
+        <!-- @hide @SystemApi -->
+        <public name="config_feedback_intent_name_key" />
+    </public-group>
+
+    <public-group type="bool" first-id="0x01110000">
+        <!-- @hide @SystemApi -->
+        <public name="config_sendPackageName" />
     </public-group>
 
   <!-- ===============================================================
diff --git a/core/res/res/values/styles.xml b/core/res/res/values/styles.xml
index e1db71f..fafcf93 100644
--- a/core/res/res/values/styles.xml
+++ b/core/res/res/values/styles.xml
@@ -799,6 +799,15 @@
         <item name="textOff">@string/capital_off</item>
     </style>
 
+    <style name="Widget.Magnifier">
+        <item name="magnifierWidth">@dimen/magnifier_width</item>
+        <item name="magnifierHeight">@dimen/magnifier_height</item>
+        <item name="magnifierZoom">@dimen/magnifier_zoom</item>
+        <item name="magnifierElevation">@dimen/magnifier_elevation</item>
+        <item name="magnifierVerticalOffset">@dimen/magnifier_vertical_offset</item>
+        <item name="magnifierHorizontalOffset">@dimen/magnifier_horizontal_offset</item>
+    </style>
+
     <!-- Text Appearances -->
     <eat-comment />
 
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index c6387f0..b82f9e5 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -2629,8 +2629,16 @@
   <java-symbol type="dimen" name="magnifier_width" />
   <java-symbol type="dimen" name="magnifier_height" />
   <java-symbol type="dimen" name="magnifier_elevation" />
-  <java-symbol type="dimen" name="magnifier_zoom_scale" />
-  <java-symbol type="dimen" name="magnifier_offset" />
+  <java-symbol type="dimen" name="magnifier_zoom" />
+  <java-symbol type="dimen" name="magnifier_vertical_offset" />
+  <java-symbol type="dimen" name="magnifier_horizontal_offset" />
+  <java-symbol type="attr" name="magnifierWidth" />
+  <java-symbol type="attr" name="magnifierHeight" />
+  <java-symbol type="attr" name="magnifierElevation" />
+  <java-symbol type="attr" name="magnifierZoom" />
+  <java-symbol type="attr" name="magnifierVerticalOffset" />
+  <java-symbol type="attr" name="magnifierHorizontalOffset" />
+  <java-symbol type="attr" name="magnifierStyle" />
 
   <java-symbol type="string" name="date_picker_prev_month_button" />
   <java-symbol type="string" name="date_picker_next_month_button" />
@@ -3256,12 +3264,12 @@
   <java-symbol type="integer" name="default_data_warning_level_mb" />
   <java-symbol type="bool" name="config_useVideoPauseWorkaround" />
   <java-symbol type="bool" name="config_sendPackageName" />
-  <java-symbol type="string" name="config_helpPackageNameKey" />
-  <java-symbol type="string" name="config_helpPackageNameValue" />
-  <java-symbol type="string" name="config_helpIntentExtraKey" />
-  <java-symbol type="string" name="config_helpIntentNameKey" />
-  <java-symbol type="string" name="config_feedbackIntentExtraKey" />
-  <java-symbol type="string" name="config_feedbackIntentNameKey" />
+  <java-symbol type="string" name="config_help_package_name_key" />
+  <java-symbol type="string" name="config_help_package_name_value" />
+  <java-symbol type="string" name="config_help_intent_extra_key" />
+  <java-symbol type="string" name="config_help_intent_name_key" />
+  <java-symbol type="string" name="config_feedback_intent_extra_key" />
+  <java-symbol type="string" name="config_feedback_intent_name_key" />
 
   <java-symbol type="array" name="config_hideWhenDisabled_packageNames" />
 
@@ -3455,4 +3463,5 @@
   <java-symbol type="string" name="config_misprovisionedBrandValue" />
 
   <java-symbol type="integer" name="db_wal_truncate_size" />
+  <java-symbol type="integer" name="config_wakeUpDelayDoze" />
 </resources>
diff --git a/core/res/res/values/themes.xml b/core/res/res/values/themes.xml
index 090f9af..3937af5 100644
--- a/core/res/res/values/themes.xml
+++ b/core/res/res/values/themes.xml
@@ -310,6 +310,7 @@
         <item name="activityChooserViewStyle">@style/Widget.ActivityChooserView</item>
         <item name="fragmentBreadCrumbsStyle">@style/Widget.FragmentBreadCrumbs</item>
         <item name="contextPopupMenuStyle">?attr/popupMenuStyle</item>
+        <item name="magnifierStyle">@style/Widget.Magnifier</item>
 
         <!-- Preference styles -->
         <item name="preferenceScreenStyle">@style/Preference.PreferenceScreen</item>
diff --git a/data/keyboards/Vendor_18d1_Product_2c40.kl b/data/keyboards/Vendor_18d1_Product_2c40.kl
index 6efde4f..2b42f87 100644
--- a/data/keyboards/Vendor_18d1_Product_2c40.kl
+++ b/data/keyboards/Vendor_18d1_Product_2c40.kl
@@ -20,13 +20,9 @@
 key 308 BUTTON_Y
 key 310 BUTTON_L1
 key 311 BUTTON_R1
-key 316 BUTTON_MODE
 key 317 BUTTON_THUMBL
 key 318 BUTTON_THUMBR
 
-key 158 BACK
-key 172 HOME
-
 axis 0x00 X
 axis 0x01 Y
 axis 0x02 Z
@@ -40,3 +36,14 @@
 led 0x01 CONTROLLER_2
 led 0x02 CONTROLLER_3
 led 0x03 CONTROLLER_4
+
+# The next 2 buttons do not follow Linux standard because this behaviour was specified by the UX
+# Left arrow to the immediate left of the power button
+key 158 BACK
+# Circle to the immediate right of the power button
+key 172 HOME
+
+
+# Mapping according to https://www.kernel.org/doc/Documentation/input/gamepad.txt
+# Power button
+key 316 BUTTON_MODE
diff --git a/graphics/java/android/graphics/drawable/ColorStateListDrawable.java b/graphics/java/android/graphics/drawable/ColorStateListDrawable.java
index 40ba5d1..c0c6a4f 100644
--- a/graphics/java/android/graphics/drawable/ColorStateListDrawable.java
+++ b/graphics/java/android/graphics/drawable/ColorStateListDrawable.java
@@ -33,6 +33,7 @@
 public class ColorStateListDrawable extends Drawable implements Drawable.Callback {
     private ColorDrawable mColorDrawable;
     private ColorStateListDrawableState mState;
+    private boolean mMutated = false;
 
     public ColorStateListDrawable() {
         mState = new ColorStateListDrawableState();
@@ -139,7 +140,7 @@
             int color = mState.mColor.getColorForState(state, mState.mColor.getDefaultColor());
 
             if (mState.mAlpha != -1) {
-                color = color & 0xFFFFFF | MathUtils.constrain(mState.mAlpha, 0, 255) << 24;
+                color = (color & 0xFFFFFF) | MathUtils.constrain(mState.mAlpha, 0, 255) << 24;
             }
 
             if (color != mColorDrawable.getColor()) {
@@ -183,6 +184,8 @@
 
     @Override
     public ConstantState getConstantState() {
+        mState.mChangingConfigurations = mState.mChangingConfigurations
+                | (getChangingConfigurations() & ~mState.getChangingConfigurations());
         return mState;
     }
 
@@ -200,6 +203,29 @@
         }
     }
 
+    @Override
+    public int getChangingConfigurations() {
+        return super.getChangingConfigurations() | mState.getChangingConfigurations();
+    }
+
+    @Override
+    public Drawable mutate() {
+        if (!mMutated && super.mutate() == this) {
+            mState = new ColorStateListDrawableState(mState);
+            mMutated = true;
+        }
+        return this;
+    }
+
+    /**
+     * @hide
+     */
+    @Override
+    public void clearMutated() {
+        super.clearMutated();
+        mMutated = false;
+    }
+
     /**
      * Replace this Drawable's ColorStateList. It is not copied, so changes will propagate on the
      * next call to {@link #setState(int[])}.
@@ -221,6 +247,13 @@
         ColorStateListDrawableState() {
         }
 
+        ColorStateListDrawableState(ColorStateListDrawableState state) {
+            mColor = state.mColor;
+            mTint = state.mTint;
+            mAlpha = state.mAlpha;
+            mTintMode = state.mTintMode;
+            mChangingConfigurations = state.mChangingConfigurations;
+        }
 
         @Override
         public Drawable newDrawable() {
diff --git a/libs/hwui/Android.bp b/libs/hwui/Android.bp
index 62ab790..d582983 100644
--- a/libs/hwui/Android.bp
+++ b/libs/hwui/Android.bp
@@ -175,7 +175,9 @@
         "pipeline/skia/SkiaRecordingCanvas.cpp",
         "pipeline/skia/SkiaVulkanPipeline.cpp",
         "pipeline/skia/VectorDrawableAtlas.cpp",
+        "renderstate/PixelBufferState.cpp",
         "renderstate/RenderState.cpp",
+        "renderstate/TextureState.cpp",
         "renderthread/CacheManager.cpp",
         "renderthread/CanvasContext.cpp",
         "renderthread/DrawFrameTask.cpp",
@@ -187,9 +189,6 @@
         "renderthread/TimeLord.cpp",
         "renderthread/Frame.cpp",
         "service/GraphicsStatsService.cpp",
-        "surfacetexture/EGLConsumer.cpp",
-        "surfacetexture/ImageConsumer.cpp",
-        "surfacetexture/SurfaceTexture.cpp",
         "thread/TaskManager.cpp",
         "utils/Blur.cpp",
         "utils/Color.cpp",
@@ -201,6 +200,7 @@
         "AnimationContext.cpp",
         "Animator.cpp",
         "AnimatorManager.cpp",
+        "Caches.cpp",
         "CanvasState.cpp",
         "CanvasTransform.cpp",
         "ClipArea.cpp",
@@ -209,6 +209,7 @@
         "DeviceInfo.cpp",
         "FrameInfo.cpp",
         "FrameInfoVisualizer.cpp",
+        "GlLayer.cpp",
         "GpuMemoryTracker.cpp",
         "HardwareBitmapUploader.cpp",
         "Interpolator.cpp",
@@ -218,17 +219,21 @@
         "Matrix.cpp",
         "EglReadback.cpp",
         "PathParser.cpp",
+        "PixelBuffer.cpp",
         "ProfileData.cpp",
         "ProfileDataContainer.cpp",
         "Properties.cpp",
         "PropertyValuesAnimatorSet.cpp",
         "PropertyValuesHolder.cpp",
+        "RecordingCanvas.cpp",
         "RenderNode.cpp",
         "RenderProperties.cpp",
         "ResourceCache.cpp",
         "SkiaCanvas.cpp",
         "Snapshot.cpp",
+        "Texture.cpp",
         "VectorDrawable.cpp",
+        "VkLayer.cpp",
         "protos/graphicsstats.proto",
     ],
 
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
new file mode 100644
index 0000000..2541444
--- /dev/null
+++ b/libs/hwui/Caches.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#include "Caches.h"
+
+#include "GlLayer.h"
+#include "Properties.h"
+#include "renderstate/RenderState.h"
+#include "utils/GLUtils.h"
+
+#include <cutils/properties.h>
+#include <utils/Log.h>
+#include <utils/String8.h>
+
+namespace android {
+namespace uirenderer {
+
+Caches* Caches::sInstance = nullptr;
+
+///////////////////////////////////////////////////////////////////////////////
+// Macros
+///////////////////////////////////////////////////////////////////////////////
+
+#if DEBUG_CACHE_FLUSH
+#define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
+#else
+#define FLUSH_LOGD(...)
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// Constructors/destructor
+///////////////////////////////////////////////////////////////////////////////
+
+Caches::Caches(RenderState& renderState) : mInitialized(false) {
+    INIT_LOGD("Creating OpenGL renderer caches");
+    init();
+    initStaticProperties();
+}
+
+bool Caches::init() {
+    if (mInitialized) return false;
+
+    ATRACE_NAME("Caches::init");
+
+    mRegionMesh = nullptr;
+
+    mInitialized = true;
+
+    mPixelBufferState = new PixelBufferState();
+    mTextureState = new TextureState();
+    mTextureState->constructTexture(*this);
+
+    return true;
+}
+
+void Caches::initStaticProperties() {
+    // OpenGL ES 3.0+ specific features
+    gpuPixelBuffersEnabled = extensions().hasPixelBufferObjects() &&
+                             property_get_bool(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, true);
+}
+
+void Caches::terminate() {
+    if (!mInitialized) return;
+    mRegionMesh.reset(nullptr);
+
+    clearGarbage();
+
+    delete mPixelBufferState;
+    mPixelBufferState = nullptr;
+    delete mTextureState;
+    mTextureState = nullptr;
+    mInitialized = false;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Memory management
+///////////////////////////////////////////////////////////////////////////////
+
+void Caches::clearGarbage() {}
+
+void Caches::flush(FlushMode mode) {
+    clearGarbage();
+    glFinish();
+    // Errors during cleanup should be considered non-fatal, dump them and
+    // and move on. TODO: All errors or just errors like bad surface?
+    GLUtils::dumpGLErrors();
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/Caches.h b/libs/hwui/Caches.h
new file mode 100644
index 0000000..642f9dc
--- /dev/null
+++ b/libs/hwui/Caches.h
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#pragma once
+
+#include "DeviceInfo.h"
+#include "Extensions.h"
+#include "ResourceCache.h"
+#include "renderstate/PixelBufferState.h"
+#include "renderstate/TextureState.h"
+#include "thread/TaskManager.h"
+#include "thread/TaskProcessor.h"
+
+#include <memory>
+#include <vector>
+
+#include <GLES3/gl3.h>
+
+#include <utils/KeyedVector.h>
+
+#include <cutils/compiler.h>
+
+#include <SkPath.h>
+
+#include <vector>
+
+namespace android {
+namespace uirenderer {
+
+///////////////////////////////////////////////////////////////////////////////
+// Caches
+///////////////////////////////////////////////////////////////////////////////
+
+class RenderNode;
+class RenderState;
+
+class ANDROID_API Caches {
+public:
+    static Caches& createInstance(RenderState& renderState) {
+        LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
+        sInstance = new Caches(renderState);
+        return *sInstance;
+    }
+
+    static Caches& getInstance() {
+        LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
+        return *sInstance;
+    }
+
+    static bool hasInstance() { return sInstance != nullptr; }
+
+private:
+    explicit Caches(RenderState& renderState);
+    static Caches* sInstance;
+
+public:
+    enum class FlushMode { Layers = 0, Moderate, Full };
+
+    /**
+     * Initialize caches.
+     */
+    bool init();
+
+    bool isInitialized() { return mInitialized; }
+
+    /**
+     * Flush the cache.
+     *
+     * @param mode Indicates how much of the cache should be flushed
+     */
+    void flush(FlushMode mode);
+
+    /**
+     * Destroys all resources associated with this cache. This should
+     * be called after a flush(FlushMode::Full).
+     */
+    void terminate();
+
+    /**
+     * Call this on each frame to ensure that garbage is deleted from
+     * GPU memory.
+     */
+    void clearGarbage();
+
+    /**
+     * Returns the GL RGBA internal format to use for the current device
+     * If the device supports linear blending and needSRGB is true,
+     * this function returns GL_SRGB8_ALPHA8, otherwise it returns GL_RGBA
+     */
+    constexpr GLint rgbaInternalFormat(bool needSRGB = true) const {
+        return extensions().hasLinearBlending() && needSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA;
+    }
+
+public:
+    TaskManager tasks;
+
+    bool gpuPixelBuffersEnabled;
+
+    const Extensions& extensions() const { return DeviceInfo::get()->extensions(); }
+    PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
+    TextureState& textureState() { return *mTextureState; }
+
+private:
+    void initStaticProperties();
+
+    static void eventMarkNull(GLsizei length, const GLchar* marker) {}
+    static void startMarkNull(GLsizei length, const GLchar* marker) {}
+    static void endMarkNull() {}
+
+    // Used to render layers
+    std::unique_ptr<TextureVertex[]> mRegionMesh;
+
+    bool mInitialized;
+
+    // TODO: move below to RenderState
+    PixelBufferState* mPixelBufferState = nullptr;
+    TextureState* mTextureState = nullptr;
+
+};  // class Caches
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/CanvasTransform.cpp b/libs/hwui/CanvasTransform.cpp
index 1b15dbd..adcdc18 100644
--- a/libs/hwui/CanvasTransform.cpp
+++ b/libs/hwui/CanvasTransform.cpp
@@ -15,16 +15,20 @@
  */
 
 #include "CanvasTransform.h"
-#include "utils/Color.h"
 #include "Properties.h"
+#include "utils/Color.h"
 
-#include <ui/ColorSpace.h>
 #include <SkColorFilter.h>
+#include <SkGradientShader.h>
 #include <SkPaint.h>
+#include <SkShader.h>
+#include <ui/ColorSpace.h>
 
 #include <algorithm>
 #include <cmath>
 
+#include <log/log.h>
+
 namespace android::uirenderer {
 
 static SkColor makeLight(SkColor color) {
@@ -66,6 +70,32 @@
     SkColor newColor = transformColor(transform, paint.getColor());
     paint.setColor(newColor);
 
+    if (paint.getShader()) {
+        SkShader::GradientInfo info;
+        std::array<SkColor, 10> _colorStorage;
+        std::array<SkScalar, _colorStorage.size()> _offsetStorage;
+        info.fColorCount = _colorStorage.size();
+        info.fColors = _colorStorage.data();
+        info.fColorOffsets = _offsetStorage.data();
+        SkShader::GradientType type = paint.getShader()->asAGradient(&info);
+        ALOGW_IF(type, "Found gradient of type = %d", type);
+
+        if (info.fColorCount <= 10) {
+            switch (type) {
+                case SkShader::kLinear_GradientType:
+                    for (int i = 0; i < info.fColorCount; i++) {
+                        info.fColors[i] = transformColor(transform, info.fColors[i]);
+                    }
+                    paint.setShader(SkGradientShader::MakeLinear(info.fPoint, info.fColors,
+                                                                 info.fColorOffsets, info.fColorCount,
+                                                                 info.fTileMode, info.fGradientFlags, nullptr));
+                    break;
+                default:break;
+            }
+
+        }
+    }
+
     if (paint.getColorFilter()) {
         SkBlendMode mode;
         SkColor color;
@@ -77,43 +107,10 @@
     }
 }
 
-class ColorFilterCanvas : public SkPaintFilterCanvas {
-public:
-    ColorFilterCanvas(ColorTransform transform, SkCanvas* canvas)
-            : SkPaintFilterCanvas(canvas), mTransform(transform) {}
-
-    bool onFilter(SkTCopyOnFirstWrite<SkPaint>* paint, Type type) const override {
-        if (*paint) {
-            applyColorTransform(mTransform, *(paint->writable()));
-        }
-        return true;
-    }
-
-private:
-    ColorTransform mTransform;
-};
-
-std::unique_ptr<SkCanvas> makeTransformCanvas(SkCanvas* inCanvas, ColorTransform transform) {
-    switch (transform) {
-        case ColorTransform::Light:
-            return std::make_unique<ColorFilterCanvas>(ColorTransform::Light, inCanvas);
-        case ColorTransform::Dark:
-            return std::make_unique<ColorFilterCanvas>(ColorTransform::Dark, inCanvas);
-        default:
-            return nullptr;
-    }
-}
-
-std::unique_ptr<SkCanvas> makeTransformCanvas(SkCanvas* inCanvas, UsageHint usageHint) {
-    if (Properties::forceDarkMode) {
-        switch (usageHint) {
-            case UsageHint::Unknown:
-                return makeTransformCanvas(inCanvas, ColorTransform::Light);
-            case UsageHint::Background:
-                return makeTransformCanvas(inCanvas, ColorTransform::Dark);
-        }
-    }
-    return nullptr;
+bool transformPaint(ColorTransform transform, SkPaint* paint) {
+    // TODO
+    applyColorTransform(transform, *paint);
+    return true;
 }
 
 };  // namespace android::uirenderer
\ No newline at end of file
diff --git a/libs/hwui/CanvasTransform.h b/libs/hwui/CanvasTransform.h
index f71fdfa..32d9a05 100644
--- a/libs/hwui/CanvasTransform.h
+++ b/libs/hwui/CanvasTransform.h
@@ -34,7 +34,7 @@
     Dark,
 };
 
-std::unique_ptr<SkCanvas> makeTransformCanvas(SkCanvas* inCanvas, ColorTransform transform);
-std::unique_ptr<SkCanvas> makeTransformCanvas(SkCanvas* inCanvas, UsageHint usageHint);
+// True if the paint was modified, false otherwise
+bool transformPaint(ColorTransform transform, SkPaint* paint);
 
 }  // namespace android::uirenderer;
\ No newline at end of file
diff --git a/libs/hwui/DeferredLayerUpdater.cpp b/libs/hwui/DeferredLayerUpdater.cpp
index 0091655..569de76 100644
--- a/libs/hwui/DeferredLayerUpdater.cpp
+++ b/libs/hwui/DeferredLayerUpdater.cpp
@@ -15,20 +15,27 @@
  */
 #include "DeferredLayerUpdater.h"
 
+#include "GlLayer.h"
+#include "VkLayer.h"
 #include "renderstate/RenderState.h"
+#include "renderthread/EglManager.h"
+#include "renderthread/RenderTask.h"
 #include "utils/PaintUtils.h"
 
 namespace android {
 namespace uirenderer {
 
-DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState)
+DeferredLayerUpdater::DeferredLayerUpdater(RenderState& renderState, CreateLayerFn createLayerFn,
+                                           Layer::Api layerApi)
         : mRenderState(renderState)
         , mBlend(false)
         , mSurfaceTexture(nullptr)
         , mTransform(nullptr)
         , mGLContextAttached(false)
         , mUpdateTexImage(false)
-        , mLayer(nullptr) {
+        , mLayer(nullptr)
+        , mLayerApi(layerApi)
+        , mCreateLayerFn(createLayerFn) {
     renderState.registerDeferredLayerUpdater(this);
 }
 
@@ -43,9 +50,13 @@
         return;
     }
 
-    if (mSurfaceTexture.get() && mGLContextAttached) {
-        mSurfaceTexture->detachFromView();
+    if (mSurfaceTexture.get() && mLayerApi == Layer::Api::OpenGL && mGLContextAttached) {
+        status_t err = mSurfaceTexture->detachFromContext();
         mGLContextAttached = false;
+        if (err != 0) {
+            // TODO: Elevate to fatal exception
+            ALOGE("Failed to detach SurfaceTexture from context %d", err);
+        }
     }
 
     mLayer->postDecStrong();
@@ -64,53 +75,99 @@
 
 void DeferredLayerUpdater::apply() {
     if (!mLayer) {
-        mLayer = new Layer(mRenderState, mColorFilter, mAlpha, mMode);
+        mLayer = mCreateLayerFn(mRenderState, mWidth, mHeight, mColorFilter, mAlpha, mMode, mBlend);
     }
 
     mLayer->setColorFilter(mColorFilter);
     mLayer->setAlpha(mAlpha, mMode);
 
     if (mSurfaceTexture.get()) {
-        if (!mGLContextAttached) {
-            mGLContextAttached = true;
-            mUpdateTexImage = true;
-            mSurfaceTexture->attachToView();
-        }
-        if (mUpdateTexImage) {
-            mUpdateTexImage = false;
-            sk_sp<SkImage> layerImage;
-            SkMatrix textureTransform;
-            android_dataspace dataSpace;
-            bool queueEmpty = true;
-            // If the SurfaceTexture queue is in synchronous mode, need to discard all
-            // but latest frame. Since we can't tell which mode it is in,
-            // do this unconditionally.
-            do {
-                layerImage = mSurfaceTexture->dequeueImage(textureTransform, dataSpace, &queueEmpty,
-                        mRenderState);
-            } while (layerImage.get() && (!queueEmpty));
-            if (layerImage.get()) {
-                // force filtration if buffer size != layer size
-                bool forceFilter = mWidth != layerImage->width() || mHeight != layerImage->height();
-                updateLayer(forceFilter, textureTransform, dataSpace, layerImage);
+        if (mLayer->getApi() == Layer::Api::Vulkan) {
+            if (mUpdateTexImage) {
+                mUpdateTexImage = false;
+                doUpdateVkTexImage();
             }
+        } else {
+            LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::OpenGL,
+                                "apply surfaceTexture with non GL backend %x, GL %x, VK %x",
+                                mLayer->getApi(), Layer::Api::OpenGL, Layer::Api::Vulkan);
+            if (!mGLContextAttached) {
+                mGLContextAttached = true;
+                mUpdateTexImage = true;
+                mSurfaceTexture->attachToContext(static_cast<GlLayer*>(mLayer)->getTextureId());
+            }
+            if (mUpdateTexImage) {
+                mUpdateTexImage = false;
+                doUpdateTexImage();
+            }
+            GLenum renderTarget = mSurfaceTexture->getCurrentTextureTarget();
+            static_cast<GlLayer*>(mLayer)->setRenderTarget(renderTarget);
         }
-
         if (mTransform) {
-            mLayer->getTransform() = *mTransform;
+            mLayer->getTransform().load(*mTransform);
             setTransform(nullptr);
         }
     }
 }
 
-void DeferredLayerUpdater::updateLayer(bool forceFilter, const SkMatrix& textureTransform,
-        android_dataspace dataspace, const sk_sp<SkImage>& layerImage) {
+void DeferredLayerUpdater::doUpdateTexImage() {
+    LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::OpenGL,
+                        "doUpdateTexImage non GL backend %x, GL %x, VK %x", mLayer->getApi(),
+                        Layer::Api::OpenGL, Layer::Api::Vulkan);
+    if (mSurfaceTexture->updateTexImage() == NO_ERROR) {
+        float transform[16];
+
+        int64_t frameNumber = mSurfaceTexture->getFrameNumber();
+        // If the GLConsumer queue is in synchronous mode, need to discard all
+        // but latest frame, using the frame number to tell when we no longer
+        // have newer frames to target. Since we can't tell which mode it is in,
+        // do this unconditionally.
+        int dropCounter = 0;
+        while (mSurfaceTexture->updateTexImage() == NO_ERROR) {
+            int64_t newFrameNumber = mSurfaceTexture->getFrameNumber();
+            if (newFrameNumber == frameNumber) break;
+            frameNumber = newFrameNumber;
+            dropCounter++;
+        }
+
+        bool forceFilter = false;
+        sp<GraphicBuffer> buffer = mSurfaceTexture->getCurrentBuffer();
+        if (buffer != nullptr) {
+            // force filtration if buffer size != layer size
+            forceFilter = mWidth != static_cast<int>(buffer->getWidth()) ||
+                          mHeight != static_cast<int>(buffer->getHeight());
+        }
+
+#if DEBUG_RENDERER
+        if (dropCounter > 0) {
+            RENDERER_LOGD("Dropped %d frames on texture layer update", dropCounter);
+        }
+#endif
+        mSurfaceTexture->getTransformMatrix(transform);
+
+        updateLayer(forceFilter, transform, mSurfaceTexture->getCurrentDataSpace());
+    }
+}
+
+void DeferredLayerUpdater::doUpdateVkTexImage() {
+    LOG_ALWAYS_FATAL_IF(mLayer->getApi() != Layer::Api::Vulkan,
+                        "updateLayer non Vulkan backend %x, GL %x, VK %x", mLayer->getApi(),
+                        Layer::Api::OpenGL, Layer::Api::Vulkan);
+
+    static const mat4 identityMatrix;
+    updateLayer(false, identityMatrix.data, HAL_DATASPACE_UNKNOWN);
+
+    VkLayer* vkLayer = static_cast<VkLayer*>(mLayer);
+    vkLayer->updateTexture();
+}
+
+void DeferredLayerUpdater::updateLayer(bool forceFilter, const float* textureTransform,
+                                       android_dataspace dataspace) {
     mLayer->setBlend(mBlend);
     mLayer->setForceFilter(forceFilter);
     mLayer->setSize(mWidth, mHeight);
-    mLayer->getTexTransform() = textureTransform;
+    mLayer->getTexTransform().load(textureTransform);
     mLayer->setDataSpace(dataspace);
-    mLayer->setImage(layerImage);
 }
 
 void DeferredLayerUpdater::detachSurfaceTexture() {
diff --git a/libs/hwui/DeferredLayerUpdater.h b/libs/hwui/DeferredLayerUpdater.h
index 4c323b8..fe3ee7a 100644
--- a/libs/hwui/DeferredLayerUpdater.h
+++ b/libs/hwui/DeferredLayerUpdater.h
@@ -17,19 +17,18 @@
 #pragma once
 
 #include <SkColorFilter.h>
-#include <SkImage.h>
 #include <SkMatrix.h>
 #include <cutils/compiler.h>
-#include <map>
+#include <gui/GLConsumer.h>
 #include <system/graphics.h>
 #include <utils/StrongPointer.h>
 
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 
-#include "surfacetexture/SurfaceTexture.h"
 #include "Layer.h"
 #include "Rect.h"
+#include "renderthread/RenderThread.h"
 
 namespace android {
 namespace uirenderer {
@@ -42,7 +41,12 @@
 public:
     // Note that DeferredLayerUpdater assumes it is taking ownership of the layer
     // and will not call incrementRef on it as a result.
-    ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState);
+    typedef std::function<Layer*(RenderState& renderState, uint32_t layerWidth,
+                                 uint32_t layerHeight, sk_sp<SkColorFilter> colorFilter, int alpha,
+                                 SkBlendMode mode, bool blend)>
+            CreateLayerFn;
+    ANDROID_API explicit DeferredLayerUpdater(RenderState& renderState, CreateLayerFn createLayerFn,
+                                              Layer::Api layerApi);
 
     ANDROID_API ~DeferredLayerUpdater();
 
@@ -66,13 +70,13 @@
         return false;
     }
 
-    ANDROID_API void setSurfaceTexture(const sp<SurfaceTexture>& consumer) {
-        if (consumer.get() != mSurfaceTexture.get()) {
-            mSurfaceTexture = consumer;
+    ANDROID_API void setSurfaceTexture(const sp<GLConsumer>& texture) {
+        if (texture.get() != mSurfaceTexture.get()) {
+            mSurfaceTexture = texture;
 
-            GLenum target = consumer->getCurrentTextureTarget();
+            GLenum target = texture->getCurrentTextureTarget();
             LOG_ALWAYS_FATAL_IF(target != GL_TEXTURE_2D && target != GL_TEXTURE_EXTERNAL_OES,
-                                "set unsupported SurfaceTexture with target %x", target);
+                                "set unsupported GLConsumer with target %x", target);
         }
     }
 
@@ -93,11 +97,12 @@
 
     void detachSurfaceTexture();
 
-    void updateLayer(bool forceFilter, const SkMatrix& textureTransform,
-            android_dataspace dataspace, const sk_sp<SkImage>& layerImage);
+    void updateLayer(bool forceFilter, const float* textureTransform, android_dataspace dataspace);
 
     void destroyLayer();
 
+    Layer::Api getBackingLayerApi() { return mLayerApi; }
+
 private:
     RenderState& mRenderState;
 
@@ -108,12 +113,17 @@
     sk_sp<SkColorFilter> mColorFilter;
     int mAlpha = 255;
     SkBlendMode mMode = SkBlendMode::kSrcOver;
-    sp<SurfaceTexture> mSurfaceTexture;
+    sp<GLConsumer> mSurfaceTexture;
     SkMatrix* mTransform;
     bool mGLContextAttached;
     bool mUpdateTexImage;
 
     Layer* mLayer;
+    Layer::Api mLayerApi;
+    CreateLayerFn mCreateLayerFn;
+
+    void doUpdateTexImage();
+    void doUpdateVkTexImage();
 };
 
 } /* namespace uirenderer */
diff --git a/libs/hwui/DeviceInfo.cpp b/libs/hwui/DeviceInfo.cpp
index 10fcee8..a43f58c 100644
--- a/libs/hwui/DeviceInfo.cpp
+++ b/libs/hwui/DeviceInfo.cpp
@@ -42,6 +42,8 @@
         false,  // secure?
         0,      // appVsyncOffset
         0,      // presentationDeadline
+        1080,   // viewportW
+        1920,   // viewportH
 };
 
 static DeviceInfo* sDeviceInfo = nullptr;
diff --git a/libs/hwui/DisplayListOps.in b/libs/hwui/DisplayListOps.in
new file mode 100644
index 0000000..f61c156
--- /dev/null
+++ b/libs/hwui/DisplayListOps.in
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+X(Flush) 
+X(Save) 
+X(Restore) 
+X(SaveLayer)
+X(Concat) 
+X(SetMatrix) 
+X(Translate)
+X(ClipPath) 
+X(ClipRect) 
+X(ClipRRect) 
+X(ClipRegion)
+X(DrawPaint) 
+X(DrawPath) 
+X(DrawRect) 
+X(DrawRegion) 
+X(DrawOval) 
+X(DrawArc)
+X(DrawRRect) 
+X(DrawDRRect) 
+X(DrawAnnotation) 
+X(DrawDrawable) 
+X(DrawPicture)
+X(DrawImage) 
+X(DrawImageNine) 
+X(DrawImageRect) 
+X(DrawImageLattice)
+X(DrawText) 
+X(DrawPosText) 
+X(DrawPosTextH)
+X(DrawTextRSXform) 
+X(DrawTextBlob)
+X(DrawPatch) 
+X(DrawPoints) 
+X(DrawVertices) 
+X(DrawAtlas) 
+X(DrawShadowRec)
\ No newline at end of file
diff --git a/libs/hwui/GlLayer.cpp b/libs/hwui/GlLayer.cpp
new file mode 100644
index 0000000..432bb85
--- /dev/null
+++ b/libs/hwui/GlLayer.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#include "GlLayer.h"
+
+#include "Caches.h"
+#include "RenderNode.h"
+#include "renderstate/RenderState.h"
+
+namespace android {
+namespace uirenderer {
+
+GlLayer::GlLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
+                 sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode, bool blend)
+        : Layer(renderState, Api::OpenGL, colorFilter, alpha, mode)
+        , caches(Caches::getInstance())
+        , texture(caches) {
+    texture.mWidth = layerWidth;
+    texture.mHeight = layerHeight;
+    texture.blend = blend;
+}
+
+GlLayer::~GlLayer() {
+    // There's a rare possibility that Caches could have been destroyed already
+    // since this method is queued up as a task.
+    // Since this is a reset method, treat this as non-fatal.
+    if (caches.isInitialized() && texture.mId) {
+        texture.deleteTexture();
+    }
+}
+
+void GlLayer::onGlContextLost() {
+    texture.deleteTexture();
+}
+
+void GlLayer::setRenderTarget(GLenum renderTarget) {
+    if (renderTarget != getRenderTarget()) {
+        // new render target: bind with new target, and update filter/wrap
+        texture.mTarget = renderTarget;
+        if (texture.mId) {
+            caches.textureState().bindTexture(texture.target(), texture.mId);
+        }
+        texture.setFilter(GL_NEAREST, false, true);
+        texture.setWrap(GL_CLAMP_TO_EDGE, false, true);
+    }
+}
+
+void GlLayer::generateTexture() {
+    if (!texture.mId) {
+        glGenTextures(1, &texture.mId);
+    }
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/GlLayer.h b/libs/hwui/GlLayer.h
new file mode 100644
index 0000000..9f70fda
--- /dev/null
+++ b/libs/hwui/GlLayer.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#pragma once
+
+#include "Layer.h"
+
+#include "Texture.h"
+
+namespace android {
+namespace uirenderer {
+
+// Forward declarations
+class Caches;
+
+/**
+ * A layer has dimensions and is backed by an OpenGL texture or FBO.
+ */
+class GlLayer : public Layer {
+public:
+    GlLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
+            sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode, bool blend);
+    virtual ~GlLayer();
+
+    uint32_t getWidth() const override { return texture.mWidth; }
+
+    uint32_t getHeight() const override { return texture.mHeight; }
+
+    void setSize(uint32_t width, uint32_t height) override {
+        texture.updateLayout(width, height, texture.internalFormat(), texture.format(),
+                             texture.target());
+    }
+
+    void setBlend(bool blend) override { texture.blend = blend; }
+
+    bool isBlend() const override { return texture.blend; }
+
+    inline GLuint getTextureId() const { return texture.id(); }
+
+    inline GLenum getRenderTarget() const { return texture.target(); }
+
+    void setRenderTarget(GLenum renderTarget);
+
+    void generateTexture();
+
+    /**
+     * Lost the GL context but the layer is still around, mark it invalid internally
+     * so the dtor knows not to do any GL work
+     */
+    void onGlContextLost();
+
+private:
+    Caches& caches;
+
+    /**
+     * The texture backing this layer.
+     */
+    Texture texture;
+};  // struct GlLayer
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/GpuMemoryTracker.cpp b/libs/hwui/GpuMemoryTracker.cpp
index a9a7af8..612bfde 100644
--- a/libs/hwui/GpuMemoryTracker.cpp
+++ b/libs/hwui/GpuMemoryTracker.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "Texture.h"
 #include "utils/StringUtils.h"
 
 #include <GpuMemoryTracker.h>
@@ -116,6 +117,22 @@
             ATRACE_INT(buf, stats.count);
         }
     }
+
+    std::vector<const Texture*> freeList;
+    for (const auto& obj : gObjectSet) {
+        if (obj->objectType() == GpuObjectType::Texture) {
+            const Texture* texture = static_cast<Texture*>(obj);
+            if (texture->cleanup) {
+                ALOGE("Leaked texture marked for cleanup! id=%u, size %ux%u", texture->id(),
+                      texture->width(), texture->height());
+                freeList.push_back(texture);
+            }
+        }
+    }
+    for (auto& texture : freeList) {
+        const_cast<Texture*>(texture)->deleteTexture();
+        delete texture;
+    }
 }
 
 }  // namespace uirenderer
diff --git a/libs/hwui/Layer.cpp b/libs/hwui/Layer.cpp
index f59a2e6..fb8f033 100644
--- a/libs/hwui/Layer.cpp
+++ b/libs/hwui/Layer.cpp
@@ -17,17 +17,17 @@
 #include "Layer.h"
 
 #include "renderstate/RenderState.h"
-#include "utils/Color.h"
 
 #include <SkToSRGBColorFilter.h>
 
 namespace android {
 namespace uirenderer {
 
-Layer::Layer(RenderState& renderState, sk_sp<SkColorFilter> colorFilter, int alpha,
-        SkBlendMode mode)
+Layer::Layer(RenderState& renderState, Api api, sk_sp<SkColorFilter> colorFilter, int alpha,
+             SkBlendMode mode)
         : GpuMemoryTracker(GpuObjectType::Layer)
         , mRenderState(renderState)
+        , mApi(api)
         , mColorFilter(colorFilter)
         , alpha(alpha)
         , mode(mode) {
@@ -36,8 +36,6 @@
     incStrong(nullptr);
     buildColorSpaceWithFilter();
     renderState.registerLayer(this);
-    texTransform.setIdentity();
-    transform.setIdentity();
 }
 
 Layer::~Layer() {
diff --git a/libs/hwui/Layer.h b/libs/hwui/Layer.h
index c4e4c1c..31878ac 100644
--- a/libs/hwui/Layer.h
+++ b/libs/hwui/Layer.h
@@ -23,9 +23,8 @@
 #include <SkColorFilter.h>
 #include <SkColorSpace.h>
 #include <SkPaint.h>
-#include <SkImage.h>
-#include <SkMatrix.h>
-#include <system/graphics.h>
+
+#include "Matrix.h"
 
 namespace android {
 namespace uirenderer {
@@ -41,19 +40,24 @@
  */
 class Layer : public VirtualLightRefBase, GpuMemoryTracker {
 public:
-    Layer(RenderState& renderState, sk_sp<SkColorFilter>, int alpha, SkBlendMode mode);
+    enum class Api {
+        OpenGL = 0,
+        Vulkan = 1,
+    };
+
+    Api getApi() const { return mApi; }
 
     ~Layer();
 
-    virtual uint32_t getWidth() const { return mWidth; }
+    virtual uint32_t getWidth() const = 0;
 
-    virtual uint32_t getHeight() const { return mHeight; }
+    virtual uint32_t getHeight() const = 0;
 
-    virtual void setSize(uint32_t width, uint32_t height) { mWidth = width; mHeight = height; }
+    virtual void setSize(uint32_t width, uint32_t height) = 0;
 
-    virtual void setBlend(bool blend) { mBlend = blend; }
+    virtual void setBlend(bool blend) = 0;
 
-    virtual bool isBlend() const { return mBlend; }
+    virtual bool isBlend() const = 0;
 
     inline void setForceFilter(bool forceFilter) { this->forceFilter = forceFilter; }
 
@@ -80,9 +84,9 @@
 
     inline sk_sp<SkColorFilter> getColorSpaceWithFilter() const { return mColorSpaceWithFilter; }
 
-    inline SkMatrix& getTexTransform() { return texTransform; }
+    inline mat4& getTexTransform() { return texTransform; }
 
-    inline SkMatrix& getTransform() { return transform; }
+    inline mat4& getTransform() { return transform; }
 
     /**
      * Posts a decStrong call to the appropriate thread.
@@ -90,17 +94,16 @@
      */
     void postDecStrong();
 
-    inline void setImage(const sk_sp<SkImage>& image) { this->layerImage = image; }
-
-    inline sk_sp<SkImage> getImage() const { return this->layerImage; }
-
 protected:
+    Layer(RenderState& renderState, Api api, sk_sp<SkColorFilter>, int alpha, SkBlendMode mode);
 
     RenderState& mRenderState;
 
 private:
     void buildColorSpaceWithFilter();
 
+    Api mApi;
+
     /**
      * Color filter used to draw this layer. Optional.
      */
@@ -134,32 +137,12 @@
     /**
      * Optional texture coordinates transform.
      */
-    SkMatrix texTransform;
+    mat4 texTransform;
 
     /**
      * Optional transform.
      */
-    SkMatrix transform;
-
-    /**
-     * An image backing the layer.
-     */
-    sk_sp<SkImage> layerImage;
-
-    /**
-     * layer width.
-     */
-    uint32_t mWidth = 0;
-
-    /**
-     * layer height.
-     */
-    uint32_t mHeight = 0;
-
-    /**
-     * enable blending
-     */
-    bool mBlend = false;
+    mat4 transform;
 
 };  // struct Layer
 
diff --git a/libs/hwui/PixelBuffer.cpp b/libs/hwui/PixelBuffer.cpp
new file mode 100644
index 0000000..910a988
--- /dev/null
+++ b/libs/hwui/PixelBuffer.cpp
@@ -0,0 +1,156 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "PixelBuffer.h"
+
+#include "Debug.h"
+#include "Extensions.h"
+#include "Properties.h"
+#include "renderstate/RenderState.h"
+#include "utils/GLUtils.h"
+
+#include <utils/Log.h>
+
+namespace android {
+namespace uirenderer {
+
+///////////////////////////////////////////////////////////////////////////////
+// CPU pixel buffer
+///////////////////////////////////////////////////////////////////////////////
+
+class CpuPixelBuffer : public PixelBuffer {
+public:
+    CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
+
+    uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
+
+    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
+
+protected:
+    void unmap() override;
+
+private:
+    std::unique_ptr<uint8_t[]> mBuffer;
+};
+
+CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
+        : PixelBuffer(format, width, height)
+        , mBuffer(new uint8_t[width * height * formatSize(format)]) {}
+
+uint8_t* CpuPixelBuffer::map(AccessMode mode) {
+    if (mAccessMode == kAccessMode_None) {
+        mAccessMode = mode;
+    }
+    return mBuffer.get();
+}
+
+void CpuPixelBuffer::unmap() {
+    mAccessMode = kAccessMode_None;
+}
+
+void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
+    glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE,
+                    &mBuffer[offset]);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// GPU pixel buffer
+///////////////////////////////////////////////////////////////////////////////
+
+class GpuPixelBuffer : public PixelBuffer {
+public:
+    GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
+    ~GpuPixelBuffer();
+
+    uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
+
+    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
+
+protected:
+    void unmap() override;
+
+private:
+    GLuint mBuffer;
+    uint8_t* mMappedPointer;
+    Caches& mCaches;
+};
+
+GpuPixelBuffer::GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
+        : PixelBuffer(format, width, height)
+        , mMappedPointer(nullptr)
+        , mCaches(Caches::getInstance()) {
+    glGenBuffers(1, &mBuffer);
+
+    mCaches.pixelBufferState().bind(mBuffer);
+    glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
+    mCaches.pixelBufferState().unbind();
+}
+
+GpuPixelBuffer::~GpuPixelBuffer() {
+    glDeleteBuffers(1, &mBuffer);
+}
+
+uint8_t* GpuPixelBuffer::map(AccessMode mode) {
+    if (mAccessMode == kAccessMode_None) {
+        mCaches.pixelBufferState().bind(mBuffer);
+        mMappedPointer = (uint8_t*)glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
+        if (CC_UNLIKELY(!mMappedPointer)) {
+            GLUtils::dumpGLErrors();
+            LOG_ALWAYS_FATAL("Failed to map PBO");
+        }
+        mAccessMode = mode;
+        mCaches.pixelBufferState().unbind();
+    }
+
+    return mMappedPointer;
+}
+
+void GpuPixelBuffer::unmap() {
+    if (mAccessMode != kAccessMode_None) {
+        if (mMappedPointer) {
+            mCaches.pixelBufferState().bind(mBuffer);
+            GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
+            if (status == GL_FALSE) {
+                ALOGE("Corrupted GPU pixel buffer");
+            }
+        }
+        mAccessMode = kAccessMode_None;
+        mMappedPointer = nullptr;
+    }
+}
+
+void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
+    // If the buffer is not mapped, unmap() will not bind it
+    mCaches.pixelBufferState().bind(mBuffer);
+    unmap();
+    glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat, GL_UNSIGNED_BYTE,
+                    reinterpret_cast<void*>(offset));
+    mCaches.pixelBufferState().unbind();
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// Factory
+///////////////////////////////////////////////////////////////////////////////
+
+PixelBuffer* PixelBuffer::create(GLenum format, uint32_t width, uint32_t height, BufferType type) {
+    if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
+        return new GpuPixelBuffer(format, width, height);
+    }
+    return new CpuPixelBuffer(format, width, height);
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/PixelBuffer.h b/libs/hwui/PixelBuffer.h
new file mode 100644
index 0000000..e7e341b
--- /dev/null
+++ b/libs/hwui/PixelBuffer.h
@@ -0,0 +1,198 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#ifndef ANDROID_HWUI_PIXEL_BUFFER_H
+#define ANDROID_HWUI_PIXEL_BUFFER_H
+
+#include <GLES3/gl3.h>
+
+#include <log/log.h>
+
+namespace android {
+namespace uirenderer {
+
+/**
+ * Represents a pixel buffer. A pixel buffer will be backed either by a
+ * PBO on OpenGL ES 3.0 and higher or by an array of uint8_t on other
+ * versions. If the buffer is backed by a PBO it will of type
+ * GL_PIXEL_UNPACK_BUFFER.
+ *
+ * To read from or write into a PixelBuffer you must first map the
+ * buffer using the map(AccessMode) method. This method returns a
+ * pointer to the beginning of the buffer.
+ *
+ * Before the buffer can be used by the GPU, for instance to upload
+ * a texture, you must first unmap the buffer. To do so, call the
+ * unmap() method.
+ *
+ * Mapping and unmapping a PixelBuffer can have the side effect of
+ * changing the currently active GL_PIXEL_UNPACK_BUFFER. It is
+ * therefore recommended to call Caches::unbindPixelbuffer() after
+ * using a PixelBuffer to upload to a texture.
+ */
+class PixelBuffer {
+public:
+    enum BufferType { kBufferType_Auto, kBufferType_CPU };
+
+    enum AccessMode {
+        kAccessMode_None = 0,
+        kAccessMode_Read = GL_MAP_READ_BIT,
+        kAccessMode_Write = GL_MAP_WRITE_BIT,
+        kAccessMode_ReadWrite = GL_MAP_READ_BIT | GL_MAP_WRITE_BIT
+    };
+
+    /**
+     * Creates a new PixelBuffer object with the specified format and
+     * dimensions. The buffer is immediately allocated.
+     *
+     * The buffer type specifies how the buffer should be allocated.
+     * By default this method will automatically choose whether to allocate
+     * a CPU or GPU buffer.
+     */
+    static PixelBuffer* create(GLenum format, uint32_t width, uint32_t height,
+                               BufferType type = kBufferType_Auto);
+
+    virtual ~PixelBuffer() {}
+
+    /**
+     * Returns the format of this render buffer.
+     */
+    GLenum getFormat() const { return mFormat; }
+
+    /**
+     * Maps this before with the specified access mode. This method
+     * returns a pointer to the region of memory where the buffer was
+     * mapped.
+     *
+     * If the buffer is already mapped when this method is invoked,
+     * this method will return the previously mapped pointer. The
+     * access mode can only be changed by calling unmap() first.
+     *
+     * The specified access mode cannot be kAccessMode_None.
+     */
+    virtual uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) = 0;
+
+    /**
+     * Returns the current access mode for this buffer. If the buffer
+     * is not mapped, this method returns kAccessMode_None.
+     */
+    AccessMode getAccessMode() const { return mAccessMode; }
+
+    /**
+     * Upload the specified rectangle of this pixel buffer as a
+     * GL_TEXTURE_2D texture. Calling this method will trigger
+     * an unmap() if necessary.
+     */
+    virtual void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) = 0;
+
+    /**
+     * Upload the specified rectangle of this pixel buffer as a
+     * GL_TEXTURE_2D texture. Calling this method will trigger
+     * an unmap() if necessary.
+     *
+     * This is a convenience function provided to save callers the
+     * trouble of computing the offset parameter.
+     */
+    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height) {
+        upload(x, y, width, height, getOffset(x, y));
+    }
+
+    /**
+     * Returns the width of the render buffer in pixels.
+     */
+    uint32_t getWidth() const { return mWidth; }
+
+    /**
+     * Returns the height of the render buffer in pixels.
+     */
+    uint32_t getHeight() const { return mHeight; }
+
+    /**
+     * Returns the size of this pixel buffer in bytes.
+     */
+    uint32_t getSize() const { return mWidth * mHeight * formatSize(mFormat); }
+
+    /**
+     * Returns the offset of a pixel in this pixel buffer, in bytes.
+     */
+    uint32_t getOffset(uint32_t x, uint32_t y) const {
+        return (y * mWidth + x) * formatSize(mFormat);
+    }
+
+    /**
+     * Returns the number of bytes per pixel in the specified format.
+     *
+     * Supported formats:
+     *      GL_ALPHA
+     *      GL_RGBA
+     */
+    static uint32_t formatSize(GLenum format) {
+        switch (format) {
+            case GL_ALPHA:
+                return 1;
+            case GL_RGBA:
+                return 4;
+        }
+        return 0;
+    }
+
+    /**
+     * Returns the alpha channel offset in the specified format.
+     *
+     * Supported formats:
+     *      GL_ALPHA
+     *      GL_RGBA
+     */
+    static uint32_t formatAlphaOffset(GLenum format) {
+        switch (format) {
+            case GL_ALPHA:
+                return 0;
+            case GL_RGBA:
+                return 3;
+        }
+
+        ALOGE("unsupported format: %d", format);
+        return 0;
+    }
+
+protected:
+    /**
+     * Creates a new render buffer in the specified format and dimensions.
+     * The format must be GL_ALPHA or GL_RGBA.
+     */
+    PixelBuffer(GLenum format, uint32_t width, uint32_t height)
+            : mFormat(format), mWidth(width), mHeight(height), mAccessMode(kAccessMode_None) {}
+
+    /**
+     * Unmaps this buffer, if needed. After the buffer is unmapped,
+     * the pointer previously returned by map() becomes invalid and
+     * should not be used.
+     */
+    virtual void unmap() = 0;
+
+    GLenum mFormat;
+
+    uint32_t mWidth;
+    uint32_t mHeight;
+
+    AccessMode mAccessMode;
+
+};  // class PixelBuffer
+
+};  // namespace uirenderer
+};  // namespace android
+
+#endif  // ANDROID_HWUI_PIXEL_BUFFER_H
diff --git a/libs/hwui/RecordingCanvas.cpp b/libs/hwui/RecordingCanvas.cpp
new file mode 100644
index 0000000..3eaff03
--- /dev/null
+++ b/libs/hwui/RecordingCanvas.cpp
@@ -0,0 +1,934 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#include "RecordingCanvas.h"
+
+#include "SkCanvas.h"
+#include "SkData.h"
+#include "SkDrawShadowInfo.h"
+#include "SkImage.h"
+#include "SkImageFilter.h"
+#include "SkMath.h"
+#include "SkPicture.h"
+#include "SkRSXform.h"
+#include "SkRegion.h"
+#include "SkTextBlob.h"
+#include "SkVertices.h"
+
+#include <experimental/type_traits>
+
+namespace android {
+namespace uirenderer {
+
+#ifndef SKLITEDL_PAGE
+#define SKLITEDL_PAGE 4096
+#endif
+
+// A stand-in for an optional SkRect which was not set, e.g. bounds for a saveLayer().
+static const SkRect kUnset = {SK_ScalarInfinity, 0, 0, 0};
+static const SkRect* maybe_unset(const SkRect& r) {
+    return r.left() == SK_ScalarInfinity ? nullptr : &r;
+}
+
+// copy_v(dst, src,n, src,n, ...) copies an arbitrary number of typed srcs into dst.
+static void copy_v(void* dst) {}
+
+template <typename S, typename... Rest>
+static void copy_v(void* dst, const S* src, int n, Rest&&... rest) {
+    SkASSERTF(((uintptr_t)dst & (alignof(S) - 1)) == 0,
+              "Expected %p to be aligned for at least %zu bytes.", dst, alignof(S));
+    sk_careful_memcpy(dst, src, n * sizeof(S));
+    copy_v(SkTAddOffset<void>(dst, n * sizeof(S)), std::forward<Rest>(rest)...);
+}
+
+// Helper for getting back at arrays which have been copy_v'd together after an Op.
+template <typename D, typename T>
+static const D* pod(const T* op, size_t offset = 0) {
+    return SkTAddOffset<const D>(op + 1, offset);
+}
+
+namespace {
+
+#define X(T) T,
+enum class Type : uint8_t {
+#include "DisplayListOps.in"
+};
+#undef X
+
+struct Op {
+    uint32_t type : 8;
+    uint32_t skip : 24;
+};
+static_assert(sizeof(Op) == 4, "");
+
+struct Flush final : Op {
+    static const auto kType = Type::Flush;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->flush(); }
+};
+
+struct Save final : Op {
+    static const auto kType = Type::Save;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->save(); }
+};
+struct Restore final : Op {
+    static const auto kType = Type::Restore;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->restore(); }
+};
+struct SaveLayer final : Op {
+    static const auto kType = Type::SaveLayer;
+    SaveLayer(const SkRect* bounds, const SkPaint* paint, const SkImageFilter* backdrop,
+              const SkImage* clipMask, const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
+        if (bounds) {
+            this->bounds = *bounds;
+        }
+        if (paint) {
+            this->paint = *paint;
+        }
+        this->backdrop = sk_ref_sp(backdrop);
+        this->clipMask = sk_ref_sp(clipMask);
+        this->clipMatrix = clipMatrix ? *clipMatrix : SkMatrix::I();
+        this->flags = flags;
+    }
+    SkRect bounds = kUnset;
+    SkPaint paint;
+    sk_sp<const SkImageFilter> backdrop;
+    sk_sp<const SkImage> clipMask;
+    SkMatrix clipMatrix;
+    SkCanvas::SaveLayerFlags flags;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->saveLayer({maybe_unset(bounds), &paint, backdrop.get(), clipMask.get(),
+                      clipMatrix.isIdentity() ? nullptr : &clipMatrix, flags});
+    }
+};
+
+struct Concat final : Op {
+    static const auto kType = Type::Concat;
+    Concat(const SkMatrix& matrix) : matrix(matrix) {}
+    SkMatrix matrix;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->concat(matrix); }
+};
+struct SetMatrix final : Op {
+    static const auto kType = Type::SetMatrix;
+    SetMatrix(const SkMatrix& matrix) : matrix(matrix) {}
+    SkMatrix matrix;
+    void draw(SkCanvas* c, const SkMatrix& original) const {
+        c->setMatrix(SkMatrix::Concat(original, matrix));
+    }
+};
+struct Translate final : Op {
+    static const auto kType = Type::Translate;
+    Translate(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {}
+    SkScalar dx, dy;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->translate(dx, dy); }
+};
+
+struct ClipPath final : Op {
+    static const auto kType = Type::ClipPath;
+    ClipPath(const SkPath& path, SkClipOp op, bool aa) : path(path), op(op), aa(aa) {}
+    SkPath path;
+    SkClipOp op;
+    bool aa;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->clipPath(path, op, aa); }
+};
+struct ClipRect final : Op {
+    static const auto kType = Type::ClipRect;
+    ClipRect(const SkRect& rect, SkClipOp op, bool aa) : rect(rect), op(op), aa(aa) {}
+    SkRect rect;
+    SkClipOp op;
+    bool aa;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->clipRect(rect, op, aa); }
+};
+struct ClipRRect final : Op {
+    static const auto kType = Type::ClipRRect;
+    ClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) : rrect(rrect), op(op), aa(aa) {}
+    SkRRect rrect;
+    SkClipOp op;
+    bool aa;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->clipRRect(rrect, op, aa); }
+};
+struct ClipRegion final : Op {
+    static const auto kType = Type::ClipRegion;
+    ClipRegion(const SkRegion& region, SkClipOp op) : region(region), op(op) {}
+    SkRegion region;
+    SkClipOp op;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->clipRegion(region, op); }
+};
+
+struct DrawPaint final : Op {
+    static const auto kType = Type::DrawPaint;
+    DrawPaint(const SkPaint& paint) : paint(paint) {}
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawPaint(paint); }
+};
+struct DrawPath final : Op {
+    static const auto kType = Type::DrawPath;
+    DrawPath(const SkPath& path, const SkPaint& paint) : path(path), paint(paint) {}
+    SkPath path;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawPath(path, paint); }
+};
+struct DrawRect final : Op {
+    static const auto kType = Type::DrawRect;
+    DrawRect(const SkRect& rect, const SkPaint& paint) : rect(rect), paint(paint) {}
+    SkRect rect;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawRect(rect, paint); }
+};
+struct DrawRegion final : Op {
+    static const auto kType = Type::DrawRegion;
+    DrawRegion(const SkRegion& region, const SkPaint& paint) : region(region), paint(paint) {}
+    SkRegion region;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawRegion(region, paint); }
+};
+struct DrawOval final : Op {
+    static const auto kType = Type::DrawOval;
+    DrawOval(const SkRect& oval, const SkPaint& paint) : oval(oval), paint(paint) {}
+    SkRect oval;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawOval(oval, paint); }
+};
+struct DrawArc final : Op {
+    static const auto kType = Type::DrawArc;
+    DrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter,
+            const SkPaint& paint)
+            : oval(oval)
+            , startAngle(startAngle)
+            , sweepAngle(sweepAngle)
+            , useCenter(useCenter)
+            , paint(paint) {}
+    SkRect oval;
+    SkScalar startAngle;
+    SkScalar sweepAngle;
+    bool useCenter;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
+    }
+};
+struct DrawRRect final : Op {
+    static const auto kType = Type::DrawRRect;
+    DrawRRect(const SkRRect& rrect, const SkPaint& paint) : rrect(rrect), paint(paint) {}
+    SkRRect rrect;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawRRect(rrect, paint); }
+};
+struct DrawDRRect final : Op {
+    static const auto kType = Type::DrawDRRect;
+    DrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint)
+            : outer(outer), inner(inner), paint(paint) {}
+    SkRRect outer, inner;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawDRRect(outer, inner, paint); }
+};
+
+struct DrawAnnotation final : Op {
+    static const auto kType = Type::DrawAnnotation;
+    DrawAnnotation(const SkRect& rect, SkData* value) : rect(rect), value(sk_ref_sp(value)) {}
+    SkRect rect;
+    sk_sp<SkData> value;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawAnnotation(rect, pod<char>(this), value.get());
+    }
+};
+struct DrawDrawable final : Op {
+    static const auto kType = Type::DrawDrawable;
+    DrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) : drawable(sk_ref_sp(drawable)) {
+        if (matrix) {
+            this->matrix = *matrix;
+        }
+    }
+    sk_sp<SkDrawable> drawable;
+    SkMatrix matrix = SkMatrix::I();
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawDrawable(drawable.get(), &matrix); }
+};
+struct DrawPicture final : Op {
+    static const auto kType = Type::DrawPicture;
+    DrawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint)
+            : picture(sk_ref_sp(picture)) {
+        if (matrix) {
+            this->matrix = *matrix;
+        }
+        if (paint) {
+            this->paint = *paint;
+            has_paint = true;
+        }
+    }
+    sk_sp<const SkPicture> picture;
+    SkMatrix matrix = SkMatrix::I();
+    SkPaint paint;
+    bool has_paint = false;  // TODO: why is a default paint not the same?
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawPicture(picture.get(), &matrix, has_paint ? &paint : nullptr);
+    }
+};
+
+struct DrawImage final : Op {
+    static const auto kType = Type::DrawImage;
+    DrawImage(sk_sp<const SkImage>&& image, SkScalar x, SkScalar y, const SkPaint* paint)
+            : image(std::move(image)), x(x), y(y) {
+        if (paint) {
+            this->paint = *paint;
+        }
+    }
+    sk_sp<const SkImage> image;
+    SkScalar x, y;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawImage(image.get(), x, y, &paint); }
+};
+struct DrawImageNine final : Op {
+    static const auto kType = Type::DrawImageNine;
+    DrawImageNine(sk_sp<const SkImage>&& image, const SkIRect& center, const SkRect& dst,
+                  const SkPaint* paint)
+            : image(std::move(image)), center(center), dst(dst) {
+        if (paint) {
+            this->paint = *paint;
+        }
+    }
+    sk_sp<const SkImage> image;
+    SkIRect center;
+    SkRect dst;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawImageNine(image.get(), center, dst, &paint);
+    }
+};
+struct DrawImageRect final : Op {
+    static const auto kType = Type::DrawImageRect;
+    DrawImageRect(sk_sp<const SkImage>&& image, const SkRect* src, const SkRect& dst,
+                  const SkPaint* paint, SkCanvas::SrcRectConstraint constraint)
+            : image(std::move(image)), dst(dst), constraint(constraint) {
+        this->src = src ? *src : SkRect::MakeIWH(this->image->width(), this->image->height());
+        if (paint) {
+            this->paint = *paint;
+        }
+    }
+    sk_sp<const SkImage> image;
+    SkRect src, dst;
+    SkPaint paint;
+    SkCanvas::SrcRectConstraint constraint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawImageRect(image.get(), src, dst, &paint, constraint);
+    }
+};
+struct DrawImageLattice final : Op {
+    static const auto kType = Type::DrawImageLattice;
+    DrawImageLattice(sk_sp<const SkImage>&& image, int xs, int ys, int fs, const SkIRect& src,
+                     const SkRect& dst, const SkPaint* paint)
+            : image(std::move(image)), xs(xs), ys(ys), fs(fs), src(src), dst(dst) {
+        if (paint) {
+            this->paint = *paint;
+        }
+    }
+    sk_sp<const SkImage> image;
+    int xs, ys, fs;
+    SkIRect src;
+    SkRect dst;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        auto xdivs = pod<int>(this, 0), ydivs = pod<int>(this, xs * sizeof(int));
+        auto colors = (0 == fs) ? nullptr : pod<SkColor>(this, (xs + ys) * sizeof(int));
+        auto flags =
+                (0 == fs) ? nullptr : pod<SkCanvas::Lattice::RectType>(
+                                              this, (xs + ys) * sizeof(int) + fs * sizeof(SkColor));
+        c->drawImageLattice(image.get(), {xdivs, ydivs, flags, xs, ys, &src, colors}, dst, &paint);
+    }
+};
+
+struct DrawText final : Op {
+    static const auto kType = Type::DrawText;
+    DrawText(size_t bytes, SkScalar x, SkScalar y, const SkPaint& paint)
+            : bytes(bytes), x(x), y(y), paint(paint) {}
+    size_t bytes;
+    SkScalar x, y;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawText(pod<void>(this), bytes, x, y, paint);
+    }
+};
+struct DrawPosText final : Op {
+    static const auto kType = Type::DrawPosText;
+    DrawPosText(size_t bytes, const SkPaint& paint, int n) : bytes(bytes), paint(paint), n(n) {}
+    size_t bytes;
+    SkPaint paint;
+    int n;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        auto points = pod<SkPoint>(this);
+        auto text = pod<void>(this, n * sizeof(SkPoint));
+        c->drawPosText(text, bytes, points, paint);
+    }
+};
+struct DrawPosTextH final : Op {
+    static const auto kType = Type::DrawPosTextH;
+    DrawPosTextH(size_t bytes, SkScalar y, const SkPaint& paint, int n)
+            : bytes(bytes), y(y), paint(paint), n(n) {}
+    size_t bytes;
+    SkScalar y;
+    SkPaint paint;
+    int n;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        auto xs = pod<SkScalar>(this);
+        auto text = pod<void>(this, n * sizeof(SkScalar));
+        c->drawPosTextH(text, bytes, xs, y, paint);
+    }
+};
+struct DrawTextRSXform final : Op {
+    static const auto kType = Type::DrawTextRSXform;
+    DrawTextRSXform(size_t bytes, int xforms, const SkRect* cull, const SkPaint& paint)
+            : bytes(bytes), xforms(xforms), paint(paint) {
+        if (cull) {
+            this->cull = *cull;
+        }
+    }
+    size_t bytes;
+    int xforms;
+    SkRect cull = kUnset;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        // For alignment, the SkRSXforms are first in the pod section, followed by the text.
+        c->drawTextRSXform(pod<void>(this, xforms * sizeof(SkRSXform)), bytes, pod<SkRSXform>(this),
+                           maybe_unset(cull), paint);
+    }
+};
+struct DrawTextBlob final : Op {
+    static const auto kType = Type::DrawTextBlob;
+    DrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, const SkPaint& paint)
+            : blob(sk_ref_sp(blob)), x(x), y(y), paint(paint) {}
+    sk_sp<const SkTextBlob> blob;
+    SkScalar x, y;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->drawTextBlob(blob.get(), x, y, paint); }
+};
+
+struct DrawPatch final : Op {
+    static const auto kType = Type::DrawPatch;
+    DrawPatch(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texs[4],
+              SkBlendMode bmode, const SkPaint& paint)
+            : xfermode(bmode), paint(paint) {
+        copy_v(this->cubics, cubics, 12);
+        if (colors) {
+            copy_v(this->colors, colors, 4);
+            has_colors = true;
+        }
+        if (texs) {
+            copy_v(this->texs, texs, 4);
+            has_texs = true;
+        }
+    }
+    SkPoint cubics[12];
+    SkColor colors[4];
+    SkPoint texs[4];
+    SkBlendMode xfermode;
+    SkPaint paint;
+    bool has_colors = false;
+    bool has_texs = false;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawPatch(cubics, has_colors ? colors : nullptr, has_texs ? texs : nullptr, xfermode,
+                     paint);
+    }
+};
+struct DrawPoints final : Op {
+    static const auto kType = Type::DrawPoints;
+    DrawPoints(SkCanvas::PointMode mode, size_t count, const SkPaint& paint)
+            : mode(mode), count(count), paint(paint) {}
+    SkCanvas::PointMode mode;
+    size_t count;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawPoints(mode, count, pod<SkPoint>(this), paint);
+    }
+};
+struct DrawVertices final : Op {
+    static const auto kType = Type::DrawVertices;
+    DrawVertices(const SkVertices* v, int bc, SkBlendMode m, const SkPaint& p)
+            : vertices(sk_ref_sp(const_cast<SkVertices*>(v))), boneCount(bc), mode(m), paint(p) {}
+    sk_sp<SkVertices> vertices;
+    int boneCount;
+    SkBlendMode mode;
+    SkPaint paint;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        c->drawVertices(vertices, pod<SkVertices::Bone>(this), boneCount, mode, paint);
+    }
+};
+struct DrawAtlas final : Op {
+    static const auto kType = Type::DrawAtlas;
+    DrawAtlas(const SkImage* atlas, int count, SkBlendMode xfermode, const SkRect* cull,
+              const SkPaint* paint, bool has_colors)
+            : atlas(sk_ref_sp(atlas)), count(count), xfermode(xfermode), has_colors(has_colors) {
+        if (cull) {
+            this->cull = *cull;
+        }
+        if (paint) {
+            this->paint = *paint;
+        }
+    }
+    sk_sp<const SkImage> atlas;
+    int count;
+    SkBlendMode xfermode;
+    SkRect cull = kUnset;
+    SkPaint paint;
+    bool has_colors;
+    void draw(SkCanvas* c, const SkMatrix&) const {
+        auto xforms = pod<SkRSXform>(this, 0);
+        auto texs = pod<SkRect>(this, count * sizeof(SkRSXform));
+        auto colors = has_colors ? pod<SkColor>(this, count * (sizeof(SkRSXform) + sizeof(SkRect)))
+                                 : nullptr;
+        c->drawAtlas(atlas.get(), xforms, texs, colors, count, xfermode, maybe_unset(cull), &paint);
+    }
+};
+struct DrawShadowRec final : Op {
+    static const auto kType = Type::DrawShadowRec;
+    DrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) : fPath(path), fRec(rec) {}
+    SkPath fPath;
+    SkDrawShadowRec fRec;
+    void draw(SkCanvas* c, const SkMatrix&) const { c->private_draw_shadow_rec(fPath, fRec); }
+};
+}
+
+template <typename T, typename... Args>
+void* DisplayListData::push(size_t pod, Args&&... args) {
+    size_t skip = SkAlignPtr(sizeof(T) + pod);
+    SkASSERT(skip < (1 << 24));
+    if (fUsed + skip > fReserved) {
+        static_assert(SkIsPow2(SKLITEDL_PAGE), "This math needs updating for non-pow2.");
+        // Next greater multiple of SKLITEDL_PAGE.
+        fReserved = (fUsed + skip + SKLITEDL_PAGE) & ~(SKLITEDL_PAGE - 1);
+        fBytes.realloc(fReserved);
+    }
+    SkASSERT(fUsed + skip <= fReserved);
+    auto op = (T*)(fBytes.get() + fUsed);
+    fUsed += skip;
+    new (op) T{std::forward<Args>(args)...};
+    op->type = (uint32_t)T::kType;
+    op->skip = skip;
+    return op + 1;
+}
+
+template <typename Fn, typename... Args>
+inline void DisplayListData::map(const Fn fns[], Args... args) const {
+    auto end = fBytes.get() + fUsed;
+    for (const uint8_t* ptr = fBytes.get(); ptr < end;) {
+        auto op = (const Op*)ptr;
+        auto type = op->type;
+        auto skip = op->skip;
+        if (auto fn = fns[type]) {  // We replace no-op functions with nullptrs
+            fn(op, args...);        // to avoid the overhead of a pointless call.
+        }
+        ptr += skip;
+    }
+}
+
+void DisplayListData::flush() {
+    this->push<Flush>(0);
+}
+
+void DisplayListData::save() {
+    this->push<Save>(0);
+}
+void DisplayListData::restore() {
+    this->push<Restore>(0);
+}
+void DisplayListData::saveLayer(const SkRect* bounds, const SkPaint* paint,
+                                const SkImageFilter* backdrop, const SkImage* clipMask,
+                                const SkMatrix* clipMatrix, SkCanvas::SaveLayerFlags flags) {
+    this->push<SaveLayer>(0, bounds, paint, backdrop, clipMask, clipMatrix, flags);
+}
+
+void DisplayListData::concat(const SkMatrix& matrix) {
+    this->push<Concat>(0, matrix);
+}
+void DisplayListData::setMatrix(const SkMatrix& matrix) {
+    this->push<SetMatrix>(0, matrix);
+}
+void DisplayListData::translate(SkScalar dx, SkScalar dy) {
+    this->push<Translate>(0, dx, dy);
+}
+
+void DisplayListData::clipPath(const SkPath& path, SkClipOp op, bool aa) {
+    this->push<ClipPath>(0, path, op, aa);
+}
+void DisplayListData::clipRect(const SkRect& rect, SkClipOp op, bool aa) {
+    this->push<ClipRect>(0, rect, op, aa);
+}
+void DisplayListData::clipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
+    this->push<ClipRRect>(0, rrect, op, aa);
+}
+void DisplayListData::clipRegion(const SkRegion& region, SkClipOp op) {
+    this->push<ClipRegion>(0, region, op);
+}
+
+void DisplayListData::drawPaint(const SkPaint& paint) {
+    this->push<DrawPaint>(0, paint);
+}
+void DisplayListData::drawPath(const SkPath& path, const SkPaint& paint) {
+    this->push<DrawPath>(0, path, paint);
+}
+void DisplayListData::drawRect(const SkRect& rect, const SkPaint& paint) {
+    this->push<DrawRect>(0, rect, paint);
+}
+void DisplayListData::drawRegion(const SkRegion& region, const SkPaint& paint) {
+    this->push<DrawRegion>(0, region, paint);
+}
+void DisplayListData::drawOval(const SkRect& oval, const SkPaint& paint) {
+    this->push<DrawOval>(0, oval, paint);
+}
+void DisplayListData::drawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
+                              bool useCenter, const SkPaint& paint) {
+    this->push<DrawArc>(0, oval, startAngle, sweepAngle, useCenter, paint);
+}
+void DisplayListData::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
+    this->push<DrawRRect>(0, rrect, paint);
+}
+void DisplayListData::drawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
+    this->push<DrawDRRect>(0, outer, inner, paint);
+}
+
+void DisplayListData::drawAnnotation(const SkRect& rect, const char* key, SkData* value) {
+    size_t bytes = strlen(key) + 1;
+    void* pod = this->push<DrawAnnotation>(bytes, rect, value);
+    copy_v(pod, key, bytes);
+}
+void DisplayListData::drawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
+    this->push<DrawDrawable>(0, drawable, matrix);
+}
+void DisplayListData::drawPicture(const SkPicture* picture, const SkMatrix* matrix,
+                                  const SkPaint* paint) {
+    this->push<DrawPicture>(0, picture, matrix, paint);
+}
+void DisplayListData::drawImage(sk_sp<const SkImage> image, SkScalar x, SkScalar y,
+                                const SkPaint* paint) {
+    this->push<DrawImage>(0, std::move(image), x, y, paint);
+}
+void DisplayListData::drawImageNine(sk_sp<const SkImage> image, const SkIRect& center,
+                                    const SkRect& dst, const SkPaint* paint) {
+    this->push<DrawImageNine>(0, std::move(image), center, dst, paint);
+}
+void DisplayListData::drawImageRect(sk_sp<const SkImage> image, const SkRect* src,
+                                    const SkRect& dst, const SkPaint* paint,
+                                    SkCanvas::SrcRectConstraint constraint) {
+    this->push<DrawImageRect>(0, std::move(image), src, dst, paint, constraint);
+}
+void DisplayListData::drawImageLattice(sk_sp<const SkImage> image, const SkCanvas::Lattice& lattice,
+                                       const SkRect& dst, const SkPaint* paint) {
+    int xs = lattice.fXCount, ys = lattice.fYCount;
+    int fs = lattice.fRectTypes ? (xs + 1) * (ys + 1) : 0;
+    size_t bytes = (xs + ys) * sizeof(int) + fs * sizeof(SkCanvas::Lattice::RectType) +
+                   fs * sizeof(SkColor);
+    SkASSERT(lattice.fBounds);
+    void* pod = this->push<DrawImageLattice>(bytes, std::move(image), xs, ys, fs, *lattice.fBounds,
+                                             dst, paint);
+    copy_v(pod, lattice.fXDivs, xs, lattice.fYDivs, ys, lattice.fColors, fs, lattice.fRectTypes,
+           fs);
+}
+
+void DisplayListData::drawText(const void* text, size_t bytes, SkScalar x, SkScalar y,
+                               const SkPaint& paint) {
+    void* pod = this->push<DrawText>(bytes, bytes, x, y, paint);
+    copy_v(pod, (const char*)text, bytes);
+}
+void DisplayListData::drawPosText(const void* text, size_t bytes, const SkPoint pos[],
+                                  const SkPaint& paint) {
+    int n = paint.countText(text, bytes);
+    void* pod = this->push<DrawPosText>(n * sizeof(SkPoint) + bytes, bytes, paint, n);
+    copy_v(pod, pos, n, (const char*)text, bytes);
+}
+void DisplayListData::drawPosTextH(const void* text, size_t bytes, const SkScalar xs[], SkScalar y,
+                                   const SkPaint& paint) {
+    int n = paint.countText(text, bytes);
+    void* pod = this->push<DrawPosTextH>(n * sizeof(SkScalar) + bytes, bytes, y, paint, n);
+    copy_v(pod, xs, n, (const char*)text, bytes);
+}
+void DisplayListData::drawTextRSXform(const void* text, size_t bytes, const SkRSXform xforms[],
+                                      const SkRect* cull, const SkPaint& paint) {
+    int n = paint.countText(text, bytes);
+    void* pod = this->push<DrawTextRSXform>(bytes + n * sizeof(SkRSXform), bytes, n, cull, paint);
+    copy_v(pod, xforms, n, (const char*)text, bytes);
+}
+void DisplayListData::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
+                                   const SkPaint& paint) {
+    this->push<DrawTextBlob>(0, blob, x, y, paint);
+}
+
+void DisplayListData::drawPatch(const SkPoint points[12], const SkColor colors[4],
+                                const SkPoint texs[4], SkBlendMode bmode, const SkPaint& paint) {
+    this->push<DrawPatch>(0, points, colors, texs, bmode, paint);
+}
+void DisplayListData::drawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint points[],
+                                 const SkPaint& paint) {
+    void* pod = this->push<DrawPoints>(count * sizeof(SkPoint), mode, count, paint);
+    copy_v(pod, points, count);
+}
+void DisplayListData::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
+                                   int boneCount, SkBlendMode mode, const SkPaint& paint) {
+    void* pod = this->push<DrawVertices>(boneCount * sizeof(SkVertices::Bone), vertices, boneCount,
+                                         mode, paint);
+    copy_v(pod, bones, boneCount);
+}
+void DisplayListData::drawAtlas(const SkImage* atlas, const SkRSXform xforms[], const SkRect texs[],
+                                const SkColor colors[], int count, SkBlendMode xfermode,
+                                const SkRect* cull, const SkPaint* paint) {
+    size_t bytes = count * (sizeof(SkRSXform) + sizeof(SkRect));
+    if (colors) {
+        bytes += count * sizeof(SkColor);
+    }
+    void* pod =
+            this->push<DrawAtlas>(bytes, atlas, count, xfermode, cull, paint, colors != nullptr);
+    copy_v(pod, xforms, count, texs, count, colors, colors ? count : 0);
+}
+void DisplayListData::drawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
+    this->push<DrawShadowRec>(0, path, rec);
+}
+
+typedef void (*draw_fn)(const void*, SkCanvas*, const SkMatrix&);
+typedef void (*void_fn)(const void*);
+typedef void (*color_transform_fn)(const void*, ColorTransform);
+
+// All ops implement draw().
+#define X(T)                                                    \
+    [](const void* op, SkCanvas* c, const SkMatrix& original) { \
+        ((const T*)op)->draw(c, original);                      \
+    },
+static const draw_fn draw_fns[] = {
+#include "DisplayListOps.in"
+};
+#undef X
+
+// Most state ops (matrix, clip, save, restore) have a trivial destructor.
+#define X(T)                                                                                 \
+    !std::is_trivially_destructible<T>::value ? [](const void* op) { ((const T*)op)->~T(); } \
+                                              : (void_fn) nullptr,
+
+static const void_fn dtor_fns[] = {
+#include "DisplayListOps.in"
+};
+#undef X
+
+void DisplayListData::draw(SkCanvas* canvas) const {
+    SkAutoCanvasRestore acr(canvas, false);
+    this->map(draw_fns, canvas, canvas->getTotalMatrix());
+}
+
+DisplayListData::~DisplayListData() {
+    this->reset();
+}
+
+void DisplayListData::reset() {
+    this->map(dtor_fns);
+
+    // Leave fBytes and fReserved alone.
+    fUsed = 0;
+}
+
+template <class T>
+using has_paint_t = decltype(std::declval<T>().paint);
+
+template <class T>
+constexpr color_transform_fn colorTransformForOp() {
+    if
+        constexpr(std::experimental::is_detected_v<has_paint_t, T>) {
+            return [](const void* op, ColorTransform transform) {
+                // TODO: We should be const. Or not. Or just use a different map
+                // Unclear, but this is the quick fix
+                transformPaint(transform,
+                               const_cast<SkPaint*>(&(reinterpret_cast<const T*>(op)->paint)));
+            };
+        }
+    else {
+        return nullptr;
+    }
+}
+
+#define X(T) colorTransformForOp<T>(),
+static const color_transform_fn color_transform_fns[] = {
+#include "DisplayListOps.in"
+};
+#undef X
+
+void DisplayListData::applyColorTransform(ColorTransform transform) {
+    this->map(color_transform_fns, transform);
+}
+
+RecordingCanvas::RecordingCanvas() : INHERITED(1, 1), fDL(nullptr) {}
+
+void RecordingCanvas::reset(DisplayListData* dl, const SkIRect& bounds) {
+    this->resetCanvas(bounds.right(), bounds.bottom());
+    fDL = dl;
+}
+
+sk_sp<SkSurface> RecordingCanvas::onNewSurface(const SkImageInfo&, const SkSurfaceProps&) {
+    return nullptr;
+}
+
+void RecordingCanvas::onFlush() {
+    fDL->flush();
+}
+
+void RecordingCanvas::willSave() {
+    fDL->save();
+}
+SkCanvas::SaveLayerStrategy RecordingCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
+    fDL->saveLayer(rec.fBounds, rec.fPaint, rec.fBackdrop, rec.fClipMask, rec.fClipMatrix,
+                   rec.fSaveLayerFlags);
+    return SkCanvas::kNoLayer_SaveLayerStrategy;
+}
+void RecordingCanvas::willRestore() {
+    fDL->restore();
+}
+
+void RecordingCanvas::didConcat(const SkMatrix& matrix) {
+    fDL->concat(matrix);
+}
+void RecordingCanvas::didSetMatrix(const SkMatrix& matrix) {
+    fDL->setMatrix(matrix);
+}
+void RecordingCanvas::didTranslate(SkScalar dx, SkScalar dy) {
+    fDL->translate(dx, dy);
+}
+
+void RecordingCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle style) {
+    fDL->clipRect(rect, op, style == kSoft_ClipEdgeStyle);
+    this->INHERITED::onClipRect(rect, op, style);
+}
+void RecordingCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle style) {
+    fDL->clipRRect(rrect, op, style == kSoft_ClipEdgeStyle);
+    this->INHERITED::onClipRRect(rrect, op, style);
+}
+void RecordingCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle style) {
+    fDL->clipPath(path, op, style == kSoft_ClipEdgeStyle);
+    this->INHERITED::onClipPath(path, op, style);
+}
+void RecordingCanvas::onClipRegion(const SkRegion& region, SkClipOp op) {
+    fDL->clipRegion(region, op);
+    this->INHERITED::onClipRegion(region, op);
+}
+
+void RecordingCanvas::onDrawPaint(const SkPaint& paint) {
+    fDL->drawPaint(paint);
+}
+void RecordingCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
+    fDL->drawPath(path, paint);
+}
+void RecordingCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
+    fDL->drawRect(rect, paint);
+}
+void RecordingCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) {
+    fDL->drawRegion(region, paint);
+}
+void RecordingCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) {
+    fDL->drawOval(oval, paint);
+}
+void RecordingCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle,
+                                bool useCenter, const SkPaint& paint) {
+    fDL->drawArc(oval, startAngle, sweepAngle, useCenter, paint);
+}
+void RecordingCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
+    fDL->drawRRect(rrect, paint);
+}
+void RecordingCanvas::onDrawDRRect(const SkRRect& out, const SkRRect& in, const SkPaint& paint) {
+    fDL->drawDRRect(out, in, paint);
+}
+
+void RecordingCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
+    fDL->drawDrawable(drawable, matrix);
+}
+void RecordingCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
+                                    const SkPaint* paint) {
+    fDL->drawPicture(picture, matrix, paint);
+}
+void RecordingCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* val) {
+    fDL->drawAnnotation(rect, key, val);
+}
+
+void RecordingCanvas::onDrawText(const void* text, size_t bytes, SkScalar x, SkScalar y,
+                                 const SkPaint& paint) {
+    fDL->drawText(text, bytes, x, y, paint);
+}
+void RecordingCanvas::onDrawPosText(const void* text, size_t bytes, const SkPoint pos[],
+                                    const SkPaint& paint) {
+    fDL->drawPosText(text, bytes, pos, paint);
+}
+void RecordingCanvas::onDrawPosTextH(const void* text, size_t bytes, const SkScalar xs[],
+                                     SkScalar y, const SkPaint& paint) {
+    fDL->drawPosTextH(text, bytes, xs, y, paint);
+}
+void RecordingCanvas::onDrawTextRSXform(const void* text, size_t bytes, const SkRSXform xform[],
+                                        const SkRect* cull, const SkPaint& paint) {
+    fDL->drawTextRSXform(text, bytes, xform, cull, paint);
+}
+void RecordingCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
+                                     const SkPaint& paint) {
+    fDL->drawTextBlob(blob, x, y, paint);
+}
+
+void RecordingCanvas::onDrawBitmap(const SkBitmap& bm, SkScalar x, SkScalar y,
+                                   const SkPaint* paint) {
+    fDL->drawImage(SkImage::MakeFromBitmap(bm), x, y, paint);
+}
+void RecordingCanvas::onDrawBitmapNine(const SkBitmap& bm, const SkIRect& center, const SkRect& dst,
+                                       const SkPaint* paint) {
+    fDL->drawImageNine(SkImage::MakeFromBitmap(bm), center, dst, paint);
+}
+void RecordingCanvas::onDrawBitmapRect(const SkBitmap& bm, const SkRect* src, const SkRect& dst,
+                                       const SkPaint* paint, SrcRectConstraint constraint) {
+    fDL->drawImageRect(SkImage::MakeFromBitmap(bm), src, dst, paint, constraint);
+}
+void RecordingCanvas::onDrawBitmapLattice(const SkBitmap& bm, const SkCanvas::Lattice& lattice,
+                                          const SkRect& dst, const SkPaint* paint) {
+    fDL->drawImageLattice(SkImage::MakeFromBitmap(bm), lattice, dst, paint);
+}
+
+void RecordingCanvas::onDrawImage(const SkImage* img, SkScalar x, SkScalar y,
+                                  const SkPaint* paint) {
+    fDL->drawImage(sk_ref_sp(img), x, y, paint);
+}
+void RecordingCanvas::onDrawImageNine(const SkImage* img, const SkIRect& center, const SkRect& dst,
+                                      const SkPaint* paint) {
+    fDL->drawImageNine(sk_ref_sp(img), center, dst, paint);
+}
+void RecordingCanvas::onDrawImageRect(const SkImage* img, const SkRect* src, const SkRect& dst,
+                                      const SkPaint* paint, SrcRectConstraint constraint) {
+    fDL->drawImageRect(sk_ref_sp(img), src, dst, paint, constraint);
+}
+void RecordingCanvas::onDrawImageLattice(const SkImage* img, const SkCanvas::Lattice& lattice,
+                                         const SkRect& dst, const SkPaint* paint) {
+    fDL->drawImageLattice(sk_ref_sp(img), lattice, dst, paint);
+}
+
+void RecordingCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
+                                  const SkPoint texCoords[4], SkBlendMode bmode,
+                                  const SkPaint& paint) {
+    fDL->drawPatch(cubics, colors, texCoords, bmode, paint);
+}
+void RecordingCanvas::onDrawPoints(SkCanvas::PointMode mode, size_t count, const SkPoint pts[],
+                                   const SkPaint& paint) {
+    fDL->drawPoints(mode, count, pts, paint);
+}
+void RecordingCanvas::onDrawVerticesObject(const SkVertices* vertices,
+                                          const SkVertices::Bone bones[], int boneCount,
+                                          SkBlendMode mode, const SkPaint& paint) {
+    fDL->drawVertices(vertices, bones, boneCount, mode, paint);
+}
+void RecordingCanvas::onDrawAtlas(const SkImage* atlas, const SkRSXform xforms[],
+                                  const SkRect texs[], const SkColor colors[], int count,
+                                  SkBlendMode bmode, const SkRect* cull, const SkPaint* paint) {
+    fDL->drawAtlas(atlas, xforms, texs, colors, count, bmode, cull, paint);
+}
+void RecordingCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) {
+    fDL->drawShadowRec(path, rec);
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/RecordingCanvas.h b/libs/hwui/RecordingCanvas.h
new file mode 100644
index 0000000..32ce1d3
--- /dev/null
+++ b/libs/hwui/RecordingCanvas.h
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include "CanvasTransform.h"
+#include "hwui/Canvas.h"
+#include "utils/Macros.h"
+#include "utils/TypeLogic.h"
+
+#include "SkCanvas.h"
+#include "SkCanvasVirtualEnforcer.h"
+#include "SkDrawable.h"
+#include "SkNoDrawCanvas.h"
+#include "SkPaint.h"
+#include "SkPath.h"
+#include "SkRect.h"
+#include "SkTDArray.h"
+#include "SkTemplates.h"
+
+#include <vector>
+
+namespace android {
+namespace uirenderer {
+
+enum class DisplayListOpType : uint8_t {
+#define X(T) T,
+#include "DisplayListOps.in"
+#undef X
+};
+
+struct DisplayListOp {
+    const uint8_t type : 8;
+    const uint32_t skip : 24;
+};
+
+static_assert(sizeof(DisplayListOp) == 4);
+
+class RecordingCanvas;
+
+class DisplayListData final {
+public:
+    ~DisplayListData();
+
+    void draw(SkCanvas* canvas) const;
+
+    void reset();
+    bool empty() const { return fUsed == 0; }
+
+    void applyColorTransform(ColorTransform transform);
+
+private:
+    friend class RecordingCanvas;
+
+    void flush();
+
+    void save();
+    void saveLayer(const SkRect*, const SkPaint*, const SkImageFilter*, const SkImage*,
+                   const SkMatrix*, SkCanvas::SaveLayerFlags);
+    void restore();
+
+    void concat(const SkMatrix&);
+    void setMatrix(const SkMatrix&);
+    void translate(SkScalar, SkScalar);
+    void translateZ(SkScalar);
+
+    void clipPath(const SkPath&, SkClipOp, bool aa);
+    void clipRect(const SkRect&, SkClipOp, bool aa);
+    void clipRRect(const SkRRect&, SkClipOp, bool aa);
+    void clipRegion(const SkRegion&, SkClipOp);
+
+    void drawPaint(const SkPaint&);
+    void drawPath(const SkPath&, const SkPaint&);
+    void drawRect(const SkRect&, const SkPaint&);
+    void drawRegion(const SkRegion&, const SkPaint&);
+    void drawOval(const SkRect&, const SkPaint&);
+    void drawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&);
+    void drawRRect(const SkRRect&, const SkPaint&);
+    void drawDRRect(const SkRRect&, const SkRRect&, const SkPaint&);
+
+    void drawAnnotation(const SkRect&, const char*, SkData*);
+    void drawDrawable(SkDrawable*, const SkMatrix*);
+    void drawPicture(const SkPicture*, const SkMatrix*, const SkPaint*);
+
+    void drawText(const void*, size_t, SkScalar, SkScalar, const SkPaint&);
+    void drawPosText(const void*, size_t, const SkPoint[], const SkPaint&);
+    void drawPosTextH(const void*, size_t, const SkScalar[], SkScalar, const SkPaint&);
+    void drawTextRSXform(const void*, size_t, const SkRSXform[], const SkRect*, const SkPaint&);
+    void drawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&);
+
+    void drawImage(sk_sp<const SkImage>, SkScalar, SkScalar, const SkPaint*);
+    void drawImageNine(sk_sp<const SkImage>, const SkIRect&, const SkRect&, const SkPaint*);
+    void drawImageRect(sk_sp<const SkImage>, const SkRect*, const SkRect&, const SkPaint*,
+                       SkCanvas::SrcRectConstraint);
+    void drawImageLattice(sk_sp<const SkImage>, const SkCanvas::Lattice&, const SkRect&,
+                          const SkPaint*);
+
+    void drawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode,
+                   const SkPaint&);
+    void drawPoints(SkCanvas::PointMode, size_t, const SkPoint[], const SkPaint&);
+    void drawVertices(const SkVertices*, const SkVertices::Bone bones[], int boneCount, SkBlendMode,
+                      const SkPaint&);
+    void drawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
+                   SkBlendMode, const SkRect*, const SkPaint*);
+    void drawShadowRec(const SkPath&, const SkDrawShadowRec&);
+
+    template <typename T, typename... Args>
+    void* push(size_t, Args&&...);
+
+    template <typename Fn, typename... Args>
+    void map(const Fn[], Args...) const;
+
+    SkAutoTMalloc<uint8_t> fBytes;
+    size_t fUsed = 0;
+    size_t fReserved = 0;
+};
+
+class RecordingCanvas final : public SkCanvasVirtualEnforcer<SkNoDrawCanvas> {
+public:
+    RecordingCanvas();
+    void reset(DisplayListData*, const SkIRect& bounds);
+
+    sk_sp<SkSurface> onNewSurface(const SkImageInfo&, const SkSurfaceProps&) override;
+
+    void willSave() override;
+    SaveLayerStrategy getSaveLayerStrategy(const SaveLayerRec&) override;
+    void willRestore() override;
+
+    void onFlush() override;
+
+    void didConcat(const SkMatrix&) override;
+    void didSetMatrix(const SkMatrix&) override;
+    void didTranslate(SkScalar, SkScalar) override;
+
+    void onClipRect(const SkRect&, SkClipOp, ClipEdgeStyle) override;
+    void onClipRRect(const SkRRect&, SkClipOp, ClipEdgeStyle) override;
+    void onClipPath(const SkPath&, SkClipOp, ClipEdgeStyle) override;
+    void onClipRegion(const SkRegion&, SkClipOp) override;
+
+    void onDrawPaint(const SkPaint&) override;
+    void onDrawPath(const SkPath&, const SkPaint&) override;
+    void onDrawRect(const SkRect&, const SkPaint&) override;
+    void onDrawRegion(const SkRegion&, const SkPaint&) override;
+    void onDrawOval(const SkRect&, const SkPaint&) override;
+    void onDrawArc(const SkRect&, SkScalar, SkScalar, bool, const SkPaint&) override;
+    void onDrawRRect(const SkRRect&, const SkPaint&) override;
+    void onDrawDRRect(const SkRRect&, const SkRRect&, const SkPaint&) override;
+
+    void onDrawDrawable(SkDrawable*, const SkMatrix*) override;
+    void onDrawPicture(const SkPicture*, const SkMatrix*, const SkPaint*) override;
+    void onDrawAnnotation(const SkRect&, const char[], SkData*) override;
+
+    void onDrawText(const void*, size_t, SkScalar x, SkScalar y, const SkPaint&) override;
+    void onDrawPosText(const void*, size_t, const SkPoint[], const SkPaint&) override;
+    void onDrawPosTextH(const void*, size_t, const SkScalar[], SkScalar, const SkPaint&) override;
+
+    void onDrawTextRSXform(const void*, size_t, const SkRSXform[], const SkRect*,
+                           const SkPaint&) override;
+    void onDrawTextBlob(const SkTextBlob*, SkScalar, SkScalar, const SkPaint&) override;
+
+    void onDrawBitmap(const SkBitmap&, SkScalar, SkScalar, const SkPaint*) override;
+    void onDrawBitmapLattice(const SkBitmap&, const Lattice&, const SkRect&,
+                             const SkPaint*) override;
+    void onDrawBitmapNine(const SkBitmap&, const SkIRect&, const SkRect&, const SkPaint*) override;
+    void onDrawBitmapRect(const SkBitmap&, const SkRect*, const SkRect&, const SkPaint*,
+                          SrcRectConstraint) override;
+
+    void onDrawImage(const SkImage*, SkScalar, SkScalar, const SkPaint*) override;
+    void onDrawImageLattice(const SkImage*, const Lattice&, const SkRect&, const SkPaint*) override;
+    void onDrawImageNine(const SkImage*, const SkIRect&, const SkRect&, const SkPaint*) override;
+    void onDrawImageRect(const SkImage*, const SkRect*, const SkRect&, const SkPaint*,
+                         SrcRectConstraint) override;
+
+    void onDrawPatch(const SkPoint[12], const SkColor[4], const SkPoint[4], SkBlendMode,
+                     const SkPaint&) override;
+    void onDrawPoints(PointMode, size_t count, const SkPoint pts[], const SkPaint&) override;
+    void onDrawVerticesObject(const SkVertices*, const SkVertices::Bone bones[], int boneCount,
+                              SkBlendMode, const SkPaint&) override;
+    void onDrawAtlas(const SkImage*, const SkRSXform[], const SkRect[], const SkColor[], int,
+                     SkBlendMode, const SkRect*, const SkPaint*) override;
+    void onDrawShadowRec(const SkPath&, const SkDrawShadowRec&) override;
+
+private:
+    typedef SkCanvasVirtualEnforcer<SkNoDrawCanvas> INHERITED;
+
+    DisplayListData* fDL;
+};
+
+};  // namespace uirenderer
+};  // namespace android
\ No newline at end of file
diff --git a/libs/hwui/RenderNode.cpp b/libs/hwui/RenderNode.cpp
index a2e1f60..d5afb20 100644
--- a/libs/hwui/RenderNode.cpp
+++ b/libs/hwui/RenderNode.cpp
@@ -272,6 +272,20 @@
     mStagingDisplayList = nullptr;
     if (mDisplayList) {
         mDisplayList->syncContents();
+        if (CC_UNLIKELY(Properties::forceDarkMode)) {
+            auto usage = usageHint();
+            if (usage == UsageHint::Unknown) {
+                if (mDisplayList->mChildNodes.size() > 1) {
+                    usage = UsageHint::Background;
+                } else if (mDisplayList->mChildNodes.size() == 1 &&
+                           mDisplayList->mChildNodes.front().getRenderNode()->usageHint() !=
+                                   UsageHint::Background) {
+                    usage = UsageHint::Background;
+                }
+            }
+            mDisplayList->mDisplayList.applyColorTransform(
+                    usage == UsageHint::Background ? ColorTransform::Dark : ColorTransform::Light);
+        }
     }
 }
 
diff --git a/libs/hwui/RenderNode.h b/libs/hwui/RenderNode.h
index 8393288..83b0c22 100644
--- a/libs/hwui/RenderNode.h
+++ b/libs/hwui/RenderNode.h
@@ -209,13 +209,9 @@
 
     void output(std::ostream& output, uint32_t level);
 
-    void setUsageHint(UsageHint usageHint) {
-        mUsageHint = usageHint;
-    }
+    void setUsageHint(UsageHint usageHint) { mUsageHint = usageHint; }
 
-    UsageHint usageHint() const {
-        return mUsageHint;
-    }
+    UsageHint usageHint() const { return mUsageHint; }
 
 private:
     void computeOrderingImpl(RenderNodeOp* opState,
diff --git a/libs/hwui/RenderProperties.h b/libs/hwui/RenderProperties.h
index 7966845..0766e3b 100644
--- a/libs/hwui/RenderProperties.h
+++ b/libs/hwui/RenderProperties.h
@@ -16,6 +16,7 @@
 
 #pragma once
 
+#include "Caches.h"
 #include "DeviceInfo.h"
 #include "Outline.h"
 #include "Rect.h"
diff --git a/libs/hwui/ResourceCache.cpp b/libs/hwui/ResourceCache.cpp
index 65bee47..464a58d 100644
--- a/libs/hwui/ResourceCache.cpp
+++ b/libs/hwui/ResourceCache.cpp
@@ -15,6 +15,7 @@
  */
 
 #include "ResourceCache.h"
+#include "Caches.h"
 
 namespace android {
 
@@ -111,9 +112,13 @@
     ResourceReference* ref = index >= 0 ? mCache->valueAt(index) : nullptr;
     if (ref == nullptr) {
         // If we're not tracking this resource, just delete it
-        // A Res_png_9patch is actually an array of byte that's larger
-        // than sizeof(Res_png_9patch). It must be freed as an array.
-        delete[](int8_t*) resource;
+        if (Caches::hasInstance()) {
+            // DEAD CODE
+        } else {
+            // A Res_png_9patch is actually an array of byte that's larger
+            // than sizeof(Res_png_9patch). It must be freed as an array.
+            delete[](int8_t*) resource;
+        }
         return;
     }
     ref->destroyed = true;
@@ -130,10 +135,14 @@
     if (ref->destroyed) {
         switch (ref->resourceType) {
             case kNinePatch: {
-                // A Res_png_9patch is actually an array of byte that's larger
-                // than sizeof(Res_png_9patch). It must be freed as an array.
-                int8_t* patch = (int8_t*)resource;
-                delete[] patch;
+                if (Caches::hasInstance()) {
+                    // DEAD CODE
+                } else {
+                    // A Res_png_9patch is actually an array of byte that's larger
+                    // than sizeof(Res_png_9patch). It must be freed as an array.
+                    int8_t* patch = (int8_t*)resource;
+                    delete[] patch;
+                }
             } break;
         }
     }
diff --git a/libs/hwui/Texture.cpp b/libs/hwui/Texture.cpp
new file mode 100644
index 0000000..1e90eeb
--- /dev/null
+++ b/libs/hwui/Texture.cpp
@@ -0,0 +1,413 @@
+/*
+ * Copyright (C) 2013 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.
+ */
+
+#include "Texture.h"
+#include "Caches.h"
+#include "utils/GLUtils.h"
+#include "utils/MathUtils.h"
+#include "utils/TraceUtils.h"
+
+#include <utils/Log.h>
+
+#include <math/mat4.h>
+
+#include <SkCanvas.h>
+
+namespace android {
+namespace uirenderer {
+
+// Number of bytes used by a texture in the given format
+static int bytesPerPixel(GLint glFormat) {
+    switch (glFormat) {
+        // The wrapped-texture case, usually means a SurfaceTexture
+        case 0:
+            return 0;
+        case GL_LUMINANCE:
+        case GL_ALPHA:
+            return 1;
+        case GL_SRGB8:
+        case GL_RGB:
+            return 3;
+        case GL_SRGB8_ALPHA8:
+        case GL_RGBA:
+            return 4;
+        case GL_RGBA16F:
+            return 8;
+        default:
+            LOG_ALWAYS_FATAL("UNKNOWN FORMAT 0x%x", glFormat);
+    }
+}
+
+void Texture::setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture, bool force) {
+    if (force || wrapS != mWrapS || wrapT != mWrapT) {
+        mWrapS = wrapS;
+        mWrapT = wrapT;
+
+        if (bindTexture) {
+            mCaches.textureState().bindTexture(mTarget, mId);
+        }
+
+        glTexParameteri(mTarget, GL_TEXTURE_WRAP_S, wrapS);
+        glTexParameteri(mTarget, GL_TEXTURE_WRAP_T, wrapT);
+    }
+}
+
+void Texture::setFilterMinMag(GLenum min, GLenum mag, bool bindTexture, bool force) {
+    if (force || min != mMinFilter || mag != mMagFilter) {
+        mMinFilter = min;
+        mMagFilter = mag;
+
+        if (bindTexture) {
+            mCaches.textureState().bindTexture(mTarget, mId);
+        }
+
+        if (mipMap && min == GL_LINEAR) min = GL_LINEAR_MIPMAP_LINEAR;
+
+        glTexParameteri(mTarget, GL_TEXTURE_MIN_FILTER, min);
+        glTexParameteri(mTarget, GL_TEXTURE_MAG_FILTER, mag);
+    }
+}
+
+void Texture::deleteTexture() {
+    mCaches.textureState().deleteTexture(mId);
+    mId = 0;
+    mTarget = GL_NONE;
+    if (mEglImageHandle != EGL_NO_IMAGE_KHR) {
+        EGLDisplay eglDisplayHandle = eglGetCurrentDisplay();
+        eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle);
+        mEglImageHandle = EGL_NO_IMAGE_KHR;
+    }
+}
+
+bool Texture::updateLayout(uint32_t width, uint32_t height, GLint internalFormat, GLint format,
+                           GLenum target) {
+    if (mWidth == width && mHeight == height && mFormat == format &&
+        mInternalFormat == internalFormat && mTarget == target) {
+        return false;
+    }
+    mWidth = width;
+    mHeight = height;
+    mFormat = format;
+    mInternalFormat = internalFormat;
+    mTarget = target;
+    notifySizeChanged(mWidth * mHeight * bytesPerPixel(internalFormat));
+    return true;
+}
+
+void Texture::resetCachedParams() {
+    mWrapS = GL_REPEAT;
+    mWrapT = GL_REPEAT;
+    mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
+    mMagFilter = GL_LINEAR;
+}
+
+void Texture::upload(GLint internalFormat, uint32_t width, uint32_t height, GLenum format,
+                     GLenum type, const void* pixels) {
+    GL_CHECKPOINT(MODERATE);
+
+    // We don't have color space information, we assume the data is gamma encoded
+    mIsLinear = false;
+
+    bool needsAlloc = updateLayout(width, height, internalFormat, format, GL_TEXTURE_2D);
+    if (!mId) {
+        glGenTextures(1, &mId);
+        needsAlloc = true;
+        resetCachedParams();
+    }
+    mCaches.textureState().bindTexture(GL_TEXTURE_2D, mId);
+    if (needsAlloc) {
+        glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, format, type, pixels);
+    } else if (pixels) {
+        glTexSubImage2D(GL_TEXTURE_2D, 0, internalFormat, mWidth, mHeight, 0, format, type, pixels);
+    }
+    GL_CHECKPOINT(MODERATE);
+}
+
+void Texture::uploadHardwareBitmapToTexture(GraphicBuffer* buffer) {
+    EGLDisplay eglDisplayHandle = eglGetCurrentDisplay();
+    if (mEglImageHandle != EGL_NO_IMAGE_KHR) {
+        eglDestroyImageKHR(eglDisplayHandle, mEglImageHandle);
+        mEglImageHandle = EGL_NO_IMAGE_KHR;
+    }
+    mEglImageHandle = eglCreateImageKHR(eglDisplayHandle, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
+                                        buffer->getNativeBuffer(), 0);
+    glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, mEglImageHandle);
+}
+
+static void uploadToTexture(bool resize, GLint internalFormat, GLenum format, GLenum type,
+                            GLsizei stride, GLsizei bpp, GLsizei width, GLsizei height,
+                            const GLvoid* data) {
+    const bool useStride =
+            stride != width && Caches::getInstance().extensions().hasUnpackRowLength();
+    if ((stride == width) || useStride) {
+        if (useStride) {
+            glPixelStorei(GL_UNPACK_ROW_LENGTH, stride);
+        }
+
+        if (resize) {
+            glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data);
+        } else {
+            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, data);
+        }
+
+        if (useStride) {
+            glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
+        }
+    } else {
+        //  With OpenGL ES 2.0 we need to copy the bitmap in a temporary buffer
+        //  if the stride doesn't match the width
+
+        GLvoid* temp = (GLvoid*)malloc(width * height * bpp);
+        if (!temp) return;
+
+        uint8_t* pDst = (uint8_t*)temp;
+        uint8_t* pSrc = (uint8_t*)data;
+        for (GLsizei i = 0; i < height; i++) {
+            memcpy(pDst, pSrc, width * bpp);
+            pDst += width * bpp;
+            pSrc += stride * bpp;
+        }
+
+        if (resize) {
+            glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, temp);
+        } else {
+            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, type, temp);
+        }
+
+        free(temp);
+    }
+}
+
+void Texture::colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType, bool needSRGB,
+                                         GLint* outInternalFormat, GLint* outFormat,
+                                         GLint* outType) {
+    switch (colorType) {
+        case kAlpha_8_SkColorType:
+            *outFormat = GL_ALPHA;
+            *outInternalFormat = GL_ALPHA;
+            *outType = GL_UNSIGNED_BYTE;
+            break;
+        case kRGB_565_SkColorType:
+            if (needSRGB) {
+                // We would ideally use a GL_RGB/GL_SRGB8 texture but the
+                // intermediate Skia bitmap needs to be ARGB_8888
+                *outFormat = GL_RGBA;
+                *outInternalFormat = caches.rgbaInternalFormat();
+                *outType = GL_UNSIGNED_BYTE;
+            } else {
+                *outFormat = GL_RGB;
+                *outInternalFormat = GL_RGB;
+                *outType = GL_UNSIGNED_SHORT_5_6_5;
+            }
+            break;
+        // ARGB_4444 is upconverted to RGBA_8888
+        case kARGB_4444_SkColorType:
+        case kN32_SkColorType:
+            *outFormat = GL_RGBA;
+            *outInternalFormat = caches.rgbaInternalFormat(needSRGB);
+            *outType = GL_UNSIGNED_BYTE;
+            break;
+        case kGray_8_SkColorType:
+            *outFormat = GL_LUMINANCE;
+            *outInternalFormat = GL_LUMINANCE;
+            *outType = GL_UNSIGNED_BYTE;
+            break;
+        case kRGBA_F16_SkColorType:
+            if (caches.extensions().getMajorGlVersion() >= 3) {
+                // This format is always linear
+                *outFormat = GL_RGBA;
+                *outInternalFormat = GL_RGBA16F;
+                *outType = GL_HALF_FLOAT;
+            } else {
+                *outFormat = GL_RGBA;
+                *outInternalFormat = caches.rgbaInternalFormat(true);
+                *outType = GL_UNSIGNED_BYTE;
+            }
+            break;
+        default:
+            LOG_ALWAYS_FATAL("Unsupported bitmap colorType: %d", colorType);
+            break;
+    }
+}
+
+SkBitmap Texture::uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending,
+                              sk_sp<SkColorSpace> sRGB) {
+    SkBitmap rgbaBitmap;
+    rgbaBitmap.allocPixels(SkImageInfo::MakeN32(bitmap.width(), bitmap.height(),
+                                                bitmap.info().alphaType(),
+                                                hasLinearBlending ? sRGB : nullptr));
+    rgbaBitmap.eraseColor(0);
+
+    if (bitmap.colorType() == kRGBA_F16_SkColorType) {
+        // Drawing RGBA_F16 onto ARGB_8888 is not supported
+        bitmap.readPixels(rgbaBitmap.info().makeColorSpace(SkColorSpace::MakeSRGB()),
+                          rgbaBitmap.getPixels(), rgbaBitmap.rowBytes(), 0, 0);
+    } else {
+        SkCanvas canvas(rgbaBitmap);
+        canvas.drawBitmap(bitmap, 0.0f, 0.0f, nullptr);
+    }
+
+    return rgbaBitmap;
+}
+
+bool Texture::hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending) {
+    return info.colorType() == kARGB_4444_SkColorType ||
+           (info.colorType() == kRGB_565_SkColorType && hasLinearBlending &&
+            info.colorSpace()->isSRGB()) ||
+           (info.colorType() == kRGBA_F16_SkColorType &&
+            Caches::getInstance().extensions().getMajorGlVersion() < 3);
+}
+
+void Texture::upload(Bitmap& bitmap) {
+    ATRACE_FORMAT("Upload %ux%u Texture", bitmap.width(), bitmap.height());
+
+    // We could also enable mipmapping if both bitmap dimensions are powers
+    // of 2 but we'd have to deal with size changes. Let's keep this simple
+    const bool canMipMap = mCaches.extensions().hasNPot();
+
+    // If the texture had mipmap enabled but not anymore,
+    // force a glTexImage2D to discard the mipmap levels
+    bool needsAlloc = canMipMap && mipMap && !bitmap.hasHardwareMipMap();
+    bool setDefaultParams = false;
+
+    if (!mId) {
+        glGenTextures(1, &mId);
+        needsAlloc = true;
+        setDefaultParams = true;
+    }
+
+    bool hasLinearBlending = mCaches.extensions().hasLinearBlending();
+    bool needSRGB = transferFunctionCloseToSRGB(bitmap.info().colorSpace());
+
+    GLint internalFormat, format, type;
+    colorTypeToGlFormatAndType(mCaches, bitmap.colorType(), needSRGB && hasLinearBlending,
+                               &internalFormat, &format, &type);
+
+    // Some devices don't support GL_RGBA16F, so we need to compare the color type
+    // and internal GL format to decide what to do with 16 bit bitmaps
+    bool rgba16fNeedsConversion =
+            bitmap.colorType() == kRGBA_F16_SkColorType && internalFormat != GL_RGBA16F;
+
+    // RGBA16F is always linear extended sRGB
+    if (internalFormat == GL_RGBA16F) {
+        mIsLinear = true;
+    }
+
+    mConnector.reset();
+
+    // Alpha masks don't have color profiles
+    // If an RGBA16F bitmap needs conversion, we know the target will be sRGB
+    if (!mIsLinear && internalFormat != GL_ALPHA && !rgba16fNeedsConversion) {
+        SkColorSpace* colorSpace = bitmap.info().colorSpace();
+        // If the bitmap is sRGB we don't need conversion
+        if (colorSpace != nullptr && !colorSpace->isSRGB()) {
+            SkMatrix44 xyzMatrix(SkMatrix44::kUninitialized_Constructor);
+            if (!colorSpace->toXYZD50(&xyzMatrix)) {
+                ALOGW("Incompatible color space!");
+            } else {
+                SkColorSpaceTransferFn fn;
+                if (!colorSpace->isNumericalTransferFn(&fn)) {
+                    ALOGW("Incompatible color space, no numerical transfer function!");
+                } else {
+                    float data[16];
+                    xyzMatrix.asColMajorf(data);
+
+                    ColorSpace::TransferParameters p = {fn.fG, fn.fA, fn.fB, fn.fC,
+                                                        fn.fD, fn.fE, fn.fF};
+                    ColorSpace src("Unnamed", mat4f((const float*)&data[0]).upperLeft(), p);
+                    mConnector.reset(new ColorSpaceConnector(src, ColorSpace::sRGB()));
+
+                    // A non-sRGB color space might have a transfer function close enough to sRGB
+                    // that we can save shader instructions by using an sRGB sampler
+                    // This is only possible if we have hardware support for sRGB textures
+                    if (needSRGB && internalFormat == GL_RGBA && mCaches.extensions().hasSRGB() &&
+                        !bitmap.isHardware()) {
+                        internalFormat = GL_SRGB8_ALPHA8;
+                    }
+                }
+            }
+        }
+    }
+
+    GLenum target = bitmap.isHardware() ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
+    needsAlloc |= updateLayout(bitmap.width(), bitmap.height(), internalFormat, format, target);
+
+    blend = !bitmap.isOpaque();
+    mCaches.textureState().bindTexture(mTarget, mId);
+
+    // TODO: Handle sRGB gray bitmaps
+    if (CC_UNLIKELY(hasUnsupportedColorType(bitmap.info(), hasLinearBlending))) {
+        SkBitmap skBitmap;
+        bitmap.getSkBitmap(&skBitmap);
+        sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB();
+        SkBitmap rgbaBitmap = uploadToN32(skBitmap, hasLinearBlending, std::move(sRGB));
+        uploadToTexture(needsAlloc, internalFormat, format, type, rgbaBitmap.rowBytesAsPixels(),
+                        rgbaBitmap.bytesPerPixel(), rgbaBitmap.width(), rgbaBitmap.height(),
+                        rgbaBitmap.getPixels());
+    } else if (bitmap.isHardware()) {
+        uploadHardwareBitmapToTexture(bitmap.graphicBuffer());
+    } else {
+        uploadToTexture(needsAlloc, internalFormat, format, type, bitmap.rowBytesAsPixels(),
+                        bitmap.info().bytesPerPixel(), bitmap.width(), bitmap.height(),
+                        bitmap.pixels());
+    }
+
+    if (canMipMap) {
+        mipMap = bitmap.hasHardwareMipMap();
+        if (mipMap) {
+            glGenerateMipmap(GL_TEXTURE_2D);
+        }
+    }
+
+    if (setDefaultParams) {
+        setFilter(GL_NEAREST);
+        setWrap(GL_CLAMP_TO_EDGE);
+    }
+}
+
+void Texture::wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, GLint format,
+                   GLenum target) {
+    mId = id;
+    mWidth = width;
+    mHeight = height;
+    mFormat = format;
+    mInternalFormat = internalFormat;
+    mTarget = target;
+    mConnector.reset();
+    // We're wrapping an existing texture, so don't double count this memory
+    notifySizeChanged(0);
+}
+
+TransferFunctionType Texture::getTransferFunctionType() const {
+    if (mConnector.get() != nullptr && mInternalFormat != GL_SRGB8_ALPHA8) {
+        const ColorSpace::TransferParameters& p = mConnector->getSource().getTransferParameters();
+        if (MathUtils::isZero(p.e) && MathUtils::isZero(p.f)) {
+            if (MathUtils::areEqual(p.a, 1.0f) && MathUtils::isZero(p.b) &&
+                MathUtils::isZero(p.c) && MathUtils::isZero(p.d)) {
+                if (MathUtils::areEqual(p.g, 1.0f)) {
+                    return TransferFunctionType::None;
+                }
+                return TransferFunctionType::Gamma;
+            }
+            return TransferFunctionType::Limited;
+        }
+        return TransferFunctionType::Full;
+    }
+    return TransferFunctionType::None;
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/Texture.h b/libs/hwui/Texture.h
new file mode 100644
index 0000000..5b7e4e2
--- /dev/null
+++ b/libs/hwui/Texture.h
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#ifndef ANDROID_HWUI_TEXTURE_H
+#define ANDROID_HWUI_TEXTURE_H
+
+#include "GpuMemoryTracker.h"
+#include "hwui/Bitmap.h"
+#include "utils/Color.h"
+
+#include <memory>
+
+#include <math/mat3.h>
+
+#include <ui/ColorSpace.h>
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#include <GLES2/gl2.h>
+#include <GLES3/gl3.h>
+#include <SkBitmap.h>
+
+namespace android {
+
+class GraphicBuffer;
+
+namespace uirenderer {
+
+class Caches;
+class UvMapper;
+class Layer;
+
+/**
+ * Represents an OpenGL texture.
+ */
+class Texture : public GpuMemoryTracker {
+public:
+    static SkBitmap uploadToN32(const SkBitmap& bitmap, bool hasLinearBlending,
+                                sk_sp<SkColorSpace> sRGB);
+    static bool hasUnsupportedColorType(const SkImageInfo& info, bool hasLinearBlending);
+    static void colorTypeToGlFormatAndType(const Caches& caches, SkColorType colorType,
+                                           bool needSRGB, GLint* outInternalFormat,
+                                           GLint* outFormat, GLint* outType);
+
+    explicit Texture(Caches& caches) : GpuMemoryTracker(GpuObjectType::Texture), mCaches(caches) {}
+
+    virtual ~Texture() {}
+
+    inline void setWrap(GLenum wrap, bool bindTexture = false, bool force = false) {
+        setWrapST(wrap, wrap, bindTexture, force);
+    }
+
+    virtual void setWrapST(GLenum wrapS, GLenum wrapT, bool bindTexture = false,
+                           bool force = false);
+
+    inline void setFilter(GLenum filter, bool bindTexture = false, bool force = false) {
+        setFilterMinMag(filter, filter, bindTexture, force);
+    }
+
+    virtual void setFilterMinMag(GLenum min, GLenum mag, bool bindTexture = false,
+                                 bool force = false);
+
+    /**
+     * Convenience method to call glDeleteTextures() on this texture's id.
+     */
+    void deleteTexture();
+
+    /**
+     * Sets the width, height, and format of the texture along with allocating
+     * the texture ID. Does nothing if the width, height, and format are already
+     * the requested values.
+     *
+     * The image data is undefined after calling this.
+     */
+    void resize(uint32_t width, uint32_t height, GLint internalFormat, GLint format) {
+        upload(internalFormat, width, height, format,
+               internalFormat == GL_RGBA16F ? GL_HALF_FLOAT : GL_UNSIGNED_BYTE, nullptr);
+    }
+
+    /**
+     * Updates this Texture with the contents of the provided Bitmap,
+     * also setting the appropriate width, height, and format. It is not necessary
+     * to call resize() prior to this.
+     *
+     * Note this does not set the generation from the Bitmap.
+     */
+    void upload(Bitmap& source);
+
+    /**
+     * Basically glTexImage2D/glTexSubImage2D.
+     */
+    void upload(GLint internalFormat, uint32_t width, uint32_t height, GLenum format, GLenum type,
+                const void* pixels);
+
+    /**
+     * Wraps an existing texture.
+     */
+    void wrap(GLuint id, uint32_t width, uint32_t height, GLint internalFormat, GLint format,
+              GLenum target);
+
+    GLuint id() const { return mId; }
+
+    uint32_t width() const { return mWidth; }
+
+    uint32_t height() const { return mHeight; }
+
+    GLint format() const { return mFormat; }
+
+    GLint internalFormat() const { return mInternalFormat; }
+
+    GLenum target() const { return mTarget; }
+
+    /**
+     * Returns nullptr if this texture does not require color space conversion
+     * to sRGB, or a valid pointer to a ColorSpaceConnector if a conversion
+     * is required.
+     */
+    constexpr const ColorSpaceConnector* getColorSpaceConnector() const { return mConnector.get(); }
+
+    constexpr bool hasColorSpaceConversion() const { return mConnector.get() != nullptr; }
+
+    TransferFunctionType getTransferFunctionType() const;
+
+    /**
+     * Returns true if this texture uses a linear encoding format.
+     */
+    constexpr bool isLinear() const { return mIsLinear; }
+
+    /**
+     * Generation of the backing bitmap,
+     */
+    uint32_t generation = 0;
+    /**
+     * Indicates whether the texture requires blending.
+     */
+    bool blend = false;
+    /**
+     * Indicates whether this texture should be cleaned up after use.
+     */
+    bool cleanup = false;
+    /**
+     * Optional, size of the original bitmap.
+     */
+    uint32_t bitmapSize = 0;
+    /**
+     * Indicates whether this texture will use trilinear filtering.
+     */
+    bool mipMap = false;
+
+    /**
+     * Optional, pointer to a texture coordinates mapper.
+     */
+    const UvMapper* uvMapper = nullptr;
+
+    /**
+     * Whether or not the Texture is marked in use and thus not evictable for
+     * the current frame. This is reset at the start of a new frame.
+     */
+    void* isInUse = nullptr;
+
+private:
+    // TODO: Temporarily grant private access to GlLayer, remove once
+    // GlLayer can be de-tangled from being a dual-purpose render target
+    // and external texture wrapper
+    friend class GlLayer;
+
+    // Returns true if the texture layout (size, format, etc.) changed, false if it was the same
+    bool updateLayout(uint32_t width, uint32_t height, GLint internalFormat, GLint format,
+                      GLenum target);
+    void uploadHardwareBitmapToTexture(GraphicBuffer* buffer);
+    void resetCachedParams();
+
+    GLuint mId = 0;
+    uint32_t mWidth = 0;
+    uint32_t mHeight = 0;
+    GLint mFormat = 0;
+    GLint mInternalFormat = 0;
+    GLenum mTarget = GL_NONE;
+    EGLImageKHR mEglImageHandle = EGL_NO_IMAGE_KHR;
+
+    /* See GLES spec section 3.8.14
+     * "In the initial state, the value assigned to TEXTURE_MIN_FILTER is
+     * NEAREST_MIPMAP_LINEAR and the value for TEXTURE_MAG_FILTER is LINEAR.
+     * s, t, and r wrap modes are all set to REPEAT."
+     */
+    GLenum mWrapS = GL_REPEAT;
+    GLenum mWrapT = GL_REPEAT;
+    GLenum mMinFilter = GL_NEAREST_MIPMAP_LINEAR;
+    GLenum mMagFilter = GL_LINEAR;
+
+    // Indicates whether the content of the texture is in linear space
+    bool mIsLinear = false;
+
+    Caches& mCaches;
+
+    std::unique_ptr<ColorSpaceConnector> mConnector;
+};  // struct Texture
+
+class AutoTexture {
+public:
+    explicit AutoTexture(Texture* texture) : texture(texture) {}
+    ~AutoTexture() {
+        if (texture && texture->cleanup) {
+            texture->deleteTexture();
+            delete texture;
+        }
+    }
+
+    Texture* const texture;
+};  // class AutoTexture
+
+};  // namespace uirenderer
+};  // namespace android
+
+#endif  // ANDROID_HWUI_TEXTURE_H
diff --git a/libs/hwui/VkLayer.cpp b/libs/hwui/VkLayer.cpp
new file mode 100644
index 0000000..30fba7a
--- /dev/null
+++ b/libs/hwui/VkLayer.cpp
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#include "VkLayer.h"
+
+#include "renderstate/RenderState.h"
+
+#include <SkCanvas.h>
+#include <SkSurface.h>
+
+namespace android {
+namespace uirenderer {
+
+void VkLayer::updateTexture() {
+    sk_sp<SkSurface> surface;
+    SkImageInfo info = SkImageInfo::MakeS32(mWidth, mHeight, kPremul_SkAlphaType);
+    surface = SkSurface::MakeRenderTarget(mRenderState.getGrContext(), SkBudgeted::kNo, info);
+    surface->getCanvas()->clear(SK_ColorBLUE);
+    mImage = surface->makeImageSnapshot();
+}
+
+void VkLayer::onVkContextDestroyed() {
+    mImage = nullptr;
+}
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/VkLayer.h b/libs/hwui/VkLayer.h
new file mode 100644
index 0000000..e9664d0
--- /dev/null
+++ b/libs/hwui/VkLayer.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2017 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.
+ */
+
+#pragma once
+
+#include "Layer.h"
+
+#include <SkImage.h>
+
+namespace android {
+namespace uirenderer {
+/**
+ * A layer has dimensions and is backed by a VkImage.
+ */
+class VkLayer : public Layer {
+public:
+    VkLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
+            sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode, bool blend)
+            : Layer(renderState, Api::Vulkan, colorFilter, alpha, mode)
+            , mWidth(layerWidth)
+            , mHeight(layerHeight)
+            , mBlend(blend) {}
+
+    virtual ~VkLayer() {}
+
+    uint32_t getWidth() const override { return mWidth; }
+
+    uint32_t getHeight() const override { return mHeight; }
+
+    void setSize(uint32_t width, uint32_t height) override {
+        mWidth = width;
+        mHeight = height;
+    }
+
+    void setBlend(bool blend) override { mBlend = blend; }
+
+    bool isBlend() const override { return mBlend; }
+
+    sk_sp<SkImage> getImage() { return mImage; }
+
+    void updateTexture();
+
+    // If we've destroyed the vulkan context (VkInstance, VkDevice, etc.), we must make sure to
+    // destroy any VkImages that were made with that context.
+    void onVkContextDestroyed();
+
+private:
+    int mWidth;
+    int mHeight;
+    bool mBlend;
+
+    sk_sp<SkImage> mImage;
+
+};  // struct VkLayer
+
+};  // namespace uirenderer
+};  // namespace android
diff --git a/libs/hwui/hwui/Bitmap.cpp b/libs/hwui/hwui/Bitmap.cpp
index e7d12de..a7d37f8 100644
--- a/libs/hwui/hwui/Bitmap.cpp
+++ b/libs/hwui/hwui/Bitmap.cpp
@@ -15,11 +15,11 @@
  */
 #include "Bitmap.h"
 
+#include "Caches.h"
 #include "HardwareBitmapUploader.h"
 #include "Properties.h"
 #include "renderthread/RenderProxy.h"
 #include "utils/Color.h"
-#include <utils/Trace.h>
 
 #include <sys/mman.h>
 
@@ -34,8 +34,8 @@
 #include <SkImagePriv.h>
 #include <SkToSRGBColorFilter.h>
 
-#include <limits>
 #include <SkHighContrastFilter.h>
+#include <limits>
 
 namespace android {
 
@@ -333,17 +333,17 @@
 
     // TODO: Move this to the canvas (or other?) layer where we have the target lightness
     // mode and can selectively do the right thing.
-    if (palette() != BitmapPalette::Unknown && uirenderer::Properties::forceDarkMode) {
-        SkHighContrastConfig config;
-        config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
-        *outputColorFilter = SkHighContrastFilter::Make(config)->makeComposed(*outputColorFilter);
-    }
+    //    if (palette() != BitmapPalette::Unknown && uirenderer::Properties::forceDarkMode) {
+    //        SkHighContrastConfig config;
+    //        config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
+    //        *outputColorFilter =
+    //        SkHighContrastFilter::Make(config)->makeComposed(*outputColorFilter);
+    //    }
     return image;
 }
 
 class MinMaxAverage {
 public:
-
     void add(float sample) {
         if (mCount == 0) {
             mMin = sample;
@@ -356,21 +356,13 @@
         mCount++;
     }
 
-    float average() {
-        return mTotal / mCount;
-    }
+    float average() { return mTotal / mCount; }
 
-    float min() {
-        return mMin;
-    }
+    float min() { return mMin; }
 
-    float max() {
-        return mMax;
-    }
+    float max() { return mMax; }
 
-    float delta() {
-        return mMax - mMin;
-    }
+    float delta() { return mMax - mMin; }
 
 private:
     float mMin = 0.0f;
@@ -413,14 +405,15 @@
     // TODO: Tune the coverage threshold
     if (sampledCount < 5) {
         ALOGV("Not enough samples, only found %d for image sized %dx%d, format = %d, alpha = %d",
-              sampledCount, info.width(), info.height(), (int) info.colorType(), (int) info.alphaType());
+              sampledCount, info.width(), info.height(), (int)info.colorType(),
+              (int)info.alphaType());
         return BitmapPalette::Unknown;
     }
 
-    ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = %f]",
-          sampledCount,
-          hue.min(), hue.max(), hue.average(),
-          saturation.min(), saturation.max(), saturation.average());
+    ALOGV("samples = %d, hue [min = %f, max = %f, avg = %f]; saturation [min = %f, max = %f, avg = "
+          "%f]",
+          sampledCount, hue.min(), hue.max(), hue.average(), saturation.min(), saturation.max(),
+          saturation.average());
 
     if (hue.delta() <= 20 && saturation.delta() <= .1f) {
         if (value.average() >= .5f) {
diff --git a/libs/hwui/hwui/Bitmap.h b/libs/hwui/hwui/Bitmap.h
index d268042..170335d 100644
--- a/libs/hwui/hwui/Bitmap.h
+++ b/libs/hwui/hwui/Bitmap.h
@@ -69,7 +69,8 @@
     Bitmap(void* address, void* context, FreeFunc freeFunc, const SkImageInfo& info,
            size_t rowBytes);
     Bitmap(void* address, int fd, size_t mappedSize, const SkImageInfo& info, size_t rowBytes);
-    Bitmap(GraphicBuffer* buffer, const SkImageInfo& info, BitmapPalette palette = BitmapPalette::Unknown);
+    Bitmap(GraphicBuffer* buffer, const SkImageInfo& info,
+           BitmapPalette palette = BitmapPalette::Unknown);
 
     int rowBytesAsPixels() const { return rowBytes() >> mInfo.shiftPerPixel(); }
 
diff --git a/libs/hwui/pipeline/skia/LayerDrawable.cpp b/libs/hwui/pipeline/skia/LayerDrawable.cpp
index fb66b50..c41f6a6 100644
--- a/libs/hwui/pipeline/skia/LayerDrawable.cpp
+++ b/libs/hwui/pipeline/skia/LayerDrawable.cpp
@@ -15,6 +15,8 @@
  */
 
 #include "LayerDrawable.h"
+#include "GlLayer.h"
+#include "VkLayer.h"
 
 #include "GrBackendSurface.h"
 #include "SkColorFilter.h"
@@ -39,14 +41,35 @@
         return false;
     }
     // transform the matrix based on the layer
-    SkMatrix layerTransform = layer->getTransform();
-    sk_sp<SkImage> layerImage = layer->getImage();
+    SkMatrix layerTransform;
+    layer->getTransform().copyTo(layerTransform);
+    sk_sp<SkImage> layerImage;
     const int layerWidth = layer->getWidth();
     const int layerHeight = layer->getHeight();
+    if (layer->getApi() == Layer::Api::OpenGL) {
+        GlLayer* glLayer = static_cast<GlLayer*>(layer);
+        GrGLTextureInfo externalTexture;
+        externalTexture.fTarget = glLayer->getRenderTarget();
+        externalTexture.fID = glLayer->getTextureId();
+        // The format may not be GL_RGBA8, but given the DeferredLayerUpdater and GLConsumer don't
+        // expose that info we use it as our default.  Further, given that we only use this texture
+        // as a source this will not impact how Skia uses the texture.  The only potential affect
+        // this is anticipated to have is that for some format types if we are not bound as an OES
+        // texture we may get invalid results for SKP capture if we read back the texture.
+        externalTexture.fFormat = GL_RGBA8;
+        GrBackendTexture backendTexture(layerWidth, layerHeight, GrMipMapped::kNo, externalTexture);
+        layerImage = SkImage::MakeFromTexture(context, backendTexture, kTopLeft_GrSurfaceOrigin,
+                                              kRGBA_8888_SkColorType, kPremul_SkAlphaType, nullptr);
+    } else {
+        SkASSERT(layer->getApi() == Layer::Api::Vulkan);
+        VkLayer* vkLayer = static_cast<VkLayer*>(layer);
+        canvas->clear(SK_ColorGREEN);
+        layerImage = vkLayer->getImage();
+    }
 
     if (layerImage) {
         SkMatrix textureMatrixInv;
-        textureMatrixInv = layer->getTexTransform();
+        layer->getTexTransform().copyTo(textureMatrixInv);
         // TODO: after skia bug https://bugs.chromium.org/p/skia/issues/detail?id=7075 is fixed
         // use bottom left origin and remove flipV and invert transformations.
         SkMatrix flipV;
@@ -72,9 +95,6 @@
         paint.setAlpha(layer->getAlpha());
         paint.setBlendMode(layer->getMode());
         paint.setColorFilter(layer->getColorSpaceWithFilter());
-        if (layer->getForceFilter()) {
-            paint.setFilterQuality(kLow_SkFilterQuality);
-        }
 
         const bool nonIdentityMatrix = !matrix.isIdentity();
         if (nonIdentityMatrix) {
diff --git a/libs/hwui/pipeline/skia/SkiaDisplayList.h b/libs/hwui/pipeline/skia/SkiaDisplayList.h
index 6eff589..4f30f98 100644
--- a/libs/hwui/pipeline/skia/SkiaDisplayList.h
+++ b/libs/hwui/pipeline/skia/SkiaDisplayList.h
@@ -18,12 +18,11 @@
 
 #include "hwui/AnimatedImageDrawable.h"
 #include "GLFunctorDrawable.h"
+#include "RecordingCanvas.h"
 #include "RenderNodeDrawable.h"
 #include "TreeInfo.h"
 #include "utils/LinearAllocator.h"
 
-#include <SkLiteDL.h>
-#include <SkLiteRecorder.h>
 #include <deque>
 
 namespace android {
@@ -140,7 +139,7 @@
      */
     inline bool containsProjectionReceiver() const { return mProjectionReceiver; }
 
-    void attachRecorder(SkLiteRecorder* recorder, const SkIRect& bounds) {
+    void attachRecorder(RecordingCanvas* recorder, const SkIRect& bounds) {
         recorder->reset(&mDisplayList, bounds);
     }
 
@@ -160,7 +159,7 @@
     std::vector<SkImage*> mMutableImages;
     std::vector<VectorDrawableRoot*> mVectorDrawables;
     std::vector<AnimatedImageDrawable*> mAnimatedImages;
-    SkLiteDL mDisplayList;
+    DisplayListData mDisplayList;
 
     // mProjectionReceiver points to a child node (stored in mChildNodes) that is as a projection
     // receiver. It is set at record time and used at both prepare and draw tree traversals to
diff --git a/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp b/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
index 2ae3723..78f5a71 100644
--- a/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
+++ b/libs/hwui/pipeline/skia/SkiaOpenGLPipeline.cpp
@@ -17,6 +17,7 @@
 #include "SkiaOpenGLPipeline.h"
 
 #include "DeferredLayerUpdater.h"
+#include "GlLayer.h"
 #include "LayerDrawable.h"
 #include "SkiaPipeline.h"
 #include "SkiaProfileRenderer.h"
@@ -186,9 +187,18 @@
     return false;
 }
 
+static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
+                          sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode,
+                          bool blend) {
+    GlLayer* layer =
+            new GlLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend);
+    layer->generateTexture();
+    return layer;
+}
+
 DeferredLayerUpdater* SkiaOpenGLPipeline::createTextureLayer() {
     mRenderThread.requireGlContext();
-    return new DeferredLayerUpdater(mRenderThread.renderState());
+    return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::OpenGL);
 }
 
 void SkiaOpenGLPipeline::onStop() {
diff --git a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
index b9748af..83d7e6a 100644
--- a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
+++ b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.cpp
@@ -45,21 +45,13 @@
     }
 
     mDisplayList->attachRecorder(&mRecorder, SkIRect::MakeWH(width, height));
-    SkCanvas* canvas = &mRecorder;
-    if (renderNode) {
-        mWrappedCanvas = makeTransformCanvas(&mRecorder, renderNode->usageHint());
-        if (mWrappedCanvas) {
-            canvas = mWrappedCanvas.get();
-        }
-    }
-    SkiaCanvas::reset(canvas);
+    SkiaCanvas::reset(&mRecorder);
 }
 
 uirenderer::DisplayList* SkiaRecordingCanvas::finishRecording() {
     // close any existing chunks if necessary
     insertReorderBarrier(false);
     mRecorder.restoreToCount(1);
-    mWrappedCanvas = nullptr;
     return mDisplayList.release();
 }
 
diff --git a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.h b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.h
index 50d84d6..988728d 100644
--- a/libs/hwui/pipeline/skia/SkiaRecordingCanvas.h
+++ b/libs/hwui/pipeline/skia/SkiaRecordingCanvas.h
@@ -15,7 +15,7 @@
  */
 #pragma once
 
-#include <SkLiteRecorder.h>
+#include "RecordingCanvas.h"
 #include "ReorderBarrierDrawables.h"
 #include "SkiaCanvas.h"
 #include "SkiaDisplayList.h"
@@ -76,9 +76,8 @@
                                     uirenderer::GlFunctorLifecycleListener* listener) override;
 
 private:
-    SkLiteRecorder mRecorder;
+    RecordingCanvas mRecorder;
     std::unique_ptr<SkiaDisplayList> mDisplayList;
-    std::unique_ptr<SkCanvas> mWrappedCanvas;
     StartReorderBarrierDrawable* mCurrentBarrier;
 
     /**
diff --git a/libs/hwui/pipeline/skia/SkiaVulkanPipeline.cpp b/libs/hwui/pipeline/skia/SkiaVulkanPipeline.cpp
index 5f2eee4..b2519fe 100644
--- a/libs/hwui/pipeline/skia/SkiaVulkanPipeline.cpp
+++ b/libs/hwui/pipeline/skia/SkiaVulkanPipeline.cpp
@@ -20,6 +20,7 @@
 #include "Readback.h"
 #include "SkiaPipeline.h"
 #include "SkiaProfileRenderer.h"
+#include "VkLayer.h"
 #include "renderstate/RenderState.h"
 #include "renderthread/Frame.h"
 
@@ -113,10 +114,16 @@
     return false;
 }
 
+static Layer* createLayer(RenderState& renderState, uint32_t layerWidth, uint32_t layerHeight,
+                          sk_sp<SkColorFilter> colorFilter, int alpha, SkBlendMode mode,
+                          bool blend) {
+    return new VkLayer(renderState, layerWidth, layerHeight, colorFilter, alpha, mode, blend);
+}
+
 DeferredLayerUpdater* SkiaVulkanPipeline::createTextureLayer() {
     mVkManager.initialize();
 
-    return new DeferredLayerUpdater(mRenderThread.renderState());
+    return new DeferredLayerUpdater(mRenderThread.renderState(), createLayer, Layer::Api::Vulkan);
 }
 
 void SkiaVulkanPipeline::onStop() {}
diff --git a/libs/hwui/renderstate/PixelBufferState.cpp b/libs/hwui/renderstate/PixelBufferState.cpp
new file mode 100644
index 0000000..3a6efb8
--- /dev/null
+++ b/libs/hwui/renderstate/PixelBufferState.cpp
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+#include "renderstate/PixelBufferState.h"
+
+namespace android {
+namespace uirenderer {
+
+PixelBufferState::PixelBufferState() : mCurrentPixelBuffer(0) {}
+
+bool PixelBufferState::bind(GLuint buffer) {
+    if (mCurrentPixelBuffer != buffer) {
+        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
+        mCurrentPixelBuffer = buffer;
+        return true;
+    }
+    return false;
+}
+
+bool PixelBufferState::unbind() {
+    if (mCurrentPixelBuffer) {
+        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+        mCurrentPixelBuffer = 0;
+        return true;
+    }
+    return false;
+}
+
+} /* namespace uirenderer */
+} /* namespace android */
diff --git a/libs/hwui/renderstate/PixelBufferState.h b/libs/hwui/renderstate/PixelBufferState.h
new file mode 100644
index 0000000..f7ae6c5
--- /dev/null
+++ b/libs/hwui/renderstate/PixelBufferState.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+#ifndef RENDERSTATE_PIXELBUFFERSTATE_H
+#define RENDERSTATE_PIXELBUFFERSTATE_H
+
+#include <GLES3/gl3.h>
+
+namespace android {
+namespace uirenderer {
+
+class PixelBufferState {
+    friend class Caches;  // TODO: move to RenderState
+public:
+    bool bind(GLuint buffer);
+    bool unbind();
+
+private:
+    PixelBufferState();
+    GLuint mCurrentPixelBuffer;
+};
+
+} /* namespace uirenderer */
+} /* namespace android */
+
+#endif  // RENDERSTATE_PIXELBUFFERSTATE_H
diff --git a/libs/hwui/renderstate/RenderState.cpp b/libs/hwui/renderstate/RenderState.cpp
index b524bcb..3be84f5 100644
--- a/libs/hwui/renderstate/RenderState.cpp
+++ b/libs/hwui/renderstate/RenderState.cpp
@@ -16,6 +16,8 @@
 #include "renderstate/RenderState.h"
 #include <GpuMemoryTracker.h>
 #include "DeferredLayerUpdater.h"
+#include "GlLayer.h"
+#include "VkLayer.h"
 #include "Snapshot.h"
 
 #include "renderthread/CanvasContext.h"
@@ -37,11 +39,44 @@
 RenderState::~RenderState() {
 }
 
-void RenderState::onContextCreated() {
+void RenderState::onGLContextCreated() {
+    GpuMemoryTracker::onGpuContextCreated();
+
+    // This is delayed because the first access of Caches makes GL calls
+    if (!mCaches) {
+        mCaches = &Caches::createInstance(*this);
+    }
+    mCaches->init();
+}
+
+static void layerLostGlContext(Layer* layer) {
+    LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::OpenGL,
+                        "layerLostGlContext on non GL layer");
+    static_cast<GlLayer*>(layer)->onGlContextLost();
+}
+
+void RenderState::onGLContextDestroyed() {
+    // TODO: reset all cached state in state objects
+    std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerLostGlContext);
+
+    mCaches->terminate();
+
+    destroyLayersInUpdater();
+    GpuMemoryTracker::onGpuContextDestroyed();
+}
+
+void RenderState::onVkContextCreated() {
     GpuMemoryTracker::onGpuContextCreated();
 }
 
-void RenderState::onContextDestroyed() {
+static void layerDestroyedVkContext(Layer* layer) {
+    LOG_ALWAYS_FATAL_IF(layer->getApi() != Layer::Api::Vulkan,
+                        "layerLostVkContext on non Vulkan layer");
+    static_cast<VkLayer*>(layer)->onVkContextDestroyed();
+}
+
+void RenderState::onVkContextDestroyed() {
+    std::for_each(mActiveLayers.begin(), mActiveLayers.end(), layerDestroyedVkContext);
     destroyLayersInUpdater();
     GpuMemoryTracker::onGpuContextDestroyed();
 }
@@ -50,6 +85,10 @@
     return mRenderThread.getGrContext();
 }
 
+void RenderState::flush(Caches::FlushMode mode) {
+    if (mCaches) mCaches->flush(mode);
+}
+
 void RenderState::onBitmapDestroyed(uint32_t pixelRefId) {
     // DEAD CODE
 }
@@ -87,6 +126,42 @@
     glDeleteFramebuffers(1, &fbo);
 }
 
+void RenderState::invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info) {
+    if (mode == DrawGlInfo::kModeProcessNoContext) {
+        // If there's no context we don't need to interrupt as there's
+        // no gl state to save/restore
+        (*functor)(mode, info);
+    } else {
+        interruptForFunctorInvoke();
+        (*functor)(mode, info);
+        resumeFromFunctorInvoke();
+    }
+}
+
+void RenderState::interruptForFunctorInvoke() {
+    mCaches->textureState().resetActiveTexture();
+    debugOverdraw(false, false);
+    // TODO: We need a way to know whether the functor is sRGB aware (b/32072673)
+    if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
+        glDisable(GL_FRAMEBUFFER_SRGB_EXT);
+    }
+}
+
+void RenderState::resumeFromFunctorInvoke() {
+    if (mCaches->extensions().hasLinearBlending() && mCaches->extensions().hasSRGBWriteControl()) {
+        glEnable(GL_FRAMEBUFFER_SRGB_EXT);
+    }
+
+    glViewport(0, 0, mViewportWidth, mViewportHeight);
+    glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
+    debugOverdraw(false, false);
+
+    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+
+    mCaches->textureState().activateTexture(0);
+    mCaches->textureState().resetBoundTextures();
+}
+
 void RenderState::debugOverdraw(bool enable, bool clear) {
     // DEAD CODE
 }
@@ -115,9 +190,5 @@
     // DEAD CODE
 }
 
-renderthread::RenderThread& RenderState::getRenderThread() {
-    return mRenderThread;
-}
-
 } /* namespace uirenderer */
 } /* namespace android */
diff --git a/libs/hwui/renderstate/RenderState.h b/libs/hwui/renderstate/RenderState.h
index f39aa4b..97785a4 100644
--- a/libs/hwui/renderstate/RenderState.h
+++ b/libs/hwui/renderstate/RenderState.h
@@ -16,6 +16,8 @@
 #ifndef RENDERSTATE_H
 #define RENDERSTATE_H
 
+#include "Caches.h"
+#include "renderstate/PixelBufferState.h"
 #include "utils/Macros.h"
 
 #include <GLES2/gl2.h>
@@ -32,6 +34,7 @@
 namespace android {
 namespace uirenderer {
 
+class Caches;
 class Layer;
 class DeferredLayerUpdater;
 
@@ -41,16 +44,22 @@
 class RenderThread;
 }
 
+// TODO: Replace Cache's GL state tracking with this. For now it's more a thin
 // wrapper of Caches for users to migrate to.
 class RenderState {
     PREVENT_COPY_AND_ASSIGN(RenderState);
     friend class renderthread::RenderThread;
+    friend class Caches;
     friend class renderthread::CacheManager;
 
 public:
-    void onContextCreated();
-    void onContextDestroyed();
+    void onGLContextCreated();
+    void onGLContextDestroyed();
 
+    void onVkContextCreated();
+    void onVkContextDestroyed();
+
+    void flush(Caches::FlushMode flushMode);
     void onBitmapDestroyed(uint32_t pixelRefId);
 
     void setViewport(GLsizei width, GLsizei height);
@@ -61,6 +70,8 @@
     GLuint createFramebuffer();
     void deleteFramebuffer(GLuint fbo);
 
+    void invokeFunctor(Functor* functor, DrawGlInfo::Mode mode, DrawGlInfo* info);
+
     void debugOverdraw(bool enable, bool clear);
 
     void registerLayer(Layer* layer) { mActiveLayers.insert(layer); }
@@ -90,15 +101,16 @@
 
     void dump();
 
-    renderthread::RenderThread& getRenderThread();
-
 private:
+    void interruptForFunctorInvoke();
+    void resumeFromFunctorInvoke();
     void destroyLayersInUpdater();
 
     explicit RenderState(renderthread::RenderThread& thread);
     ~RenderState();
 
     renderthread::RenderThread& mRenderThread;
+    Caches* mCaches = nullptr;
 
     std::set<Layer*> mActiveLayers;
     std::set<DeferredLayerUpdater*> mActiveLayerUpdaters;
diff --git a/libs/hwui/renderstate/TextureState.cpp b/libs/hwui/renderstate/TextureState.cpp
new file mode 100644
index 0000000..470b4f5
--- /dev/null
+++ b/libs/hwui/renderstate/TextureState.cpp
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+#include "renderstate/TextureState.h"
+
+#include "Caches.h"
+#include "utils/TraceUtils.h"
+
+#include <GLES3/gl3.h>
+#include <SkBitmap.h>
+#include <SkCanvas.h>
+#include <memory>
+
+namespace android {
+namespace uirenderer {
+
+// Width of mShadowLutTexture, defines how accurate the shadow alpha lookup table is
+static const int SHADOW_LUT_SIZE = 128;
+
+// Must define as many texture units as specified by kTextureUnitsCount
+const GLenum kTextureUnits[] = {GL_TEXTURE0, GL_TEXTURE1, GL_TEXTURE2, GL_TEXTURE3};
+
+TextureState::TextureState() : mTextureUnit(0) {
+    glActiveTexture(kTextureUnits[0]);
+    resetBoundTextures();
+
+    GLint maxTextureUnits;
+    glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
+    LOG_ALWAYS_FATAL_IF(maxTextureUnits < kTextureUnitsCount,
+                        "At least %d texture units are required!", kTextureUnitsCount);
+    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+}
+
+TextureState::~TextureState() {
+    if (mShadowLutTexture != nullptr) {
+        mShadowLutTexture->deleteTexture();
+    }
+}
+
+/**
+ * Maps shadow geometry 'alpha' varying (1 for darkest, 0 for transparent) to
+ * darkness at that spot. Input values of 0->1 should be mapped within the same
+ * range, but can affect the curve for a different visual falloff.
+ *
+ * This is used to populate the shadow LUT texture for quick lookup in the
+ * shadow shader.
+ */
+static float computeShadowOpacity(float ratio) {
+    // exponential falloff function provided by UX
+    float val = 1 - ratio;
+    return exp(-val * val * 4.0) - 0.018;
+}
+
+void TextureState::constructTexture(Caches& caches) {
+    if (mShadowLutTexture == nullptr) {
+        mShadowLutTexture.reset(new Texture(caches));
+
+        unsigned char bytes[SHADOW_LUT_SIZE];
+        for (int i = 0; i < SHADOW_LUT_SIZE; i++) {
+            float inputRatio = i / (SHADOW_LUT_SIZE - 1.0f);
+            bytes[i] = computeShadowOpacity(inputRatio) * 255;
+        }
+        mShadowLutTexture->upload(GL_ALPHA, SHADOW_LUT_SIZE, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &bytes);
+        mShadowLutTexture->setFilter(GL_LINEAR);
+        mShadowLutTexture->setWrap(GL_CLAMP_TO_EDGE);
+    }
+}
+
+void TextureState::activateTexture(GLuint textureUnit) {
+    LOG_ALWAYS_FATAL_IF(textureUnit >= kTextureUnitsCount,
+                        "Tried to use texture unit index %d, only %d exist", textureUnit,
+                        kTextureUnitsCount);
+    if (mTextureUnit != textureUnit) {
+        glActiveTexture(kTextureUnits[textureUnit]);
+        mTextureUnit = textureUnit;
+    }
+}
+
+void TextureState::resetActiveTexture() {
+    mTextureUnit = -1;
+}
+
+void TextureState::bindTexture(GLuint texture) {
+    if (mBoundTextures[mTextureUnit] != texture) {
+        glBindTexture(GL_TEXTURE_2D, texture);
+        mBoundTextures[mTextureUnit] = texture;
+    }
+}
+
+void TextureState::bindTexture(GLenum target, GLuint texture) {
+    if (target == GL_TEXTURE_2D) {
+        bindTexture(texture);
+    } else {
+        // GLConsumer directly calls glBindTexture() with
+        // target=GL_TEXTURE_EXTERNAL_OES, don't cache this target
+        // since the cached state could be stale
+        glBindTexture(target, texture);
+    }
+}
+
+void TextureState::deleteTexture(GLuint texture) {
+    // When glDeleteTextures() is called on a currently bound texture,
+    // OpenGL ES specifies that the texture is then considered unbound
+    // Consider the following series of calls:
+    //
+    // glGenTextures -> creates texture name 2
+    // glBindTexture(2)
+    // glDeleteTextures(2) -> 2 is now unbound
+    // glGenTextures -> can return 2 again
+    //
+    // If we don't call glBindTexture(2) after the second glGenTextures
+    // call, any texture operation will be performed on the default
+    // texture (name=0)
+
+    unbindTexture(texture);
+
+    glDeleteTextures(1, &texture);
+}
+
+void TextureState::resetBoundTextures() {
+    for (int i = 0; i < kTextureUnitsCount; i++) {
+        mBoundTextures[i] = 0;
+    }
+}
+
+void TextureState::unbindTexture(GLuint texture) {
+    for (int i = 0; i < kTextureUnitsCount; i++) {
+        if (mBoundTextures[i] == texture) {
+            mBoundTextures[i] = 0;
+        }
+    }
+}
+
+} /* namespace uirenderer */
+} /* namespace android */
diff --git a/libs/hwui/renderstate/TextureState.h b/libs/hwui/renderstate/TextureState.h
new file mode 100644
index 0000000..f1996d4
--- /dev/null
+++ b/libs/hwui/renderstate/TextureState.h
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+#ifndef RENDERSTATE_TEXTURESTATE_H
+#define RENDERSTATE_TEXTURESTATE_H
+
+#include "Texture.h"
+#include "Vertex.h"
+
+#include <GLES2/gl2.h>
+#include <GLES2/gl2ext.h>
+#include <memory>
+
+namespace android {
+namespace uirenderer {
+
+class Texture;
+
+class TextureState {
+    friend class Caches;  // TODO: move to RenderState
+public:
+    void constructTexture(Caches& caches);
+
+    /**
+     * Activate the specified texture unit. The texture unit must
+     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
+     */
+    void activateTexture(GLuint textureUnit);
+
+    /**
+     * Invalidate the cached value of the active texture unit.
+     */
+    void resetActiveTexture();
+
+    /**
+     * Binds the specified texture as a GL_TEXTURE_2D texture.
+     * All texture bindings must be performed with this method or
+     * bindTexture(GLenum, GLuint).
+     */
+    void bindTexture(GLuint texture);
+
+    /**
+     * Binds the specified texture with the specified render target.
+     * All texture bindings must be performed with this method or
+     * bindTexture(GLuint).
+     */
+    void bindTexture(GLenum target, GLuint texture);
+
+    /**
+     * Deletes the specified texture and clears it from the cache
+     * of bound textures.
+     * All textures must be deleted using this method.
+     */
+    void deleteTexture(GLuint texture);
+
+    /**
+     * Signals that the cache of bound textures should be cleared.
+     * Other users of the context may have altered which textures are bound.
+     */
+    void resetBoundTextures();
+
+    /**
+     * Clear the cache of bound textures.
+     */
+    void unbindTexture(GLuint texture);
+
+    Texture* getShadowLutTexture() { return mShadowLutTexture.get(); }
+
+private:
+    // total number of texture units available for use
+    static const int kTextureUnitsCount = 4;
+
+    TextureState();
+    ~TextureState();
+    GLuint mTextureUnit;
+
+    // Caches texture bindings for the GL_TEXTURE_2D target
+    GLuint mBoundTextures[kTextureUnitsCount];
+
+    std::unique_ptr<Texture> mShadowLutTexture;
+};
+
+} /* namespace uirenderer */
+} /* namespace android */
+
+#endif  // RENDERSTATE_BLEND_H
diff --git a/libs/hwui/renderthread/CacheManager.cpp b/libs/hwui/renderthread/CacheManager.cpp
index c45eeda..bec80b1e 100644
--- a/libs/hwui/renderthread/CacheManager.cpp
+++ b/libs/hwui/renderthread/CacheManager.cpp
@@ -21,7 +21,6 @@
 #include "RenderThread.h"
 #include "pipeline/skia/ShaderCache.h"
 #include "pipeline/skia/SkiaMemoryTracer.h"
-#include "Properties.h"
 #include "renderstate/RenderState.h"
 
 #include <GrContextOptions.h>
@@ -215,12 +214,11 @@
             log.appendFormat("  Layer Info:\n");
         }
 
-        const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
-                ? "GlLayer" : "VkLayer";
         size_t layerMemoryTotal = 0;
         for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
              it != renderState->mActiveLayers.end(); it++) {
             const Layer* layer = *it;
+            const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
             log.appendFormat("    %s size %dx%d\n", layerType, layer->getWidth(),
                              layer->getHeight());
             layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index 8b07d1d..5d72523 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -18,6 +18,7 @@
 #include <GpuMemoryTracker.h>
 
 #include "AnimationContext.h"
+#include "Caches.h"
 #include "EglManager.h"
 #include "Frame.h"
 #include "LayerUpdateQueue.h"
@@ -494,6 +495,13 @@
     }
 
     GpuMemoryTracker::onFrameCompleted();
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    auto renderType = Properties::getRenderPipelineType();
+    if (RenderPipelineType::OpenGL == renderType) {
+        Caches& caches = Caches::getInstance();
+        caches.fontRenderer.getFontRenderer().historyTracker().frameCompleted();
+    }
+#endif
 }
 
 // Called by choreographer to do an RT-driven animation
diff --git a/libs/hwui/renderthread/EglManager.cpp b/libs/hwui/renderthread/EglManager.cpp
index 5f8d7ad..cd21822 100644
--- a/libs/hwui/renderthread/EglManager.cpp
+++ b/libs/hwui/renderthread/EglManager.cpp
@@ -18,7 +18,6 @@
 
 #include <cutils/properties.h>
 #include <log/log.h>
-#include <private/gui/SyncFeatures.h>
 #include <utils/Trace.h>
 #include "utils/StringUtils.h"
 
@@ -465,109 +464,6 @@
     return preserved;
 }
 
-status_t EglManager::fenceWait(sp<Fence>& fence) {
-    if (!hasEglContext()) {
-        ALOGE("EglManager::fenceWait: EGLDisplay not initialized");
-        return INVALID_OPERATION;
-    }
-
-    if (SyncFeatures::getInstance().useWaitSync() &&
-        SyncFeatures::getInstance().useNativeFenceSync()) {
-        // Block GPU on the fence.
-        // Create an EGLSyncKHR from the current fence.
-        int fenceFd = fence->dup();
-        if (fenceFd == -1) {
-            ALOGE("EglManager::fenceWait: error dup'ing fence fd: %d", errno);
-            return -errno;
-        }
-        EGLint attribs[] = {
-            EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd,
-            EGL_NONE
-        };
-        EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
-                EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
-        if (sync == EGL_NO_SYNC_KHR) {
-            close(fenceFd);
-            ALOGE("EglManager::fenceWait: error creating EGL fence: %#x", eglGetError());
-            return UNKNOWN_ERROR;
-        }
-
-        // XXX: The spec draft is inconsistent as to whether this should
-        // return an EGLint or void.  Ignore the return value for now, as
-        // it's not strictly needed.
-        eglWaitSyncKHR(mEglDisplay, sync, 0);
-        EGLint eglErr = eglGetError();
-        eglDestroySyncKHR(mEglDisplay, sync);
-        if (eglErr != EGL_SUCCESS) {
-            ALOGE("EglManager::fenceWait: error waiting for EGL fence: %#x", eglErr);
-            return UNKNOWN_ERROR;
-        }
-    } else {
-        // Block CPU on the fence.
-        status_t err = fence->waitForever("EglManager::fenceWait");
-        if (err != NO_ERROR) {
-            ALOGE("EglManager::fenceWait: error waiting for fence: %d", err);
-            return err;
-        }
-    }
-    return OK;
-}
-
-status_t EglManager::createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence,
-        sp<Fence>& nativeFence) {
-    if (!hasEglContext()) {
-        ALOGE("EglManager::createReleaseFence: EGLDisplay not initialized");
-        return INVALID_OPERATION;
-    }
-
-    if (SyncFeatures::getInstance().useNativeFenceSync()) {
-        EGLSyncKHR sync = eglCreateSyncKHR(mEglDisplay,
-                EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
-        if (sync == EGL_NO_SYNC_KHR) {
-            ALOGE("EglManager::createReleaseFence: error creating EGL fence: %#x",
-                    eglGetError());
-            return UNKNOWN_ERROR;
-        }
-        glFlush();
-        int fenceFd = eglDupNativeFenceFDANDROID(mEglDisplay, sync);
-        eglDestroySyncKHR(mEglDisplay, sync);
-        if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
-            ALOGE("EglManager::createReleaseFence: error dup'ing native fence "
-                    "fd: %#x", eglGetError());
-            return UNKNOWN_ERROR;
-        }
-        nativeFence = new Fence(fenceFd);
-        *eglFence = EGL_NO_SYNC_KHR;
-    } else if (useFenceSync && SyncFeatures::getInstance().useFenceSync()) {
-        if (*eglFence != EGL_NO_SYNC_KHR) {
-            // There is already a fence for the current slot.  We need to
-            // wait on that before replacing it with another fence to
-            // ensure that all outstanding buffer accesses have completed
-            // before the producer accesses it.
-            EGLint result = eglClientWaitSyncKHR(mEglDisplay, *eglFence, 0, 1000000000);
-            if (result == EGL_FALSE) {
-                ALOGE("EglManager::createReleaseFence: error waiting for previous fence: %#x",
-                        eglGetError());
-                return UNKNOWN_ERROR;
-            } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
-                ALOGE("EglManager::createReleaseFence: timeout waiting for previous fence");
-                return TIMED_OUT;
-            }
-            eglDestroySyncKHR(mEglDisplay, *eglFence);
-        }
-
-        // Create a fence for the outstanding accesses in the current
-        // OpenGL ES context.
-        *eglFence = eglCreateSyncKHR(mEglDisplay, EGL_SYNC_FENCE_KHR, nullptr);
-        if (*eglFence == EGL_NO_SYNC_KHR) {
-            ALOGE("EglManager::createReleaseFence: error creating fence: %#x", eglGetError());
-            return UNKNOWN_ERROR;
-        }
-        glFlush();
-    }
-    return OK;
-}
-
 } /* namespace renderthread */
 } /* namespace uirenderer */
 } /* namespace android */
diff --git a/libs/hwui/renderthread/EglManager.h b/libs/hwui/renderthread/EglManager.h
index 507673a..8e8bb8b 100644
--- a/libs/hwui/renderthread/EglManager.h
+++ b/libs/hwui/renderthread/EglManager.h
@@ -17,10 +17,8 @@
 #define EGLMANAGER_H
 
 #include <EGL/egl.h>
-#include <EGL/eglext.h>
 #include <SkRect.h>
 #include <cutils/compiler.h>
-#include <ui/Fence.h>
 #include <ui/GraphicBuffer.h>
 #include <utils/StrongPointer.h>
 
@@ -68,14 +66,6 @@
 
     EGLDisplay eglDisplay() const { return mEglDisplay; }
 
-    // Inserts a wait on fence command into the OpenGL ES command stream. If EGL extension
-    // support is missing, block the CPU on the fence.
-    status_t fenceWait(sp<Fence>& fence);
-
-    // Creates a fence that is signaled, when all the pending GL commands are flushed.
-    // Depending on installed extensions, the result is either Android native fence or EGL fence.
-    status_t createReleaseFence(bool useFenceSync, EGLSyncKHR* eglFence, sp<Fence>& nativeFence);
-
 private:
 
     void initExtensions();
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 65f95ad..c1284ec 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -178,7 +178,7 @@
         return;
     }
     mEglManager->initialize();
-    renderState().onContextCreated();
+    renderState().onGLContextCreated();
 
 #ifdef HWUI_GLES_WRAP_ENABLED
     debug::GlesDriver* driver = debug::GlesDriver::get();
@@ -200,7 +200,7 @@
 void RenderThread::destroyGlContext() {
     if (mEglManager->hasEglContext()) {
         setGrContext(nullptr);
-        renderState().onContextDestroyed();
+        renderState().onGLContextDestroyed();
         mEglManager->destroy();
     }
 }
diff --git a/libs/hwui/renderthread/VulkanManager.cpp b/libs/hwui/renderthread/VulkanManager.cpp
index 0c49dc0..1517f57 100644
--- a/libs/hwui/renderthread/VulkanManager.cpp
+++ b/libs/hwui/renderthread/VulkanManager.cpp
@@ -40,7 +40,7 @@
 VulkanManager::VulkanManager(RenderThread& thread) : mRenderThread(thread) {}
 
 void VulkanManager::destroy() {
-    mRenderThread.renderState().onContextDestroyed();
+    mRenderThread.renderState().onVkContextDestroyed();
     mRenderThread.setGrContext(nullptr);
 
     if (VK_NULL_HANDLE != mCommandPool) {
@@ -404,7 +404,7 @@
         mSwapBehavior = SwapBehavior::BufferAge;
     }
 
-    mRenderThread.renderState().onContextCreated();
+    mRenderThread.renderState().onVkContextCreated();
 }
 
 // Returns the next BackbufferInfo to use for the next draw. The function will make sure all
@@ -981,22 +981,6 @@
     return surface->mCurrentTime - lastUsed;
 }
 
-status_t VulkanManager::fenceWait(sp<Fence>& fence) {
-    //TODO: Insert a wait on fence command into the Vulkan command buffer.
-    // Block CPU on the fence.
-    status_t err = fence->waitForever("VulkanManager::fenceWait");
-    if (err != NO_ERROR) {
-        ALOGE("VulkanManager::fenceWait: error waiting for fence: %d", err);
-        return err;
-    }
-    return OK;
-}
-
-status_t VulkanManager::createReleaseFence(sp<Fence>& nativeFence) {
-    //TODO: Create a fence that is signaled, when all the pending Vulkan commands are flushed.
-    return OK;
-}
-
 } /* namespace renderthread */
 } /* namespace uirenderer */
 } /* namespace android */
diff --git a/libs/hwui/renderthread/VulkanManager.h b/libs/hwui/renderthread/VulkanManager.h
index ebc11a5..5524c39 100644
--- a/libs/hwui/renderthread/VulkanManager.h
+++ b/libs/hwui/renderthread/VulkanManager.h
@@ -23,8 +23,6 @@
 #include <vulkan/vulkan.h>
 
 #include <SkSurface.h>
-#include <ui/Fence.h>
-#include <utils/StrongPointer.h>
 #include <vk/GrVkBackendContext.h>
 
 class GrVkExtensions;
@@ -112,12 +110,6 @@
     // Presents the current VkImage.
     void swapBuffers(VulkanSurface* surface);
 
-    // Inserts a wait on fence command into the Vulkan command buffer.
-    status_t fenceWait(sp<Fence>& fence);
-
-    // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
-    status_t createReleaseFence(sp<Fence>& nativeFence);
-
 private:
     friend class RenderThread;
 
diff --git a/libs/hwui/surfacetexture/EGLConsumer.cpp b/libs/hwui/surfacetexture/EGLConsumer.cpp
deleted file mode 100644
index c8220c6..0000000
--- a/libs/hwui/surfacetexture/EGLConsumer.cpp
+++ /dev/null
@@ -1,675 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#include <inttypes.h>
-
-#include <EGL/egl.h>
-#include <EGL/eglext.h>
-#include <GLES2/gl2.h>
-#include <GLES2/gl2ext.h>
-#include <cutils/compiler.h>
-#include <gui/BufferItem.h>
-#include <gui/BufferQueue.h>
-#include <private/gui/SyncFeatures.h>
-#include "EGLConsumer.h"
-#include "SurfaceTexture.h"
-
-#include <utils/Log.h>
-#include <utils/String8.h>
-#include <utils/Trace.h>
-
-#define PROT_CONTENT_EXT_STR "EGL_EXT_protected_content"
-#define EGL_PROTECTED_CONTENT_EXT 0x32C0
-
-namespace android {
-
-// Macros for including the SurfaceTexture name in log messages
-#define EGC_LOGV(x, ...) ALOGV("[%s] " x, st.mName.string(), ##__VA_ARGS__)
-#define EGC_LOGD(x, ...) ALOGD("[%s] " x, st.mName.string(), ##__VA_ARGS__)
-#define EGC_LOGW(x, ...) ALOGW("[%s] " x, st.mName.string(), ##__VA_ARGS__)
-#define EGC_LOGE(x, ...) ALOGE("[%s] " x, st.mName.string(), ##__VA_ARGS__)
-
-static const struct {
-    uint32_t width, height;
-    char const* bits;
-} kDebugData = {15, 12,
-                "_______________"
-                "_______________"
-                "_____XX_XX_____"
-                "__X_X_____X_X__"
-                "__X_XXXXXXX_X__"
-                "__XXXXXXXXXXX__"
-                "___XX_XXX_XX___"
-                "____XXXXXXX____"
-                "_____X___X_____"
-                "____X_____X____"
-                "_______________"
-                "_______________"};
-
-Mutex EGLConsumer::sStaticInitLock;
-sp<GraphicBuffer> EGLConsumer::sReleasedTexImageBuffer;
-
-static bool hasEglProtectedContentImpl() {
-    EGLDisplay dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
-    const char* exts = eglQueryString(dpy, EGL_EXTENSIONS);
-    size_t cropExtLen = strlen(PROT_CONTENT_EXT_STR);
-    size_t extsLen = strlen(exts);
-    bool equal = !strcmp(PROT_CONTENT_EXT_STR, exts);
-    bool atStart = !strncmp(PROT_CONTENT_EXT_STR " ", exts, cropExtLen + 1);
-    bool atEnd = (cropExtLen + 1) < extsLen &&
-                 !strcmp(" " PROT_CONTENT_EXT_STR, exts + extsLen - (cropExtLen + 1));
-    bool inMiddle = strstr(exts, " " PROT_CONTENT_EXT_STR " ");
-    return equal || atStart || atEnd || inMiddle;
-}
-
-static bool hasEglProtectedContent() {
-    // Only compute whether the extension is present once the first time this
-    // function is called.
-    static bool hasIt = hasEglProtectedContentImpl();
-    return hasIt;
-}
-
-EGLConsumer::EGLConsumer() : mEglDisplay(EGL_NO_DISPLAY), mEglContext(EGL_NO_CONTEXT) {}
-
-status_t EGLConsumer::updateTexImage(SurfaceTexture& st) {
-    // Make sure the EGL state is the same as in previous calls.
-    status_t err = checkAndUpdateEglStateLocked(st);
-    if (err != NO_ERROR) {
-        return err;
-    }
-
-    BufferItem item;
-
-    // Acquire the next buffer.
-    // In asynchronous mode the list is guaranteed to be one buffer
-    // deep, while in synchronous mode we use the oldest buffer.
-    err = st.acquireBufferLocked(&item, 0);
-    if (err != NO_ERROR) {
-        if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
-            // We always bind the texture even if we don't update its contents.
-            EGC_LOGV("updateTexImage: no buffers were available");
-            glBindTexture(st.mTexTarget, st.mTexName);
-            err = NO_ERROR;
-        } else {
-            EGC_LOGE("updateTexImage: acquire failed: %s (%d)", strerror(-err), err);
-        }
-        return err;
-    }
-
-    // Release the previous buffer.
-    err = updateAndReleaseLocked(item, nullptr, st);
-    if (err != NO_ERROR) {
-        // We always bind the texture.
-        glBindTexture(st.mTexTarget, st.mTexName);
-        return err;
-    }
-
-    // Bind the new buffer to the GL texture, and wait until it's ready.
-    return bindTextureImageLocked(st);
-}
-
-status_t EGLConsumer::releaseTexImage(SurfaceTexture& st) {
-    // Make sure the EGL state is the same as in previous calls.
-    status_t err = NO_ERROR;
-
-    // if we're detached, no need to validate EGL's state -- we won't use it.
-    if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
-        err = checkAndUpdateEglStateLocked(st, true);
-        if (err != NO_ERROR) {
-            return err;
-        }
-    }
-
-    // Update the EGLConsumer state.
-    int buf = st.mCurrentTexture;
-    if (buf != BufferQueue::INVALID_BUFFER_SLOT) {
-        EGC_LOGV("releaseTexImage: (slot=%d, mOpMode=%d)", buf, (int)st.mOpMode);
-
-        // if we're detached, we just use the fence that was created in detachFromContext()
-        // so... basically, nothing more to do here.
-        if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
-            // Do whatever sync ops we need to do before releasing the slot.
-            err = syncForReleaseLocked(mEglDisplay, st);
-            if (err != NO_ERROR) {
-                EGC_LOGE("syncForReleaseLocked failed (slot=%d), err=%d", buf, err);
-                return err;
-            }
-        }
-
-        err = st.releaseBufferLocked(buf, st.mSlots[buf].mGraphicBuffer, mEglDisplay,
-                                     EGL_NO_SYNC_KHR);
-        if (err < NO_ERROR) {
-            EGC_LOGE("releaseTexImage: failed to release buffer: %s (%d)", strerror(-err), err);
-            return err;
-        }
-
-        if (mReleasedTexImage == nullptr) {
-            mReleasedTexImage = new EglImage(getDebugTexImageBuffer());
-        }
-
-        st.mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
-        mCurrentTextureImage = mReleasedTexImage;
-        st.mCurrentCrop.makeInvalid();
-        st.mCurrentTransform = 0;
-        st.mCurrentTimestamp = 0;
-        st.mCurrentDataSpace = HAL_DATASPACE_UNKNOWN;
-        st.mCurrentFence = Fence::NO_FENCE;
-        st.mCurrentFenceTime = FenceTime::NO_FENCE;
-
-        // detached, don't touch the texture (and we may not even have an
-        // EGLDisplay here.
-        if (st.mOpMode == SurfaceTexture::OpMode::attachedToGL) {
-            // This binds a dummy buffer (mReleasedTexImage).
-            status_t result = bindTextureImageLocked(st);
-            if (result != NO_ERROR) {
-                return result;
-            }
-        }
-    }
-
-    return NO_ERROR;
-}
-
-sp<GraphicBuffer> EGLConsumer::getDebugTexImageBuffer() {
-    Mutex::Autolock _l(sStaticInitLock);
-    if (CC_UNLIKELY(sReleasedTexImageBuffer == nullptr)) {
-        // The first time, create the debug texture in case the application
-        // continues to use it.
-        sp<GraphicBuffer> buffer = new GraphicBuffer(
-                kDebugData.width, kDebugData.height, PIXEL_FORMAT_RGBA_8888,
-                GraphicBuffer::USAGE_SW_WRITE_RARELY, "[EGLConsumer debug texture]");
-        uint32_t* bits;
-        buffer->lock(GraphicBuffer::USAGE_SW_WRITE_RARELY, reinterpret_cast<void**>(&bits));
-        uint32_t stride = buffer->getStride();
-        uint32_t height = buffer->getHeight();
-        memset(bits, 0, stride * height * 4);
-        for (uint32_t y = 0; y < kDebugData.height; y++) {
-            for (uint32_t x = 0; x < kDebugData.width; x++) {
-                bits[x] = (kDebugData.bits[y + kDebugData.width + x] == 'X') ? 0xFF000000
-                                                                             : 0xFFFFFFFF;
-            }
-            bits += stride;
-        }
-        buffer->unlock();
-        sReleasedTexImageBuffer = buffer;
-    }
-    return sReleasedTexImageBuffer;
-}
-
-void EGLConsumer::onAcquireBufferLocked(BufferItem* item, SurfaceTexture& st) {
-    // If item->mGraphicBuffer is not null, this buffer has not been acquired
-    // before, so any prior EglImage created is using a stale buffer. This
-    // replaces any old EglImage with a new one (using the new buffer).
-    int slot = item->mSlot;
-    if (item->mGraphicBuffer != nullptr || mEglSlots[slot].mEglImage.get() == nullptr) {
-        mEglSlots[slot].mEglImage = new EglImage(st.mSlots[slot].mGraphicBuffer);
-    }
-}
-
-void EGLConsumer::onReleaseBufferLocked(int buf) {
-    mEglSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
-}
-
-status_t EGLConsumer::updateAndReleaseLocked(const BufferItem& item, PendingRelease* pendingRelease,
-                                             SurfaceTexture& st) {
-    status_t err = NO_ERROR;
-
-    int slot = item.mSlot;
-
-    if (st.mOpMode != SurfaceTexture::OpMode::attachedToGL) {
-        EGC_LOGE(
-                "updateAndRelease: EGLConsumer is not attached to an OpenGL "
-                "ES context");
-        st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
-        return INVALID_OPERATION;
-    }
-
-    // Confirm state.
-    err = checkAndUpdateEglStateLocked(st);
-    if (err != NO_ERROR) {
-        st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
-        return err;
-    }
-
-    // Ensure we have a valid EglImageKHR for the slot, creating an EglImage
-    // if nessessary, for the gralloc buffer currently in the slot in
-    // ConsumerBase.
-    // We may have to do this even when item.mGraphicBuffer == NULL (which
-    // means the buffer was previously acquired).
-    err = mEglSlots[slot].mEglImage->createIfNeeded(mEglDisplay);
-    if (err != NO_ERROR) {
-        EGC_LOGW("updateAndRelease: unable to createImage on display=%p slot=%d", mEglDisplay,
-                 slot);
-        st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay, EGL_NO_SYNC_KHR);
-        return UNKNOWN_ERROR;
-    }
-
-    // Do whatever sync ops we need to do before releasing the old slot.
-    if (slot != st.mCurrentTexture) {
-        err = syncForReleaseLocked(mEglDisplay, st);
-        if (err != NO_ERROR) {
-            // Release the buffer we just acquired.  It's not safe to
-            // release the old buffer, so instead we just drop the new frame.
-            // As we are still under lock since acquireBuffer, it is safe to
-            // release by slot.
-            st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, mEglDisplay,
-                                   EGL_NO_SYNC_KHR);
-            return err;
-        }
-    }
-
-    EGC_LOGV(
-            "updateAndRelease: (slot=%d buf=%p) -> (slot=%d buf=%p)", st.mCurrentTexture,
-            mCurrentTextureImage != nullptr ? mCurrentTextureImage->graphicBufferHandle() : nullptr,
-            slot, st.mSlots[slot].mGraphicBuffer->handle);
-
-    // Hang onto the pointer so that it isn't freed in the call to
-    // releaseBufferLocked() if we're in shared buffer mode and both buffers are
-    // the same.
-    sp<EglImage> nextTextureImage = mEglSlots[slot].mEglImage;
-
-    // release old buffer
-    if (st.mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
-        if (pendingRelease == nullptr) {
-            status_t status = st.releaseBufferLocked(
-                    st.mCurrentTexture, mCurrentTextureImage->graphicBuffer(), mEglDisplay,
-                    mEglSlots[st.mCurrentTexture].mEglFence);
-            if (status < NO_ERROR) {
-                EGC_LOGE("updateAndRelease: failed to release buffer: %s (%d)", strerror(-status),
-                         status);
-                err = status;
-                // keep going, with error raised [?]
-            }
-        } else {
-            pendingRelease->currentTexture = st.mCurrentTexture;
-            pendingRelease->graphicBuffer = mCurrentTextureImage->graphicBuffer();
-            pendingRelease->display = mEglDisplay;
-            pendingRelease->fence = mEglSlots[st.mCurrentTexture].mEglFence;
-            pendingRelease->isPending = true;
-        }
-    }
-
-    // Update the EGLConsumer state.
-    st.mCurrentTexture = slot;
-    mCurrentTextureImage = nextTextureImage;
-    st.mCurrentCrop = item.mCrop;
-    st.mCurrentTransform = item.mTransform;
-    st.mCurrentScalingMode = item.mScalingMode;
-    st.mCurrentTimestamp = item.mTimestamp;
-    st.mCurrentDataSpace = item.mDataSpace;
-    st.mCurrentFence = item.mFence;
-    st.mCurrentFenceTime = item.mFenceTime;
-    st.mCurrentFrameNumber = item.mFrameNumber;
-
-    st.computeCurrentTransformMatrixLocked();
-
-    return err;
-}
-
-status_t EGLConsumer::bindTextureImageLocked(SurfaceTexture& st) {
-    if (mEglDisplay == EGL_NO_DISPLAY) {
-        ALOGE("bindTextureImage: invalid display");
-        return INVALID_OPERATION;
-    }
-
-    GLenum error;
-    while ((error = glGetError()) != GL_NO_ERROR) {
-        EGC_LOGW("bindTextureImage: clearing GL error: %#04x", error);
-    }
-
-    glBindTexture(st.mTexTarget, st.mTexName);
-    if (st.mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT && mCurrentTextureImage == nullptr) {
-        EGC_LOGE("bindTextureImage: no currently-bound texture");
-        return NO_INIT;
-    }
-
-    status_t err = mCurrentTextureImage->createIfNeeded(mEglDisplay);
-    if (err != NO_ERROR) {
-        EGC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
-                 st.mCurrentTexture);
-        return UNKNOWN_ERROR;
-    }
-    mCurrentTextureImage->bindToTextureTarget(st.mTexTarget);
-
-    // In the rare case that the display is terminated and then initialized
-    // again, we can't detect that the display changed (it didn't), but the
-    // image is invalid. In this case, repeat the exact same steps while
-    // forcing the creation of a new image.
-    if ((error = glGetError()) != GL_NO_ERROR) {
-        glBindTexture(st.mTexTarget, st.mTexName);
-        status_t result = mCurrentTextureImage->createIfNeeded(mEglDisplay, true);
-        if (result != NO_ERROR) {
-            EGC_LOGW("bindTextureImage: can't create image on display=%p slot=%d", mEglDisplay,
-                     st.mCurrentTexture);
-            return UNKNOWN_ERROR;
-        }
-        mCurrentTextureImage->bindToTextureTarget(st.mTexTarget);
-        if ((error = glGetError()) != GL_NO_ERROR) {
-            EGC_LOGE("bindTextureImage: error binding external image: %#04x", error);
-            return UNKNOWN_ERROR;
-        }
-    }
-
-    // Wait for the new buffer to be ready.
-    return doGLFenceWaitLocked(st);
-}
-
-status_t EGLConsumer::checkAndUpdateEglStateLocked(SurfaceTexture& st, bool contextCheck) {
-    EGLDisplay dpy = eglGetCurrentDisplay();
-    EGLContext ctx = eglGetCurrentContext();
-
-    if (!contextCheck) {
-        // if this is the first time we're called, mEglDisplay/mEglContext have
-        // never been set, so don't error out (below).
-        if (mEglDisplay == EGL_NO_DISPLAY) {
-            mEglDisplay = dpy;
-        }
-        if (mEglContext == EGL_NO_CONTEXT) {
-            mEglContext = ctx;
-        }
-    }
-
-    if (mEglDisplay != dpy || dpy == EGL_NO_DISPLAY) {
-        EGC_LOGE("checkAndUpdateEglState: invalid current EGLDisplay");
-        return INVALID_OPERATION;
-    }
-
-    if (mEglContext != ctx || ctx == EGL_NO_CONTEXT) {
-        EGC_LOGE("checkAndUpdateEglState: invalid current EGLContext");
-        return INVALID_OPERATION;
-    }
-
-    mEglDisplay = dpy;
-    mEglContext = ctx;
-    return NO_ERROR;
-}
-
-status_t EGLConsumer::detachFromContext(SurfaceTexture& st) {
-    EGLDisplay dpy = eglGetCurrentDisplay();
-    EGLContext ctx = eglGetCurrentContext();
-
-    if (mEglDisplay != dpy && mEglDisplay != EGL_NO_DISPLAY) {
-        EGC_LOGE("detachFromContext: invalid current EGLDisplay");
-        return INVALID_OPERATION;
-    }
-
-    if (mEglContext != ctx && mEglContext != EGL_NO_CONTEXT) {
-        EGC_LOGE("detachFromContext: invalid current EGLContext");
-        return INVALID_OPERATION;
-    }
-
-    if (dpy != EGL_NO_DISPLAY && ctx != EGL_NO_CONTEXT) {
-        status_t err = syncForReleaseLocked(dpy, st);
-        if (err != OK) {
-            return err;
-        }
-
-        glDeleteTextures(1, &st.mTexName);
-    }
-
-    mEglDisplay = EGL_NO_DISPLAY;
-    mEglContext = EGL_NO_CONTEXT;
-
-    return OK;
-}
-
-status_t EGLConsumer::attachToContext(uint32_t tex, SurfaceTexture& st) {
-    // Initialize mCurrentTextureImage if there is a current buffer from past attached state.
-    int slot = st.mCurrentTexture;
-    if (slot != BufferItem::INVALID_BUFFER_SLOT) {
-        if (!mEglSlots[slot].mEglImage.get()) {
-            mEglSlots[slot].mEglImage = new EglImage(st.mSlots[slot].mGraphicBuffer);
-        }
-        mCurrentTextureImage = mEglSlots[slot].mEglImage;
-    }
-
-    EGLDisplay dpy = eglGetCurrentDisplay();
-    EGLContext ctx = eglGetCurrentContext();
-
-    if (dpy == EGL_NO_DISPLAY) {
-        EGC_LOGE("attachToContext: invalid current EGLDisplay");
-        return INVALID_OPERATION;
-    }
-
-    if (ctx == EGL_NO_CONTEXT) {
-        EGC_LOGE("attachToContext: invalid current EGLContext");
-        return INVALID_OPERATION;
-    }
-
-    // We need to bind the texture regardless of whether there's a current
-    // buffer.
-    glBindTexture(st.mTexTarget, GLuint(tex));
-
-    mEglDisplay = dpy;
-    mEglContext = ctx;
-    st.mTexName = tex;
-    st.mOpMode = SurfaceTexture::OpMode::attachedToGL;
-
-    if (mCurrentTextureImage != nullptr) {
-        // This may wait for a buffer a second time. This is likely required if
-        // this is a different context, since otherwise the wait could be skipped
-        // by bouncing through another context. For the same context the extra
-        // wait is redundant.
-        status_t err = bindTextureImageLocked(st);
-        if (err != NO_ERROR) {
-            return err;
-        }
-    }
-
-    return OK;
-}
-
-status_t EGLConsumer::syncForReleaseLocked(EGLDisplay dpy, SurfaceTexture& st) {
-    EGC_LOGV("syncForReleaseLocked");
-
-    if (st.mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
-        if (SyncFeatures::getInstance().useNativeFenceSync()) {
-            EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
-            if (sync == EGL_NO_SYNC_KHR) {
-                EGC_LOGE("syncForReleaseLocked: error creating EGL fence: %#x", eglGetError());
-                return UNKNOWN_ERROR;
-            }
-            glFlush();
-            int fenceFd = eglDupNativeFenceFDANDROID(dpy, sync);
-            eglDestroySyncKHR(dpy, sync);
-            if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
-                EGC_LOGE(
-                        "syncForReleaseLocked: error dup'ing native fence "
-                        "fd: %#x",
-                        eglGetError());
-                return UNKNOWN_ERROR;
-            }
-            sp<Fence> fence(new Fence(fenceFd));
-            status_t err = st.addReleaseFenceLocked(st.mCurrentTexture,
-                                                    mCurrentTextureImage->graphicBuffer(), fence);
-            if (err != OK) {
-                EGC_LOGE(
-                        "syncForReleaseLocked: error adding release fence: "
-                        "%s (%d)",
-                        strerror(-err), err);
-                return err;
-            }
-        } else if (st.mUseFenceSync && SyncFeatures::getInstance().useFenceSync()) {
-            EGLSyncKHR fence = mEglSlots[st.mCurrentTexture].mEglFence;
-            if (fence != EGL_NO_SYNC_KHR) {
-                // There is already a fence for the current slot.  We need to
-                // wait on that before replacing it with another fence to
-                // ensure that all outstanding buffer accesses have completed
-                // before the producer accesses it.
-                EGLint result = eglClientWaitSyncKHR(dpy, fence, 0, 1000000000);
-                if (result == EGL_FALSE) {
-                    EGC_LOGE(
-                            "syncForReleaseLocked: error waiting for previous "
-                            "fence: %#x",
-                            eglGetError());
-                    return UNKNOWN_ERROR;
-                } else if (result == EGL_TIMEOUT_EXPIRED_KHR) {
-                    EGC_LOGE(
-                            "syncForReleaseLocked: timeout waiting for previous "
-                            "fence");
-                    return TIMED_OUT;
-                }
-                eglDestroySyncKHR(dpy, fence);
-            }
-
-            // Create a fence for the outstanding accesses in the current
-            // OpenGL ES context.
-            fence = eglCreateSyncKHR(dpy, EGL_SYNC_FENCE_KHR, nullptr);
-            if (fence == EGL_NO_SYNC_KHR) {
-                EGC_LOGE("syncForReleaseLocked: error creating fence: %#x", eglGetError());
-                return UNKNOWN_ERROR;
-            }
-            glFlush();
-            mEglSlots[st.mCurrentTexture].mEglFence = fence;
-        }
-    }
-
-    return OK;
-}
-
-status_t EGLConsumer::doGLFenceWaitLocked(SurfaceTexture& st) const {
-    EGLDisplay dpy = eglGetCurrentDisplay();
-    EGLContext ctx = eglGetCurrentContext();
-
-    if (mEglDisplay != dpy || mEglDisplay == EGL_NO_DISPLAY) {
-        EGC_LOGE("doGLFenceWait: invalid current EGLDisplay");
-        return INVALID_OPERATION;
-    }
-
-    if (mEglContext != ctx || mEglContext == EGL_NO_CONTEXT) {
-        EGC_LOGE("doGLFenceWait: invalid current EGLContext");
-        return INVALID_OPERATION;
-    }
-
-    if (st.mCurrentFence->isValid()) {
-        if (SyncFeatures::getInstance().useWaitSync() &&
-            SyncFeatures::getInstance().useNativeFenceSync()) {
-            // Create an EGLSyncKHR from the current fence.
-            int fenceFd = st.mCurrentFence->dup();
-            if (fenceFd == -1) {
-                EGC_LOGE("doGLFenceWait: error dup'ing fence fd: %d", errno);
-                return -errno;
-            }
-            EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
-            EGLSyncKHR sync = eglCreateSyncKHR(dpy, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
-            if (sync == EGL_NO_SYNC_KHR) {
-                close(fenceFd);
-                EGC_LOGE("doGLFenceWait: error creating EGL fence: %#x", eglGetError());
-                return UNKNOWN_ERROR;
-            }
-
-            // XXX: The spec draft is inconsistent as to whether this should
-            // return an EGLint or void.  Ignore the return value for now, as
-            // it's not strictly needed.
-            eglWaitSyncKHR(dpy, sync, 0);
-            EGLint eglErr = eglGetError();
-            eglDestroySyncKHR(dpy, sync);
-            if (eglErr != EGL_SUCCESS) {
-                EGC_LOGE("doGLFenceWait: error waiting for EGL fence: %#x", eglErr);
-                return UNKNOWN_ERROR;
-            }
-        } else {
-            status_t err = st.mCurrentFence->waitForever("EGLConsumer::doGLFenceWaitLocked");
-            if (err != NO_ERROR) {
-                EGC_LOGE("doGLFenceWait: error waiting for fence: %d", err);
-                return err;
-            }
-        }
-    }
-
-    return NO_ERROR;
-}
-
-void EGLConsumer::onFreeBufferLocked(int slotIndex) {
-    mEglSlots[slotIndex].mEglImage.clear();
-}
-
-void EGLConsumer::onAbandonLocked() {
-    mCurrentTextureImage.clear();
-}
-
-EGLConsumer::EglImage::EglImage(sp<GraphicBuffer> graphicBuffer)
-        : mGraphicBuffer(graphicBuffer), mEglImage(EGL_NO_IMAGE_KHR), mEglDisplay(EGL_NO_DISPLAY) {}
-
-EGLConsumer::EglImage::~EglImage() {
-    if (mEglImage != EGL_NO_IMAGE_KHR) {
-        if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
-            ALOGE("~EglImage: eglDestroyImageKHR failed");
-        }
-        eglTerminate(mEglDisplay);
-    }
-}
-
-status_t EGLConsumer::EglImage::createIfNeeded(EGLDisplay eglDisplay, bool forceCreation) {
-    // If there's an image and it's no longer valid, destroy it.
-    bool haveImage = mEglImage != EGL_NO_IMAGE_KHR;
-    bool displayInvalid = mEglDisplay != eglDisplay;
-    if (haveImage && (displayInvalid || forceCreation)) {
-        if (!eglDestroyImageKHR(mEglDisplay, mEglImage)) {
-            ALOGE("createIfNeeded: eglDestroyImageKHR failed");
-        }
-        eglTerminate(mEglDisplay);
-        mEglImage = EGL_NO_IMAGE_KHR;
-        mEglDisplay = EGL_NO_DISPLAY;
-    }
-
-    // If there's no image, create one.
-    if (mEglImage == EGL_NO_IMAGE_KHR) {
-        mEglDisplay = eglDisplay;
-        mEglImage = createImage(mEglDisplay, mGraphicBuffer);
-    }
-
-    // Fail if we can't create a valid image.
-    if (mEglImage == EGL_NO_IMAGE_KHR) {
-        mEglDisplay = EGL_NO_DISPLAY;
-        const sp<GraphicBuffer>& buffer = mGraphicBuffer;
-        ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
-              buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(),
-              buffer->getPixelFormat());
-        return UNKNOWN_ERROR;
-    }
-
-    return OK;
-}
-
-void EGLConsumer::EglImage::bindToTextureTarget(uint32_t texTarget) {
-    glEGLImageTargetTexture2DOES(texTarget, static_cast<GLeglImageOES>(mEglImage));
-}
-
-EGLImageKHR EGLConsumer::EglImage::createImage(EGLDisplay dpy,
-                                               const sp<GraphicBuffer>& graphicBuffer) {
-    EGLClientBuffer cbuf = static_cast<EGLClientBuffer>(graphicBuffer->getNativeBuffer());
-    const bool createProtectedImage =
-            (graphicBuffer->getUsage() & GRALLOC_USAGE_PROTECTED) && hasEglProtectedContent();
-    EGLint attrs[] = {
-            EGL_IMAGE_PRESERVED_KHR,
-            EGL_TRUE,
-            createProtectedImage ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
-            createProtectedImage ? EGL_TRUE : EGL_NONE,
-            EGL_NONE,
-    };
-    eglInitialize(dpy, nullptr, nullptr);
-    EGLImageKHR image =
-            eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, cbuf, attrs);
-    if (image == EGL_NO_IMAGE_KHR) {
-        EGLint error = eglGetError();
-        ALOGE("error creating EGLImage: %#x", error);
-        eglTerminate(dpy);
-    }
-    return image;
-}
-
-};  // namespace android
diff --git a/libs/hwui/surfacetexture/EGLConsumer.h b/libs/hwui/surfacetexture/EGLConsumer.h
deleted file mode 100644
index eccb082..0000000
--- a/libs/hwui/surfacetexture/EGLConsumer.h
+++ /dev/null
@@ -1,311 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#pragma once
-
-#include <EGL/egl.h>
-#include <EGL/eglext.h>
-
-#include <gui/BufferQueueDefs.h>
-
-#include <ui/FenceTime.h>
-#include <ui/GraphicBuffer.h>
-#include <utils/Mutex.h>
-
-namespace android {
-
-class SurfaceTexture;
-
-/*
- * EGLConsumer implements the parts of SurfaceTexture that deal with
- * textures attached to an GL context.
- */
-class EGLConsumer {
-public:
-    EGLConsumer();
-
-    /**
-     * updateTexImage acquires the most recently queued buffer, and sets the
-     * image contents of the target texture to it.
-     *
-     * This call may only be made while the OpenGL ES context to which the
-     * target texture belongs is bound to the calling thread.
-     *
-     * This calls doGLFenceWait to ensure proper synchronization.
-     */
-    status_t updateTexImage(SurfaceTexture& st);
-
-    /*
-     * releaseTexImage releases the texture acquired in updateTexImage().
-     * This is intended to be used in single buffer mode.
-     *
-     * This call may only be made while the OpenGL ES context to which the
-     * target texture belongs is bound to the calling thread.
-     */
-    status_t releaseTexImage(SurfaceTexture& st);
-
-    /**
-     * detachFromContext detaches the EGLConsumer from the calling thread's
-     * current OpenGL ES context.  This context must be the same as the context
-     * that was current for previous calls to updateTexImage.
-     *
-     * Detaching a EGLConsumer from an OpenGL ES context will result in the
-     * deletion of the OpenGL ES texture object into which the images were being
-     * streamed.  After a EGLConsumer has been detached from the OpenGL ES
-     * context calls to updateTexImage will fail returning INVALID_OPERATION
-     * until the EGLConsumer is attached to a new OpenGL ES context using the
-     * attachToContext method.
-     */
-    status_t detachFromContext(SurfaceTexture& st);
-
-    /**
-     * attachToContext attaches a EGLConsumer that is currently in the
-     * 'detached' state to the current OpenGL ES context.  A EGLConsumer is
-     * in the 'detached' state iff detachFromContext has successfully been
-     * called and no calls to attachToContext have succeeded since the last
-     * detachFromContext call.  Calls to attachToContext made on a
-     * EGLConsumer that is not in the 'detached' state will result in an
-     * INVALID_OPERATION error.
-     *
-     * The tex argument specifies the OpenGL ES texture object name in the
-     * new context into which the image contents will be streamed.  A successful
-     * call to attachToContext will result in this texture object being bound to
-     * the texture target and populated with the image contents that were
-     * current at the time of the last call to detachFromContext.
-     */
-    status_t attachToContext(uint32_t tex, SurfaceTexture& st);
-
-    /**
-     * onAcquireBufferLocked amends the ConsumerBase method to update the
-     * mEglSlots array in addition to the ConsumerBase behavior.
-     */
-    void onAcquireBufferLocked(BufferItem* item, SurfaceTexture& st);
-
-    /**
-     * onReleaseBufferLocked amends the ConsumerBase method to update the
-     * mEglSlots array in addition to the ConsumerBase.
-     */
-    void onReleaseBufferLocked(int slot);
-
-    /**
-     * onFreeBufferLocked frees up the given buffer slot. If the slot has been
-     * initialized this will release the reference to the GraphicBuffer in that
-     * slot and destroy the EGLImage in that slot.  Otherwise it has no effect.
-     */
-    void onFreeBufferLocked(int slotIndex);
-
-    /**
-     * onAbandonLocked amends the ConsumerBase method to clear
-     * mCurrentTextureImage in addition to the ConsumerBase behavior.
-     */
-    void onAbandonLocked();
-
-protected:
-    struct PendingRelease {
-        PendingRelease()
-                : isPending(false)
-                , currentTexture(-1)
-                , graphicBuffer()
-                , display(nullptr)
-                , fence(nullptr) {}
-
-        bool isPending;
-        int currentTexture;
-        sp<GraphicBuffer> graphicBuffer;
-        EGLDisplay display;
-        EGLSyncKHR fence;
-    };
-
-    /**
-     * This releases the buffer in the slot referenced by mCurrentTexture,
-     * then updates state to refer to the BufferItem, which must be a
-     * newly-acquired buffer. If pendingRelease is not null, the parameters
-     * which would have been passed to releaseBufferLocked upon the successful
-     * completion of the method will instead be returned to the caller, so that
-     * it may call releaseBufferLocked itself later.
-     */
-    status_t updateAndReleaseLocked(const BufferItem& item, PendingRelease* pendingRelease,
-                                    SurfaceTexture& st);
-
-    /**
-     * Binds mTexName and the current buffer to mTexTarget.  Uses
-     * mCurrentTexture if it's set, mCurrentTextureImage if not.  If the
-     * bind succeeds, this calls doGLFenceWait.
-     */
-    status_t bindTextureImageLocked(SurfaceTexture& st);
-
-    /**
-     * Gets the current EGLDisplay and EGLContext values, and compares them
-     * to mEglDisplay and mEglContext.  If the fields have been previously
-     * set, the values must match; if not, the fields are set to the current
-     * values.
-     * The contextCheck argument is used to ensure that a GL context is
-     * properly set; when set to false, the check is not performed.
-     */
-    status_t checkAndUpdateEglStateLocked(SurfaceTexture& st, bool contextCheck = false);
-
-    /**
-     * EglImage is a utility class for tracking and creating EGLImageKHRs. There
-     * is primarily just one image per slot, but there is also special cases:
-     *  - For releaseTexImage, we use a debug image (mReleasedTexImage)
-     *  - After freeBuffer, we must still keep the current image/buffer
-     * Reference counting EGLImages lets us handle all these cases easily while
-     * also only creating new EGLImages from buffers when required.
-     */
-    class EglImage : public LightRefBase<EglImage> {
-    public:
-        EglImage(sp<GraphicBuffer> graphicBuffer);
-
-        /**
-         * createIfNeeded creates an EGLImage if required (we haven't created
-         * one yet, or the EGLDisplay or crop-rect has changed).
-         */
-        status_t createIfNeeded(EGLDisplay display, bool forceCreate = false);
-
-        /**
-         * This calls glEGLImageTargetTexture2DOES to bind the image to the
-         * texture in the specified texture target.
-         */
-        void bindToTextureTarget(uint32_t texTarget);
-
-        const sp<GraphicBuffer>& graphicBuffer() { return mGraphicBuffer; }
-        const native_handle* graphicBufferHandle() {
-            return mGraphicBuffer == nullptr ? nullptr : mGraphicBuffer->handle;
-        }
-
-    private:
-        // Only allow instantiation using ref counting.
-        friend class LightRefBase<EglImage>;
-        virtual ~EglImage();
-
-        // createImage creates a new EGLImage from a GraphicBuffer.
-        EGLImageKHR createImage(EGLDisplay dpy, const sp<GraphicBuffer>& graphicBuffer);
-
-        // Disallow copying
-        EglImage(const EglImage& rhs);
-        void operator=(const EglImage& rhs);
-
-        // mGraphicBuffer is the buffer that was used to create this image.
-        sp<GraphicBuffer> mGraphicBuffer;
-
-        // mEglImage is the EGLImage created from mGraphicBuffer.
-        EGLImageKHR mEglImage;
-
-        // mEGLDisplay is the EGLDisplay that was used to create mEglImage.
-        EGLDisplay mEglDisplay;
-
-        // mCropRect is the crop rectangle passed to EGL when mEglImage
-        // was created.
-        Rect mCropRect;
-    };
-
-    /**
-     * doGLFenceWaitLocked inserts a wait command into the OpenGL ES command
-     * stream to ensure that it is safe for future OpenGL ES commands to
-     * access the current texture buffer.
-     */
-    status_t doGLFenceWaitLocked(SurfaceTexture& st) const;
-
-    /**
-     * syncForReleaseLocked performs the synchronization needed to release the
-     * current slot from an OpenGL ES context.  If needed it will set the
-     * current slot's fence to guard against a producer accessing the buffer
-     * before the outstanding accesses have completed.
-     */
-    status_t syncForReleaseLocked(EGLDisplay dpy, SurfaceTexture& st);
-
-    /**
-     * returns a graphic buffer used when the texture image has been released
-     */
-    static sp<GraphicBuffer> getDebugTexImageBuffer();
-
-    /**
-     * The default consumer usage flags that EGLConsumer always sets on its
-     * BufferQueue instance; these will be OR:d with any additional flags passed
-     * from the EGLConsumer user. In particular, EGLConsumer will always
-     * consume buffers as hardware textures.
-     */
-    static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
-
-    /**
-     * mCurrentTextureImage is the EglImage/buffer of the current texture. It's
-     * possible that this buffer is not associated with any buffer slot, so we
-     * must track it separately in order to support the getCurrentBuffer method.
-     */
-    sp<EglImage> mCurrentTextureImage;
-
-    /**
-     * EGLSlot contains the information and object references that
-     * EGLConsumer maintains about a BufferQueue buffer slot.
-     */
-    struct EglSlot {
-        EglSlot() : mEglFence(EGL_NO_SYNC_KHR) {}
-
-        /**
-         * mEglImage is the EGLImage created from mGraphicBuffer.
-         */
-        sp<EglImage> mEglImage;
-
-        /**
-         * mFence is the EGL sync object that must signal before the buffer
-         * associated with this buffer slot may be dequeued. It is initialized
-         * to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
-         * on a compile-time option) set to a new sync object in updateTexImage.
-         */
-        EGLSyncKHR mEglFence;
-    };
-
-    /**
-     * mEglDisplay is the EGLDisplay with which this EGLConsumer is currently
-     * associated.  It is intialized to EGL_NO_DISPLAY and gets set to the
-     * current display when updateTexImage is called for the first time and when
-     * attachToContext is called.
-     */
-    EGLDisplay mEglDisplay;
-
-    /**
-     * mEglContext is the OpenGL ES context with which this EGLConsumer is
-     * currently associated.  It is initialized to EGL_NO_CONTEXT and gets set
-     * to the current GL context when updateTexImage is called for the first
-     * time and when attachToContext is called.
-     */
-    EGLContext mEglContext;
-
-    /**
-     * mEGLSlots stores the buffers that have been allocated by the BufferQueue
-     * for each buffer slot.  It is initialized to null pointers, and gets
-     * filled in with the result of BufferQueue::acquire when the
-     * client dequeues a buffer from a
-     * slot that has not yet been used. The buffer allocated to a slot will also
-     * be replaced if the requested buffer usage or geometry differs from that
-     * of the buffer allocated to a slot.
-     */
-    EglSlot mEglSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
-
-    /**
-     * protects static initialization
-     */
-    static Mutex sStaticInitLock;
-
-    /**
-     * mReleasedTexImageBuffer is a dummy buffer used when in single buffer
-     * mode and releaseTexImage() has been called
-     */
-    static sp<GraphicBuffer> sReleasedTexImageBuffer;
-    sp<EglImage> mReleasedTexImage;
-};
-
-};  // namespace android
diff --git a/libs/hwui/surfacetexture/ImageConsumer.cpp b/libs/hwui/surfacetexture/ImageConsumer.cpp
deleted file mode 100644
index c86cd96..0000000
--- a/libs/hwui/surfacetexture/ImageConsumer.cpp
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#include "ImageConsumer.h"
-#include <gui/BufferQueue.h>
-#include "Properties.h"
-#include "SurfaceTexture.h"
-#include "renderstate/RenderState.h"
-#include "renderthread/EglManager.h"
-#include "renderthread/RenderThread.h"
-#include "renderthread/VulkanManager.h"
-
-// Macro for including the SurfaceTexture name in log messages
-#define IMG_LOGE(x, ...) ALOGE("[%s] " x, st.mName.string(), ##__VA_ARGS__)
-
-namespace android {
-
-void ImageConsumer::onFreeBufferLocked(int slotIndex) {
-    mImageSlots[slotIndex].mImage.reset();
-}
-
-void ImageConsumer::onAcquireBufferLocked(BufferItem* item) {
-    // If item->mGraphicBuffer is not null, this buffer has not been acquired
-    // before, so any prior SkImage is created with a stale buffer. This resets the stale SkImage.
-    if (item->mGraphicBuffer != nullptr) {
-        mImageSlots[item->mSlot].mImage.reset();
-    }
-}
-
-void ImageConsumer::onReleaseBufferLocked(int buf) {
-    mImageSlots[buf].mEglFence = EGL_NO_SYNC_KHR;
-}
-
-void ImageConsumer::ImageSlot::createIfNeeded(sp<GraphicBuffer> graphicBuffer) {
-    if (!mImage.get()) {
-        mImage = graphicBuffer.get()
-                         ? SkImage::MakeFromAHardwareBuffer(
-                                   reinterpret_cast<AHardwareBuffer*>(graphicBuffer.get()),
-                                   kPremul_SkAlphaType, SkColorSpace::MakeSRGB())
-                         : nullptr;
-    }
-}
-
-sk_sp<SkImage> ImageConsumer::dequeueImage(bool* queueEmpty, SurfaceTexture& st,
-                                           uirenderer::RenderState& renderState) {
-    BufferItem item;
-    status_t err;
-    err = st.acquireBufferLocked(&item, 0);
-    if (err != OK) {
-        if (err != BufferQueue::NO_BUFFER_AVAILABLE) {
-            IMG_LOGE("Error acquiring buffer: %s (%d)", strerror(err), err);
-        } else {
-            int slot = st.mCurrentTexture;
-            if (slot != BufferItem::INVALID_BUFFER_SLOT) {
-                *queueEmpty = true;
-                mImageSlots[slot].createIfNeeded(st.mSlots[slot].mGraphicBuffer);
-                return mImageSlots[slot].mImage;
-            }
-        }
-        return nullptr;
-    }
-
-    int slot = item.mSlot;
-    if (item.mFence->isValid()) {
-        // Wait on the producer fence for the buffer to be ready.
-        if (uirenderer::Properties::getRenderPipelineType() ==
-            uirenderer::RenderPipelineType::SkiaGL) {
-            err = renderState.getRenderThread().eglManager().fenceWait(item.mFence);
-        } else {
-            err = renderState.getRenderThread().vulkanManager().fenceWait(item.mFence);
-        }
-        if (err != OK) {
-            st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
-                                   EGL_NO_SYNC_KHR);
-            return nullptr;
-        }
-    }
-
-    // Release old buffer.
-    if (st.mCurrentTexture != BufferItem::INVALID_BUFFER_SLOT) {
-        // If needed, set the released slot's fence to guard against a producer accessing the
-        // buffer before the outstanding accesses have completed.
-        sp<Fence> releaseFence;
-        EGLDisplay display = EGL_NO_DISPLAY;
-        if (uirenderer::Properties::getRenderPipelineType() ==
-            uirenderer::RenderPipelineType::SkiaGL) {
-            auto& eglManager = renderState.getRenderThread().eglManager();
-            display = eglManager.eglDisplay();
-            err = eglManager.createReleaseFence(st.mUseFenceSync, &mImageSlots[slot].mEglFence,
-                                                releaseFence);
-        } else {
-            err = renderState.getRenderThread().vulkanManager().createReleaseFence(releaseFence);
-        }
-        if (OK != err) {
-            st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
-                                   EGL_NO_SYNC_KHR);
-            return nullptr;
-        }
-
-        if (releaseFence.get()) {
-            status_t err = st.addReleaseFenceLocked(
-                    st.mCurrentTexture, st.mSlots[st.mCurrentTexture].mGraphicBuffer, releaseFence);
-            if (err != OK) {
-                IMG_LOGE("dequeueImage: error adding release fence: %s (%d)", strerror(-err), err);
-                st.releaseBufferLocked(slot, st.mSlots[slot].mGraphicBuffer, EGL_NO_DISPLAY,
-                                       EGL_NO_SYNC_KHR);
-                return nullptr;
-            }
-        }
-
-        // Finally release the old buffer.
-        status_t status = st.releaseBufferLocked(
-                st.mCurrentTexture, st.mSlots[st.mCurrentTexture].mGraphicBuffer, display,
-                mImageSlots[st.mCurrentTexture].mEglFence);
-        if (status < NO_ERROR) {
-            IMG_LOGE("dequeueImage: failed to release buffer: %s (%d)", strerror(-status), status);
-            err = status;
-            // Keep going, with error raised.
-        }
-    }
-
-    // Update the state.
-    st.mCurrentTexture = slot;
-    st.mCurrentCrop = item.mCrop;
-    st.mCurrentTransform = item.mTransform;
-    st.mCurrentScalingMode = item.mScalingMode;
-    st.mCurrentTimestamp = item.mTimestamp;
-    st.mCurrentDataSpace = item.mDataSpace;
-    st.mCurrentFence = item.mFence;
-    st.mCurrentFenceTime = item.mFenceTime;
-    st.mCurrentFrameNumber = item.mFrameNumber;
-    st.computeCurrentTransformMatrixLocked();
-
-    *queueEmpty = false;
-    mImageSlots[slot].createIfNeeded(st.mSlots[slot].mGraphicBuffer);
-    return mImageSlots[slot].mImage;
-}
-
-} /* namespace android */
diff --git a/libs/hwui/surfacetexture/ImageConsumer.h b/libs/hwui/surfacetexture/ImageConsumer.h
deleted file mode 100644
index 31ee8db..0000000
--- a/libs/hwui/surfacetexture/ImageConsumer.h
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#pragma once
-
-#include <EGL/egl.h>
-#include <EGL/eglext.h>
-
-#include <gui/BufferQueueDefs.h>
-
-#include <SkImage.h>
-#include <cutils/compiler.h>
-#include <gui/BufferItem.h>
-#include <system/graphics.h>
-
-namespace android {
-
-namespace uirenderer {
-class RenderState;
-}
-
-class SurfaceTexture;
-
-/*
- * ImageConsumer implements the parts of SurfaceTexture that deal with
- * images consumed by HWUI view system.
- */
-class ImageConsumer {
-public:
-    sk_sp<SkImage> dequeueImage(bool* queueEmpty, SurfaceTexture& cb,
-                                uirenderer::RenderState& renderState);
-
-    /**
-     * onAcquireBufferLocked amends the ConsumerBase method to update the
-     * mImageSlots array in addition to the ConsumerBase behavior.
-     */
-    void onAcquireBufferLocked(BufferItem* item);
-
-    /**
-     * onReleaseBufferLocked amends the ConsumerBase method to update the
-     * mImageSlots array in addition to the ConsumerBase.
-     */
-    void onReleaseBufferLocked(int slot);
-
-    /**
-     * onFreeBufferLocked frees up the given buffer slot. If the slot has been
-     * initialized this will release the reference to the GraphicBuffer in that
-     * slot and destroy the SkImage in that slot. Otherwise it has no effect.
-     */
-    void onFreeBufferLocked(int slotIndex);
-
-private:
-    /**
-     * ImageSlot contains the information and object references that
-     * ImageConsumer maintains about a BufferQueue buffer slot.
-     */
-    struct ImageSlot {
-        ImageSlot() : mEglFence(EGL_NO_SYNC_KHR) {}
-
-        // mImage is the SkImage created from mGraphicBuffer.
-        sk_sp<SkImage> mImage;
-
-        /**
-         * mEglFence is the EGL sync object that must signal before the buffer
-         * associated with this buffer slot may be dequeued.
-         */
-        EGLSyncKHR mEglFence;
-
-        void createIfNeeded(sp<GraphicBuffer> graphicBuffer);
-    };
-
-    /**
-     * ImageConsumer stores the SkImages that have been allocated by the BufferQueue
-     * for each buffer slot.  It is initialized to null pointers, and gets
-     * filled in with the result of BufferQueue::acquire when the
-     * client dequeues a buffer from a
-     * slot that has not yet been used. The buffer allocated to a slot will also
-     * be replaced if the requested buffer usage or geometry differs from that
-     * of the buffer allocated to a slot.
-     */
-    ImageSlot mImageSlots[BufferQueueDefs::NUM_BUFFER_SLOTS];
-};
-
-}; /* namespace android */
diff --git a/libs/hwui/surfacetexture/SurfaceTexture.cpp b/libs/hwui/surfacetexture/SurfaceTexture.cpp
deleted file mode 100644
index 4bff715..0000000
--- a/libs/hwui/surfacetexture/SurfaceTexture.cpp
+++ /dev/null
@@ -1,496 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#include <cutils/compiler.h>
-#include <gui/BufferQueue.h>
-#include <math/mat4.h>
-#include <system/window.h>
-
-#include <utils/Trace.h>
-
-#include "Matrix.h"
-#include "SurfaceTexture.h"
-
-namespace android {
-
-// Macros for including the SurfaceTexture name in log messages
-#define SFT_LOGV(x, ...) ALOGV("[%s] " x, mName.string(), ##__VA_ARGS__)
-#define SFT_LOGD(x, ...) ALOGD("[%s] " x, mName.string(), ##__VA_ARGS__)
-#define SFT_LOGW(x, ...) ALOGW("[%s] " x, mName.string(), ##__VA_ARGS__)
-#define SFT_LOGE(x, ...) ALOGE("[%s] " x, mName.string(), ##__VA_ARGS__)
-
-static const mat4 mtxIdentity;
-
-SurfaceTexture::SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t tex,
-                               uint32_t texTarget, bool useFenceSync, bool isControlledByApp)
-        : ConsumerBase(bq, isControlledByApp)
-        , mCurrentCrop(Rect::EMPTY_RECT)
-        , mCurrentTransform(0)
-        , mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE)
-        , mCurrentFence(Fence::NO_FENCE)
-        , mCurrentTimestamp(0)
-        , mCurrentDataSpace(HAL_DATASPACE_UNKNOWN)
-        , mCurrentFrameNumber(0)
-        , mDefaultWidth(1)
-        , mDefaultHeight(1)
-        , mFilteringEnabled(true)
-        , mTexName(tex)
-        , mUseFenceSync(useFenceSync)
-        , mTexTarget(texTarget)
-        , mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
-        , mOpMode(OpMode::attachedToGL) {
-    SFT_LOGV("SurfaceTexture");
-
-    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
-
-    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
-}
-
-SurfaceTexture::SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t texTarget,
-                               bool useFenceSync, bool isControlledByApp)
-        : ConsumerBase(bq, isControlledByApp)
-        , mCurrentCrop(Rect::EMPTY_RECT)
-        , mCurrentTransform(0)
-        , mCurrentScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE)
-        , mCurrentFence(Fence::NO_FENCE)
-        , mCurrentTimestamp(0)
-        , mCurrentDataSpace(HAL_DATASPACE_UNKNOWN)
-        , mCurrentFrameNumber(0)
-        , mDefaultWidth(1)
-        , mDefaultHeight(1)
-        , mFilteringEnabled(true)
-        , mTexName(0)
-        , mUseFenceSync(useFenceSync)
-        , mTexTarget(texTarget)
-        , mCurrentTexture(BufferQueue::INVALID_BUFFER_SLOT)
-        , mOpMode(OpMode::detached) {
-    SFT_LOGV("SurfaceTexture");
-
-    memcpy(mCurrentTransformMatrix, mtxIdentity.asArray(), sizeof(mCurrentTransformMatrix));
-
-    mConsumer->setConsumerUsageBits(DEFAULT_USAGE_FLAGS);
-}
-
-status_t SurfaceTexture::setDefaultBufferSize(uint32_t w, uint32_t h) {
-    Mutex::Autolock lock(mMutex);
-    if (mAbandoned) {
-        SFT_LOGE("setDefaultBufferSize: SurfaceTexture is abandoned!");
-        return NO_INIT;
-    }
-    mDefaultWidth = w;
-    mDefaultHeight = h;
-    return mConsumer->setDefaultBufferSize(w, h);
-}
-
-status_t SurfaceTexture::updateTexImage() {
-    ATRACE_CALL();
-    SFT_LOGV("updateTexImage");
-    Mutex::Autolock lock(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("updateTexImage: SurfaceTexture is abandoned!");
-        return NO_INIT;
-    }
-
-    return mEGLConsumer.updateTexImage(*this);
-}
-
-status_t SurfaceTexture::releaseTexImage() {
-    // releaseTexImage can be invoked even when not attached to a GL context.
-    ATRACE_CALL();
-    SFT_LOGV("releaseTexImage");
-    Mutex::Autolock lock(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("releaseTexImage: SurfaceTexture is abandoned!");
-        return NO_INIT;
-    }
-
-    return mEGLConsumer.releaseTexImage(*this);
-}
-
-status_t SurfaceTexture::acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
-                                             uint64_t maxFrameNumber) {
-    status_t err = ConsumerBase::acquireBufferLocked(item, presentWhen, maxFrameNumber);
-    if (err != NO_ERROR) {
-        return err;
-    }
-
-    switch (mOpMode) {
-        case OpMode::attachedToView:
-            mImageConsumer.onAcquireBufferLocked(item);
-            break;
-        case OpMode::attachedToGL:
-            mEGLConsumer.onAcquireBufferLocked(item, *this);
-            break;
-        case OpMode::detached:
-            break;
-    }
-
-    return NO_ERROR;
-}
-
-status_t SurfaceTexture::releaseBufferLocked(int buf, sp<GraphicBuffer> graphicBuffer,
-                                             EGLDisplay display, EGLSyncKHR eglFence) {
-    // release the buffer if it hasn't already been discarded by the
-    // BufferQueue. This can happen, for example, when the producer of this
-    // buffer has reallocated the original buffer slot after this buffer
-    // was acquired.
-    status_t err = ConsumerBase::releaseBufferLocked(buf, graphicBuffer, display, eglFence);
-    // We could be releasing an EGL buffer, even if not currently attached to a GL context.
-    mImageConsumer.onReleaseBufferLocked(buf);
-    mEGLConsumer.onReleaseBufferLocked(buf);
-    return err;
-}
-
-status_t SurfaceTexture::detachFromContext() {
-    ATRACE_CALL();
-    SFT_LOGV("detachFromContext");
-    Mutex::Autolock lock(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("detachFromContext: abandoned SurfaceTexture");
-        return NO_INIT;
-    }
-
-    if (mOpMode != OpMode::attachedToGL) {
-        SFT_LOGE("detachFromContext: SurfaceTexture is not attached to a GL context");
-        return INVALID_OPERATION;
-    }
-
-    status_t err = mEGLConsumer.detachFromContext(*this);
-    if (err == OK) {
-        mOpMode = OpMode::detached;
-    }
-
-    return err;
-}
-
-status_t SurfaceTexture::attachToContext(uint32_t tex) {
-    ATRACE_CALL();
-    SFT_LOGV("attachToContext");
-    Mutex::Autolock lock(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("attachToContext: abandoned SurfaceTexture");
-        return NO_INIT;
-    }
-
-    if (mOpMode != OpMode::detached) {
-        SFT_LOGE(
-                "attachToContext: SurfaceTexture is already attached to a "
-                "context");
-        return INVALID_OPERATION;
-    }
-
-    if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
-        // release possible ImageConsumer cache
-        mImageConsumer.onFreeBufferLocked(mCurrentTexture);
-    }
-
-    return mEGLConsumer.attachToContext(tex, *this);
-}
-
-void SurfaceTexture::attachToView() {
-    ATRACE_CALL();
-    Mutex::Autolock _l(mMutex);
-    if (mAbandoned) {
-        SFT_LOGE("attachToView: abandoned SurfaceTexture");
-        return;
-    }
-    if (mOpMode == OpMode::detached) {
-        mOpMode = OpMode::attachedToView;
-
-        if (mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
-            // release possible EGLConsumer texture cache
-            mEGLConsumer.onFreeBufferLocked(mCurrentTexture);
-            mEGLConsumer.onAbandonLocked();
-        }
-    } else {
-        SFT_LOGE("attachToView: already attached");
-    }
-}
-
-void SurfaceTexture::detachFromView() {
-    ATRACE_CALL();
-    Mutex::Autolock _l(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("detachFromView: abandoned SurfaceTexture");
-        return;
-    }
-
-    if (mOpMode == OpMode::attachedToView) {
-        mOpMode = OpMode::detached;
-    } else {
-        SFT_LOGE("detachFromView: not attached to View");
-    }
-}
-
-uint32_t SurfaceTexture::getCurrentTextureTarget() const {
-    return mTexTarget;
-}
-
-void SurfaceTexture::getTransformMatrix(float mtx[16]) {
-    Mutex::Autolock lock(mMutex);
-    memcpy(mtx, mCurrentTransformMatrix, sizeof(mCurrentTransformMatrix));
-}
-
-void SurfaceTexture::setFilteringEnabled(bool enabled) {
-    Mutex::Autolock lock(mMutex);
-    if (mAbandoned) {
-        SFT_LOGE("setFilteringEnabled: SurfaceTexture is abandoned!");
-        return;
-    }
-    bool needsRecompute = mFilteringEnabled != enabled;
-    mFilteringEnabled = enabled;
-
-    if (needsRecompute && mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT) {
-        SFT_LOGD("setFilteringEnabled called with no current item");
-    }
-
-    if (needsRecompute && mCurrentTexture != BufferQueue::INVALID_BUFFER_SLOT) {
-        computeCurrentTransformMatrixLocked();
-    }
-}
-
-void SurfaceTexture::computeCurrentTransformMatrixLocked() {
-    SFT_LOGV("computeCurrentTransformMatrixLocked");
-    sp<GraphicBuffer> buf = (mCurrentTexture == BufferQueue::INVALID_BUFFER_SLOT)
-                                    ? nullptr
-                                    : mSlots[mCurrentTexture].mGraphicBuffer;
-    if (buf == nullptr) {
-        SFT_LOGD("computeCurrentTransformMatrixLocked: no current item");
-    }
-    computeTransformMatrix(mCurrentTransformMatrix, buf, mCurrentCrop, mCurrentTransform,
-                           mFilteringEnabled);
-}
-
-void SurfaceTexture::computeTransformMatrix(float outTransform[16], const sp<GraphicBuffer>& buf,
-                                            const Rect& cropRect, uint32_t transform,
-                                            bool filtering) {
-    // Transform matrices
-    static const mat4 mtxFlipH(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
-    static const mat4 mtxFlipV(1, 0, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1);
-    static const mat4 mtxRot90(0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1);
-
-    mat4 xform;
-    if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_H) {
-        xform *= mtxFlipH;
-    }
-    if (transform & NATIVE_WINDOW_TRANSFORM_FLIP_V) {
-        xform *= mtxFlipV;
-    }
-    if (transform & NATIVE_WINDOW_TRANSFORM_ROT_90) {
-        xform *= mtxRot90;
-    }
-
-    if (!cropRect.isEmpty() && buf.get()) {
-        float tx = 0.0f, ty = 0.0f, sx = 1.0f, sy = 1.0f;
-        float bufferWidth = buf->getWidth();
-        float bufferHeight = buf->getHeight();
-        float shrinkAmount = 0.0f;
-        if (filtering) {
-            // In order to prevent bilinear sampling beyond the edge of the
-            // crop rectangle we may need to shrink it by 2 texels in each
-            // dimension.  Normally this would just need to take 1/2 a texel
-            // off each end, but because the chroma channels of YUV420 images
-            // are subsampled we may need to shrink the crop region by a whole
-            // texel on each side.
-            switch (buf->getPixelFormat()) {
-                case PIXEL_FORMAT_RGBA_8888:
-                case PIXEL_FORMAT_RGBX_8888:
-                case PIXEL_FORMAT_RGBA_FP16:
-                case PIXEL_FORMAT_RGBA_1010102:
-                case PIXEL_FORMAT_RGB_888:
-                case PIXEL_FORMAT_RGB_565:
-                case PIXEL_FORMAT_BGRA_8888:
-                    // We know there's no subsampling of any channels, so we
-                    // only need to shrink by a half a pixel.
-                    shrinkAmount = 0.5;
-                    break;
-
-                default:
-                    // If we don't recognize the format, we must assume the
-                    // worst case (that we care about), which is YUV420.
-                    shrinkAmount = 1.0;
-                    break;
-            }
-        }
-
-        // Only shrink the dimensions that are not the size of the buffer.
-        if (cropRect.width() < bufferWidth) {
-            tx = (float(cropRect.left) + shrinkAmount) / bufferWidth;
-            sx = (float(cropRect.width()) - (2.0f * shrinkAmount)) / bufferWidth;
-        }
-        if (cropRect.height() < bufferHeight) {
-            ty = (float(bufferHeight - cropRect.bottom) + shrinkAmount) / bufferHeight;
-            sy = (float(cropRect.height()) - (2.0f * shrinkAmount)) / bufferHeight;
-        }
-
-        mat4 crop(sx, 0, 0, 0, 0, sy, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1);
-        xform = crop * xform;
-    }
-
-    // SurfaceFlinger expects the top of its window textures to be at a Y
-    // coordinate of 0, so SurfaceTexture must behave the same way.  We don't
-    // want to expose this to applications, however, so we must add an
-    // additional vertical flip to the transform after all the other transforms.
-    xform = mtxFlipV * xform;
-
-    memcpy(outTransform, xform.asArray(), sizeof(xform));
-}
-
-Rect SurfaceTexture::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
-    Rect outCrop = crop;
-
-    uint32_t newWidth = static_cast<uint32_t>(crop.width());
-    uint32_t newHeight = static_cast<uint32_t>(crop.height());
-
-    if (newWidth * bufferHeight > newHeight * bufferWidth) {
-        newWidth = newHeight * bufferWidth / bufferHeight;
-        ALOGV("too wide: newWidth = %d", newWidth);
-    } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
-        newHeight = newWidth * bufferHeight / bufferWidth;
-        ALOGV("too tall: newHeight = %d", newHeight);
-    }
-
-    uint32_t currentWidth = static_cast<uint32_t>(crop.width());
-    uint32_t currentHeight = static_cast<uint32_t>(crop.height());
-
-    // The crop is too wide
-    if (newWidth < currentWidth) {
-        uint32_t dw = currentWidth - newWidth;
-        auto halfdw = dw / 2;
-        outCrop.left += halfdw;
-        // Not halfdw because it would subtract 1 too few when dw is odd
-        outCrop.right -= (dw - halfdw);
-        // The crop is too tall
-    } else if (newHeight < currentHeight) {
-        uint32_t dh = currentHeight - newHeight;
-        auto halfdh = dh / 2;
-        outCrop.top += halfdh;
-        // Not halfdh because it would subtract 1 too few when dh is odd
-        outCrop.bottom -= (dh - halfdh);
-    }
-
-    ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]", outCrop.left, outCrop.top, outCrop.right,
-          outCrop.bottom);
-
-    return outCrop;
-}
-
-nsecs_t SurfaceTexture::getTimestamp() {
-    SFT_LOGV("getTimestamp");
-    Mutex::Autolock lock(mMutex);
-    return mCurrentTimestamp;
-}
-
-android_dataspace SurfaceTexture::getCurrentDataSpace() {
-    SFT_LOGV("getCurrentDataSpace");
-    Mutex::Autolock lock(mMutex);
-    return mCurrentDataSpace;
-}
-
-uint64_t SurfaceTexture::getFrameNumber() {
-    SFT_LOGV("getFrameNumber");
-    Mutex::Autolock lock(mMutex);
-    return mCurrentFrameNumber;
-}
-
-Rect SurfaceTexture::getCurrentCrop() const {
-    Mutex::Autolock lock(mMutex);
-    return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
-                   ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
-                   : mCurrentCrop;
-}
-
-uint32_t SurfaceTexture::getCurrentTransform() const {
-    Mutex::Autolock lock(mMutex);
-    return mCurrentTransform;
-}
-
-uint32_t SurfaceTexture::getCurrentScalingMode() const {
-    Mutex::Autolock lock(mMutex);
-    return mCurrentScalingMode;
-}
-
-sp<Fence> SurfaceTexture::getCurrentFence() const {
-    Mutex::Autolock lock(mMutex);
-    return mCurrentFence;
-}
-
-std::shared_ptr<FenceTime> SurfaceTexture::getCurrentFenceTime() const {
-    Mutex::Autolock lock(mMutex);
-    return mCurrentFenceTime;
-}
-
-void SurfaceTexture::freeBufferLocked(int slotIndex) {
-    SFT_LOGV("freeBufferLocked: slotIndex=%d", slotIndex);
-    if (slotIndex == mCurrentTexture) {
-        mCurrentTexture = BufferQueue::INVALID_BUFFER_SLOT;
-    }
-    // The slotIndex buffer could have EGL or SkImage cache, but there is no way to tell for sure.
-    // Buffers can be freed after SurfaceTexture has detached from GL context or View.
-    mImageConsumer.onFreeBufferLocked(slotIndex);
-    mEGLConsumer.onFreeBufferLocked(slotIndex);
-    ConsumerBase::freeBufferLocked(slotIndex);
-}
-
-void SurfaceTexture::abandonLocked() {
-    SFT_LOGV("abandonLocked");
-    mEGLConsumer.onAbandonLocked();
-    ConsumerBase::abandonLocked();
-}
-
-status_t SurfaceTexture::setConsumerUsageBits(uint64_t usage) {
-    return ConsumerBase::setConsumerUsageBits(usage | DEFAULT_USAGE_FLAGS);
-}
-
-void SurfaceTexture::dumpLocked(String8& result, const char* prefix) const {
-    result.appendFormat(
-            "%smTexName=%d mCurrentTexture=%d\n"
-            "%smCurrentCrop=[%d,%d,%d,%d] mCurrentTransform=%#x\n",
-            prefix, mTexName, mCurrentTexture, prefix, mCurrentCrop.left, mCurrentCrop.top,
-            mCurrentCrop.right, mCurrentCrop.bottom, mCurrentTransform);
-
-    ConsumerBase::dumpLocked(result, prefix);
-}
-
-sk_sp<SkImage> SurfaceTexture::dequeueImage(SkMatrix& transformMatrix, android_dataspace& dataSpace,
-                                            bool* queueEmpty,
-                                            uirenderer::RenderState& renderState) {
-    Mutex::Autolock _l(mMutex);
-
-    if (mAbandoned) {
-        SFT_LOGE("dequeueImage: SurfaceTexture is abandoned!");
-        return nullptr;
-    }
-
-    if (mOpMode != OpMode::attachedToView) {
-        SFT_LOGE("dequeueImage: SurfaceTexture is not attached to a View");
-        return nullptr;
-    }
-
-    auto image = mImageConsumer.dequeueImage(queueEmpty, *this, renderState);
-    if (image.get()) {
-        uirenderer::mat4(mCurrentTransformMatrix).copyTo(transformMatrix);
-        dataSpace = mCurrentDataSpace;
-    }
-    return image;
-}
-
-};  // namespace android
diff --git a/libs/hwui/surfacetexture/SurfaceTexture.h b/libs/hwui/surfacetexture/SurfaceTexture.h
deleted file mode 100644
index db392a9..0000000
--- a/libs/hwui/surfacetexture/SurfaceTexture.h
+++ /dev/null
@@ -1,452 +0,0 @@
-/*
- * Copyright (C) 2018 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.
- */
-
-#pragma once
-
-#include <gui/BufferQueueDefs.h>
-#include <gui/ConsumerBase.h>
-
-#include <ui/FenceTime.h>
-#include <ui/GraphicBuffer.h>
-
-#include <utils/Mutex.h>
-#include <utils/String8.h>
-
-#include "EGLConsumer.h"
-#include "ImageConsumer.h"
-
-namespace android {
-
-namespace uirenderer {
-class RenderState;
-}
-
-/*
- * SurfaceTexture consumes buffers of graphics data from a BufferQueue,
- * and makes them available to HWUI render thread as a SkImage and to
- * an application GL render thread as an OpenGL texture.
- *
- * When attached to an application GL render thread, a typical usage
- * pattern is to set up the SurfaceTexture with the
- * desired options, and call updateTexImage() when a new frame is desired.
- * If a new frame is available, the texture will be updated.  If not,
- * the previous contents are retained.
- *
- * When attached to a HWUI render thread, the TextureView implementation
- * calls dequeueImage, which either pulls a new SkImage or returns the
- * last cached SkImage if BufferQueue is empty.
- * When attached to HWUI render thread, SurfaceTexture is compatible to
- * both Vulkan and GL drawing pipelines.
- */
-class ANDROID_API SurfaceTexture : public ConsumerBase {
-public:
-    enum { TEXTURE_EXTERNAL = 0x8D65 };  // GL_TEXTURE_EXTERNAL_OES
-    typedef ConsumerBase::FrameAvailableListener FrameAvailableListener;
-
-    /**
-     * SurfaceTexture constructs a new SurfaceTexture object. If the constructor with
-     * the tex parameter is used, tex indicates the name of the OpenGL ES
-     * texture to which images are to be streamed. texTarget specifies the
-     * OpenGL ES texture target to which the texture will be bound in
-     * updateTexImage. useFenceSync specifies whether fences should be used to
-     * synchronize access to buffers if that behavior is enabled at
-     * compile-time.
-     *
-     * A SurfaceTexture may be detached from one OpenGL ES context and then
-     * attached to a different context using the detachFromContext and
-     * attachToContext methods, respectively. The intention of these methods is
-     * purely to allow a SurfaceTexture to be transferred from one consumer
-     * context to another. If such a transfer is not needed there is no
-     * requirement that either of these methods be called.
-     *
-     * If the constructor with the tex parameter is used, the SurfaceTexture is
-     * created in a state where it is considered attached to an OpenGL ES
-     * context for the purposes of the attachToContext and detachFromContext
-     * methods. However, despite being considered "attached" to a context, the
-     * specific OpenGL ES context doesn't get latched until the first call to
-     * updateTexImage. After that point, all calls to updateTexImage must be
-     * made with the same OpenGL ES context current.
-     *
-     * If the constructor without the tex parameter is used, the SurfaceTexture is
-     * created in a detached state, and attachToContext must be called before
-     * calls to updateTexImage.
-     */
-    SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t tex, uint32_t texureTarget,
-                   bool useFenceSync, bool isControlledByApp);
-
-    SurfaceTexture(const sp<IGraphicBufferConsumer>& bq, uint32_t texureTarget, bool useFenceSync,
-                   bool isControlledByApp);
-
-    /**
-     * updateTexImage acquires the most recently queued buffer, and sets the
-     * image contents of the target texture to it.
-     *
-     * This call may only be made while the OpenGL ES context to which the
-     * target texture belongs is bound to the calling thread.
-     *
-     * This calls doGLFenceWait to ensure proper synchronization.
-     */
-    status_t updateTexImage();
-
-    /**
-     * releaseTexImage releases the texture acquired in updateTexImage().
-     * This is intended to be used in single buffer mode.
-     *
-     * This call may only be made while the OpenGL ES context to which the
-     * target texture belongs is bound to the calling thread.
-     */
-    status_t releaseTexImage();
-
-    /**
-     * getTransformMatrix retrieves the 4x4 texture coordinate transform matrix
-     * associated with the texture image set by the most recent call to
-     * updateTexImage.
-     *
-     * This transform matrix maps 2D homogeneous texture coordinates of the form
-     * (s, t, 0, 1) with s and t in the inclusive range [0, 1] to the texture
-     * coordinate that should be used to sample that location from the texture.
-     * Sampling the texture outside of the range of this transform is undefined.
-     *
-     * This transform is necessary to compensate for transforms that the stream
-     * content producer may implicitly apply to the content. By forcing users of
-     * a SurfaceTexture to apply this transform we avoid performing an extra
-     * copy of the data that would be needed to hide the transform from the
-     * user.
-     *
-     * The matrix is stored in column-major order so that it may be passed
-     * directly to OpenGL ES via the glLoadMatrixf or glUniformMatrix4fv
-     * functions.
-     */
-    void getTransformMatrix(float mtx[16]);
-
-    /**
-     * Computes the transform matrix documented by getTransformMatrix
-     * from the BufferItem sub parts.
-     */
-    static void computeTransformMatrix(float outTransform[16], const sp<GraphicBuffer>& buf,
-                                       const Rect& cropRect, uint32_t transform, bool filtering);
-
-    /**
-     * Scale the crop down horizontally or vertically such that it has the
-     * same aspect ratio as the buffer does.
-     */
-    static Rect scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight);
-
-    /**
-     * getTimestamp retrieves the timestamp associated with the texture image
-     * set by the most recent call to updateTexImage.
-     *
-     * The timestamp is in nanoseconds, and is monotonically increasing. Its
-     * other semantics (zero point, etc) are source-dependent and should be
-     * documented by the source.
-     */
-    int64_t getTimestamp();
-
-    /**
-     * getDataSpace retrieves the DataSpace associated with the texture image
-     * set by the most recent call to updateTexImage.
-     */
-    android_dataspace getCurrentDataSpace();
-
-    /**
-     * getFrameNumber retrieves the frame number associated with the texture
-     * image set by the most recent call to updateTexImage.
-     *
-     * The frame number is an incrementing counter set to 0 at the creation of
-     * the BufferQueue associated with this consumer.
-     */
-    uint64_t getFrameNumber();
-
-    /**
-     * setDefaultBufferSize is used to set the size of buffers returned by
-     * requestBuffers when a with and height of zero is requested.
-     * A call to setDefaultBufferSize() may trigger requestBuffers() to
-     * be called from the client.
-     * The width and height parameters must be no greater than the minimum of
-     * GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
-     * An error due to invalid dimensions might not be reported until
-     * updateTexImage() is called.
-     */
-    status_t setDefaultBufferSize(uint32_t width, uint32_t height);
-
-    /**
-     * setFilteringEnabled sets whether the transform matrix should be computed
-     * for use with bilinear filtering.
-     */
-    void setFilteringEnabled(bool enabled);
-
-    /**
-     * getCurrentTextureTarget returns the texture target of the current
-     * texture as returned by updateTexImage().
-     */
-    uint32_t getCurrentTextureTarget() const;
-
-    /**
-     * getCurrentCrop returns the cropping rectangle of the current buffer.
-     */
-    Rect getCurrentCrop() const;
-
-    /**
-     * getCurrentTransform returns the transform of the current buffer.
-     */
-    uint32_t getCurrentTransform() const;
-
-    /**
-     * getCurrentScalingMode returns the scaling mode of the current buffer.
-     */
-    uint32_t getCurrentScalingMode() const;
-
-    /**
-     * getCurrentFence returns the fence indicating when the current buffer is
-     * ready to be read from.
-     */
-    sp<Fence> getCurrentFence() const;
-
-    /**
-     * getCurrentFence returns the FenceTime indicating when the current
-     * buffer is ready to be read from.
-     */
-    std::shared_ptr<FenceTime> getCurrentFenceTime() const;
-
-    /**
-     * setConsumerUsageBits overrides the ConsumerBase method to OR
-     * DEFAULT_USAGE_FLAGS to usage.
-     */
-    status_t setConsumerUsageBits(uint64_t usage);
-
-    /**
-     * detachFromContext detaches the SurfaceTexture from the calling thread's
-     * current OpenGL ES context.  This context must be the same as the context
-     * that was current for previous calls to updateTexImage.
-     *
-     * Detaching a SurfaceTexture from an OpenGL ES context will result in the
-     * deletion of the OpenGL ES texture object into which the images were being
-     * streamed.  After a SurfaceTexture has been detached from the OpenGL ES
-     * context calls to updateTexImage will fail returning INVALID_OPERATION
-     * until the SurfaceTexture is attached to a new OpenGL ES context using the
-     * attachToContext method.
-     */
-    status_t detachFromContext();
-
-    /**
-     * attachToContext attaches a SurfaceTexture that is currently in the
-     * 'detached' state to the current OpenGL ES context.  A SurfaceTexture is
-     * in the 'detached' state iff detachFromContext has successfully been
-     * called and no calls to attachToContext have succeeded since the last
-     * detachFromContext call.  Calls to attachToContext made on a
-     * SurfaceTexture that is not in the 'detached' state will result in an
-     * INVALID_OPERATION error.
-     *
-     * The tex argument specifies the OpenGL ES texture object name in the
-     * new context into which the image contents will be streamed.  A successful
-     * call to attachToContext will result in this texture object being bound to
-     * the texture target and populated with the image contents that were
-     * current at the time of the last call to detachFromContext.
-     */
-    status_t attachToContext(uint32_t tex);
-
-    sk_sp<SkImage> dequeueImage(SkMatrix& transformMatrix, android_dataspace& dataSpace,
-                                bool* queueEmpty, uirenderer::RenderState& renderState);
-
-    /**
-     * attachToView attaches a SurfaceTexture that is currently in the
-     * 'detached' state to HWUI View system.
-     */
-    void attachToView();
-
-    /**
-     * detachFromView detaches a SurfaceTexture from HWUI View system.
-     */
-    void detachFromView();
-
-protected:
-    /**
-     * abandonLocked overrides the ConsumerBase method to clear
-     * mCurrentTextureImage in addition to the ConsumerBase behavior.
-     */
-    virtual void abandonLocked();
-
-    /**
-     * dumpLocked overrides the ConsumerBase method to dump SurfaceTexture-
-     * specific info in addition to the ConsumerBase behavior.
-     */
-    virtual void dumpLocked(String8& result, const char* prefix) const override;
-
-    /**
-     * acquireBufferLocked overrides the ConsumerBase method to update the
-     * mEglSlots array in addition to the ConsumerBase behavior.
-     */
-    virtual status_t acquireBufferLocked(BufferItem* item, nsecs_t presentWhen,
-                                         uint64_t maxFrameNumber = 0) override;
-
-    /**
-     * releaseBufferLocked overrides the ConsumerBase method to update the
-     * mEglSlots array in addition to the ConsumerBase.
-     */
-    virtual status_t releaseBufferLocked(int slot, const sp<GraphicBuffer> graphicBuffer,
-                                         EGLDisplay display, EGLSyncKHR eglFence) override;
-
-    /**
-     * freeBufferLocked frees up the given buffer slot. If the slot has been
-     * initialized this will release the reference to the GraphicBuffer in that
-     * slot and destroy the EGLImage in that slot.  Otherwise it has no effect.
-     *
-     * This method must be called with mMutex locked.
-     */
-    virtual void freeBufferLocked(int slotIndex);
-
-    /**
-     * computeCurrentTransformMatrixLocked computes the transform matrix for the
-     * current texture.  It uses mCurrentTransform and the current GraphicBuffer
-     * to compute this matrix and stores it in mCurrentTransformMatrix.
-     * mCurrentTextureImage must not be NULL.
-     */
-    void computeCurrentTransformMatrixLocked();
-
-    /**
-     * The default consumer usage flags that SurfaceTexture always sets on its
-     * BufferQueue instance; these will be OR:d with any additional flags passed
-     * from the SurfaceTexture user. In particular, SurfaceTexture will always
-     * consume buffers as hardware textures.
-     */
-    static const uint64_t DEFAULT_USAGE_FLAGS = GraphicBuffer::USAGE_HW_TEXTURE;
-
-    /**
-     * mCurrentCrop is the crop rectangle that applies to the current texture.
-     * It gets set each time updateTexImage is called.
-     */
-    Rect mCurrentCrop;
-
-    /**
-     * mCurrentTransform is the transform identifier for the current texture. It
-     * gets set each time updateTexImage is called.
-     */
-    uint32_t mCurrentTransform;
-
-    /**
-     * mCurrentScalingMode is the scaling mode for the current texture. It gets
-     * set each time updateTexImage is called.
-     */
-    uint32_t mCurrentScalingMode;
-
-    /**
-     * mCurrentFence is the fence received from BufferQueue in updateTexImage.
-     */
-    sp<Fence> mCurrentFence;
-
-    /**
-     * The FenceTime wrapper around mCurrentFence.
-     */
-    std::shared_ptr<FenceTime> mCurrentFenceTime{FenceTime::NO_FENCE};
-
-    /**
-     * mCurrentTransformMatrix is the transform matrix for the current texture.
-     * It gets computed by computeTransformMatrix each time updateTexImage is
-     * called.
-     */
-    float mCurrentTransformMatrix[16];
-
-    /**
-     * mCurrentTimestamp is the timestamp for the current texture. It
-     * gets set each time updateTexImage is called.
-     */
-    int64_t mCurrentTimestamp;
-
-    /**
-     * mCurrentDataSpace is the dataspace for the current texture. It
-     * gets set each time updateTexImage is called.
-     */
-    android_dataspace mCurrentDataSpace;
-
-    /**
-     * mCurrentFrameNumber is the frame counter for the current texture.
-     * It gets set each time updateTexImage is called.
-     */
-    uint64_t mCurrentFrameNumber;
-
-    uint32_t mDefaultWidth, mDefaultHeight;
-
-    /**
-     * mFilteringEnabled indicates whether the transform matrix is computed for
-     * use with bilinear filtering. It defaults to true and is changed by
-     * setFilteringEnabled().
-     */
-    bool mFilteringEnabled;
-
-    /**
-     * mTexName is the name of the OpenGL texture to which streamed images will
-     * be bound when updateTexImage is called. It is set at construction time
-     * and can be changed with a call to attachToContext.
-     */
-    uint32_t mTexName;
-
-    /**
-     * mUseFenceSync indicates whether creation of the EGL_KHR_fence_sync
-     * extension should be used to prevent buffers from being dequeued before
-     * it's safe for them to be written. It gets set at construction time and
-     * never changes.
-     */
-    const bool mUseFenceSync;
-
-    /**
-     * mTexTarget is the GL texture target with which the GL texture object is
-     * associated.  It is set in the constructor and never changed.  It is
-     * almost always GL_TEXTURE_EXTERNAL_OES except for one use case in Android
-     * Browser.  In that case it is set to GL_TEXTURE_2D to allow
-     * glCopyTexSubImage to read from the texture.  This is a hack to work
-     * around a GL driver limitation on the number of FBO attachments, which the
-     * browser's tile cache exceeds.
-     */
-    const uint32_t mTexTarget;
-
-    /**
-     * mCurrentTexture is the buffer slot index of the buffer that is currently
-     * bound to the OpenGL texture. It is initialized to INVALID_BUFFER_SLOT,
-     * indicating that no buffer slot is currently bound to the texture. Note,
-     * however, that a value of INVALID_BUFFER_SLOT does not necessarily mean
-     * that no buffer is bound to the texture. A call to setBufferCount will
-     * reset mCurrentTexture to INVALID_BUFFER_SLOT.
-     */
-    int mCurrentTexture;
-
-    enum class OpMode { detached, attachedToView, attachedToGL };
-    /**
-     * mOpMode indicates whether the SurfaceTexture is currently attached to
-     * an OpenGL ES context or the HWUI view system.  For legacy reasons, this is initialized to,
-     * "attachedToGL" indicating that the SurfaceTexture is considered to be attached to
-     * whatever GL context is current at the time of the first updateTexImage call.
-     * It is set to "detached" by detachFromContext, and then set to "attachedToGL" again by
-     * attachToContext.
-     * attachToView/detachFromView are used to attach/detach from HWUI view system.
-     */
-    OpMode mOpMode;
-
-    /**
-     * mEGLConsumer has SurfaceTexture logic used when attached to GL context.
-     */
-    EGLConsumer mEGLConsumer;
-
-    /**
-     * mImageConsumer has SurfaceTexture logic used when attached to HWUI view system.
-     */
-    ImageConsumer mImageConsumer;
-
-    friend class ImageConsumer;
-    friend class EGLConsumer;
-};
-
-// ----------------------------------------------------------------------------
-};  // namespace android
diff --git a/libs/hwui/tests/common/LeakChecker.cpp b/libs/hwui/tests/common/LeakChecker.cpp
index d2d37dc..5b36154 100644
--- a/libs/hwui/tests/common/LeakChecker.cpp
+++ b/libs/hwui/tests/common/LeakChecker.cpp
@@ -16,6 +16,7 @@
 
 #include "LeakChecker.h"
 
+#include "Caches.h"
 #include "TestUtils.h"
 
 #include <memunreachable/memunreachable.h>
@@ -70,6 +71,9 @@
     // thread-local caches so some leaks will not be properly tagged as leaks
     UnreachableMemoryInfo rtMemInfo;
     TestUtils::runOnRenderThread([&rtMemInfo](renderthread::RenderThread& thread) {
+        if (Caches::hasInstance()) {
+            Caches::getInstance().tasks.stop();
+        }
         // Check for leaks
         if (!GetUnreachableMemory(rtMemInfo)) {
             cerr << "Failed to get unreachable memory!" << endl;
diff --git a/libs/hwui/tests/common/TestUtils.cpp b/libs/hwui/tests/common/TestUtils.cpp
index 66b9b85..6958634 100644
--- a/libs/hwui/tests/common/TestUtils.cpp
+++ b/libs/hwui/tests/common/TestUtils.cpp
@@ -67,14 +67,16 @@
         renderthread::RenderThread& renderThread, uint32_t width, uint32_t height,
         const SkMatrix& transform) {
     sp<DeferredLayerUpdater> layerUpdater = createTextureLayerUpdater(renderThread);
-    layerUpdater->backingLayer()->getTransform() = transform;
+    layerUpdater->backingLayer()->getTransform().load(transform);
     layerUpdater->setSize(width, height);
     layerUpdater->setTransform(&transform);
 
     // updateLayer so it's ready to draw
-    SkMatrix identity;
-    identity.setIdentity();
-    layerUpdater->updateLayer(true, identity, HAL_DATASPACE_UNKNOWN, nullptr);
+    layerUpdater->updateLayer(true, Matrix4::identity().data, HAL_DATASPACE_UNKNOWN);
+    if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
+        static_cast<GlLayer*>(layerUpdater->backingLayer())
+                ->setRenderTarget(GL_TEXTURE_EXTERNAL_OES);
+    }
     return layerUpdater;
 }
 
@@ -115,6 +117,7 @@
     if (Properties::getRenderPipelineType() == RenderPipelineType::SkiaVulkan) {
         renderThread.vulkanManager().destroy();
     } else {
+        renderThread.renderState().flush(Caches::FlushMode::Full);
         renderThread.destroyGlContext();
     }
 }
diff --git a/libs/hwui/tests/common/TestUtils.h b/libs/hwui/tests/common/TestUtils.h
index 0e6582c..743f809 100644
--- a/libs/hwui/tests/common/TestUtils.h
+++ b/libs/hwui/tests/common/TestUtils.h
@@ -18,6 +18,7 @@
 
 #include <DeviceInfo.h>
 #include <DisplayList.h>
+#include <GlLayer.h>
 #include <Matrix.h>
 #include <Properties.h>
 #include <Rect.h>
diff --git a/libs/hwui/tests/unit/DeferredLayerUpdaterTests.cpp b/libs/hwui/tests/unit/DeferredLayerUpdaterTests.cpp
index 6c8775b..f29830f 100644
--- a/libs/hwui/tests/unit/DeferredLayerUpdaterTests.cpp
+++ b/libs/hwui/tests/unit/DeferredLayerUpdaterTests.cpp
@@ -15,13 +15,12 @@
  */
 
 #include "DeferredLayerUpdater.h"
+#include "GlLayer.h"
 #include "Properties.h"
 
 #include "tests/common/TestUtils.h"
 
 #include <gtest/gtest.h>
-#include <SkBitmap.h>
-#include <SkImage.h>
 
 using namespace android;
 using namespace android::uirenderer;
@@ -32,6 +31,10 @@
     layerUpdater->setBlend(true);
 
     // updates are deferred so the backing layer should still be in its default state
+    if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
+        GlLayer* glLayer = static_cast<GlLayer*>(layerUpdater->backingLayer());
+        EXPECT_EQ((uint32_t)GL_NONE, glLayer->getRenderTarget());
+    }
     EXPECT_EQ(0u, layerUpdater->backingLayer()->getWidth());
     EXPECT_EQ(0u, layerUpdater->backingLayer()->getHeight());
     EXPECT_FALSE(layerUpdater->backingLayer()->getForceFilter());
@@ -39,13 +42,19 @@
     EXPECT_EQ(Matrix4::identity(), layerUpdater->backingLayer()->getTexTransform());
 
     // push the deferred updates to the layer
-    SkMatrix scaledMatrix = SkMatrix::MakeScale(0.5, 0.5);
-    SkBitmap bitmap;
-    bitmap.allocN32Pixels(16, 16);
-    sk_sp<SkImage> layerImage = SkImage::MakeFromBitmap(bitmap);
-    layerUpdater->updateLayer(true, scaledMatrix, HAL_DATASPACE_UNKNOWN, layerImage);
+    Matrix4 scaledMatrix;
+    scaledMatrix.loadScale(0.5, 0.5, 0.0);
+    layerUpdater->updateLayer(true, scaledMatrix.data, HAL_DATASPACE_UNKNOWN);
+    if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
+        GlLayer* glLayer = static_cast<GlLayer*>(layerUpdater->backingLayer());
+        glLayer->setRenderTarget(GL_TEXTURE_EXTERNAL_OES);
+    }
 
     // the backing layer should now have all the properties applied.
+    if (layerUpdater->backingLayer()->getApi() == Layer::Api::OpenGL) {
+        GlLayer* glLayer = static_cast<GlLayer*>(layerUpdater->backingLayer());
+        EXPECT_EQ((uint32_t)GL_TEXTURE_EXTERNAL_OES, glLayer->getRenderTarget());
+    }
     EXPECT_EQ(100u, layerUpdater->backingLayer()->getWidth());
     EXPECT_EQ(100u, layerUpdater->backingLayer()->getHeight());
     EXPECT_TRUE(layerUpdater->backingLayer()->getForceFilter());
diff --git a/libs/hwui/tests/unit/RenderNodeDrawableTests.cpp b/libs/hwui/tests/unit/RenderNodeDrawableTests.cpp
index eb67e6c..48bc8e4 100644
--- a/libs/hwui/tests/unit/RenderNodeDrawableTests.cpp
+++ b/libs/hwui/tests/unit/RenderNodeDrawableTests.cpp
@@ -18,13 +18,13 @@
 #include <gtest/gtest.h>
 
 #include <SkClipStack.h>
-#include <SkLiteRecorder.h>
 #include <SkSurface_Base.h>
 #include <string.h>
 #include "AnimationContext.h"
 #include "DamageAccumulator.h"
 #include "FatalTestCanvas.h"
 #include "IContextFactory.h"
+#include "RecordingCanvas.h"
 #include "SkiaCanvas.h"
 #include "pipeline/skia/SkiaDisplayList.h"
 #include "pipeline/skia/SkiaOpenGLPipeline.h"
@@ -44,8 +44,8 @@
                 canvas.drawColor(Color::Red_500, SkBlendMode::kSrcOver);
             });
 
-    SkLiteDL skLiteDL;
-    SkLiteRecorder canvas;
+    DisplayListData skLiteDL;
+    RecordingCanvas canvas;
     canvas.reset(&skLiteDL, SkIRect::MakeWH(1, 1));
     canvas.translate(100, 100);
     RenderNodeDrawable drawable(rootNode.get(), &canvas);
diff --git a/libs/hwui/tests/unit/SkiaDisplayListTests.cpp b/libs/hwui/tests/unit/SkiaDisplayListTests.cpp
index 88d6dcf..6c398ee 100644
--- a/libs/hwui/tests/unit/SkiaDisplayListTests.cpp
+++ b/libs/hwui/tests/unit/SkiaDisplayListTests.cpp
@@ -36,32 +36,36 @@
 }
 
 TEST(SkiaDisplayList, reset) {
-    SkiaDisplayList skiaDL;
+    std::unique_ptr<SkiaDisplayList> skiaDL;
+    {
+        SkiaRecordingCanvas canvas{nullptr, 1, 1};
+        canvas.drawColor(0, SkBlendMode::kSrc);
+        skiaDL.reset(canvas.finishRecording());
+    }
 
     SkCanvas dummyCanvas;
     RenderNodeDrawable drawable(nullptr, &dummyCanvas);
-    skiaDL.mChildNodes.emplace_back(nullptr, &dummyCanvas);
-    skiaDL.mChildFunctors.emplace_back(nullptr, nullptr, &dummyCanvas);
-    skiaDL.mMutableImages.push_back(nullptr);
-    skiaDL.mVectorDrawables.push_back(nullptr);
-    skiaDL.mDisplayList.drawAnnotation(SkRect::MakeWH(200, 200), "testAnnotation", nullptr);
-    skiaDL.mProjectionReceiver = &drawable;
+    skiaDL->mChildNodes.emplace_back(nullptr, &dummyCanvas);
+    skiaDL->mChildFunctors.emplace_back(nullptr, nullptr, &dummyCanvas);
+    skiaDL->mMutableImages.push_back(nullptr);
+    skiaDL->mVectorDrawables.push_back(nullptr);
+    skiaDL->mProjectionReceiver = &drawable;
 
-    ASSERT_FALSE(skiaDL.mChildNodes.empty());
-    ASSERT_FALSE(skiaDL.mChildFunctors.empty());
-    ASSERT_FALSE(skiaDL.mMutableImages.empty());
-    ASSERT_FALSE(skiaDL.mVectorDrawables.empty());
-    ASSERT_FALSE(skiaDL.isEmpty());
-    ASSERT_TRUE(skiaDL.mProjectionReceiver);
+    ASSERT_FALSE(skiaDL->mChildNodes.empty());
+    ASSERT_FALSE(skiaDL->mChildFunctors.empty());
+    ASSERT_FALSE(skiaDL->mMutableImages.empty());
+    ASSERT_FALSE(skiaDL->mVectorDrawables.empty());
+    ASSERT_FALSE(skiaDL->isEmpty());
+    ASSERT_TRUE(skiaDL->mProjectionReceiver);
 
-    skiaDL.reset();
+    skiaDL->reset();
 
-    ASSERT_TRUE(skiaDL.mChildNodes.empty());
-    ASSERT_TRUE(skiaDL.mChildFunctors.empty());
-    ASSERT_TRUE(skiaDL.mMutableImages.empty());
-    ASSERT_TRUE(skiaDL.mVectorDrawables.empty());
-    ASSERT_TRUE(skiaDL.isEmpty());
-    ASSERT_FALSE(skiaDL.mProjectionReceiver);
+    ASSERT_TRUE(skiaDL->mChildNodes.empty());
+    ASSERT_TRUE(skiaDL->mChildFunctors.empty());
+    ASSERT_TRUE(skiaDL->mMutableImages.empty());
+    ASSERT_TRUE(skiaDL->mVectorDrawables.empty());
+    ASSERT_TRUE(skiaDL->isEmpty());
+    ASSERT_FALSE(skiaDL->mProjectionReceiver);
 }
 
 TEST(SkiaDisplayList, reuseDisplayList) {
diff --git a/libs/hwui/tests/unit/SkiaPipelineTests.cpp b/libs/hwui/tests/unit/SkiaPipelineTests.cpp
index 26b6000..a9124d9 100644
--- a/libs/hwui/tests/unit/SkiaPipelineTests.cpp
+++ b/libs/hwui/tests/unit/SkiaPipelineTests.cpp
@@ -18,7 +18,6 @@
 #include <gtest/gtest.h>
 
 #include <SkClipStack.h>
-#include <SkLiteRecorder.h>
 #include <SkSurface_Base.h>
 #include <string.h>
 #include "AnimationContext.h"
diff --git a/libs/hwui/tests/unit/SkiaRenderPropertiesTests.cpp b/libs/hwui/tests/unit/SkiaRenderPropertiesTests.cpp
index ad5fdac..479c462 100644
--- a/libs/hwui/tests/unit/SkiaRenderPropertiesTests.cpp
+++ b/libs/hwui/tests/unit/SkiaRenderPropertiesTests.cpp
@@ -18,7 +18,6 @@
 #include <gtest/gtest.h>
 
 #include <SkClipStack.h>
-#include <SkLiteRecorder.h>
 #include <SkSurface_Base.h>
 #include <string.h>
 #include "AnimationContext.h"
diff --git a/libs/hwui/tests/unit/main.cpp b/libs/hwui/tests/unit/main.cpp
index aecceb3..9e6d9a8 100644
--- a/libs/hwui/tests/unit/main.cpp
+++ b/libs/hwui/tests/unit/main.cpp
@@ -17,13 +17,12 @@
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
+#include "Caches.h"
 #include "debug/GlesDriver.h"
 #include "debug/NullGlesDriver.h"
 #include "hwui/Typeface.h"
 #include "Properties.h"
 #include "tests/common/LeakChecker.h"
-#include "thread/TaskProcessor.h"
-#include "thread/Task.h"
 #include "thread/TaskManager.h"
 
 #include <signal.h>
diff --git a/libs/hwui/utils/LinearAllocator.cpp b/libs/hwui/utils/LinearAllocator.cpp
index 5a59de8..3e5021c 100644
--- a/libs/hwui/utils/LinearAllocator.cpp
+++ b/libs/hwui/utils/LinearAllocator.cpp
@@ -29,6 +29,7 @@
 
 #include <stdlib.h>
 #include <utils/Log.h>
+#include <utils/Macros.h>
 
 // The ideal size of a page allocation (these need to be multiples of 8)
 #define INITIAL_PAGE_SIZE ((size_t)512)  // 512b
@@ -41,15 +42,6 @@
 // Must be smaller than INITIAL_PAGE_SIZE
 #define MAX_WASTE_RATIO (0.5f)
 
-#if ALIGN_DOUBLE
-#define ALIGN_SZ (sizeof(double))
-#else
-#define ALIGN_SZ (sizeof(int))
-#endif
-
-#define ALIGN(x) (((x) + ALIGN_SZ - 1) & ~(ALIGN_SZ - 1))
-#define ALIGN_PTR(p) ((void*)(ALIGN((size_t)(p))))
-
 #if LOG_NDEBUG
 #define ADD_ALLOCATION()
 #define RM_ALLOCATION()
diff --git a/libs/hwui/utils/Macros.h b/libs/hwui/utils/Macros.h
index d758f29..eee6785 100644
--- a/libs/hwui/utils/Macros.h
+++ b/libs/hwui/utils/Macros.h
@@ -34,4 +34,13 @@
 
 #define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
 
+#if ALIGN_DOUBLE
+#define ALIGN_SZ (sizeof(double))
+#else
+#define ALIGN_SZ (sizeof(int))
+#endif
+
+#define ALIGN(x) (((x) + ALIGN_SZ - 1) & ~(ALIGN_SZ - 1))
+#define ALIGN_PTR(p) ((void*)(ALIGN((size_t)(p))))
+
 #endif /* MACROS_H */
diff --git a/libs/hwui/utils/PaintUtils.h b/libs/hwui/utils/PaintUtils.h
index ebf2343..f8e8a0a 100644
--- a/libs/hwui/utils/PaintUtils.h
+++ b/libs/hwui/utils/PaintUtils.h
@@ -16,7 +16,6 @@
 #ifndef PAINT_UTILS_H
 #define PAINT_UTILS_H
 
-#include <GLES2/gl2.h>
 #include <utils/Blur.h>
 
 #include <SkColorFilter.h>
diff --git a/libs/hwui/utils/TypeLogic.h b/libs/hwui/utils/TypeLogic.h
new file mode 100644
index 0000000..dbdad33
--- /dev/null
+++ b/libs/hwui/utils/TypeLogic.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2018 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.
+ */
+
+#pragma once
+
+#include <type_traits>
+
+namespace android::uirenderer {
+
+template <typename D, typename S> struct copy_const {
+    using type = std::conditional_t<std::is_const<S>::value, std::add_const_t<D>, D>;
+};
+template <typename D, typename S> using copy_const_t = typename copy_const<D, S>::type;
+
+template <typename D, typename S> struct copy_volatile {
+    using type = std::conditional_t<std::is_volatile<S>::value, std::add_volatile_t<D>, D>;
+};
+template <typename D, typename S> using copy_volatile_t = typename copy_volatile<D, S>::type;
+
+template <typename D, typename S> struct copy_cv {
+    using type = copy_volatile_t<copy_const_t<D, S>, S>;
+};
+
+template <typename D, typename S> using same_cv = copy_cv<std::remove_cv_t<D>, S>;
+template <typename D, typename S> using same_cv_t = typename same_cv<D, S>::type;
+
+}; // namespace android::uirenderer
\ No newline at end of file
diff --git a/native/android/Android.bp b/native/android/Android.bp
index 43847cc..4fb5e74 100644
--- a/native/android/Android.bp
+++ b/native/android/Android.bp
@@ -64,7 +64,6 @@
         "libsensor",
         "libandroid_runtime",
         "libnetd_client",
-        "libhwui",
     ],
 
     static_libs: [
diff --git a/native/android/surface_texture.cpp b/native/android/surface_texture.cpp
index ced279277..b266881 100644
--- a/native/android/surface_texture.cpp
+++ b/native/android/surface_texture.cpp
@@ -21,16 +21,15 @@
 
 #include <utils/Log.h>
 
+#include <gui/GLConsumer.h>
 #include <gui/Surface.h>
 
 #include <android_runtime/android_graphics_SurfaceTexture.h>
 
-#include "surfacetexture/SurfaceTexture.h"
-
 using namespace android;
 
 struct ASurfaceTexture {
-    sp<SurfaceTexture> consumer;
+    sp<GLConsumer> consumer;
     sp<IGraphicBufferProducer> producer;
 };
 
diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp
index 4791517..57767e9 100644
--- a/packages/SettingsLib/Android.bp
+++ b/packages/SettingsLib/Android.bp
@@ -11,6 +11,10 @@
         "androidx.lifecycle_lifecycle-runtime",
     ],
 
+    static_libs: [
+        "SettingsLibHelpUtils",
+    ],
+
     // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_SHARED_JAVA_LIBRARIES
     // LOCAL_SHARED_JAVA_LIBRARIES := androidx.lifecycle_lifecycle-common
 
diff --git a/packages/SettingsLib/HelpUtils/Android.bp b/packages/SettingsLib/HelpUtils/Android.bp
new file mode 100644
index 0000000..af60185
--- /dev/null
+++ b/packages/SettingsLib/HelpUtils/Android.bp
@@ -0,0 +1,9 @@
+android_library {
+    name: "SettingsLibHelpUtils",
+
+    srcs: ["src/**/*.java"],
+    resource_dirs: ["res"],
+
+    sdk_version: "system_current",
+    min_sdk_version: "21",
+}
diff --git a/packages/SettingsLib/HelpUtils/AndroidManifest.xml b/packages/SettingsLib/HelpUtils/AndroidManifest.xml
new file mode 100644
index 0000000..35b3933
--- /dev/null
+++ b/packages/SettingsLib/HelpUtils/AndroidManifest.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2018 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.settingslib.helputils">
+
+    <uses-sdk android:minSdkVersion="21" />
+
+</manifest>
diff --git a/packages/SettingsLib/res/drawable/ic_help_actionbar.xml b/packages/SettingsLib/HelpUtils/res/drawable/ic_help_actionbar.xml
similarity index 100%
rename from packages/SettingsLib/res/drawable/ic_help_actionbar.xml
rename to packages/SettingsLib/HelpUtils/res/drawable/ic_help_actionbar.xml
diff --git a/packages/SettingsLib/HelpUtils/res/values/strings.xml b/packages/SettingsLib/HelpUtils/res/values/strings.xml
new file mode 100644
index 0000000..ae07f5d
--- /dev/null
+++ b/packages/SettingsLib/HelpUtils/res/values/strings.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2018 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.
+  -->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+
+    <!-- Label for Help and feedback menu item -->
+    <string name="help_feedback_label">Help &amp; feedback</string>
+
+</resources>
\ No newline at end of file
diff --git a/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java b/packages/SettingsLib/HelpUtils/src/com/android/settingslib/HelpUtils.java
similarity index 91%
rename from packages/SettingsLib/src/com/android/settingslib/HelpUtils.java
rename to packages/SettingsLib/HelpUtils/src/com/android/settingslib/HelpUtils.java
index 4cbeb8a..7306d968 100644
--- a/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java
+++ b/packages/SettingsLib/HelpUtils/src/com/android/settingslib/HelpUtils.java
@@ -33,12 +33,11 @@
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
 
-import com.android.internal.logging.MetricsLogger;
-import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
-
 import java.net.URISyntaxException;
 import java.util.Locale;
 
+import com.android.settingslib.helputils.R;
+
 /**
  * Functions to easily prepare contextual help menu option items with an intent that opens up the
  * browser to a particular URL, while taking into account the preferred language and app version.
@@ -115,9 +114,13 @@
                 helpMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                     @Override
                     public boolean onMenuItemClick(MenuItem item) {
+                        /**
+                         * TODO: Enable metrics logger for @SystemApi (b/111552654)
+                         *
                         MetricsLogger.action(activity,
                             MetricsEvent.ACTION_SETTING_HELP_AND_FEEDBACK,
                             intent.getStringExtra(EXTRA_CONTEXT));
+                        */
                         try {
                             activity.startActivityForResult(intent, 0);
                         } catch (ActivityNotFoundException exc) {
@@ -180,23 +183,21 @@
 
         Resources resources = context.getResources();
         boolean includePackageName =
-                resources.getBoolean(com.android.internal.R.bool.config_sendPackageName);
+                resources.getBoolean(android.R.bool.config_sendPackageName);
 
         if (sendPackageName && includePackageName) {
             String[] packageNameKey =
-                    {resources.getString(com.android.internal.R.string.config_helpPackageNameKey)};
+                    {resources.getString(android.R.string.config_help_package_name_key)};
             String[] packageNameValue =
-                    {resources.getString(
-                            com.android.internal.R.string.config_helpPackageNameValue)};
+                    {resources.getString(android.R.string.config_help_package_name_value)};
             String helpIntentExtraKey =
-                    resources.getString(com.android.internal.R.string.config_helpIntentExtraKey);
+                    resources.getString(android.R.string.config_help_intent_extra_key);
             String helpIntentNameKey =
-                    resources.getString(com.android.internal.R.string.config_helpIntentNameKey);
+                    resources.getString(android.R.string.config_help_intent_name_key);
             String feedbackIntentExtraKey =
-                    resources.getString(
-                            com.android.internal.R.string.config_feedbackIntentExtraKey);
+                    resources.getString(android.R.string.config_feedback_intent_extra_key);
             String feedbackIntentNameKey =
-                    resources.getString(com.android.internal.R.string.config_feedbackIntentNameKey);
+                    resources.getString(android.R.string.config_feedback_intent_name_key);
             intent.putExtra(helpIntentExtraKey, packageNameKey);
             intent.putExtra(helpIntentNameKey, packageNameValue);
             intent.putExtra(feedbackIntentExtraKey, packageNameKey);
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml
index 6bc07c2..ea6844e 100644
--- a/packages/SettingsLib/res/values/strings.xml
+++ b/packages/SettingsLib/res/values/strings.xml
@@ -1028,8 +1028,6 @@
     <!-- Description for a custom screen zoom level. This shows the requested display
          density in raw pixels per inch rather than using a relative description. [CHAR LIMIT=24] -->
     <string name="screen_zoom_summary_custom">Custom (<xliff:g id="densityDpi" example="160">%d</xliff:g>)</string>
-    <!-- Label for Help and feedback menu item -->
-    <string name="help_feedback_label">Help &amp; feedback</string>
 
     <!-- Content description for drawer menu button [CHAR_LIMIT=30]-->
     <string name="content_description_menu_button">Menu</string>
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
index e071b7c..4aca2bb 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/BluetoothEventManager.java
@@ -53,22 +53,23 @@
     private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver();
     private final BroadcastReceiver mProfileBroadcastReceiver = new BluetoothBroadcastReceiver();
     private final Collection<BluetoothCallback> mCallbacks = new ArrayList<>();
-
-    private android.os.Handler mReceiverHandler;
-    private Context mContext;
+    private final android.os.Handler mReceiverHandler;
+    private final Context mContext;
 
     interface Handler {
         void onReceive(Context context, Intent intent, BluetoothDevice device);
     }
 
     BluetoothEventManager(LocalBluetoothAdapter adapter,
-            CachedBluetoothDeviceManager deviceManager, Context context) {
+            CachedBluetoothDeviceManager deviceManager, Context context,
+            android.os.Handler handler) {
         mLocalAdapter = adapter;
         mDeviceManager = deviceManager;
         mAdapterIntentFilter = new IntentFilter();
         mProfileIntentFilter = new IntentFilter();
         mHandlerMap = new HashMap<>();
         mContext = context;
+        mReceiverHandler = handler;
 
         // Bluetooth on/off broadcasts
         addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());
@@ -104,15 +105,6 @@
                 new AudioModeChangedHandler());
 
         mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter, null, mReceiverHandler);
-        mContext.registerReceiver(mProfileBroadcastReceiver, mProfileIntentFilter, null, mReceiverHandler);
-    }
-
-    public void setReceiverHandler(android.os.Handler handler) {
-        mContext.unregisterReceiver(mBroadcastReceiver);
-        mContext.unregisterReceiver(mProfileBroadcastReceiver);
-        mReceiverHandler = handler;
-        mContext.registerReceiver(mBroadcastReceiver, mAdapterIntentFilter, null, mReceiverHandler);
-        registerProfileIntentReceiver();
     }
 
     /** Register to start receiving callbacks for Bluetooth events. */
@@ -233,12 +225,6 @@
                 BluetoothDevice device) {
             int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                     BluetoothAdapter.ERROR);
-            // Reregister Profile Broadcast Receiver as part of TURN OFF
-            if (state == BluetoothAdapter.STATE_OFF)
-            {
-                context.unregisterReceiver(mProfileBroadcastReceiver);
-                registerProfileIntentReceiver();
-            }
             // update local profiles and get paired devices
             mLocalAdapter.setBluetoothStateInt(state);
             // send callback to update UI and possibly start scanning
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java
index 5a64e02..e8f47e1 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManager.java
@@ -211,26 +211,6 @@
         }
     }
 
-    /**
-     * Attempts to get the name of a remote device, otherwise returns the address.
-     *
-     * @param device The remote device.
-     * @return The name, or if unavailable, the address.
-     */
-    public String getName(BluetoothDevice device) {
-        CachedBluetoothDevice cachedDevice = findDevice(device);
-        if (cachedDevice != null && cachedDevice.getName() != null) {
-            return cachedDevice.getName();
-        }
-
-        String name = device.getAliasName();
-        if (name != null) {
-            return name;
-        }
-
-        return device.getAddress();
-    }
-
     public synchronized void clearNonBondedDevices() {
 
         mCachedDevicesMapForHearingAids.entrySet().removeIf(entries
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java
index 99f550b..2dd8eaf 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HeadsetProfile.java
@@ -36,7 +36,6 @@
  */
 public class HeadsetProfile implements LocalBluetoothProfile {
     private static final String TAG = "HeadsetProfile";
-    private static boolean V = true;
 
     private BluetoothHeadset mService;
     private boolean mIsProfileReady;
@@ -59,7 +58,7 @@
             implements BluetoothProfile.ServiceListener {
 
         public void onServiceConnected(int profile, BluetoothProfile proxy) {
-            if (V) Log.d(TAG,"Bluetooth service connected");
+            Log.d(TAG,"Bluetooth service connected");
             mService = (BluetoothHeadset) proxy;
             // We just bound to the service, so refresh the UI for any connected HFP devices.
             List<BluetoothDevice> deviceList = mService.getConnectedDevices();
@@ -81,7 +80,7 @@
         }
 
         public void onServiceDisconnected(int profile) {
-            if (V) Log.d(TAG,"Bluetooth service disconnected");
+            Log.d(TAG,"Bluetooth service disconnected");
             mProfileManager.callServiceDisconnectedListeners();
             mIsProfileReady=false;
         }
@@ -113,80 +112,76 @@
     }
 
     public boolean connect(BluetoothDevice device) {
-        if (mService == null) return false;
-        List<BluetoothDevice> sinks = mService.getConnectedDevices();
-        if (sinks != null) {
-            for (BluetoothDevice sink : sinks) {
-                Log.d(TAG,"Not disconnecting device = " + sink);
-            }
+        if (mService == null) {
+            return false;
         }
         return mService.connect(device);
     }
 
     public boolean disconnect(BluetoothDevice device) {
-        if (mService == null) return false;
-        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
-        if (!deviceList.isEmpty()) {
-            for (BluetoothDevice dev : deviceList) {
-                if (dev.equals(device)) {
-                    if (V) Log.d(TAG,"Downgrade priority as user" +
-                                        "is disconnecting the headset");
-                    // Downgrade priority as user is disconnecting the headset.
-                    if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
-                        mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
-                    }
-                    return mService.disconnect(device);
-                }
-            }
+        if (mService == null) {
+            return false;
         }
-        return false;
+        // Downgrade priority as user is disconnecting the headset.
+        if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
+            mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
+        }
+        return mService.disconnect(device);
     }
 
     public int getConnectionStatus(BluetoothDevice device) {
-        if (mService == null) return BluetoothProfile.STATE_DISCONNECTED;
-        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
-        if (!deviceList.isEmpty()){
-            for (BluetoothDevice dev : deviceList) {
-                if (dev.equals(device)) {
-                    return mService.getConnectionState(device);
-                }
-            }
+        if (mService == null) {
+            return BluetoothProfile.STATE_DISCONNECTED;
         }
-        return BluetoothProfile.STATE_DISCONNECTED;
+        return mService.getConnectionState(device);
     }
 
     public boolean setActiveDevice(BluetoothDevice device) {
-        if (mService == null) return false;
+        if (mService == null) {
+            return false;
+        }
         return mService.setActiveDevice(device);
     }
 
     public BluetoothDevice getActiveDevice() {
-        if (mService == null) return null;
+        if (mService == null) {
+            return null;
+        }
         return mService.getActiveDevice();
     }
 
     public boolean isAudioOn() {
-        if (mService == null) return false;
+        if (mService == null) {
+            return false;
+        }
         return mService.isAudioOn();
     }
 
     public int getAudioState(BluetoothDevice device) {
-        if (mService == null) return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
+        if (mService == null) {
+            return BluetoothHeadset.STATE_AUDIO_DISCONNECTED;
+        }
         return mService.getAudioState(device);
     }
 
     public boolean isPreferred(BluetoothDevice device) {
-        if (mService == null) return false;
+        if (mService == null) {
+            return false;
+        }
         return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
     }
 
     public int getPreferred(BluetoothDevice device) {
-        if (mService == null) return BluetoothProfile.PRIORITY_OFF;
+        if (mService == null) {
+            return BluetoothProfile.PRIORITY_OFF;
+        }
         return mService.getPriority(device);
     }
 
     public void setPreferred(BluetoothDevice device, boolean preferred) {
-        if (mService == null) return;
+        if (mService == null) {
+            return;
+        }
         if (preferred) {
             if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
                 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
@@ -197,7 +192,9 @@
     }
 
     public List<BluetoothDevice> getConnectedDevices() {
-        if (mService == null) return new ArrayList<BluetoothDevice>(0);
+        if (mService == null) {
+            return new ArrayList<BluetoothDevice>(0);
+        }
         return mService.getDevicesMatchingConnectionStates(
               new int[] {BluetoothProfile.STATE_CONNECTED,
                          BluetoothProfile.STATE_CONNECTING,
@@ -235,7 +232,7 @@
     }
 
     protected void finalize() {
-        if (V) Log.d(TAG, "finalize()");
+        Log.d(TAG, "finalize()");
         if (mService != null) {
             try {
                 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HEADSET,
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HidProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HidProfile.java
index 701ef00..fe6b222 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HidProfile.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HidProfile.java
@@ -29,7 +29,7 @@
 import java.util.List;
 
 /**
- * HidProfile handles Bluetooth HID profile.
+ * HidProfile handles Bluetooth HID Host role.
  */
 public class HidProfile implements LocalBluetoothProfile {
     private static final String TAG = "HidProfile";
@@ -115,11 +115,7 @@
         if (mService == null) {
             return BluetoothProfile.STATE_DISCONNECTED;
         }
-        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
-
-        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
-                ? mService.getConnectionState(device)
-                : BluetoothProfile.STATE_DISCONNECTED;
+        return mService.getConnectionState(device);
     }
 
     public boolean isPreferred(BluetoothDevice device) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothManager.java
index 51601a5..7fe6205 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LocalBluetoothManager.java
@@ -17,6 +17,7 @@
 package com.android.settingslib.bluetooth;
 
 import android.content.Context;
+import android.os.Handler;
 import android.util.Log;
 
 import java.lang.ref.WeakReference;
@@ -57,7 +58,7 @@
             }
             // This will be around as long as this process is
             Context appContext = context.getApplicationContext();
-            sInstance = new LocalBluetoothManager(adapter, appContext);
+            sInstance = new LocalBluetoothManager(adapter, appContext, null);
             if (onInitCallback != null) {
                 onInitCallback.onBluetoothManagerInitialized(appContext, sInstance);
             }
@@ -66,13 +67,25 @@
         return sInstance;
     }
 
-    private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
+    /**
+     * Returns a new instance of {@link LocalBluetoothManager} or null if Bluetooth is not
+     * supported for this hardware. This instance should be globally cached by the caller.
+     */
+    public static LocalBluetoothManager create(Context context, Handler handler) {
+        LocalBluetoothAdapter adapter = LocalBluetoothAdapter.getInstance();
+        if (adapter == null) {
+            return null;
+        }
+        return new LocalBluetoothManager(adapter, context.getApplicationContext(), handler);
+    }
+
+    private LocalBluetoothManager(
+            LocalBluetoothAdapter adapter, Context context, Handler handler) {
         mContext = context;
         mLocalAdapter = adapter;
-
         mCachedDeviceManager = new CachedBluetoothDeviceManager(context, this);
         mEventManager = new BluetoothEventManager(mLocalAdapter,
-                mCachedDeviceManager, context);
+                mCachedDeviceManager, context, handler);
         mProfileManager = new LocalBluetoothProfileManager(context,
                 mLocalAdapter, mCachedDeviceManager, mEventManager);
 
diff --git a/packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java b/packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java
index 3e3c039..88be2b0 100644
--- a/packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java
+++ b/packages/SettingsLib/src/com/android/settingslib/utils/StringUtil.java
@@ -27,6 +27,7 @@
 import android.text.SpannableStringBuilder;
 import android.text.Spanned;
 import android.text.style.TtsSpan;
+import android.util.Log;
 
 import com.android.settingslib.R;
 
@@ -36,6 +37,8 @@
 /** Utility class for generally useful string methods **/
 public class StringUtil {
 
+    private static final String TAG = "StringUtil";
+
     public static final int SECONDS_PER_MINUTE = 60;
     public static final int SECONDS_PER_HOUR = 60 * 60;
     public static final int SECONDS_PER_DAY = 24 * 60 * 60;
@@ -94,6 +97,7 @@
         final Locale locale = context.getResources().getConfiguration().locale;
         final MeasureFormat measureFormat = MeasureFormat.getInstance(
                 locale, FormatWidth.SHORT);
+        Log.i(TAG, "Locale is: " + locale);
         sb.append(measureFormat.formatMeasures(measureArray));
 
         if (measureArray.length == 1 && MeasureUnit.MINUTE.equals(measureArray[0].getUnit())) {
@@ -146,6 +150,7 @@
                 null /* default NumberFormat */,
                 RelativeDateTimeFormatter.Style.LONG,
                 android.icu.text.DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE);
+        Log.i(TAG, "Locale is: " + locale);
 
         return formatter.format(value, RelativeDateTimeFormatter.Direction.LAST, unit);
     }
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/HelpUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/HelpUtilsTest.java
index 7553313..1091e16 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/HelpUtilsTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/HelpUtilsTest.java
@@ -36,7 +36,7 @@
 import android.provider.Settings;
 import android.view.MenuItem;
 
-import com.android.internal.R;
+import android.R;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -70,17 +70,17 @@
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        when(mContext.getResources().getString(R.string.config_helpPackageNameKey))
+        when(mContext.getResources().getString(R.string.config_help_package_name_key))
                 .thenReturn(PACKAGE_NAME_KEY);
-        when(mContext.getResources().getString(R.string.config_helpPackageNameValue))
+        when(mContext.getResources().getString(R.string.config_help_package_name_value))
                 .thenReturn(PACKAGE_NAME_VALUE);
-        when(mContext.getResources().getString(R.string.config_helpIntentExtraKey))
+        when(mContext.getResources().getString(R.string.config_help_intent_extra_key))
                 .thenReturn(HELP_INTENT_EXTRA_KEY);
-        when(mContext.getResources().getString(R.string.config_helpIntentNameKey))
+        when(mContext.getResources().getString(R.string.config_help_intent_name_key))
                 .thenReturn(HELP_INTENT_NAME_KEY);
-        when(mContext.getResources().getString(R.string.config_feedbackIntentExtraKey))
+        when(mContext.getResources().getString(R.string.config_feedback_intent_extra_key))
                 .thenReturn(FEEDBACK_INTENT_EXTRA_KEY);
-        when(mContext.getResources().getString(R.string.config_feedbackIntentNameKey))
+        when(mContext.getResources().getString(R.string.config_feedback_intent_name_key))
                 .thenReturn(FEEDBACK_INTENT_NAME_KEY);
         when(mActivity.getPackageManager()).thenReturn(mPackageManager);
 
@@ -173,4 +173,4 @@
         verify(item).setVisible(true);
         verify(item).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
     }
-}
\ No newline at end of file
+}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
index 5f42b66..14bfb27 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/BluetoothEventManagerTest.java
@@ -54,7 +54,7 @@
         mContext = RuntimeEnvironment.application;
 
         mBluetoothEventManager = new BluetoothEventManager(mLocalAdapter,
-                mCachedDeviceManager, mContext);
+                mCachedDeviceManager, mContext, null);
     }
 
     /**
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java
index 7baded8..8ac611f 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/CachedBluetoothDeviceManagerTest.java
@@ -139,7 +139,7 @@
     public void testGetName_validCachedDevice_nameFound() {
         CachedBluetoothDevice cachedDevice1 = mCachedDeviceManager.addDevice(mDevice1);
         assertThat(cachedDevice1).isNotNull();
-        assertThat(mCachedDeviceManager.getName(mDevice1)).isEqualTo(DEVICE_ALIAS_1);
+        assertThat(mCachedDeviceManager.findDevice(mDevice1).getName()).isEqualTo(DEVICE_ALIAS_1);
     }
 
     /**
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java
index 5550b7e..6f4c292 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/LocalBluetoothProfileManagerTest.java
@@ -76,7 +76,7 @@
         mContext = spy(RuntimeEnvironment.application);
         mLocalBluetoothAdapter = LocalBluetoothAdapter.getInstance();
         mEventManager = spy(new BluetoothEventManager(mLocalBluetoothAdapter, mDeviceManager,
-                mContext));
+                mContext, null));
         mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
         when(mDeviceManager.findDevice(mDevice)).thenReturn(mCachedBluetoothDevice);
         mProfileManager = new LocalBluetoothProfileManager(mContext, mLocalBluetoothAdapter,
@@ -151,9 +151,7 @@
         mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                 new int[] {BluetoothProfile.A2DP}));
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -174,9 +172,7 @@
         mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                 new int[] {BluetoothProfile.HEADSET}));
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -197,9 +193,7 @@
         mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                 new int[] {BluetoothProfile.HEARING_AID}));
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -220,9 +214,7 @@
         mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                 new int[] {BluetoothProfile.PAN}));
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -243,9 +235,7 @@
     () {
         mShadowBluetoothAdapter.setSupportedProfiles(null);
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
@@ -265,9 +255,7 @@
         mShadowBluetoothAdapter.setSupportedProfiles(generateList(
                 new int[] {BluetoothProfile.PAN}));
         mProfileManager.updateLocalProfiles();
-        // Refer to BluetoothControllerImpl, it will call setReceiverHandler after
-        // LocalBluetoothProfileManager created.
-        mEventManager.setReceiverHandler(null);
+
         mIntent = new Intent(BluetoothPan.ACTION_CONNECTION_STATE_CHANGED);
         mIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
         mIntent.putExtra(BluetoothProfile.EXTRA_PREVIOUS_STATE, BluetoothProfile.STATE_CONNECTING);
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TransactionCompat.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TransactionCompat.java
index 67afee0..70258c2 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TransactionCompat.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TransactionCompat.java
@@ -83,11 +83,6 @@
         return this;
     }
 
-    public TransactionCompat setFinalCrop(SurfaceControlCompat surfaceControl, Rect crop) {
-        mTransaction.setFinalCrop(surfaceControl.mSurfaceControl, crop);
-        return this;
-    }
-
     public TransactionCompat deferTransactionUntil(SurfaceControlCompat surfaceControl,
             IBinder handle, long frameNumber) {
         mTransaction.deferTransactionUntil(surfaceControl.mSurfaceControl, handle, frameNumber);
diff --git a/packages/SystemUI/src/com/android/systemui/Dependency.java b/packages/SystemUI/src/com/android/systemui/Dependency.java
index 2dd54aa..b2cf305 100644
--- a/packages/SystemUI/src/com/android/systemui/Dependency.java
+++ b/packages/SystemUI/src/com/android/systemui/Dependency.java
@@ -133,6 +133,10 @@
      */
     public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>("background_looper");
     /**
+     * Key for getting a background Handler for background work.
+     */
+    public static final DependencyKey<Handler> BG_HANDLER = new DependencyKey<>("background_handler");
+    /**
      * Key for getting a Handler for receiving time tick broadcasts on.
      */
     public static final DependencyKey<Handler> TIME_TICK_HANDLER =
@@ -166,6 +170,7 @@
             thread.start();
             return thread.getLooper();
         });
+        mProviders.put(BG_HANDLER, () -> new Handler(getDependency(BG_LOOPER)));
         mProviders.put(MAIN_HANDLER, () -> new Handler(Looper.getMainLooper()));
         mProviders.put(ActivityStarter.class, () -> new ActivityStarterDelegate());
         mProviders.put(ActivityStarterDelegate.class, () ->
@@ -288,7 +293,7 @@
                 new PluginDependencyProvider(get(PluginManager.class)));
 
         mProviders.put(LocalBluetoothManager.class, () ->
-                LocalBluetoothManager.getInstance(mContext, null));
+                LocalBluetoothManager.create(mContext, getDependency(BG_HANDLER)));
 
         mProviders.put(VolumeDialogController.class, () ->
                 new VolumeDialogControllerImpl(mContext));
diff --git a/packages/SystemUI/src/com/android/systemui/keyboard/KeyboardUI.java b/packages/SystemUI/src/com/android/systemui/keyboard/KeyboardUI.java
index 76a1acc..e0657c9 100644
--- a/packages/SystemUI/src/com/android/systemui/keyboard/KeyboardUI.java
+++ b/packages/SystemUI/src/com/android/systemui/keyboard/KeyboardUI.java
@@ -49,6 +49,7 @@
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
 import com.android.settingslib.bluetooth.BluetoothUtils;
+import com.android.systemui.Dependency;
 import com.android.systemui.R;
 import com.android.systemui.SystemUI;
 
@@ -171,7 +172,7 @@
             return;
         }
 
-        LocalBluetoothManager bluetoothManager = LocalBluetoothManager.getInstance(context, null);
+        LocalBluetoothManager bluetoothManager = Dependency.get(LocalBluetoothManager.class);
         if (bluetoothManager == null)  {
             if (DEBUG) {
                 Slog.e(TAG, "Failed to retrieve LocalBluetoothManager instance");
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
index ae99786..bb65bed 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
@@ -31,15 +31,15 @@
 import android.service.quicksettings.IQSTileService;
 import android.service.quicksettings.Tile;
 import android.service.quicksettings.TileService;
+import android.text.TextUtils;
 import android.text.format.DateUtils;
 import android.util.Log;
 import android.view.IWindowManager;
 import android.view.WindowManagerGlobal;
-import com.android.internal.logging.MetricsLogger;
+
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.systemui.Dependency;
 import com.android.systemui.plugins.ActivityStarter;
-import com.android.systemui.plugins.qs.QSTile;
 import com.android.systemui.plugins.qs.QSTile.State;
 import com.android.systemui.qs.tileimpl.QSTileImpl;
 import com.android.systemui.qs.external.TileLifecycleManager.TileChangeListener;
@@ -68,9 +68,9 @@
     private final TileServiceManager mServiceManager;
     private final int mUser;
     private android.graphics.drawable.Icon mDefaultIcon;
+    private CharSequence mDefaultLabel;
 
     private boolean mListening;
-    private boolean mBound;
     private boolean mIsTokenGranted;
     private boolean mIsShowingDialog;
 
@@ -79,7 +79,7 @@
         mWindowManager = WindowManagerGlobal.getWindowManagerService();
         mComponent = ComponentName.unflattenFromString(action);
         mTile = new Tile();
-        setTileIcon();
+        updateDefaultTileAndIcon();
         mServiceManager = host.getTileServices().getTileWrapper(this);
         mService = mServiceManager.getTileService();
         mServiceManager.setTileChangeListener(this);
@@ -91,13 +91,14 @@
         return CUSTOM_STALE_TIMEOUT + DateUtils.MINUTE_IN_MILLIS * mHost.indexOf(getTileSpec());
     }
 
-    private void setTileIcon() {
+    private void updateDefaultTileAndIcon() {
         try {
             PackageManager pm = mContext.getPackageManager();
             int flags = PackageManager.MATCH_DIRECT_BOOT_UNAWARE | PackageManager.MATCH_DIRECT_BOOT_AWARE;
             if (isSystemApp(pm)) {
                 flags |= PackageManager.MATCH_DISABLED_COMPONENTS;
             }
+
             ServiceInfo info = pm.getServiceInfo(mComponent, flags);
             int icon = info.icon != 0 ? info.icon
                     : info.applicationInfo.icon;
@@ -109,12 +110,16 @@
             if (updateIcon) {
                 mTile.setIcon(mDefaultIcon);
             }
-            // Update the label if there is no label.
-            if (mTile.getLabel() == null) {
-                mTile.setLabel(info.loadLabel(pm));
+            // Update the label if there is no label or it is the default label.
+            boolean updateLabel = mTile.getLabel() == null
+                    || TextUtils.equals(mTile.getLabel(), mDefaultLabel);
+            mDefaultLabel = info.loadLabel(pm);
+            if (updateLabel) {
+                mTile.setLabel(mDefaultLabel);
             }
-        } catch (Exception e) {
+        } catch (PackageManager.NameNotFoundException e) {
             mDefaultIcon = null;
+            mDefaultLabel = null;
         }
     }
 
@@ -148,7 +153,7 @@
 
     @Override
     public void onTileChanged(ComponentName tile) {
-        setTileIcon();
+        updateDefaultTileAndIcon();
     }
 
     @Override
@@ -170,6 +175,7 @@
     }
 
     public Tile getQsTile() {
+        updateDefaultTileAndIcon();
         return mTile;
     }
 
@@ -199,7 +205,7 @@
         mListening = listening;
         try {
             if (listening) {
-                setTileIcon();
+                updateDefaultTileAndIcon();
                 refreshState();
                 if (!mServiceManager.isActiveTile()) {
                     mServiceManager.setBindRequested(true);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java b/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java
index 91512dd..b7eee36 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/ScreenPinningRequest.java
@@ -262,16 +262,10 @@
             }
 
             if (navigationBarView != null) {
-                int dualToneDarkTheme = Utils.getThemeAttr(getContext(), R.attr.darkIconTheme);
-                int dualToneLightTheme = Utils.getThemeAttr(getContext(), R.attr.lightIconTheme);
-                Context lightContext = new ContextThemeWrapper(getContext(), dualToneLightTheme);
-                Context darkContext = new ContextThemeWrapper(getContext(), dualToneDarkTheme);
                 ((ImageView) mLayout.findViewById(R.id.screen_pinning_back_icon))
-                        .setImageDrawable(navigationBarView.getBackDrawable(lightContext,
-                                darkContext));
+                        .setImageDrawable(navigationBarView.getBackDrawable());
                 ((ImageView) mLayout.findViewById(R.id.screen_pinning_home_icon))
-                        .setImageDrawable(navigationBarView.getHomeDrawable(lightContext,
-                                darkContext));
+                        .setImageDrawable(navigationBarView.getHomeDrawable());
             }
 
             ((TextView) mLayout.findViewById(R.id.screen_pinning_description))
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
index b57a366..c094669 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/BiometricUnlockController.java
@@ -108,6 +108,7 @@
     private StatusBar mStatusBar;
     private final UnlockMethodCache mUnlockMethodCache;
     private final Context mContext;
+    private final int mWakeUpDelay;
     private int mPendingAuthenticatedUserId = -1;
     private BiometricSourceType mPendingAuthenticatedBioSourceType = null;
     private boolean mPendingShowBouncer;
@@ -131,6 +132,8 @@
         mScrimController = scrimController;
         mStatusBar = statusBar;
         mUnlockMethodCache = unlockMethodCache;
+        mWakeUpDelay = context.getResources().getInteger(
+                com.android.internal.R.integer.config_wakeUpDelayDoze);
     }
 
     public void setStatusBarKeyguardViewManager(
@@ -219,7 +222,7 @@
         // During wake and unlock, we need to draw black before waking up to avoid abrupt
         // brightness changes due to display state transitions.
         boolean alwaysOnEnabled = DozeParameters.getInstance(mContext).getAlwaysOn();
-        boolean delayWakeUp = mode == MODE_WAKE_AND_UNLOCK && alwaysOnEnabled;
+        boolean delayWakeUp = mode == MODE_WAKE_AND_UNLOCK && alwaysOnEnabled && mWakeUpDelay > 0;
         Runnable wakeUp = ()-> {
             if (!wasDeviceInteractive) {
                 if (DEBUG_BIO_WAKELOCK) {
@@ -271,7 +274,7 @@
                 }
                 mStatusBarWindowController.setStatusBarFocusable(false);
                 if (delayWakeUp) {
-                    mHandler.postDelayed(wakeUp, 50);
+                    mHandler.postDelayed(wakeUp, mWakeUpDelay);
                 } else {
                     mKeyguardViewMediator.onWakeAndUnlocking();
                 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
index b84ee03..10b019d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java
@@ -49,7 +49,6 @@
 import android.database.ContentObserver;
 import android.graphics.PixelFormat;
 import android.graphics.Rect;
-import android.graphics.drawable.AnimatedVectorDrawable;
 import android.inputmethodservice.InputMethodService;
 import android.os.Binder;
 import android.os.Bundle;
@@ -532,12 +531,6 @@
         KeyButtonDrawable kbd = rotBtn.getImageDrawable();
         if (kbd == null) return;
 
-        // The KBD and AVD is recreated every new valid suggestion because of style changes.
-        AnimatedVectorDrawable animIcon = null;
-        if (kbd.getDrawable(0) instanceof AnimatedVectorDrawable) {
-            animIcon = (AnimatedVectorDrawable) kbd.getDrawable(0);
-        }
-
         // Clear any pending suggestion flag as it has either been nullified or is being shown
         mPendingRotationSuggestion = false;
         if (getView() != null) getView().removeCallbacks(mCancelPendingRotationProposal);
@@ -554,9 +547,9 @@
             view.setAlpha(1f);
 
             // Run the rotate icon's animation if it has one
-            if (animIcon != null) {
-                animIcon.reset();
-                animIcon.start();
+            if (kbd.canAnimate()) {
+                kbd.resetAnimation();
+                kbd.startAnimation();
             }
 
             if (!isRotateSuggestionIntroduced()) mViewRippler.start(view);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index 1892d37..2141016 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -80,7 +80,6 @@
 import com.android.systemui.stackdivider.Divider;
 import com.android.systemui.statusbar.policy.DeadZone;
 import com.android.systemui.statusbar.policy.KeyButtonDrawable;
-import com.android.systemui.statusbar.policy.TintedKeyButtonDrawable;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -124,14 +123,12 @@
 
     private KeyButtonDrawable mBackIcon;
     private KeyButtonDrawable mHomeDefaultIcon;
-    private KeyButtonDrawable mBackCarModeIcon;
-    private KeyButtonDrawable mHomeCarModeIcon;
     private KeyButtonDrawable mRecentIcon;
     private KeyButtonDrawable mDockedIcon;
     private KeyButtonDrawable mImeIcon;
     private KeyButtonDrawable mMenuIcon;
     private KeyButtonDrawable mAccessibilityIcon;
-    private TintedKeyButtonDrawable mRotateSuggestionIcon;
+    private KeyButtonDrawable mRotateSuggestionIcon;
 
     private GestureHelper mGestureHelper;
     private final DeadZone mDeadZone;
@@ -461,62 +458,47 @@
                 && ((mOverviewProxyService.getInteractionFlags() & FLAG_DISABLE_QUICK_SCRUB) == 0);
     }
 
-    private void updateCarModeIcons(Context ctx) {
-        mBackCarModeIcon = getDrawable(ctx, R.drawable.ic_sysbar_back_carmode);
-        mHomeCarModeIcon = getDrawable(ctx, R.drawable.ic_sysbar_home_carmode);
-    }
-
     private void reloadNavIcons() {
-        updateIcons(mContext, Configuration.EMPTY, mConfiguration);
+        updateIcons(Configuration.EMPTY, mConfiguration);
     }
 
-    private void updateIcons(Context ctx, Configuration oldConfig, Configuration newConfig) {
-        int dualToneDarkTheme = Utils.getThemeAttr(ctx, R.attr.darkIconTheme);
-        int dualToneLightTheme = Utils.getThemeAttr(ctx, R.attr.lightIconTheme);
-        Context lightContext = new ContextThemeWrapper(ctx, dualToneLightTheme);
-        Context darkContext = new ContextThemeWrapper(ctx, dualToneDarkTheme);
-
+    private void updateIcons(Configuration oldConfig, Configuration newConfig) {
         final boolean orientationChange = oldConfig.orientation != newConfig.orientation;
         final boolean densityChange = oldConfig.densityDpi != newConfig.densityDpi;
         final boolean dirChange = oldConfig.getLayoutDirection() != newConfig.getLayoutDirection();
 
         if (orientationChange || densityChange) {
-            mDockedIcon = getDrawable(lightContext, darkContext, R.drawable.ic_sysbar_docked);
-            mHomeDefaultIcon = getHomeDrawable(lightContext, darkContext);
+            mDockedIcon = getDrawable(R.drawable.ic_sysbar_docked);
+            mHomeDefaultIcon = getHomeDrawable();
         }
         if (densityChange || dirChange) {
-            mRecentIcon = getDrawable(lightContext, darkContext, R.drawable.ic_sysbar_recent);
-            mMenuIcon = getDrawable(lightContext, darkContext, R.drawable.ic_sysbar_menu);
+            mRecentIcon = getDrawable(R.drawable.ic_sysbar_recent);
+            mMenuIcon = getDrawable(R.drawable.ic_sysbar_menu);
 
-            mAccessibilityIcon = getDrawable(lightContext, darkContext,
-                    R.drawable.ic_sysbar_accessibility_button, false /* hasShadow */);
-
-            mImeIcon = getDrawable(lightContext, darkContext, R.drawable.ic_ime_switcher_default,
+            mAccessibilityIcon = getDrawable(R.drawable.ic_sysbar_accessibility_button,
                     false /* hasShadow */);
 
-            updateRotateSuggestionButtonStyle(mRotateBtnStyle, false);
+            mImeIcon = getDrawable(R.drawable.ic_ime_switcher_default, false /* hasShadow */);
 
-            if (ALTERNATE_CAR_MODE_UI) {
-                updateCarModeIcons(ctx);
-            }
+            updateRotateSuggestionButtonStyle(mRotateBtnStyle, false);
         }
         if (orientationChange || densityChange || dirChange) {
-            mBackIcon = getBackDrawable(lightContext, darkContext);
+            mBackIcon = getBackDrawable();
         }
     }
 
-    public KeyButtonDrawable getBackDrawable(Context lightContext, Context darkContext) {
-        KeyButtonDrawable drawable = chooseNavigationIconDrawable(lightContext, darkContext,
-                R.drawable.ic_sysbar_back, R.drawable.ic_sysbar_back_quick_step);
+    public KeyButtonDrawable getBackDrawable() {
+        KeyButtonDrawable drawable = chooseNavigationIconDrawable(R.drawable.ic_sysbar_back,
+                R.drawable.ic_sysbar_back_quick_step);
         orientBackButton(drawable);
         return drawable;
     }
 
-    public KeyButtonDrawable getHomeDrawable(Context lightContext, Context darkContext) {
+    public KeyButtonDrawable getHomeDrawable() {
         final boolean quickStepEnabled = mOverviewProxyService.shouldShowSwipeUpUI();
         KeyButtonDrawable drawable = quickStepEnabled
-                ? getDrawable(lightContext, darkContext, R.drawable.ic_sysbar_home_quick_step)
-                : getDrawable(lightContext, darkContext, R.drawable.ic_sysbar_home);
+                ? getDrawable(R.drawable.ic_sysbar_home_quick_step)
+                : getDrawable(R.drawable.ic_sysbar_home);
         orientHomeButton(drawable);
         return drawable;
     }
@@ -549,33 +531,26 @@
         drawable.setRotation(mVertical ? 90 : 0);
     }
 
-    private KeyButtonDrawable chooseNavigationIconDrawable(Context lightContext,
-            Context darkContext, @DrawableRes int icon, @DrawableRes int quickStepIcon) {
+    private KeyButtonDrawable chooseNavigationIconDrawable(@DrawableRes int icon,
+            @DrawableRes int quickStepIcon) {
         final boolean quickStepEnabled = mOverviewProxyService.shouldShowSwipeUpUI();
-        return quickStepEnabled
-                ? getDrawable(lightContext, darkContext, quickStepIcon)
-                : getDrawable(lightContext, darkContext, icon);
+        return quickStepEnabled ? getDrawable(quickStepIcon) : getDrawable(icon);
     }
 
-    private KeyButtonDrawable getDrawable(Context lightContext, Context darkContext,
-            @DrawableRes int icon) {
-        return getDrawable(lightContext, darkContext, icon, true /* hasShadow */);
+    private KeyButtonDrawable getDrawable(@DrawableRes int icon) {
+        return getDrawable(mContext, icon, true /* hasShadow */);
     }
 
-    private KeyButtonDrawable getDrawable(Context lightContext, Context darkContext,
-            @DrawableRes int icon, boolean hasShadow) {
-        return KeyButtonDrawable.create(lightContext, lightContext.getDrawable(icon),
-                darkContext.getDrawable(icon), hasShadow);
+    private KeyButtonDrawable getDrawable(@DrawableRes int icon, boolean hasShadow) {
+        return getDrawable(mContext, icon, hasShadow);
     }
 
-    private KeyButtonDrawable getDrawable(Context ctx, @DrawableRes int icon) {
-        // Legacy image icons using a single image will not support shadows
-        return KeyButtonDrawable.create(ctx, ctx.getDrawable(icon), null, false /* hasShadow */);
-    }
-
-    private TintedKeyButtonDrawable getDrawable(Context ctx, @DrawableRes int icon,
-            @ColorInt int lightColor, @ColorInt int darkColor) {
-        return TintedKeyButtonDrawable.create(ctx.getDrawable(icon), lightColor, darkColor);
+    private KeyButtonDrawable getDrawable(Context ctx, @DrawableRes int icon, boolean hasShadow) {
+        final int dualToneDarkTheme = Utils.getThemeAttr(ctx, R.attr.darkIconTheme);
+        final int dualToneLightTheme = Utils.getThemeAttr(ctx, R.attr.lightIconTheme);
+        Context lightContext = new ContextThemeWrapper(ctx, dualToneLightTheme);
+        Context darkContext = new ContextThemeWrapper(ctx, dualToneDarkTheme);
+        return KeyButtonDrawable.create(lightContext, darkContext, icon, hasShadow);
     }
 
     @Override
@@ -585,10 +560,6 @@
         super.setLayoutDirection(layoutDirection);
     }
 
-    private KeyButtonDrawable getBackIcon(boolean carMode) {
-        return carMode ? mBackCarModeIcon : mBackIcon;
-    }
-
     public void setNavigationIconHints(int hints) {
         if (hints == mNavigationIconHints) return;
         final boolean backAlt = (hints & StatusBarManager.NAVIGATION_HINT_BACK_ALT) != 0;
@@ -626,9 +597,9 @@
         // to recent icon is not required.
         final boolean useAltBack =
                 (mNavigationIconHints & StatusBarManager.NAVIGATION_HINT_BACK_ALT) != 0;
-        KeyButtonDrawable backIcon = getBackIcon(mUseCarModeUi);
+        KeyButtonDrawable backIcon = mBackIcon;
         orientBackButton(backIcon);
-        KeyButtonDrawable homeIcon = mUseCarModeUi ? mHomeCarModeIcon : mHomeDefaultIcon;
+        KeyButtonDrawable homeIcon = mHomeDefaultIcon;
         if (!mUseCarModeUi) {
             orientHomeButton(homeIcon);
         }
@@ -845,31 +816,20 @@
         mRotateBtnStyle = style;
         final Context ctx = getContext();
 
-        // Extract the dark and light tints
-        final int dualToneDarkTheme = Utils.getThemeAttr(ctx, R.attr.darkIconTheme);
-        final int dualToneLightTheme = Utils.getThemeAttr(ctx, R.attr.lightIconTheme);
-        Context darkContext = new ContextThemeWrapper(ctx, dualToneDarkTheme);
-        Context lightContext = new ContextThemeWrapper(ctx, dualToneLightTheme);
-        final int lightColor = Utils.getColorAttrDefaultColor(lightContext, R.attr.singleToneColor);
-        final int darkColor = Utils.getColorAttrDefaultColor(darkContext, R.attr.singleToneColor);
-
         // Use the supplied style to set the icon's rotation parameters
         Context rotateContext = new ContextThemeWrapper(ctx, style);
 
         // Recreate the icon and set it if needed
-        TintedKeyButtonDrawable priorIcon = mRotateSuggestionIcon;
+        float previousIntensity = mRotateSuggestionIcon != null
+                ? mRotateSuggestionIcon.getDarkIntensity() : 0;
         mRotateSuggestionIcon = getDrawable(rotateContext, R.drawable.ic_sysbar_rotate_button,
-                lightColor, darkColor);
-
-        // Apply any prior set dark intensity
-        if (priorIcon != null && priorIcon.isDarkIntensitySet()) {
-            mRotateSuggestionIcon.setDarkIntensity(priorIcon.getDarkIntensity());
-        }
+                false /* hasShadow */);
+        mRotateSuggestionIcon.setDarkIntensity(previousIntensity);
 
         if (setIcon) getRotateSuggestionButton().setImageDrawable(mRotateSuggestionIcon);
     }
 
-    public int setRotateButtonVisibility(final boolean visible) {
+    public int setRotateButtonVisibility(boolean visible) {
         // Never show if a11y is visible
         final boolean adjVisible = visible && !mShowAccessibilityButton;
         final int vis = adjVisible ? View.VISIBLE : View.INVISIBLE;
@@ -881,13 +841,9 @@
         mShowRotateButton = visible;
 
         // Stop any active animations if hidden
-        if (!visible) {
-            Drawable d = mRotateSuggestionIcon.getDrawable(0);
-            if (d instanceof AnimatedVectorDrawable) {
-                AnimatedVectorDrawable avd = (AnimatedVectorDrawable) d;
-                avd.clearAnimationCallbacks();
-                avd.reset();
-            }
+        if (!visible && mRotateSuggestionIcon.canAnimate()) {
+            mRotateSuggestionIcon.clearAnimationCallbacks();
+            mRotateSuggestionIcon.resetAnimation();
         }
 
         // Hide/restore other button visibility, if necessary
@@ -1082,7 +1038,7 @@
         super.onConfigurationChanged(newConfig);
         boolean uiCarModeChanged = updateCarMode(newConfig);
         updateTaskSwitchHelper();
-        updateIcons(getContext(), mConfiguration, newConfig);
+        updateIcons(mConfiguration, newConfig);
         updateRecentsIcon();
         mRecentsOnboarding.onConfigurationChanged(newConfig);
         if (uiCarModeChanged || mConfiguration.densityDpi != newConfig.densityDpi
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ShadowKeyDrawable.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ShadowKeyDrawable.java
deleted file mode 100644
index 8bd8048..0000000
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ShadowKeyDrawable.java
+++ /dev/null
@@ -1,242 +0,0 @@
-/*
- * Copyright (C) 2018 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 com.android.systemui.statusbar.phone;
-
-
-import android.graphics.Bitmap;
-import android.graphics.BlurMaskFilter;
-import android.graphics.BlurMaskFilter.Blur;
-import android.graphics.Canvas;
-import android.graphics.Color;
-import android.graphics.ColorFilter;
-import android.graphics.Paint;
-import android.graphics.PixelFormat;
-import android.graphics.Rect;
-import android.graphics.drawable.Drawable;
-
-/**
- * A drawable which adds shadow around a child drawable.
- */
-public class ShadowKeyDrawable extends Drawable {
-    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
-
-    private final ShadowDrawableState mState;
-
-    public ShadowKeyDrawable(Drawable d) {
-        this(d, new ShadowDrawableState());
-    }
-
-    private ShadowKeyDrawable(Drawable d, ShadowDrawableState state) {
-        mState = state;
-        if (d != null) {
-            mState.mBaseHeight = d.getIntrinsicHeight();
-            mState.mBaseWidth = d.getIntrinsicWidth();
-            mState.mChangingConfigurations = d.getChangingConfigurations();
-            mState.mChildState = d.getConstantState();
-        }
-    }
-
-    public void setRotation(float degrees) {
-        if (mState.mRotateDegrees != degrees) {
-            mState.mRotateDegrees = degrees;
-            mState.mLastDrawnBitmap = null;
-            invalidateSelf();
-        }
-    }
-
-    public void setTranslationX(float x) {
-        setTranslation(x, mState.mTranslationY);
-    }
-
-    public void setTranslationY(float y) {
-        setTranslation(mState.mTranslationX, y);
-    }
-
-    public void setTranslation(float x, float y) {
-        if (mState.mTranslationX != x || mState.mTranslationY != y) {
-            mState.mTranslationX = x;
-            mState.mTranslationY = y;
-            mState.mLastDrawnBitmap = null;
-            invalidateSelf();
-        }
-    }
-
-    public void setShadowProperties(int x, int y, int size, int color) {
-        if (mState.mShadowOffsetX != x || mState.mShadowOffsetY != y
-                || mState.mShadowSize != size || mState.mShadowColor != color) {
-            mState.mShadowOffsetX = x;
-            mState.mShadowOffsetY = y;
-            mState.mShadowSize = size;
-            mState.mShadowColor = color;
-            mState.mLastDrawnBitmap = null;
-            invalidateSelf();
-        }
-    }
-
-    public float getRotation() {
-        return mState.mRotateDegrees;
-    }
-
-    public float getTranslationX() {
-        return mState.mTranslationX;
-    }
-
-    public float getTranslationY() {
-        return mState.mTranslationY;
-    }
-
-    @Override
-    public void draw(Canvas canvas) {
-        Rect bounds = getBounds();
-        if (bounds.isEmpty()) {
-            return;
-        }
-
-        // If no cache or previous cached bitmap is hardware/software acceleration does not match
-        // the current canvas on draw then regenerate
-        if (mState.mLastDrawnBitmap == null
-                || mState.mIsHardwareBitmap != canvas.isHardwareAccelerated()) {
-            mState.mIsHardwareBitmap = canvas.isHardwareAccelerated();
-            regenerateBitmapCache();
-        }
-        canvas.drawBitmap(mState.mLastDrawnBitmap, null, bounds, mPaint);
-    }
-
-    @Override
-    public void setTint(int tintColor) {
-        super.setTint(tintColor);
-    }
-
-    @Override
-    public void setAlpha(int alpha) {
-        mPaint.setAlpha(alpha);
-        invalidateSelf();
-    }
-
-    @Override
-    public void setColorFilter(ColorFilter colorFilter) {
-        mPaint.setColorFilter(colorFilter);
-        invalidateSelf();
-    }
-
-    @Override
-    public ConstantState getConstantState() {
-        return mState;
-    }
-
-    @Override
-    public int getOpacity() {
-        return PixelFormat.TRANSLUCENT;
-    }
-
-    @Override
-    public int getIntrinsicHeight() {
-        return mState.mBaseHeight;
-    }
-
-    @Override
-    public int getIntrinsicWidth() {
-        return mState.mBaseWidth;
-    }
-
-    @Override
-    public boolean canApplyTheme() {
-        return mState.canApplyTheme();
-    }
-
-    private void regenerateBitmapCache() {
-        final int width = getIntrinsicWidth();
-        final int height = getIntrinsicHeight();
-        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
-        Canvas canvas = new Canvas(bitmap);
-        canvas.save();
-        final float radians = (float) (mState.mRotateDegrees * Math.PI / 180);
-
-        // Rotate canvas before drawing original drawable if no shadow
-        if (mState.mShadowSize == 0) {
-            canvas.rotate(mState.mRotateDegrees, width / 2, height / 2);
-        }
-
-        // Call mutate, so that the pixel allocation by the underlying vector drawable is cleared.
-        final Drawable d = mState.mChildState.newDrawable().mutate();
-        d.setBounds(0, 0, mState.mBaseWidth, mState.mBaseHeight);
-        canvas.translate(mState.mTranslationX, mState.mTranslationY);
-        d.draw(canvas);
-
-        if (mState.mShadowSize > 0) {
-            // Draws the shadow
-            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
-            paint.setMaskFilter(new BlurMaskFilter(mState.mShadowSize, Blur.NORMAL));
-            int[] offset = new int[2];
-
-            final Bitmap shadow = bitmap.extractAlpha(paint, offset);
-
-            paint.setMaskFilter(null);
-            paint.setColor(mState.mShadowColor);
-            bitmap.eraseColor(Color.TRANSPARENT);
-
-            canvas.rotate(mState.mRotateDegrees, width / 2, height / 2);
-
-            final float shadowOffsetX = (float) (Math.sin(radians) * mState.mShadowOffsetY
-                    + Math.cos(radians) * mState.mShadowOffsetX) - mState.mTranslationX;
-            final float shadowOffsetY = (float) (Math.cos(radians) * mState.mShadowOffsetY
-                    - Math.sin(radians) * mState.mShadowOffsetX) - mState.mTranslationY;
-
-            canvas.drawBitmap(shadow, offset[0] + shadowOffsetX, offset[1] + shadowOffsetY, paint);
-            d.draw(canvas);
-        }
-
-        if (mState.mIsHardwareBitmap) {
-            bitmap = bitmap.copy(Bitmap.Config.HARDWARE, false);
-        }
-
-        mState.mLastDrawnBitmap = bitmap;
-        canvas.restore();
-    }
-
-    private static class ShadowDrawableState extends ConstantState {
-        int mChangingConfigurations;
-        int mBaseWidth;
-        int mBaseHeight;
-        float mRotateDegrees;
-        float mTranslationX;
-        float mTranslationY;
-        int mShadowOffsetX;
-        int mShadowOffsetY;
-        int mShadowSize;
-        int mShadowColor;
-
-        boolean mIsHardwareBitmap;
-        Bitmap mLastDrawnBitmap;
-        ConstantState mChildState;
-
-        @Override
-        public Drawable newDrawable() {
-            return new ShadowKeyDrawable(null, this);
-        }
-
-        @Override
-        public int getChangingConfigurations() {
-            return mChangingConfigurations;
-        }
-
-        @Override
-        public boolean canApplyTheme() {
-            return true;
-        }
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
index 1085b06..6f64a563 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
@@ -65,7 +65,6 @@
         mLocalBluetoothManager = Dependency.get(LocalBluetoothManager.class);
         mBgHandler = new Handler(bgLooper);
         if (mLocalBluetoothManager != null) {
-            mLocalBluetoothManager.getEventManager().setReceiverHandler(mBgHandler);
             mLocalBluetoothManager.getEventManager().registerCallback(this);
             mLocalBluetoothManager.getProfileManager().addServiceListener(this);
             onBluetoothStateChanged(
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
index 8e31f31..945d9b9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonDrawable.java
@@ -16,23 +16,34 @@
 
 package com.android.systemui.statusbar.policy;
 
-import android.annotation.Nullable;
+import android.animation.ArgbEvaluator;
+import android.annotation.ColorInt;
+import android.annotation.DrawableRes;
 import android.content.Context;
 import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BlurMaskFilter;
+import android.graphics.BlurMaskFilter.Blur;
+import android.graphics.Canvas;
 import android.graphics.Color;
+import android.graphics.ColorFilter;
+import android.graphics.Paint;
+import android.graphics.PixelFormat;
+import android.graphics.PorterDuff.Mode;
+import android.graphics.PorterDuffColorFilter;
+import android.graphics.Rect;
+import android.graphics.drawable.AnimatedVectorDrawable;
 import android.graphics.drawable.Drawable;
-import android.graphics.drawable.LayerDrawable;
 import android.util.FloatProperty;
-import android.view.Gravity;
-
+import com.android.settingslib.Utils;
 import com.android.systemui.R;
-import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
 
 /**
- * Drawable for {@link KeyButtonView}s which contains an asset for both normal mode and light
- * navigation bar mode.
+ * Drawable for {@link KeyButtonView}s that supports tinting between two colors, rotation and shows
+ * a shadow. AnimatedVectorDrawable will only support tinting from intensities but has no support
+ * for shadows nor rotations.
  */
-public class KeyButtonDrawable extends LayerDrawable {
+public class KeyButtonDrawable extends Drawable {
 
     public static final FloatProperty<KeyButtonDrawable> KEY_DRAWABLE_ROTATE =
         new FloatProperty<KeyButtonDrawable>("KeyButtonRotation") {
@@ -60,83 +71,343 @@
             }
         };
 
-    private final boolean mHasDarkDrawable;
+    private final Paint mIconPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
+    private final Paint mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
+    private final ShadowDrawableState mState;
+    private AnimatedVectorDrawable mAnimatedDrawable;
 
-    public static KeyButtonDrawable create(Context lightContext, Drawable lightDrawable,
-            @Nullable Drawable darkDrawable, boolean hasShadow) {
-        if (darkDrawable != null) {
-            ShadowKeyDrawable light = new ShadowKeyDrawable(lightDrawable.mutate());
-            ShadowKeyDrawable dark = new ShadowKeyDrawable(darkDrawable.mutate());
-            if (hasShadow) {
-                // Only apply the shadow on the light drawable
-                Resources res = lightContext.getResources();
-                int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x);
-                int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y);
-                int radius = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_radius);
-                int color = lightContext.getColor(R.color.nav_key_button_shadow_color);
-                light.setShadowProperties(offsetX, offsetY, radius, color);
-            }
-            return new KeyButtonDrawable(new Drawable[] { light, dark });
-        } else {
-            return new KeyButtonDrawable(new Drawable[] {
-                    new ShadowKeyDrawable(lightDrawable.mutate()) });
-        }
+    public KeyButtonDrawable(Drawable d, @ColorInt int lightColor, @ColorInt int darkColor) {
+        this(d, new ShadowDrawableState(lightColor, darkColor,
+                d instanceof AnimatedVectorDrawable));
     }
 
-    protected KeyButtonDrawable(Drawable[] drawables) {
-        super(drawables);
-        for (int i = 0; i < drawables.length; i++) {
-            setLayerGravity(i, Gravity.CENTER);
+    private KeyButtonDrawable(Drawable d, ShadowDrawableState state) {
+        mState = state;
+        if (d != null) {
+            mState.mBaseHeight = d.getIntrinsicHeight();
+            mState.mBaseWidth = d.getIntrinsicWidth();
+            mState.mChangingConfigurations = d.getChangingConfigurations();
+            mState.mChildState = d.getConstantState();
         }
-        mutate();
-        mHasDarkDrawable = drawables.length > 1;
-        setDarkIntensity(0f);
+        if (canAnimate()) {
+            mAnimatedDrawable = (AnimatedVectorDrawable) mState.mChildState.newDrawable().mutate();
+            setDrawableBounds(mAnimatedDrawable);
+        }
     }
 
     public void setDarkIntensity(float intensity) {
-        if (!mHasDarkDrawable) {
-            return;
-        }
-        getDrawable(0).setAlpha((int) ((1 - intensity) * 255f));
-        getDrawable(1).setAlpha((int) (intensity * 255f));
-        invalidateSelf();
+        mState.mDarkIntensity = intensity;
+        final int color = (int) ArgbEvaluator.getInstance()
+                .evaluate(intensity, mState.mLightColor, mState.mDarkColor);
+        updateShadowAlpha();
+        setColorFilter(new PorterDuffColorFilter(color, Mode.SRC_ATOP));
     }
 
     public void setRotation(float degrees) {
-        if (getDrawable(0) instanceof ShadowKeyDrawable) {
-            ((ShadowKeyDrawable) getDrawable(0)).setRotation(degrees);
+        if (canAnimate()) {
+            // AnimatedVectorDrawables will not support rotation
+            return;
         }
-        if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) {
-            ((ShadowKeyDrawable) getDrawable(1)).setRotation(degrees);
+        if (mState.mRotateDegrees != degrees) {
+            mState.mRotateDegrees = degrees;
+            invalidateSelf();
         }
     }
 
+    public void setTranslationX(float x) {
+        setTranslation(x, mState.mTranslationY);
+    }
+
     public void setTranslationY(float y) {
-        if (getDrawable(0) instanceof ShadowKeyDrawable) {
-            ((ShadowKeyDrawable) getDrawable(0)).setTranslationY(y);
+        setTranslation(mState.mTranslationX, y);
+    }
+
+    public void setTranslation(float x, float y) {
+        if (mState.mTranslationX != x || mState.mTranslationY != y) {
+            mState.mTranslationX = x;
+            mState.mTranslationY = y;
+            invalidateSelf();
         }
-        if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) {
-            ((ShadowKeyDrawable) getDrawable(1)).setTranslationY(y);
+    }
+
+    public void setShadowProperties(int x, int y, int size, int color) {
+        if (canAnimate()) {
+            // AnimatedVectorDrawables will not support shadows
+            return;
         }
+        if (mState.mShadowOffsetX != x || mState.mShadowOffsetY != y
+                || mState.mShadowSize != size || mState.mShadowColor != color) {
+            mState.mShadowOffsetX = x;
+            mState.mShadowOffsetY = y;
+            mState.mShadowSize = size;
+            mState.mShadowColor = color;
+            mShadowPaint.setColorFilter(
+                    new PorterDuffColorFilter(mState.mShadowColor, Mode.SRC_ATOP));
+            updateShadowAlpha();
+            invalidateSelf();
+        }
+    }
+
+    @Override
+    public void setAlpha(int alpha) {
+        mState.mAlpha = alpha;
+        mIconPaint.setAlpha(alpha);
+        updateShadowAlpha();
+        invalidateSelf();
+    }
+
+    @Override
+    public void setColorFilter(ColorFilter colorFilter) {
+        mIconPaint.setColorFilter(colorFilter);
+        if (mAnimatedDrawable != null) {
+            mAnimatedDrawable.setColorFilter(colorFilter);
+        }
+        invalidateSelf();
+    }
+
+    public float getDarkIntensity() {
+        return mState.mDarkIntensity;
     }
 
     public float getRotation() {
-        if (getDrawable(0) instanceof ShadowKeyDrawable) {
-            return ((ShadowKeyDrawable) getDrawable(0)).getRotation();
-        }
-        if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) {
-            return ((ShadowKeyDrawable) getDrawable(1)).getRotation();
-        }
-        return 0;
+        return mState.mRotateDegrees;
+    }
+
+    public float getTranslationX() {
+        return mState.mTranslationX;
     }
 
     public float getTranslationY() {
-        if (getDrawable(0) instanceof ShadowKeyDrawable) {
-            return ((ShadowKeyDrawable) getDrawable(0)).getTranslationY();
+        return mState.mTranslationY;
+    }
+
+    @Override
+    public ConstantState getConstantState() {
+        return mState;
+    }
+
+    @Override
+    public int getOpacity() {
+        return PixelFormat.TRANSLUCENT;
+    }
+
+    @Override
+    public int getIntrinsicHeight() {
+        return mState.mBaseHeight + (mState.mShadowSize + Math.abs(mState.mShadowOffsetY)) * 2;
+    }
+
+    @Override
+    public int getIntrinsicWidth() {
+        return mState.mBaseWidth + (mState.mShadowSize + Math.abs(mState.mShadowOffsetX)) * 2;
+    }
+
+    public boolean canAnimate() {
+        return mState.mSupportsAnimation;
+    }
+
+    public void startAnimation() {
+        if (mAnimatedDrawable != null) {
+            mAnimatedDrawable.start();
         }
-        if (mHasDarkDrawable && getDrawable(1) instanceof ShadowKeyDrawable) {
-            return ((ShadowKeyDrawable) getDrawable(1)).getTranslationY();
+    }
+
+    public void resetAnimation() {
+        if (mAnimatedDrawable != null) {
+            mAnimatedDrawable.reset();
         }
-        return 0;
+    }
+
+    public void clearAnimationCallbacks() {
+        if (mAnimatedDrawable != null) {
+            mAnimatedDrawable.clearAnimationCallbacks();
+        }
+    }
+
+    @Override
+    public void draw(Canvas canvas) {
+        Rect bounds = getBounds();
+        if (bounds.isEmpty()) {
+            return;
+        }
+
+        if (mAnimatedDrawable != null) {
+            mAnimatedDrawable.draw(canvas);
+        } else {
+            // If no cache or previous cached bitmap is hardware/software acceleration does not
+            // match the current canvas on draw then regenerate
+            boolean hwBitmapChanged = mState.mIsHardwareBitmap != canvas.isHardwareAccelerated();
+            if (hwBitmapChanged) {
+                mState.mIsHardwareBitmap = canvas.isHardwareAccelerated();
+            }
+            if (mState.mLastDrawnIcon == null || hwBitmapChanged) {
+                regenerateBitmapIconCache();
+            }
+            canvas.save();
+            canvas.translate(mState.mTranslationX, mState.mTranslationY);
+            canvas.rotate(mState.mRotateDegrees, getIntrinsicWidth() / 2, getIntrinsicHeight() / 2);
+
+            if (mState.mShadowSize > 0) {
+                if (mState.mLastDrawnShadow == null || hwBitmapChanged) {
+                    regenerateBitmapShadowCache();
+                }
+
+                // Translate (with rotation offset) before drawing the shadow
+                final float radians = (float) (mState.mRotateDegrees * Math.PI / 180);
+                final float shadowOffsetX = (float) (Math.sin(radians) * mState.mShadowOffsetY
+                        + Math.cos(radians) * mState.mShadowOffsetX) - mState.mTranslationX;
+                final float shadowOffsetY = (float) (Math.cos(radians) * mState.mShadowOffsetY
+                        - Math.sin(radians) * mState.mShadowOffsetX) - mState.mTranslationY;
+                canvas.drawBitmap(mState.mLastDrawnShadow, shadowOffsetX, shadowOffsetY,
+                        mShadowPaint);
+            }
+            canvas.drawBitmap(mState.mLastDrawnIcon, null, bounds, mIconPaint);
+            canvas.restore();
+        }
+    }
+
+    @Override
+    public boolean canApplyTheme() {
+        return mState.canApplyTheme();
+    }
+
+    private void regenerateBitmapIconCache() {
+        final int width = getIntrinsicWidth();
+        final int height = getIntrinsicHeight();
+        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+        final Canvas canvas = new Canvas(bitmap);
+
+        // Call mutate, so that the pixel allocation by the underlying vector drawable is cleared.
+        final Drawable d = mState.mChildState.newDrawable().mutate();
+        setDrawableBounds(d);
+        d.draw(canvas);
+
+        if (mState.mIsHardwareBitmap) {
+            bitmap = bitmap.copy(Bitmap.Config.HARDWARE, false);
+        }
+        mState.mLastDrawnIcon = bitmap;
+    }
+
+    private void regenerateBitmapShadowCache() {
+        if (mState.mShadowSize == 0) {
+            // No shadow
+            mState.mLastDrawnIcon = null;
+            return;
+        }
+
+        final int width = getIntrinsicWidth();
+        final int height = getIntrinsicHeight();
+        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+        Canvas canvas = new Canvas(bitmap);
+
+        // Call mutate, so that the pixel allocation by the underlying vector drawable is cleared.
+        final Drawable d = mState.mChildState.newDrawable().mutate();
+        setDrawableBounds(d);
+        d.draw(canvas);
+
+        // Draws the shadow from original drawable
+        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
+        paint.setMaskFilter(new BlurMaskFilter(mState.mShadowSize, Blur.NORMAL));
+        int[] offset = new int[2];
+        final Bitmap shadow = bitmap.extractAlpha(paint, offset);
+        paint.setMaskFilter(null);
+        bitmap.eraseColor(Color.TRANSPARENT);
+        canvas.drawBitmap(shadow, offset[0], offset[1], paint);
+
+        if (mState.mIsHardwareBitmap) {
+            bitmap = bitmap.copy(Bitmap.Config.HARDWARE, false);
+        }
+        mState.mLastDrawnShadow = bitmap;
+    }
+
+    /**
+     * Set the alpha of the shadow. As dark intensity increases, drop the alpha of the shadow since
+     * dark color and shadow should not be visible at the same time.
+     */
+    private void updateShadowAlpha() {
+        // Update the color from the original color's alpha as the max
+        int alpha = Color.alpha(mState.mShadowColor);
+        mShadowPaint.setAlpha(
+                Math.round(alpha * (mState.mAlpha / 255f) * (1 - mState.mDarkIntensity)));
+    }
+
+    /**
+     * Prevent shadow clipping by offsetting the drawable bounds by the shadow and its offset
+     * @param d the drawable to set the bounds
+     */
+    private void setDrawableBounds(Drawable d) {
+        final int offsetX = mState.mShadowSize + Math.abs(mState.mShadowOffsetX);
+        final int offsetY = mState.mShadowSize + Math.abs(mState.mShadowOffsetY);
+        d.setBounds(offsetX, offsetY, getIntrinsicWidth() - offsetX,
+                getIntrinsicHeight() - offsetY);
+    }
+
+    private static class ShadowDrawableState extends ConstantState {
+        int mChangingConfigurations;
+        int mBaseWidth;
+        int mBaseHeight;
+        float mRotateDegrees;
+        float mTranslationX;
+        float mTranslationY;
+        int mShadowOffsetX;
+        int mShadowOffsetY;
+        int mShadowSize;
+        int mShadowColor;
+        float mDarkIntensity;
+        int mAlpha;
+
+        boolean mIsHardwareBitmap;
+        Bitmap mLastDrawnIcon;
+        Bitmap mLastDrawnShadow;
+        ConstantState mChildState;
+
+        final int mLightColor;
+        final int mDarkColor;
+        final boolean mSupportsAnimation;
+
+        public ShadowDrawableState(@ColorInt int lightColor, @ColorInt int darkColor,
+                boolean animated) {
+            mLightColor = lightColor;
+            mDarkColor = darkColor;
+            mSupportsAnimation = animated;
+            mAlpha = 255;
+        }
+
+        @Override
+        public Drawable newDrawable() {
+            return new KeyButtonDrawable(null, this);
+        }
+
+        @Override
+        public int getChangingConfigurations() {
+            return mChangingConfigurations;
+        }
+
+        @Override
+        public boolean canApplyTheme() {
+            return true;
+        }
+    }
+
+    public static KeyButtonDrawable create(Context lightContext, Context darkContext,
+        @DrawableRes int iconResId, boolean hasShadow) {
+        return create(lightContext,
+            Utils.getColorAttrDefaultColor(lightContext, R.attr.singleToneColor),
+            Utils.getColorAttrDefaultColor(darkContext, R.attr.singleToneColor),
+            iconResId, hasShadow);
+    }
+
+    public static KeyButtonDrawable create(Context context, @ColorInt int lightColor,
+        @ColorInt int darkColor, @DrawableRes int iconResId, boolean hasShadow) {
+        final KeyButtonDrawable drawable = new KeyButtonDrawable(context.getDrawable(iconResId),
+            lightColor, darkColor);
+        if (hasShadow) {
+            final Resources res = context.getResources();
+            int offsetX = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_x);
+            int offsetY = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_offset_y);
+            int radius = res.getDimensionPixelSize(R.dimen.nav_key_button_shadow_radius);
+            int color = context.getColor(R.color.nav_key_button_shadow_color);
+            drawable.setShadowProperties(offsetX, offsetY, radius, color);
+        }
+        return drawable;
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/TintedKeyButtonDrawable.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/TintedKeyButtonDrawable.java
deleted file mode 100644
index 30a5cc84..0000000
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/TintedKeyButtonDrawable.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * Copyright (C) 2018 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 com.android.systemui.statusbar.policy;
-
-import android.annotation.ColorInt;
-import android.graphics.Color;
-import android.graphics.drawable.Drawable;
-
-import com.android.internal.graphics.ColorUtils;
-import com.android.systemui.statusbar.phone.ShadowKeyDrawable;
-
-/**
- * Drawable for {@link KeyButtonView}s which contains a single asset and colors for light and dark
- * navigation bar mode.
- */
-public class TintedKeyButtonDrawable extends KeyButtonDrawable {
-
-    private final int mLightColor;
-    private final int mDarkColor;
-
-    public static final float DARK_INTENSITY_NOT_SET = -1f;
-    private float mDarkIntensity = DARK_INTENSITY_NOT_SET;
-
-    public static TintedKeyButtonDrawable create(Drawable drawable, @ColorInt int lightColor,
-            @ColorInt int darkColor) {
-        return new TintedKeyButtonDrawable(new Drawable[] { drawable },
-                lightColor, darkColor);
-    }
-
-    private TintedKeyButtonDrawable(Drawable[] drawables, int lightColor, int darkColor){
-        super(drawables);
-        mLightColor = lightColor;
-        mDarkColor = darkColor;
-        setDarkIntensity(0f); // Set initial coloration
-    }
-
-    @Override
-    public void setDarkIntensity(float intensity) {
-        // Duplicate intensity scaling from KeyButtonDrawable
-        mDarkIntensity = intensity;
-
-        // Dark and light colors may have an alpha component
-        final int intermediateColor = ColorUtils.compositeColors(
-                blendAlpha(mDarkColor, intensity),
-                blendAlpha(mLightColor, (1f - intensity)));
-
-        getDrawable(0).setTint(intermediateColor);
-        invalidateSelf();
-    }
-
-    private int blendAlpha(int color, float alpha) {
-        final float newAlpha = alpha < 0f ? 0f : (alpha > 1f ? 1f : alpha);
-        final float colorAlpha = Color.alpha(color) / 255f;
-        final int alphaInt = (int) (255 * newAlpha * colorAlpha); // Blend by multiplying
-        // Ensure alpha is clamped [0-255] or ColorUtils will crash
-        return ColorUtils.setAlphaComponent(color, alphaInt);
-    }
-
-    public boolean isDarkIntensitySet() {
-        return mDarkIntensity != DARK_INTENSITY_NOT_SET;
-    }
-
-    public float getDarkIntensity() {
-        return mDarkIntensity;
-    }
-}
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 02a62ff..4e4e208 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -19,37 +19,9 @@
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
+
 import static java.lang.annotation.RetentionPolicy.SOURCE;
 
-import com.android.internal.annotations.GuardedBy;
-import com.android.internal.content.PackageMonitor;
-import com.android.internal.inputmethod.IInputContentUriToken;
-import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
-import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
-import com.android.internal.inputmethod.InputMethodUtils;
-import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
-import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
-import com.android.internal.notification.SystemNotificationChannels;
-import com.android.internal.os.HandlerCaller;
-import com.android.internal.os.SomeArgs;
-import com.android.internal.os.TransferPipe;
-import com.android.internal.util.DumpUtils;
-import com.android.internal.util.FastXmlSerializer;
-import com.android.internal.util.IndentingPrintWriter;
-import com.android.internal.view.IInputContext;
-import com.android.internal.view.IInputMethod;
-import com.android.internal.view.IInputMethodClient;
-import com.android.internal.view.IInputMethodManager;
-import com.android.internal.view.IInputMethodSession;
-import com.android.internal.view.IInputSessionCallback;
-import com.android.internal.view.InputBindResult;
-import com.android.internal.view.InputMethodClient;
-import com.android.server.statusbar.StatusBarManagerService;
-
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlSerializer;
-
 import android.Manifest;
 import android.accessibilityservice.AccessibilityService;
 import android.annotation.AnyThread;
@@ -60,7 +32,6 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.RequiresPermission;
-import android.annotation.TestApi;
 import android.annotation.UserIdInt;
 import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
@@ -102,8 +73,8 @@
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.IInterface;
-import android.os.Message;
 import android.os.LocaleList;
+import android.os.Message;
 import android.os.Parcel;
 import android.os.Process;
 import android.os.RemoteException;
@@ -144,7 +115,6 @@
 import android.view.inputmethod.InputMethod;
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodManager;
-import android.view.inputmethod.InputMethodManagerInternal;
 import android.view.inputmethod.InputMethodSubtype;
 import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
 import android.widget.ArrayAdapter;
@@ -154,8 +124,37 @@
 import android.widget.Switch;
 import android.widget.TextView;
 
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.content.PackageMonitor;
+import com.android.internal.inputmethod.IInputContentUriToken;
+import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
+import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
+import com.android.internal.inputmethod.InputMethodUtils;
+import com.android.internal.inputmethod.InputMethodUtils.InputMethodSettings;
+import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
+import com.android.internal.notification.SystemNotificationChannels;
+import com.android.internal.os.HandlerCaller;
+import com.android.internal.os.SomeArgs;
+import com.android.internal.os.TransferPipe;
+import com.android.internal.util.DumpUtils;
+import com.android.internal.util.FastXmlSerializer;
+import com.android.internal.util.IndentingPrintWriter;
+import com.android.internal.view.IInputContext;
+import com.android.internal.view.IInputMethod;
+import com.android.internal.view.IInputMethodClient;
+import com.android.internal.view.IInputMethodManager;
+import com.android.internal.view.IInputMethodSession;
+import com.android.internal.view.IInputSessionCallback;
+import com.android.internal.view.InputBindResult;
+import com.android.internal.view.InputMethodClient;
+import com.android.server.inputmethod.InputMethodManagerInternal;
+import com.android.server.statusbar.StatusBarManagerService;
 import com.android.server.wm.WindowManagerInternal;
 
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlSerializer;
+
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileInputStream;
@@ -205,6 +204,8 @@
     static final int MSG_START_INPUT = 2000;
     static final int MSG_START_VR_INPUT = 2010;
 
+    static final int MSG_ADD_CLIENT = 2980;
+    static final int MSG_REMOVE_CLIENT = 2990;
     static final int MSG_UNBIND_CLIENT = 3000;
     static final int MSG_BIND_CLIENT = 3010;
     static final int MSG_SET_ACTIVE = 3020;
@@ -1713,22 +1714,13 @@
         }
     }
 
-    @Override
-    public void addClient(IInputMethodClient client, IInputContext inputContext, int uid, int pid) {
-        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
-            throw new SecurityException("Only system process can call this method.");
-        }
+    void addClient(ClientState clientState) {
         synchronized (mMethodMap) {
-            mClients.put(client.asBinder(), new ClientState(client,
-                    inputContext, uid, pid));
+            mClients.put(clientState.client.asBinder(), clientState);
         }
     }
 
-    @Override
-    public void removeClient(IInputMethodClient client) {
-        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
-            throw new SecurityException("Only system process can call this method.");
-        }
+    void removeClient(IInputMethodClient client) {
         synchronized (mMethodMap) {
             ClientState cs = mClients.remove(client.asBinder());
             if (cs != null) {
@@ -3415,6 +3407,15 @@
             }
 
             // ---------------------------------------------------------
+            case MSG_ADD_CLIENT:
+                addClient((ClientState) msg.obj);
+                return true;
+
+            case MSG_REMOVE_CLIENT:
+                removeClient((IInputMethodClient) msg.obj);
+                return true;
+
+            // ---------------------------------------------------------
 
             case MSG_UNBIND_CLIENT:
                 try {
@@ -4405,7 +4406,7 @@
         }
     }
 
-    private static final class LocalServiceImpl implements InputMethodManagerInternal {
+    private static final class LocalServiceImpl extends InputMethodManagerInternal {
         @NonNull
         private final Handler mHandler;
 
@@ -4414,6 +4415,18 @@
         }
 
         @Override
+        public void addClient(IInputMethodClient client, IInputContext inputContext, int uid,
+                int pid) {
+            mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_CLIENT,
+                    new ClientState(client, inputContext, uid, pid)));
+        }
+
+        @Override
+        public void removeClient(IInputMethodClient client) {
+            mHandler.sendMessage(mHandler.obtainMessage(MSG_REMOVE_CLIENT, client));
+        }
+
+        @Override
         public void setInteractive(boolean interactive) {
             // Do everything in handler so as not to block the caller.
             mHandler.sendMessage(mHandler.obtainMessage(MSG_SET_INTERACTIVE,
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 5bf6892..e5aa3f4 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -1628,7 +1628,7 @@
             // Once the apps have become associated, if one of them is caller is ephemeral
             // the target app should now be able to see the calling app
             mAm.grantEphemeralAccessLocked(callerApp.userId, service,
-                    s.appInfo.uid, UserHandle.getAppId(callerApp.uid));
+                    UserHandle.getAppId(s.appInfo.uid), UserHandle.getAppId(callerApp.uid));
 
             AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
             ConnectionRecord c = new ConnectionRecord(b, activity,
@@ -2581,8 +2581,8 @@
                 mAm.mUgmInternal.grantUriPermissionUncheckedFromIntent(si.neededGrants,
                         si.getUriPermissionsLocked());
             }
-            mAm.grantEphemeralAccessLocked(r.userId, si.intent,
-                    r.appInfo.uid, UserHandle.getAppId(si.callingId));
+            mAm.grantEphemeralAccessLocked(r.userId, si.intent, UserHandle.getAppId(r.appInfo.uid),
+                    UserHandle.getAppId(si.callingId));
             bumpServiceExecutingLocked(r, execInFg, "start");
             if (!oomAdjusted) {
                 oomAdjusted = true;
diff --git a/services/core/java/com/android/server/am/ActivityDisplay.java b/services/core/java/com/android/server/am/ActivityDisplay.java
index 73ffd5c..3568a47 100644
--- a/services/core/java/com/android/server/am/ActivityDisplay.java
+++ b/services/core/java/com/android/server/am/ActivityDisplay.java
@@ -31,6 +31,7 @@
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.Display.FLAG_PRIVATE;
 import static android.view.Display.REMOVE_MODE_DESTROY_CONTENT;
+
 import static com.android.server.am.ActivityDisplayProto.CONFIGURATION_CONTAINER;
 import static com.android.server.am.ActivityDisplayProto.FOCUSED_STACK_ID;
 import static com.android.server.am.ActivityDisplayProto.ID;
@@ -43,7 +44,6 @@
 import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.am.ActivityStackSupervisor.TAG_STATES;
 
-import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityOptions;
 import android.app.WindowConfiguration;
@@ -103,6 +103,12 @@
 
     private boolean mSleeping;
 
+    /**
+     * The display is removed from the system and we are just waiting for all activities on it to be
+     * finished before removing this object.
+     */
+    private boolean mRemoved;
+
     // Cached reference to some special stacks we tend to get a lot so we don't need to loop
     // through the list to find them.
     private ActivityStack mHomeStack = null;
@@ -155,6 +161,7 @@
                 + " from displayId=" + mDisplayId);
         mStacks.remove(stack);
         removeStackReferenceIfNeeded(stack);
+        releaseSelfIfNeeded();
         mSupervisor.mService.updateSleepIfNeededLocked();
         onStackOrderChanged();
     }
@@ -484,16 +491,11 @@
         final int windowingMode = stack.getWindowingMode();
 
         if (activityType == ACTIVITY_TYPE_HOME) {
-            // TODO(b/111363427) Rollback to throws exceptions once we figure out how to properly
-            // deal with home type stack when external display removed
             if (mHomeStack != null && mHomeStack != stack) {
-                // throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
-                //         + mHomeStack + " already exist on display=" + this + " stack=" + stack);
-                Slog.e(TAG, "addStackReferenceIfNeeded: home stack="
+                throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
                         + mHomeStack + " already exist on display=" + this + " stack=" + stack);
-            } else {
-                mHomeStack = stack;
             }
+            mHomeStack = stack;
         } else if (activityType == ACTIVITY_TYPE_RECENTS) {
             if (mRecentsStack != null && mRecentsStack != stack) {
                 throw new IllegalArgumentException("addStackReferenceIfNeeded: recents stack="
@@ -796,28 +798,47 @@
         return false;
     }
 
+    /**
+     * @see #mRemoved
+     */
+    boolean isRemoved() {
+        return mRemoved;
+    }
+
     void remove() {
         final boolean destroyContentOnRemoval = shouldDestroyContentOnRemove();
-        while (getChildCount() > 0) {
-            final ActivityStack stack = getChildAt(0);
-            if (destroyContentOnRemoval) {
-                // Override the stack configuration to make it equal to the current applied one, so
-                // that we don't accidentally report configuration change to activities that are
-                // going to be finished.
-                stack.onOverrideConfigurationChanged(stack.getConfiguration());
-                mSupervisor.moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY,
-                        false /* onTop */);
+
+        // Stacks could be reparented from the removed display to other display. While
+        // reparenting the last stack of the removed display, the remove display is ready to be
+        // released (no more ActivityStack). But, we cannot release it at that moment or the
+        // related WindowContainer and WindowContainerController will also be removed. So, we
+        // set display as removed after reparenting stack finished.
+        for (int i = mStacks.size() - 1; i >= 0; --i) {
+            final ActivityStack stack = mStacks.get(i);
+            // Always finish non-standard type stacks.
+            if (destroyContentOnRemoval || !stack.isActivityTypeStandardOrUndefined()) {
                 stack.finishAllActivitiesLocked(true /* immediately */);
             } else {
-                // Moving all tasks to fullscreen stack, because it's guaranteed to be
-                // a valid launch stack for all activities. This way the task history from
-                // external display will be preserved on primary after move.
-                mSupervisor.moveTasksToFullscreenStackLocked(stack, true /* onTop */);
+                // If default display is in split-window mode, set windowing mode of the stack to
+                // split-screen secondary. Otherwise, set the windowing mode to undefined by
+                // default to let stack inherited the windowing mode from the new display.
+                int windowingMode = mSupervisor.getDefaultDisplay().hasSplitScreenPrimaryStack()
+                        ? WINDOWING_MODE_SPLIT_SCREEN_SECONDARY : WINDOWING_MODE_UNDEFINED;
+                mSupervisor.moveStackToDisplayLocked(stack.mStackId, DEFAULT_DISPLAY, true);
+                stack.setWindowingMode(windowingMode);
             }
         }
+        mRemoved = true;
 
-        mWindowContainerController.removeContainer();
-        mWindowContainerController = null;
+        releaseSelfIfNeeded();
+    }
+
+    private void releaseSelfIfNeeded() {
+        if (mStacks.isEmpty() && mRemoved) {
+            mWindowContainerController.removeContainer();
+            mWindowContainerController = null;
+            mSupervisor.releaseActivityDisplayLocked(mDisplayId);
+        }
     }
 
     /** Update and get all UIDs that are present on the display and have access to it. */
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 1bc0524..fb0d9ad 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -8239,7 +8239,8 @@
             checkTime(startTime, "getContentProviderImpl: done!");
 
             grantEphemeralAccessLocked(userId, null /*intent*/,
-                    cpi.applicationInfo.uid, UserHandle.getAppId(Binder.getCallingUid()));
+                    UserHandle.getAppId(cpi.applicationInfo.uid),
+                    UserHandle.getAppId(Binder.getCallingUid()));
         }
 
         // Wait for the provider to be published...
@@ -17059,8 +17060,8 @@
             activeInstr.mUiAutomationConnection = uiAutomationConnection;
             activeInstr.mResultClass = className;
 
-            boolean disableHiddenApiChecks =
-                    (flags & INSTRUMENTATION_FLAG_DISABLE_HIDDEN_API_CHECKS) != 0;
+            boolean disableHiddenApiChecks = ai.usesNonSdkApi
+                    || (flags & INSTRUMENTATION_FLAG_DISABLE_HIDDEN_API_CHECKS) != 0;
             if (disableHiddenApiChecks) {
                 enforceCallingPermission(android.Manifest.permission.DISABLE_HIDDEN_API_CHECKS,
                         "disable hidden API checks");
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index fbf2855..78fef65 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -36,6 +36,15 @@
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.Display.FLAG_CAN_SHOW_WITH_INSECURE_KEYGUARD;
 import static android.view.Display.INVALID_DISPLAY;
+import static android.view.WindowManager.TRANSIT_ACTIVITY_CLOSE;
+import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
+import static android.view.WindowManager.TRANSIT_CRASHING_ACTIVITY_CLOSE;
+import static android.view.WindowManager.TRANSIT_NONE;
+import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
+import static android.view.WindowManager.TRANSIT_TASK_OPEN;
+import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
+import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
+import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
 
 import static com.android.server.am.ActivityDisplay.POSITION_BOTTOM;
 import static com.android.server.am.ActivityDisplay.POSITION_TOP;
@@ -82,10 +91,6 @@
 import static com.android.server.am.ActivityStack.ActivityState.RESUMED;
 import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
 import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
-import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
-import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
-import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
-import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
 import static com.android.server.am.ActivityStackProto.BOUNDS;
 import static com.android.server.am.ActivityStackProto.CONFIGURATION_CONTAINER;
 import static com.android.server.am.ActivityStackProto.DISPLAY_ID;
@@ -93,15 +98,10 @@
 import static com.android.server.am.ActivityStackProto.ID;
 import static com.android.server.am.ActivityStackProto.RESUMED_ACTIVITY;
 import static com.android.server.am.ActivityStackProto.TASKS;
-import static android.view.WindowManager.TRANSIT_ACTIVITY_CLOSE;
-import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
-import static android.view.WindowManager.TRANSIT_CRASHING_ACTIVITY_CLOSE;
-import static android.view.WindowManager.TRANSIT_NONE;
-import static android.view.WindowManager.TRANSIT_TASK_CLOSE;
-import static android.view.WindowManager.TRANSIT_TASK_OPEN;
-import static android.view.WindowManager.TRANSIT_TASK_OPEN_BEHIND;
-import static android.view.WindowManager.TRANSIT_TASK_TO_BACK;
-import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
+import static com.android.server.am.ActivityStackSupervisor.FindTaskResult;
+import static com.android.server.am.ActivityStackSupervisor.PAUSE_IMMEDIATELY;
+import static com.android.server.am.ActivityStackSupervisor.PRESERVE_WINDOWS;
+import static com.android.server.am.ActivityStackSupervisor.REMOVE_FROM_RECENTS;
 
 import static java.lang.Integer.MAX_VALUE;
 
@@ -115,12 +115,12 @@
 import android.app.WindowConfiguration.WindowingMode;
 import android.app.servertransaction.ActivityResultItem;
 import android.app.servertransaction.ClientTransaction;
-import android.app.servertransaction.NewIntentItem;
-import android.app.servertransaction.WindowVisibilityItem;
 import android.app.servertransaction.DestroyActivityItem;
+import android.app.servertransaction.NewIntentItem;
 import android.app.servertransaction.PauseActivityItem;
 import android.app.servertransaction.ResumeActivityItem;
 import android.app.servertransaction.StopActivityItem;
+import android.app.servertransaction.WindowVisibilityItem;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
@@ -5191,7 +5191,7 @@
             if (isAttached()) {
                 getDisplay().positionChildAtBottom(this);
             }
-            if (!isActivityTypeHome()) {
+            if (!isActivityTypeHome() || getDisplay().isRemoved()) {
                 remove();
             }
         }
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 9809bfa..4cfcbee 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -48,6 +48,7 @@
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.Display.INVALID_DISPLAY;
 import static android.view.Display.TYPE_VIRTUAL;
+import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
 
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_FOCUS;
@@ -81,13 +82,6 @@
 import static com.android.server.am.ActivityStack.ActivityState.STOPPED;
 import static com.android.server.am.ActivityStack.ActivityState.STOPPING;
 import static com.android.server.am.ActivityStack.REMOVE_TASK_MODE_MOVING;
-import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
-import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
-import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
-import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
-import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
-import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
-import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
 import static com.android.server.am.ActivityStackSupervisorProto.CONFIGURATION_CONTAINER;
 import static com.android.server.am.ActivityStackSupervisorProto.DISPLAYS;
 import static com.android.server.am.ActivityStackSupervisorProto.FOCUSED_STACK_ID;
@@ -95,7 +89,13 @@
 import static com.android.server.am.ActivityStackSupervisorProto.KEYGUARD_CONTROLLER;
 import static com.android.server.am.ActivityStackSupervisorProto.PENDING_ACTIVITIES;
 import static com.android.server.am.ActivityStackSupervisorProto.RESUMED_ACTIVITY;
-import static android.view.WindowManager.TRANSIT_DOCK_TASK_FROM_RECENTS;
+import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE_PRIV;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
+import static com.android.server.am.TaskRecord.REPARENT_KEEP_STACK_AT_FRONT;
+import static com.android.server.am.TaskRecord.REPARENT_LEAVE_STACK_IN_PLACE;
+import static com.android.server.am.TaskRecord.REPARENT_MOVE_STACK_TO_FRONT;
 
 import static java.lang.Integer.MAX_VALUE;
 
@@ -109,8 +109,6 @@
 import android.app.ActivityManager.RunningTaskInfo;
 import android.app.ActivityManager.StackInfo;
 import android.app.ActivityManagerInternal;
-import com.android.internal.util.function.pooled.PooledLambda;
-import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
 import android.app.ActivityOptions;
 import android.app.AppOpsManager;
 import android.app.ProfilerInfo;
@@ -170,11 +168,13 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.content.ReferrerIntent;
-import com.android.internal.os.logging.MetricsLoggerWrapper;
 import com.android.internal.os.TransferPipe;
+import com.android.internal.os.logging.MetricsLoggerWrapper;
 import com.android.internal.util.ArrayUtils;
+import com.android.internal.util.function.pooled.PooledLambda;
 import com.android.server.LocalServices;
 import com.android.server.am.ActivityStack.ActivityState;
+import com.android.server.wm.ActivityTaskManagerInternal.SleepToken;
 import com.android.server.wm.ConfigurationContainer;
 import com.android.server.wm.PinnedStackWindowController;
 import com.android.server.wm.WindowManagerService;
@@ -1862,7 +1862,7 @@
         }
 
         final ActivityDisplay activityDisplay = getActivityDisplayOrCreateLocked(launchDisplayId);
-        if (activityDisplay == null) {
+        if (activityDisplay == null || activityDisplay.isRemoved()) {
             Slog.w(TAG, "Launch on display check: display not found");
             return false;
         }
@@ -4359,11 +4359,14 @@
             activityDisplay.remove();
 
             releaseSleepTokens(activityDisplay);
-
-            mActivityDisplays.remove(displayId);
         }
     }
 
+    void releaseActivityDisplayLocked(int displayId) {
+        mActivityDisplays.remove(displayId);
+    }
+
+
     private void handleDisplayChanged(int displayId) {
         synchronized (mService.mGlobalLock) {
             ActivityDisplay activityDisplay = mActivityDisplays.get(displayId);
diff --git a/services/core/java/com/android/server/am/ActivityStarter.java b/services/core/java/com/android/server/am/ActivityStarter.java
index dcf9344..eb41fe7 100644
--- a/services/core/java/com/android/server/am/ActivityStarter.java
+++ b/services/core/java/com/android/server/am/ActivityStarter.java
@@ -1469,7 +1469,7 @@
         mService.mUgmInternal.grantUriPermissionFromIntent(mCallingUid, mStartActivity.packageName,
                 mIntent, mStartActivity.getUriPermissionsLocked(), mStartActivity.userId);
         mService.mAm.grantEphemeralAccessLocked(mStartActivity.userId, mIntent,
-                mStartActivity.appInfo.uid, UserHandle.getAppId(mCallingUid));
+                UserHandle.getAppId(mStartActivity.appInfo.uid), UserHandle.getAppId(mCallingUid));
         if (newTask) {
             EventLog.writeEvent(EventLogTags.AM_CREATE_TASK, mStartActivity.userId,
                     mStartActivity.getTask().taskId);
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index 84bdbba..d16c277 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -1126,7 +1126,9 @@
     }
 
     public String[] getTetheredDhcpRanges() {
-        return mConfig.dhcpRanges;
+        // TODO: this is only valid for the old DHCP server. Latest search suggests it is only used
+        // by WifiP2pServiceImpl to start dnsmasq: remove/deprecate after migrating callers.
+        return mConfig.legacyDhcpRanges;
     }
 
     public String[] getErroredIfaces() {
@@ -1297,13 +1299,17 @@
                 return false;
             }
             // TODO: Randomize DHCPv4 ranges, especially in hotspot mode.
+            // Legacy DHCP server is disabled if passed an empty ranges array
+            final String[] dhcpRanges = cfg.enableLegacyDhcpServer
+                    ? cfg.legacyDhcpRanges
+                    : new String[0];
             try {
                 // TODO: Find a more accurate method name (startDHCPv4()?).
-                mNMService.startTethering(cfg.dhcpRanges);
+                mNMService.startTethering(dhcpRanges);
             } catch (Exception e) {
                 try {
                     mNMService.stopTethering();
-                    mNMService.startTethering(cfg.dhcpRanges);
+                    mNMService.startTethering(dhcpRanges);
                 } catch (Exception ee) {
                     mLog.e(ee);
                     transitionTo(mStartTetheringErrorState);
@@ -1972,7 +1978,7 @@
         final TetherState tetherState = new TetherState(
                 new TetherInterfaceStateMachine(
                     iface, mLooper, interfaceType, mLog, mNMService, mStatsService,
-                    makeControlCallback(iface), mDeps));
+                    makeControlCallback(iface), mConfig.enableLegacyDhcpServer, mDeps));
         mTetherStates.put(iface, tetherState);
         tetherState.stateMachine.start();
     }
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java b/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
index 9e6b659..5accb45 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
@@ -16,6 +16,7 @@
 
 package com.android.server.connectivity.tethering;
 
+import static android.net.NetworkUtils.numericToInetAddress;
 import static android.net.util.NetworkConstants.asByte;
 import static android.net.util.NetworkConstants.FF;
 import static android.net.util.NetworkConstants.RFC7421_PREFIX_LENGTH;
@@ -27,8 +28,9 @@
 import android.net.IpPrefix;
 import android.net.LinkAddress;
 import android.net.LinkProperties;
-import android.net.NetworkUtils;
 import android.net.RouteInfo;
+import android.net.dhcp.DhcpServer;
+import android.net.dhcp.DhcpServingParams;
 import android.net.ip.InterfaceController;
 import android.net.ip.RouterAdvertisementDaemon;
 import android.net.ip.RouterAdvertisementDaemon.RaParams;
@@ -49,6 +51,7 @@
 import com.android.internal.util.State;
 import com.android.internal.util.StateMachine;
 
+import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
@@ -73,6 +76,13 @@
     private static final String WIFI_HOST_IFACE_ADDR = "192.168.43.1";
     private static final int WIFI_HOST_IFACE_PREFIX_LENGTH = 24;
 
+    // TODO: have PanService use some visible version of this constant
+    private static final String BLUETOOTH_IFACE_ADDR = "192.168.44.1";
+    private static final int BLUETOOTH_DHCP_PREFIX_LENGTH = 24;
+
+    // TODO: have this configurable
+    private static final int DHCP_LEASE_TIME_SECS = 3600;
+
     private final static String TAG = "TetherInterfaceSM";
     private final static boolean DBG = false;
     private final static boolean VDBG = false;
@@ -119,6 +129,7 @@
     private final String mIfaceName;
     private final int mInterfaceType;
     private final LinkProperties mLinkProperties;
+    private final boolean mUsingLegacyDhcp;
 
     private final TetheringDependencies mDeps;
 
@@ -134,12 +145,13 @@
     // Advertisements (otherwise, we do not add them to mLinkProperties at all).
     private LinkProperties mLastIPv6LinkProperties;
     private RouterAdvertisementDaemon mRaDaemon;
+    private DhcpServer mDhcpServer;
     private RaParams mLastRaParams;
 
     public TetherInterfaceStateMachine(
             String ifaceName, Looper looper, int interfaceType, SharedLog log,
             INetworkManagementService nMService, INetworkStatsService statsService,
-            IControlsTethering tetherController,
+            IControlsTethering tetherController, boolean usingLegacyDhcp,
             TetheringDependencies deps) {
         super(ifaceName, looper);
         mLog = log.forSubComponent(ifaceName);
@@ -151,6 +163,7 @@
         mIfaceName = ifaceName;
         mInterfaceType = interfaceType;
         mLinkProperties = new LinkProperties();
+        mUsingLegacyDhcp = usingLegacyDhcp;
         mDeps = deps;
         resetLinkProperties();
         mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
@@ -188,6 +201,53 @@
 
     private boolean startIPv4() { return configureIPv4(true); }
 
+    private boolean startDhcp(Inet4Address addr, int prefixLen) {
+        if (mUsingLegacyDhcp) {
+            return true;
+        }
+
+        final InterfaceParams ifaceParams = mDeps.getInterfaceParams(mIfaceName);
+        if (ifaceParams == null) {
+            Log.e(TAG, "Failed to find interface params for DHCPv4");
+            return false;
+        }
+        final DhcpServingParams params;
+        try {
+            params = new DhcpServingParams.Builder()
+                    .setDefaultRouters(addr)
+                    .setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS)
+                    .setDnsServers(addr)
+                    .setServerAddr(new LinkAddress(addr, prefixLen))
+                    .setMetered(true)
+                    .build();
+            // TODO: also advertise link MTU
+        } catch (DhcpServingParams.InvalidParameterException e) {
+            Log.e(TAG, "Invalid DHCP parameters", e);
+            return false;
+        }
+
+        mDhcpServer = mDeps.makeDhcpServer(getHandler().getLooper(), ifaceParams, params,
+                mLog.forSubComponent("DHCP"));
+        mDhcpServer.start();
+        return true;
+    }
+
+    private void stopDhcp() {
+        if (mDhcpServer != null) {
+            mDhcpServer.stop();
+            mDhcpServer = null;
+        }
+    }
+
+    private boolean configureDhcp(boolean enable, Inet4Address addr, int prefixLen) {
+        if (enable) {
+            return startDhcp(addr, prefixLen);
+        } else {
+            stopDhcp();
+            return true;
+        }
+    }
+
     private void stopIPv4() {
         configureIPv4(false);
         // NOTE: All of configureIPv4() will be refactored out of existence
@@ -210,8 +270,9 @@
             ipAsString = getRandomWifiIPv4Address();
             prefixLen = WIFI_HOST_IFACE_PREFIX_LENGTH;
         } else {
-            // Nothing to do, BT does this elsewhere.
-            return true;
+            // BT configures the interface elsewhere: only start DHCP.
+            final Inet4Address srvAddr = (Inet4Address) numericToInetAddress(BLUETOOTH_IFACE_ADDR);
+            return configureDhcp(enabled, srvAddr, BLUETOOTH_DHCP_PREFIX_LENGTH);
         }
 
         final LinkAddress linkAddr;
@@ -222,7 +283,7 @@
                 return false;
             }
 
-            InetAddress addr = NetworkUtils.numericToInetAddress(ipAsString);
+            InetAddress addr = numericToInetAddress(ipAsString);
             linkAddr = new LinkAddress(addr, prefixLen);
             ifcg.setLinkAddress(linkAddr);
             if (mInterfaceType == ConnectivityManager.TETHERING_WIFI) {
@@ -239,6 +300,10 @@
             }
             ifcg.clearFlag("running");
             mNMService.setInterfaceConfig(mIfaceName, ifcg);
+
+            if (!configureDhcp(enabled, (Inet4Address) addr, prefixLen)) {
+                return false;
+            }
         } catch (Exception e) {
             mLog.e("Error configuring interface " + e);
             return false;
@@ -258,7 +323,7 @@
 
     private String getRandomWifiIPv4Address() {
         try {
-            byte[] bytes = NetworkUtils.numericToInetAddress(WIFI_HOST_IFACE_ADDR).getAddress();
+            byte[] bytes = numericToInetAddress(WIFI_HOST_IFACE_ADDR).getAddress();
             bytes[3] = getRandomSanitizedByte(DOUG_ADAMS, asByte(0), asByte(1), FF);
             return InetAddress.getByAddress(bytes).getHostAddress();
         } catch (Exception e) {
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java b/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
index dd9fe05..6699444 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetheringConfiguration.java
@@ -21,6 +21,8 @@
 import static android.net.ConnectivityManager.TYPE_MOBILE;
 import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
 import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
+import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER;
+
 import static com.android.internal.R.array.config_mobile_hotspot_provision_app;
 import static com.android.internal.R.array.config_tether_bluetooth_regexs;
 import static com.android.internal.R.array.config_tether_dhcp_range;
@@ -30,15 +32,16 @@
 import static com.android.internal.R.bool.config_tether_upstream_automatic;
 import static com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui;
 
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.Resources;
 import android.net.ConnectivityManager;
 import android.net.util.SharedLog;
+import android.provider.Settings;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.R;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -68,12 +71,13 @@
     public static final int DUN_REQUIRED = 1;
     public static final int DUN_UNSPECIFIED = 2;
 
+    // Default ranges used for the legacy DHCP server.
     // USB is  192.168.42.1 and 255.255.255.0
     // Wifi is 192.168.43.1 and 255.255.255.0
     // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
     // with 255.255.255.0
     // P2P is 192.168.49.1 and 255.255.255.0
-    private static final String[] DHCP_DEFAULT_RANGE = {
+    private static final String[] LEGACY_DHCP_DEFAULT_RANGE = {
         "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
         "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
         "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
@@ -89,8 +93,9 @@
     public final boolean isDunRequired;
     public final boolean chooseUpstreamAutomatically;
     public final Collection<Integer> preferredUpstreamIfaceTypes;
-    public final String[] dhcpRanges;
+    public final String[] legacyDhcpRanges;
     public final String[] defaultIPv4DNS;
+    public final boolean enableLegacyDhcpServer;
 
     public final String[] provisioningApp;
     public final String provisioningAppNoUi;
@@ -112,8 +117,9 @@
         preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(ctx, dunCheck);
         isDunRequired = preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN);
 
-        dhcpRanges = getDhcpRanges(ctx);
+        legacyDhcpRanges = getLegacyDhcpRanges(ctx);
         defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
+        enableLegacyDhcpServer = getEnableLegacyDhcpServer(ctx);
 
         provisioningApp = getResourceStringArray(ctx, config_mobile_hotspot_provision_app);
         provisioningAppNoUi = getProvisioningAppNoUi(ctx);
@@ -150,7 +156,7 @@
         dumpStringArray(pw, "preferredUpstreamIfaceTypes",
                 preferredUpstreamNames(preferredUpstreamIfaceTypes));
 
-        dumpStringArray(pw, "dhcpRanges", dhcpRanges);
+        dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges);
         dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS);
 
         dumpStringArray(pw, "provisioningApp", provisioningApp);
@@ -276,12 +282,12 @@
         return false;
     }
 
-    private static String[] getDhcpRanges(Context ctx) {
+    private static String[] getLegacyDhcpRanges(Context ctx) {
         final String[] fromResource = getResourceStringArray(ctx, config_tether_dhcp_range);
         if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) {
             return fromResource;
         }
-        return copy(DHCP_DEFAULT_RANGE);
+        return copy(LEGACY_DHCP_DEFAULT_RANGE);
     }
 
     private static String getProvisioningAppNoUi(Context ctx) {
@@ -309,6 +315,13 @@
         }
     }
 
+    private static boolean getEnableLegacyDhcpServer(Context ctx) {
+        // TODO: make the default false (0) and update javadoc in Settings.java
+        final ContentResolver cr = ctx.getContentResolver();
+        final int intVal = Settings.Global.getInt(cr, TETHER_ENABLE_LEGACY_DHCP_SERVER, 1);
+        return intVal != 0;
+    }
+
     private static String[] copy(String[] strarray) {
         return Arrays.copyOf(strarray, strarray.length);
     }
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
index 605ee9c..caa867c 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java
@@ -19,11 +19,14 @@
 import android.content.Context;
 import android.net.INetd;
 import android.net.NetworkRequest;
+import android.net.dhcp.DhcpServer;
+import android.net.dhcp.DhcpServingParams;
 import android.net.ip.RouterAdvertisementDaemon;
 import android.net.util.InterfaceParams;
 import android.net.util.NetdService;
 import android.os.Handler;
 import android.net.util.SharedLog;
+import android.os.Looper;
 
 import com.android.internal.util.StateMachine;
 
@@ -69,4 +72,9 @@
     public NetworkRequest getDefaultNetworkRequest() {
         return null;
     }
+
+    public DhcpServer makeDhcpServer(Looper looper, InterfaceParams iface, DhcpServingParams params,
+            SharedLog log) {
+        return new DhcpServer(looper, iface, params, log);
+    }
 }
diff --git a/services/core/java/com/android/server/content/ContentService.java b/services/core/java/com/android/server/content/ContentService.java
index c145d64..d8d650b 100644
--- a/services/core/java/com/android/server/content/ContentService.java
+++ b/services/core/java/com/android/server/content/ContentService.java
@@ -385,7 +385,7 @@
     @Override
     public void notifyChange(Uri uri, IContentObserver observer,
             boolean observerWantsSelfNotifications, int flags, int userHandle,
-            int targetSdkVersion) {
+            int targetSdkVersion, String callingPackage) {
         if (DEBUG) Slog.d(TAG, "Notifying update of " + uri + " for user " + userHandle
                 + " from observer " + observer + ", flags " + Integer.toHexString(flags));
 
@@ -393,11 +393,11 @@
             throw new NullPointerException("Uri must not be null");
         }
 
-        final int uid = Binder.getCallingUid();
-        final int pid = Binder.getCallingPid();
+        final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
         final int callingUserHandle = UserHandle.getCallingUserId();
 
-        userHandle = handleIncomingUser(uri, pid, uid,
+        userHandle = handleIncomingUser(uri, callingPid, callingUid,
                 Intent.FLAG_GRANT_WRITE_URI_PERMISSION, true, userHandle);
 
         final String msg = LocalServices.getService(ActivityManagerInternal.class)
@@ -410,7 +410,7 @@
                     // Sigh, we need to quietly let apps targeting older API
                     // levels notify on non-existent providers.
                 } else {
-                    Log.w(TAG, "Ignoring notify for " + uri + " from " + uid + ": " + msg);
+                    Log.w(TAG, "Ignoring notify for " + uri + " from " + callingUid + ": " + msg);
                     return;
                 }
             }
@@ -453,8 +453,10 @@
             if ((flags&ContentResolver.NOTIFY_SYNC_TO_NETWORK) != 0) {
                 SyncManager syncManager = getSyncManager();
                 if (syncManager != null) {
-                    syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid,
-                            uri.getAuthority(), getSyncExemptionForCaller(uid));
+                    syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle,
+                            callingUid,
+                            uri.getAuthority(), getSyncExemptionForCaller(callingUid),
+                            callingUid, callingPid, callingPackage);
                 }
             }
 
@@ -477,10 +479,11 @@
     }
 
     public void notifyChange(Uri uri, IContentObserver observer,
-                             boolean observerWantsSelfNotifications, boolean syncToNetwork) {
+            boolean observerWantsSelfNotifications, boolean syncToNetwork,
+            String callingPackage) {
         notifyChange(uri, observer, observerWantsSelfNotifications,
                 syncToNetwork ? ContentResolver.NOTIFY_SYNC_TO_NETWORK : 0,
-                UserHandle.getCallingUserId(), Build.VERSION_CODES.CUR_DEVELOPMENT);
+                UserHandle.getCallingUserId(), Build.VERSION_CODES.CUR_DEVELOPMENT, callingPackage);
     }
 
     /**
@@ -504,14 +507,16 @@
     }
 
     @Override
-    public void requestSync(Account account, String authority, Bundle extras) {
+    public void requestSync(Account account, String authority, Bundle extras,
+            String callingPackage) {
         Bundle.setDefusable(extras, true);
         ContentResolver.validateSyncExtrasBundle(extras);
         int userId = UserHandle.getCallingUserId();
-        int uId = Binder.getCallingUid();
+        final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
 
-        validateExtras(uId, extras);
-        final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(uId, extras);
+        validateExtras(callingUid, extras);
+        final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callingUid, extras);
 
         // This makes it so that future permission checks will be in the context of this
         // process rather than the caller's process. We will restore this before returning.
@@ -519,9 +524,9 @@
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                syncManager.scheduleSync(account, userId, uId, authority, extras,
+                syncManager.scheduleSync(account, userId, callingUid, authority, extras,
                         SyncStorageEngine.AuthorityInfo.UNDEFINED,
-                        syncExemption);
+                        syncExemption, callingUid, callingPid, callingPackage);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -538,8 +543,8 @@
      * @param request The request object. Validation of this object is done by its builder.
      */
     @Override
-    public void sync(SyncRequest request) {
-        syncAsUser(request, UserHandle.getCallingUserId());
+    public void sync(SyncRequest request, String callingPackage) {
+        syncAsUser(request, UserHandle.getCallingUserId(), callingPackage);
     }
 
     private long clampPeriod(long period) {
@@ -557,14 +562,15 @@
      * INTERACT_ACROSS_USERS_FULL permission.
      */
     @Override
-    public void syncAsUser(SyncRequest request, int userId) {
+    public void syncAsUser(SyncRequest request, int userId, String callingPackage) {
         enforceCrossUserPermission(userId, "no permission to request sync as user: " + userId);
-        int callerUid = Binder.getCallingUid();
+        final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
 
         final Bundle extras = request.getBundle();
 
-        validateExtras(callerUid, extras);
-        final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callerUid, extras);
+        validateExtras(callingUid, extras);
+        final int syncExemption = getSyncExemptionAndCleanUpExtrasForCaller(callingUid, extras);
 
         // This makes it so that future permission checks will be in the context of this
         // process rather than the caller's process. We will restore this before returning.
@@ -590,9 +596,9 @@
                         flextime, extras);
             } else {
                 syncManager.scheduleSync(
-                        request.getAccount(), userId, callerUid, request.getProvider(), extras,
+                        request.getAccount(), userId, callingUid, request.getProvider(), extras,
                         SyncStorageEngine.AuthorityInfo.UNDEFINED,
-                        syncExemption);
+                        syncExemption, callingUid, callingPid, callingPackage);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -781,6 +787,7 @@
         enforceCrossUserPermission(userId,
                 "no permission to modify the sync settings for user " + userId);
         final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
         final int syncExemptionFlag = getSyncExemptionForCaller(callingUid);
 
         long identityToken = clearCallingIdentity();
@@ -788,7 +795,7 @@
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.getSyncStorageEngine().setSyncAutomatically(account, userId,
-                        providerName, sync, syncExemptionFlag, callingUid);
+                        providerName, sync, syncExemptionFlag, callingUid, callingPid);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -916,6 +923,7 @@
 
         syncable = normalizeSyncable(syncable);
         final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
 
         int userId = UserHandle.getCallingUserId();
         long identityToken = clearCallingIdentity();
@@ -923,7 +931,7 @@
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.getSyncStorageEngine().setIsSyncable(
-                        account, userId, providerName, syncable, callingUid);
+                        account, userId, providerName, syncable, callingUid, callingPid);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -971,13 +979,14 @@
                 "no permission to write the sync settings");
 
         final int callingUid = Binder.getCallingUid();
+        final int callingPid = Binder.getCallingPid();
 
         long identityToken = clearCallingIdentity();
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.getSyncStorageEngine().setMasterSyncAutomatically(flag, userId,
-                        getSyncExemptionForCaller(callingUid), callingUid);
+                        getSyncExemptionForCaller(callingUid), callingUid, callingPid);
             }
         } finally {
             restoreCallingIdentity(identityToken);
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index f4d20b3..8b93e04 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -584,9 +584,9 @@
         mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() {
             @Override
             public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras,
-                    @SyncExemption int syncExemptionFlag) {
+                    @SyncExemption int syncExemptionFlag, int callingUid, int callingPid) {
                 scheduleSync(info.account, info.userId, reason, info.provider, extras,
-                        AuthorityInfo.UNDEFINED, syncExemptionFlag);
+                        AuthorityInfo.UNDEFINED, syncExemptionFlag, callingUid, callingPid, null);
             }
         });
 
@@ -619,7 +619,8 @@
                     scheduleSync(null, UserHandle.USER_ALL,
                             SyncOperation.REASON_SERVICE_CHANGED,
                             type.authority, null, AuthorityInfo.UNDEFINED,
-                            ContentResolver.SYNC_EXEMPTION_NONE);
+                            ContentResolver.SYNC_EXEMPTION_NONE,
+                            Process.myUid(), -1, null);
                 }
             }
         }, mSyncHandler);
@@ -666,7 +667,8 @@
                 scheduleSync(account, UserHandle.getUserId(uid),
                         SyncOperation.REASON_ACCOUNTS_UPDATED,
                         null, null, AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS,
-                        ContentResolver.SYNC_EXEMPTION_NONE);
+                        ContentResolver.SYNC_EXEMPTION_NONE,
+                        Process.myUid(), -2, null);
             }
         });
 
@@ -893,9 +895,11 @@
      */
     public void scheduleSync(Account requestedAccount, int userId, int reason,
             String requestedAuthority, Bundle extras, int targetSyncState,
-            @SyncExemption int syncExemptionFlag) {
+            @SyncExemption int syncExemptionFlag, int callingUid, int callingPid,
+            String callingPackage) {
         scheduleSync(requestedAccount, userId, reason, requestedAuthority, extras, targetSyncState,
-                0 /* min delay */, true /* checkIfAccountReady */, syncExemptionFlag);
+                0 /* min delay */, true /* checkIfAccountReady */, syncExemptionFlag,
+                callingUid, callingPid, callingPackage);
     }
 
     /**
@@ -904,18 +908,21 @@
     private void scheduleSync(Account requestedAccount, int userId, int reason,
             String requestedAuthority, Bundle extras, int targetSyncState,
             final long minDelayMillis, boolean checkIfAccountReady,
-            @SyncExemption int syncExemptionFlag) {
-        final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
+            @SyncExemption int syncExemptionFlag,
+            int callingUid, int callingPid, String callingPackage) {
         if (extras == null) {
             extras = new Bundle();
         }
-        if (isLoggable) {
-            Log.d(TAG, "one-time sync for: " + requestedAccount + " " + extras.toString() + " "
-                    + requestedAuthority
-                    + " reason=" + reason
-                    + " checkIfAccountReady=" + checkIfAccountReady
-                    + " syncExemptionFlag=" + syncExemptionFlag);
-        }
+        extras.size(); // Force unpacel.
+        mLogger.log("scheduleSync: account=", requestedAccount,
+                " u", userId,
+                " authority=", requestedAuthority,
+                " reason=", reason,
+                " extras=", extras,
+                " cuid=", callingUid, " cpid=", callingPid, " cpkg=", callingPackage,
+                " mdm=", minDelayMillis,
+                " ciar=", checkIfAccountReady,
+                " sef=", syncExemptionFlag);
 
         AccountAndUser[] accounts = null;
         if (requestedAccount != null) {
@@ -934,9 +941,7 @@
         }
 
         if (ArrayUtils.isEmpty(accounts)) {
-            if (isLoggable) {
-                Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
-            }
+            mLogger.log("scheduleSync: no accounts configured, dropping");
             return;
         }
 
@@ -1007,10 +1012,8 @@
                 final int owningUid = syncAdapterInfo.uid;
 
                 if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
-                    if (isLoggable) {
-                        Slog.v(TAG, "    Not scheduling sync operation: "
+                    mLogger.log("scheduleSync: Not scheduling sync operation: "
                                 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
-                    }
                     Bundle finalExtras = new Bundle(extras);
                     String packageName = syncAdapterInfo.componentName.getPackageName();
                     // If the app did not run and has no account access, done
@@ -1025,7 +1028,8 @@
                                     scheduleSync(account.account, userId, reason, authority,
                                             finalExtras, targetSyncState, minDelayMillis,
                                             true /* checkIfAccountReady */,
-                                            syncExemptionFlag);
+                                            syncExemptionFlag, callingUid, callingPid,
+                                            callingPackage);
                                 }
                             }
                         ));
@@ -1037,7 +1041,7 @@
                 if (!checkIfAccountReady && isSyncable < 0 && isAlwaysSyncable) {
                     mSyncStorageEngine.setIsSyncable(
                             account.account, account.userId, authority, AuthorityInfo.SYNCABLE,
-                            SyncLogger.CALLING_UID_SELF);
+                            callingUid, callingPid);
                     isSyncable = AuthorityInfo.SYNCABLE;
                 }
 
@@ -1056,10 +1060,8 @@
                                 && mSyncStorageEngine.getSyncAutomatically(account.account,
                                 account.userId, authority));
                 if (!syncAllowed) {
-                    if (isLoggable) {
-                        Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority
-                                + " is not allowed, dropping request");
-                    }
+                    mLogger.log("scheduleSync: sync of ", account, " ", authority,
+                            " is not allowed, dropping request");
                     continue;
                 }
                 SyncStorageEngine.EndPoint info =
@@ -1077,21 +1079,16 @@
                         sendOnUnsyncableAccount(mContext, syncAdapterInfo, account.userId,
                                 () -> scheduleSync(account.account, account.userId, reason,
                                         authority, finalExtras, targetSyncState, minDelayMillis,
-                                        false, syncExemptionFlag));
+                                        false, syncExemptionFlag, callingUid, callingPid,
+                                        callingPackage));
                     } else {
                         // Initialisation sync.
                         Bundle newExtras = new Bundle();
                         newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
-                        if (isLoggable) {
-                            Slog.v(TAG, "schedule initialisation Sync:"
-                                    + ", delay until " + delayUntil
-                                    + ", run by " + 0
-                                    + ", flexMillis " + 0
-                                    + ", source " + source
-                                    + ", account " + account
-                                    + ", authority " + authority
-                                    + ", extras " + newExtras);
-                        }
+
+                        mLogger.log("scheduleSync: schedule initialisation sync ",
+                                account, " ", authority);
+
                         postScheduleSyncMessage(
                                 new SyncOperation(account.account, account.userId,
                                         owningUid, owningPackage, reason, source,
@@ -1102,20 +1099,17 @@
                     }
                 } else if (targetSyncState == AuthorityInfo.UNDEFINED
                         || targetSyncState == isSyncable) {
-                    if (isLoggable) {
-                        Slog.v(TAG, "scheduleSync:"
-                                + " delay until " + delayUntil
-                                + ", source " + source
-                                + ", account " + account
-                                + ", authority " + authority
-                                + ", extras " + extras);
-                    }
+                    mLogger.log("scheduleSync: scheduling sync ",
+                            account, " ", authority);
                     postScheduleSyncMessage(
                             new SyncOperation(account.account, account.userId,
                                     owningUid, owningPackage, reason, source,
                                     authority, extras, allowParallelSyncs, syncExemptionFlag),
                             minDelayMillis
                     );
+                } else {
+                    mLogger.log("scheduleSync: not handling ",
+                            account, " ", authority);
                 }
             }
         }
@@ -1227,12 +1221,13 @@
      * ms to batch syncs.
      */
     public void scheduleLocalSync(Account account, int userId, int reason, String authority,
-            @SyncExemption int syncExemptionFlag) {
+            @SyncExemption int syncExemptionFlag,
+            int callingUid, int callingPid, String callingPackage) {
         final Bundle extras = new Bundle();
         extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
         scheduleSync(account, userId, reason, authority, extras,
                 AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY, true /* checkIfAccountReady */,
-                syncExemptionFlag);
+                syncExemptionFlag, callingUid, callingPid, callingPackage);
     }
 
     public SyncAdapterType[] getSyncAdapterTypes(int userId) {
@@ -1769,7 +1764,8 @@
                 mContext.getOpPackageName());
         for (Account account : accounts) {
             scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null,
-                    AuthorityInfo.NOT_INITIALIZED, ContentResolver.SYNC_EXEMPTION_NONE);
+                    AuthorityInfo.NOT_INITIALIZED, ContentResolver.SYNC_EXEMPTION_NONE,
+                    Process.myUid(), -3, null);
         }
     }
 
@@ -3272,7 +3268,7 @@
                 scheduleSync(syncTargets.account, syncTargets.userId,
                         SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider,
                         null, AuthorityInfo.NOT_INITIALIZED,
-                        ContentResolver.SYNC_EXEMPTION_NONE);
+                        ContentResolver.SYNC_EXEMPTION_NONE, Process.myUid(), -4, null);
             }
         }
 
diff --git a/services/core/java/com/android/server/content/SyncStorageEngine.java b/services/core/java/com/android/server/content/SyncStorageEngine.java
index 391e3b0..bfd1791 100644
--- a/services/core/java/com/android/server/content/SyncStorageEngine.java
+++ b/services/core/java/com/android/server/content/SyncStorageEngine.java
@@ -357,7 +357,7 @@
 
         /** Called when a sync is needed on an account(s) due to some change in state. */
         public void onSyncRequest(EndPoint info, int reason, Bundle extras,
-                @SyncExemption int syncExemptionFlag);
+                @SyncExemption int syncExemptionFlag, int callingUid, int callingPid);
     }
 
     interface PeriodicSyncAddedListener {
@@ -669,7 +669,7 @@
     }
 
     public void setSyncAutomatically(Account account, int userId, String providerName,
-            boolean sync, @SyncExemption int syncExemptionFlag, int callingUid) {
+            boolean sync, @SyncExemption int syncExemptionFlag, int callingUid, int callingPid) {
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Slog.d(TAG, "setSyncAutomatically: " + /* account + */" provider " + providerName
                     + ", user " + userId + " -> " + sync);
@@ -678,7 +678,9 @@
                 " user=", userId,
                 " authority=", providerName,
                 " value=", Boolean.toString(sync),
-                " callingUid=", callingUid);
+                " cuid=", callingUid,
+                " cpid=", callingPid
+        );
         synchronized (mAuthorities) {
             AuthorityInfo authority =
                     getOrCreateAuthorityLocked(
@@ -704,7 +706,7 @@
         if (sync) {
             requestSync(account, userId, SyncOperation.REASON_SYNC_AUTO, providerName,
                     new Bundle(),
-                    syncExemptionFlag);
+                    syncExemptionFlag, callingUid, callingPid);
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
         queueBackup();
@@ -736,9 +738,9 @@
     }
 
     public void setIsSyncable(Account account, int userId, String providerName, int syncable,
-            int callingUid) {
+            int callingUid, int callingPid) {
         setSyncableStateForEndPoint(new EndPoint(account, providerName, userId), syncable,
-                callingUid);
+                callingUid, callingPid);
     }
 
     /**
@@ -747,10 +749,12 @@
      * @param target target to set value for.
      * @param syncable 0 indicates unsyncable, <0 unknown, >0 is active/syncable.
      */
-    private void setSyncableStateForEndPoint(EndPoint target, int syncable, int callingUid) {
+    private void setSyncableStateForEndPoint(EndPoint target, int syncable,
+            int callingUid, int callingPid) {
         AuthorityInfo aInfo;
         mLogger.log("Set syncable ", target, " value=", Integer.toString(syncable),
-                " callingUid=", callingUid);
+                " cuid=", callingUid,
+                " cpid=", callingPid);
         synchronized (mAuthorities) {
             aInfo = getOrCreateAuthorityLocked(target, -1, false);
             if (syncable < AuthorityInfo.NOT_INITIALIZED) {
@@ -770,7 +774,7 @@
         }
         if (syncable == AuthorityInfo.SYNCABLE) {
             requestSync(aInfo, SyncOperation.REASON_IS_SYNCABLE, new Bundle(),
-                    ContentResolver.SYNC_EXEMPTION_NONE);
+                    ContentResolver.SYNC_EXEMPTION_NONE, callingUid, callingPid);
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
     }
@@ -932,9 +936,10 @@
     }
 
     public void setMasterSyncAutomatically(boolean flag, int userId,
-            @SyncExemption int syncExemptionFlag, int callingUid) {
+            @SyncExemption int syncExemptionFlag, int callingUid, int callingPid) {
         mLogger.log("Set master enabled=", flag, " user=", userId,
-                " caller=" + callingUid);
+                " cuid=", callingUid,
+                " cpid=", callingPid);
         synchronized (mAuthorities) {
             Boolean auto = mMasterSyncAutomatically.get(userId);
             if (auto != null && auto.equals(flag)) {
@@ -946,7 +951,7 @@
         if (flag) {
             requestSync(null, userId, SyncOperation.REASON_MASTER_SYNC_AUTO, null,
                     new Bundle(),
-                    syncExemptionFlag);
+                    syncExemptionFlag, callingUid, callingPid);
         }
         reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
         mContext.sendBroadcast(ContentResolver.ACTION_SYNC_CONN_STATUS_CHANGED);
@@ -2064,11 +2069,11 @@
     }
 
     private void requestSync(AuthorityInfo authorityInfo, int reason, Bundle extras,
-            @SyncExemption int syncExemptionFlag) {
+            @SyncExemption int syncExemptionFlag, int callingUid, int callingPid) {
         if (android.os.Process.myUid() == android.os.Process.SYSTEM_UID
                 && mSyncRequestListener != null) {
             mSyncRequestListener.onSyncRequest(authorityInfo.target, reason, extras,
-                    syncExemptionFlag);
+                    syncExemptionFlag, callingUid, callingPid);
         } else {
             SyncRequest.Builder req =
                     new SyncRequest.Builder()
@@ -2080,7 +2085,7 @@
     }
 
     private void requestSync(Account account, int userId, int reason, String authority,
-            Bundle extras, @SyncExemption int syncExemptionFlag) {
+            Bundle extras, @SyncExemption int syncExemptionFlag, int callingUid, int callingPid) {
         // If this is happening in the system process, then call the syncrequest listener
         // to make a request back to the SyncManager directly.
         // If this is probably a test instance, then call back through the ContentResolver
@@ -2089,7 +2094,7 @@
                 && mSyncRequestListener != null) {
             mSyncRequestListener.onSyncRequest(
                     new EndPoint(account, authority, userId),
-                    reason, extras, syncExemptionFlag);
+                    reason, extras, syncExemptionFlag, callingUid, callingPid);
         } else {
             ContentResolver.requestSync(account, authority, extras);
         }
diff --git a/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java b/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java
new file mode 100644
index 0000000..b57356f
--- /dev/null
+++ b/services/core/java/com/android/server/inputmethod/InputMethodManagerInternal.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2015 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 com.android.server.inputmethod;
+
+import android.content.ComponentName;
+
+import com.android.internal.view.IInputContext;
+import com.android.internal.view.IInputMethodClient;
+
+/**
+ * Input method manager local system service interface.
+ */
+public abstract class InputMethodManagerInternal {
+    /**
+     * Called by the window manager service when a client process is being attached to the window
+     * manager service.
+     * @param client {@link android.os.Binder} proxy that is associated with the singleton instance
+     *               of {@link android.view.inputmethod.InputMethodManager} that runs on the client
+     *               process
+     * @param inputContext communication channel for the dummy
+     *                     {@link android.view.inputmethod.InputConnection}
+     * @param uid UID of the client process
+     * @param pid PID of the client process
+     */
+    public abstract void addClient(IInputMethodClient client, IInputContext inputContext, int uid,
+            int pid);
+
+    /**
+     * Called by the window manager service when a client process is being attached to the window
+     * manager service.
+     * @param client {@link android.os.Binder} proxy that is associated with the singleton instance
+     *               of {@link android.view.inputmethod.InputMethodManager} that runs on the client
+     *               process
+     */
+    public abstract void removeClient(IInputMethodClient client);
+
+    /**
+     * Called by the power manager to tell the input method manager whether it
+     * should start watching for wake events.
+     */
+    public abstract void setInteractive(boolean interactive);
+
+    /**
+     * Hides the current input method, if visible.
+     */
+    public abstract void hideCurrentInputMethod();
+
+    /**
+     * Switches to VR InputMethod defined in the packageName of {@param componentName}.
+     */
+    public abstract void startVrInputMethodNoCheck(ComponentName componentName);
+
+    /**
+     * Fake implementation of {@link InputMethodManagerInternal}.  All the methods do nothing.
+     */
+    public static final InputMethodManagerInternal NOP =
+            new InputMethodManagerInternal() {
+                @Override
+                public void addClient(IInputMethodClient client, IInputContext inputContext,
+                        int uid, int pid) {
+                }
+
+                @Override
+                public void removeClient(IInputMethodClient client) {
+                }
+
+                @Override
+                public void setInteractive(boolean interactive) {
+                }
+
+                @Override
+                public void hideCurrentInputMethod() {
+                }
+
+                @Override
+                public void startVrInputMethodNoCheck(ComponentName componentName) {
+                }
+            };
+}
diff --git a/services/core/java/com/android/server/notification/ConditionProviders.java b/services/core/java/com/android/server/notification/ConditionProviders.java
index 18f4bc7..41f2cc5 100644
--- a/services/core/java/com/android/server/notification/ConditionProviders.java
+++ b/services/core/java/com/android/server/notification/ConditionProviders.java
@@ -279,11 +279,13 @@
 
     public void ensureRecordExists(ComponentName component, Uri conditionId,
             IConditionProvider provider) {
-        // constructed by convention, make sure the record exists...
-        final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/);
-        if (r.info == null) {
-            // ... and is associated with the in-process service
-            r.info = checkServiceTokenLocked(provider);
+        synchronized (mMutex) {
+            // constructed by convention, make sure the record exists...
+            final ConditionRecord r = getRecordLocked(conditionId, component, true /*create*/);
+            if (r.info == null) {
+                // ... and is associated with the in-process service
+                r.info = checkServiceTokenLocked(provider);
+            }
         }
     }
 
diff --git a/services/core/java/com/android/server/notification/ManagedServices.java b/services/core/java/com/android/server/notification/ManagedServices.java
index efc18ad..340ae0a 100644
--- a/services/core/java/com/android/server/notification/ManagedServices.java
+++ b/services/core/java/com/android/server/notification/ManagedServices.java
@@ -216,12 +216,14 @@
         }
 
         pw.println("    Live " + getCaption() + "s (" + mServices.size() + "):");
-        for (ManagedServiceInfo info : mServices) {
-            if (filter != null && !filter.matches(info.component)) continue;
-            pw.println("      " + info.component
-                    + " (user " + info.userid + "): " + info.service
-                    + (info.isSystem?" SYSTEM":"")
-                    + (info.isGuest(this)?" GUEST":""));
+        synchronized (mMutex) {
+            for (ManagedServiceInfo info : mServices) {
+                if (filter != null && !filter.matches(info.component)) continue;
+                pw.println("      " + info.component
+                        + " (user " + info.userid + "): " + info.service
+                        + (info.isSystem ? " SYSTEM" : "")
+                        + (info.isGuest(this) ? " GUEST" : ""));
+            }
         }
 
         pw.println("    Snoozed " + getCaption() + "s (" +
@@ -260,9 +262,11 @@
             cmpt.writeToProto(proto, ManagedServicesProto.ENABLED);
         }
 
-        for (ManagedServiceInfo info : mServices) {
-            if (filter != null && !filter.matches(info.component)) continue;
-            info.writeToProto(proto, ManagedServicesProto.LIVE_SERVICES, this);
+        synchronized (mMutex) {
+            for (ManagedServiceInfo info : mServices) {
+                if (filter != null && !filter.matches(info.component)) continue;
+                info.writeToProto(proto, ManagedServicesProto.LIVE_SERVICES, this);
+            }
         }
 
         for (ComponentName name : mSnoozingForCurrentProfiles) {
@@ -631,11 +635,13 @@
 
     public boolean isSameUser(IInterface service, int userId) {
         checkNotNull(service);
-        ManagedServiceInfo info = getServiceFromTokenLocked(service);
-        if (info != null) {
-            return info.isSameUser(userId);
+        synchronized (mMutex) {
+            ManagedServiceInfo info = getServiceFromTokenLocked(service);
+            if (info != null) {
+                return info.isSameUser(userId);
+            }
+            return false;
         }
-        return false;
     }
 
     public void unregisterService(IInterface service, int userid) {
diff --git a/services/core/java/com/android/server/pm/InstantAppResolver.java b/services/core/java/com/android/server/pm/InstantAppResolver.java
index 9bd3924..30b2c9d 100644
--- a/services/core/java/com/android/server/pm/InstantAppResolver.java
+++ b/services/core/java/com/android/server/pm/InstantAppResolver.java
@@ -132,7 +132,7 @@
         try {
             final List<InstantAppResolveInfo> instantAppResolveInfoList =
                     connection.getInstantAppResolveInfoList(sanitizedIntent,
-                            requestObj.digest.getDigestPrefixSecure(), token);
+                            requestObj.digest.getDigestPrefixSecure(), requestObj.userId, token);
             if (instantAppResolveInfoList != null && instantAppResolveInfoList.size() > 0) {
                 resolveInfo = InstantAppResolver.filterInstantAppIntent(
                         instantAppResolveInfoList, origIntent, requestObj.resolvedType,
@@ -224,8 +224,8 @@
         };
         try {
             connection.getInstantAppIntentFilterList(sanitizedIntent,
-                    requestObj.digest.getDigestPrefixSecure(), token, callback, callbackHandler,
-                    startTime);
+                    requestObj.digest.getDigestPrefixSecure(), requestObj.userId, token, callback,
+                    callbackHandler, startTime);
         } catch (ConnectionException e) {
             @ResolutionStatus int resolutionStatus = RESOLUTION_FAILURE;
             if (e.failure == ConnectionException.FAILURE_BIND) {
diff --git a/services/core/java/com/android/server/pm/InstantAppResolverConnection.java b/services/core/java/com/android/server/pm/InstantAppResolverConnection.java
index 16b4368..c0e9f58 100644
--- a/services/core/java/com/android/server/pm/InstantAppResolverConnection.java
+++ b/services/core/java/com/android/server/pm/InstantAppResolverConnection.java
@@ -85,8 +85,8 @@
         mBgHandler = BackgroundThread.getHandler();
     }
 
-    public final List<InstantAppResolveInfo> getInstantAppResolveInfoList(Intent sanitizedIntent,
-            int hashPrefix[], String token) throws ConnectionException {
+    public List<InstantAppResolveInfo> getInstantAppResolveInfoList(Intent sanitizedIntent,
+            int[] hashPrefix, int userId, String token) throws ConnectionException {
         throwIfCalledOnMainThread();
         IInstantAppResolver target = null;
         try {
@@ -99,7 +99,8 @@
             }
             try {
                 return mGetInstantAppResolveInfoCaller
-                        .getInstantAppResolveInfoList(target, sanitizedIntent, hashPrefix, token);
+                        .getInstantAppResolveInfoList(target, sanitizedIntent, hashPrefix, userId,
+                                token);
             } catch (TimeoutException e) {
                 throw new ConnectionException(ConnectionException.FAILURE_CALL);
             } catch (RemoteException ignore) {
@@ -112,7 +113,7 @@
         return null;
     }
 
-    public final void getInstantAppIntentFilterList(Intent sanitizedIntent, int hashPrefix[],
+    public void getInstantAppIntentFilterList(Intent sanitizedIntent, int[] hashPrefix, int userId,
             String token, PhaseTwoCallback callback, Handler callbackHandler, final long startTime)
             throws ConnectionException {
         final IRemoteCallback remoteCallback = new IRemoteCallback.Stub() {
@@ -126,7 +127,7 @@
         };
         try {
             getRemoteInstanceLazy(token)
-                    .getInstantAppIntentFilterList(sanitizedIntent, hashPrefix, token,
+                    .getInstantAppIntentFilterList(sanitizedIntent, hashPrefix, userId, token,
                             remoteCallback);
         } catch (TimeoutException e) {
             throw new ConnectionException(ConnectionException.FAILURE_BIND);
@@ -352,11 +353,11 @@
         }
 
         public List<InstantAppResolveInfo> getInstantAppResolveInfoList(
-                IInstantAppResolver target, Intent sanitizedIntent,  int hashPrefix[], String token)
-                        throws RemoteException, TimeoutException {
+                IInstantAppResolver target, Intent sanitizedIntent, int[] hashPrefix, int userId,
+                String token) throws RemoteException, TimeoutException {
             final int sequence = onBeforeRemoteCall();
-            target.getInstantAppResolveInfoList(sanitizedIntent, hashPrefix, token, sequence,
-                    mCallback);
+            target.getInstantAppResolveInfoList(sanitizedIntent, hashPrefix, userId, token,
+                    sequence, mCallback);
             return getResultTimed(sequence);
         }
     }
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 52a8510..182901a 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -3214,6 +3214,21 @@
                 mRequiredPermissionControllerPackage = null;
             }
 
+            // Initialize InstantAppRegistry's Instant App list for all users.
+            final int[] userIds = UserManagerService.getInstance().getUserIds();
+            for (PackageParser.Package pkg : mPackages.values()) {
+                if (pkg.isSystem()) {
+                    continue;
+                }
+                for (int userId : userIds) {
+                    final PackageSetting ps = (PackageSetting) pkg.mExtras;
+                    if (ps == null || !ps.getInstantApp(userId) || !ps.getInstalled(userId)) {
+                        continue;
+                    }
+                    mInstantAppRegistry.addInstantAppLPw(userId, ps.appId);
+                }
+            }
+
             mInstallerService = new PackageInstallerService(context, this);
             final Pair<ComponentName, String> instantAppResolverComponent =
                     getInstantAppResolverLPr();
@@ -3241,8 +3256,7 @@
             // should take a fairly small time compare to the other activities (e.g. package
             // scanning).
             final Map<Integer, List<PackageInfo>> userPackages = new HashMap<>();
-            final int[] currentUserIds = UserManagerService.getInstance().getUserIds();
-            for (int userId : currentUserIds) {
+            for (int userId : userIds) {
                 userPackages.put(userId, getInstalledPackages(/*flags*/ 0, userId).getList());
             }
             mDexManager.load(userPackages);
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index e46ad2c..3e0429f 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -257,7 +257,6 @@
 import android.view.animation.AnimationSet;
 import android.view.animation.AnimationUtils;
 import android.view.autofill.AutofillManagerInternal;
-import android.view.inputmethod.InputMethodManagerInternal;
 
 import com.android.internal.R;
 import com.android.internal.accessibility.AccessibilityShortcutController;
@@ -276,6 +275,7 @@
 import com.android.server.GestureLauncherService;
 import com.android.server.LocalServices;
 import com.android.server.SystemServiceManager;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.policy.keyguard.KeyguardServiceDelegate;
 import com.android.server.policy.keyguard.KeyguardServiceDelegate.DrawnListener;
 import com.android.server.policy.keyguard.KeyguardStateMonitor.StateCallback;
diff --git a/services/core/java/com/android/server/power/Notifier.java b/services/core/java/com/android/server/power/Notifier.java
index b4dafc9..5981ab0 100644
--- a/services/core/java/com/android/server/power/Notifier.java
+++ b/services/core/java/com/android/server/power/Notifier.java
@@ -48,13 +48,13 @@
 import android.util.EventLog;
 import android.util.Slog;
 import android.util.StatsLog;
-import android.view.inputmethod.InputMethodManagerInternal;
 
 import com.android.internal.app.IBatteryStats;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.server.EventLogTags;
 import com.android.server.LocalServices;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.policy.WindowManagerPolicy;
 import com.android.server.statusbar.StatusBarManagerInternal;
 
diff --git a/services/core/java/com/android/server/vr/VrManagerService.java b/services/core/java/com/android/server/vr/VrManagerService.java
index 5c45afc..b3eafa4 100644
--- a/services/core/java/com/android/server/vr/VrManagerService.java
+++ b/services/core/java/com/android/server/vr/VrManagerService.java
@@ -58,7 +58,6 @@
 import android.util.ArraySet;
 import android.util.Slog;
 import android.util.SparseArray;
-import android.view.inputmethod.InputMethodManagerInternal;
 
 import com.android.internal.R;
 import com.android.internal.util.DumpUtils;
@@ -66,6 +65,7 @@
 import com.android.server.LocalServices;
 import com.android.server.SystemConfig;
 import com.android.server.SystemService;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.utils.ManagedApplicationService;
 import com.android.server.utils.ManagedApplicationService.BinderChecker;
 import com.android.server.utils.ManagedApplicationService.LogEvent;
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index a2dd679..ba46737 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -3514,16 +3514,12 @@
 
         private void addStackReferenceIfNeeded(TaskStack stack) {
             if (stack.isActivityTypeHome()) {
-                // TODO(b/111363427) Rollback to throws exceptions once we figure out how to
-                // properly deal with home type stack when external display removed
                 if (mHomeStack != null) {
-                    // throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
-                    //        + mHomeStack + " already exist on display=" + this + " stack=" + stack);
-                    Slog.e(TAG, "addStackReferenceIfNeeded: home stack="
+                    throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
                             + mHomeStack + " already exist on display=" + this + " stack=" + stack);
-                } else {
-                    mHomeStack = stack;
+
                 }
+                mHomeStack = stack;
             }
             final int windowingMode = stack.getWindowingMode();
             if (windowingMode == WINDOWING_MODE_PINNED) {
diff --git a/services/core/java/com/android/server/wm/DockedStackDividerController.java b/services/core/java/com/android/server/wm/DockedStackDividerController.java
index e817d1a..ac93848 100644
--- a/services/core/java/com/android/server/wm/DockedStackDividerController.java
+++ b/services/core/java/com/android/server/wm/DockedStackDividerController.java
@@ -50,11 +50,11 @@
 import android.view.animation.AnimationUtils;
 import android.view.animation.Interpolator;
 import android.view.animation.PathInterpolator;
-import android.view.inputmethod.InputMethodManagerInternal;
 
 import com.android.internal.policy.DividerSnapAlgorithm;
 import com.android.internal.policy.DockedDividerUtils;
 import com.android.server.LocalServices;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.wm.WindowManagerService.H;
 
 import java.io.PrintWriter;
diff --git a/services/core/java/com/android/server/wm/RecentsAnimationController.java b/services/core/java/com/android/server/wm/RecentsAnimationController.java
index dea4076..03c61f0 100644
--- a/services/core/java/com/android/server/wm/RecentsAnimationController.java
+++ b/services/core/java/com/android/server/wm/RecentsAnimationController.java
@@ -47,13 +47,13 @@
 import android.view.RemoteAnimationTarget;
 import android.view.SurfaceControl;
 import android.view.SurfaceControl.Transaction;
-import android.view.inputmethod.InputMethodManagerInternal;
 
 import com.google.android.collect.Sets;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.LocalServices;
 import com.android.server.input.InputWindowHandle;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
 import com.android.server.wm.utils.InsetUtils;
 
diff --git a/services/core/java/com/android/server/wm/Session.java b/services/core/java/com/android/server/wm/Session.java
index 0319864..f9a71d3 100644
--- a/services/core/java/com/android/server/wm/Session.java
+++ b/services/core/java/com/android/server/wm/Session.java
@@ -58,7 +58,8 @@
 import com.android.internal.os.logging.MetricsLoggerWrapper;
 import com.android.internal.view.IInputContext;
 import com.android.internal.view.IInputMethodClient;
-import com.android.internal.view.IInputMethodManager;
+import com.android.server.LocalServices;
+import com.android.server.inputmethod.InputMethodManagerInternal;
 import com.android.server.wm.WindowManagerService.H;
 
 import java.io.PrintWriter;
@@ -73,6 +74,7 @@
     final WindowManagerService mService;
     final IWindowSessionCallback mCallback;
     final IInputMethodClient mClient;
+    final InputMethodManagerInternal mInputMethodManagerInternal;
     final int mUid;
     final int mPid;
     private final String mStringName;
@@ -98,6 +100,12 @@
         mService = service;
         mCallback = callback;
         mClient = client;
+        // Depending on the timing when Session object gets called and SystemServer#mFactoryTestMode
+        // this could be null, right?
+        final InputMethodManagerInternal immInternal =
+                LocalServices.getService(InputMethodManagerInternal.class);
+        mInputMethodManagerInternal =
+                immInternal != null ? immInternal : InputMethodManagerInternal.NOP;
         mUid = Binder.getCallingUid();
         mPid = Binder.getCallingPid();
         mLastReportedAnimatorScale = service.getCurrentAnimatorScale();
@@ -126,31 +134,12 @@
         sb.append("}");
         mStringName = sb.toString();
 
-        synchronized (mService.mWindowMap) {
-            if (mService.mInputMethodManager == null && mService.mHaveInputMethods) {
-                IBinder b = ServiceManager.getService(
-                        Context.INPUT_METHOD_SERVICE);
-                mService.mInputMethodManager = IInputMethodManager.Stub.asInterface(b);
-            }
-        }
-        long ident = Binder.clearCallingIdentity();
+        mInputMethodManagerInternal.addClient(client, inputContext, mUid, mPid);
         try {
-            // Note: it is safe to call in to the input method manager
-            // here because we are not holding our lock.
-            if (mService.mInputMethodManager != null) {
-                mService.mInputMethodManager.addClient(client, inputContext, mUid, mPid);
-            }
             client.asBinder().linkToDeath(this, 0);
         } catch (RemoteException e) {
             // The caller has died, so we can just forget about this.
-            try {
-                if (mService.mInputMethodManager != null) {
-                    mService.mInputMethodManager.removeClient(client);
-                }
-            } catch (RemoteException ee) {
-            }
-        } finally {
-            Binder.restoreCallingIdentity(ident);
+            mInputMethodManagerInternal.removeClient(client);
         }
     }
 
@@ -170,14 +159,7 @@
 
     @Override
     public void binderDied() {
-        // Note: it is safe to call in to the input method manager
-        // here because we are not holding our lock.
-        try {
-            if (mService.mInputMethodManager != null) {
-                mService.mInputMethodManager.removeClient(mClient);
-            }
-        } catch (RemoteException e) {
-        }
+        mInputMethodManagerInternal.removeClient(mClient);
         synchronized(mService.mWindowMap) {
             mClient.asBinder().unlinkToDeath(this, 0);
             mClientDead = true;
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index da77edf..ea0dd7e 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -234,7 +234,6 @@
 import com.android.internal.util.LatencyTracker;
 import com.android.internal.view.IInputContext;
 import com.android.internal.view.IInputMethodClient;
-import com.android.internal.view.IInputMethodManager;
 import com.android.internal.view.WindowManagerPolicyThread;
 import com.android.server.AnimationThread;
 import com.android.server.DisplayThread;
@@ -417,8 +416,6 @@
 
     final Context mContext;
 
-    final boolean mHaveInputMethods;
-
     final boolean mHasPermanentDpad;
     final long mDrawLockTimeoutMillis;
     final boolean mAllowAnimationsInLowPowerMode;
@@ -521,8 +518,6 @@
     /** List of window currently causing non-system overlay windows to be hidden. */
     private ArrayList<WindowState> mHidingNonSystemOverlayWindows = new ArrayList<>();
 
-    IInputMethodManager mInputMethodManager;
-
     AccessibilityController mAccessibilityController;
     private RecentsAnimationController mRecentsAnimationController;
 
@@ -905,11 +900,10 @@
     }
 
     public static WindowManagerService main(final Context context, final InputManagerService im,
-            final boolean haveInputMethods, final boolean showBootMsgs, final boolean onlyCore,
-            WindowManagerPolicy policy) {
+            final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy) {
         DisplayThread.getHandler().runWithScissors(() ->
-                sInstance = new WindowManagerService(context, im, haveInputMethods, showBootMsgs,
-                        onlyCore, policy), 0);
+                sInstance = new WindowManagerService(context, im, showBootMsgs, onlyCore, policy),
+                0);
         return sInstance;
     }
 
@@ -930,11 +924,9 @@
     }
 
     private WindowManagerService(Context context, InputManagerService inputManager,
-            boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore,
-            WindowManagerPolicy policy) {
+            boolean showBootMsgs, boolean onlyCore, WindowManagerPolicy policy) {
         installLock(this, INDEX_WINDOW);
         mContext = context;
-        mHaveInputMethods = haveInputMethods;
         mAllowBootMessages = showBootMsgs;
         mOnlyCore = onlyCore;
         mLimitedAlphaCompositing = context.getResources().getBoolean(
diff --git a/services/core/java/com/android/server/wm/WindowSurfaceController.java b/services/core/java/com/android/server/wm/WindowSurfaceController.java
index 66c8cca..6821e94 100644
--- a/services/core/java/com/android/server/wm/WindowSurfaceController.java
+++ b/services/core/java/com/android/server/wm/WindowSurfaceController.java
@@ -225,16 +225,6 @@
         }
     }
 
-    void setFinalCropInTransaction(Rect clipRect) {
-        if (SHOW_TRANSACTIONS) logSurface(
-                "FINAL CROP " + clipRect.toShortString(), null);
-        try {
-            mSurfaceControl.setFinalCrop(clipRect);
-        } catch (RuntimeException e) {
-            Slog.w(TAG, "Error disconnecting surface in: " + this, e);
-        }
-    }
-
     void setLayerStackInTransaction(int layerStack) {
         if (mSurfaceControl != null) {
             mSurfaceControl.setLayerStack(layerStack);
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index fd982d6..ecc13b2 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -889,9 +889,8 @@
             // WMS needs sensor service ready
             ConcurrentUtils.waitForFutureNoInterrupt(mSensorServiceStart, START_SENSOR_SERVICE);
             mSensorServiceStart = null;
-            wm = WindowManagerService.main(context, inputManager,
-                    mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
-                    !mFirstBoot, mOnlyCore, new PhoneWindowManager());
+            wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,
+                    new PhoneWindowManager());
             ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
                     DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
             ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
diff --git a/services/net/java/android/net/dhcp/DhcpAckPacket.java b/services/net/java/android/net/dhcp/DhcpAckPacket.java
index df44b11..b2eb4e2 100644
--- a/services/net/java/android/net/dhcp/DhcpAckPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpAckPacket.java
@@ -30,8 +30,8 @@
     private final Inet4Address mSrcIp;
 
     DhcpAckPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress,
-                  Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac) {
-        super(transId, secs, clientIp, yourIp, serverAddress, INADDR_ANY, clientMac, broadcast);
+            Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac) {
+        super(transId, secs, clientIp, yourIp, serverAddress, relayIp, clientMac, broadcast);
         mBroadcast = broadcast;
         mSrcIp = serverAddress;
     }
@@ -70,19 +70,8 @@
     void finishPacket(ByteBuffer buffer) {
         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_ACK);
         addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier);
-        addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime);
 
-        // the client should renew at 1/2 the lease-expiry interval
-        if (mLeaseTime != null) {
-            addTlv(buffer, DHCP_RENEWAL_TIME,
-                Integer.valueOf(mLeaseTime.intValue() / 2));
-        }
-
-        addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask);
-        addTlv(buffer, DHCP_ROUTER, mGateways);
-        addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName);
-        addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress);
-        addTlv(buffer, DHCP_DNS_SERVER, mDnsServers);
+        addCommonServerTlvs(buffer);
         addTlvEnd(buffer);
     }
 
diff --git a/services/net/java/android/net/dhcp/DhcpNakPacket.java b/services/net/java/android/net/dhcp/DhcpNakPacket.java
index ef9af52..1da0b73 100644
--- a/services/net/java/android/net/dhcp/DhcpNakPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpNakPacket.java
@@ -26,9 +26,10 @@
     /**
      * Generates a NAK packet with the specified parameters.
      */
-    DhcpNakPacket(int transId, short secs, Inet4Address nextIp, Inet4Address relayIp,
-            byte[] clientMac, boolean broadcast) {
-        super(transId, secs, INADDR_ANY, INADDR_ANY, nextIp, relayIp, clientMac, broadcast);
+    DhcpNakPacket(int transId, short secs, Inet4Address relayIp, byte[] clientMac,
+            boolean broadcast) {
+        super(transId, secs, INADDR_ANY /* clientIp */, INADDR_ANY /* yourIp */,
+                INADDR_ANY /* nextIp */, relayIp, clientMac, broadcast);
     }
 
     public String toString() {
diff --git a/services/net/java/android/net/dhcp/DhcpOfferPacket.java b/services/net/java/android/net/dhcp/DhcpOfferPacket.java
index 99154ef..0eba77e 100644
--- a/services/net/java/android/net/dhcp/DhcpOfferPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpOfferPacket.java
@@ -32,8 +32,8 @@
      * Generates a OFFER packet with the specified parameters.
      */
     DhcpOfferPacket(int transId, short secs, boolean broadcast, Inet4Address serverAddress,
-                    Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac) {
-        super(transId, secs, clientIp, yourIp, INADDR_ANY, INADDR_ANY, clientMac, broadcast);
+            Inet4Address relayIp, Inet4Address clientIp, Inet4Address yourIp, byte[] clientMac) {
+        super(transId, secs, clientIp, yourIp, serverAddress, relayIp, clientMac, broadcast);
         mSrcIp = serverAddress;
     }
 
@@ -72,19 +72,8 @@
     void finishPacket(ByteBuffer buffer) {
         addTlv(buffer, DHCP_MESSAGE_TYPE, DHCP_MESSAGE_TYPE_OFFER);
         addTlv(buffer, DHCP_SERVER_IDENTIFIER, mServerIdentifier);
-        addTlv(buffer, DHCP_LEASE_TIME, mLeaseTime);
 
-        // the client should renew at 1/2 the lease-expiry interval
-        if (mLeaseTime != null) {
-            addTlv(buffer, DHCP_RENEWAL_TIME,
-                Integer.valueOf(mLeaseTime.intValue() / 2));
-        }
-
-        addTlv(buffer, DHCP_SUBNET_MASK, mSubnetMask);
-        addTlv(buffer, DHCP_ROUTER, mGateways);
-        addTlv(buffer, DHCP_DOMAIN_NAME, mDomainName);
-        addTlv(buffer, DHCP_BROADCAST_ADDRESS, mBroadcastAddress);
-        addTlv(buffer, DHCP_DNS_SERVER, mDnsServers);
+        addCommonServerTlvs(buffer);
         addTlvEnd(buffer);
     }
 }
diff --git a/services/net/java/android/net/dhcp/DhcpPacket.java b/services/net/java/android/net/dhcp/DhcpPacket.java
index 175e27e..595a129 100644
--- a/services/net/java/android/net/dhcp/DhcpPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpPacket.java
@@ -184,6 +184,11 @@
     protected String mVendorInfo;
 
     /**
+     * Value of the vendor specific option used to indicate that the network is metered
+     */
+    public static final String VENDOR_INFO_ANDROID_METERED = "ANDROID_METERED";
+
+    /**
      * DHCP Optional Type: DHCP Requested IP Address
      */
     protected static final byte DHCP_REQUESTED_IP = 50;
@@ -677,6 +682,23 @@
         if (!TextUtils.isEmpty(hn)) addTlv(buf, DHCP_HOST_NAME, hn);
     }
 
+    protected void addCommonServerTlvs(ByteBuffer buf) {
+        addTlv(buf, DHCP_LEASE_TIME, mLeaseTime);
+        if (mLeaseTime != null && mLeaseTime != INFINITE_LEASE) {
+            // The client should renew at 1/2 the lease-expiry interval
+            addTlv(buf, DHCP_RENEWAL_TIME, (int) (Integer.toUnsignedLong(mLeaseTime) / 2));
+            // Default rebinding time is set as below by RFC2131
+            addTlv(buf, DHCP_REBINDING_TIME,
+                    (int) (Integer.toUnsignedLong(mLeaseTime) * 875L / 1000L));
+        }
+        addTlv(buf, DHCP_SUBNET_MASK, mSubnetMask);
+        addTlv(buf, DHCP_BROADCAST_ADDRESS, mBroadcastAddress);
+        addTlv(buf, DHCP_ROUTER, mGateways);
+        addTlv(buf, DHCP_DNS_SERVER, mDnsServers);
+        addTlv(buf, DHCP_DOMAIN_NAME, mDomainName);
+        addTlv(buf, DHCP_VENDOR_INFO, mVendorInfo);
+    }
+
     /**
      * Converts a MAC from an array of octets to an ASCII string.
      */
@@ -1085,7 +1107,7 @@
                 break;
             case DHCP_MESSAGE_TYPE_OFFER:
                 newPacket = new DhcpOfferPacket(
-                    transactionId, secs, broadcast, ipSrc, clientIp, yourIp, clientMac);
+                    transactionId, secs, broadcast, ipSrc, relayIp, clientIp, yourIp, clientMac);
                 break;
             case DHCP_MESSAGE_TYPE_REQUEST:
                 newPacket = new DhcpRequestPacket(
@@ -1098,11 +1120,11 @@
                 break;
             case DHCP_MESSAGE_TYPE_ACK:
                 newPacket = new DhcpAckPacket(
-                    transactionId, secs, broadcast, ipSrc, clientIp, yourIp, clientMac);
+                    transactionId, secs, broadcast, ipSrc, relayIp, clientIp, yourIp, clientMac);
                 break;
             case DHCP_MESSAGE_TYPE_NAK:
                 newPacket = new DhcpNakPacket(
-                        transactionId, secs, nextIp, relayIp, clientMac, broadcast);
+                        transactionId, secs, relayIp, clientMac, broadcast);
                 break;
             case DHCP_MESSAGE_TYPE_RELEASE:
                 if (serverIdentifier == null) {
@@ -1234,12 +1256,13 @@
      * parameters.
      */
     public static ByteBuffer buildOfferPacket(int encap, int transactionId,
-        boolean broadcast, Inet4Address serverIpAddr, Inet4Address clientIpAddr,
-        byte[] mac, Integer timeout, Inet4Address netMask, Inet4Address bcAddr,
-        List<Inet4Address> gateways, List<Inet4Address> dnsServers,
-        Inet4Address dhcpServerIdentifier, String domainName) {
+        boolean broadcast, Inet4Address serverIpAddr, Inet4Address relayIp,
+        Inet4Address yourIp, byte[] mac, Integer timeout, Inet4Address netMask,
+        Inet4Address bcAddr, List<Inet4Address> gateways, List<Inet4Address> dnsServers,
+        Inet4Address dhcpServerIdentifier, String domainName, boolean metered) {
         DhcpPacket pkt = new DhcpOfferPacket(
-            transactionId, (short) 0, broadcast, serverIpAddr, INADDR_ANY, clientIpAddr, mac);
+                transactionId, (short) 0, broadcast, serverIpAddr, relayIp,
+                INADDR_ANY /* clientIp */, yourIp, mac);
         pkt.mGateways = gateways;
         pkt.mDnsServers = dnsServers;
         pkt.mLeaseTime = timeout;
@@ -1247,6 +1270,9 @@
         pkt.mServerIdentifier = dhcpServerIdentifier;
         pkt.mSubnetMask = netMask;
         pkt.mBroadcastAddress = bcAddr;
+        if (metered) {
+            pkt.mVendorInfo = VENDOR_INFO_ANDROID_METERED;
+        }
         return pkt.buildPacket(encap, DHCP_CLIENT, DHCP_SERVER);
     }
 
@@ -1254,12 +1280,13 @@
      * Builds a DHCP-ACK packet from the required specified parameters.
      */
     public static ByteBuffer buildAckPacket(int encap, int transactionId,
-        boolean broadcast, Inet4Address serverIpAddr, Inet4Address clientIpAddr,
+        boolean broadcast, Inet4Address serverIpAddr, Inet4Address relayIp, Inet4Address yourIp,
         byte[] mac, Integer timeout, Inet4Address netMask, Inet4Address bcAddr,
         List<Inet4Address> gateways, List<Inet4Address> dnsServers,
-        Inet4Address dhcpServerIdentifier, String domainName) {
+        Inet4Address dhcpServerIdentifier, String domainName, boolean metered) {
         DhcpPacket pkt = new DhcpAckPacket(
-            transactionId, (short) 0, broadcast, serverIpAddr, INADDR_ANY, clientIpAddr, mac);
+                transactionId, (short) 0, broadcast, serverIpAddr, relayIp,
+                INADDR_ANY /* clientIp */, yourIp, mac);
         pkt.mGateways = gateways;
         pkt.mDnsServers = dnsServers;
         pkt.mLeaseTime = timeout;
@@ -1267,6 +1294,9 @@
         pkt.mSubnetMask = netMask;
         pkt.mServerIdentifier = dhcpServerIdentifier;
         pkt.mBroadcastAddress = bcAddr;
+        if (metered) {
+            pkt.mVendorInfo = VENDOR_INFO_ANDROID_METERED;
+        }
         return pkt.buildPacket(encap, DHCP_CLIENT, DHCP_SERVER);
     }
 
@@ -1274,10 +1304,11 @@
      * Builds a DHCP-NAK packet from the required specified parameters.
      */
     public static ByteBuffer buildNakPacket(int encap, int transactionId, Inet4Address serverIpAddr,
-            byte[] mac, boolean broadcast, String message) {
+            Inet4Address relayIp, byte[] mac, boolean broadcast, String message) {
         DhcpPacket pkt = new DhcpNakPacket(
-                transactionId, (short) 0, serverIpAddr, serverIpAddr, mac, broadcast);
+                transactionId, (short) 0, relayIp, mac, broadcast);
         pkt.mMessage = message;
+        pkt.mServerIdentifier = serverIpAddr;
         return pkt.buildPacket(encap, DHCP_CLIENT, DHCP_SERVER);
     }
 
diff --git a/services/net/java/android/net/dhcp/DhcpServer.java b/services/net/java/android/net/dhcp/DhcpServer.java
index 095a5eb72..da8c8bb 100644
--- a/services/net/java/android/net/dhcp/DhcpServer.java
+++ b/services/net/java/android/net/dhcp/DhcpServer.java
@@ -351,12 +351,10 @@
                 mServingParams.getServerInet4Addr(), mServingParams.serverAddr.getPrefixLength());
         final ByteBuffer offerPacket = DhcpPacket.buildOfferPacket(
                 ENCAP_BOOTP, request.mTransId, broadcastFlag, mServingParams.getServerInet4Addr(),
-                lease.getNetAddr(), request.mClientMac, timeout,
-                prefixMask,
-                broadcastAddr,
-                new ArrayList<>(mServingParams.defaultRouters),
+                request.mRelayIp, lease.getNetAddr(), request.mClientMac, timeout, prefixMask,
+                broadcastAddr, new ArrayList<>(mServingParams.defaultRouters),
                 new ArrayList<>(mServingParams.dnsServers),
-                mServingParams.getServerInet4Addr(), null /* domainName */);
+                mServingParams.getServerInet4Addr(), null /* domainName */, mServingParams.metered);
 
         return transmitOfferOrAckPacket(offerPacket, request, lease, clientMac, broadcastFlag);
     }
@@ -368,12 +366,12 @@
         final boolean broadcastFlag = getBroadcastFlag(request, lease);
         final int timeout = getLeaseTimeout(lease);
         final ByteBuffer ackPacket = DhcpPacket.buildAckPacket(ENCAP_BOOTP, request.mTransId,
-                broadcastFlag, mServingParams.getServerInet4Addr(), lease.getNetAddr(),
-                request.mClientMac, timeout, mServingParams.getPrefixMaskAsAddress(),
-                mServingParams.getBroadcastAddress(),
+                broadcastFlag, mServingParams.getServerInet4Addr(), request.mRelayIp,
+                lease.getNetAddr(), request.mClientMac, timeout,
+                mServingParams.getPrefixMaskAsAddress(), mServingParams.getBroadcastAddress(),
                 new ArrayList<>(mServingParams.defaultRouters),
                 new ArrayList<>(mServingParams.dnsServers),
-                mServingParams.getServerInet4Addr(), null /* domainName */);
+                mServingParams.getServerInet4Addr(), null /* domainName */, mServingParams.metered);
 
         return transmitOfferOrAckPacket(ackPacket, request, lease, clientMac, broadcastFlag);
     }
@@ -383,7 +381,7 @@
         // Always set broadcast flag for NAK: client may not have a correct IP
         final ByteBuffer nakPacket = DhcpPacket.buildNakPacket(
                 ENCAP_BOOTP, request.mTransId, mServingParams.getServerInet4Addr(),
-                request.mClientMac, true /* broadcast */, message);
+                request.mRelayIp, request.mClientMac, true /* broadcast */, message);
 
         final Inet4Address dst = isEmpty(request.mRelayIp)
                 ? (Inet4Address) Inet4Address.ALL
diff --git a/services/net/java/android/net/dhcp/DhcpServingParams.java b/services/net/java/android/net/dhcp/DhcpServingParams.java
index 6d58bc6..df15ba1 100644
--- a/services/net/java/android/net/dhcp/DhcpServingParams.java
+++ b/services/net/java/android/net/dhcp/DhcpServingParams.java
@@ -76,6 +76,11 @@
     public final int linkMtu;
 
     /**
+     * Indicates whether the DHCP server should send the ANDROID_METERED vendor-specific option.
+     */
+    public final boolean metered;
+
+    /**
      * Checked exception thrown when some parameters used to build {@link DhcpServingParams} are
      * missing or invalid.
      */
@@ -88,13 +93,14 @@
     private DhcpServingParams(@NonNull LinkAddress serverAddr,
             @NonNull Set<Inet4Address> defaultRouters,
             @NonNull Set<Inet4Address> dnsServers, @NonNull Set<Inet4Address> excludedAddrs,
-            long dhcpLeaseTimeSecs, int linkMtu) {
+            long dhcpLeaseTimeSecs, int linkMtu, boolean metered) {
         this.serverAddr = serverAddr;
         this.defaultRouters = defaultRouters;
         this.dnsServers = dnsServers;
         this.excludedAddrs = excludedAddrs;
         this.dhcpLeaseTimeSecs = dhcpLeaseTimeSecs;
         this.linkMtu = linkMtu;
+        this.metered = metered;
     }
 
     @NonNull
@@ -134,6 +140,7 @@
         private Set<Inet4Address> excludedAddrs;
         private long dhcpLeaseTimeSecs;
         private int linkMtu = MTU_UNSET;
+        private boolean metered;
 
         /**
          * Set the server address and served prefix for the DHCP server.
@@ -248,6 +255,16 @@
         }
 
         /**
+         * Set whether the DHCP server should send the ANDROID_METERED vendor-specific option.
+         *
+         * <p>If not set, the default value is false.
+         */
+        public Builder setMetered(boolean metered) {
+            this.metered = metered;
+            return this;
+        }
+
+        /**
          * Create a new {@link DhcpServingParams} instance based on parameters set in the builder.
          *
          * <p>This method has no side-effects. If it does not throw, a valid
@@ -301,7 +318,7 @@
                     Collections.unmodifiableSet(new HashSet<>(defaultRouters)),
                     Collections.unmodifiableSet(new HashSet<>(dnsServers)),
                     Collections.unmodifiableSet(excl),
-                    dhcpLeaseTimeSecs, linkMtu);
+                    dhcpLeaseTimeSecs, linkMtu, metered);
         }
     }
 
diff --git a/services/tests/servicestests/src/com/android/server/wm/WindowManagerServiceRule.java b/services/tests/servicestests/src/com/android/server/wm/WindowManagerServiceRule.java
index 6664756..54fd7db 100644
--- a/services/tests/servicestests/src/com/android/server/wm/WindowManagerServiceRule.java
+++ b/services/tests/servicestests/src/com/android/server/wm/WindowManagerServiceRule.java
@@ -111,7 +111,7 @@
                     doReturn(input[1]).when(ims).monitorInput(anyString());
                 }
 
-                mService = WindowManagerService.main(context, ims, true, false,
+                mService = WindowManagerService.main(context, ims, false,
                         false, mPolicy = new TestWindowManagerPolicy(
                                 WindowManagerServiceRule.this::getWindowManagerService));
 
diff --git a/services/usage/java/com/android/server/usage/AppStandbyController.java b/services/usage/java/com/android/server/usage/AppStandbyController.java
index 77cb749..02ad3a8 100644
--- a/services/usage/java/com/android/server/usage/AppStandbyController.java
+++ b/services/usage/java/com/android/server/usage/AppStandbyController.java
@@ -1433,6 +1433,8 @@
                     + "): " + mCarrierPrivilegedApps);
         }
 
+        final long now = System.currentTimeMillis();
+
         pw.println();
         pw.println("Settings:");
 
@@ -1493,7 +1495,7 @@
         pw.print(" mCharging="); pw.print(mCharging);
         pw.print(" mChargingStable="); pw.print(mChargingStable);
         pw.print(" mLastAppIdleParoledTime=");
-        TimeUtils.formatDuration(mLastAppIdleParoledTime, pw);
+        TimeUtils.formatDuration(now - mLastAppIdleParoledTime, pw);
         pw.println();
         pw.print("mScreenThresholds="); pw.println(Arrays.toString(mAppStandbyScreenThresholds));
         pw.print("mElapsedThresholds="); pw.println(Arrays.toString(mAppStandbyElapsedThresholds));
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index 63945a9..57f1668 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -280,8 +280,8 @@
         }
         mControlFds.put(UsbManager.FUNCTION_MTP, mtpFd);
         FileDescriptor ptpFd = nativeOpenControl(UsbManager.USB_FUNCTION_PTP);
-        if (mtpFd == null) {
-            Slog.e(TAG, "Failed to open control for mtp");
+        if (ptpFd == null) {
+            Slog.e(TAG, "Failed to open control for ptp");
         }
         mControlFds.put(UsbManager.FUNCTION_PTP, ptpFd);
 
diff --git a/telecomm/OWNERS b/telecomm/OWNERS
index a3bcfb2..673a0a9 100644
--- a/telecomm/OWNERS
+++ b/telecomm/OWNERS
@@ -4,3 +4,4 @@
 breadley@google.com
 hallliu@google.com
 rgreenwalt@google.com
+paulye@google.com
diff --git a/telephony/OWNERS b/telephony/OWNERS
index 6f67bc2..054288b 100644
--- a/telephony/OWNERS
+++ b/telephony/OWNERS
@@ -12,3 +12,4 @@
 satk@google.com
 shuoq@google.com
 refuhoo@google.com
+paulye@google.com
diff --git a/telephony/java/android/provider/Telephony.java b/telephony/java/android/provider/Telephony.java
index b9de374..ed84788 100644
--- a/telephony/java/android/provider/Telephony.java
+++ b/telephony/java/android/provider/Telephony.java
@@ -1190,7 +1190,10 @@
              *
              * <p>Requires {@code android.Manifest.permission#CONTROL_INCALL_EXPERIENCE} to
              * send and receive.</p>
+             * @deprecated it is no longer supported, use {@link
+             * TelephonyManager#ACTION_SECRET_CODE} instead
              */
+            @Deprecated
             @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
             public static final String SECRET_CODE_ACTION =
                     "android.provider.Telephony.SECRET_CODE";
diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java
index 0cf1aec..8703e65 100644
--- a/telephony/java/android/telephony/CarrierConfigManager.java
+++ b/telephony/java/android/telephony/CarrierConfigManager.java
@@ -520,19 +520,19 @@
             "carrier_wfc_supports_wifi_only_bool";
 
     /**
-     * Default WFC_IMS_MODE for home network   0: WIFI_ONLY
-     *                                         1: CELLULAR_PREFERRED
-     *                                         2: WIFI_PREFERRED
-     * @hide
+     * Default mode for WFC over IMS on home network:
+     * <ul>
+     *   <li>0: Wi-Fi only
+     *   <li>1: prefer mobile network
+     *   <li>2: prefer Wi-Fi
+     * </ul>
      */
     public static final String KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT =
             "carrier_default_wfc_ims_mode_int";
 
     /**
-     * Default WFC_IMS_MODE for roaming
-     * See {@link KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT} for valid values.
-     *
-     * @hide
+     * Default mode for WFC over IMS on roaming network.
+     * See {@link #KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT} for meaning of values.
      */
     public static final String KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT =
             "carrier_default_wfc_ims_roaming_mode_int";
@@ -2056,6 +2056,13 @@
      */
     public static final String KEY_CALL_REDIRECTION_SERVICE_COMPONENT_NAME_STRING =
             "call_redirection_service_component_name_string";
+    /**
+     * Support for the original string display of CDMA MO call.
+     * By default, it is disabled.
+     * @hide
+     */
+    public static final String KEY_CONFIG_SHOW_ORIG_DIAL_STRING_FOR_CDMA_BOOL =
+            "config_show_orig_dial_string_for_cdma";
 
     /** The default value for every variable. */
     private final static PersistableBundle sDefaults;
@@ -2390,6 +2397,7 @@
                         -85  /* SIGNAL_STRENGTH_GREAT */
                 });
         sDefaults.putString(KEY_WCDMA_DEFAULT_SIGNAL_STRENGTH_MEASUREMENT_STRING, "");
+        sDefaults.putBoolean(KEY_CONFIG_SHOW_ORIG_DIAL_STRING_FOR_CDMA_BOOL, false);
     }
 
     /**
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index dab62f1..abcdeed 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -2498,6 +2498,25 @@
             "android.telephony.action.SIM_SLOT_STATUS_CHANGED";
 
     /**
+     * Broadcast Action: A debug code has been entered in the dialer.
+     * <p>
+     * This intent is broadcast by the system and OEM telephony apps may need to receive these
+     * broadcasts. And it requires the sender to be default dialer or has carrier privileges
+     * (see {@link #hasCarrierPrivileges}).
+     * <p>
+     * These "secret codes" are used to activate developer menus by dialing certain codes.
+     * And they are of the form {@code *#*#&lt;code&gt;#*#*}. The intent will have the data
+     * URI: {@code android_secret_code://&lt;code&gt;}. It is possible that a manifest
+     * receiver would be woken up even if it is not currently running.
+     * <p>
+     * It is supposed to replace {@link android.provider.Telephony.Sms.Intents#SECRET_CODE_ACTION}
+     * in the next Android version.
+     * Before that both of these two actions will be broadcast.
+     */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
+    public static final String ACTION_SECRET_CODE = "android.telephony.action.SECRET_CODE";
+
+    /**
      * @return true if a ICC card is present
      */
     public boolean hasIccCard() {
diff --git a/tests/net/java/android/net/dhcp/DhcpPacketTest.java b/tests/net/java/android/net/dhcp/DhcpPacketTest.java
index 050183c..312b3d1 100644
--- a/tests/net/java/android/net/dhcp/DhcpPacketTest.java
+++ b/tests/net/java/android/net/dhcp/DhcpPacketTest.java
@@ -16,6 +16,8 @@
 
 package android.net.dhcp;
 
+import static android.net.NetworkUtils.getBroadcastAddress;
+import static android.net.NetworkUtils.getPrefixMaskAsInet4Address;
 import static android.net.dhcp.DhcpPacket.*;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -29,14 +31,15 @@
 import android.net.metrics.DhcpErrorEvent;
 import android.support.test.runner.AndroidJUnit4;
 import android.support.test.filters.SmallTest;
-import android.system.OsConstants;
 
 import com.android.internal.util.HexDump;
 
+import java.io.ByteArrayOutputStream;
 import java.net.Inet4Address;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Random;
 
 import org.junit.Before;
@@ -47,13 +50,17 @@
 @SmallTest
 public class DhcpPacketTest {
 
-    private static Inet4Address SERVER_ADDR = v4Address("192.0.2.1");
-    private static Inet4Address CLIENT_ADDR = v4Address("192.0.2.234");
+    private static final Inet4Address SERVER_ADDR = v4Address("192.0.2.1");
+    private static final Inet4Address CLIENT_ADDR = v4Address("192.0.2.234");
+    private static final int PREFIX_LENGTH = 22;
+    private static final Inet4Address NETMASK = getPrefixMaskAsInet4Address(PREFIX_LENGTH);
+    private static final Inet4Address BROADCAST_ADDR = getBroadcastAddress(
+            SERVER_ADDR, PREFIX_LENGTH);
     // Use our own empty address instead of Inet4Address.ANY or INADDR_ANY to ensure that the code
     // doesn't use == instead of equals when comparing addresses.
-    private static Inet4Address ANY = (Inet4Address) v4Address("0.0.0.0");
+    private static final Inet4Address ANY = (Inet4Address) v4Address("0.0.0.0");
 
-    private static byte[] CLIENT_MAC = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
+    private static final byte[] CLIENT_MAC = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
 
     private static final Inet4Address v4Address(String addrString) throws IllegalArgumentException {
         return (Inet4Address) NetworkUtils.numericToInetAddress(addrString);
@@ -952,4 +959,96 @@
                 "\nActual:\n  " + Arrays.toString(actual);
         assertTrue(msg, Arrays.equals(expected, actual));
     }
+
+    public void checkBuildOfferPacket(int leaseTimeSecs) throws Exception {
+        final int renewalTime = (int) (Integer.toUnsignedLong(leaseTimeSecs) / 2);
+        final int rebindingTime = (int) (Integer.toUnsignedLong(leaseTimeSecs) * 875 / 1000);
+        final int transactionId = 0xdeadbeef;
+
+        final ByteBuffer packet = DhcpPacket.buildOfferPacket(
+                DhcpPacket.ENCAP_BOOTP, transactionId, false /* broadcast */,
+                SERVER_ADDR, INADDR_ANY /* relayIp */, CLIENT_ADDR /* yourIp */,
+                CLIENT_MAC, leaseTimeSecs, NETMASK /* netMask */,
+                BROADCAST_ADDR /* bcAddr */, Collections.singletonList(SERVER_ADDR) /* gateways */,
+                Collections.singletonList(SERVER_ADDR) /* dnsServers */,
+                SERVER_ADDR /* dhcpServerIdentifier */, null /* domainName */, false /* metered */);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        // BOOTP headers
+        bos.write(new byte[] {
+                (byte) 0x02, (byte) 0x01, (byte) 0x06, (byte) 0x00,
+                (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef,
+                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+                // ciaddr
+                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
+        });
+        // yiaddr
+        bos.write(CLIENT_ADDR.getAddress());
+        // siaddr
+        bos.write(SERVER_ADDR.getAddress());
+        // giaddr
+        bos.write(INADDR_ANY.getAddress());
+        // chaddr
+        bos.write(CLIENT_MAC);
+
+        // Padding
+        bos.write(new byte[202]);
+
+        // Options
+        bos.write(new byte[]{
+                // Magic cookie 0x63825363.
+                (byte) 0x63, (byte) 0x82, (byte) 0x53, (byte) 0x63,
+                // Message type OFFER.
+                (byte) 0x35, (byte) 0x01, (byte) 0x02,
+        });
+        // Server ID
+        bos.write(new byte[] { (byte) 0x36, (byte) 0x04 });
+        bos.write(SERVER_ADDR.getAddress());
+        // Lease time
+        bos.write(new byte[] { (byte) 0x33, (byte) 0x04 });
+        bos.write(intToByteArray(leaseTimeSecs));
+        if (leaseTimeSecs != INFINITE_LEASE) {
+            // Renewal time
+            bos.write(new byte[]{(byte) 0x3a, (byte) 0x04});
+            bos.write(intToByteArray(renewalTime));
+            // Rebinding time
+            bos.write(new byte[]{(byte) 0x3b, (byte) 0x04});
+            bos.write(intToByteArray(rebindingTime));
+        }
+        // Subnet mask
+        bos.write(new byte[] { (byte) 0x01, (byte) 0x04 });
+        bos.write(NETMASK.getAddress());
+        // Broadcast address
+        bos.write(new byte[] { (byte) 0x1c, (byte) 0x04 });
+        bos.write(BROADCAST_ADDR.getAddress());
+        // Router
+        bos.write(new byte[] { (byte) 0x03, (byte) 0x04 });
+        bos.write(SERVER_ADDR.getAddress());
+        // Nameserver
+        bos.write(new byte[] { (byte) 0x06, (byte) 0x04 });
+        bos.write(SERVER_ADDR.getAddress());
+        // End options.
+        bos.write(0xff);
+
+        final byte[] expected = bos.toByteArray();
+        assertTrue((expected.length & 1) == 0);
+
+        final byte[] actual = new byte[packet.limit()];
+        packet.get(actual);
+        final String msg = "Expected:\n  " + HexDump.dumpHexString(expected) +
+                "\nActual:\n  " + HexDump.dumpHexString(actual);
+        assertTrue(msg, Arrays.equals(expected, actual));
+    }
+
+    @Test
+    public void testOfferPacket() throws Exception {
+        checkBuildOfferPacket(3600);
+        checkBuildOfferPacket(Integer.MAX_VALUE);
+        checkBuildOfferPacket(0x80000000);
+        checkBuildOfferPacket(INFINITE_LEASE);
+    }
+
+    private static byte[] intToByteArray(int val) {
+        return ByteBuffer.allocate(4).putInt(val).array();
+    }
 }
diff --git a/tests/net/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachineTest.java b/tests/net/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachineTest.java
index 19d3a2e..5934653 100644
--- a/tests/net/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachineTest.java
+++ b/tests/net/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachineTest.java
@@ -16,6 +16,7 @@
 
 package com.android.server.connectivity.tethering;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
@@ -23,7 +24,9 @@
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
@@ -40,9 +43,15 @@
 
 import android.net.INetworkStatsService;
 import android.net.InterfaceConfiguration;
+import android.net.IpPrefix;
 import android.net.LinkAddress;
 import android.net.LinkProperties;
+import android.net.MacAddress;
 import android.net.RouteInfo;
+import android.net.dhcp.DhcpServer;
+import android.net.dhcp.DhcpServingParams;
+import android.net.ip.RouterAdvertisementDaemon;
+import android.net.util.InterfaceParams;
 import android.net.util.InterfaceSet;
 import android.net.util.SharedLog;
 import android.os.INetworkManagementService;
@@ -58,6 +67,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
 import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
@@ -68,33 +78,58 @@
     private static final String IFACE_NAME = "testnet1";
     private static final String UPSTREAM_IFACE = "upstream0";
     private static final String UPSTREAM_IFACE2 = "upstream1";
+    private static final int DHCP_LEASE_TIME_SECS = 3600;
+
+    private static final InterfaceParams TEST_IFACE_PARAMS = new InterfaceParams(
+            IFACE_NAME, 42 /* index */, MacAddress.ALL_ZEROS_ADDRESS, 1500 /* defaultMtu */);
 
     @Mock private INetworkManagementService mNMService;
     @Mock private INetworkStatsService mStatsService;
     @Mock private IControlsTethering mTetherHelper;
     @Mock private InterfaceConfiguration mInterfaceConfiguration;
     @Mock private SharedLog mSharedLog;
+    @Mock private DhcpServer mDhcpServer;
+    @Mock private RouterAdvertisementDaemon mRaDaemon;
     @Mock private TetheringDependencies mTetheringDependencies;
 
+    @Captor private ArgumentCaptor<DhcpServingParams> mDhcpParamsCaptor;
+
     private final TestLooper mLooper = new TestLooper();
     private final ArgumentCaptor<LinkProperties> mLinkPropertiesCaptor =
             ArgumentCaptor.forClass(LinkProperties.class);
     private TetherInterfaceStateMachine mTestedSm;
 
     private void initStateMachine(int interfaceType) throws Exception {
+        initStateMachine(interfaceType, false /* usingLegacyDhcp */);
+    }
+
+    private void initStateMachine(int interfaceType, boolean usingLegacyDhcp) throws Exception {
         mTestedSm = new TetherInterfaceStateMachine(
                 IFACE_NAME, mLooper.getLooper(), interfaceType, mSharedLog,
-                mNMService, mStatsService, mTetherHelper, mTetheringDependencies);
+                mNMService, mStatsService, mTetherHelper, usingLegacyDhcp,
+                mTetheringDependencies);
         mTestedSm.start();
         // Starting the state machine always puts us in a consistent state and notifies
         // the rest of the world that we've changed from an unknown to available state.
         mLooper.dispatchAll();
         reset(mNMService, mStatsService, mTetherHelper);
         when(mNMService.getInterfaceConfig(IFACE_NAME)).thenReturn(mInterfaceConfiguration);
+        when(mTetheringDependencies.makeDhcpServer(
+                any(), any(), mDhcpParamsCaptor.capture(), any())).thenReturn(mDhcpServer);
+        when(mTetheringDependencies.getRouterAdvertisementDaemon(any())).thenReturn(mRaDaemon);
+        when(mTetheringDependencies.getInterfaceParams(IFACE_NAME)).thenReturn(TEST_IFACE_PARAMS);
+
+        when(mRaDaemon.start()).thenReturn(true);
     }
 
-    private void initTetheredStateMachine(int interfaceType, String upstreamIface) throws Exception {
-        initStateMachine(interfaceType);
+    private void initTetheredStateMachine(int interfaceType, String upstreamIface)
+            throws Exception {
+        initTetheredStateMachine(interfaceType, upstreamIface, false);
+    }
+
+    private void initTetheredStateMachine(int interfaceType, String upstreamIface,
+            boolean usingLegacyDhcp) throws Exception {
+        initStateMachine(interfaceType, usingLegacyDhcp);
         dispatchCommand(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, STATE_TETHERED);
         if (upstreamIface != null) {
             dispatchTetherConnectionChanged(upstreamIface);
@@ -112,7 +147,7 @@
     public void startsOutAvailable() {
         mTestedSm = new TetherInterfaceStateMachine(IFACE_NAME, mLooper.getLooper(),
                 TETHERING_BLUETOOTH, mSharedLog, mNMService, mStatsService, mTetherHelper,
-                mTetheringDependencies);
+                false /* usingLegacyDhcp */, mTetheringDependencies);
         mTestedSm.start();
         mLooper.dispatchAll();
         verify(mTetherHelper).updateInterfaceState(
@@ -345,6 +380,45 @@
         }
     }
 
+    @Test
+    public void startsDhcpServer() throws Exception {
+        initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE);
+        dispatchTetherConnectionChanged(UPSTREAM_IFACE);
+
+        assertDhcpStarted(new IpPrefix("192.168.43.0/24"));
+    }
+
+    @Test
+    public void startsDhcpServerOnBluetooth() throws Exception {
+        initTetheredStateMachine(TETHERING_BLUETOOTH, UPSTREAM_IFACE);
+        dispatchTetherConnectionChanged(UPSTREAM_IFACE);
+
+        assertDhcpStarted(new IpPrefix("192.168.44.0/24"));
+    }
+
+    @Test
+    public void doesNotStartDhcpServerIfDisabled() throws Exception {
+        initTetheredStateMachine(TETHERING_WIFI, UPSTREAM_IFACE, true /* usingLegacyDhcp */);
+        dispatchTetherConnectionChanged(UPSTREAM_IFACE);
+
+        verify(mTetheringDependencies, never()).makeDhcpServer(any(), any(), any(), any());
+    }
+
+    private void assertDhcpStarted(IpPrefix expectedPrefix) {
+        verify(mTetheringDependencies, times(1)).makeDhcpServer(
+                eq(mLooper.getLooper()), eq(TEST_IFACE_PARAMS), any(), eq(mSharedLog));
+        verify(mDhcpServer, times(1)).start();
+        final DhcpServingParams params = mDhcpParamsCaptor.getValue();
+        // Last address byte is random
+        assertTrue(expectedPrefix.contains(params.serverAddr.getAddress()));
+        assertEquals(expectedPrefix.getPrefixLength(), params.serverAddr.getPrefixLength());
+        assertEquals(1, params.defaultRouters.size());
+        assertEquals(params.serverAddr.getAddress(), params.defaultRouters.iterator().next());
+        assertEquals(1, params.dnsServers.size());
+        assertEquals(params.serverAddr.getAddress(), params.dnsServers.iterator().next());
+        assertEquals(DHCP_LEASE_TIME_SECS, params.dhcpLeaseTimeSecs);
+    }
+
     /**
      * Send a command to the state machine under test, and run the event loop to idle.
      *
diff --git a/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java b/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
index b68f203..bb31230 100644
--- a/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
+++ b/tests/net/java/com/android/server/connectivity/tethering/TetheringConfigurationTest.java
@@ -21,6 +21,8 @@
 import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
 import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
 import static android.net.ConnectivityManager.TYPE_WIFI;
+import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER;
+
 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_NOT_REQUIRED;
 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_REQUIRED;
 import static com.android.server.connectivity.tethering.TetheringConfiguration.DUN_UNSPECIFIED;
@@ -29,15 +31,18 @@
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.when;
 
+import android.content.ContentResolver;
 import android.content.Context;
-import android.content.ContextWrapper;
 import android.content.res.Resources;
 import android.net.util.SharedLog;
+import android.provider.Settings;
 import android.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 import android.telephony.TelephonyManager;
+import android.test.mock.MockContentResolver;
 
 import com.android.internal.util.test.BroadcastInterceptingContext;
+import com.android.internal.util.test.FakeSettingsProvider;
 
 import java.util.Iterator;
 
@@ -55,6 +60,7 @@
     @Mock private Context mContext;
     @Mock private TelephonyManager mTelephonyManager;
     @Mock private Resources mResources;
+    private MockContentResolver mContentResolver;
     private Context mMockContext;
     private boolean mHasTelephonyManager;
 
@@ -73,6 +79,11 @@
             }
             return super.getSystemService(name);
         }
+
+        @Override
+        public ContentResolver getContentResolver() {
+            return mContentResolver;
+        }
     }
 
     @Before
@@ -86,6 +97,10 @@
                 .thenReturn(new String[]{ "test_wlan\\d" });
         when(mResources.getStringArray(com.android.internal.R.array.config_tether_bluetooth_regexs))
                 .thenReturn(new String[0]);
+        when(mResources.getIntArray(com.android.internal.R.array.config_tether_upstream_types))
+                .thenReturn(new int[0]);
+        mContentResolver = new MockContentResolver();
+        mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
         mMockContext = new MockContext(mContext);
     }
 
@@ -194,4 +209,29 @@
         assertEquals(TYPE_MOBILE_HIPRI, upstreamIterator.next().intValue());
         assertFalse(upstreamIterator.hasNext());
     }
+
+    @Test
+    public void testNewDhcpServerDisabled() {
+        Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 1);
+
+        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
+        assertTrue(cfg.enableLegacyDhcpServer);
+    }
+
+    @Test
+    public void testNewDhcpServerEnabled() {
+        Settings.Global.putInt(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0);
+
+        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
+        assertFalse(cfg.enableLegacyDhcpServer);
+    }
+
+    @Test
+    public void testNewDhcpServerDefault() {
+        Settings.Global.putString(mContentResolver, TETHER_ENABLE_LEGACY_DHCP_SERVER, null);
+
+        final TetheringConfiguration cfg = new TetheringConfiguration(mMockContext, mLog);
+        // TODO: change to false when new server is promoted to default
+        assertTrue(cfg.enableLegacyDhcpServer);
+    }
 }
diff --git a/tools/aapt/Android.bp b/tools/aapt/Android.bp
index e26c9c3..a594e5b 100644
--- a/tools/aapt/Android.bp
+++ b/tools/aapt/Android.bp
@@ -99,6 +99,17 @@
 }
 
 // ==========================================================
+// Build the host executable: aapt
+// ==========================================================
+cc_binary_host {
+    name: "aapt",
+    defaults: ["aapt_defaults"],
+    srcs: ["Main.cpp"],
+    use_version_lib: true,
+    static_libs: ["libaapt"],
+}
+
+// ==========================================================
 // Build the host tests: libaapt_tests
 // ==========================================================
 cc_test_host {
diff --git a/tools/aapt/Android.mk b/tools/aapt/Android.mk
deleted file mode 100644
index 7bcf631..0000000
--- a/tools/aapt/Android.mk
+++ /dev/null
@@ -1,51 +0,0 @@
-#
-# Copyright (C) 2014 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.
-#
-
-ifeq ($(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)),)
-
-# ==========================================================
-# Setup some common variables for the different build
-# targets here.
-# ==========================================================
-LOCAL_PATH:= $(call my-dir)
-
-aaptHostStaticLibs := \
-    libandroidfw \
-    libpng \
-    libutils \
-    liblog \
-    libcutils \
-    libexpat \
-    libziparchive \
-    libbase \
-    libz
-
-aaptCFlags := -Wall -Werror
-
-# ==========================================================
-# Build the host executable: aapt
-# ==========================================================
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := aapt
-LOCAL_MODULE_HOST_OS := darwin linux windows
-LOCAL_CFLAGS := -DAAPT_VERSION=\"$(BUILD_NUMBER_FROM_FILE)\" $(aaptCFlags)
-LOCAL_SRC_FILES := Main.cpp
-LOCAL_STATIC_LIBRARIES := libaapt $(aaptHostStaticLibs)
-
-include $(BUILD_HOST_EXECUTABLE)
-
-endif # No TARGET_BUILD_APPS or TARGET_BUILD_PDK
diff --git a/tools/aapt/Main.cpp b/tools/aapt/Main.cpp
index d714687..2f2ef92 100644
--- a/tools/aapt/Main.cpp
+++ b/tools/aapt/Main.cpp
@@ -6,6 +6,7 @@
 #include "Main.h"
 #include "Bundle.h"
 
+#include <build/version.h>
 #include <utils/Compat.h>
 #include <utils/Log.h>
 #include <utils/threads.h>
@@ -20,10 +21,6 @@
 
 static const char* gProgName = "aapt";
 
-#ifndef AAPT_VERSION
-    #define AAPT_VERSION ""
-#endif
-
 /*
  * Show version info.  All the cool kids do it.
  */
@@ -32,7 +29,7 @@
     if (bundle->getFileSpecCount() != 0) {
         printf("(ignoring extra arguments)\n");
     }
-    printf("Android Asset Packaging Tool, v0.2-" AAPT_VERSION "\n");
+    printf("Android Asset Packaging Tool, v0.2-%s\n", android::build::GetBuildNumber().c_str());
 
     return 0;
 }
diff --git a/tools/aapt2/cmd/Dump.cpp b/tools/aapt2/cmd/Dump.cpp
index 29e471e..5cb30b6 100644
--- a/tools/aapt2/cmd/Dump.cpp
+++ b/tools/aapt2/cmd/Dump.cpp
@@ -298,7 +298,7 @@
       return 1;
     }
 
-    if (loaded_apk->GetApkFormat()) {
+    if (loaded_apk->GetApkFormat() == ApkFormat::kProto) {
       printer.Println("Proto APK");
     } else {
       printer.Println("Binary APK");
@@ -356,7 +356,7 @@
   for (auto xml_file : files_) {
     android::ResXMLTree tree;
 
-    if (loaded_apk->GetApkFormat() == kProto) {
+    if (loaded_apk->GetApkFormat() == ApkFormat::kProto) {
       auto xml = loaded_apk->LoadXml(xml_file, diag_);
       if (!xml) {
         return 1;
@@ -375,7 +375,7 @@
       std::string data = buffer.to_string();
       tree.setTo(data.data(), data.size(), /** copyData */ true);
 
-    } else if (loaded_apk->GetApkFormat() == kBinary) {
+    } else if (loaded_apk->GetApkFormat() == ApkFormat::kBinary) {
       io::IFile* file = loaded_apk->GetFileCollection()->FindFile(xml_file);
       if (!file) {
         diag_->Error(DiagMessage(xml_file) << "file '" << xml_file << "' not found in APK");
diff --git a/tools/hiddenapi/generate_hiddenapi_lists.py b/tools/hiddenapi/generate_hiddenapi_lists.py
new file mode 100755
index 0000000..4a0931a
--- /dev/null
+++ b/tools/hiddenapi/generate_hiddenapi_lists.py
@@ -0,0 +1,241 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2018 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.
+"""
+Generate API lists for non-SDK API enforcement.
+
+usage: generate-hiddenapi-lists.py [-h]
+                                   --input-public INPUT_PUBLIC
+                                   --input-private INPUT_PRIVATE
+                                   [--input-whitelists [INPUT_WHITELISTS [INPUT_WHITELISTS ...]]]
+                                   [--input-greylists [INPUT_GREYLISTS [INPUT_GREYLISTS ...]]]
+                                   [--input-blacklists [INPUT_BLACKLISTS [INPUT_BLACKLISTS ...]]]
+                                   --output-whitelist OUTPUT_WHITELIST
+                                   --output-light-greylist OUTPUT_LIGHT_GREYLIST
+                                   --output-dark-greylist OUTPUT_DARK_GREYLIST
+                                   --output-blacklist OUTPUT_BLACKLIST
+"""
+import argparse
+import os
+import sys
+import re
+
+def get_args():
+    """Parses command line arguments.
+
+    Returns:
+        Namespace: dictionary of parsed arguments
+    """
+    parser = argparse.ArgumentParser()
+    parser.add_argument('--input-public', required=True, help='List of all public members')
+    parser.add_argument('--input-private', required=True, help='List of all private members')
+    parser.add_argument(
+        '--input-whitelists', nargs='*',
+        help='Lists of members to force on whitelist')
+    parser.add_argument(
+        '--input-greylists', nargs='*',
+        help='Lists of members to force on light greylist')
+    parser.add_argument(
+        '--input-blacklists', nargs='*',
+        help='Lists of members to force on blacklist')
+    parser.add_argument('--output-whitelist', required=True)
+    parser.add_argument('--output-light-greylist', required=True)
+    parser.add_argument('--output-dark-greylist', required=True)
+    parser.add_argument('--output-blacklist', required=True)
+    return parser.parse_args()
+
+def read_lines(filename):
+    """Reads entire file and return it as a list of lines.
+
+    Args:
+        filename (string): Path to the file to read from.
+
+    Returns:
+        list: Lines of the loaded file as a list of strings.
+    """
+    with open(filename, 'r') as f:
+        return f.readlines()
+
+def write_lines(filename, lines):
+    """Writes list of lines into a file, overwriting the file it it exists.
+
+    Args:
+        filename (string): Path to the file to be writting into.
+        lines (list): List of strings to write into the file.
+    """
+    with open(filename, 'w') as f:
+        f.writelines(lines)
+
+def move_between_sets(subset, src, dst, source = "<unknown>"):
+    """Removes a subset of elements from one set and add it to another.
+
+    Args:
+        subset (set): The subset of `src` to be moved from `src` to `dst`.
+        src (set): Source set. Must be a superset of `subset`.
+        dst (set): Destination set. Must be disjoint with `subset`.
+    """
+    assert src.issuperset(subset), (
+        "Error processing: {}\n"
+        "The following entries were not found:\n"
+        "{}"
+        "Please visit go/hiddenapi for more information.").format(
+            source, "".join(map(lambda x: "  " + str(x), subset.difference(src))))
+    assert dst.isdisjoint(subset)
+    # Order matters if `src` and `subset` are the same object.
+    dst.update(subset)
+    src.difference_update(subset)
+
+def get_package_name(signature):
+    """Returns the package name prefix of a class member signature.
+
+    Example: "Ljava/lang/String;->hashCode()J" --> "Ljava/lang/"
+
+    Args:
+        signature (string): Member signature
+
+    Returns
+        string: Package name of the given member
+    """
+    class_name_end = signature.find("->")
+    assert class_name_end != -1, "Invalid signature: {}".format(signature)
+    package_name_end = signature.rfind("/", 0, class_name_end)
+    assert package_name_end != -1, "Invalid signature: {}".format(signature)
+    return signature[:package_name_end + 1]
+
+def all_package_names(*args):
+    """Returns a set of packages names in given lists of member signatures.
+
+    Example: args = [ set([ "Lpkg1/ClassA;->foo()V", "Lpkg2/ClassB;->bar()J" ]),
+                      set([ "Lpkg1/ClassC;->baz()Z" ]) ]
+             return value = set([ "Lpkg1/", "Lpkg2" ])
+
+    Args:
+        *args (list): List of sets to iterate over and extract the package names
+                      of its elements (member signatures)
+
+    Returns:
+        set: All package names extracted from the given lists of signatures.
+    """
+    packages = set()
+    for arg in args:
+        packages = packages.union(map(get_package_name, arg))
+    return packages
+
+def move_all(src, dst):
+    """Moves all elements of one set to another.
+
+    Args:
+        src (set): Source set. Will become empty.
+        dst (set): Destination set. Will contain all elements of `src`.
+    """
+    move_between_sets(src, src, dst)
+
+def move_from_files(filenames, src, dst):
+    """Loads member signatures from a list of files and moves them to a given set.
+
+    Opens files in `filenames`, reads all their lines and moves those from `src`
+    set to `dst` set.
+
+    Args:
+        filenames (list): List of paths to files to be loaded.
+        src (set): Set that loaded lines should be moved from.
+        dst (set): Set that loaded lines should be moved to.
+    """
+    if filenames:
+        for filename in filenames:
+            move_between_sets(set(read_lines(filename)), src, dst, filename)
+
+def move_serialization(src, dst):
+    """Moves all members matching serialization API signatures between given sets.
+
+    Args:
+        src (set): Set that will be searched for serialization API and that API
+                   will be removed from it.
+        dst (set): Set that serialization API will be moved to.
+    """
+    serialization_patterns = [
+        r'readObject\(Ljava/io/ObjectInputStream;\)V',
+        r'readObjectNoData\(\)V',
+        r'readResolve\(\)Ljava/lang/Object;',
+        r'serialVersionUID:J',
+        r'serialPersistentFields:\[Ljava/io/ObjectStreamField;',
+        r'writeObject\(Ljava/io/ObjectOutputStream;\)V',
+        r'writeReplace\(\)Ljava/lang/Object;',
+    ]
+    regex = re.compile(r'.*->(' + '|'.join(serialization_patterns) + r')$')
+    move_between_sets(filter(lambda api: regex.match(api), src), src, dst)
+
+def move_from_packages(packages, src, dst):
+    """Moves all members of given package names from one set to another.
+
+    Args:
+        packages (list): List of string package names.
+        src (set): Set that will be searched for API matching one of the given
+                   package names. Surch API will be removed from the set.
+        dst (set): Set that matching API will be moved to.
+    """
+    move_between_sets(filter(lambda api: get_package_name(api) in packages, src), src, dst)
+
+def main(argv):
+    args = get_args()
+
+    # Initialize API sets by loading lists of public and private API. Public API
+    # are all members resolvable from SDK API stubs, other members are private.
+    # As an optimization, skip the step of moving public API from a full set of
+    # members and start with a populated whitelist.
+    whitelist = set(read_lines(args.input_public))
+    uncategorized = set(read_lines(args.input_private))
+    light_greylist = set()
+    dark_greylist = set()
+    blacklist = set()
+
+    # Assert that there is no overlap between public and private API.
+    assert whitelist.isdisjoint(uncategorized)
+    num_all_api = len(whitelist) + len(uncategorized)
+
+    # Read all files which manually assign members to specific lists.
+    move_from_files(args.input_whitelists, uncategorized, whitelist)
+    move_from_files(args.input_greylists, uncategorized, light_greylist)
+    move_from_files(args.input_blacklists, uncategorized, blacklist)
+
+    # Iterate over all uncategorized members and move serialization API to light greylist.
+    move_serialization(uncategorized, light_greylist)
+
+    # Extract package names of members from whitelist and light greylist, which
+    # are assumed to have been finalized at this point. Assign all uncategorized
+    # members from the same packages to the dark greylist.
+    dark_greylist_packages = all_package_names(whitelist, light_greylist)
+    move_from_packages(dark_greylist_packages, uncategorized, dark_greylist)
+
+    # Assign all uncategorized members to the blacklist.
+    move_all(uncategorized, blacklist)
+
+    # Assert we have not missed anything.
+    assert whitelist.isdisjoint(light_greylist)
+    assert whitelist.isdisjoint(dark_greylist)
+    assert whitelist.isdisjoint(blacklist)
+    assert light_greylist.isdisjoint(dark_greylist)
+    assert light_greylist.isdisjoint(blacklist)
+    assert dark_greylist.isdisjoint(blacklist)
+    assert num_all_api == len(whitelist) + len(light_greylist) + len(dark_greylist) + len(blacklist)
+
+    # Write final lists to disk.
+    write_lines(args.output_whitelist, whitelist)
+    write_lines(args.output_light_greylist, light_greylist)
+    write_lines(args.output_dark_greylist, dark_greylist)
+    write_lines(args.output_blacklist, blacklist)
+
+if __name__ == "__main__":
+    main(sys.argv)
diff --git a/tools/hiddenapi/generate_hiddenapi_lists_test.py b/tools/hiddenapi/generate_hiddenapi_lists_test.py
new file mode 100755
index 0000000..8f79318
--- /dev/null
+++ b/tools/hiddenapi/generate_hiddenapi_lists_test.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2018 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.
+"""Unit tests for Hidden API list generation."""
+import unittest
+from generate_hiddenapi_lists import *
+
+class TestHiddenapiListGeneration(unittest.TestCase):
+
+    def test_move_between_sets(self):
+        A = set([1, 2, 3, 4])
+        B = set([5, 6, 7, 8])
+        move_between_sets(set([2, 4]), A, B)
+        self.assertEqual(A, set([1, 3]))
+        self.assertEqual(B, set([2, 4, 5, 6, 7, 8]))
+
+    def test_move_between_sets_fail_not_superset(self):
+        A = set([1, 2, 3, 4])
+        B = set([5, 6, 7, 8])
+        with self.assertRaises(AssertionError) as ar:
+            move_between_sets(set([0, 2]), A, B)
+
+    def test_move_between_sets_fail_not_disjoint(self):
+        A = set([1, 2, 3, 4])
+        B = set([4, 5, 6, 7, 8])
+        with self.assertRaises(AssertionError) as ar:
+            move_between_sets(set([1, 4]), A, B)
+
+    def test_get_package_name(self):
+        self.assertEqual(get_package_name("Ljava/lang/String;->clone()V"), "Ljava/lang/")
+
+    def test_get_package_name_fail_no_arrow(self):
+        with self.assertRaises(AssertionError) as ar:
+            get_package_name("Ljava/lang/String;-clone()V")
+        with self.assertRaises(AssertionError) as ar:
+            get_package_name("Ljava/lang/String;>clone()V")
+        with self.assertRaises(AssertionError) as ar:
+            get_package_name("Ljava/lang/String;__clone()V")
+
+    def test_get_package_name_fail_no_package(self):
+        with self.assertRaises(AssertionError) as ar:
+            get_package_name("LString;->clone()V")
+
+    def test_all_package_names(self):
+        self.assertEqual(all_package_names(), set())
+        self.assertEqual(all_package_names(set(["Lfoo/Bar;->baz()V"])), set(["Lfoo/"]))
+        self.assertEqual(
+            all_package_names(set(["Lfoo/Bar;->baz()V", "Lfoo/BarX;->bazx()I"])),
+            set(["Lfoo/"]))
+        self.assertEqual(
+            all_package_names(
+                set(["Lfoo/Bar;->baz()V"]),
+                set(["Lfoo/BarX;->bazx()I", "Labc/xyz/Mno;->ijk()J"])),
+            set(["Lfoo/", "Labc/xyz/"]))
+
+    def test_move_all(self):
+        src = set([ "abc", "xyz" ])
+        dst = set([ "def" ])
+        move_all(src, dst)
+        self.assertEqual(src, set())
+        self.assertEqual(dst, set([ "abc", "def", "xyz" ]))
+
+    def test_move_from_packages(self):
+        src = set([ "Lfoo/bar/ClassA;->abc()J",        # will be moved
+                    "Lfoo/bar/ClassA;->def()J",        # will be moved
+                    "Lcom/pkg/example/ClassD;->ijk:J", # not moved: different package
+                    "Lfoo/bar/xyz/ClassC;->xyz()Z" ])  # not moved: subpackage
+        dst = set()
+        packages = set([ "Lfoo/bar/" ])
+        move_from_packages(packages, src, dst)
+        self.assertEqual(
+            src, set([ "Lfoo/bar/xyz/ClassC;->xyz()Z", "Lcom/pkg/example/ClassD;->ijk:J" ]))
+        self.assertEqual(
+            dst, set([ "Lfoo/bar/ClassA;->abc()J", "Lfoo/bar/ClassA;->def()J" ]))
+
+if __name__ == '__main__':
+    unittest.main()