提交 804f0c7b 编写于 作者: C Chinmay Garde

The gr_context and canvas are stored at the frame level instead of the storing...

The gr_context and canvas are stored at the frame level instead of the storing the same temporarily in the context
上级 d2bd4cba
......@@ -13,11 +13,11 @@ ClipPathLayer::ClipPathLayer() {
ClipPathLayer::~ClipPathLayer() {
}
void ClipPathLayer::Paint(PaintContext& context) {
SkCanvas& canvas = context.canvas();
void ClipPathLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
canvas.saveLayer(&clip_path_.getBounds(), nullptr);
canvas.clipPath(clip_path_);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -18,7 +18,7 @@ class ClipPathLayer : public ContainerLayer {
void set_clip_path(const SkPath& clip_path) { clip_path_ = clip_path; }
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkPath clip_path_;
......
......@@ -13,11 +13,11 @@ ClipRectLayer::ClipRectLayer() {
ClipRectLayer::~ClipRectLayer() {
}
void ClipRectLayer::Paint(PaintContext& context) {
SkCanvas& canvas = context.canvas();
void ClipRectLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
canvas.save();
canvas.clipRect(clip_rect_);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -18,7 +18,7 @@ class ClipRectLayer : public ContainerLayer {
void set_clip_rect(const SkRect& clip_rect) { clip_rect_ = clip_rect; }
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkRect clip_rect_;
......
......@@ -13,11 +13,11 @@ ClipRRectLayer::ClipRRectLayer() {
ClipRRectLayer::~ClipRRectLayer() {
}
void ClipRRectLayer::Paint(PaintContext& context) {
SkCanvas& canvas = context.canvas();
void ClipRRectLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
canvas.saveLayer(&clip_rrect_.getBounds(), nullptr);
canvas.clipRRect(clip_rrect_);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -17,8 +17,7 @@ class ClipRRectLayer : public ContainerLayer {
void set_clip_rrect(const SkRRect& clip_rrect) { clip_rrect_ = clip_rrect; }
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkRRect clip_rrect_;
......
......@@ -13,14 +13,14 @@ ColorFilterLayer::ColorFilterLayer() {
ColorFilterLayer::~ColorFilterLayer() {
}
void ColorFilterLayer::Paint(PaintContext& context) {
void ColorFilterLayer::Paint(PaintContext::ScopedFrame& frame) {
RefPtr<SkColorFilter> color_filter =
adoptRef(SkColorFilter::CreateModeFilter(color_, transfer_mode_));
SkPaint paint;
paint.setColorFilter(color_filter.get());
SkCanvas& canvas = context.canvas();
SkCanvas& canvas = frame.canvas();
canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -21,8 +21,7 @@ class ColorFilterLayer : public ContainerLayer {
transfer_mode_ = transfer_mode;
}
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkColor color_;
......
......@@ -18,9 +18,9 @@ void ContainerLayer::Add(std::unique_ptr<Layer> layer) {
layers_.push_back(std::move(layer));
}
void ContainerLayer::PaintChildren(PaintContext& context) const {
void ContainerLayer::PaintChildren(PaintContext::ScopedFrame& frame) const {
for (auto& layer : layers_)
layer->Paint(context);
layer->Paint(frame);
}
} // namespace compositor
......
......@@ -17,7 +17,7 @@ class ContainerLayer : public Layer {
void Add(std::unique_ptr<Layer> layer);
void PaintChildren(PaintContext& context) const;
void PaintChildren(PaintContext::ScopedFrame& frame) const;
const std::vector<std::unique_ptr<Layer>>& layers() const { return layers_; }
......
......@@ -32,7 +32,7 @@ class Layer {
Layer();
virtual ~Layer();
void Paint(PaintContext::ScopedFrame& frame) { Paint(frame.paint_context()); }
virtual void Paint(PaintContext::ScopedFrame& frame) = 0;
virtual SkMatrix model_view_matrix(const SkMatrix& model_matrix) const;
......@@ -46,15 +46,10 @@ class Layer {
paint_bounds_ = paint_bounds;
}
protected:
virtual void Paint(PaintContext& context) = 0;
private:
ContainerLayer* parent_;
SkRect paint_bounds_;
friend class ContainerLayer;
DISALLOW_COPY_AND_ASSIGN(Layer);
};
......
......@@ -13,15 +13,15 @@ OpacityLayer::OpacityLayer() {
OpacityLayer::~OpacityLayer() {
}
void OpacityLayer::Paint(PaintContext& context) {
void OpacityLayer::Paint(PaintContext::ScopedFrame& frame) {
SkColor color = SkColorSetARGB(alpha_, 0, 0, 0);
RefPtr<SkColorFilter> colorFilter = adoptRef(
SkColorFilter::CreateModeFilter(color, SkXfermode::kSrcOver_Mode));
SkPaint paint;
paint.setColorFilter(colorFilter.get());
SkCanvas& canvas = context.canvas();
SkCanvas& canvas = frame.canvas();
canvas.saveLayer(&paint_bounds(), &paint);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -18,7 +18,7 @@ class OpacityLayer : public ContainerLayer {
void set_alpha(int alpha) { alpha_ = alpha; }
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
int alpha_;
......
......@@ -11,20 +11,11 @@ namespace compositor {
PaintContext::PaintContext() {
}
void PaintContext::beginFrame(SkCanvas& canvas, GrContext* gr_context) {
canvas_ = &canvas; // required
gr_context_ = gr_context; // optional
DCHECK(canvas_);
void PaintContext::beginFrame() {
}
void PaintContext::endFrame() {
DCHECK(canvas_);
rasterizer_.PurgeCache();
canvas_ = nullptr;
gr_context_ = nullptr;
}
PaintContext::ScopedFrame PaintContext::AcquireFrame(SkCanvas& canvas,
......
......@@ -19,18 +19,25 @@ class PaintContext {
public:
PaintContext& paint_context() { return context_; };
GrContext* gr_context() { return gr_context_; }
SkCanvas& canvas() { return canvas_; }
ScopedFrame(ScopedFrame&& frame) = default;
~ScopedFrame() { context_.endFrame(); }
private:
PaintContext& context_;
SkCanvas& canvas_;
GrContext* gr_context_;
ScopedFrame() = delete;
ScopedFrame(PaintContext& context, SkCanvas& canvas, GrContext* gr_context)
: context_(context) {
context_.beginFrame(canvas, gr_context);
: context_(context), canvas_(canvas), gr_context_(gr_context) {
DCHECK(&canvas) << "The frame requries a valid canvas";
context_.beginFrame();
};
friend class PaintContext;
......@@ -45,24 +52,13 @@ class PaintContext {
CompositorOptions& options() { return options_; };
GrContext* gr_context() { return gr_context_; }
SkCanvas& canvas() {
DCHECK(canvas_) << "Tried to access the canvas of a context whose frame "
"was not initialized. Did you forget to "
"`AcquireFrame`?";
return *canvas_;
}
ScopedFrame AcquireFrame(SkCanvas& canvas, GrContext* gr_context);
private:
PictureRasterzier rasterizer_;
CompositorOptions options_;
GrContext* gr_context_;
SkCanvas* canvas_;
void beginFrame(SkCanvas& canvas, GrContext* context);
void beginFrame();
void endFrame();
......
......@@ -20,15 +20,16 @@ SkMatrix PictureLayer::model_view_matrix(const SkMatrix& model_matrix) const {
return modelView;
}
void PictureLayer::Paint(PaintContext& context) {
void PictureLayer::Paint(PaintContext::ScopedFrame& frame) {
DCHECK(picture_);
const SkRect& bounds = paint_bounds();
SkISize size = SkISize::Make(bounds.width(), bounds.height());
RefPtr<SkImage> image = context.rasterizer().GetCachedImageIfPresent(
context, picture_.get(), size);
SkCanvas& canvas = context.canvas();
RefPtr<SkImage> image =
frame.paint_context().rasterizer().GetCachedImageIfPresent(
frame.paint_context(), frame.gr_context(), picture_.get(), size);
SkCanvas& canvas = frame.canvas();
if (image) {
canvas.drawImage(image.get(), offset_.x(), offset_.y());
......
......@@ -23,8 +23,7 @@ class PictureLayer : public Layer {
SkPicture* picture() const { return picture_.get(); }
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkPoint offset_;
......
......@@ -39,6 +39,7 @@ PictureRasterzier::Value::~Value() {
}
static RefPtr<SkImage> ImageFromPicture(PaintContext& context,
GrContext* gr_context,
SkPicture* picture,
const SkISize& size) {
// Step 1: Create a texture from the context's texture provider
......@@ -49,8 +50,7 @@ static RefPtr<SkImage> ImageFromPicture(PaintContext& context,
desc.fFlags = kRenderTarget_GrSurfaceFlag;
desc.fConfig = kRGBA_8888_GrPixelConfig;
GrTexture* texture =
context.gr_context()->textureProvider()->createTexture(desc, true);
GrTexture* texture = gr_context->textureProvider()->createTexture(desc, true);
if (!texture) {
// The texture provider could not allocate a texture backing. Render
......@@ -92,16 +92,17 @@ static RefPtr<SkImage> ImageFromPicture(PaintContext& context,
// Step 4: Create an image representation from the texture
RefPtr<SkImage> image = adoptRef(
SkImage::NewFromTexture(context.gr_context(), backendDesc,
kPremul_SkAlphaType, &ImageReleaseProc, texture));
SkImage::NewFromTexture(gr_context, backendDesc, kPremul_SkAlphaType,
&ImageReleaseProc, texture));
return image;
}
RefPtr<SkImage> PictureRasterzier::GetCachedImageIfPresent(
PaintContext& context,
GrContext* gr_context,
SkPicture* picture,
SkISize size) {
if (size.isEmpty() || picture == nullptr || context.gr_context() == nullptr) {
if (size.isEmpty() || picture == nullptr || gr_context == nullptr) {
return nullptr;
}
......@@ -119,7 +120,7 @@ RefPtr<SkImage> PictureRasterzier::GetCachedImageIfPresent(
<< "Did you forget to call purge_cache between frames?";
if (!value.image) {
value.image = ImageFromPicture(context, picture, size);
value.image = ImageFromPicture(context, gr_context, picture, size);
}
return value.image;
......
......@@ -25,6 +25,7 @@ class PictureRasterzier {
~PictureRasterzier();
RefPtr<SkImage> GetCachedImageIfPresent(PaintContext& context,
GrContext* gr_context,
SkPicture* picture,
SkISize size);
......
......@@ -19,11 +19,11 @@ SkMatrix TransformLayer::model_view_matrix(const SkMatrix& model_matrix) const {
return modelView;
}
void TransformLayer::Paint(PaintContext& context) {
SkCanvas& canvas = context.canvas();
void TransformLayer::Paint(PaintContext::ScopedFrame& frame) {
SkCanvas& canvas = frame.canvas();
canvas.save();
canvas.concat(transform_);
PaintChildren(context);
PaintChildren(frame);
canvas.restore();
}
......
......@@ -19,8 +19,7 @@ class TransformLayer : public ContainerLayer {
SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override;
protected:
void Paint(PaintContext& context) override;
void Paint(PaintContext::ScopedFrame& frame) override;
private:
SkMatrix transform_;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册