ImageBackend.cc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "ImageBackend.h"
  2. using namespace v8;
  3. ImageBackend::ImageBackend(int width, int height)
  4. : Backend("image", width, height)
  5. {}
  6. Backend *ImageBackend::construct(int width, int height){
  7. return new ImageBackend(width, height);
  8. }
  9. // This returns an approximate value only, suitable for Nan::AdjustExternalMemory.
  10. // The formats that don't map to intrinsic types (RGB30, A1) round up.
  11. int32_t ImageBackend::approxBytesPerPixel() {
  12. switch (format) {
  13. case CAIRO_FORMAT_ARGB32:
  14. case CAIRO_FORMAT_RGB24:
  15. return 4;
  16. #ifdef CAIRO_FORMAT_RGB30
  17. case CAIRO_FORMAT_RGB30:
  18. return 3;
  19. #endif
  20. case CAIRO_FORMAT_RGB16_565:
  21. return 2;
  22. case CAIRO_FORMAT_A8:
  23. case CAIRO_FORMAT_A1:
  24. return 1;
  25. default:
  26. return 0;
  27. }
  28. }
  29. cairo_surface_t* ImageBackend::createSurface() {
  30. assert(!surface);
  31. surface = cairo_image_surface_create(format, width, height);
  32. assert(surface);
  33. Nan::AdjustExternalMemory(approxBytesPerPixel() * width * height);
  34. return surface;
  35. }
  36. void ImageBackend::destroySurface() {
  37. if (surface) {
  38. cairo_surface_destroy(surface);
  39. surface = nullptr;
  40. Nan::AdjustExternalMemory(-approxBytesPerPixel() * width * height);
  41. }
  42. }
  43. cairo_format_t ImageBackend::getFormat() {
  44. return format;
  45. }
  46. void ImageBackend::setFormat(cairo_format_t _format) {
  47. this->format = _format;
  48. }
  49. Nan::Persistent<FunctionTemplate> ImageBackend::constructor;
  50. void ImageBackend::Initialize(Local<Object> target) {
  51. Nan::HandleScope scope;
  52. Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageBackend::New);
  53. ImageBackend::constructor.Reset(ctor);
  54. ctor->InstanceTemplate()->SetInternalFieldCount(1);
  55. ctor->SetClassName(Nan::New<String>("ImageBackend").ToLocalChecked());
  56. Nan::Set(target,
  57. Nan::New<String>("ImageBackend").ToLocalChecked(),
  58. Nan::GetFunction(ctor).ToLocalChecked()).Check();
  59. }
  60. NAN_METHOD(ImageBackend::New) {
  61. init(info);
  62. }