Path: blob/master/servers/rendering/renderer_canvas_render.cpp
10277 views
/**************************************************************************/1/* renderer_canvas_render.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "renderer_canvas_render.h"31#include "servers/rendering/rendering_server_globals.h"3233RendererCanvasRender *RendererCanvasRender::singleton = nullptr;3435const Rect2 &RendererCanvasRender::Item::get_rect() const {36if (custom_rect || (!rect_dirty && !update_when_visible && skeleton == RID())) {37return rect;38}3940//must update rect4142if (commands == nullptr) {43rect = Rect2();44rect_dirty = false;45return rect;46}4748Transform2D xf;49bool found_xform = false;50bool first = true;5152const Item::Command *c = commands;5354while (c) {55Rect2 r;5657switch (c->type) {58case Item::Command::TYPE_RECT: {59const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);60r = crect->rect;6162} break;63case Item::Command::TYPE_NINEPATCH: {64const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);65r = style->rect;66} break;6768case Item::Command::TYPE_POLYGON: {69const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);70r = polygon->polygon.rect_cache;71} break;72case Item::Command::TYPE_PRIMITIVE: {73const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);74for (uint32_t j = 0; j < primitive->point_count; j++) {75if (j == 0) {76r.position = primitive->points[0];77} else {78r.expand_to(primitive->points[j]);79}80}81} break;82case Item::Command::TYPE_MESH: {83const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);84AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton);8586r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);8788} break;89case Item::Command::TYPE_MULTIMESH: {90const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);91AABB aabb = RSG::mesh_storage->multimesh_get_aabb(multimesh->multimesh);9293r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);9495} break;96case Item::Command::TYPE_PARTICLES: {97const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);98if (particles_cmd->particles.is_valid()) {99AABB aabb = RSG::particles_storage->particles_get_aabb(particles_cmd->particles);100r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);101}102103} break;104case Item::Command::TYPE_TRANSFORM: {105const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);106xf = transform->xform;107found_xform = true;108[[fallthrough]];109}110default: {111c = c->next;112continue;113}114}115116if (found_xform) {117r = xf.xform(r);118}119120if (first) {121rect = r;122first = false;123} else {124rect = rect.merge(r);125}126c = c->next;127}128129rect_dirty = false;130return rect;131}132133RendererCanvasRender::Item::CommandMesh::~CommandMesh() {134if (mesh_instance.is_valid()) {135RSG::mesh_storage->mesh_instance_free(mesh_instance);136}137}138139140