1 /*
2 * Copyright 2023 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
17 package androidx.graphics
18
19 import android.graphics.Bitmap
20 import android.graphics.Canvas
21 import android.graphics.Paint
22 import android.opengl.EGL14
23 import androidx.graphics.opengl.egl.EGLConfigAttributes
24 import androidx.graphics.opengl.egl.EGLManager
25 import androidx.graphics.opengl.egl.EGLSpec
26 import androidx.graphics.opengl.egl.EGLVersion
27 import org.junit.Assert
28
drawSquaresnull29 fun drawSquares(
30 canvas: Canvas,
31 width: Int,
32 height: Int,
33 topLeft: Int,
34 topRight: Int,
35 bottomLeft: Int,
36 bottomRight: Int
37 ) {
38 val paint = Paint()
39 val widthF = width.toFloat()
40 val heightF = height.toFloat()
41 val halfWidth = widthF / 2f
42 val halfHeight = heightF / 2f
43 canvas.drawRect(0f, 0f, halfWidth, halfHeight, paint.apply { color = topLeft })
44 canvas.drawRect(halfWidth, 0f, widthF, halfHeight, paint.apply { color = topRight })
45 canvas.drawRect(0f, halfHeight, halfWidth, heightF, paint.apply { color = bottomLeft })
46 canvas.drawRect(halfWidth, halfHeight, widthF, heightF, paint.apply { color = bottomRight })
47 }
48
Bitmapnull49 fun Bitmap.verifyQuadrants(topLeft: Int, topRight: Int, bottomLeft: Int, bottomRight: Int) {
50 Assert.assertEquals(topLeft, getPixel(1, 1))
51 Assert.assertEquals(topLeft, getPixel(width / 2 - 2, 1))
52 Assert.assertEquals(topLeft, getPixel(width / 2 - 2, height / 2 - 2))
53 Assert.assertEquals(topLeft, getPixel(1, height / 2 - 2))
54
55 Assert.assertEquals(topRight, getPixel(width / 2 + 2, 1))
56 Assert.assertEquals(topRight, getPixel(width - 2, 1))
57 Assert.assertEquals(topRight, getPixel(width - 2, height / 2 - 2))
58 Assert.assertEquals(topRight, getPixel(width / 2 + 2, height / 2 - 2))
59
60 Assert.assertEquals(bottomLeft, getPixel(1, height / 2 + 2))
61 Assert.assertEquals(bottomLeft, getPixel(width / 2 - 2, height / 2 + 2))
62 Assert.assertEquals(bottomLeft, getPixel(width / 2 - 2, height - 2))
63 Assert.assertEquals(bottomLeft, getPixel(1, height - 2))
64
65 Assert.assertEquals(bottomRight, getPixel(width / 2 + 2, height / 2 + 2))
66 Assert.assertEquals(bottomRight, getPixel(width - 2, height / 2 + 2))
67 Assert.assertEquals(bottomRight, getPixel(width - 2, height - 2))
68 Assert.assertEquals(bottomRight, getPixel(width / 2 + 2, height - 2))
69 }
70
Bitmapnull71 fun Bitmap.isAllColor(targetColor: Int): Boolean {
72 for (i in 0 until width) {
73 for (j in 0 until height) {
74 if (getPixel(i, j) != targetColor) {
75 return false
76 }
77 }
78 }
79 return true
80 }
81
withEglnull82 fun withEgl(block: (egl: EGLManager) -> Unit) {
83 val egl = createAndSetupEGLManager(EGLSpec.V14)
84 try {
85 block(egl)
86 } finally {
87 releaseEGLManager(egl)
88 }
89 }
90
91 // Helper method to create and initialize an EGLManager
createAndSetupEGLManagernull92 private fun createAndSetupEGLManager(eglSpec: EGLSpec = EGLSpec.V14): EGLManager {
93 val egl = EGLManager(eglSpec)
94 Assert.assertEquals(EGLVersion.Unknown, egl.eglVersion)
95 Assert.assertEquals(EGL14.EGL_NO_CONTEXT, egl.eglContext)
96
97 egl.initialize()
98
99 val config = egl.loadConfig(EGLConfigAttributes.RGBA_8888)
100 if (config == null) {
101 Assert.fail("Config 888 should be supported")
102 }
103
104 egl.createContext(config!!)
105 return egl
106 }
107
108 // Helper method to release EGLManager
releaseEGLManagernull109 private fun releaseEGLManager(egl: EGLManager) {
110 egl.release()
111 Assert.assertEquals(EGLVersion.Unknown, egl.eglVersion)
112 Assert.assertEquals(EGL14.EGL_NO_CONTEXT, egl.eglContext)
113 }
114