xref: /aosp_15_r20/frameworks/rs/toolkit/test/IntrinsicBlend.kt (revision e1eccf28f96817838ad6867f7f39d2351ec11f56)
1*e1eccf28SAndroid Build Coastguard Worker /*
2*e1eccf28SAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*e1eccf28SAndroid Build Coastguard Worker  *
4*e1eccf28SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1eccf28SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1eccf28SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1eccf28SAndroid Build Coastguard Worker  *
8*e1eccf28SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1eccf28SAndroid Build Coastguard Worker  *
10*e1eccf28SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1eccf28SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1eccf28SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1eccf28SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1eccf28SAndroid Build Coastguard Worker  * limitations under the License.
15*e1eccf28SAndroid Build Coastguard Worker  */
16*e1eccf28SAndroid Build Coastguard Worker 
17*e1eccf28SAndroid Build Coastguard Worker package com.example.testapp
18*e1eccf28SAndroid Build Coastguard Worker 
19*e1eccf28SAndroid Build Coastguard Worker import android.graphics.Bitmap
20*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.Allocation
21*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.Element
22*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.RenderScript
23*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.Script
24*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.ScriptIntrinsicBlend
25*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.Type
26*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.toolkit.BlendingMode
27*e1eccf28SAndroid Build Coastguard Worker import android.renderscript.toolkit.Range2d
28*e1eccf28SAndroid Build Coastguard Worker 
29*e1eccf28SAndroid Build Coastguard Worker /**
30*e1eccf28SAndroid Build Coastguard Worker  * Does a Blend operation using the RenderScript Intrinsics.
31*e1eccf28SAndroid Build Coastguard Worker  */
intrinsicBlendnull32*e1eccf28SAndroid Build Coastguard Worker fun intrinsicBlend(
33*e1eccf28SAndroid Build Coastguard Worker     context: RenderScript,
34*e1eccf28SAndroid Build Coastguard Worker     mode: BlendingMode,
35*e1eccf28SAndroid Build Coastguard Worker     sourceArray: ByteArray,
36*e1eccf28SAndroid Build Coastguard Worker     destArray: ByteArray,
37*e1eccf28SAndroid Build Coastguard Worker     sizeX: Int,
38*e1eccf28SAndroid Build Coastguard Worker     sizeY: Int,
39*e1eccf28SAndroid Build Coastguard Worker     restriction: Range2d?
40*e1eccf28SAndroid Build Coastguard Worker ) {
41*e1eccf28SAndroid Build Coastguard Worker     val scriptBlend = ScriptIntrinsicBlend.create(context, Element.U8_4(context))
42*e1eccf28SAndroid Build Coastguard Worker     val builder = Type.Builder(context, Element.U8_4(context))
43*e1eccf28SAndroid Build Coastguard Worker     builder.setX(sizeX)
44*e1eccf28SAndroid Build Coastguard Worker     builder.setY(sizeY)
45*e1eccf28SAndroid Build Coastguard Worker     val arrayType = builder.create()
46*e1eccf28SAndroid Build Coastguard Worker     val sourceAllocation = Allocation.createTyped(context, arrayType)
47*e1eccf28SAndroid Build Coastguard Worker     val destAllocation = Allocation.createTyped(context, arrayType)
48*e1eccf28SAndroid Build Coastguard Worker     sourceAllocation.copyFrom(sourceArray)
49*e1eccf28SAndroid Build Coastguard Worker     destAllocation.copyFrom(destArray)
50*e1eccf28SAndroid Build Coastguard Worker 
51*e1eccf28SAndroid Build Coastguard Worker     callBlendForEach(scriptBlend, sourceAllocation, destAllocation, mode, restriction)
52*e1eccf28SAndroid Build Coastguard Worker     destAllocation.copyTo(destArray)
53*e1eccf28SAndroid Build Coastguard Worker 
54*e1eccf28SAndroid Build Coastguard Worker     sourceAllocation.destroy()
55*e1eccf28SAndroid Build Coastguard Worker     destAllocation.destroy()
56*e1eccf28SAndroid Build Coastguard Worker     arrayType.destroy()
57*e1eccf28SAndroid Build Coastguard Worker     scriptBlend.destroy()
58*e1eccf28SAndroid Build Coastguard Worker }
59*e1eccf28SAndroid Build Coastguard Worker 
intrinsicBlendnull60*e1eccf28SAndroid Build Coastguard Worker fun intrinsicBlend(
61*e1eccf28SAndroid Build Coastguard Worker     context: RenderScript,
62*e1eccf28SAndroid Build Coastguard Worker     mode: BlendingMode,
63*e1eccf28SAndroid Build Coastguard Worker     sourceBitmap: Bitmap,
64*e1eccf28SAndroid Build Coastguard Worker     destBitmap: Bitmap,
65*e1eccf28SAndroid Build Coastguard Worker     restriction: Range2d?
66*e1eccf28SAndroid Build Coastguard Worker ) {
67*e1eccf28SAndroid Build Coastguard Worker     val scriptBlend = ScriptIntrinsicBlend.create(context, Element.U8_4(context))
68*e1eccf28SAndroid Build Coastguard Worker     val sourceAllocation = Allocation.createFromBitmap(context, sourceBitmap)
69*e1eccf28SAndroid Build Coastguard Worker     val destAllocation = Allocation.createFromBitmap(context, destBitmap)
70*e1eccf28SAndroid Build Coastguard Worker     sourceAllocation.copyFrom(sourceBitmap)
71*e1eccf28SAndroid Build Coastguard Worker     destAllocation.copyFrom(destBitmap)
72*e1eccf28SAndroid Build Coastguard Worker 
73*e1eccf28SAndroid Build Coastguard Worker     callBlendForEach(scriptBlend, sourceAllocation, destAllocation, mode, restriction)
74*e1eccf28SAndroid Build Coastguard Worker     destAllocation.copyTo(destBitmap)
75*e1eccf28SAndroid Build Coastguard Worker 
76*e1eccf28SAndroid Build Coastguard Worker     sourceAllocation.destroy()
77*e1eccf28SAndroid Build Coastguard Worker     destAllocation.destroy()
78*e1eccf28SAndroid Build Coastguard Worker     scriptBlend.destroy()
79*e1eccf28SAndroid Build Coastguard Worker }
80*e1eccf28SAndroid Build Coastguard Worker 
callBlendForEachnull81*e1eccf28SAndroid Build Coastguard Worker private fun callBlendForEach(
82*e1eccf28SAndroid Build Coastguard Worker     scriptBlend: ScriptIntrinsicBlend,
83*e1eccf28SAndroid Build Coastguard Worker     sourceAllocation: Allocation,
84*e1eccf28SAndroid Build Coastguard Worker     destAllocation: Allocation,
85*e1eccf28SAndroid Build Coastguard Worker     mode: BlendingMode,
86*e1eccf28SAndroid Build Coastguard Worker     restriction: Range2d?
87*e1eccf28SAndroid Build Coastguard Worker ) {
88*e1eccf28SAndroid Build Coastguard Worker     if (restriction != null) {
89*e1eccf28SAndroid Build Coastguard Worker         val options = Script.LaunchOptions()
90*e1eccf28SAndroid Build Coastguard Worker         options.setX(restriction.startX, restriction.endX)
91*e1eccf28SAndroid Build Coastguard Worker         options.setY(restriction.startY, restriction.endY)
92*e1eccf28SAndroid Build Coastguard Worker         when (mode) {
93*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.CLEAR -> scriptBlend.forEachClear(
94*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
95*e1eccf28SAndroid Build Coastguard Worker             )
96*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC -> scriptBlend.forEachSrc(
97*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
98*e1eccf28SAndroid Build Coastguard Worker             )
99*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST -> scriptBlend.forEachDst(
100*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
101*e1eccf28SAndroid Build Coastguard Worker             )
102*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_OVER -> scriptBlend.forEachSrcOver(
103*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
104*e1eccf28SAndroid Build Coastguard Worker             )
105*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_OVER -> scriptBlend.forEachDstOver(
106*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
107*e1eccf28SAndroid Build Coastguard Worker             )
108*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_IN -> scriptBlend.forEachSrcIn(
109*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
110*e1eccf28SAndroid Build Coastguard Worker             )
111*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_IN -> scriptBlend.forEachDstIn(
112*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
113*e1eccf28SAndroid Build Coastguard Worker             )
114*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_OUT -> scriptBlend.forEachSrcOut(
115*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
116*e1eccf28SAndroid Build Coastguard Worker             )
117*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_OUT -> scriptBlend.forEachDstOut(
118*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
119*e1eccf28SAndroid Build Coastguard Worker             )
120*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_ATOP -> scriptBlend.forEachSrcAtop(
121*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
122*e1eccf28SAndroid Build Coastguard Worker             )
123*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_ATOP -> scriptBlend.forEachDstAtop(
124*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
125*e1eccf28SAndroid Build Coastguard Worker             )
126*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.XOR -> scriptBlend.forEachXor(
127*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
128*e1eccf28SAndroid Build Coastguard Worker             )
129*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.MULTIPLY -> scriptBlend.forEachMultiply(
130*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
131*e1eccf28SAndroid Build Coastguard Worker             )
132*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.ADD -> scriptBlend.forEachAdd(
133*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
134*e1eccf28SAndroid Build Coastguard Worker             )
135*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SUBTRACT -> scriptBlend.forEachSubtract(
136*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation, options
137*e1eccf28SAndroid Build Coastguard Worker             )
138*e1eccf28SAndroid Build Coastguard Worker         }
139*e1eccf28SAndroid Build Coastguard Worker     } else {
140*e1eccf28SAndroid Build Coastguard Worker         when (mode) {
141*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.CLEAR -> scriptBlend.forEachClear(
142*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
143*e1eccf28SAndroid Build Coastguard Worker             )
144*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC -> scriptBlend.forEachSrc(
145*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
146*e1eccf28SAndroid Build Coastguard Worker             )
147*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST -> scriptBlend.forEachDst(
148*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
149*e1eccf28SAndroid Build Coastguard Worker             )
150*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_OVER -> scriptBlend.forEachSrcOver(
151*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
152*e1eccf28SAndroid Build Coastguard Worker             )
153*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_OVER -> scriptBlend.forEachDstOver(
154*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
155*e1eccf28SAndroid Build Coastguard Worker             )
156*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_IN -> scriptBlend.forEachSrcIn(
157*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
158*e1eccf28SAndroid Build Coastguard Worker             )
159*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_IN -> scriptBlend.forEachDstIn(
160*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
161*e1eccf28SAndroid Build Coastguard Worker             )
162*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_OUT -> scriptBlend.forEachSrcOut(
163*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
164*e1eccf28SAndroid Build Coastguard Worker             )
165*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_OUT -> scriptBlend.forEachDstOut(
166*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
167*e1eccf28SAndroid Build Coastguard Worker             )
168*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SRC_ATOP -> scriptBlend.forEachSrcAtop(
169*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
170*e1eccf28SAndroid Build Coastguard Worker             )
171*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.DST_ATOP -> scriptBlend.forEachDstAtop(
172*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
173*e1eccf28SAndroid Build Coastguard Worker             )
174*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.XOR -> scriptBlend.forEachXor(
175*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
176*e1eccf28SAndroid Build Coastguard Worker             )
177*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.MULTIPLY -> scriptBlend.forEachMultiply(
178*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
179*e1eccf28SAndroid Build Coastguard Worker             )
180*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.ADD -> scriptBlend.forEachAdd(
181*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
182*e1eccf28SAndroid Build Coastguard Worker             )
183*e1eccf28SAndroid Build Coastguard Worker             BlendingMode.SUBTRACT -> scriptBlend.forEachSubtract(
184*e1eccf28SAndroid Build Coastguard Worker                 sourceAllocation, destAllocation
185*e1eccf28SAndroid Build Coastguard Worker             )
186*e1eccf28SAndroid Build Coastguard Worker         }
187*e1eccf28SAndroid Build Coastguard Worker     }
188*e1eccf28SAndroid Build Coastguard Worker }
189