Skip to content

Commit

Permalink
Use raw pointer (#11804)
Browse files Browse the repository at this point in the history
* Revert "use IntrusivePtr to share object (#11729)"

This reverts commit 8caec59.

* Framebuffer uses weak reference

* make doDestroy reentency
  • Loading branch information
minggo authored Jul 4, 2022
1 parent 98a53fc commit 02a1eb0
Show file tree
Hide file tree
Showing 45 changed files with 119 additions and 90 deletions.
3 changes: 1 addition & 2 deletions native/cocos/base/Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

#include <memory>
#include "Macros.h"
#include "base/Ptr.h"

namespace cc {

Expand All @@ -52,7 +51,7 @@ class CC_DLL Agent : public Actor {
inline Actor *getActor() const noexcept { return _actor; }

protected:
IntrusivePtr<Actor> _actor;
Actor *_actor{nullptr};
};

} // namespace cc
2 changes: 1 addition & 1 deletion native/cocos/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void Engine::destroy() {

delete _builtinResMgr;
delete _programLib;
CC_SAFE_DELETE(_gfxDevice);
CC_SAFE_DESTROY_AND_DELETE(_gfxDevice);
delete _fs;
_scheduler.reset();

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/BufferAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ BufferAgent::~BufferAgent() {
BufferDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/CommandBufferAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ CommandBufferAgent::~CommandBufferAgent() {
DeviceAgent::getInstance()->getMessageQueue(), CommandBufferDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/DescriptorSetAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ DescriptorSetAgent::~DescriptorSetAgent() {
DescriptorSetDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DescriptorSetLayoutAgent::~DescriptorSetLayoutAgent() {
DescriptorSetLayoutDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
31 changes: 19 additions & 12 deletions native/cocos/renderer/gfx-agent/DeviceAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DeviceAgent::DeviceAgent(Device *device) : Agent(device) {
}

DeviceAgent::~DeviceAgent() {
destroy();
CC_SAFE_DELETE(_actor);
DeviceAgent::instance = nullptr;
}

Expand All @@ -83,8 +83,8 @@ bool DeviceAgent::doInit(const DeviceInfo &info) {
// TODO(PatriceJiang): replace with: _mainMessageQueue = ccnew MessageQueue;
_mainMessageQueue = ccnew_placement(CC_MALLOC_ALIGN(sizeof(MessageQueue), alignof(MessageQueue))) MessageQueue; // NOLINT

static_cast<CommandBufferAgent *>(_cmdBuff.get())->_queue = _queue;
static_cast<CommandBufferAgent *>(_cmdBuff.get())->initAgent();
static_cast<CommandBufferAgent *>(_cmdBuff)->_queue = _queue;
static_cast<CommandBufferAgent *>(_cmdBuff)->initAgent();

setMultithreaded(true);

Expand All @@ -96,18 +96,25 @@ void DeviceAgent::doDestroy() {
_mainMessageQueue, DeviceDestroy,
actor, _actor,
{
if (actor) {
actor->destroy();
}
actor->destroy();
});

if (_cmdBuff) {
static_cast<CommandBufferAgent *>(_cmdBuff.get())->destroyAgent();
static_cast<CommandBufferAgent *>(_cmdBuff)->destroyAgent();
static_cast<CommandBufferAgent *>(_cmdBuff)->_actor = nullptr;
delete _cmdBuff;
_cmdBuff = nullptr;
}

_queryPool = nullptr;
_queue = nullptr;
if (_queryPool) {
static_cast<QueryPoolAgent *>(_queryPool)->_actor = nullptr;
delete _queryPool;
_queryPool = nullptr;
}
if (_queue) {
static_cast<QueueAgent *>(_queue)->_actor = nullptr;
delete _queue;
_queue = nullptr;
}

if (_mainMessageQueue) {
_mainMessageQueue->terminateConsumerThread();
Expand Down Expand Up @@ -352,11 +359,11 @@ void doBufferTextureCopy(const uint8_t *const *buffers, Texture *texture, const
}

void DeviceAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) {
doBufferTextureCopy(buffers, dst, regions, count, _mainMessageQueue, _actor.get());
doBufferTextureCopy(buffers, dst, regions, count, _mainMessageQueue, _actor);
}

void CommandBufferAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *texture, const BufferTextureCopy *regions, uint32_t count) {
doBufferTextureCopy(buffers, texture, regions, count, _messageQueue, _actor.get());
doBufferTextureCopy(buffers, texture, regions, count, _messageQueue, _actor);
}

void DeviceAgent::copyTextureToBuffers(Texture *srcTexture, uint8_t *const *buffers, const BufferTextureCopy *regions, uint32_t count) {
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/FramebufferAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FramebufferAgent::~FramebufferAgent() {
FramebufferDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/InputAssemblerAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ InputAssemblerAgent::~InputAssemblerAgent() {
InputAssemblerDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/PipelineLayoutAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ PipelineLayoutAgent::~PipelineLayoutAgent() {
PipelineLayoutDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/PipelineStateAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ PipelineStateAgent::~PipelineStateAgent() {
PipelineStateDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/QueryPoolAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ QueryPoolAgent::~QueryPoolAgent() {
QueryDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/QueueAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ QueueAgent::~QueueAgent() {
QueueDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/RenderPassAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RenderPassAgent::~RenderPassAgent() {
RenderPassDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/ShaderAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ ShaderAgent::~ShaderAgent() {
ShaderDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/SwapchainAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SwapchainAgent::~SwapchainAgent() {
DeviceAgent::getInstance()->getMessageQueue(), SwapchainDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/renderer/gfx-agent/TextureAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ TextureAgent::~TextureAgent() {
TextureDestruct,
actor, _actor,
{
actor = nullptr;
CC_SAFE_DELETE(actor);
});
}
}
Expand Down
6 changes: 5 additions & 1 deletion native/cocos/renderer/gfx-base/GFXDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ bool Device::initialize(const DeviceInfo &info) {
static_assert(sizeof(void *) == 8, "pointer size assumption broken");
#endif

return doInit(info);
bool result = doInit(info);
_cmdBuff->addRef();
_queue->addRef();

return result;
}

void Device::destroy() {
Expand Down
6 changes: 3 additions & 3 deletions native/cocos/renderer/gfx-base/GFXDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class CC_DLL Device : public RefCounted {
ccstd::array<bool, static_cast<size_t>(Feature::COUNT)> _features;
ccstd::array<FormatFeature, static_cast<size_t>(Format::COUNT)> _formatFeatures;

IntrusivePtr<Queue> _queue;
IntrusivePtr<QueryPool> _queryPool;
IntrusivePtr<CommandBuffer> _cmdBuff;
Queue *_queue{nullptr};
QueryPool *_queryPool{nullptr};
CommandBuffer *_cmdBuff{nullptr};
Executable *_onAcquire{nullptr};

uint32_t _numDrawCalls{0U};
Expand Down
12 changes: 7 additions & 5 deletions native/cocos/renderer/gfx-base/GFXFramebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ class CC_DLL Framebuffer : public GFXObject, public RefCounted {
void destroy();

inline RenderPass *getRenderPass() const { return _renderPass; }
inline const TextureList &getColorTextures() const { return _colorTextures.get(); }
inline const TextureList &getColorTextures() const { return _colorTextures; }
inline Texture *getDepthStencilTexture() const { return _depthStencilTexture; }

protected:
virtual void doInit(const FramebufferInfo &info) = 0;
virtual void doDestroy() = 0;

IntrusivePtr<RenderPass> _renderPass;
// To keep compatibility, so don't use IntrusivePtr.
RefVector<Texture *> _colorTextures;
IntrusivePtr<Texture> _depthStencilTexture;
// weak reference
RenderPass *_renderPass{nullptr};
// weak reference
TextureList _colorTextures;
// weak reference
Texture *_depthStencilTexture{nullptr};
};

} // namespace gfx
Expand Down
3 changes: 1 addition & 2 deletions native/cocos/renderer/gfx-base/GFXQueryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <cstdint>
#include <mutex>
#include "GFXObject.h"
#include "base/RefCounted.h"
#include "base/std/container/unordered_map.h"

namespace cc {
Expand All @@ -47,7 +46,7 @@ namespace gfx {
* completeQueryPool
*/

class CC_DLL QueryPool : public GFXObject, public RefCounted {
class CC_DLL QueryPool : public GFXObject {
public:
QueryPool();
~QueryPool() override;
Expand Down
3 changes: 1 addition & 2 deletions native/cocos/renderer/gfx-base/GFXSwapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@

#include "GFXTexture.h"
#include "base/Ptr.h"
#include "base/RefCounted.h"
#include "gfx-base/GFXDef-common.h"

namespace cc {
namespace gfx {

class CC_DLL Swapchain : public GFXObject, public RefCounted {
class CC_DLL Swapchain : public GFXObject {
public:
Swapchain();
~Swapchain() override;
Expand Down
9 changes: 4 additions & 5 deletions native/cocos/renderer/gfx-gles2/GLES2Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ void GLES2Device::doDestroy() {
CC_ASSERT(!_memoryStatus.bufferSize); // Buffer memory leaked.
CC_ASSERT(!_memoryStatus.textureSize); // Texture memory leaked.

_queryPool = nullptr;
_queue = nullptr;
_cmdBuff = nullptr;

CC_SAFE_DESTROY_AND_DELETE(_cmdBuff)
CC_SAFE_DESTROY_AND_DELETE(_queryPool)
CC_SAFE_DESTROY_AND_DELETE(_queue)
CC_SAFE_DESTROY_AND_DELETE(_gpuContext)
}

Expand All @@ -246,7 +245,7 @@ void GLES2Device::acquire(Swapchain *const *swapchains, uint32_t count) {

void GLES2Device::present() {
CC_PROFILE(GLES2DevicePresent);
auto *queue = static_cast<GLES2Queue *>(_queue.get());
auto *queue = static_cast<GLES2Queue *>(_queue);
_numDrawCalls = queue->_numDrawCalls;
_numInstances = queue->_numInstances;
_numTriangles = queue->_numTriangles;
Expand Down
6 changes: 3 additions & 3 deletions native/cocos/renderer/gfx-gles2/GLES2Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ GLES2Framebuffer::~GLES2Framebuffer() {

void GLES2Framebuffer::doInit(const FramebufferInfo & /*info*/) {
_gpuFBO = ccnew GLES2GPUFramebuffer;
_gpuFBO->gpuRenderPass = static_cast<GLES2RenderPass *>(_renderPass.get())->gpuRenderPass();
_gpuFBO->gpuRenderPass = static_cast<GLES2RenderPass *>(_renderPass)->gpuRenderPass();

_gpuFBO->gpuColorTextures.resize(_colorTextures.size());
for (size_t i = 0; i < _colorTextures.size(); ++i) {
Expand All @@ -55,7 +55,7 @@ void GLES2Framebuffer::doInit(const FramebufferInfo & /*info*/) {
}

if (_depthStencilTexture) {
auto *depthTexture = static_cast<GLES2Texture *>(_depthStencilTexture.get());
auto *depthTexture = static_cast<GLES2Texture *>(_depthStencilTexture);
_gpuFBO->gpuDepthStencilTexture = depthTexture->gpuTexture();
_gpuFBO->lodLevel = depthTexture->getViewInfo().baseLevel;
GLES2Device::getInstance()->framebufferHub()->connect(depthTexture->gpuTexture(), _gpuFBO);
Expand All @@ -73,7 +73,7 @@ void GLES2Framebuffer::doDestroy() {
GLES2Device::getInstance()->framebufferHub()->disengage(colorTexture->gpuTexture(), _gpuFBO);
}
if (_depthStencilTexture) {
auto *depthTexture = static_cast<GLES2Texture *>(_depthStencilTexture.get());
auto *depthTexture = static_cast<GLES2Texture *>(_depthStencilTexture);
GLES2Device::getInstance()->framebufferHub()->disengage(depthTexture->gpuTexture(), _gpuFBO);
}

Expand Down
8 changes: 4 additions & 4 deletions native/cocos/renderer/gfx-gles3/GLES3Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ void GLES3Device::doDestroy() {
CC_ASSERT(!_memoryStatus.bufferSize); // Buffer memory leaked
CC_ASSERT(!_memoryStatus.textureSize); // Texture memory leaked

_cmdBuff = nullptr;
_queryPool = nullptr;
_queue = nullptr;
CC_SAFE_DESTROY_AND_DELETE(_cmdBuff)
CC_SAFE_DESTROY_AND_DELETE(_queryPool)
CC_SAFE_DESTROY_AND_DELETE(_queue)
CC_SAFE_DESTROY_AND_DELETE(_gpuContext)
}

Expand All @@ -253,7 +253,7 @@ void GLES3Device::acquire(Swapchain *const *swapchains, uint32_t count) {

void GLES3Device::present() {
CC_PROFILE(GLES3DevicePresent);
auto *queue = static_cast<GLES3Queue *>(_queue.get());
auto *queue = static_cast<GLES3Queue *>(_queue);
_numDrawCalls = queue->_numDrawCalls;
_numInstances = queue->_numInstances;
_numTriangles = queue->_numTriangles;
Expand Down
Loading

0 comments on commit 02a1eb0

Please sign in to comment.