Skip to content

Commit

Permalink
Postpone the acquisition of RenderTarget until context is flushed. (#85)
Browse files Browse the repository at this point in the history
* Postpone the acquisition of RenderTarget until context is flushed.

* Fix the build error in qt demo.
  • Loading branch information
domchen authored Dec 27, 2023
1 parent f139322 commit f6231d9
Show file tree
Hide file tree
Showing 33 changed files with 337 additions and 178 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"common": [
{
"url": "${PAG_GROUP}/vendor_tools.git",
"commit": "d288af5f03c0790e98bae413b41029e0d9270bb0",
"commit": "9787ea5507fe62bb02cb4de49cf5a87cfffbc1df",
"dir": "third_party/vendor_tools"
},
{
Expand Down
10 changes: 4 additions & 6 deletions include/tgfx/gpu/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace tgfx {
class Canvas;
class Context;
class RenderTarget;
class RenderTargetProxy;

/**
* The Surface class is responsible for managing the pixels that a Canvas draws into. The Surface
Expand Down Expand Up @@ -200,10 +201,8 @@ class Surface {
bool readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX = 0, int srcY = 0);

private:
std::shared_ptr<RenderTarget> renderTarget = nullptr;
std::shared_ptr<Texture> texture = nullptr;
std::shared_ptr<RenderTargetProxy> renderTargetProxy = nullptr;
SurfaceOptions surfaceOptions = {};
bool externalTexture = false;
Canvas* canvas = nullptr;
std::shared_ptr<Image> cachedImage = nullptr;

Expand All @@ -213,15 +212,14 @@ class Surface {
static std::shared_ptr<Surface> MakeFrom(std::shared_ptr<Texture> texture, int sampleCount = 1,
const SurfaceOptions* options = nullptr);

Surface(std::shared_ptr<RenderTarget> renderTarget, std::shared_ptr<Texture> texture,
const SurfaceOptions* options, bool externalTexture = true);
Surface(std::shared_ptr<RenderTargetProxy> proxy, const SurfaceOptions* options);

std::shared_ptr<Texture> getTexture();

void aboutToDraw(bool discardContent = false);

friend class DrawingManager;
friend class SurfaceDrawContext;
friend class Canvas;
friend class QGLWindow;
};
} // namespace tgfx
7 changes: 5 additions & 2 deletions src/core/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "gpu/processors/ConstColorProcessor.h"
#include "gpu/processors/DeviceSpaceTextureEffect.h"
#include "gpu/processors/TextureEffect.h"
#include "gpu/proxies/RenderTargetProxy.h"
#include "tgfx/core/BlendMode.h"
#include "tgfx/core/Mask.h"
#include "tgfx/core/PathEffect.h"
Expand Down Expand Up @@ -709,7 +710,9 @@ bool Canvas::drawAsClear(const Path& path, const GpuPaint& paint) {
return false;
}
surface->aboutToDraw(true);
color = surface->renderTarget->writeSwizzle().applyTo(color);
auto format = surface->renderTargetProxy->format();
const auto& writeSwizzle = getContext()->caps()->getWriteSwizzle(format);
color = writeSwizzle.applyTo(color);
auto [rect, useScissor] = getClipRect();
if (rect.has_value()) {
if (useScissor) {
Expand All @@ -731,7 +734,7 @@ void Canvas::draw(std::unique_ptr<DrawOp> op, GpuPaint paint, bool aa) {
return;
}
auto aaType = AAType::None;
if (surface->renderTarget->sampleCount() > 1) {
if (surface->renderTargetProxy->sampleCount() > 1) {
aaType = AAType::MSAA;
} else if (aa && !IsPixelAligned(op->bounds())) {
aaType = AAType::Coverage;
Expand Down
25 changes: 13 additions & 12 deletions src/gpu/DrawingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "DrawingManager.h"
#include "TextureResolveRenderTask.h"
#include "gpu/Gpu.h"
#include "gpu/RenderTarget.h"
#include "gpu/proxies/RenderTargetProxy.h"
#include "gpu/proxies/TextureProxy.h"
#include "gpu/tasks/TextureResolveRenderTask.h"
#include "utils/Log.h"

namespace tgfx {
Expand All @@ -31,21 +31,22 @@ void DrawingManager::closeActiveOpsTask() {
}
}

void DrawingManager::newTextureResolveRenderTask(Surface* surface) {
auto texture = surface->texture;
if (surface->renderTarget->sampleCount() <= 1 &&
(!texture || !texture->getSampler()->hasMipmaps())) {
void DrawingManager::newTextureResolveRenderTask(
std::shared_ptr<RenderTargetProxy> renderTargetProxy) {
auto textureProxy = renderTargetProxy->getTextureProxy();
if (renderTargetProxy->sampleCount() <= 1 && (!textureProxy || !textureProxy->hasMipmaps())) {
return;
}
closeActiveOpsTask();
auto task = std::make_shared<TextureResolveRenderTask>(surface->renderTarget, surface->texture);
auto task = std::make_shared<TextureResolveRenderTask>(renderTargetProxy);
task->makeClosed();
tasks.push_back(std::move(task));
}

std::shared_ptr<OpsTask> DrawingManager::newOpsTask(Surface* surface) {
std::shared_ptr<OpsTask> DrawingManager::newOpsTask(
std::shared_ptr<RenderTargetProxy> renderTargetProxy) {
closeActiveOpsTask();
auto opsTask = std::make_shared<OpsTask>(surface->renderTarget, surface->texture);
auto opsTask = std::make_shared<OpsTask>(renderTargetProxy);
tasks.push_back(opsTask);
activeOpsTask = opsTask.get();
return opsTask;
Expand All @@ -55,12 +56,12 @@ bool DrawingManager::flush(Semaphore* signalSemaphore) {
auto* gpu = context->gpu();
closeAllTasks();
activeOpsTask = nullptr;
std::vector<TextureProxy*> proxies;
std::vector<ProxyBase*> proxies = {};
std::for_each(tasks.begin(), tasks.end(),
[&proxies](std::shared_ptr<RenderTask>& task) { task->gatherProxies(&proxies); });
std::for_each(proxies.begin(), proxies.end(), [](TextureProxy* proxy) {
std::for_each(proxies.begin(), proxies.end(), [](ProxyBase* proxy) {
if (!(proxy->isInstantiated() || proxy->instantiate())) {
LOGE("Failed to instantiate texture from proxy.");
LOGE("DrawingManager::flush() Failed to instantiate proxy!");
}
});
std::for_each(tasks.begin(), tasks.end(),
Expand Down
10 changes: 6 additions & 4 deletions src/gpu/DrawingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pragma once

#include <vector>
#include "RenderTask.h"
#include "gpu/ops/OpsTask.h"
#include "gpu/tasks/OpsTask.h"
#include "gpu/tasks/RenderTask.h"
#include "tgfx/gpu/Surface.h"

namespace tgfx {
Expand All @@ -29,11 +29,11 @@ class DrawingManager {
explicit DrawingManager(Context* context) : context(context) {
}

std::shared_ptr<OpsTask> newOpsTask(Surface* surface);
std::shared_ptr<OpsTask> newOpsTask(std::shared_ptr<RenderTargetProxy> renderTargetProxy);

bool flush(Semaphore* signalSemaphore);

void newTextureResolveRenderTask(Surface* surface);
void newTextureResolveRenderTask(std::shared_ptr<RenderTargetProxy> renderTargetProxy);

private:
void closeAllTasks();
Expand All @@ -45,5 +45,7 @@ class DrawingManager {
Context* context = nullptr;
std::vector<std::shared_ptr<RenderTask>> tasks;
OpsTask* activeOpsTask = nullptr;

friend class Surface;
};
} // namespace tgfx
5 changes: 0 additions & 5 deletions src/gpu/RenderTarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,9 @@ class RenderTarget : public Resource {
}

private:
virtual const Swizzle& writeSwizzle() const = 0;

int _width = 0;
int _height = 0;
ImageOrigin _origin = ImageOrigin::TopLeft;
int _sampleCount = 1;

friend class DrawOp;
friend class Canvas;
};
} // namespace tgfx
Loading

0 comments on commit f6231d9

Please sign in to comment.