michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ClientLayerManager.h" // for ClientLayerManager, etc michael@0: #include "ImageContainer.h" // for AutoLockImage, etc michael@0: #include "ImageLayers.h" // for ImageLayer michael@0: #include "mozilla/Attributes.h" // for MOZ_OVERRIDE michael@0: #include "mozilla/RefPtr.h" // for RefPtr michael@0: #include "mozilla/layers/CompositorTypes.h" michael@0: #include "mozilla/layers/ImageClient.h" // for ImageClient, etc michael@0: #include "mozilla/layers/LayersMessages.h" // for ImageLayerAttributes, etc michael@0: #include "mozilla/mozalloc.h" // for operator delete, etc michael@0: #include "nsAutoPtr.h" // for nsRefPtr, getter_AddRefs, etc michael@0: #include "nsCOMPtr.h" // for already_AddRefed michael@0: #include "nsDebug.h" // for NS_ASSERTION michael@0: #include "nsISupportsImpl.h" // for Layer::AddRef, etc michael@0: #include "nsRegion.h" // for nsIntRegion michael@0: michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: class ClientImageLayer : public ImageLayer, michael@0: public ClientLayer { michael@0: public: michael@0: ClientImageLayer(ClientLayerManager* aLayerManager) michael@0: : ImageLayer(aLayerManager, michael@0: static_cast(MOZ_THIS_IN_INITIALIZER_LIST())) michael@0: , mImageClientTypeContainer(BUFFER_UNKNOWN) michael@0: { michael@0: MOZ_COUNT_CTOR(ClientImageLayer); michael@0: } michael@0: virtual ~ClientImageLayer() michael@0: { michael@0: DestroyBackBuffer(); michael@0: MOZ_COUNT_DTOR(ClientImageLayer); michael@0: } michael@0: michael@0: virtual void SetContainer(ImageContainer* aContainer) MOZ_OVERRIDE michael@0: { michael@0: ImageLayer::SetContainer(aContainer); michael@0: mImageClientTypeContainer = BUFFER_UNKNOWN; michael@0: } michael@0: michael@0: virtual void SetVisibleRegion(const nsIntRegion& aRegion) michael@0: { michael@0: NS_ASSERTION(ClientManager()->InConstruction(), michael@0: "Can only set properties in construction phase"); michael@0: ImageLayer::SetVisibleRegion(aRegion); michael@0: } michael@0: michael@0: virtual void RenderLayer(); michael@0: michael@0: virtual void ClearCachedResources() MOZ_OVERRIDE michael@0: { michael@0: DestroyBackBuffer(); michael@0: } michael@0: michael@0: virtual void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) michael@0: { michael@0: aAttrs = ImageLayerAttributes(mFilter, mScaleToSize, mScaleMode); michael@0: } michael@0: michael@0: virtual Layer* AsLayer() { return this; } michael@0: virtual ShadowableLayer* AsShadowableLayer() { return this; } michael@0: michael@0: virtual void Disconnect() michael@0: { michael@0: DestroyBackBuffer(); michael@0: ClientLayer::Disconnect(); michael@0: } michael@0: michael@0: void DestroyBackBuffer() michael@0: { michael@0: if (mImageClient) { michael@0: mImageClient->OnDetach(); michael@0: mImageClient = nullptr; michael@0: } michael@0: } michael@0: michael@0: virtual CompositableClient* GetCompositableClient() MOZ_OVERRIDE michael@0: { michael@0: return mImageClient; michael@0: } michael@0: michael@0: protected: michael@0: ClientLayerManager* ClientManager() michael@0: { michael@0: return static_cast(mManager); michael@0: } michael@0: michael@0: CompositableType GetImageClientType() michael@0: { michael@0: if (mImageClientTypeContainer != BUFFER_UNKNOWN) { michael@0: return mImageClientTypeContainer; michael@0: } michael@0: michael@0: if (mContainer->IsAsync()) { michael@0: mImageClientTypeContainer = BUFFER_BRIDGE; michael@0: return mImageClientTypeContainer; michael@0: } michael@0: michael@0: RefPtr surface; michael@0: AutoLockImage autoLock(mContainer, &surface); michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // gralloc buffer needs BUFFER_IMAGE_BUFFERED to prevent michael@0: // the buffer's usage conflict. michael@0: mImageClientTypeContainer = autoLock.GetImage() ? michael@0: BUFFER_IMAGE_BUFFERED : BUFFER_UNKNOWN; michael@0: #else michael@0: mImageClientTypeContainer = autoLock.GetImage() ? michael@0: BUFFER_IMAGE_SINGLE : BUFFER_UNKNOWN; michael@0: #endif michael@0: return mImageClientTypeContainer; michael@0: } michael@0: michael@0: RefPtr mImageClient; michael@0: CompositableType mImageClientTypeContainer; michael@0: }; michael@0: michael@0: void michael@0: ClientImageLayer::RenderLayer() michael@0: { michael@0: if (GetMaskLayer()) { michael@0: ToClientLayer(GetMaskLayer())->RenderLayer(); michael@0: } michael@0: michael@0: if (!mContainer) { michael@0: return; michael@0: } michael@0: michael@0: if (mImageClient) { michael@0: mImageClient->OnTransaction(); michael@0: } michael@0: michael@0: if (!mImageClient || michael@0: !mImageClient->UpdateImage(mContainer, GetContentFlags())) { michael@0: CompositableType type = GetImageClientType(); michael@0: if (type == BUFFER_UNKNOWN) { michael@0: return; michael@0: } michael@0: TextureFlags flags = TEXTURE_FRONT; michael@0: if (mDisallowBigImage) { michael@0: flags |= TEXTURE_DISALLOW_BIGIMAGE; michael@0: } michael@0: mImageClient = ImageClient::CreateImageClient(type, michael@0: ClientManager()->AsShadowForwarder(), michael@0: flags); michael@0: if (type == BUFFER_BRIDGE) { michael@0: static_cast(mImageClient.get())->SetLayer(this); michael@0: } michael@0: michael@0: if (!mImageClient) { michael@0: return; michael@0: } michael@0: if (HasShadow() && !mContainer->IsAsync()) { michael@0: mImageClient->Connect(); michael@0: ClientManager()->AsShadowForwarder()->Attach(mImageClient, this); michael@0: } michael@0: if (!mImageClient->UpdateImage(mContainer, GetContentFlags())) { michael@0: return; michael@0: } michael@0: } michael@0: if (mImageClient) { michael@0: mImageClient->OnTransaction(); michael@0: } michael@0: ClientManager()->Hold(this); michael@0: } michael@0: michael@0: already_AddRefed michael@0: ClientLayerManager::CreateImageLayer() michael@0: { michael@0: NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); michael@0: nsRefPtr layer = michael@0: new ClientImageLayer(this); michael@0: CREATE_SHADOW(Image); michael@0: return layer.forget(); michael@0: } michael@0: } michael@0: }