Beatmup
texture_handler.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "texture_handler.h"
20 #include "../basic_types.h"
21 #include "../gpu/pipeline.h"
22 #include "../gpu/bgl.h"
23 #include "../debug.h"
24 
25 using namespace Beatmup;
26 using namespace GL;
27 
29  1, 3, 4,
30  4 * 1, 4 * 3, 4 * 4,
31  0
32 };
33 
34 
36  switch (format) {
37  case GL::TextureHandler::TextureFormat::Rx8:
38  return "Rx8";
39  case GL::TextureHandler::TextureFormat::RGBx8:
40  return "RGBx8";
41  case GL::TextureHandler::TextureFormat::RGBAx8:
42  return "RGBAx8";
43 
44  case GL::TextureHandler::TextureFormat::Rx32f:
45  return "Rx32f";
46  case GL::TextureHandler::TextureFormat::RGBx32f:
47  return "RGBx32f";
48  case GL::TextureHandler::TextureFormat::RGBAx32f:
49  return "RGBAx32f";
50 
51  case OES_Ext:
52  return "OES extension";
53  }
54  return "invalid format";
55 }
56 
57 
59  textureHandle(0)
60 {}
61 
62 
64  if (hasValidHandle())
65  BEATMUP_DEBUG_E("Destroying texture handler still having a valid handle");
66 }
67 
68 
70  if (!hasValidHandle())
71  glGenTextures(1, &textureHandle);
72 }
73 
74 
76  switch (getTextureFormat())
77  {
78  case GL::TextureHandler::TextureFormat::Rx8:
79  case GL::TextureHandler::TextureFormat::Rx32f:
80  return 1;
81  case GL::TextureHandler::TextureFormat::RGBx8:
82  case GL::TextureHandler::TextureFormat::RGBx32f:
83  return 3;
84  case GL::TextureHandler::TextureFormat::RGBAx8:
85  case GL::TextureHandler::TextureFormat::RGBAx32f:
86  return 4;
87 
88  case OES_Ext:
89  return 3;
90  //fixme
91  }
92  return 0;
93 }
94 
95 
97  if (hasValidHandle()) {
98  class Deleter : public GL::RecycleBin::Item {
100  public:
101  Deleter(handle_t handle) : handle(handle) {}
102  ~Deleter() {
103  glDeleteTextures(1, &handle);
104  }
105  };
106  bin.put(new Deleter(textureHandle));
107 
108  textureHandle = 0;
109  }
110 }
A wrapper for a GPU resource.
Definition: recycle_bin.h:39
Stores references to GPU resources that will not be used anymore and needed to be recycled in a threa...
Definition: recycle_bin.h:34
void put(Item *item)
Puts an item into the recycle bin.
Definition: recycle_bin.cpp:73
static const char * textureFormatToString(const TextureFormat &)
const int getNumberOfChannels() const
Returns number of channels containing in the texture.
bool hasValidHandle() const
Returns true if the texture handle points to a valid texture.
virtual void prepare(GraphicPipeline &gpu)
Prepares (eventually uploads) texture data on GPU.
TextureFormat
Texture format, specifies how the texture should be interpreted on the shader side.
@ OES_Ext
external EGL image
static const int TEXTURE_FORMAT_BYTES_PER_PIXEL[]
size of a texel in bytes for different texture formats
virtual const TextureFormat getTextureFormat() const =0
Returns the texture format specifying how the shader must interpret the data.
void invalidate(RecycleBin &)
Forces disposing the texture data, e.g.
Internal low-level GPU control API.
Definition: pipeline.h:33
#define BEATMUP_DEBUG_E(...)
Definition: debug.h:34
unsigned int handle_t
A reference to a GPU resource.
Definition: basic_types.h:61
JNIEnv jobject jlong handle
JNIEnv jobject jint format