Home | History | Annotate | Download | only in walt
      1 /*
      2  * Copyright (C) 2017 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 org.chromium.latency.walt;
     18 
     19 import android.content.Context;
     20 import android.graphics.Canvas;
     21 import android.graphics.Color;
     22 import android.graphics.Paint;
     23 import android.graphics.Rect;
     24 import android.os.Build;
     25 import android.support.annotation.RequiresApi;
     26 import android.util.AttributeSet;
     27 import android.view.Surface;
     28 import android.view.SurfaceHolder;
     29 import android.view.SurfaceView;
     30 import android.widget.Toast;
     31 
     32 import java.lang.reflect.InvocationTargetException;
     33 import java.lang.reflect.Method;
     34 
     35 class FastPathSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
     36 
     37     private boolean isActive = false;
     38 
     39     public FastPathSurfaceView(Context context, AttributeSet attrs) {
     40         super(context, attrs);
     41         getHolder().addCallback(this);
     42         setZOrderOnTop(true);
     43     }
     44 
     45     @RequiresApi(api = Build.VERSION_CODES.KITKAT)
     46     @Override
     47     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
     48         Surface surface = holder.getSurface();
     49         if (surface == null)
     50             return;
     51 
     52         try {
     53             Method setSharedBufferMode = Surface.class.getMethod("setSharedBufferMode", boolean.class);
     54             setSharedBufferMode.invoke(surface, true);
     55             displayMessage("Using shared buffer mode.");
     56         } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
     57             displayMessage("Shared buffer mode is not supported.");
     58         }
     59         Canvas canvas = surface.lockCanvas(null);
     60         canvas.drawColor(Color.GRAY);
     61         surface.unlockCanvasAndPost(canvas);
     62     }
     63 
     64     @Override
     65     public void surfaceCreated(SurfaceHolder holder) {
     66         isActive = true;
     67     }
     68 
     69     @Override
     70     public void surfaceDestroyed(SurfaceHolder holder) {
     71         isActive = false;
     72     }
     73 
     74     private void displayMessage(String message) {
     75         Toast toast = Toast.makeText(getContext(), message, Toast.LENGTH_SHORT);
     76         toast.show();
     77     }
     78 
     79     public void setRectColor(int color) {
     80         Surface surface = getHolder().getSurface();
     81         if (surface == null || !isActive)
     82             return;
     83         Rect rect = new Rect(10, 10, 310, 310);
     84         Canvas canvas = surface.lockCanvas(rect);
     85         Paint paint = new Paint();
     86         paint.setColor(color);
     87         canvas.drawRect(rect, paint);
     88         surface.unlockCanvasAndPost(canvas);
     89     }
     90 }
     91