Skip to content

Commit

Permalink
Add the RenderTargetProxy classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
domchen committed Dec 25, 2023
1 parent aa177de commit 36625da
Show file tree
Hide file tree
Showing 28 changed files with 499 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/core/PixelBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ std::shared_ptr<Texture> PixelBuffer::onMakeTexture(Context* context, bool mipMa
return nullptr;
}
auto format = ColorTypeToPixelFormat(_info.colorType());
auto texture = Texture::MakeFormat(context, width(), height(), pixels, _info.rowBytes(), format,
ImageOrigin::TopLeft, mipMapped);
auto texture =
Texture::MakeFormat(context, width(), height(), pixels, _info.rowBytes(), format, mipMapped);
onUnlockPixels();
return texture;
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpu/PlainTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ static void ComputeScratchKey(BytesKey* scratchKey, int width, int height, Pixel

std::shared_ptr<Texture> Texture::MakeFormat(Context* context, int width, int height,
const void* pixels, size_t rowBytes,
PixelFormat pixelFormat, ImageOrigin origin,
bool mipMapped) {
PixelFormat pixelFormat, bool mipMapped,
ImageOrigin origin) {
if (!PlainTexture::CheckSizeAndFormat(context, width, height, pixelFormat)) {
return nullptr;
}
Expand Down
10 changes: 5 additions & 5 deletions src/gpu/ProxyProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
#include "gpu/proxies/DeferredTextureProxy.h"
#include "gpu/proxies/ImageBufferTextureProxy.h"
#include "gpu/proxies/ImageGeneratorTextureProxy.h"
#include "gpu/proxies/TextureRenderTargetProxy.h"

namespace tgfx {
ProxyProvider::ProxyProvider(Context* context) : context(context) {
}

std::shared_ptr<TextureProxy> ProxyProvider::findProxyByUniqueKey(const UniqueKey& uniqueKey) {
std::shared_ptr<TextureProxy> ProxyProvider::findTextureProxy(const UniqueKey& uniqueKey) {
if (uniqueKey.empty()) {
return nullptr;
}
Expand Down Expand Up @@ -80,14 +81,13 @@ std::shared_ptr<TextureProxy> ProxyProvider::createTextureProxy(
}

std::shared_ptr<TextureProxy> ProxyProvider::createTextureProxy(int width, int height,
PixelFormat format,
ImageOrigin origin,
bool mipMapped) {
PixelFormat format, bool mipMapped,
ImageOrigin origin) {
if (!PlainTexture::CheckSizeAndFormat(context, width, height, format)) {
return nullptr;
}
auto proxy = std::shared_ptr<DeferredTextureProxy>(
new DeferredTextureProxy(this, width, height, format, origin, mipMapped));
new DeferredTextureProxy(this, width, height, format, mipMapped, origin));
proxy->weakThis = proxy;
return proxy;
}
Expand Down
16 changes: 9 additions & 7 deletions src/gpu/ProxyProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#pragma once

#include "gpu/proxies/RenderTargetProxy.h"
#include "gpu/proxies/TextureProxy.h"
#include "images/ImageGeneratorTask.h"
#include "tgfx/core/ImageBuffer.h"
Expand All @@ -31,9 +32,9 @@ class ProxyProvider {
explicit ProxyProvider(Context* context);

/*
* Finds a proxy by the specified UniqueKey.
* Finds a texture proxy by the specified UniqueKey.
*/
std::shared_ptr<TextureProxy> findProxyByUniqueKey(const UniqueKey& uniqueKey);
std::shared_ptr<TextureProxy> findTextureProxy(const UniqueKey& uniqueKey);

/*
* Create a texture proxy for the image buffer. The image buffer will be released after being
Expand All @@ -44,25 +45,25 @@ class ProxyProvider {

/*
* Create a texture proxy for the ImageGeneratorTask. The task will be released after the
* associated texture being instantiated.
* associated texture is instantiated.
*/
std::shared_ptr<TextureProxy> createTextureProxy(std::shared_ptr<ImageGenerator> generator,
bool mipMapped = false,
bool disableAsyncTask = false);

/*
* Create a texture proxy for the ImageGeneratorTask. The task will be released after the
* associated texture being instantiated.
* associated texture is instantiated.
*/
std::shared_ptr<TextureProxy> createTextureProxy(std::shared_ptr<ImageGeneratorTask> task,
bool mipMapped = false);

/**
* Create a TextureProxy without any data.
* Create a TextureProxy without any pixel data.
*/
std::shared_ptr<TextureProxy> createTextureProxy(int width, int height, PixelFormat format,
ImageOrigin origin = ImageOrigin::TopLeft,
bool mipMapped = false);
bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft);

/*
* Create a texture proxy that wraps an existing texture.
Expand All @@ -78,5 +79,6 @@ class ProxyProvider {
void removeUniqueKey(TextureProxy* proxy);

friend class TextureProxy;
friend class RenderTargetProxy;
};
} // namespace tgfx
18 changes: 8 additions & 10 deletions src/gpu/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ std::shared_ptr<Surface> Surface::Make(Context* context, int width, int height,
if (!caps->isFormatRenderable(pixelFormat)) {
return nullptr;
}
auto texture =
Texture::MakeFormat(context, width, height, pixelFormat, ImageOrigin::TopLeft, mipMapped);
auto texture = Texture::MakeFormat(context, width, height, pixelFormat, mipMapped);
if (texture == nullptr) {
return nullptr;
}
Expand Down Expand Up @@ -85,9 +84,6 @@ std::shared_ptr<Surface> Surface::MakeFrom(std::shared_ptr<RenderTarget> renderT

std::shared_ptr<Surface> Surface::MakeFrom(std::shared_ptr<Texture> texture, int sampleCount,
const SurfaceOptions* options) {
if (texture == nullptr || texture->isYUV()) {
return nullptr;
}
auto renderTarget = RenderTarget::MakeFrom(texture.get(), sampleCount);
if (renderTarget == nullptr) {
return nullptr;
Expand Down Expand Up @@ -182,16 +178,17 @@ void Surface::flushAndSubmit(bool syncCpu) {
}

static std::shared_ptr<Texture> MakeTextureFromRenderTarget(const RenderTarget* renderTarget,
bool mipMapped,
bool discardContent = false) {
auto context = renderTarget->getContext();
auto width = renderTarget->width();
auto height = renderTarget->height();
if (discardContent) {
return Texture::MakeFormat(context, width, height, renderTarget->format(),
return Texture::MakeFormat(context, width, height, renderTarget->format(), mipMapped,
renderTarget->origin());
}
auto texture =
Texture::MakeFormat(context, width, height, renderTarget->format(), renderTarget->origin());
auto texture = Texture::MakeFormat(context, width, height, renderTarget->format(), mipMapped,
renderTarget->origin());
if (texture == nullptr) {
return nullptr;
}
Expand All @@ -208,7 +205,7 @@ std::shared_ptr<Image> Surface::makeImageSnapshot() {
if (texture != nullptr && !externalTexture) {
cachedImage = Image::MakeFrom(texture);
} else {
auto textureCopy = MakeTextureFromRenderTarget(renderTarget.get());
auto textureCopy = MakeTextureFromRenderTarget(renderTarget.get(), false);
cachedImage = Image::MakeFrom(textureCopy);
}
return cachedImage;
Expand All @@ -226,7 +223,8 @@ void Surface::aboutToDraw(bool discardContent) {
if (texture == nullptr || externalTexture) {
return;
}
auto newTexture = MakeTextureFromRenderTarget(renderTarget.get(), discardContent);
auto mipMapped = texture->getSampler()->hasMipmaps();
auto newTexture = MakeTextureFromRenderTarget(renderTarget.get(), mipMapped, discardContent);
auto success = renderTarget->replaceTexture(newTexture.get());
if (!success) {
LOGE("Surface::aboutToDraw(): Failed to replace the backing texture of the renderTarget!");
Expand Down
40 changes: 20 additions & 20 deletions src/gpu/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ class Texture : public Resource {
*/
static std::shared_ptr<Texture> MakeRGBA(Context* context, int width, int height,
const void* pixels, size_t rowBytes,
ImageOrigin origin = ImageOrigin::TopLeft,
bool mipMapped = false) {
return MakeFormat(context, width, height, pixels, rowBytes, PixelFormat::RGBA_8888, origin,
mipMapped);
bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft) {
return MakeFormat(context, width, height, pixels, rowBytes, PixelFormat::RGBA_8888, mipMapped,
origin);
}
/**
* Creates an empty texture with each pixel stored as 32-bit RGBA data. Returns nullptr if any of
* the parameters is invalid.
*/
static std::shared_ptr<Texture> MakeRGBA(Context* context, int width, int height,
ImageOrigin origin = ImageOrigin::TopLeft,
bool mipMapped = false) {
return MakeFormat(context, width, height, nullptr, 0, PixelFormat::RGBA_8888, origin,
mipMapped);
bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft) {
return MakeFormat(context, width, height, nullptr, 0, PixelFormat::RGBA_8888, mipMapped,
origin);
}

/**
Expand All @@ -65,20 +65,20 @@ class Texture : public Resource {
*/
static std::shared_ptr<Texture> MakeAlpha(Context* context, int width, int height,
const void* pixels, size_t rowBytes,
ImageOrigin origin = ImageOrigin::TopLeft,
bool mipMapped = false) {
return MakeFormat(context, width, height, pixels, rowBytes, PixelFormat::ALPHA_8, origin,
mipMapped);
bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft) {
return MakeFormat(context, width, height, pixels, rowBytes, PixelFormat::ALPHA_8, mipMapped,
origin);
}
/**
* Creates an empty texture with each pixel stored as a single translucency (alpha) channel.
* Returns nullptr if any of the parameters is invalid or the backend does not support creating
* alpha only textures.
*/
static std::shared_ptr<Texture> MakeAlpha(Context* context, int width, int height,
ImageOrigin origin = ImageOrigin::TopLeft,
bool mipMapped = false) {
return MakeFormat(context, width, height, nullptr, 0, PixelFormat::ALPHA_8, origin, mipMapped);
bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft) {
return MakeFormat(context, width, height, nullptr, 0, PixelFormat::ALPHA_8, mipMapped, origin);
}

/**
Expand All @@ -87,9 +87,9 @@ class Texture : public Resource {
* specified pixelFormat.
*/
static std::shared_ptr<Texture> MakeFormat(Context* context, int width, int height,
PixelFormat pixelFormat, ImageOrigin origin,
bool mipMapped = false) {
return MakeFormat(context, width, height, nullptr, 0, pixelFormat, origin, mipMapped);
PixelFormat pixelFormat, bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft) {
return MakeFormat(context, width, height, nullptr, 0, pixelFormat, mipMapped, origin);
}

/**
Expand All @@ -99,8 +99,8 @@ class Texture : public Resource {
*/
static std::shared_ptr<Texture> MakeFormat(Context* context, int width, int height,
const void* pixels, size_t rowBytes,
PixelFormat pixelFormat, ImageOrigin origin,
bool mipMapped = false);
PixelFormat pixelFormat, bool mipMapped = false,
ImageOrigin origin = ImageOrigin::TopLeft);

/**
* Creates a new Texture which wraps the specified backend texture. The caller must ensure the
Expand Down
6 changes: 3 additions & 3 deletions src/gpu/ops/DrawOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ static DstTextureInfo CreateDstTextureInfo(RenderPass* renderPass, Rect dstRect)
}
dstRect.roundOut();
dstTextureInfo.offset = {dstRect.x(), dstRect.y()};
auto dstTexture =
Texture::MakeRGBA(renderPass->context(), static_cast<int>(dstRect.width()),
static_cast<int>(dstRect.height()), renderPass->renderTarget()->origin());
auto dstTexture = Texture::MakeRGBA(renderPass->context(), static_cast<int>(dstRect.width()),
static_cast<int>(dstRect.height()), false,
renderPass->renderTarget()->origin());
if (dstTexture == nullptr) {
LOGE("Failed to create dst texture(%f*%f).", dstRect.width(), dstRect.height());
return {};
Expand Down
12 changes: 8 additions & 4 deletions src/gpu/proxies/DeferredTextureProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace tgfx {
DeferredTextureProxy::DeferredTextureProxy(ProxyProvider* provider, int width, int height,
PixelFormat format, ImageOrigin origin, bool mipMapped)
: TextureProxy(provider), _width(width), _height(height), format(format), origin(origin),
mipMapped(mipMapped) {
PixelFormat format, bool mipMapped, ImageOrigin origin)
: TextureProxy(provider), _width(width), _height(height), format(format), mipMapped(mipMapped),
_origin(origin) {
}

int DeferredTextureProxy::width() const {
Expand All @@ -33,6 +33,10 @@ int DeferredTextureProxy::height() const {
return _height;
}

ImageOrigin DeferredTextureProxy::origin() const {
return _origin;
}

bool DeferredTextureProxy::hasMipmaps() const {
return texture ? texture->getSampler()->hasMipmaps() : mipMapped;
}
Expand All @@ -41,6 +45,6 @@ std::shared_ptr<Texture> DeferredTextureProxy::onMakeTexture(Context* context) {
if (context == nullptr) {
return nullptr;
}
return Texture::MakeFormat(context, width(), height(), format, origin, mipMapped);
return Texture::MakeFormat(context, width(), height(), format, mipMapped);
}
} // namespace tgfx
6 changes: 4 additions & 2 deletions src/gpu/proxies/DeferredTextureProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DeferredTextureProxy : public TextureProxy {

int height() const override;

ImageOrigin origin() const override;

bool hasMipmaps() const override;

protected:
Expand All @@ -36,11 +38,11 @@ class DeferredTextureProxy : public TextureProxy {
int _width = 0;
int _height = 0;
PixelFormat format = PixelFormat::RGBA_8888;
ImageOrigin origin = ImageOrigin::TopLeft;
bool mipMapped = false;
ImageOrigin _origin = ImageOrigin::TopLeft;

DeferredTextureProxy(ProxyProvider* provider, int width, int height, PixelFormat format,
ImageOrigin origin, bool mipMapped);
bool mipMapped = false, ImageOrigin origin = ImageOrigin::TopLeft);

friend class ProxyProvider;
};
Expand Down
4 changes: 4 additions & 0 deletions src/gpu/proxies/ImageBufferTextureProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ int ImageBufferTextureProxy::height() const {
return texture ? texture->height() : imageBuffer->height();
}

ImageOrigin ImageBufferTextureProxy::origin() const {
return texture ? texture->origin() : ImageOrigin::TopLeft;
}

bool ImageBufferTextureProxy::hasMipmaps() const {
return texture ? texture->getSampler()->hasMipmaps() : mipMapped;
}
Expand Down
2 changes: 2 additions & 0 deletions src/gpu/proxies/ImageBufferTextureProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ImageBufferTextureProxy : public TextureProxy {

int height() const override;

ImageOrigin origin() const override;

bool hasMipmaps() const override;

protected:
Expand Down
4 changes: 4 additions & 0 deletions src/gpu/proxies/ImageGeneratorTextureProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ int ImageGeneratorTextureProxy::height() const {
return texture ? texture->height() : task->imageHeight();
}

ImageOrigin ImageGeneratorTextureProxy::origin() const {
return texture ? texture->origin() : ImageOrigin::TopLeft;
}

bool ImageGeneratorTextureProxy::hasMipmaps() const {
return texture ? texture->getSampler()->hasMipmaps() : mipMapped;
}
Expand Down
2 changes: 2 additions & 0 deletions src/gpu/proxies/ImageGeneratorTextureProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ImageGeneratorTextureProxy : public TextureProxy {

int height() const override;

ImageOrigin origin() const override;

bool hasMipmaps() const override;

protected:
Expand Down
Loading

0 comments on commit 36625da

Please sign in to comment.