2017-12-19 17:21:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkSGRenderNode_DEFINED
|
|
|
|
#define SkSGRenderNode_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "modules/sksg/include/SkSGNode.h"
|
2018-08-09 15:19:14 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkBlendMode.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkShader.h"
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
2019-02-10 01:49:46 +00:00
|
|
|
class SkImageFilter;
|
2018-08-09 11:40:01 +00:00
|
|
|
class SkPaint;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
namespace sksg {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for nodes which can render to a canvas.
|
|
|
|
*/
|
|
|
|
class RenderNode : public Node {
|
2018-08-09 11:40:01 +00:00
|
|
|
protected:
|
|
|
|
struct RenderContext;
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
public:
|
|
|
|
// Render the node and its descendants to the canvas.
|
2018-08-09 11:40:01 +00:00
|
|
|
void render(SkCanvas*, const RenderContext* = nullptr) const;
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2019-02-12 14:33:21 +00:00
|
|
|
// Perform a front-to-back hit-test, and return the RenderNode located at |point|.
|
|
|
|
// Normally, hit-testing stops at leaf Draw nodes.
|
|
|
|
const RenderNode* nodeAt(const SkPoint& point) const;
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
protected:
|
2019-02-10 17:54:20 +00:00
|
|
|
explicit RenderNode(uint32_t inval_traits = 0);
|
2017-12-19 17:21:02 +00:00
|
|
|
|
2018-08-09 11:40:01 +00:00
|
|
|
virtual void onRender(SkCanvas*, const RenderContext*) const = 0;
|
2019-02-12 14:33:21 +00:00
|
|
|
virtual const RenderNode* onNodeAt(const SkPoint& p) const = 0;
|
2018-08-09 11:40:01 +00:00
|
|
|
|
|
|
|
// Paint property overrides.
|
|
|
|
// These are deferred until we can determine whether they can be applied to the individual
|
|
|
|
// draw paints, or whether they require content isolation (applied to a layer).
|
|
|
|
struct RenderContext {
|
2018-08-09 15:19:14 +00:00
|
|
|
sk_sp<SkColorFilter> fColorFilter;
|
2019-04-01 17:40:58 +00:00
|
|
|
sk_sp<SkShader> fShader;
|
2019-04-03 19:30:16 +00:00
|
|
|
SkMatrix fShaderCTM = SkMatrix::I();
|
2019-02-25 14:55:04 +00:00
|
|
|
float fOpacity = 1;
|
|
|
|
SkBlendMode fBlendMode = SkBlendMode::kSrcOver;
|
2018-08-09 11:40:01 +00:00
|
|
|
|
2019-04-04 16:12:14 +00:00
|
|
|
// Returns true if the paint overrides require a layer when applied to non-atomic draws.
|
|
|
|
bool requiresIsolation() const;
|
|
|
|
|
|
|
|
void modulatePaint(const SkMatrix& ctm, SkPaint*) const;
|
2018-08-09 11:40:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScopedRenderContext final {
|
|
|
|
public:
|
|
|
|
ScopedRenderContext(SkCanvas*, const RenderContext*);
|
|
|
|
~ScopedRenderContext();
|
|
|
|
|
|
|
|
ScopedRenderContext(ScopedRenderContext&& that) { *this = std::move(that); }
|
|
|
|
|
|
|
|
ScopedRenderContext& operator=(ScopedRenderContext&& that) {
|
|
|
|
fCanvas = that.fCanvas;
|
|
|
|
fCtx = std::move(that.fCtx);
|
|
|
|
fRestoreCount = that.fRestoreCount;
|
|
|
|
|
|
|
|
// scope ownership is being transferred
|
|
|
|
that.fRestoreCount = -1;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:50:08 +00:00
|
|
|
operator const RenderContext* () const { return &fCtx; }
|
2018-08-09 11:40:01 +00:00
|
|
|
|
2018-08-09 15:19:14 +00:00
|
|
|
// Add (cumulative) paint overrides to a render node sub-DAG.
|
2018-08-09 11:40:01 +00:00
|
|
|
ScopedRenderContext&& modulateOpacity(float opacity);
|
2018-08-09 15:19:14 +00:00
|
|
|
ScopedRenderContext&& modulateColorFilter(sk_sp<SkColorFilter>);
|
2019-04-03 19:30:16 +00:00
|
|
|
ScopedRenderContext&& modulateShader(sk_sp<SkShader>, const SkMatrix& shader_ctm);
|
2019-02-25 14:55:04 +00:00
|
|
|
ScopedRenderContext&& modulateBlendMode(SkBlendMode);
|
2018-08-09 11:40:01 +00:00
|
|
|
|
|
|
|
// Force content isolation for a node sub-DAG by applying the RenderContext
|
|
|
|
// overrides via a layer.
|
2019-04-03 19:30:16 +00:00
|
|
|
ScopedRenderContext&& setIsolation(const SkRect& bounds, const SkMatrix& ctm,
|
|
|
|
bool do_isolate);
|
2018-08-09 11:40:01 +00:00
|
|
|
|
2019-02-10 01:49:46 +00:00
|
|
|
// Similarly, force content isolation by applying the RenderContext overrides and
|
|
|
|
// an image filter via a single layer.
|
2019-04-03 19:30:16 +00:00
|
|
|
ScopedRenderContext&& setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm,
|
|
|
|
sk_sp<SkImageFilter>);
|
2019-02-10 01:49:46 +00:00
|
|
|
|
2018-08-09 11:40:01 +00:00
|
|
|
private:
|
|
|
|
// stack-only
|
|
|
|
void* operator new(size_t) = delete;
|
|
|
|
void* operator new(size_t, void*) = delete;
|
|
|
|
|
|
|
|
// Scopes cannot be copied.
|
|
|
|
ScopedRenderContext(const ScopedRenderContext&) = delete;
|
|
|
|
ScopedRenderContext& operator=(const ScopedRenderContext&) = delete;
|
|
|
|
|
2018-08-10 16:50:08 +00:00
|
|
|
SkCanvas* fCanvas;
|
|
|
|
RenderContext fCtx;
|
|
|
|
int fRestoreCount;
|
2018-08-09 11:40:01 +00:00
|
|
|
};
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
private:
|
2019-02-10 01:49:46 +00:00
|
|
|
friend class ImageFilterEffect;
|
|
|
|
|
2017-12-19 17:21:02 +00:00
|
|
|
typedef Node INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sksg
|
|
|
|
|
|
|
|
#endif // SkSGRenderNode_DEFINED
|