Home | History | Annotate | Download | only in graphics
      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 package com.android.launcher3.graphics;
     17 
     18 import android.annotation.TargetApi;
     19 import android.graphics.Bitmap;
     20 import android.graphics.Canvas;
     21 import android.graphics.Picture;
     22 import android.os.Build;
     23 
     24 import com.android.launcher3.Utilities;
     25 
     26 public class BitmapRenderer {
     27 
     28      public static final boolean USE_HARDWARE_BITMAP = Utilities.ATLEAST_P;
     29 
     30      public static Bitmap createSoftwareBitmap(int width, int height, Renderer renderer) {
     31           Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     32           renderer.draw(new Canvas(result));
     33           return result;
     34      }
     35 
     36      @TargetApi(Build.VERSION_CODES.P)
     37      public static Bitmap createHardwareBitmap(int width, int height, Renderer renderer) {
     38           if (!USE_HARDWARE_BITMAP) {
     39                return createSoftwareBitmap(width, height, renderer);
     40           }
     41 
     42           Picture picture = new Picture();
     43           renderer.draw(picture.beginRecording(width, height));
     44           picture.endRecording();
     45           return Bitmap.createBitmap(picture);
     46      }
     47 
     48      /**
     49       * Interface representing a bitmap draw operation.
     50       */
     51      public interface Renderer {
     52           void draw(Canvas out);
     53      }
     54 }
     55