Skip to content

Commit

Permalink
Set FLAG_HARDWARE_ACCELERATED in ShadowWindow constructor.
Browse files Browse the repository at this point in the history
This is needed for ShadowPixelCopy to work correctly on Android S.

PiperOrigin-RevId: 628494031
  • Loading branch information
JuliaSullivanGoogle authored and Copybara-Service committed May 3, 2024
1 parent da5e70d commit 5267ca2
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
Expand Up @@ -13,7 +13,6 @@
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -57,12 +56,6 @@ static class HardwareAcceleratedActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO(hoisie): manually setting these flags should not be required. Robolectric should
// set them automatically by default (they have been default since ICS).
getWindow()
.setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Expand Down
Expand Up @@ -34,10 +34,11 @@ private HardwareRenderingScreenshot() {}
* the presence of the {@link #USE_HARDWARE_RENDERER_NATIVE_ENV} property, and the {@link
* GraphicsMode}.
*/
static boolean canTakeScreenshot() {
static boolean canTakeScreenshot(View view) {
return VERSION.SDK_INT >= VERSION_CODES.S
&& "hardware".equalsIgnoreCase(System.getProperty(PIXEL_COPY_RENDER_MODE, ""))
&& ShadowView.useRealGraphics();
&& ShadowView.useRealGraphics()
&& view.canHaveDisplayList();
}

/**
Expand Down Expand Up @@ -68,7 +69,7 @@ static void takeScreenshot(View view, Bitmap destBitmap) {

RenderNode node = getRenderNode(view);
renderer.setContentRoot(node);

renderer.createRenderRequest().syncAndDraw();
Plane[] planes = nativeImage.getPlanes();
destBitmap.copyPixelsFromBuffer(planes[0].getBuffer());
Expand Down
Expand Up @@ -204,12 +204,13 @@ public class ShadowPackageManager {
* @return existing or newly created activity info.
*/
public ActivityInfo addActivityIfNotPresent(ComponentName componentName) {
ActivityInfo activityInfo = updateName(componentName, new ActivityInfo());
// if (RuntimeEnvironment.getApiLevel() >= S
// && System.getProperty("robolectric.pixelCopyRenderMode", "").equals("hardware")) {
activityInfo.flags |= ActivityInfo.FLAG_HARDWARE_ACCELERATED;
// }
return addComponent(
activityFilters,
p -> p.activities,
(p, a) -> p.activities = a,
updateName(componentName, new ActivityInfo()),
false);
activityFilters, p -> p.activities, (p, a) -> p.activities = a, activityInfo, false);
}

/**
Expand Down
Expand Up @@ -168,7 +168,7 @@ private static void takeScreenshot(View view, Bitmap screenshot, @Nullable Rect

Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);

if (HardwareRenderingScreenshot.canTakeScreenshot()) {
if (HardwareRenderingScreenshot.canTakeScreenshot(view)) {
PerfStatsCollector.getInstance()
.measure(
"ShadowPixelCopy-Hardware",
Expand Down
Expand Up @@ -131,7 +131,7 @@ protected Bitmap takeScreenshot() throws Exception {
Bitmap window =
Bitmap.createBitmap(
rootView.getWidth(), rootView.getHeight(), Bitmap.Config.ARGB_8888);
if (HardwareRenderingScreenshot.canTakeScreenshot()) {
if (HardwareRenderingScreenshot.canTakeScreenshot(rootView)) {
HardwareRenderingScreenshot.takeScreenshot(rootView, window);
} else {
Canvas windowCanvas = new Canvas(window);
Expand Down

0 comments on commit 5267ca2

Please sign in to comment.