summaryrefslogtreecommitdiff
path: root/src/mbgl/util/raster.cpp
blob: a74b73a016df160f6f98954e797e927384062749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <mbgl/platform/platform.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/platform/log.hpp>

#include <mbgl/util/raster.hpp>

#include <cassert>
#include <cstring>

using namespace mbgl;

Raster::Raster(gl::TexturePool& texturePool_)
    : texturePool(texturePool_)
{}

Raster::~Raster() {
    if (textured) {
        texturePool.releaseTextureID(textureID);
        textureID = 0;
    }
}

bool Raster::isLoaded() const {
    std::lock_guard<std::mutex> lock(mtx);
    return loaded;
}

void Raster::load(PremultipliedImage image) {
    assert(image.data.get());

    img = std::move(image);
    width = GLsizei(img.width);
    height = GLsizei(img.height);

    std::lock_guard<std::mutex> lock(mtx);
    loaded = true;
}


void Raster::bind(bool linear, gl::GLObjectStore& glObjectStore) {
    if (!width || !height) {
        Log::Error(Event::OpenGL, "trying to bind texture without dimension");
        return;
    }

    if (img.data && !textured) {
        upload(glObjectStore);
    } else if (textured) {
        MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, textureID));
    }

    GLint new_filter = linear ? GL_LINEAR : GL_NEAREST;
    if (new_filter != this->filter) {
        MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, new_filter));
        MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, new_filter));
        filter = new_filter;
    }
}

void Raster::upload(gl::GLObjectStore& glObjectStore) {
    if (img.data && !textured) {
        textureID = texturePool.getTextureID(glObjectStore);
        MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, textureID));
#ifndef GL_ES_VERSION_2_0
        MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
#endif
        MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
        MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
        MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img.data.get()));
        img.data.reset();
        textured = true;
    }
}