Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 920160 - Add prefs for SkiaGL cache size r=snorp
Browse files Browse the repository at this point in the history
  • Loading branch information
George Wright committed Oct 22, 2013
1 parent f3f8dc7 commit 2f8b208
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 21 deletions.
3 changes: 3 additions & 0 deletions b2g/app/b2g.js
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,9 @@ pref("devtools.debugger.unix-domain-socket", "/data/local/debugger-socket");
pref("gfx.canvas.azure.backends", "skia");
pref("gfx.canvas.azure.accelerated", true);

// Turn on dynamic cache size for Skia
pref("gfx.canvas.skiagl.dynamic-cache", true);

// Enable Telephony API
pref("dom.telephony.enabled", true);

Expand Down
3 changes: 3 additions & 0 deletions gfx/2d/2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,9 @@ class GFX2D_API Factory
GrGLInterface* aGrGLInterface,
const IntSize &aSize,
SurfaceFormat aFormat);

static void
SetGlobalSkiaCacheLimits(int aCount, int aSizeInBytes);
#endif

#if defined(USE_SKIA) && defined(MOZ_ENABLE_FREETYPE)
Expand Down
41 changes: 21 additions & 20 deletions gfx/2d/DrawTargetSkia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class GradientStopsSkia : public GradientStops
};

#ifdef USE_SKIA_GPU
int DrawTargetSkia::sTextureCacheCount = 256;
int DrawTargetSkia::sTextureCacheSizeInBytes = 96*1024*1024;

static std::vector<DrawTargetSkia*>&
GLDrawTargets()
Expand All @@ -84,35 +86,27 @@ GLDrawTargets()
return targets;
}

#define SKIA_MAX_CACHE_ITEMS 256

// 64MB was chosen because it seems we can pass all of our tests
// on the current hardware (Tegra2) without running out of memory
#define SKIA_TOTAL_CACHE_SIZE 64*1024*1024

static void
SetCacheLimits()
void
DrawTargetSkia::RebalanceCacheLimits()
{
// Divide the global cache limits equally between all currently active GL-backed
// Skia DrawTargets.
std::vector<DrawTargetSkia*>& targets = GLDrawTargets();
uint32_t size = targets.size();
if (size == 0)
uint32_t targetCount = targets.size();
if (targetCount == 0)
return;

int individualCacheSize = SKIA_TOTAL_CACHE_SIZE / size;
for (uint32_t i = 0; i < size; i++) {
targets[i]->SetCacheLimits(SKIA_MAX_CACHE_ITEMS, individualCacheSize);
int individualCacheSize = sTextureCacheSizeInBytes / targetCount;
for (uint32_t i = 0; i < targetCount; i++) {
targets[i]->SetCacheLimits(sTextureCacheCount, individualCacheSize);
}

}

#undef SKIA_MAX_CACHE_ITEMS
#undef SKIA_TOTAL_CACHE_SIZE

static void
AddGLDrawTarget(DrawTargetSkia* target)
{
GLDrawTargets().push_back(target);
SetCacheLimits();
DrawTargetSkia::RebalanceCacheLimits();
}

static void
Expand All @@ -122,10 +116,18 @@ RemoveGLDrawTarget(DrawTargetSkia* target)
std::vector<DrawTargetSkia*>::iterator it = std::find(targets.begin(), targets.end(), target);
if (it != targets.end()) {
targets.erase(it);
SetCacheLimits();
DrawTargetSkia::RebalanceCacheLimits();
}
}

void
DrawTargetSkia::SetGlobalCacheLimits(int aCount, int aSizeInBytes)
{
sTextureCacheCount = aCount;
sTextureCacheSizeInBytes = aSizeInBytes;

DrawTargetSkia::RebalanceCacheLimits();
}
#endif

DrawTargetSkia::DrawTargetSkia()
Expand Down Expand Up @@ -732,7 +734,6 @@ DrawTargetSkia::SetCacheLimits(int aCount, int aSizeInBytes)
MOZ_ASSERT(mGrContext, "No GrContext!");
mGrContext->setTextureCacheLimits(aCount, aSizeInBytes);
}

#endif

void
Expand Down
7 changes: 6 additions & 1 deletion gfx/2d/DrawTargetSkia.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ class DrawTargetSkia : public DrawTarget
const IntSize &aSize,
SurfaceFormat aFormat) MOZ_OVERRIDE;

void SetCacheLimits(int number, int sizeInBytes);
void SetCacheLimits(int aCount, int aSizeInBytes);
static void SetGlobalCacheLimits(int aCount, int aSizeInBytes);
static void RebalanceCacheLimits();
#endif

operator std::string() const {
Expand All @@ -130,6 +132,9 @@ class DrawTargetSkia : public DrawTarget
RefPtr<GenericRefCountedBase> mGLContext;
SkRefPtr<GrGLInterface> mGrGLInterface;
SkRefPtr<GrContext> mGrContext;

static int sTextureCacheCount;
static int sTextureCacheSizeInBytes;
#endif

IntSize mSize;
Expand Down
6 changes: 6 additions & 0 deletions gfx/2d/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,12 @@ Factory::CreateDrawTargetSkiaWithGLContextAndGrGLInterface(GenericRefCountedBase
RefPtr<DrawTarget> newTarget = newDrawTargetSkia;
return newTarget;
}

void
Factory::SetGlobalSkiaCacheLimits(int aCount, int aSizeInBytes)
{
DrawTargetSkia::SetGlobalCacheLimits(aCount, aSizeInBytes);
}
#endif // USE_SKIA_GPU

#ifdef USE_SKIA_FREETYPE
Expand Down
38 changes: 38 additions & 0 deletions gfx/thebes/gfxPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#endif

#ifdef USE_SKIA
#include "mozilla/Hal.h"
#include "skia/SkGraphics.h"
#endif

Expand Down Expand Up @@ -277,6 +278,10 @@ gfxPlatform::gfxPlatform()
uint32_t canvasMask = (1 << BACKEND_CAIRO) | (1 << BACKEND_SKIA);
uint32_t contentMask = 1 << BACKEND_CAIRO;
InitBackendPrefs(canvasMask, contentMask);

#ifdef USE_SKIA
InitializeSkiaCaches();
#endif
}

gfxPlatform*
Expand Down Expand Up @@ -810,6 +815,39 @@ gfxPlatform::UseAcceleratedSkiaCanvas()
mPreferredCanvasBackend == BACKEND_SKIA;
}

void
gfxPlatform::InitializeSkiaCaches()
{
#ifdef USE_SKIA_GPU
if (UseAcceleratedSkiaCanvas()) {
bool usingDynamicCache = Preferences::GetBool("gfx.canvas.skiagl.dynamic-cache", false);

int cacheItemLimit = Preferences::GetInt("gfx.canvas.skiagl.cache-items", 256);
int cacheSizeLimit = Preferences::GetInt("gfx.canvas.skiagl.cache-size", 96);

// Prefs are in megabytes, but we want the sizes in bytes
cacheSizeLimit *= 1024*1024;

if (usingDynamicCache) {
uint32_t totalMemory = mozilla::hal::GetTotalSystemMemory();

if (totalMemory <= 256*1024*1024) {
// We need a very minimal cache on 256 meg devices
cacheSizeLimit = 2*1024*1024;
} else if (totalMemory > 0) {
cacheSizeLimit = totalMemory / 16;
}
}

#ifdef DEBUG
printf_stderr("Determined SkiaGL cache limits: Size %i, Items: %i\n", cacheSizeLimit, cacheItemLimit);
#endif

Factory::SetGlobalSkiaCacheLimits(cacheItemLimit, cacheSizeLimit);
}
#endif
}

already_AddRefed<gfxASurface>
gfxPlatform::GetThebesSurfaceForDrawTarget(DrawTarget *aTarget)
{
Expand Down
2 changes: 2 additions & 0 deletions gfx/thebes/gfxPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ class gfxPlatform {

virtual bool UseAcceleratedSkiaCanvas();

virtual void InitializeSkiaCaches();

void GetAzureBackendInfo(mozilla::widget::InfoObject &aObj) {
aObj.DefineProperty("AzureCanvasBackend", GetBackendName(mPreferredCanvasBackend));
aObj.DefineProperty("AzureSkiaAccelerated", UseAcceleratedSkiaCanvas());
Expand Down
7 changes: 7 additions & 0 deletions hal/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,5 +1195,12 @@ StopDiskSpaceWatcher()
PROXY_IF_SANDBOXED(StopDiskSpaceWatcher());
}

uint32_t
GetTotalSystemMemory()
{
return hal_impl::GetTotalSystemMemory();
}


} // namespace hal
} // namespace mozilla
7 changes: 7 additions & 0 deletions hal/Hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,13 @@ void StartDiskSpaceWatcher();
*/
void StopDiskSpaceWatcher();

/**
* Get total system memory of device being run on in bytes.
*
* Returns 0 if we are unable to determine this information from /proc/meminfo.
*/
uint32_t GetTotalSystemMemory();

} // namespace MOZ_HAL_NAMESPACE
} // namespace mozilla

Expand Down
20 changes: 20 additions & 0 deletions hal/fallback/FallbackMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=8 et :
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Hal.h"

namespace mozilla {
namespace hal_impl {

uint32_t
GetTotalSystemMemory()
{
return 0;
}

} // namespace hal_impl
} // namespace mozilla
39 changes: 39 additions & 0 deletions hal/linux/LinuxMemory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=8 et :
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdio.h>
#include "Hal.h"

namespace mozilla {
namespace hal_impl {

uint32_t
GetTotalSystemMemory()
{
static uint32_t sTotalMemory;
static bool sTotalMemoryObtained = false;

if (!sTotalMemoryObtained) {
sTotalMemoryObtained = true;

FILE* fd = fopen("/proc/meminfo", "r");
if (!fd) {
return 0;
}

int rv = fscanf(fd, "MemTotal: %i kB", &sTotalMemory);

if (fclose(fd) || rv != 1) {
return 0;
}
}

return sTotalMemory * 1024;
}

} // namespace hal_impl
} // namespace mozilla
7 changes: 7 additions & 0 deletions hal/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'android':
'android/AndroidSensor.cpp',
'fallback/FallbackPower.cpp',
'fallback/FallbackAlarm.cpp',
'linux/LinuxMemory.cpp',
]
elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
CPP_SOURCES += [
Expand All @@ -60,6 +61,7 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk':
'gonk/GonkSwitch.cpp',
'gonk/UeventPoller.cpp',
'linux/LinuxPower.cpp',
'linux/LinuxMemory.cpp',
]
elif CONFIG['OS_TARGET'] == 'Linux':
CPP_SOURCES += [
Expand All @@ -68,6 +70,7 @@ elif CONFIG['OS_TARGET'] == 'Linux':
'fallback/FallbackSensor.cpp',
'fallback/FallbackVibration.cpp',
'linux/LinuxPower.cpp',
'linux/LinuxMemory.cpp',
]
if CONFIG['MOZ_ENABLE_DBUS']:
CPP_SOURCES += [
Expand All @@ -85,6 +88,7 @@ elif CONFIG['OS_TARGET'] == 'WINNT':
'fallback/FallbackVibration.cpp',
'windows/WindowsBattery.cpp',
'windows/WindowsSensor.cpp',
'fallback/FallbackMemory.cpp',
]
elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
CPP_SOURCES += [
Expand All @@ -93,6 +97,7 @@ elif CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
'fallback/FallbackPower.cpp',
'fallback/FallbackScreenConfiguration.cpp',
'fallback/FallbackVibration.cpp',
'fallback/FallbackMemory.cpp',
]
elif CONFIG['OS_TARGET'] in ('OpenBSD', 'NetBSD', 'FreeBSD', 'DragonFly'):
CPP_SOURCES += [
Expand All @@ -101,6 +106,7 @@ elif CONFIG['OS_TARGET'] in ('OpenBSD', 'NetBSD', 'FreeBSD', 'DragonFly'):
'fallback/FallbackScreenConfiguration.cpp',
'fallback/FallbackSensor.cpp',
'fallback/FallbackVibration.cpp',
'fallback/FallbackMemory.cpp',
]
if CONFIG['MOZ_ENABLE_DBUS']:
CPP_SOURCES += [
Expand All @@ -118,6 +124,7 @@ else:
'fallback/FallbackScreenConfiguration.cpp',
'fallback/FallbackSensor.cpp',
'fallback/FallbackVibration.cpp',
'fallback/FallbackMemory.cpp',
]

# Fallbacks for backends implemented on Gonk only.
Expand Down

0 comments on commit 2f8b208

Please sign in to comment.