1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/layers/composite/CanvasLayerComposite.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "CanvasLayerComposite.h" 1.10 +#include "composite/CompositableHost.h" // for CompositableHost 1.11 +#include "gfx2DGlue.h" // for ToFilter, ToMatrix4x4 1.12 +#include "GraphicsFilter.h" // for GraphicsFilter 1.13 +#include "gfxUtils.h" // for gfxUtils, etc 1.14 +#include "mozilla/gfx/Matrix.h" // for Matrix4x4 1.15 +#include "mozilla/gfx/Point.h" // for Point 1.16 +#include "mozilla/gfx/Rect.h" // for Rect 1.17 +#include "mozilla/layers/Compositor.h" // for Compositor 1.18 +#include "mozilla/layers/Effects.h" // for EffectChain 1.19 +#include "mozilla/mozalloc.h" // for operator delete 1.20 +#include "nsAString.h" 1.21 +#include "nsAutoPtr.h" // for nsRefPtr 1.22 +#include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc 1.23 +#include "nsPoint.h" // for nsIntPoint 1.24 +#include "nsString.h" // for nsAutoCString 1.25 + 1.26 +using namespace mozilla; 1.27 +using namespace mozilla::layers; 1.28 +using namespace mozilla::gfx; 1.29 + 1.30 +CanvasLayerComposite::CanvasLayerComposite(LayerManagerComposite* aManager) 1.31 + : CanvasLayer(aManager, nullptr) 1.32 + , LayerComposite(aManager) 1.33 + , mImageHost(nullptr) 1.34 +{ 1.35 + MOZ_COUNT_CTOR(CanvasLayerComposite); 1.36 + mImplData = static_cast<LayerComposite*>(this); 1.37 +} 1.38 + 1.39 +CanvasLayerComposite::~CanvasLayerComposite() 1.40 +{ 1.41 + MOZ_COUNT_DTOR(CanvasLayerComposite); 1.42 + 1.43 + CleanupResources(); 1.44 +} 1.45 + 1.46 +bool 1.47 +CanvasLayerComposite::SetCompositableHost(CompositableHost* aHost) 1.48 +{ 1.49 + switch (aHost->GetType()) { 1.50 + case BUFFER_IMAGE_SINGLE: 1.51 + case BUFFER_IMAGE_BUFFERED: 1.52 + case COMPOSITABLE_IMAGE: 1.53 + mImageHost = aHost; 1.54 + return true; 1.55 + default: 1.56 + return false; 1.57 + } 1.58 + 1.59 +} 1.60 + 1.61 +Layer* 1.62 +CanvasLayerComposite::GetLayer() 1.63 +{ 1.64 + return this; 1.65 +} 1.66 + 1.67 +LayerRenderState 1.68 +CanvasLayerComposite::GetRenderState() 1.69 +{ 1.70 + if (mDestroyed || !mImageHost || !mImageHost->IsAttached()) { 1.71 + return LayerRenderState(); 1.72 + } 1.73 + return mImageHost->GetRenderState(); 1.74 +} 1.75 + 1.76 +void 1.77 +CanvasLayerComposite::RenderLayer(const nsIntRect& aClipRect) 1.78 +{ 1.79 + if (!mImageHost || !mImageHost->IsAttached()) { 1.80 + return; 1.81 + } 1.82 + 1.83 + mCompositor->MakeCurrent(); 1.84 + 1.85 +#ifdef MOZ_DUMP_PAINTING 1.86 + if (gfxUtils::sDumpPainting) { 1.87 + RefPtr<gfx::DataSourceSurface> surf = mImageHost->GetAsSurface(); 1.88 + WriteSnapshotToDumpFile(this, surf); 1.89 + } 1.90 +#endif 1.91 + 1.92 + GraphicsFilter filter = mFilter; 1.93 +#ifdef ANDROID 1.94 + // Bug 691354 1.95 + // Using the LINEAR filter we get unexplained artifacts. 1.96 + // Use NEAREST when no scaling is required. 1.97 + Matrix matrix; 1.98 + bool is2D = GetEffectiveTransform().Is2D(&matrix); 1.99 + if (is2D && !ThebesMatrix(matrix).HasNonTranslationOrFlip()) { 1.100 + filter = GraphicsFilter::FILTER_NEAREST; 1.101 + } 1.102 +#endif 1.103 + 1.104 + EffectChain effectChain(this); 1.105 + 1.106 + LayerManagerComposite::AutoAddMaskEffect autoMaskEffect(mMaskLayer, effectChain); 1.107 + gfx::Rect clipRect(aClipRect.x, aClipRect.y, aClipRect.width, aClipRect.height); 1.108 + 1.109 + mImageHost->Composite(effectChain, 1.110 + GetEffectiveOpacity(), 1.111 + GetEffectiveTransform(), 1.112 + gfx::ToFilter(filter), 1.113 + clipRect); 1.114 + mImageHost->BumpFlashCounter(); 1.115 +} 1.116 + 1.117 +CompositableHost* 1.118 +CanvasLayerComposite::GetCompositableHost() 1.119 +{ 1.120 + if ( mImageHost && mImageHost->IsAttached()) { 1.121 + return mImageHost.get(); 1.122 + } 1.123 + 1.124 + return nullptr; 1.125 +} 1.126 + 1.127 +void 1.128 +CanvasLayerComposite::CleanupResources() 1.129 +{ 1.130 + if (mImageHost) { 1.131 + mImageHost->Detach(this); 1.132 + } 1.133 + mImageHost = nullptr; 1.134 +} 1.135 + 1.136 +nsACString& 1.137 +CanvasLayerComposite::PrintInfo(nsACString& aTo, const char* aPrefix) 1.138 +{ 1.139 + CanvasLayer::PrintInfo(aTo, aPrefix); 1.140 + aTo += "\n"; 1.141 + if (mImageHost && mImageHost->IsAttached()) { 1.142 + nsAutoCString pfx(aPrefix); 1.143 + pfx += " "; 1.144 + mImageHost->PrintInfo(aTo, pfx.get()); 1.145 + } 1.146 + return aTo; 1.147 +} 1.148 +