From 61808609069b69974c5278703b4255afe4c3cb53 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 11 Sep 2015 14:46:03 -0700 Subject: [PATCH 1/5] Use the CTM of the canvas to determine the dimensions of the rasterized image --- sky/compositor/checkerboard.cc | 2 +- sky/compositor/layer.cc | 4 ---- sky/compositor/layer.h | 2 -- sky/compositor/picture_layer.cc | 17 ++++++---------- sky/compositor/picture_layer.h | 2 -- sky/compositor/picture_rasterizer.cc | 29 +++++++++++++++++----------- sky/compositor/picture_rasterizer.h | 6 ++++-- sky/compositor/transform_layer.cc | 6 ------ sky/compositor/transform_layer.h | 2 -- 9 files changed, 29 insertions(+), 41 deletions(-) diff --git a/sky/compositor/checkerboard.cc b/sky/compositor/checkerboard.cc index ab7d70f39ae..42b3a245d49 100644 --- a/sky/compositor/checkerboard.cc +++ b/sky/compositor/checkerboard.cc @@ -32,7 +32,7 @@ void DrawCheckerboard(SkCanvas* canvas, int width, int height) { // Draw a checkerboard canvas->clipRect(rect); - DrawCheckerboard(canvas, 0x9900FF00, 0x00000000, 12); + DrawCheckerboard(canvas, 0x4400FF00, 0x00000000, 12); // Stroke the drawn area SkPaint debugPaint; diff --git a/sky/compositor/layer.cc b/sky/compositor/layer.cc index 85721e79d15..d76290744af 100644 --- a/sky/compositor/layer.cc +++ b/sky/compositor/layer.cc @@ -15,9 +15,5 @@ Layer::Layer() { Layer::~Layer() { } -SkMatrix Layer::model_view_matrix(const SkMatrix& model_matrix) const { - return model_matrix; -} - } // namespace compositor } // namespace sky diff --git a/sky/compositor/layer.h b/sky/compositor/layer.h index eef63a7b8f5..888346c1758 100644 --- a/sky/compositor/layer.h +++ b/sky/compositor/layer.h @@ -34,8 +34,6 @@ class Layer { virtual void Paint(PaintContext::ScopedFrame& frame) = 0; - virtual SkMatrix model_view_matrix(const SkMatrix& model_matrix) const; - ContainerLayer* parent() const { return parent_; } void set_parent(ContainerLayer* parent) { parent_ = parent; } diff --git a/sky/compositor/picture_layer.cc b/sky/compositor/picture_layer.cc index 36e06d59dd9..f19118abf34 100644 --- a/sky/compositor/picture_layer.cc +++ b/sky/compositor/picture_layer.cc @@ -14,22 +14,17 @@ PictureLayer::PictureLayer() { PictureLayer::~PictureLayer() { } -SkMatrix PictureLayer::model_view_matrix(const SkMatrix& model_matrix) const { - SkMatrix modelView = model_matrix; - modelView.postTranslate(offset_.x(), offset_.y()); - return modelView; -} - void PictureLayer::Paint(PaintContext::ScopedFrame& frame) { DCHECK(picture_); const SkRect& bounds = paint_bounds(); - SkISize size = SkISize::Make(bounds.width(), bounds.height()); - - RefPtr image = - frame.paint_context().rasterizer().GetCachedImageIfPresent( - frame.paint_context(), frame.gr_context(), picture_.get(), size); + const SkISize size = SkISize::Make(bounds.width(), bounds.height()); SkCanvas& canvas = frame.canvas(); + PictureRasterzier& rasterizer = frame.paint_context().rasterizer(); + + RefPtr image = rasterizer.GetCachedImageIfPresent( + frame.paint_context(), frame.gr_context(), picture_.get(), size, + canvas.getTotalMatrix()); if (image) { canvas.drawImage(image.get(), offset_.x(), offset_.y()); diff --git a/sky/compositor/picture_layer.h b/sky/compositor/picture_layer.h index f225e76cf6d..3f254e558aa 100644 --- a/sky/compositor/picture_layer.h +++ b/sky/compositor/picture_layer.h @@ -19,8 +19,6 @@ class PictureLayer : public Layer { void set_picture(PassRefPtr picture) { picture_ = picture; } - SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; - SkPicture* picture() const { return picture_.get(); } void Paint(PaintContext::ScopedFrame& frame) override; diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 3781018ec0f..0b629523675 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -38,15 +38,17 @@ PictureRasterzier::Value::Value() PictureRasterzier::Value::~Value() { } -RefPtr PictureRasterzier::ImageFromPicture(PaintContext& context, - GrContext* gr_context, - SkPicture* picture, - const SkISize& size) { +RefPtr PictureRasterzier::ImageFromPicture( + PaintContext& context, + GrContext* gr_context, + SkPicture* picture, + const SkISize& size, + const SkMatrix& incomingCTM) { // Step 1: Create a texture from the context's texture provider GrSurfaceDesc desc; - desc.fWidth = size.width(); - desc.fHeight = size.height(); + desc.fWidth = size.width() * incomingCTM.getScaleX(); + desc.fHeight = size.height() * incomingCTM.getScaleY(); desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fConfig = kRGBA_8888_GrPixelConfig; @@ -63,8 +65,10 @@ RefPtr PictureRasterzier::ImageFromPicture(PaintContext& context, GrBackendTextureDesc backendDesc; backendDesc.fConfig = desc.fConfig; - backendDesc.fWidth = desc.fWidth; - backendDesc.fHeight = desc.fHeight; + // Note that here, we are not using GrSurfaceDesc's dimensions as those are + // not in point space. + backendDesc.fWidth = size.width(); + backendDesc.fHeight = size.height(); backendDesc.fSampleCnt = desc.fSampleCnt; backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; backendDesc.fConfig = desc.fConfig; @@ -82,6 +86,7 @@ RefPtr PictureRasterzier::ImageFromPicture(PaintContext& context, SkCanvas* canvas = surface->getCanvas(); DCHECK(canvas); + canvas->setMatrix(incomingCTM); canvas->drawPicture(picture); if (context.options().isEnabled( @@ -106,7 +111,8 @@ RefPtr PictureRasterzier::GetCachedImageIfPresent( PaintContext& context, GrContext* gr_context, SkPicture* picture, - SkISize size) { + const SkISize& size, + const SkMatrix& incomingCTM) { if (size.isEmpty() || picture == nullptr || gr_context == nullptr) { return nullptr; } @@ -122,10 +128,11 @@ RefPtr PictureRasterzier::GetCachedImageIfPresent( value.access_count++; DCHECK(value.access_count == 1) - << "Did you forget to call purge_cache between frames?"; + << "Did you forget to call PurgeCache between frames?"; if (!value.image) { - value.image = ImageFromPicture(context, gr_context, picture, size); + value.image = + ImageFromPicture(context, gr_context, picture, size, incomingCTM); } if (value.image) { diff --git a/sky/compositor/picture_rasterizer.h b/sky/compositor/picture_rasterizer.h index a6b19bd5ebc..ddc6f8fabd6 100644 --- a/sky/compositor/picture_rasterizer.h +++ b/sky/compositor/picture_rasterizer.h @@ -28,7 +28,8 @@ class PictureRasterzier { RefPtr GetCachedImageIfPresent(PaintContext& context, GrContext* gr_context, SkPicture* picture, - SkISize size); + const SkISize& size, + const SkMatrix& incomingCTM); void PurgeCache(); @@ -80,7 +81,8 @@ class PictureRasterzier { RefPtr ImageFromPicture(PaintContext& context, GrContext* gr_context, SkPicture* picture, - const SkISize& size); + const SkISize& size, + const SkMatrix& incomingCTM); DISALLOW_COPY_AND_ASSIGN(PictureRasterzier); }; diff --git a/sky/compositor/transform_layer.cc b/sky/compositor/transform_layer.cc index 9eda297d4db..63fd4543006 100644 --- a/sky/compositor/transform_layer.cc +++ b/sky/compositor/transform_layer.cc @@ -13,12 +13,6 @@ TransformLayer::TransformLayer() { TransformLayer::~TransformLayer() { } -SkMatrix TransformLayer::model_view_matrix(const SkMatrix& model_matrix) const { - SkMatrix modelView = model_matrix; - modelView.postConcat(transform_); - return modelView; -} - void TransformLayer::Paint(PaintContext::ScopedFrame& frame) { SkCanvas& canvas = frame.canvas(); canvas.save(); diff --git a/sky/compositor/transform_layer.h b/sky/compositor/transform_layer.h index 4f2fea1577b..a2a3b2b6ef4 100644 --- a/sky/compositor/transform_layer.h +++ b/sky/compositor/transform_layer.h @@ -17,8 +17,6 @@ class TransformLayer : public ContainerLayer { void set_transform(const SkMatrix& transform) { transform_ = transform; } - SkMatrix model_view_matrix(const SkMatrix& model_matrix) const override; - void Paint(PaintContext::ScopedFrame& frame) override; private: From 4f356fa5f200039ab9f963b24ee4dbc1be2460c0 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 11 Sep 2015 14:50:46 -0700 Subject: [PATCH 2/5] Draw the debugging checkerboard pattern at the correct scale --- sky/compositor/picture_rasterizer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 0b629523675..bffff1e278a 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -91,7 +91,7 @@ RefPtr PictureRasterzier::ImageFromPicture( if (context.options().isEnabled( CompositorOptions::Option::HightlightRasterizedImages)) { - DrawCheckerboard(canvas, desc.fWidth, desc.fHeight); + DrawCheckerboard(canvas, backendDesc.fWidth, backendDesc.fHeight); } // Step 4: Create an image representation from the texture From f6aa6899b7edb7f22d4f7bf91e22bcee9200f847 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 11 Sep 2015 15:10:32 -0700 Subject: [PATCH 3/5] Use the physical size to key the images cached in the rasterizer --- sky/compositor/picture_layer.cc | 12 +++++++----- sky/compositor/picture_rasterizer.cc | 28 +++++++++++++--------------- sky/compositor/picture_rasterizer.h | 6 +++--- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/sky/compositor/picture_layer.cc b/sky/compositor/picture_layer.cc index f19118abf34..a886992ec23 100644 --- a/sky/compositor/picture_layer.cc +++ b/sky/compositor/picture_layer.cc @@ -17,14 +17,16 @@ PictureLayer::~PictureLayer() { void PictureLayer::Paint(PaintContext::ScopedFrame& frame) { DCHECK(picture_); - const SkRect& bounds = paint_bounds(); - const SkISize size = SkISize::Make(bounds.width(), bounds.height()); SkCanvas& canvas = frame.canvas(); - PictureRasterzier& rasterizer = frame.paint_context().rasterizer(); + const SkRect& bounds = paint_bounds(); + const SkMatrix& ctm = canvas.getTotalMatrix(); + const SkISize physical_size = SkISize::Make( + bounds.width() * ctm.getScaleX(), bounds.height() * ctm.getScaleY()); + PictureRasterzier& rasterizer = frame.paint_context().rasterizer(); RefPtr image = rasterizer.GetCachedImageIfPresent( - frame.paint_context(), frame.gr_context(), picture_.get(), size, - canvas.getTotalMatrix()); + frame.paint_context(), frame.gr_context(), picture_.get(), physical_size, + ctm); if (image) { canvas.drawImage(image.get(), offset_.x(), offset_.y()); diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index bffff1e278a..aed7eee72b4 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -42,13 +42,13 @@ RefPtr PictureRasterzier::ImageFromPicture( PaintContext& context, GrContext* gr_context, SkPicture* picture, - const SkISize& size, - const SkMatrix& incomingCTM) { + const SkISize& physical_size, + const SkMatrix& incoming_ctm) { // Step 1: Create a texture from the context's texture provider GrSurfaceDesc desc; - desc.fWidth = size.width() * incomingCTM.getScaleX(); - desc.fHeight = size.height() * incomingCTM.getScaleY(); + desc.fWidth = physical_size.width(); + desc.fHeight = physical_size.height(); desc.fFlags = kRenderTarget_GrSurfaceFlag; desc.fConfig = kRGBA_8888_GrPixelConfig; @@ -65,10 +65,8 @@ RefPtr PictureRasterzier::ImageFromPicture( GrBackendTextureDesc backendDesc; backendDesc.fConfig = desc.fConfig; - // Note that here, we are not using GrSurfaceDesc's dimensions as those are - // not in point space. - backendDesc.fWidth = size.width(); - backendDesc.fHeight = size.height(); + backendDesc.fWidth = physical_size.width() / incoming_ctm.getScaleX(); + backendDesc.fHeight = physical_size.height() / incoming_ctm.getScaleY(); backendDesc.fSampleCnt = desc.fSampleCnt; backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; backendDesc.fConfig = desc.fConfig; @@ -86,7 +84,7 @@ RefPtr PictureRasterzier::ImageFromPicture( SkCanvas* canvas = surface->getCanvas(); DCHECK(canvas); - canvas->setMatrix(incomingCTM); + canvas->setMatrix(incoming_ctm); canvas->drawPicture(picture); if (context.options().isEnabled( @@ -111,13 +109,13 @@ RefPtr PictureRasterzier::GetCachedImageIfPresent( PaintContext& context, GrContext* gr_context, SkPicture* picture, - const SkISize& size, - const SkMatrix& incomingCTM) { - if (size.isEmpty() || picture == nullptr || gr_context == nullptr) { + const SkISize& physical_size, + const SkMatrix& incoming_ctm) { + if (physical_size.isEmpty() || picture == nullptr || gr_context == nullptr) { return nullptr; } - const Key key(picture->uniqueID(), size); + const Key key(picture->uniqueID(), physical_size); Value& value = cache_[key]; @@ -131,8 +129,8 @@ RefPtr PictureRasterzier::GetCachedImageIfPresent( << "Did you forget to call PurgeCache between frames?"; if (!value.image) { - value.image = - ImageFromPicture(context, gr_context, picture, size, incomingCTM); + value.image = ImageFromPicture(context, gr_context, picture, physical_size, + incoming_ctm); } if (value.image) { diff --git a/sky/compositor/picture_rasterizer.h b/sky/compositor/picture_rasterizer.h index ddc6f8fabd6..9df098ae95d 100644 --- a/sky/compositor/picture_rasterizer.h +++ b/sky/compositor/picture_rasterizer.h @@ -28,8 +28,8 @@ class PictureRasterzier { RefPtr GetCachedImageIfPresent(PaintContext& context, GrContext* gr_context, SkPicture* picture, - const SkISize& size, - const SkMatrix& incomingCTM); + const SkISize& physical_size, + const SkMatrix& incoming_ctm); void PurgeCache(); @@ -81,7 +81,7 @@ class PictureRasterzier { RefPtr ImageFromPicture(PaintContext& context, GrContext* gr_context, SkPicture* picture, - const SkISize& size, + const SkISize& physical_size, const SkMatrix& incomingCTM); DISALLOW_COPY_AND_ASSIGN(PictureRasterzier); From 94a3898b53f480bb05b6932d7808c4cf00c1f772 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 11 Sep 2015 15:25:14 -0700 Subject: [PATCH 4/5] Rename rasterizer variable to be more descriptive --- sky/compositor/picture_rasterizer.cc | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index aed7eee72b4..8abf9489693 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -46,13 +46,14 @@ RefPtr PictureRasterzier::ImageFromPicture( const SkMatrix& incoming_ctm) { // Step 1: Create a texture from the context's texture provider - GrSurfaceDesc desc; - desc.fWidth = physical_size.width(); - desc.fHeight = physical_size.height(); - desc.fFlags = kRenderTarget_GrSurfaceFlag; - desc.fConfig = kRGBA_8888_GrPixelConfig; + GrSurfaceDesc surfaceDesc; + surfaceDesc.fWidth = physical_size.width(); + surfaceDesc.fHeight = physical_size.height(); + surfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag; + surfaceDesc.fConfig = kRGBA_8888_GrPixelConfig; - GrTexture* texture = gr_context->textureProvider()->createTexture(desc, true); + GrTexture* texture = + gr_context->textureProvider()->createTexture(surfaceDesc, true); if (!texture) { // The texture provider could not allocate a texture backing. Render @@ -63,14 +64,14 @@ RefPtr PictureRasterzier::ImageFromPicture( // Step 2: Create a backend render target description for the created texture - GrBackendTextureDesc backendDesc; - backendDesc.fConfig = desc.fConfig; - backendDesc.fWidth = physical_size.width() / incoming_ctm.getScaleX(); - backendDesc.fHeight = physical_size.height() / incoming_ctm.getScaleY(); - backendDesc.fSampleCnt = desc.fSampleCnt; - backendDesc.fFlags = kRenderTarget_GrBackendTextureFlag; - backendDesc.fConfig = desc.fConfig; - backendDesc.fTextureHandle = texture->getTextureHandle(); + GrBackendTextureDesc textureDesc; + textureDesc.fConfig = surfaceDesc.fConfig; + textureDesc.fWidth = physical_size.width() / incoming_ctm.getScaleX(); + textureDesc.fHeight = physical_size.height() / incoming_ctm.getScaleY(); + textureDesc.fSampleCnt = surfaceDesc.fSampleCnt; + textureDesc.fFlags = kRenderTarget_GrBackendTextureFlag; + textureDesc.fConfig = surfaceDesc.fConfig; + textureDesc.fTextureHandle = texture->getTextureHandle(); // Step 3: Render the picture into the offscreen texture @@ -89,13 +90,13 @@ RefPtr PictureRasterzier::ImageFromPicture( if (context.options().isEnabled( CompositorOptions::Option::HightlightRasterizedImages)) { - DrawCheckerboard(canvas, backendDesc.fWidth, backendDesc.fHeight); + DrawCheckerboard(canvas, textureDesc.fWidth, textureDesc.fHeight); } // Step 4: Create an image representation from the texture RefPtr image = adoptRef( - SkImage::NewFromTexture(gr_context, backendDesc, kPremul_SkAlphaType, + SkImage::NewFromTexture(gr_context, textureDesc, kPremul_SkAlphaType, &ImageReleaseProc, texture)); if (image) { From 801559471dfb6b285158cabf126b325f6e286ccf Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Fri, 11 Sep 2015 15:36:58 -0700 Subject: [PATCH 5/5] Use only the scale of the incoming CTM to render the picture into the offscreen surface --- sky/compositor/picture_rasterizer.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sky/compositor/picture_rasterizer.cc b/sky/compositor/picture_rasterizer.cc index 8abf9489693..2c7ec05dcaa 100644 --- a/sky/compositor/picture_rasterizer.cc +++ b/sky/compositor/picture_rasterizer.cc @@ -85,7 +85,8 @@ RefPtr PictureRasterzier::ImageFromPicture( SkCanvas* canvas = surface->getCanvas(); DCHECK(canvas); - canvas->setMatrix(incoming_ctm); + canvas->setMatrix( + SkMatrix::MakeScale(incoming_ctm.getScaleX(), incoming_ctm.getScaleY())); canvas->drawPicture(picture); if (context.options().isEnabled(