From b28bac1f5ff27d8d09344b4263d62101ea1effaf Mon Sep 17 00:00:00 2001 From: Ada Date: Thu, 27 Feb 2025 06:22:56 +1000 Subject: [PATCH] Canvas closer but still not working --- .../src/RenderableCanvasEntityItem.cpp | 30 +++---------------- .../src/RenderableCanvasEntityItem.h | 4 +-- .../entities/src/CanvasEntityItem.cpp.in | 19 +++++++----- libraries/entities/src/CanvasEntityItem.h.in | 4 +++ .../entities/src/EntityItemProperties.txt | 4 +-- .../entities/src/EntityScriptingInterface.cpp | 1 - 6 files changed, 23 insertions(+), 39 deletions(-) diff --git a/libraries/entities-renderer/src/RenderableCanvasEntityItem.cpp b/libraries/entities-renderer/src/RenderableCanvasEntityItem.cpp index 01c514cc2a..c25fd1d4fc 100644 --- a/libraries/entities-renderer/src/RenderableCanvasEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableCanvasEntityItem.cpp @@ -11,34 +11,12 @@ using namespace render; using namespace render::entities; -CanvasEntityRenderer::CanvasEntityRenderer(const EntityItemPointer& entity) : Parent(entity) { - auto canvas = std::dynamic_pointer_cast(entity); - _width = canvas->getWidth(); - _width = canvas->getHeight(); -} +CanvasEntityRenderer::CanvasEntityRenderer(const EntityItemPointer& entity) : Parent(entity) { } CanvasEntityRenderer::~CanvasEntityRenderer() { } -void CanvasEntityRenderer::doRenderUpdateSynchronousTyped(const ScenePointer& scene, Transaction& transaction, const TypedEntityPointer& entity) { - _width = entity->getWidth(); - _height = entity->getHeight(); +void CanvasEntityRenderer::doRenderUpdateAsynchronousTyped(const TypedEntityPointer& entity) { + _texture = entity->getTexture(); - qDebug() << "width: " << _width << ", height: " << _height; - - if (entity->needsRenderUpdate()) { - // misaligned size, can't safely copy - if (entity->getImageData().length() != _width * _height * 4) { - entity->setNeedsRenderUpdate(false); - return; - } - - auto texture = gpu::Texture::createStrict(gpu::Element::COLOR_SRGBA_32, _width, _height); - texture->setStoredMipFormat(gpu::Element::COLOR_SRGBA_32); - texture->setAutoGenerateMips(false); - texture->assignStoredMip(0, _width * _height * 4, reinterpret_cast(entity->getImageData().data())); - texture->setSource("CanvasEntityRenderer"); - _texture = texture; - - entity->setNeedsRenderUpdate(false); - } + qDebug() << "CanvasEntityRenderer::doRenderUpdateAsynchronousTyped"; } diff --git a/libraries/entities-renderer/src/RenderableCanvasEntityItem.h b/libraries/entities-renderer/src/RenderableCanvasEntityItem.h index fe61195d5d..e26952d1d0 100644 --- a/libraries/entities-renderer/src/RenderableCanvasEntityItem.h +++ b/libraries/entities-renderer/src/RenderableCanvasEntityItem.h @@ -32,12 +32,10 @@ protected: virtual bool wantsHandControllerPointerEvents() const override { return false; } virtual bool wantsKeyboardFocus() const override { return false; } - virtual void doRenderUpdateSynchronousTyped(const ScenePointer& scene, Transaction& transaction, const TypedEntityPointer& entity) override; + virtual void doRenderUpdateAsynchronousTyped(const TypedEntityPointer& entity) override; private: gpu::TexturePointer _texture; - - int _width, _height; }; } } diff --git a/libraries/entities/src/CanvasEntityItem.cpp.in b/libraries/entities/src/CanvasEntityItem.cpp.in index 92a4ee2866..2effb5f591 100644 --- a/libraries/entities/src/CanvasEntityItem.cpp.in +++ b/libraries/entities/src/CanvasEntityItem.cpp.in @@ -16,8 +16,7 @@ EntityItemPointer CanvasEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { std::shared_ptr entity(new CanvasEntityItem(entityID), [](CanvasEntityItem* ptr) { ptr->deleteLater(); }); entity->setProperties(properties); - size_t bufferSize = 4 * static_cast(properties._width) + static_cast(properties._height); - qDebug() << __FUNCTION__ << ": (4 * " << properties._width << " * " << properties._height << " = " << bufferSize; + size_t bufferSize = 4 * static_cast(properties._width) * static_cast(properties._height); entity->_imageData = QByteArray(bufferSize, (unsigned char)255); return entity; } @@ -83,7 +82,7 @@ bool CanvasEntityItem::setSubClassProperties(const EntityItemProperties& propert // reallocate and resize if the size properties are changed if (oldWidth != getWidth() || oldHeight != getHeight()) { - size_t bufferSize = 4 * static_cast(getWidth()) + static_cast(getHeight()); + size_t bufferSize = 4 * static_cast(getWidth()) * static_cast(getHeight()); _imageData = QByteArray(bufferSize, (unsigned char)255); } @@ -103,16 +102,22 @@ QByteArray& CanvasEntityItem::getImageData() { } void CanvasEntityItem::setImageData(const QByteArray& data) { - if (data.length() != _imageData.length()) { + if (data.length() != _width * _height * 4) { return; } _imageData = data; - _needsRenderUpdate = true; + + auto texture = gpu::Texture::createStrict(gpu::Element::COLOR_SRGBA_32, _width, _height); + texture->setStoredMipFormat(gpu::Element::COLOR_SRGBA_32); + texture->setAutoGenerateMips(false); + texture->assignStoredMip(0, _width * _height * 4, reinterpret_cast(_imageData.data())); + texture->setSource("CanvasEntityRenderer"); + _texture = texture; + + setNeedsRenderUpdate(true); } void CanvasEntityItem::setImageSubData(const QByteArray& data, uint32_t dx, uint32_t dy, uint32_t dw, uint32_t dh, uint32_t sx, uint32_t sy, uint32_t sw, uint32_t sh) { qCWarning(entities) << "CanvasEntityItem::setImageSubData unimplemented!"; - - _needsRenderUpdate = false; } diff --git a/libraries/entities/src/CanvasEntityItem.h.in b/libraries/entities/src/CanvasEntityItem.h.in index d389dad6d1..5d2581c3d3 100644 --- a/libraries/entities/src/CanvasEntityItem.h.in +++ b/libraries/entities/src/CanvasEntityItem.h.in @@ -9,6 +9,7 @@ #define hifi_CanvasEntityItem_h #include "EntityItem.h" +#include class CanvasEntityItem : public EntityItem { public: @@ -28,10 +29,13 @@ public: void setImageData(const QByteArray& data); void setImageSubData(const QByteArray& data, uint32_t dx, uint32_t dy, uint32_t dw, uint32_t dh, uint32_t sx, uint32_t sy, uint32_t sw, uint32_t sh); + gpu::TexturePointer getTexture() const { return _texture; } + protected: @Canvas_ENTITY_PROPS@ QByteArray _imageData; + gpu::TexturePointer _texture; }; #endif // hifi_CanvasEntityItem_h diff --git a/libraries/entities/src/EntityItemProperties.txt b/libraries/entities/src/EntityItemProperties.txt index 579a9eb94c..a36cc48da2 100644 --- a/libraries/entities/src/EntityItemProperties.txt +++ b/libraries/entities/src/EntityItemProperties.txt @@ -267,7 +267,7 @@ enum:SOUND_LOOP prop:loop type:bool default:true, enum:SOUND_POSITIONAL prop:positional type:bool default:true, enum:SOUND_LOCAL_ONLY prop:localOnly type:bool default:false, Canvas -enum:CANVAS_WIDTH prop:width type:uint16_t default:300 renderProp, -enum:CANVAS_HEIGHT prop:height type:uint16_t default:150 renderProp, +enum:CANVAS_WIDTH prop:width type:uint16_t default:300 basicProp, +enum:CANVAS_HEIGHT prop:height type:uint16_t default:150 basicProp, enum:CANVAS_BG_COLOR prop:bgColor type:u8vec3Color default:ENTITY_ITEM_DEFAULT_COLOR basicProp, enum:CANVAS_BG_ALPHA prop:bgAlpha type:float default:1.0f min:0.0f max:1.0f basicProp, diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index bf4173b18b..c5c1c94172 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -2702,7 +2702,6 @@ void EntityScriptingInterface::canvasSubmitImage(const QUuid& entityID, const QB if (imageData.length() != canvas->getImageData().length()) { qCDebug(entities) << "canvasSubmitImage with different sized buffers on " << entityID << ": input size: " << imageData.length() << ", canvas size: " << canvas->getImageData().length(); - qCDebug(entities) << "width: " << canvas->getWidth() << ", height: " << canvas->getHeight(); } canvas->setImageData(imageData);