Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "SimpleTextureClientPool.h" |
michael@0 | 7 | #include "CompositableClient.h" |
michael@0 | 8 | #include "mozilla/layers/ISurfaceAllocator.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "gfxPrefs.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsComponentManagerUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | #if 0 |
michael@0 | 15 | #define RECYCLE_LOG(...) printf_stderr(__VA_ARGS__) |
michael@0 | 16 | #else |
michael@0 | 17 | #define RECYCLE_LOG(...) do { } while (0) |
michael@0 | 18 | #endif |
michael@0 | 19 | |
michael@0 | 20 | namespace mozilla { |
michael@0 | 21 | namespace layers { |
michael@0 | 22 | |
michael@0 | 23 | using gfx::SurfaceFormat; |
michael@0 | 24 | |
michael@0 | 25 | /* static */ void |
michael@0 | 26 | SimpleTextureClientPool::ShrinkCallback(nsITimer *aTimer, void *aClosure) |
michael@0 | 27 | { |
michael@0 | 28 | static_cast<SimpleTextureClientPool*>(aClosure)->ShrinkToMinimumSize(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | /* static */ void |
michael@0 | 32 | SimpleTextureClientPool::RecycleCallback(TextureClient* aClient, void* aClosure) |
michael@0 | 33 | { |
michael@0 | 34 | SimpleTextureClientPool* pool = |
michael@0 | 35 | static_cast<SimpleTextureClientPool*>(aClosure); |
michael@0 | 36 | |
michael@0 | 37 | aClient->ClearRecycleCallback(); |
michael@0 | 38 | pool->ReturnTextureClient(aClient); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | /* static */ void |
michael@0 | 42 | SimpleTextureClientPool::WaitForCompositorRecycleCallback(TextureClient* aClient, void* aClosure) |
michael@0 | 43 | { |
michael@0 | 44 | // This will grab a reference that will be released once the compositor |
michael@0 | 45 | // acknowledges the remote recycle. Once it is received the object |
michael@0 | 46 | // will be fully recycled. |
michael@0 | 47 | aClient->WaitForCompositorRecycle(); |
michael@0 | 48 | aClient->SetRecycleCallback(SimpleTextureClientPool::RecycleCallback, aClosure); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | SimpleTextureClientPool::SimpleTextureClientPool(gfx::SurfaceFormat aFormat, gfx::IntSize aSize, |
michael@0 | 52 | ISurfaceAllocator *aAllocator) |
michael@0 | 53 | : mFormat(aFormat) |
michael@0 | 54 | , mSize(aSize) |
michael@0 | 55 | , mSurfaceAllocator(aAllocator) |
michael@0 | 56 | { |
michael@0 | 57 | mTimer = do_CreateInstance("@mozilla.org/timer;1"); |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | TemporaryRef<TextureClient> |
michael@0 | 61 | SimpleTextureClientPool::GetTextureClient(bool aAutoRecycle) |
michael@0 | 62 | { |
michael@0 | 63 | // Try to fetch a client from the pool |
michael@0 | 64 | RefPtr<TextureClient> textureClient; |
michael@0 | 65 | if (mAvailableTextureClients.size()) { |
michael@0 | 66 | textureClient = mAvailableTextureClients.top(); |
michael@0 | 67 | textureClient->WaitReleaseFence(); |
michael@0 | 68 | mAvailableTextureClients.pop(); |
michael@0 | 69 | RECYCLE_LOG("%s Skip allocate (%i left), returning %p\n", (mFormat == SurfaceFormat::B8G8R8A8?"poolA":"poolX"), mAvailableTextureClients.size(), textureClient.get()); |
michael@0 | 70 | |
michael@0 | 71 | } else { |
michael@0 | 72 | // No unused clients in the pool, create one |
michael@0 | 73 | if (gfxPrefs::ForceShmemTiles()) { |
michael@0 | 74 | textureClient = TextureClient::CreateBufferTextureClient(mSurfaceAllocator, |
michael@0 | 75 | mFormat, TEXTURE_IMMEDIATE_UPLOAD | TEXTURE_RECYCLE, gfx::BackendType::NONE); |
michael@0 | 76 | } else { |
michael@0 | 77 | textureClient = TextureClient::CreateTextureClientForDrawing(mSurfaceAllocator, |
michael@0 | 78 | mFormat, TEXTURE_FLAGS_DEFAULT | TEXTURE_RECYCLE, gfx::BackendType::NONE, mSize); |
michael@0 | 79 | } |
michael@0 | 80 | if (!textureClient->AllocateForSurface(mSize, ALLOC_DEFAULT)) { |
michael@0 | 81 | NS_WARNING("TextureClient::AllocateForSurface failed!"); |
michael@0 | 82 | } |
michael@0 | 83 | RECYCLE_LOG("%s Must allocate (0 left), returning %p\n", (mFormat == SurfaceFormat::B8G8R8A8?"poolA":"poolX"), textureClient.get()); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | if (aAutoRecycle) { |
michael@0 | 87 | mOutstandingTextureClients.push_back(textureClient); |
michael@0 | 88 | textureClient->SetRecycleCallback(SimpleTextureClientPool::WaitForCompositorRecycleCallback, this); |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | return textureClient; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void |
michael@0 | 95 | SimpleTextureClientPool::ReturnTextureClient(TextureClient *aClient) |
michael@0 | 96 | { |
michael@0 | 97 | if (!aClient) { |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | // If we haven't hit our max cached client limit, add this one |
michael@0 | 102 | if (mAvailableTextureClients.size() < sMaxTextureClients) { |
michael@0 | 103 | mAvailableTextureClients.push(aClient); |
michael@0 | 104 | RECYCLE_LOG("%s recycled %p (have %d)\n", (mFormat == SurfaceFormat::B8G8R8A8?"poolA":"poolX"), aClient, mAvailableTextureClients.size()); |
michael@0 | 105 | } else { |
michael@0 | 106 | RECYCLE_LOG("%s did not recycle %p (have %d)\n", (mFormat == SurfaceFormat::B8G8R8A8?"poolA":"poolX"), aClient, mAvailableTextureClients.size()); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // Kick off the pool shrinking timer if there are still more unused texture |
michael@0 | 110 | // clients than our desired minimum cache size. |
michael@0 | 111 | if (mAvailableTextureClients.size() > sMinCacheSize) { |
michael@0 | 112 | mTimer->InitWithFuncCallback(SimpleTextureClientPool::ShrinkCallback, this, sShrinkTimeout, |
michael@0 | 113 | nsITimer::TYPE_ONE_SHOT); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | mOutstandingTextureClients.remove(aClient); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | void |
michael@0 | 120 | SimpleTextureClientPool::ShrinkToMinimumSize() |
michael@0 | 121 | { |
michael@0 | 122 | RECYCLE_LOG("%s ShrinkToMinimumSize, removing %d clients", (mFormat == SurfaceFormat::B8G8R8A8?"poolA":"poolX"), mAvailableTextureClients.size() > sMinCacheSize ? mAvailableTextureClients.size() - sMinCacheSize : 0); |
michael@0 | 123 | |
michael@0 | 124 | mTimer->Cancel(); |
michael@0 | 125 | |
michael@0 | 126 | while (mAvailableTextureClients.size() > sMinCacheSize) { |
michael@0 | 127 | mAvailableTextureClients.pop(); |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void |
michael@0 | 132 | SimpleTextureClientPool::Clear() |
michael@0 | 133 | { |
michael@0 | 134 | while (!mAvailableTextureClients.empty()) { |
michael@0 | 135 | mAvailableTextureClients.pop(); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | } |
michael@0 | 140 | } |