Skip to content

Commit

Permalink
Add LayerProperty class to manage layer properties and notify updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hparty committed Oct 30, 2024
1 parent d26869a commit f3102cd
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 130 deletions.
9 changes: 5 additions & 4 deletions include/tgfx/core/Surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ class Surface {
*/
bool readPixels(const ImageInfo& dstInfo, void* dstPixels, int srcX = 0, int srcY = 0);

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

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

friend class RenderContext;
friend class PictureImage;
friend class DisplayList;
};
} // namespace tgfx
14 changes: 11 additions & 3 deletions include/tgfx/layers/Layer.h
Original file line number Diff line number Diff line change
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,8 +538,6 @@ class Layer {
} bitFields = {};

friend class DisplayList;
friend class LayerFilter;
friend class ShapeStyle;
friend class PathProvider;
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 <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:
/**
* Mark the property as dirty and notify the layer that the content needs to be updated.
*/
void invalidate();

bool dirty = true;

private:
void attachToLayer(const Layer* layer);

void detachFromLayer(const Layer* layer);

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

friend class Layer;
};

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

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

namespace tgfx {
class Layer;
/**
* 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 @@ -49,20 +47,9 @@ class PathProvider {
*/
explicit PathProvider(Path path);

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

virtual Path onGeneratePath();

private:
void attachToLayer(const Layer* layer);
void detachFromLayer(const Layer* layer);
bool dirty = true;
Path path = {};

std::vector<std::weak_ptr<Layer>> owners;
friend class Layer;
};
} // namespace tgfx
15 changes: 2 additions & 13 deletions include/tgfx/layers/ShapeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,21 @@
#pragma once

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

namespace tgfx {
class Layer;
/**
* 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.
*/
virtual std::shared_ptr<Shader> getShader() const = 0;

void invalidate();

void attachToLayer(const Layer* layer);

void detachFromLayer(const Layer* layer);

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

friend class ShapeLayer;
};
} // namespace tgfx
19 changes: 2 additions & 17 deletions include/tgfx/layers/filters/LayerFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
#pragma once

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

namespace tgfx {
class Layer;
/**
* 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 @@ -39,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 @@ -53,20 +46,12 @@ class LayerFilter {
virtual std::shared_ptr<ImageFilter> onCreateImageFilter(float scale) = 0;

private:
void attachToLayer(const Layer* layer);

void detachFromLayer(const Layer* layer);

bool dirty = true;

float lastScale = 1.0f;

std::unique_ptr<Rect> _clipBounds = nullptr;

std::shared_ptr<ImageFilter> lastFilter;

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

friend class Layer;
};
} // namespace tgfx
2 changes: 1 addition & 1 deletion src/gpu/OpContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void OpContext::addOp(std::unique_ptr<Op> op) {
if (opsTask == nullptr || opsTask->isClosed()) {
auto drawingManager = renderTargetProxy->getContext()->drawingManager();
opsTask = drawingManager->addOpsTask(renderTargetProxy);
_contentVersion++;
}
opsTask->addOp(std::move(op));
_contentVersion++;
}
} // namespace tgfx
2 changes: 1 addition & 1 deletion src/layers/Gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////

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

namespace tgfx {
std::shared_ptr<LinearGradient> Gradient::MakeLinear(const Point& startPoint,
Expand Down
14 changes: 13 additions & 1 deletion src/layers/Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std::shared_ptr<Layer> Layer::Make() {
}

Layer::~Layer() {
for (auto filter : _filters) {
for (const auto& filter : _filters) {
filter->detachFromLayer(this);
}
}
Expand Down Expand Up @@ -394,6 +394,18 @@ std::unique_ptr<LayerContent> Layer::onUpdateContent() {
return nullptr;
}

void Layer::attachProperty(LayerProperty* property) const {
if (property) {
property->attachToLayer(this);
}
}

void Layer::detachProperty(LayerProperty* property) const {
if (property) {
property->detachFromLayer(this);
}
}

void Layer::onAttachToRoot(Layer* owner) {
_root = owner;
for (auto& child : _children) {
Expand Down
20 changes: 10 additions & 10 deletions src/layers/ShapeStyle.cpp → src/layers/LayerProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@
//
/////////////////////////////////////////////////////////////////////////////////////////////////

#include "tgfx/layers/ShapeStyle.h"
#include "tgfx/layers/LayerProperty.h"
#include "tgfx/layers/Layer.h"

namespace tgfx {
void ShapeStyle::invalidate() {

void LayerProperty::invalidate() {
dirty = true;
for (auto& owner : owners) {
auto layer = owner.lock();
if (layer) {
if (auto layer = owner.lock()) {
layer->invalidateContent();
}
}
}

void ShapeStyle::attachToLayer(const Layer* layer) {
void LayerProperty::attachToLayer(const Layer* layer) {
owners.push_back(layer->weakThis);
}

void ShapeStyle::detachFromLayer(const Layer* layer) {
for (auto it = owners.begin(); it != owners.end(); ++it) {
if (it->lock().get() == layer) {
owners.erase(it);
void LayerProperty::detachFromLayer(const Layer* layer) {
for (auto owner = owners.begin(); owner != owners.end(); ++owner) {
if (owner->lock().get() == layer) {
owners.erase(owner);
break;
}
}
Expand Down
24 changes: 0 additions & 24 deletions src/layers/PathProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,7 @@ Path PathProvider::getPath() {
return path;
}

void PathProvider::invalidate() {
dirty = true;
for (auto& owner : owners) {
auto layer = owner.lock();
if (layer) {
layer->invalidateContent();
}
}
}

Path PathProvider::onGeneratePath() {
return path;
}

void PathProvider::attachToLayer(const Layer* layer) {
owners.push_back(layer->weakThis);
}

void PathProvider::detachFromLayer(const Layer* layer) {
for (auto it = owners.begin(); it != owners.end(); ++it) {
if (it->lock().get() == layer) {
owners.erase(it);
break;
}
}
}

} // namespace tgfx
Loading

0 comments on commit f3102cd

Please sign in to comment.