Java Code Examples for android.opengl.GLES20#glPixelStorei()

The following examples show how to use android.opengl.GLES20#glPixelStorei() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EglRenderer.java    From webrtc_android with MIT License 6 votes vote down vote up
@Override
// TODO(bugs.webrtc.org/8491): Remove NoSynchronizedMethodCheck suppression.
@SuppressWarnings("NoSynchronizedMethodCheck")
public synchronized void run() {
  if (surface != null && eglBase != null && !eglBase.hasSurface()) {
    if (surface instanceof Surface) {
      eglBase.createSurface((Surface) surface);
    } else if (surface instanceof SurfaceTexture) {
      eglBase.createSurface((SurfaceTexture) surface);
    } else {
      throw new IllegalStateException("Invalid surface: " + surface);
    }
    eglBase.makeCurrent();
    // Necessary for YUV frames with odd width.
    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
  }
}
 
Example 2
Source File: UgiRenderer.java    From UltraGpuImage with MIT License 6 votes vote down vote up
@Override
public synchronized void run() {
    if (mSurface != null && mEglBase != null && !mEglBase.hasSurface()
        && mNativeHandle != 0) {
        if (mSurface instanceof Surface) {
            mEglBase.createSurface((Surface) mSurface);
        } else if (mSurface instanceof SurfaceTexture) {
            mEglBase.createSurface((SurfaceTexture) mSurface);
        } else {
            logError("EglSurfaceCreation error: invalid surface " + mSurface);
            return;
        }
        mEglBase.makeCurrent();
        // Necessary for YUV frames with odd width.
        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);

        nativeOnSurfaceCreated(mNativeHandle);
    }
}
 
Example 3
Source File: EglRenderer.java    From VideoCRE with MIT License 5 votes vote down vote up
@Override
public synchronized void run() {
  if (surface != null && eglBase != null && !eglBase.hasSurface()) {
    if (surface instanceof Surface) {
      eglBase.createSurface((Surface) surface);
    } else if (surface instanceof SurfaceTexture) {
      eglBase.createSurface((SurfaceTexture) surface);
    } else {
      throw new IllegalStateException("Invalid surface: " + surface);
    }
    eglBase.makeCurrent();
    // Necessary for YUV frames with odd width.
    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
  }
}
 
Example 4
Source File: Font.java    From tilt-game-android with MIT License 5 votes vote down vote up
public synchronized void update(final GLState pGLState) {
	if (this.mTexture.isLoadedToHardware()) {
		final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture;
		if (lettersPendingToBeDrawnToTexture.size() > 0) {
			this.mTexture.bind(pGLState);
			final PixelFormat pixelFormat = this.mTexture.getPixelFormat();

			final boolean preMultipyAlpha = this.mTexture.getTextureOptions().mPreMultiplyAlpha;
			for (int i = lettersPendingToBeDrawnToTexture.size() - 1; i >= 0; i--) {
				final Letter letter = lettersPendingToBeDrawnToTexture.get(i);
				if (!letter.isWhitespace()) {
					final Bitmap bitmap = this.getLetterBitmap(letter);

					final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (pixelFormat == PixelFormat.RGBA_8888);
					if (!useDefaultAlignment) {
						/* Adjust unpack alignment. */
						GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
					}

					if (preMultipyAlpha) {
						GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap);
					} else {
						pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap, pixelFormat);
					}

					if (!useDefaultAlignment) {
						/* Restore default unpack alignment. */
						GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
					}

					bitmap.recycle();
				}
			}
			lettersPendingToBeDrawnToTexture.clear();

			System.gc();
		}
	}
}
 
Example 5
Source File: BitmapTexture.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
	final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig();
	final Bitmap bitmap = this.onGetBitmap(bitmapConfig);

	if (bitmap == null) {
		throw new NullBitmapException("Caused by: '" + this.toString() + "'.");
	}

	final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (this.mPixelFormat == PixelFormat.RGBA_8888);
	if (!useDefaultAlignment) {
		/* Adjust unpack alignment. */
		GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
	}

	final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
	if (preMultipyAlpha) {
		GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
	} else {
		pGLState.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0, this.mPixelFormat);
	}

	if (!useDefaultAlignment) {
		/* Restore default unpack alignment. */
		GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
	}

	bitmap.recycle();
}
 
Example 6
Source File: PVRTexture.java    From tilt-game-android with MIT License 5 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
	final IPVRTexturePixelBufferStrategyBufferManager pvrTextureLoadStrategyManager = this.mPVRTexturePixelBufferStrategy.newPVRTexturePixelBufferStrategyManager(this);

	int width = this.getWidth();
	int height = this.getHeight();

	final int dataLength = this.mPVRTextureHeader.getDataLength();

	final int bytesPerPixel = this.mPVRTextureHeader.getBitsPerPixel() / DataConstants.BITS_PER_BYTE;

	/* Adjust unpack alignment. */
	GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);

	int currentLevel = 0;
	int currentPixelDataOffset = 0;
	while (currentPixelDataOffset < dataLength) {
		if (currentLevel > 0 && (width != height || MathUtils.nextPowerOfTwo(width) != width)) {
			Debug.w("Mipmap level '" + currentLevel + "' is not squared. Width: '" + width + "', height: '" + height + "'. Texture won't render correctly.");
		}

		final int currentPixelDataSize = height * width * bytesPerPixel;

		/* Load the current level. */
		this.mPVRTexturePixelBufferStrategy.loadPVRTextureData(pvrTextureLoadStrategyManager, width, height, bytesPerPixel, this.mPixelFormat, currentLevel, currentPixelDataOffset, currentPixelDataSize);

		currentPixelDataOffset += currentPixelDataSize;

		/* Prepare next mipmap level. */
		width = Math.max(width / 2, 1);
		height = Math.max(height / 2, 1);

		currentLevel++;
	}

	/* Restore default unpack alignment. */
	GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
}
 
Example 7
Source File: Font.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
public synchronized void update(final GLState pGLState) {
	if(this.mTexture.isLoadedToHardware()) {
		final ArrayList<Letter> lettersPendingToBeDrawnToTexture = this.mLettersPendingToBeDrawnToTexture;
		if(lettersPendingToBeDrawnToTexture.size() > 0) {
			this.mTexture.bind(pGLState);
			final PixelFormat pixelFormat = this.mTexture.getPixelFormat();

			final boolean preMultipyAlpha = this.mTexture.getTextureOptions().mPreMultiplyAlpha;
			for(int i = lettersPendingToBeDrawnToTexture.size() - 1; i >= 0; i--) {
				final Letter letter = lettersPendingToBeDrawnToTexture.get(i);
				if(!letter.isWhitespace()) {
					final Bitmap bitmap = this.getLetterBitmap(letter);

					final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (pixelFormat == PixelFormat.RGBA_8888);
					if(!useDefaultAlignment) {
						/* Adjust unpack alignment. */
						GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
					}

					if(preMultipyAlpha) {
						GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap);
					} else {
						pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, letter.mTextureX, letter.mTextureY, bitmap, pixelFormat);
					}

					if(!useDefaultAlignment) {
						/* Restore default unpack alignment. */
						GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
					}

					bitmap.recycle();
				}
			}
			lettersPendingToBeDrawnToTexture.clear();

			System.gc();
		}
	}
}
 
Example 8
Source File: BitmapTexture.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
	final Config bitmapConfig = this.mBitmapTextureFormat.getBitmapConfig();
	final Bitmap bitmap = this.onGetBitmap(bitmapConfig);

	if(bitmap == null) {
		throw new NullBitmapException("Caused by: '" + this.toString() + "'.");
	}

	final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && (this.mPixelFormat == PixelFormat.RGBA_8888);
	if(!useDefaultAlignment) {
		/* Adjust unpack alignment. */
		GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
	}

	final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
	if(preMultipyAlpha) {
		GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
	} else {
		pGLState.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0, this.mPixelFormat);
	}

	if(!useDefaultAlignment) {
		/* Restore default unpack alignment. */
		GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
	}

	bitmap.recycle();
}
 
Example 9
Source File: PVRTexture.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) throws IOException {
	final IPVRTexturePixelBufferStrategyBufferManager pvrTextureLoadStrategyManager = this.mPVRTexturePixelBufferStrategy.newPVRTexturePixelBufferStrategyManager(this);

	int width = this.getWidth();
	int height = this.getHeight();

	final int dataLength = this.mPVRTextureHeader.getDataLength();

	final int bytesPerPixel = this.mPVRTextureHeader.getBitsPerPixel() / DataConstants.BITS_PER_BYTE;

	/* Adjust unpack alignment. */
	GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);

	int currentLevel = 0;
	int currentPixelDataOffset = 0;
	while (currentPixelDataOffset < dataLength) {
		if (currentLevel > 0 && (width != height || MathUtils.nextPowerOfTwo(width) != width)) {
			Debug.w("Mipmap level '" + currentLevel + "' is not squared. Width: '" + width + "', height: '" + height + "'. Texture won't render correctly.");
		}

		final int currentPixelDataSize = height * width * bytesPerPixel;

		/* Load the current level. */
		this.mPVRTexturePixelBufferStrategy.loadPVRTextureData(pvrTextureLoadStrategyManager, width, height, bytesPerPixel, this.mPixelFormat, currentLevel, currentPixelDataOffset, currentPixelDataSize);

		currentPixelDataOffset += currentPixelDataSize;

		/* Prepare next mipmap level. */
		width = Math.max(width / 2, 1);
		height = Math.max(height / 2, 1);

		currentLevel++;
	}

	/* Restore default unpack alignment. */
	GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
}
 
Example 10
Source File: VideoDecoderRenderer.java    From MediaSDK with Apache License 2.0 4 votes vote down vote up
@Override
public void onDrawFrame(GL10 unused) {
  VideoDecoderOutputBuffer pendingOutputBuffer = pendingOutputBufferReference.getAndSet(null);
  if (pendingOutputBuffer == null && renderedOutputBuffer == null) {
    // There is no output buffer to render at the moment.
    return;
  }
  if (pendingOutputBuffer != null) {
    if (renderedOutputBuffer != null) {
      renderedOutputBuffer.release();
    }
    renderedOutputBuffer = pendingOutputBuffer;
  }
  VideoDecoderOutputBuffer outputBuffer = renderedOutputBuffer;
  // Set color matrix. Assume BT709 if the color space is unknown.
  float[] colorConversion = kColorConversion709;
  switch (outputBuffer.colorspace) {
    case VideoDecoderOutputBuffer.COLORSPACE_BT601:
      colorConversion = kColorConversion601;
      break;
    case VideoDecoderOutputBuffer.COLORSPACE_BT2020:
      colorConversion = kColorConversion2020;
      break;
    case VideoDecoderOutputBuffer.COLORSPACE_BT709:
    default:
      break; // Do nothing
  }
  GLES20.glUniformMatrix3fv(colorMatrixLocation, 1, false, colorConversion, 0);

  for (int i = 0; i < 3; i++) {
    int h = (i == 0) ? outputBuffer.height : (outputBuffer.height + 1) / 2;
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]);
    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    GLES20.glTexImage2D(
        GLES20.GL_TEXTURE_2D,
        0,
        GLES20.GL_LUMINANCE,
        outputBuffer.yuvStrides[i],
        h,
        0,
        GLES20.GL_LUMINANCE,
        GLES20.GL_UNSIGNED_BYTE,
        outputBuffer.yuvPlanes[i]);
  }

  int[] widths = new int[3];
  widths[0] = outputBuffer.width;
  // TODO: Handle streams where chroma channels are not stored at half width and height
  // compared to luma channel. See [Internal: b/142097774].
  // U and V planes are being stored at half width compared to Y.
  widths[1] = widths[2] = (widths[0] + 1) / 2;
  for (int i = 0; i < 3; i++) {
    // Set cropping of stride if either width or stride has changed.
    if (previousWidths[i] != widths[i] || previousStrides[i] != outputBuffer.yuvStrides[i]) {
      Assertions.checkState(outputBuffer.yuvStrides[i] != 0);
      float widthRatio = (float) widths[i] / outputBuffer.yuvStrides[i];
      // These buffers are consumed during each call to glDrawArrays. They need to be member
      // variables rather than local variables in order not to get garbage collected.
      textureCoords[i] =
          GlUtil.createBuffer(
              new float[] {0.0f, 0.0f, 0.0f, 1.0f, widthRatio, 0.0f, widthRatio, 1.0f});
      GLES20.glVertexAttribPointer(
          texLocations[i], 2, GLES20.GL_FLOAT, false, 0, textureCoords[i]);
      previousWidths[i] = widths[i];
      previousStrides[i] = outputBuffer.yuvStrides[i];
    }
  }

  GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  GlUtil.checkGlError();
}
 
Example 11
Source File: FrameRenderer.java    From DanDanPlayForAndroid with MIT License 4 votes vote down vote up
@Override
public void onDrawFrame() {
  FrameBuffer pendingOutputBuffer = pendingOutputBufferReference.getAndSet(null);
  if (pendingOutputBuffer == null && renderedOutputBuffer == null) {
    // There is no output buffer to render at the moment.
    return;
  }
  if (pendingOutputBuffer != null) {
    if (renderedOutputBuffer != null) {
      renderedOutputBuffer.release();
    }
    renderedOutputBuffer = pendingOutputBuffer;
  }

  FrameBuffer outputBuffer = renderedOutputBuffer;
  // Set color matrix. Assume BT709 if the color space is unknown.
  float[] colorConversion = kColorConversion709;
  /*switch (outputBuffer.pixelFormat) {
    case FrameBuffer.COLORSPACE_BT601:
      colorConversion = kColorConversion601;
      break;
    case FrameBuffer.COLORSPACE_BT2020:
      colorConversion = kColorConversion2020;
      break;
    case FrameBuffer.COLORSPACE_BT709:
    default:
      break; // Do nothing
  }*/

  int bitDepth = outputBuffer.bitDepth;
  int format = bitDepth == 1 ? GLES20.GL_LUMINANCE : GLES20.GL_LUMINANCE_ALPHA;

  GLES20.glUniformMatrix3fv(colorMatrixLocation, 1, false, colorConversion, 0);
  GLES20.glUniform1f(bitDepthLocation, bitDepth);

  for (int i = 0; i < 3; i++) {
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, yuvTextures[i]);

    int width = outputBuffer.yuvStrides[i] / bitDepth;
    int height = (i == 0) ? outputBuffer.height : outputBuffer.height / 2;

    GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, format,
            width, height, 0, format, GLES20.GL_UNSIGNED_BYTE,
            outputBuffer.yuvPlanes[i]);
  }
  // Set cropping of stride if either width or stride has changed.
  if (previousWidth != outputBuffer.width || previousStride != outputBuffer.yuvStrides[0]) {
    float crop = (float) outputBuffer.width * bitDepth / outputBuffer.yuvStrides[0];
    // This buffer is consumed during each call to glDrawArrays. It needs to be a member variable
    // rather than a local variable to ensure that it doesn't get garbage collected.
    textureCoords = nativeFloatBuffer(
            0.0f, 0.0f,
            0.0f, 1.0f,
            crop, 0.0f,
            crop, 1.0f);
    GLES20.glVertexAttribPointer(
            texLocation, 2, GLES20.GL_FLOAT, false, 0, textureCoords);
    previousWidth = outputBuffer.width;
    previousStride = outputBuffer.yuvStrides[0];
  }
  GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
  GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
  checkNoGLES2Error();
}
 
Example 12
Source File: AndroidGL.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void pixelStorei(int pname, int param) {
    GLES20.glPixelStorei(pname, param);

}
 
Example 13
Source File: BitmapTextureAtlas.java    From tilt-game-android with MIT License 4 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) {
	final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
	final int glInternalFormat = pixelFormat.getGLInternalFormat();
	final int glFormat = pixelFormat.getGLFormat();
	final int glType = pixelFormat.getGLType();

	GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);

	final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
	/* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
	final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;

	final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
	final int textureSourceCount = textureSources.size();

	final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
	for (int i = 0; i < textureSourceCount; i++) {
		final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
		try {
			final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
			if (bitmap == null) {
				throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
			}

			final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
			if (!useDefaultAlignment) {
				/* Adjust unpack alignment. */
				GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
			}

			if (preMultipyAlpha) {
				GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
			} else {
				pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
			}

			if (!useDefaultAlignment) {
				/* Restore default unpack alignment. */
				GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
			}

			bitmap.recycle();

			if (textureStateListener != null) {
				textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
			}
		} catch (final NullBitmapException e) {
			if (textureStateListener != null) {
				textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
			} else {
				throw e;
			}
		}
	}
}
 
Example 14
Source File: BitmapTextureAtlas.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeTextureToHardware(final GLState pGLState) {
	final PixelFormat pixelFormat = this.mBitmapTextureFormat.getPixelFormat();
	final int glInternalFormat = pixelFormat.getGLInternalFormat();
	final int glFormat = pixelFormat.getGLFormat();
	final int glType = pixelFormat.getGLType();

	GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, glInternalFormat, this.mWidth, this.mHeight, 0, glFormat, glType, null);

	final boolean preMultipyAlpha = this.mTextureOptions.mPreMultiplyAlpha;
	/* Non alpha premultiplied bitmaps are loaded with ARGB_8888 and converted down manually. */
	final Config bitmapConfig = (preMultipyAlpha) ? this.mBitmapTextureFormat.getBitmapConfig() : Config.ARGB_8888;

	final ArrayList<IBitmapTextureAtlasSource> textureSources = this.mTextureAtlasSources;
	final int textureSourceCount = textureSources.size();

	final ITextureAtlasStateListener<IBitmapTextureAtlasSource> textureStateListener = this.getTextureAtlasStateListener();
	for(int i = 0; i < textureSourceCount; i++) {
		final IBitmapTextureAtlasSource bitmapTextureAtlasSource = textureSources.get(i);
		try {
			final Bitmap bitmap = bitmapTextureAtlasSource.onLoadBitmap(bitmapConfig);
			if(bitmap == null) {
				throw new NullBitmapException("Caused by: " + bitmapTextureAtlasSource.getClass().toString() + " --> " + bitmapTextureAtlasSource.toString() + " returned a null Bitmap.");
			}

			final boolean useDefaultAlignment = MathUtils.isPowerOfTwo(bitmap.getWidth()) && MathUtils.isPowerOfTwo(bitmap.getHeight()) && pixelFormat == PixelFormat.RGBA_8888;
			if(!useDefaultAlignment) {
				/* Adjust unpack alignment. */
				GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
			}

			if(preMultipyAlpha) {
				GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, glFormat, glType);
			} else {
				pGLState.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, bitmapTextureAtlasSource.getTextureX(), bitmapTextureAtlasSource.getTextureY(), bitmap, this.mPixelFormat);
			}

			if(!useDefaultAlignment) {
				/* Restore default unpack alignment. */
				GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, GLState.GL_UNPACK_ALIGNMENT_DEFAULT);
			}

			bitmap.recycle();

			if(textureStateListener != null) {
				textureStateListener.onTextureAtlasSourceLoaded(this, bitmapTextureAtlasSource);
			}
		} catch (final NullBitmapException e) {
			if(textureStateListener != null) {
				textureStateListener.onTextureAtlasSourceLoadExeption(this, bitmapTextureAtlasSource, e);
			} else {
				throw e;
			}
		}
	}
}
 
Example 15
Source File: InvertedColorsVideoRenderer.java    From opentok-android-sdk-samples with MIT License 4 votes vote down vote up
void updateTextures(Frame frame) {
    int width = frame.getWidth();
    int height = frame.getHeight();
    int half_width = (width + 1) >> 1;
    int half_height = (height + 1) >> 1;
    int y_size = width * height;
    int uv_size = half_width * half_height;

    ByteBuffer bb = frame.getBuffer();
    // If we are reusing this frame, make sure we reset position and
    // limit
    bb.clear();

    if (bb.remaining() == y_size + uv_size * 2) {
        bb.position(0);

        GLES20.glPixelStorei(GLES20.GL_UNPACK_ALIGNMENT, 1);
        GLES20.glPixelStorei(GLES20.GL_PACK_ALIGNMENT, 1);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIds[0]);
        GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, width,
                height, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,
                bb);

        bb.position(y_size);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIds[1]);
        GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
                half_width, half_height, GLES20.GL_LUMINANCE,
                GLES20.GL_UNSIGNED_BYTE, bb);

        bb.position(y_size + uv_size);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIds[2]);
        GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0,
                half_width, half_height, GLES20.GL_LUMINANCE,
                GLES20.GL_UNSIGNED_BYTE, bb);
    } else {
        mTextureWidth = 0;
        mTextureHeight = 0;
    }

}