Skip to content

Commit

Permalink
Merge branch 'main' into feature/kevingpqi_SolidLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingpqi123 authored Oct 31, 2024
2 parents 8f09f92 + 041a344 commit 0209563
Show file tree
Hide file tree
Showing 26 changed files with 345 additions and 46 deletions.
15 changes: 15 additions & 0 deletions include/tgfx/core/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,21 @@ class Surface {
*/
bool readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX = 0, int srcY = 0);

/**
* Returns the unique ID of the Surface. The ID is unique among all Surfaces.
*/
uint32_t uniqueID() const {
return _uniqueID;
}

private:
/**
* Returns the version of the content. The version is changed when the content is changed.
* The initial version is 1;
*/
uint32_t contentVersion() const;

uint32_t _uniqueID = 0;
std::shared_ptr<RenderTargetProxy> renderTargetProxy = nullptr;
uint32_t _renderFlags = 0;
RenderContext* renderContext = nullptr;
Expand All @@ -184,5 +198,6 @@ class Surface {

friend class RenderContext;
friend class PictureImage;
friend class DisplayList;
};
} // namespace tgfx
2 changes: 2 additions & 0 deletions include/tgfx/layers/DisplayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ class DisplayList {

private:
std::shared_ptr<Layer> _root = nullptr;
uint32_t surfaceContentVersion = 0u;
uint32_t surfaceID = 0u;
};
} // namespace tgfx
13 changes: 12 additions & 1 deletion include/tgfx/layers/Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Layer {
*/
static std::shared_ptr<Layer> Make();

virtual ~Layer() = default;
virtual ~Layer();

/**
* Returns the type of the layer.
Expand Down Expand Up @@ -470,6 +470,16 @@ class Layer {
*/
virtual std::unique_ptr<LayerContent> onUpdateContent();

/**
* Attachs a property to this layer.
*/
void attachProperty(LayerProperty* property) const;

/**
* Detaches a property from this layer.
*/
void detachProperty(LayerProperty* property) const;

private:
/**
* Marks the layer's children as changed and needing to be redrawn.
Expand Down Expand Up @@ -528,5 +538,6 @@ class Layer {
} bitFields = {};

friend class DisplayList;
friend class LayerProperty;
};
} // namespace tgfx
50 changes: 50 additions & 0 deletions include/tgfx/layers/LayerProperty.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// Tencent is pleased to support the open source community by making tgfx available.
//
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// unless required by applicable law or agreed to in writing, software distributed under the
// license is distributed on an "as is" basis, without warranties or conditions of any kind,
// either express or implied. see the license for the specific language governing permissions
// and limitations under the license.
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once
#include <memory>
#include <vector>

namespace tgfx {
class Layer;

/**
* A property of a layer that may change the content of the layer.
*/
class LayerProperty {
public:
virtual ~LayerProperty() = default;

protected:
/**
* Called when the property is invalidated. This method will notify the layer that the content
* of the layer should be invalidated.
*/
void invalidate();

private:
void attachToLayer(const Layer* layer);

void detachFromLayer(const Layer* layer);

std::vector<std::weak_ptr<Layer>> owners;

friend class Layer;
};

} // namespace tgfx
16 changes: 8 additions & 8 deletions include/tgfx/layers/PathProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@
#pragma once

#include "tgfx/core/Path.h"
#include "tgfx/layers/LayerProperty.h"

namespace tgfx {
/**
* PathProvider is an interface for classes that generates a Path. It defers the acquisition of the
* Path until it is actually required, allowing the Path to be invalidated and regenerate if
* necessary. Note: PathProvider is not thread-safe and should be accessed from a single thread.
*/
class PathProvider {
class PathProvider : public LayerProperty {
public:
/**
* Creates a new PathProvider that wraps the given Path.
*/
static std::shared_ptr<PathProvider> Wrap(const Path& path);

virtual ~PathProvider() = default;

/**
* Returns the Path provided by this object.
*/
Expand All @@ -48,15 +47,16 @@ class PathProvider {
*/
explicit PathProvider(Path path);

virtual Path onGeneratePath();

/**
* Invalidates the path, causing it to be re-computed the next time it is requested.
* Marks the path as dirty and invalidates the cached path.
*/
void invalidate();

virtual Path onGeneratePath();
void invalidatePath();

private:
bool dirty = true;
Path path = {};

bool dirty = true;
};
} // namespace tgfx
2 changes: 2 additions & 0 deletions include/tgfx/layers/ShapeLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ class ShapeLayer : public Layer {
*/
void setStrokeEnd(float end);

~ShapeLayer() override;

protected:
ShapeLayer() = default;

Expand Down
6 changes: 2 additions & 4 deletions include/tgfx/layers/ShapeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
#pragma once

#include "tgfx/core/Shader.h"
#include "tgfx/layers/LayerProperty.h"

namespace tgfx {
/**
* ShapeStyle specifies the source color(s) for what is being drawn in a shape layer. There are
* three types of ShapeStyle: SolidColor, Gradient, and ImagePattern. Note: All ShapeStyle objects
* are not thread-safe and should only be accessed from a single thread.
*/
class ShapeStyle {
public:
virtual ~ShapeStyle() = default;

class ShapeStyle : public LayerProperty {
protected:
/**
* Returns the current shader that will be used to draw the shape.
Expand Down
15 changes: 7 additions & 8 deletions include/tgfx/layers/filters/LayerFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@
#pragma once

#include "tgfx/core/ImageFilter.h"
#include "tgfx/layers/LayerProperty.h"

namespace tgfx {
/**
* LayerFilter represents a filter that applies effects to a layer, such as blurs, shadows, or color
* adjustments. LayerFilters are mutable and can be changed at any time.
*/
class LayerFilter {
class LayerFilter : public LayerProperty {
public:
virtual ~LayerFilter() = default;

/**
* Returns the current image filter for the given scale factor. If the filter has not been
* created yet, it will be created and cached.
Expand All @@ -38,11 +37,6 @@ class LayerFilter {
std::shared_ptr<ImageFilter> getImageFilter(float scale);

protected:
/**
* Invalidates the filter, causing it to be re-computed the next time it is requested.
*/
void invalidate();

/**
* Creates a new image filter for the given scale factor. When it is necessary to recreate the
* ImageFilter, the onCreateImageFilter method will be called.
Expand All @@ -51,6 +45,11 @@ class LayerFilter {
*/
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

/**
* Marks the filter as dirty and invalidates the cached filter.
*/
void invalidateFilter();

private:
bool dirty = true;

Expand Down
5 changes: 5 additions & 0 deletions src/gpu/OpContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "gpu/ops/FillRectOp.h"

namespace tgfx {
static constexpr uint32_t InvalidContentVersion = 0;

void OpContext::fillWithFP(std::unique_ptr<FragmentProcessor> fp, const Matrix& uvMatrix,
bool autoResolve) {
fillRectWithFP(Rect::MakeWH(renderTargetProxy->width(), renderTargetProxy->height()),
Expand Down Expand Up @@ -48,5 +50,8 @@ void OpContext::addOp(std::unique_ptr<Op> op) {
opsTask = drawingManager->addOpsTask(renderTargetProxy);
}
opsTask->addOp(std::move(op));
do {
_contentVersion++;
} while (InvalidContentVersion == _contentVersion);
}
} // namespace tgfx
5 changes: 5 additions & 0 deletions src/gpu/OpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ class OpContext {

void addOp(std::unique_ptr<Op> op);

uint32_t contentVersion() const {
return _contentVersion;
}

private:
std::shared_ptr<RenderTargetProxy> renderTargetProxy = nullptr;
std::shared_ptr<OpsRenderTask> opsTask = nullptr;
uint32_t _contentVersion = 1u;
};
} // namespace tgfx
9 changes: 8 additions & 1 deletion src/gpu/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::shared_ptr<Surface> Surface::MakeFrom(std::shared_ptr<RenderTargetProxy> re
}

Surface::Surface(std::shared_ptr<RenderTargetProxy> proxy, uint32_t renderFlags)
: renderTargetProxy(std::move(proxy)), _renderFlags(renderFlags) {
: _uniqueID(UniqueID::Next()), renderTargetProxy(std::move(proxy)), _renderFlags(renderFlags) {
DEBUG_ASSERT(this->renderTargetProxy != nullptr);
}

Expand Down Expand Up @@ -198,6 +198,13 @@ bool Surface::readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX, in
return renderTarget->readPixels(dstInfo, dstPixels, srcX, srcY);
}

uint32_t Surface::contentVersion() const {
if (renderContext == nullptr) {
return 1u;
}
return renderContext->opContext->contentVersion();
}

bool Surface::aboutToDraw(const std::function<bool()>& willDiscardContent) {
if (cachedImage == nullptr) {
return true;
Expand Down
6 changes: 5 additions & 1 deletion src/layers/DisplayList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Layer* DisplayList::root() const {
}

void DisplayList::render(Surface* surface, bool replaceAll) {
if (surface == nullptr) {
if (!surface || (replaceAll && surface->_uniqueID == surfaceID &&
surface->contentVersion() == surfaceContentVersion && !_root->bitFields.dirty)) {
return;
}
auto canvas = surface->getCanvas();
Expand All @@ -39,5 +40,8 @@ void DisplayList::render(Surface* surface, bool replaceAll) {
}
DrawArgs args(surface->getContext(), surface->renderFlags(), true);
_root->drawLayer(args, canvas, 1.0f, BlendMode::SrcOver);
surfaceContentVersion = surface->contentVersion();
surfaceID = surface->_uniqueID;
}

} // namespace tgfx
38 changes: 38 additions & 0 deletions src/layers/Gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "tgfx/layers/Gradient.h"
#include "tgfx/layers/Layer.h"

namespace tgfx {
std::shared_ptr<LinearGradient> Gradient::MakeLinear(const Point& startPoint,
Expand All @@ -35,20 +36,57 @@ std::shared_ptr<ConicGradient> Gradient::MakeConic(const Point& center, float st

void Gradient::setColors(std::vector<Color> colors) {
_colors = std::move(colors);
invalidate();
}

void Gradient::setPositions(std::vector<float> positions) {
_positions = std::move(positions);
invalidate();
}

void LinearGradient::setEndPoint(const Point& endPoint) {
_endPoint = endPoint;
invalidate();
}

void LinearGradient::setStartPoint(const Point& startPoint) {
_startPoint = startPoint;
invalidate();
}

std::shared_ptr<Shader> LinearGradient::getShader() const {
return Shader::MakeLinearGradient(_startPoint, _endPoint, _colors, _positions);
}

void RadialGradient::setCenter(const Point& center) {
_center = center;
invalidate();
}

void RadialGradient::setRadius(float radius) {
_radius = radius;
invalidate();
}

std::shared_ptr<Shader> RadialGradient::getShader() const {
return Shader::MakeRadialGradient(_center, _radius, _colors, _positions);
}

void ConicGradient::setStartAngle(float startAngle) {
_startAngle = startAngle;
invalidate();
}

void ConicGradient::setCenter(const Point& center) {
_center = center;
invalidate();
}

void ConicGradient::setEndAngle(float endAngle) {
_endAngle = endAngle;
invalidate();
}

std::shared_ptr<Shader> ConicGradient::getShader() const {
return Shader::MakeConicGradient(_center, _startAngle, _endAngle, _colors, _positions);
}
Expand Down
Loading

0 comments on commit 0209563

Please sign in to comment.