Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Merge from master (#140 nested layers). See #172 #230.
Browse files Browse the repository at this point in the history
  • Loading branch information
wsnook committed May 22, 2014
2 parents 8c65a77 + 04e70b2 commit 84fdb66
Show file tree
Hide file tree
Showing 38 changed files with 801 additions and 184 deletions.
4 changes: 4 additions & 0 deletions common/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ void show_color_debug_image(std::string name, const char *data, size_t logical_w
float x_scale = (float)fb_width / (float)width;
float y_scale = (float)fb_height / (float)height;

glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glPixelZoom(x_scale, -y_scale);
glRasterPos2f(-1.0f, 1.0f);
glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, data);
Expand Down
17 changes: 13 additions & 4 deletions include/llmr/geometry/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class Buffer : private util::noncopyable {
}
glBindBuffer(bufferType, buffer);
if (force) {
assert("Buffer was already deleted" && array != nullptr);
if (array == nullptr) {
throw std::runtime_error("Buffer was already deleted or doesn't contain elements");
}

glBufferData(bufferType, pos, array, GL_STATIC_DRAW);
if (!retainAfterUpload) {
cleanup();
Expand All @@ -58,19 +61,25 @@ class Buffer : private util::noncopyable {
protected:
// increase the buffer size by at least /required/ bytes.
inline void *addElement() {
assert("Buffer is already bound to GPU" && buffer == 0);
if (buffer != 0) {
throw std::runtime_error("Can't add elements after buffer was bound to GPU");
}
if (length < pos + itemSize) {
while (length < pos + itemSize) length += defaultLength;
array = realloc(array, length);
assert("Buffer reallocation failed" && array != nullptr);
if (array == nullptr) {
throw std::runtime_error("Buffer reallocation failed¯");
}
}
pos += itemSize;
return static_cast<char *>(array) + (pos - itemSize);
}

// Get a pointer to the item at a given index.
inline void *getElement(size_t index) {
assert("Buffer was deleted" && array != nullptr);
if (array == nullptr) {
throw std::runtime_error("Buffer was already deleted or doesn't contain elements");
}

if (index * itemSize >= pos) {
throw new std::runtime_error("Can't get element after array bounds");
Expand Down
18 changes: 13 additions & 5 deletions include/llmr/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace llmr {

class Source;

typedef std::map<std::string, const std::unique_ptr<Source>> Sources;

class Map : private util::noncopyable {
public:
explicit Map(View &view);
Expand All @@ -40,6 +42,9 @@ class Map : private util::noncopyable {
// Forces a map update: always triggers a rerender.
void update();

// Triggers a cleanup that releases resources.
void cleanup();

// Controls buffer swapping.
bool needsSwap();
void swapped();
Expand Down Expand Up @@ -96,19 +101,21 @@ class Map : private util::noncopyable {
inline uv_loop_t *getLoop() { return loop; }
inline time getAnimationTime() const { return animationTime; }
inline Texturepool &getTexturepool() { return texturepool; }
inline const Sources &getSources() { return sources; }

private:
// uv async callbacks
static void render(uv_async_t *async);
static void terminate(uv_async_t *async);
static void cleanup(uv_async_t *async);
static void delete_async(uv_handle_t *handle);

// Setup
void setup();
void loadStyle(const uint8_t *const data, uint32_t bytes);

void updateTiles();
void updateClippingIDs();
void updateRenderState();

size_t countLayers(const std::vector<LayerDescription>& layers);

Expand All @@ -120,7 +127,7 @@ class Map : private util::noncopyable {

// Unconditionally performs a render with the current map state.
void render();
void renderLayers(const std::vector<LayerDescription>& layers, RenderPass pass);
void renderLayers(const std::vector<LayerDescription>& layers);
void renderLayer(const LayerDescription& layer_desc, RenderPass pass);

private:
Expand All @@ -147,19 +154,20 @@ class Map : private util::noncopyable {
SpriteAtlas spriteAtlas;
Painter painter;

std::map<std::string, const std::unique_ptr<Source>> sources;
Sources sources;

bool debug = false;
time animationTime = 0;
float strata_thickness = 0.0f;
int strata = 0;

int indent = 0;

private:
bool async = false;
uv_loop_t *loop = nullptr;
uv_thread_t thread;
uv_async_t *async_terminate = nullptr;
uv_async_t *async_render = nullptr;
uv_async_t *async_cleanup = nullptr;
};

}
Expand Down
4 changes: 3 additions & 1 deletion include/llmr/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop


bool update();
size_t prepareRender(const TransformState &transform);
void updateMatrices(const TransformState &transform);
void drawClippingMasks();
size_t getTileCount() const;
void render(const LayerDescription& layer_desc, const BucketDescription &bucket_desc);
void finishRender();

Expand Down
3 changes: 2 additions & 1 deletion include/llmr/map/transform_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TransformState {
box cornersToBox(uint32_t z) const;

// Dimensions
bool hasSize() const;
uint16_t getWidth() const;
uint16_t getHeight() const;
uint16_t getFramebufferWidth() const;
Expand Down Expand Up @@ -60,7 +61,7 @@ class TransformState {
// map position
double x = 0, y = 0;
double angle = 0;
double scale = 1;
double scale = std::numeric_limits<double>::infinity();
};

}
Expand Down
1 change: 1 addition & 0 deletions include/llmr/platform/gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define glBindVertexArray glBindVertexArrayOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define GL_ARB_vertex_array_object 1
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#elif TARGET_IPHONE_SIMULATOR
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
Expand Down
40 changes: 35 additions & 5 deletions include/llmr/renderer/painter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@
#include <llmr/shader/raster_shader.hpp>
#include <llmr/shader/text_shader.hpp>
#include <llmr/shader/dot_shader.hpp>
#include <llmr/shader/composite_shader.hpp>

#include <llmr/map/transform_state.hpp>

#include <map>

namespace llmr {

class Transform;
class Style;
class Tile;
class GlyphAtlas;
class Source;

class FillBucket;
class LineBucket;
Expand All @@ -37,12 +41,22 @@ class RasterBucket;
class LayerDescription;
class RasterTileData;

typedef std::map<std::string, const std::unique_ptr<Source>> Sources;

class Painter : private util::noncopyable {
public:
Painter(Map &map);
~Painter();


void setup();

// Perform cleanup tasks that prepare shutting down the app. This doesn't mean that the
// app will be shut down. That means all operations must be automatically be reversed (e.g. through
// lazy initialization) in case rendering continues.
void cleanup();


void clear();

// Updates the default matrices to the current viewport dimensions.
Expand Down Expand Up @@ -75,16 +89,22 @@ class Painter : private util::noncopyable {
void setDebug(bool enabled);

// Opaque/Translucent pass setting
void startOpaquePass();
void startTranslucentPass();
void endPass();
void setOpaque();
void setTranslucent();

// Configures the painter strata that is used for early z-culling of fragments.
void setStrata(float strata);

void prepareClippingMask();
void drawClippingMasks(const Sources &sources);
void drawClippingMask(const mat4& matrix, const ClipID& clip);
void finishClippingMask();

void clearFramebuffers();
void resetFramebuffer();
void bindFramebuffer();
void pushFramebuffer();
GLuint popFramebuffer();
void discardFramebuffers();
void drawComposite(GLuint texture, const CompositeProperties &properties);

bool needsAnimation() const;
private:
Expand Down Expand Up @@ -127,6 +147,7 @@ class Painter : private util::noncopyable {
std::unique_ptr<RasterShader> rasterShader;
std::unique_ptr<TextShader> textShader;
std::unique_ptr<DotShader> dotShader;
std::unique_ptr<CompositeShader> compositeShader;

// Set up the stencil quad we're using to generate the stencil mask.
VertexBuffer tileStencilBuffer = {
Expand All @@ -144,6 +165,8 @@ class Painter : private util::noncopyable {
VertexArrayObject coveringPlainArray;
VertexArrayObject coveringPatternArray;
VertexArrayObject coveringRasterArray;

VertexArrayObject compositeArray;
VertexArrayObject matteArray;

// Set up the tile boundary lines we're using to draw the tile outlines.
Expand All @@ -157,6 +180,13 @@ class Painter : private util::noncopyable {

VertexArrayObject tileBorderArray;

// Framebuffer management
std::vector<GLuint> fbos;
std::vector<GLuint> fbos_color;
GLuint fbo_depth_stencil;
int fbo_level = -1;
bool fbo_depth_stencil_valid = false;

};

}
Expand Down
29 changes: 29 additions & 0 deletions include/llmr/shader/composite_shader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef LLMR_SHADER_COMPOSITE_SHADER
#define LLMR_SHADER_COMPOSITE_SHADER

#include <llmr/shader/shader.hpp>

namespace llmr {

class CompositeShader : public Shader {
public:
CompositeShader();

void bind(char *offset);

void setImage(int32_t image);
void setOpacity(float opacity);

private:
int32_t a_pos = -1;

int32_t image = 0;
int32_t u_image = -1;

float opacity = 0;
int32_t u_opacity = -1;
};

}

#endif
7 changes: 4 additions & 3 deletions include/llmr/shader/raster_shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class RasterShader : public Shader {
private:
int32_t a_pos = -1;

int32_t image = -1;
int32_t image = 0;
int32_t u_image = -1;
float opacity = 0.0f;
float u_opacity = 0.0f;

float opacity = 0;
int32_t u_opacity = -1;
};

}
Expand Down
1 change: 1 addition & 0 deletions include/llmr/style/bucket_description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class BucketDescription {
};

std::ostream& operator<<(std::ostream&, const BucketDescription& bucket);
std::ostream& operator<<(std::ostream&, BucketType type);

}

Expand Down
1 change: 1 addition & 0 deletions include/llmr/style/class_description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ClassDescription {
std::map<std::string, IconClass> icon;
std::map<std::string, TextClass> text;
std::map<std::string, RasterClass> raster;
std::map<std::string, CompositeClass> composite;
};


Expand Down
14 changes: 14 additions & 0 deletions include/llmr/style/properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ struct RasterProperties {
float opacity = 1.0;
};

struct CompositeClass {
FunctionProperty enabled = true;
FunctionProperty opacity = 1;
};

struct CompositeProperties {
inline CompositeProperties() {}
bool enabled = true;
float opacity = 1.0;
};


const CompositeProperties defaultCompositeProperties;

}

#endif
1 change: 1 addition & 0 deletions include/llmr/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Style {
std::map<std::string, IconProperties> icons;
std::map<std::string, TextProperties> texts;
std::map<std::string, RasterProperties> rasters;
std::map<std::string, CompositeProperties> composites;
} computed;
};

Expand Down
17 changes: 14 additions & 3 deletions include/llmr/style/style_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,30 @@ class StyleParser {
void parseBuckets(JSVal value, std::map<std::string, BucketDescription>& buckets);
void parseLayers(JSVal value, std::vector<LayerDescription>& layers);
void parseConstants(JSVal value);
void parseClasses(JSVal value, std::map<std::string, ClassDescription>& classes, std::map<std::string, BucketDescription>& buckets, std::vector<LayerDescription>& layers);
void parseClasses(JSVal value, std::map<std::string, ClassDescription> &classes,
const std::map<std::string, BucketDescription> &buckets,
const std::vector<LayerDescription> &layers);

private:
LayerDescription parseLayer(JSVal value);
BucketDescription parseBucket(JSVal value);
std::pair<std::string, ClassDescription> parseClassDescription(std::string styleName, JSVal value, std::map<std::string, BucketDescription>& buckets, std::vector<LayerDescription>& layers);
void parseClass(const std::string& name, JSVal value, ClassDescription& class_desc, std::map<std::string, BucketDescription>& buckets, std::vector<LayerDescription>& layers);

std::pair<std::string, ClassDescription>
parseClassDescription(std::string styleName, JSVal value,
const std::map<std::string, BucketDescription> &buckets,
const std::map<std::string, std::string> &layerBuckets);

void parseClass(const std::string &name, JSVal value, ClassDescription &class_desc,
const std::map<std::string, BucketDescription> &buckets,
const std::map<std::string, std::string> &layerBuckets);

FillClass parseFillClass(JSVal value);
LineClass parseLineClass(JSVal value);
IconClass parseIconClass(JSVal value);
TextClass parseTextClass(JSVal value);
BackgroundClass parseBackgroundClass(JSVal value);
RasterClass parseRasterClass(JSVal value);
CompositeClass parseCompositeClass(JSVal value);

bool parseBoolean(JSVal value);
std::string parseString(JSVal value);
Expand Down
Loading

0 comments on commit 84fdb66

Please sign in to comment.