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.Bitmap.Config;
21 import android.graphics.Canvas;
22 import android.graphics.Picture;
23 import android.graphics.Rect;
24 import android.graphics.RectF;
25 import android.os.Build;
26 import android.os.Build.VERSION_CODES;
27 
28 /**
29  * Interface representing a bitmap draw operation.
30  */
31 public interface BitmapRenderer {
32 
33     boolean USE_HARDWARE_BITMAP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
34 
createSoftwareBitmap(int width, int height, BitmapRenderer renderer)35     static Bitmap createSoftwareBitmap(int width, int height, BitmapRenderer renderer) {
36         GraphicsUtils.noteNewBitmapCreated();
37         Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
38         renderer.draw(new Canvas(result));
39         return result;
40     }
41 
42     @TargetApi(Build.VERSION_CODES.P)
createHardwareBitmap(int width, int height, BitmapRenderer renderer)43     static Bitmap createHardwareBitmap(int width, int height, BitmapRenderer renderer) {
44         if (!USE_HARDWARE_BITMAP) {
45             return createSoftwareBitmap(width, height, renderer);
46         }
47 
48         GraphicsUtils.noteNewBitmapCreated();
49         Picture picture = new Picture();
50         renderer.draw(picture.beginRecording(width, height));
51         picture.endRecording();
52         return Bitmap.createBitmap(picture);
53     }
54 
55     /**
56      * Returns a bitmap from subset of the source bitmap. The new bitmap may be the
57      * same object as source, or a copy may have been made.
58      */
createBitmap(Bitmap source, int x, int y, int width, int height)59     static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
60         if (Build.VERSION.SDK_INT >= VERSION_CODES.O && source.getConfig() == Config.HARDWARE) {
61             return createHardwareBitmap(width, height, c -> c.drawBitmap(source,
62                     new Rect(x, y, x + width, y + height), new RectF(0, 0, width, height), null));
63         } else {
64             GraphicsUtils.noteNewBitmapCreated();
65             return Bitmap.createBitmap(source, x, y, width, height);
66         }
67     }
68 
draw(Canvas out)69     void draw(Canvas out);
70 }
71