Skip to content

Commit

Permalink
canvas/android: keep GL textures while the app is paused
Browse files Browse the repository at this point in the history
This resumes the work started 5 years ago by commit e127aeb and
finally obsoletes the GLSurfaceListener interface and removes a lot of
complexity from the Bitmap class.
  • Loading branch information
MaxKellermann committed Aug 19, 2021
1 parent 7400390 commit 6eef309
Show file tree
Hide file tree
Showing 20 changed files with 10 additions and 369 deletions.
1 change: 0 additions & 1 deletion build/screen.mk
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ SCREEN_SOURCES += \
$(CANVAS_SRC_DIR)/opengl/UncompressedImage.cpp \
$(CANVAS_SRC_DIR)/opengl/Buffer.cpp \
$(CANVAS_SRC_DIR)/opengl/Shapes.cpp \
$(CANVAS_SRC_DIR)/opengl/Surface.cpp \
$(CANVAS_SRC_DIR)/opengl/Shaders.cpp \
$(CANVAS_SRC_DIR)/opengl/CanvasRotateShift.cpp \
$(CANVAS_SRC_DIR)/opengl/Triangulate.cpp
Expand Down
8 changes: 0 additions & 8 deletions src/Android/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ Copyright_License {
#include "NunchuckDevice.hpp"
#include "VoltageDevice.hpp"

#ifndef NDEBUG
#include "ui/canvas/opengl/Texture.hpp"
#include "ui/canvas/opengl/Buffer.hpp"
#endif

#include <cassert>
#include <stdlib.h>

Expand Down Expand Up @@ -315,9 +310,6 @@ Java_org_xcsoar_NativeView_pauseNative(JNIEnv *env, jobject obj)
/* event subsystem is not initialized, there is nothing to pause */

CommonInterface::main_window->Pause();

assert(num_textures == 0);
assert(num_buffers == 0);
}

gcc_visibility_default
Expand Down
22 changes: 0 additions & 22 deletions src/Topography/TopographyFileRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,11 @@ TopographyFileRenderer::TopographyFileRenderer(const TopographyFile &_file,
ResourceId icon_ID = file.GetIcon();
if (icon_ID.IsDefined())
icon.LoadResource(icon_ID, file.GetBigIcon());

#ifdef ENABLE_OPENGL
AddSurfaceListener(*this);
#endif
}

TopographyFileRenderer::~TopographyFileRenderer()
{
#ifdef ENABLE_OPENGL
RemoveSurfaceListener(*this);

delete array_buffer;
#endif
}
Expand Down Expand Up @@ -510,19 +504,3 @@ TopographyFileRenderer::PaintLabels(Canvas &canvas,
}
}
}

#ifdef ENABLE_OPENGL

void
TopographyFileRenderer::SurfaceCreated()
{
}

void
TopographyFileRenderer::SurfaceDestroyed()
{
delete array_buffer;
array_buffer = nullptr;
}

#endif
7 changes: 0 additions & 7 deletions src/Topography/TopographyFileRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Copyright_License {
#include "Geo/GeoBounds.hpp"

#ifdef ENABLE_OPENGL
#include "ui/canvas/opengl/Surface.hpp"
#else
#include "ui/canvas/Brush.hpp"
#include "Topography/ShapeRenderer.hpp"
Expand All @@ -51,9 +50,6 @@ struct TopographyLook;
* Class used to manage and render vector topography layers
*/
class TopographyFileRenderer final
#ifdef ENABLE_OPENGL
: GLSurfaceListener
#endif
{
const TopographyFile &file;

Expand Down Expand Up @@ -115,9 +111,6 @@ class TopographyFileRenderer final

void PaintPoint(Canvas &canvas, const WindowProjection &projection,
const XShape &shape, const float *opengl_matrix) const;

virtual void SurfaceCreated() override;
virtual void SurfaceDestroyed() override;
#else
void PaintPoint(Canvas &canvas, const WindowProjection &projection,
const unsigned short *lines, const unsigned short *end_lines,
Expand Down
21 changes: 1 addition & 20 deletions src/ui/canvas/Bitmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ Copyright_License {
#endif

#ifdef ANDROID
#include "ui/canvas/custom/UncompressedImage.hpp"
#include "ui/canvas/opengl/Surface.hpp"
#include <jni.h>
#endif

Expand Down Expand Up @@ -65,9 +63,6 @@ using BitmapPixelTraits = BGRAPixelTraits;
* An image loaded from storage.
*/
class Bitmap final
#ifdef ANDROID
: private GLSurfaceListener
#endif
{
public:
enum class Type {
Expand All @@ -84,14 +79,6 @@ class Bitmap final
};

protected:
#ifdef ANDROID
jobject bmp = nullptr;

UncompressedImage uncompressed;

Type type;
#endif

#ifdef ENABLE_OPENGL
GLTexture *texture = nullptr;
PixelSize size;
Expand Down Expand Up @@ -127,9 +114,7 @@ class Bitmap final
Bitmap &operator=(const Bitmap &other) = delete;
public:
bool IsDefined() const noexcept {
#ifdef ANDROID
return bmp != nullptr || uncompressed.IsDefined();
#elif defined(ENABLE_OPENGL)
#ifdef ENABLE_OPENGL
return texture != nullptr;
#elif defined(USE_MEMORY_CANVAS)
return buffer.data != nullptr;
Expand Down Expand Up @@ -236,10 +221,6 @@ class Bitmap final
bool flipped = false) noexcept;
bool MakeTexture(jobject _bmp, Type _type,
bool flipped = false) noexcept;

/* from GLSurfaceListener */
virtual void SurfaceCreated() override;
virtual void SurfaceDestroyed() override;
#endif
#endif
};
Expand Down
11 changes: 0 additions & 11 deletions src/ui/canvas/RawBitmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "util/ByteOrder.hxx"

#ifdef ENABLE_OPENGL
#include "opengl/Surface.hpp"
#include "ui/opengl/Features.hpp"
#endif

Expand Down Expand Up @@ -87,9 +86,6 @@ struct RawColor
* This class provides fast drawing methods and offscreen buffer.
*/
class RawBitmap final
#ifdef ENABLE_OPENGL
:private GLSurfaceListener
#endif
{
protected:
const unsigned width;
Expand Down Expand Up @@ -217,13 +213,6 @@ class RawBitmap final
void StretchTo(PixelSize src_size,
Canvas &dest_canvas, PixelSize dest_size,
bool transparent_white=false) const;

#ifdef ENABLE_OPENGL
private:
/* from GLSurfaceListener */
void SurfaceCreated() override;
void SurfaceDestroyed() override;
#endif
};

#endif // !defined(AFX_STSCREENBUFFER_H__22D62F5D_32E2_4785_B3D9_2341C11F84A3__INCLUDED_)
71 changes: 3 additions & 68 deletions src/ui/canvas/android/Bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Copyright_License {

#include "ui/canvas/Bitmap.hpp"
#include "ui/canvas/opengl/Texture.hpp"
#include "ui/canvas/opengl/Surface.hpp"
#include "Android/Bitmap.hpp"
#include "Android/NativeView.hpp"
#include "Android/Main.hpp"
Expand All @@ -36,24 +35,6 @@ Bitmap::Bitmap(ResourceId id)
Load(id);
}

Bitmap::Bitmap(Bitmap &&src) noexcept
:bmp(src.bmp),
uncompressed(std::move(src.uncompressed)),
type(src.type),
texture(src.texture),
size(src.size),
interpolation(src.interpolation),
flipped(src.flipped)
{
src.bmp = nullptr;
src.texture = nullptr;

if (IsDefined()) {
RemoveSurfaceListener(src);
AddSurfaceListener(*this);
}
}

static const char *
find_resource_name(unsigned id) noexcept
{
Expand All @@ -75,21 +56,14 @@ LoadResourceBitmap(ResourceId id)
}

bool
Bitmap::Set(JNIEnv *env, jobject _bmp, Type _type, bool flipped) noexcept
Bitmap::Set(JNIEnv *env, jobject bmp, Type type, bool flipped) noexcept
{
assert(bmp == nullptr);
assert(_bmp != nullptr);

bmp = env->NewGlobalRef(_bmp);

type = _type;
assert(bmp != nullptr);

size.width = AndroidBitmap::GetWidth(env, bmp);
size.height = AndroidBitmap::GetHeight(env, bmp);

AddSurfaceListener(*this);

if (surface_valid && !MakeTexture(bmp, type, flipped)) {
if (!MakeTexture(bmp, type, flipped)) {
Reset();
return false;
}
Expand Down Expand Up @@ -150,42 +124,3 @@ Bitmap::LoadFile(Path path)

return Set(Java::GetEnv(), new_bmp, Type::STANDARD, flipped);
}

void
Bitmap::Reset() noexcept
{
if (bmp != nullptr) {
auto *env = Java::GetEnv();
AndroidBitmap::Recycle(env, bmp);
env->DeleteGlobalRef(bmp);
bmp = nullptr;

RemoveSurfaceListener(*this);
} else if (uncompressed.IsDefined()) {
uncompressed = UncompressedImage();
RemoveSurfaceListener(*this);
}

delete texture;
texture = nullptr;
}

void
Bitmap::SurfaceCreated()
{
assert(bmp != nullptr || uncompressed.IsDefined());

if (bmp != nullptr)
MakeTexture(bmp, type);
else if (uncompressed.IsDefined())
MakeTexture(uncompressed, type);
}

void
Bitmap::SurfaceDestroyed()
{
assert(bmp != nullptr || uncompressed.IsDefined());

delete texture;
texture = nullptr;
}
23 changes: 0 additions & 23 deletions src/ui/canvas/opengl/Bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Copyright_License {
#include "Texture.hpp"
#include "Debug.hpp"

#ifndef ANDROID

Bitmap::Bitmap(Bitmap &&src) noexcept
:texture(src.texture),
size(src.size),
Expand All @@ -39,8 +37,6 @@ Bitmap::Bitmap(Bitmap &&src) noexcept
src.texture = nullptr;
}

#endif /* !ANDROID */

void
Bitmap::EnableInterpolation() noexcept
{
Expand Down Expand Up @@ -80,31 +76,14 @@ Bitmap::Load(UncompressedImage &&_uncompressed, Type _type)
size = { _uncompressed.GetWidth(), _uncompressed.GetHeight() };
flipped = _uncompressed.IsFlipped();

#ifdef ANDROID
uncompressed = std::move(_uncompressed);
type = _type;

AddSurfaceListener(*this);

if (!surface_valid)
return true;

if (!MakeTexture(uncompressed, type)) {
Reset();
return false;
}
#else
if (!MakeTexture(_uncompressed, _type)) {
Reset();
return false;
}
#endif

return true;
}

#ifndef ANDROID

void
Bitmap::Reset() noexcept
{
Expand All @@ -114,5 +93,3 @@ Bitmap::Reset() noexcept
delete texture;
texture = nullptr;
}

#endif /* !ANDROID */
4 changes: 0 additions & 4 deletions src/ui/canvas/opengl/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ Copyright_License {
*/

#include "Buffer.hpp"

#ifndef NDEBUG
unsigned num_buffers;
#endif
7 changes: 0 additions & 7 deletions src/ui/canvas/opengl/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ Copyright_License {
#include <cassert>
#include <stdlib.h>

#ifndef NDEBUG
extern unsigned num_buffers;
#endif

/**
* This class represents an OpenGL buffer object.
*/
Expand All @@ -56,7 +52,6 @@ class GLBuffer {

#ifndef NDEBUG
p = nullptr;
++num_buffers;
#endif
}

Expand All @@ -66,8 +61,6 @@ class GLBuffer {
~GLBuffer() noexcept {
#ifndef NDEBUG
assert(p == nullptr);
assert(num_buffers > 0);
--num_buffers;
#endif

glDeleteBuffers(1, &id);
Expand Down
Loading

0 comments on commit 6eef309

Please sign in to comment.