Skip to content

Commit

Permalink
Move GL Skia bits to their own classes to accomodate raster backends
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvb committed Mar 30, 2017
1 parent e91997e commit 21c0b76
Show file tree
Hide file tree
Showing 15 changed files with 433 additions and 139 deletions.
7 changes: 5 additions & 2 deletions graphics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ set(graphics_src
shape/rectangle.h++ shape/rectangle.c++
size.h++
skia_canvas.h++ skia_canvas.c++
skia_context.h++ skia_context.c++
skia_util.h++ skia_util.c++
skia_gl_context.h++ skia_gl_context.c++
skia_raster_context.h++ skia_raster_context.c++
skia_gl_canvas.h++ skia_gl_canvas.c++
skia_raster_canvas.h++ skia_raster_canvas.c++
skia_utility.h++ skia_utility.c++
)

# The library itself
Expand Down
181 changes: 78 additions & 103 deletions graphics/skia_canvas.c++
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* THE SOFTWARE.
**/

#include "skia_canvas.h++"
#include "graphics/skia_canvas.h++"

#include <core/debug.h++>

Expand All @@ -36,58 +36,24 @@
#include "graphics/shape/path.h++"
#include "graphics/shape/rectangle.h++"

#include "graphics/skia_util.h++"

#include <GL/gl.h>

#include <GrContext.h>
#include <GrGLInterface.h>
#include "graphics/skia_utility.h++"

#include <SkCanvas.h>
#include <SkColor.h>
#include <SkGradientShader.h>
#include <SkPath.h>
#include <SkRRect.h>

namespace skui
{
namespace graphics
{
namespace implementation
{
sk_sp<SkSurface> create_surface(const pixel_size& size,
const GrGLInterface& gl_interface,
GrContext& gr_context)
{
// Wrap the frame buffer object attached to the screen in a Skia render target so Skia can
// render to it
GrBackendRenderTargetDesc desc;
desc.fWidth = static_cast<int>(size.width);
desc.fHeight = static_cast<int>(size.height);
desc.fConfig = kSkia8888_GrPixelConfig;
desc.fOrigin = kBottomLeft_GrSurfaceOrigin;
desc.fSampleCnt = 0;
desc.fStencilBits = 8;
GrGLint buffer;
gl_interface.fFunctions.fGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer);
desc.fRenderTargetHandle = buffer;

// setup SkSurface
// To use distance field text, use commented out SkSurfaceProps instead
SkSurfaceProps props(SkSurfaceProps::kUseDeviceIndependentFonts_Flag, // distance field text
SkSurfaceProps::kLegacyFontHost_InitType);

return SkSurface::MakeFromBackendRenderTarget(&gr_context, desc, &props);
}
}

skia_canvas::skia_canvas(const pixel_size& size,
const GrGLInterface& gr_gl_interface,
skia_canvas::skia_canvas(//sk_sp<SkSurface> surface,
canvas_flags flags)
: canvas(flags)
, gr_context(GrContext::Create(kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(&gr_gl_interface)))
, surface(implementation::create_surface(size, gr_gl_interface, *gr_context))
, surface(nullptr/*surface*/)
{
SkASSERT(gr_context);
SkASSERT(surface);
//SkASSERT(surface);
}

void skia_canvas::draw(const color& background_color)
Expand All @@ -99,7 +65,6 @@ namespace skui

void skia_canvas::draw(const rectangle& rectangle)
{

auto canvas = surface->getCanvas();

const auto rect = SkRect::MakeXYWH(rectangle.position.x,
Expand All @@ -112,32 +77,43 @@ namespace skui
SkPath border;
border.addRoundRect(rect, rectangle.border.radius, rectangle.border.radius);

canvas->drawRRect(rounded_rect, make_fill_paint(rectangle));
canvas->drawRRect(rounded_rect, make_border_paint(rectangle));
canvas->save();
canvas->translate(.5f, .5f);
for(const auto& paint : {make_fill_paint(rectangle), make_border_paint(rectangle)})
{
canvas->drawRRect(rounded_rect, paint);
}
canvas->restore();
}

void skia_canvas::draw(const ellipse& ellipse)
{
auto canvas = surface->getCanvas();

canvas->save();
canvas->translate(.5f, .5f);
for(const auto& paint : make_paint(ellipse))
{
canvas->drawOval(SkRect::MakeXYWH(ellipse.position.x, ellipse.position.y,
ellipse.axes.height, ellipse.axes.width),
paint);
canvas->drawOval(SkRect::MakeXYWH(ellipse.position.x, ellipse.position.y,
ellipse.axes.height, ellipse.axes.width),
paint);
}
canvas->restore();
}

void skia_canvas::draw(const label& label)
{
auto canvas = surface->getCanvas();;

canvas->save();
canvas->translate(.5f, .5f);
for(const auto& paint : make_paint(label))
{
canvas->drawText(label.text.c_str(), label.text.size(),
label.position.x, label.position.y,
paint);
canvas->drawText(label.text.c_str(), label.text.size(),
label.position.x, label.position.y,
paint);
}
canvas->restore();
}

void skia_canvas::draw(const path& path)
Expand All @@ -148,8 +124,8 @@ namespace skui

for(const auto& paint : make_paint(path))
{
canvas->drawPath(skia_path,
paint);
canvas->drawPath(skia_path,
paint);
}
}

Expand All @@ -172,12 +148,11 @@ namespace skui
paint.setStrokeWidth(shape.border.thickness);

if(flags.test(canvas_flag::anti_alias))
paint.setAntiAlias(true);
paint.setAntiAlias(true);

return paint;
}


SkPaint skia_canvas::make_fill_paint(const shape& shape) const
{
SkPaint paint;
Expand All @@ -191,11 +166,11 @@ namespace skui

if(shape.fill.gradient)
{
set_gradient(paint, *shape.fill.gradient);
set_gradient(paint, *shape.fill.gradient);
}

if(flags.test(canvas_flag::anti_alias))
paint.setAntiAlias(true);
paint.setAntiAlias(true);

return paint;
}
Expand All @@ -204,53 +179,53 @@ namespace skui
{
switch(gradient.type)
{
case gradient_type::linear:
{
const auto& linear = static_cast<const linear_gradient&>(gradient);
auto points = to_skia(linear.points);
auto colors = to_skia(linear.colors);
paint.setShader(SkGradientShader::MakeLinear(points.data(),
colors.data(),
nullptr,
static_cast<int>(points.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::radial:
{
const auto& radial = static_cast<const radial_gradient&>(gradient);
paint.setShader(SkGradientShader::MakeRadial(to_skia(radial.center),
radial.radius,
to_skia(radial.colors).data(),
nullptr,
static_cast<int>(radial.positions.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::two_point_conical:
{
const auto& conical = static_cast<const two_point_conical_gradient&>(gradient);
paint.setShader(SkGradientShader::MakeTwoPointConical(to_skia(conical.start),
conical.start_radius,
to_skia(conical.end),
conical.end_radius,
to_skia(conical.colors).data(),
nullptr,
static_cast<int>(conical.colors.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::sweep:
{
const auto& sweep = static_cast<const sweep_gradient&>(gradient);
const auto colors = to_skia(sweep.colors);
paint.setShader(SkGradientShader::MakeSweep(sweep.center.x,
sweep.center.y,
colors.data(),
nullptr,
static_cast<int>(colors.size())));
return;
}
case gradient_type::linear:
{
const auto& linear = static_cast<const linear_gradient&>(gradient);
auto points = to_skia(linear.points);
auto colors = to_skia(linear.colors);
paint.setShader(SkGradientShader::MakeLinear(points.data(),
colors.data(),
nullptr,
static_cast<int>(points.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::radial:
{
const auto& radial = static_cast<const radial_gradient&>(gradient);
paint.setShader(SkGradientShader::MakeRadial(to_skia(radial.center),
radial.radius,
to_skia(radial.colors).data(),
nullptr,
static_cast<int>(radial.positions.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::two_point_conical:
{
const auto& conical = static_cast<const two_point_conical_gradient&>(gradient);
paint.setShader(SkGradientShader::MakeTwoPointConical(to_skia(conical.start),
conical.start_radius,
to_skia(conical.end),
conical.end_radius,
to_skia(conical.colors).data(),
nullptr,
static_cast<int>(conical.colors.size()),
SkShader::TileMode::kMirror_TileMode));
return;
}
case gradient_type::sweep:
{
const auto& sweep = static_cast<const sweep_gradient&>(gradient);
const auto colors = to_skia(sweep.colors);
paint.setShader(SkGradientShader::MakeSweep(sweep.center.x,
sweep.center.y,
colors.data(),
nullptr,
static_cast<int>(colors.size())));
return;
}
}
}
}
Expand Down
24 changes: 8 additions & 16 deletions graphics/skia_canvas.h++
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
#ifndef SKUI_GRAPHICS_SKIA_CANVAS_H
#define SKUI_GRAPHICS_SKIA_CANVAS_H

#include <core/debug.h++>

#include "canvas.h++"

#include "size.h++"
#include "graphics/canvas.h++"
#include "graphics/size.h++"

#include <GrGLInterface.h>

Expand All @@ -45,16 +42,9 @@ namespace skui
{
namespace graphics
{
namespace implementation
{
void set_gradient(SkPaint& paint, const gradient& gradient);
}
class skia_canvas : public canvas
{
public:
skia_canvas(const pixel_size& size,
const GrGLInterface& gl_interface,
canvas_flags flags);
~skia_canvas() override = default;

void draw(const color& background_color) override;
Expand All @@ -63,15 +53,17 @@ namespace skui
void draw(const label& label) override;
void draw(const path& path) override;

protected:
skia_canvas(//sk_sp<SkSurface> surface,
canvas_flags flags);

sk_sp<SkSurface> surface;

private:
std::vector<SkPaint> make_paint(const shape& shape) const;
SkPaint make_border_paint(const shape& shape) const;
SkPaint make_fill_paint(const shape& shape) const;
void set_gradient(SkPaint& paint, const gradient& gradient) const;


sk_sp<GrContext> gr_context;
sk_sp<SkSurface> surface;
};
}
}
Expand Down
Loading

0 comments on commit 21c0b76

Please sign in to comment.