Optimize Core Graphics drawing model OOPP implementation. b=555835 r=josh

This commit is contained in:
Benoit Girard 2010-04-01 17:53:56 -04:00
parent e84fd2f284
commit d0a3d6d019
7 changed files with 68 additions and 29 deletions

View File

@ -80,6 +80,7 @@ using mozilla::gfx::SharedDIB;
#define NS_OOPP_DOUBLEPASS_MSGID TEXT("MozDoublePassMsg")
#elif defined(XP_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#include "nsPluginUtilsOSX.h"
#endif // defined(XP_MACOSX)
PluginInstanceChild::PluginInstanceChild(const NPPluginFuncs* aPluginIface,
@ -99,6 +100,10 @@ PluginInstanceChild::PluginInstanceChild(const NPPluginFuncs* aPluginIface,
, mWinlessPopupSurrogateHWND(0)
#endif // OS_WIN
, mAsyncCallMutex("PluginInstanceChild::mAsyncCallMutex")
#if defined(OS_MACOSX)
, mShColorSpace(NULL)
, mShContext(NULL)
#endif
{
memset(&mWindow, 0, sizeof(mWindow));
mData.ndata = (void*) this;
@ -127,6 +132,14 @@ PluginInstanceChild::~PluginInstanceChild()
#if defined(OS_WIN)
DestroyPluginWindow();
#endif
#if defined(OS_MACOSX)
if (mShColorSpace) {
::CGColorSpaceRelease(mShColorSpace);
}
if (mShContext) {
::CGContextRelease(mShContext);
}
#endif
}
void
@ -585,28 +598,34 @@ PluginInstanceChild::AnswerNPP_HandleEvent_Shmem(const NPRemoteEvent& event,
PLUGIN_LOG_DEBUG_FUNCTION;
AssertPluginThread();
// We return access to the shared memory buffer after returning.
NPCocoaEvent evcopy = event.event;
if (evcopy.type == NPCocoaEventDrawRect) {
CGColorSpaceRef cSpace = ::CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
if (!cSpace) {
PLUGIN_LOG_DEBUG(("Could not allocate ColorSpace."));
*handled = false;
*rtnmem = mem;
return true;
}
void* cgContextByte = mem.get<char>();
CGContextRef ctxt = ::CGBitmapContextCreate(cgContextByte, mWindow.width, mWindow.height, 8, mWindow.width * 4, cSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
::CGColorSpaceRelease(cSpace);
if (!ctxt) {
PLUGIN_LOG_DEBUG(("Could not allocate CGBitmapContext."));
*handled = false;
*rtnmem = mem;
return true;
if (!mShColorSpace) {
mShColorSpace = CreateSystemColorSpace();
if (!mShColorSpace) {
PLUGIN_LOG_DEBUG(("Could not allocate ColorSpace."));
*handled = false;
*rtnmem = mem;
return true;
}
}
evcopy.data.draw.context = ctxt;
if (!mShContext) {
void* cgContextByte = mem.get<char>();
mShContext = ::CGBitmapContextCreate(cgContextByte,
mWindow.width, mWindow.height, 8,
mWindow.width * 4, mShColorSpace,
kCGImageAlphaPremultipliedFirst |
kCGBitmapByteOrder32Host);
if (!mShContext) {
PLUGIN_LOG_DEBUG(("Could not allocate CGBitmapContext."));
*handled = false;
*rtnmem = mem;
return true;
}
}
evcopy.data.draw.context = mShContext;
} else {
PLUGIN_LOG_DEBUG(("Invalid event type for AnswerNNP_HandleEvent_Shmem."));
*handled = false;
@ -620,11 +639,6 @@ PluginInstanceChild::AnswerNPP_HandleEvent_Shmem(const NPRemoteEvent& event,
*handled = mPluginIface->event(&mData, reinterpret_cast<void*>(&evcopy));
}
// Some events need cleaning up.
if (evcopy.type == NPCocoaEventDrawRect) {
::CGContextRelease(evcopy.data.draw.context);
}
*rtnmem = mem;
return true;
}
@ -779,6 +793,13 @@ PluginInstanceChild::AnswerNPP_SetWindow(const NPRemoteWindow& aWindow)
mWindow.clipRect = aWindow.clipRect;
mWindow.type = aWindow.type;
if (mShContext) {
// Release the shared context so that it is reallocated
// with the new size.
::CGContextRelease(mShContext);
mShContext = NULL;
}
if (mPluginIface->setwindow)
(void) mPluginIface->setwindow(&mData, &mWindow);

View File

@ -320,6 +320,11 @@ private:
HBITMAP bmp;
} mAlphaExtract;
#endif // defined(OS_WIN)
#if defined(OS_MACOSX)
private:
CGColorSpaceRef mShColorSpace;
CGContextRef mShContext;
#endif
};
} // namespace plugins

View File

@ -61,6 +61,7 @@ UINT gOOPPStopNativeLoopEvent =
#include <gdk/gdk.h>
#elif defined(XP_MACOSX)
#include <ApplicationServices/ApplicationServices.h>
#include "nsPluginUtilsOSX.h"
#endif // defined(XP_MACOSX)
using namespace mozilla::plugins;
@ -80,6 +81,7 @@ PluginInstanceParent::PluginInstanceParent(PluginModuleParent* parent,
#if defined(XP_MACOSX)
, mShWidth(0)
, mShHeight(0)
, mShColorSpace(NULL)
#endif
{
}
@ -93,6 +95,9 @@ PluginInstanceParent::~PluginInstanceParent()
NS_ASSERTION(!(mPluginHWND || mPluginWndProc),
"Subclass was not reset correctly before the dtor was reached!");
#endif
#if defined(OS_MACOSX)
::CGColorSpaceRelease(mShColorSpace);
#endif
}
bool
@ -693,15 +698,16 @@ PluginInstanceParent::NPP_HandleEvent(void* event)
char* shContextByte = mShSurface.get<char>();
CGColorSpaceRef cSpace = ::CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
if (!cSpace) {
if (!mShColorSpace) {
mShColorSpace = CreateSystemColorSpace();
}
if (!mShColorSpace) {
PLUGIN_LOG_DEBUG(("Could not allocate ColorSpace."));
return true;
}
CGContextRef shContext = ::CGBitmapContextCreate(shContextByte,
mShWidth, mShHeight, 8, mShWidth*4, cSpace,
mShWidth, mShHeight, 8, mShWidth*4, mShColorSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);
::CGColorSpaceRelease(cSpace);
if (!shContext) {
PLUGIN_LOG_DEBUG(("Could not allocate CGBitmapContext."));
return true;

View File

@ -285,6 +285,7 @@ private:
Shmem mShSurface;
size_t mShWidth;
size_t mShHeight;
CGColorSpaceRef mShColorSpace;
#endif // definied(OS_MACOSX)
};

View File

@ -69,6 +69,7 @@ EXPORTS = \
nsIScrollableFrame.h \
nsIStatefulFrame.h \
nsFrameSelection.h \
nsPluginUtilsOSX.h \
$(NULL)
ifdef IBMBIDI

View File

@ -64,6 +64,9 @@ NPBool NS_NPAPI_ConvertPointCocoa(void* inView,
double sourceX, double sourceY, NPCoordinateSpace sourceSpace,
double *destX, double *destY, NPCoordinateSpace destSpace);
// Get the system color space.
CGColorSpaceRef CreateSystemColorSpace();
// Manages a CARenderer
struct _CGLPBufferObject;
struct _CGLContextObject;

View File

@ -221,15 +221,17 @@ nsCARenderer::~nsCARenderer() {
Destroy();
}
CGColorSpaceRef CreateSystemColorSpace () {
CGColorSpaceRef CreateSystemColorSpace() {
CMProfileRef system_profile = NULL;
CGColorSpaceRef cspace = NULL;
if (::CMGetSystemProfile(&system_profile) == noErr) {
// Create a colorspace with the systems profile
cspace = ::CGColorSpaceCreateWithPlatformColorSpace(system_profile);
::CMCloseProfile(system_profile);
} else {
// Default to generic
cspace = ::CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
}
return cspace;