/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ gb_device_ref_t gb_device_init(gb_window_ref_t window) { // check tb_assert_and_check_return_val(window, tb_null); // the window mode tb_size_t mode = gb_window_mode(window); tb_assert_and_check_return_val(mode, tb_null); // done gb_device_ref_t device = tb_null; switch (mode) { #ifdef GB_CONFIG_PACKAGE_HAVE_OPENGL case GB_WINDOW_MODE_GL: device = gb_device_init_gl(window); break; #endif case GB_WINDOW_MODE_BITMAP: #if defined(GB_CONFIG_PACKAGE_HAVE_SKIA) device = gb_device_init_skia(gb_window_bitmap(window)); #elif defined(GB_CONFIG_DEVICE_HAVE_BITMAP) device = gb_device_init_bitmap(gb_window_bitmap(window)); #else // trace tb_trace_e("no bitmap device!"); #endif break; default: // trace tb_trace_e("unknown window mode: %lu", mode); break; } // init tb_bool_t ok = tb_false; do { // check gb_device_impl_t* impl = (gb_device_impl_t*)device; tb_assert_and_check_break(impl); // init pixfmt impl->pixfmt = (tb_uint16_t)gb_window_pixfmt(window); // init width and height impl->width = (tb_uint16_t)gb_window_width(window); impl->height = (tb_uint16_t)gb_window_height(window); // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (device) gb_device_exit(device); device = tb_null; } // ok? return device; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ gb_device_ref_t gb_device_init_skia(gb_bitmap_ref_t bitmap) { // check tb_assert_and_check_return_val(bitmap, tb_null); // done tb_bool_t ok = tb_false; gb_skia_device_ref_t impl = tb_null; do { // the width and height tb_size_t width = gb_bitmap_width(bitmap); tb_size_t height = gb_bitmap_height(bitmap); tb_assert_and_check_break(width && height && width <= GB_WIDTH_MAXN && height <= GB_HEIGHT_MAXN); // make device impl = tb_malloc0_type(gb_skia_device_t); tb_assert_and_check_break(impl); // init base impl->base.type = GB_DEVICE_TYPE_BITMAP; impl->base.resize = gb_device_skia_resize; impl->base.draw_clear = gb_device_skia_draw_clear; impl->base.draw_path = gb_device_skia_draw_path; impl->base.draw_lines = gb_device_skia_draw_lines; impl->base.draw_points = gb_device_skia_draw_points; impl->base.draw_polygon = gb_device_skia_draw_polygon; impl->base.shader_linear = gb_device_skia_shader_linear; impl->base.shader_radial = gb_device_skia_shader_radial; impl->base.shader_bitmap = gb_device_skia_shader_bitmap; impl->base.exit = gb_device_skia_exit; // make bitmap impl->bitmap = new SkBitmap(); tb_assert_and_check_break(impl->bitmap); // init bitmap impl->bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height, gb_bitmap_row_bytes(bitmap)); impl->bitmap->setPixels(gb_bitmap_data(bitmap)); // make canvas impl->canvas = new SkCanvas(*impl->bitmap); tb_assert_and_check_break(impl->canvas); // init canvas impl->canvas->resetMatrix(); // make paint impl->paint = new SkPaint(); tb_assert_and_check_break(impl->paint); // make path impl->path = new SkPath(); tb_assert_and_check_break(impl->path); // ok ok = tb_true; } while (0); // failed? if (!ok) { // exit it if (impl) gb_device_exit((gb_device_ref_t)impl); impl = tb_null; } // ok? return (gb_device_ref_t)impl; }