blob: 52b8cc2b685b7131ac7f1b3615021b340cb2b563 [file] [log] [blame]
Lucas Dupinba57b4a2019-01-21 14:55:57 -08001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.statusbar
18
19import android.annotation.ColorInt
20import android.content.Context
21import android.graphics.Bitmap
22import android.graphics.Canvas
23import android.graphics.Point
24import android.graphics.Rect
25import android.renderscript.Allocation
26import android.renderscript.Element
27import android.renderscript.RenderScript
28import android.renderscript.ScriptIntrinsicBlur
29import android.util.MathUtils
30import com.android.internal.graphics.ColorUtils
31
32import javax.inject.Inject
33import javax.inject.Singleton
34
35private const val COLOR_ALPHA = (255 * 0.7f).toInt()
36private const val BLUR_RADIUS = 25f
37private const val DOWNSAMPLE = 6
38
39@Singleton
40class MediaArtworkProcessor @Inject constructor() {
41
42 private val mTmpSize = Point()
43 private var mArtworkCache: Bitmap? = null
44
45 fun processArtwork(context: Context, artwork: Bitmap, @ColorInt color: Int): Bitmap {
46 if (mArtworkCache != null) {
47 return mArtworkCache!!
48 }
49
50 context.display.getSize(mTmpSize)
51 val renderScript = RenderScript.create(context)
Lucas Dupin9afac5a2019-02-14 15:26:51 -080052 val rect = Rect(0, 0, artwork.width, artwork.height)
Lucas Dupinba57b4a2019-01-21 14:55:57 -080053 MathUtils.fitRect(rect, Math.max(mTmpSize.x / DOWNSAMPLE, mTmpSize.y / DOWNSAMPLE))
54 val inBitmap = Bitmap.createScaledBitmap(artwork, rect.width(), rect.height(),
55 true /* filter */)
56 val input = Allocation.createFromBitmap(renderScript, inBitmap,
57 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE)
58 val outBitmap = Bitmap.createBitmap(inBitmap.width, inBitmap.height,
59 Bitmap.Config.ARGB_8888)
60 val output = Allocation.createFromBitmap(renderScript, outBitmap)
61 val blur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))
62 blur.setRadius(BLUR_RADIUS)
63 blur.setInput(input)
64 blur.forEach(output)
65 output.copyTo(outBitmap)
66
67 input.destroy()
68 output.destroy()
69 inBitmap.recycle()
Lucas Dupin9afac5a2019-02-14 15:26:51 -080070 blur.destroy()
Lucas Dupinba57b4a2019-01-21 14:55:57 -080071
72 val canvas = Canvas(outBitmap)
73 canvas.drawColor(ColorUtils.setAlphaComponent(color, COLOR_ALPHA))
74 return outBitmap
75 }
76
77 fun clearCache() {
78 mArtworkCache?.recycle()
79 mArtworkCache = null
80 }
81}