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.icons;
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 /**
25  * Interface representing a bitmap draw operation.
26  */
27 public interface BitmapRenderer {
28 
29     boolean USE_HARDWARE_BITMAP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
30 
createSoftwareBitmap(int width, int height, BitmapRenderer renderer)31     static Bitmap createSoftwareBitmap(int width, int height, BitmapRenderer renderer) {
32         Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
33         renderer.draw(new Canvas(result));
34         return result;
35     }
36 
37     @TargetApi(Build.VERSION_CODES.P)
createHardwareBitmap(int width, int height, BitmapRenderer renderer)38     static Bitmap createHardwareBitmap(int width, int height, BitmapRenderer renderer) {
39         if (!USE_HARDWARE_BITMAP) {
40             return createSoftwareBitmap(width, height, renderer);
41         }
42 
43         Picture picture = new Picture();
44         renderer.draw(picture.beginRecording(width, height));
45         picture.endRecording();
46         return Bitmap.createBitmap(picture);
47     }
48 
draw(Canvas out)49     void draw(Canvas out);
50 }
51