1 /* 2 * Copyright (C) 2016 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 android.opengl.cts; 18 19 import static android.opengl.GLES20.GL_ALPHA; 20 import static android.opengl.GLES30.GL_ARRAY_BUFFER; 21 import static android.opengl.GLES30.GL_BUFFER_MAP_POINTER; 22 import static android.opengl.GLES30.GL_COLOR_ATTACHMENT0; 23 import static android.opengl.GLES30.GL_DYNAMIC_READ; 24 import static android.opengl.GLES30.GL_FRAMEBUFFER; 25 import static android.opengl.GLES30.GL_FRAMEBUFFER_COMPLETE; 26 import static android.opengl.GLES30.GL_MAP_READ_BIT; 27 import static android.opengl.GLES30.GL_NO_ERROR; 28 import static android.opengl.GLES30.GL_RGBA; 29 import static android.opengl.GLES30.GL_STATIC_DRAW; 30 import static android.opengl.GLES30.GL_TEXTURE_2D; 31 import static android.opengl.GLES30.GL_UNIFORM_BUFFER; 32 import static android.opengl.GLES30.GL_UNSIGNED_BYTE; 33 import static android.opengl.GLES30.glBindBuffer; 34 import static android.opengl.GLES30.glBindFramebuffer; 35 import static android.opengl.GLES30.glBindTexture; 36 import static android.opengl.GLES30.glBufferData; 37 import static android.opengl.GLES30.glCheckFramebufferStatus; 38 import static android.opengl.GLES30.glDeleteBuffers; 39 import static android.opengl.GLES30.glDeleteTextures; 40 import static android.opengl.GLES30.glFramebufferTexture2D; 41 import static android.opengl.GLES30.glGenBuffers; 42 import static android.opengl.GLES30.glGenFramebuffers; 43 import static android.opengl.GLES30.glGenTextures; 44 import static android.opengl.GLES30.glGetBufferPointerv; 45 import static android.opengl.GLES30.glGetError; 46 import static android.opengl.GLES30.glMapBufferRange; 47 import static android.opengl.GLES30.glReadPixels; 48 import static android.opengl.GLES30.glTexImage2D; 49 import static android.opengl.GLES30.glUnmapBuffer; 50 import static android.opengl.GLES30.glViewport; 51 import static org.junit.Assert.assertEquals; 52 import static org.junit.Assert.assertNotNull; 53 import static org.junit.Assert.assertTrue; 54 55 import androidx.test.filters.SmallTest; 56 import java.nio.Buffer; 57 import java.nio.ByteBuffer; 58 import java.nio.IntBuffer; 59 import org.junit.Test; 60 import org.junit.runner.RunWith; 61 import org.junit.runners.BlockJUnit4ClassRunner; 62 63 /** 64 * Tests for functions that return a ByteBuffer or accept a ByteBuffer as an argument 65 */ 66 @SmallTest 67 @RunWith(BlockJUnit4ClassRunner.class) // DO NOT USE AndroidJUnit4, it messes up threading 68 public class ByteBufferTest extends GlTestBase { 69 70 private static final byte[] byteArray = {0, 63, 127, 127, 15}; 71 72 @Test testMapBufferRange()73 public void testMapBufferRange() { 74 // Always pass on ES 2.0 75 if (Egl14Utils.getMajorVersion() >= 3) { 76 int[] buffer = new int[1]; 77 glGenBuffers(1, buffer, 0); 78 glBindBuffer(GL_UNIFORM_BUFFER, buffer[0]); 79 glBufferData(GL_UNIFORM_BUFFER, 1024, null, GL_DYNAMIC_READ); 80 81 Buffer mappedBuffer = glMapBufferRange(GL_UNIFORM_BUFFER, 0, 1024, GL_MAP_READ_BIT); 82 83 assertNotNull(mappedBuffer); 84 assertTrue(mappedBuffer instanceof ByteBuffer); 85 86 Buffer pointerBuffer = glGetBufferPointerv(GL_UNIFORM_BUFFER, GL_BUFFER_MAP_POINTER); 87 assertNotNull(pointerBuffer); 88 assertTrue(pointerBuffer instanceof ByteBuffer); 89 90 glUnmapBuffer(GL_UNIFORM_BUFFER); 91 92 glBindBuffer(GL_UNIFORM_BUFFER, 0); 93 glDeleteBuffers(1, buffer, 0); 94 } 95 } 96 97 @Test testArrayBackedTexImage2d()98 public void testArrayBackedTexImage2d() throws Throwable { 99 IntBuffer textureHandles = IntBuffer.allocate(1); 100 ByteBuffer texelData = ByteBuffer.wrap(byteArray); 101 // Setting position to 1 will cause problems if the bindings don't 102 // correctly handle the buffer offset when releasing the pointer 103 texelData.position(1); 104 glGenTextures(1, textureHandles); 105 int textureHandle = textureHandles.get(0); 106 glBindTexture(GL_TEXTURE_2D, textureHandle); 107 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 2, 2, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texelData); 108 assertEquals(glGetError(), GL_NO_ERROR); 109 glDeleteTextures(1, textureHandles); 110 } 111 112 @Test testArrayBackedBufferData()113 public void testArrayBackedBufferData() throws Throwable { 114 IntBuffer bufferHandles = IntBuffer.allocate(1); 115 ByteBuffer bufferData = ByteBuffer.wrap(byteArray); 116 // Setting position to 1 will cause problems if the bindings don't 117 // correctly handle the buffer offset when releasing the pointer 118 bufferData.position(1); 119 glGenBuffers(1, bufferHandles); 120 int bufferHandle = bufferHandles.get(0); 121 glBindBuffer(GL_ARRAY_BUFFER, bufferHandle); 122 glBufferData(GL_ARRAY_BUFFER, 4, bufferData, GL_STATIC_DRAW); 123 assertEquals(glGetError(), GL_NO_ERROR); 124 glDeleteBuffers(1, bufferHandles); 125 } 126 127 @Test testArraybackedFBTextureReadPixels()128 public void testArraybackedFBTextureReadPixels() throws Throwable { 129 IntBuffer textureHandles = IntBuffer.allocate(1); 130 IntBuffer fboHandles = IntBuffer.allocate(1); 131 ByteBuffer texelData = ByteBuffer.wrap(byteArray); 132 ByteBuffer readBuffer = ByteBuffer.allocate(byteArray.length); 133 // Setting position to 1 will cause problems if the bindings don't 134 // correctly handle the buffer offset when releasing the pointer 135 texelData.position(1); 136 readBuffer.position(1); 137 glGenTextures(1, textureHandles); 138 int textureHandle = textureHandles.get(0); 139 glBindTexture(GL_TEXTURE_2D, textureHandle); 140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, texelData); 141 glGenFramebuffers(1, fboHandles); 142 int fbHandle = fboHandles.get(0); 143 glBindFramebuffer(GL_FRAMEBUFFER, fbHandle); 144 glViewport(0, 0, 1, 1); 145 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureHandle, 0); 146 assertEquals(glCheckFramebufferStatus(GL_FRAMEBUFFER), GL_FRAMEBUFFER_COMPLETE); 147 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, readBuffer); 148 assertEquals(glGetError(), GL_NO_ERROR); 149 for (int i = 0; i < 4; i++) { 150 assertEquals( 151 texelData.get(i + texelData.position()), readBuffer.get(i + readBuffer.position())); 152 } 153 glDeleteTextures(1, textureHandles); 154 } 155 } 156