Beatmup
Beatmup::Android::ExternalBitmap Class Reference

Image coming from a SurfaceTexture (Camera or video decoder) More...

#include <external_bitmap.h>

Inheritance diagram for Beatmup::Android::ExternalBitmap:
Beatmup::AbstractBitmap Beatmup::GL::TextureHandler Beatmup::Object

Public Member Functions

 ExternalBitmap (Beatmup::Context &ctx)
 Instantiates external image. More...
 
 ~ExternalBitmap ()
 
void bind (JNIEnv *jenv, jobject frontend)
 Attaches the bitmap object to its Java frontend and initializes the surface texture in it. More...
 
const Beatmup::GL::TextureHandler::TextureFormat getTextureFormat () const
 Returns the texture format specifying how the shader must interpret the data. More...
 
const PixelFormat getPixelFormat () const
 Pixel format of the bitmap. More...
 
const int getWidth () const
 Width of the texture in pixels. More...
 
const int getHeight () const
 Height of the texture in pixels. More...
 
const int getDepth () const
 Depth of the texture in pixels. More...
 
const msize getMemorySize () const
 Bitmap size in bytes. More...
 
const pixbytegetData (int x, int y) const
 Returns a pointer to given pixel. More...
 
pixbytegetData (int x, int y)
 
void notifyUpdate (const int width, const int height)
 
- Public Member Functions inherited from Beatmup::AbstractBitmap
bool isUpToDate (ProcessingTarget) const
 
bool isDirty () const
 Returns true if the bitmap does not contain any valid content. More...
 
int getPixelInt (int x, int y, int cha=0) const
 Retrieves integer value of given channel at given pixel. More...
 
const unsigned char getBitsPerPixel () const
 Returns number of bits per pixel stored in each bitmap. More...
 
const unsigned char getNumberOfChannels () const
 Returns number of bytes per pixel stored in each bitmap. More...
 
const ImageResolution getSize () const
 Returns the bitmap resolution within ImageResolution object. More...
 
ContextgetContext () const
 
void zero ()
 Sets all the pixels to zero. More...
 
bool isInteger () const
 Returns true if the bitmap contains integer values, false otherwise. More...
 
bool isFloat () const
 Returns true if the bitmap contains floating point values, false otherwise. More...
 
bool isMask () const
 Returns true if the bitmap is a mask, false otherwise. More...
 
std::string toString () const
 Retruns a string describing the bitmap. More...
 
void saveBmp (const char *filename)
 Saves the bitmap to a BMP file. More...
 
 ~AbstractBitmap ()
 
- Public Member Functions inherited from Beatmup::GL::TextureHandler
 ~TextureHandler ()
 
float getAspectRatio () const
 Aspect ratio of the texture. More...
 
float getInvAspectRatio () const
 Inverse of the aspect ratio of the texture. More...
 
const bool isFloatingPoint () const
 
const int getNumberOfChannels () const
 Returns number of channels containing in the texture. More...
 
bool hasValidHandle () const
 Returns true if the texture handle points to a valid texture. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Protected Member Functions

virtual void prepare (GraphicPipeline &gpu)
 Prepares (eventually uploads) texture data on GPU. More...
 
void lockPixelData ()
 Locks access to the CPU memory buffer containing pixel data. More...
 
void unlockPixelData ()
 Unlocks access to the CPU memory buffer containing pixel data. More...
 
- Protected Member Functions inherited from Beatmup::AbstractBitmap
 AbstractBitmap (Context &ctx)
 
- Protected Member Functions inherited from Beatmup::GL::TextureHandler
 TextureHandler ()
 
void invalidate (RecycleBin &)
 Forces disposing the texture data, e.g. More...
 

Private Attributes

int width
 
int height
 
JavaVM * jvm
 
JNIEnv * persistentJEnv
 
jmethodID updateTexImageMethodId
 
jobject surfaceTexture
 
bool textureUpdated
 

Additional Inherited Members

- Public Types inherited from Beatmup::GL::TextureHandler
enum  TextureFormat {
  Rx8 , RGBx8 , RGBAx8 , Rx32f ,
  RGBx32f , RGBAx32f , OES_Ext
}
 Texture format, specifies how the texture should be interpreted on the shader side. More...
 
- Static Public Member Functions inherited from Beatmup::AbstractBitmap
static bool isInteger (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to integer values, false otherwise. More...
 
static bool isFloat (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to floating point values, false otherwise. More...
 
static bool isMask (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to a mask, false otherwise. More...
 
- Static Public Member Functions inherited from Beatmup::GL::TextureHandler
static const char * textureFormatToString (const TextureFormat &)
 
- Static Public Attributes inherited from Beatmup::AbstractBitmap
static const int NUM_PIXEL_FORMATS = 9
 
static const char * PIXEL_FORMAT_NAMES [NUM_PIXEL_FORMATS]
 pixel format names More...
 
static const unsigned char CHANNELS_PER_PIXEL [NUM_PIXEL_FORMATS]
 number of channels for each pixel format More...
 
static const unsigned char BITS_PER_PIXEL [NUM_PIXEL_FORMATS]
 number of bits per pixel for each pixel format More...
 
- Static Public Attributes inherited from Beatmup::GL::TextureHandler
static const int TEXTURE_FORMAT_BYTES_PER_PIXEL []
 size of a texel in bytes for different texture formats More...
 
- Protected Attributes inherited from Beatmup::AbstractBitmap
Contextctx
 context managing this bitmap More...
 
bool upToDate [2]
 bitmap up-to-date state on CPU and GPU More...
 
- Protected Attributes inherited from Beatmup::GL::TextureHandler
handle_t textureHandle
 

Detailed Description

Image coming from a SurfaceTexture (Camera or video decoder)

Definition at line 30 of file external_bitmap.h.

Constructor & Destructor Documentation

◆ ExternalBitmap()

ExternalBitmap::ExternalBitmap ( Beatmup::Context ctx)

Instantiates external image.

This must not be called from Beatmup internal threads.

Definition at line 65 of file external_bitmap.cpp.

65  : AbstractBitmap(ctx) {
66  width = height = 0;
68  textureUpdated = false;
69  persistentJEnv = nullptr;
70  jvm = nullptr;
71 }
Context & ctx
context managing this bitmap
AbstractBitmap(const AbstractBitmap &that)=delete
disabling copying constructor
static Beatmup::GL::handle_t makeHandle(Context &ctx)

◆ ~ExternalBitmap()

ExternalBitmap::~ExternalBitmap ( )

Definition at line 138 of file external_bitmap.cpp.

138  {
139  if (jvm) {
140  JNIEnv *jenv;
141  jvm->AttachCurrentThread(&jenv, nullptr);
142  jenv->DeleteGlobalRef(surfaceTexture);
143  }
144 }
JNIEnv * jenv

Member Function Documentation

◆ prepare()

void ExternalBitmap::prepare ( GraphicPipeline gpu)
protectedvirtual

Prepares (eventually uploads) texture data on GPU.

Called only by the context managing thread.

Parameters
[in]gpuGraphic pipeline instance

Reimplemented from Beatmup::AbstractBitmap.

Definition at line 154 of file external_bitmap.cpp.

154  {
155  TextureHandler::prepare(gpu);
156 
157  // call updateTexImage()
158  glBindTexture(GL_TEXTURE_EXTERNAL_OES, this->textureHandle);
159  if (textureUpdated && jvm) {
160  if (!persistentJEnv)
161  jvm->AttachCurrentThread(&persistentJEnv, nullptr);
163  textureUpdated = false;
164  }
165 
166  glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
167  glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
168 }

◆ lockPixelData()

void Beatmup::Android::ExternalBitmap::lockPixelData ( )
inlineprotectedvirtual

Locks access to the CPU memory buffer containing pixel data.

Implements Beatmup::AbstractBitmap.

Definition at line 41 of file external_bitmap.h.

41 {}

◆ unlockPixelData()

void Beatmup::Android::ExternalBitmap::unlockPixelData ( )
inlineprotectedvirtual

Unlocks access to the CPU memory buffer containing pixel data.

Implements Beatmup::AbstractBitmap.

Definition at line 42 of file external_bitmap.h.

42 {}

◆ bind()

void ExternalBitmap::bind ( JNIEnv *  jenv,
jobject  frontend 
)

Attaches the bitmap object to its Java frontend and initializes the surface texture in it.

Definition at line 74 of file external_bitmap.cpp.

74  {
75  jenv->GetJavaVM(&jvm);
76 
77  // grab classes
78  jclass externalBitmapClass = jenv->FindClass("Beatmup/Android/ExternalBitmap");
79  jclass surfaceTextureClass = jenv->FindClass("android/graphics/SurfaceTexture");
80 
81 #ifdef BEATMUP_DEBUG
82  if (!externalBitmapClass) {
83  BEATMUP_DEBUG_E("Cannot find class Beatmup.Android.ExternalImage");
84  return;
85  }
86 
87  if (!surfaceTextureClass) {
88  BEATMUP_DEBUG_E("Cannot find class android.graphics.SurfaceTexture");
89  return;
90  }
91 #endif
92 
93  // grab field id
94  jfieldID surfaceTextureFieldId = jenv->GetFieldID(externalBitmapClass, "surfaceTexture", "Landroid/graphics/SurfaceTexture;");
95 #ifdef BEATMUP_DEBUG
96  if (!surfaceTextureFieldId) {
97  BEATMUP_DEBUG_E("Cannot find field surfaceTexture in Beatmup.Android.ExternalImage");
98  return;
99  }
100 #endif
101 
102  // grab method ids
103  jmethodID surfaceTextureConstructor = jenv->GetMethodID(surfaceTextureClass, "<init>", "(I)V");
104  updateTexImageMethodId = jenv->GetMethodID(surfaceTextureClass, "updateTexImage", "()V");
105 #ifdef BEATMUP_DEBUG
106  if (!surfaceTextureConstructor) {
107  BEATMUP_DEBUG_E("Cannot find SurfaceTexture constructor");
108  return;
109  }
110 
111  if (!updateTexImageMethodId) {
112  BEATMUP_DEBUG_E("Cannot find SurfaceTexture.updateTexImage method");
113  return;
114  }
115 #endif
116 
117  // create an instance of SurfaceTexture
118  jobject newSurfaceTextureObject = jenv->NewObject(surfaceTextureClass, surfaceTextureConstructor, this->textureHandle);
119 #ifdef BEATMUP_DEBUG
120  if (!newSurfaceTextureObject) {
121  BEATMUP_DEBUG_E("Cannot create SurfaceTexture instance");
122  return;
123  }
124 #endif
125 
126  // keep a global reference
127  surfaceTexture = jenv->NewGlobalRef(newSurfaceTextureObject);
128 
129  // set field value
130  jenv->SetObjectField(frontend, surfaceTextureFieldId, newSurfaceTextureObject);
131 
132  // clean up
133  jenv->DeleteLocalRef(externalBitmapClass);
134  jenv->DeleteLocalRef(surfaceTextureClass);
135 }
#define BEATMUP_DEBUG_E(...)
Definition: debug.h:34
JNIEnv jclass
JNIEnv jobject

◆ getTextureFormat()

const Beatmup::GL::TextureHandler::TextureFormat Beatmup::Android::ExternalBitmap::getTextureFormat ( ) const
inlinevirtual

Returns the texture format specifying how the shader must interpret the data.

Reimplemented from Beatmup::AbstractBitmap.

Definition at line 59 of file external_bitmap.h.

59  {
60  return Beatmup::GL::TextureHandler::TextureFormat::OES_Ext;
61  }

◆ getPixelFormat()

const PixelFormat Beatmup::Android::ExternalBitmap::getPixelFormat ( ) const
inlinevirtual

Pixel format of the bitmap.

Implements Beatmup::AbstractBitmap.

Definition at line 63 of file external_bitmap.h.

63  {
65  }
@ TripleByte
3 channels of 8 bits per pixel (like RGB), unsigned integer values

◆ getWidth()

const int Beatmup::Android::ExternalBitmap::getWidth ( ) const
inlinevirtual

Width of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 67 of file external_bitmap.h.

67  {
68  return width;
69  }

◆ getHeight()

const int Beatmup::Android::ExternalBitmap::getHeight ( ) const
inlinevirtual

Height of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 71 of file external_bitmap.h.

71  {
72  return height;
73  }

◆ getDepth()

const int Beatmup::Android::ExternalBitmap::getDepth ( ) const
inlinevirtual

Depth of the texture in pixels.

Reimplemented from Beatmup::AbstractBitmap.

Definition at line 75 of file external_bitmap.h.

75  {
76  return 1;
77  }

◆ getMemorySize()

const msize Beatmup::Android::ExternalBitmap::getMemorySize ( ) const
inlinevirtual

Bitmap size in bytes.

Implements Beatmup::AbstractBitmap.

Definition at line 79 of file external_bitmap.h.

79  {
80  return 0;
81  }

◆ getData() [1/2]

const pixbyte* Beatmup::Android::ExternalBitmap::getData ( int  x,
int  y 
) const
inlinevirtual

Returns a pointer to given pixel.

Parameters
xtarget pixel horizontal coordinate
ytarget pixel vertical coordinate
Returns
a pointer, may be NULL.

Implements Beatmup::AbstractBitmap.

Definition at line 83 of file external_bitmap.h.

83  {
84  return nullptr;
85  }

◆ getData() [2/2]

pixbyte* Beatmup::Android::ExternalBitmap::getData ( int  x,
int  y 
)
inlinevirtual

Implements Beatmup::AbstractBitmap.

Definition at line 87 of file external_bitmap.h.

87  {
88  return nullptr;
89  }

◆ notifyUpdate()

void ExternalBitmap::notifyUpdate ( const int  width,
const int  height 
)

Definition at line 147 of file external_bitmap.cpp.

147  {
148  this->width = width;
149  this->height = height;
150  this->textureUpdated = true;
151 }

Member Data Documentation

◆ width

int Beatmup::Android::ExternalBitmap::width
private

Definition at line 32 of file external_bitmap.h.

◆ height

int Beatmup::Android::ExternalBitmap::height
private

Definition at line 32 of file external_bitmap.h.

◆ jvm

JavaVM* Beatmup::Android::ExternalBitmap::jvm
private

Definition at line 33 of file external_bitmap.h.

◆ persistentJEnv

JNIEnv* Beatmup::Android::ExternalBitmap::persistentJEnv
private

Definition at line 34 of file external_bitmap.h.

◆ updateTexImageMethodId

jmethodID Beatmup::Android::ExternalBitmap::updateTexImageMethodId
private

Definition at line 35 of file external_bitmap.h.

◆ surfaceTexture

jobject Beatmup::Android::ExternalBitmap::surfaceTexture
private

Definition at line 36 of file external_bitmap.h.

◆ textureUpdated

bool Beatmup::Android::ExternalBitmap::textureUpdated
private

Definition at line 37 of file external_bitmap.h.


The documentation for this class was generated from the following files: