1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.compatibility.common.util; 18 19 import static org.junit.Assert.fail; 20 21 import android.graphics.Bitmap; 22 import android.graphics.Rect; 23 import android.os.Handler; 24 import android.os.HandlerThread; 25 import android.view.PixelCopy; 26 import android.view.Surface; 27 import android.view.Window; 28 import android.view.PixelCopy.OnPixelCopyFinishedListener; 29 import android.view.SurfaceView; 30 31 public class SynchronousPixelCopy implements OnPixelCopyFinishedListener { 32 private static Handler sHandler; 33 static { 34 HandlerThread thread = new HandlerThread("PixelCopyHelper"); thread.start()35 thread.start(); 36 sHandler = new Handler(thread.getLooper()); 37 } 38 39 private int mStatus = -1; 40 request(Surface source, Bitmap dest)41 public int request(Surface source, Bitmap dest) { 42 synchronized (this) { 43 PixelCopy.request(source, dest, this, sHandler); 44 return getResultLocked(); 45 } 46 } 47 request(Surface source, Rect srcRect, Bitmap dest)48 public int request(Surface source, Rect srcRect, Bitmap dest) { 49 synchronized (this) { 50 PixelCopy.request(source, srcRect, dest, this, sHandler); 51 return getResultLocked(); 52 } 53 } 54 request(SurfaceView source, Bitmap dest)55 public int request(SurfaceView source, Bitmap dest) { 56 synchronized (this) { 57 PixelCopy.request(source, dest, this, sHandler); 58 return getResultLocked(); 59 } 60 } 61 request(SurfaceView source, Rect srcRect, Bitmap dest)62 public int request(SurfaceView source, Rect srcRect, Bitmap dest) { 63 synchronized (this) { 64 PixelCopy.request(source, srcRect, dest, this, sHandler); 65 return getResultLocked(); 66 } 67 } 68 request(Window source, Bitmap dest)69 public int request(Window source, Bitmap dest) { 70 synchronized (this) { 71 PixelCopy.request(source, dest, this, sHandler); 72 return getResultLocked(); 73 } 74 } 75 request(Window source, Rect srcRect, Bitmap dest)76 public int request(Window source, Rect srcRect, Bitmap dest) { 77 synchronized (this) { 78 PixelCopy.request(source, srcRect, dest, this, sHandler); 79 return getResultLocked(); 80 } 81 } 82 getResultLocked()83 private int getResultLocked() { 84 try { 85 this.wait(250); 86 } catch (InterruptedException e) { 87 fail("PixelCopy request didn't complete within 250ms"); 88 } 89 return mStatus; 90 } 91 92 @Override onPixelCopyFinished(int copyResult)93 public void onPixelCopyFinished(int copyResult) { 94 synchronized (this) { 95 mStatus = copyResult; 96 this.notify(); 97 } 98 } 99 } 100