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
|
|
|
|
|
|
|
|
#include "SkSGNode.h"
|
2018-08-09 11:40:01 +00:00
|
|
|
#include "SkTLazy.h"
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
class SkCanvas;
|
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
|
|
|
|
|
|
|
protected:
|
|
|
|
RenderNode();
|
|
|
|
|
2018-08-09 11:40:01 +00:00
|
|
|
virtual void onRender(SkCanvas*, const RenderContext*) const = 0;
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
float fOpacity = 1;
|
|
|
|
|
|
|
|
// Returns true if the paint was modified.
|
|
|
|
bool modulatePaint(SkPaint*) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const RenderContext* () const { return fCtx.get(); }
|
|
|
|
|
|
|
|
// Add opacity to a render node sub-DAG.
|
|
|
|
ScopedRenderContext&& modulateOpacity(float opacity);
|
|
|
|
|
|
|
|
// Force content isolation for a node sub-DAG by applying the RenderContext
|
|
|
|
// overrides via a layer.
|
|
|
|
ScopedRenderContext&& setIsolation(const SkRect& bounds, bool do_isolate);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
RenderContext* writableContext();
|
|
|
|
|
|
|
|
SkCanvas* fCanvas;
|
|
|
|
SkTCopyOnFirstWrite<RenderContext> fCtx;
|
|
|
|
int fRestoreCount;
|
|
|
|
};
|
2017-12-19 17:21:02 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef Node INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sksg
|
|
|
|
|
|
|
|
#endif // SkSGRenderNode_DEFINED
|