Commit b8f3f49d authored by Linshizhi's avatar Linshizhi

implement Canvas.

parent 507026b6
#include <exception>
#include "canvas.h" #include "canvas.h"
#include "pixelTypes.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkColorType.h"
std::shared_ptr<Canvas> Canvas::MakeCanvas(int width, int height, PixelTypes pixType) { namespace {
SkColorType pixelTypeConvert(PixelType type) {
switch (type) {
case RGBA:
return kRGBA_8888_SkColorType;
default:
throw std::runtime_error("invalid pixel type");
}
}
SkAlphaType alphaTypeConvert(AlphaType alpha) {
switch (alpha) {
case PREMUL:
return kPremul_SkAlphaType;
default:
throw std::runtime_error("invalid alpha type");
}
}
};
std::unique_ptr<Canvas> Canvas::MakeCanvas(
int width, int height, PixelType pixType, AlphaType alpha) {
std::unique_ptr<Canvas> canvas =
std::make_unique<Canvas>(width, height, pixType);
return canvas;
}
Canvas::Canvas(int width, int height, PixelType pixType, AlphaType alpha): canvas_(nullptr) {
SkImageInfo imgInfo = SkImageInfo::Make(
width, height,
pixelTypeConvert(pixType),
alphaTypeConvert(alpha));
uint8_t *buffer = new uint8_t[width * height * 4];
canvas_ = SkCanvas::MakeRasterDirect(imgInfo, buffer, width * 4);
} }
#include <memory> #include <memory>
#include "pixelTypes.h"
#include "include/core/SkCanvas.h"
#include "Drawable.h" #include "Drawable.h"
#ifndef CANVAS_H #ifndef CANVAS_H
#define CANVAS_H #define CANVAS_H
enum PixelTypes {};
class Canvas { class Canvas {
public: public:
static std::shared_ptr<Canvas> MakeCanvas(int width, int height, PixelTypes pixType); static std::unique_ptr<Canvas> MakeCanvas(
int width, int height, PixelType pixType, AlphaType alpha);
template<typename T> template<typename T>
bool draw(Drawable<T>* drawable); bool draw(Drawable<T>* drawable);
protected: protected:
Canvas(int width, int height, PixelTypes pixType); Canvas(int width, int height, PixelType pixType, AlphaType alpha);
private:
std::unique_ptr<SkCanvas> canvas_;
}; };
#endif /* CANVAS_H */ #endif /* CANVAS_H */
#ifndef PIXELTYPES_H
#define PIXELTYPES_H
enum PixelType {
RGBA = 0,
};
enum AlphaType {
PREMUL,
};
#endif /* PIXELTYPES_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment