Add coverage AA support for SampleApp root layers (windows only)

Review URL: http://codereview.appspot.com/6043045/




git-svn-id: http://skia.googlecode.com/svn/trunk@3703 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2012-04-17 12:43:00 +00:00
Родитель 78e17130f3
Коммит 8a189b0632
3 изменённых файлов: 139 добавлений и 34 удалений

Просмотреть файл

@ -33,28 +33,30 @@
#undef SK_LOCAL_LEAN_AND_MEAN
#endif
#define SK_WGL_DRAW_TO_WINDOW 0x2001
#define SK_WGL_ACCELERATION 0x2003
#define SK_WGL_SUPPORT_OPENGL 0x2010
#define SK_WGL_DOUBLE_BUFFER 0x2011
#define SK_WGL_COLOR_BITS 0x2014
#define SK_WGL_ALPHA_BITS 0x201B
#define SK_WGL_STENCIL_BITS 0x2023
#define SK_WGL_FULL_ACCELERATION 0x2027
#define SK_WGL_SAMPLE_BUFFERS 0x2041
#define SK_WGL_SAMPLES 0x2042
#define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
#define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
#define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
#define SK_WGL_CONTEXT_FLAGS 0x2094
#define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
#define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
#define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
#define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
#define SK_ERROR_INVALID_VERSION 0x2095
#define SK_ERROR_INVALID_PROFILE 0x2096
#define SK_WGL_DRAW_TO_WINDOW 0x2001
#define SK_WGL_ACCELERATION 0x2003
#define SK_WGL_SUPPORT_OPENGL 0x2010
#define SK_WGL_DOUBLE_BUFFER 0x2011
#define SK_WGL_COLOR_BITS 0x2014
#define SK_WGL_ALPHA_BITS 0x201B
#define SK_WGL_STENCIL_BITS 0x2023
#define SK_WGL_FULL_ACCELERATION 0x2027
#define SK_WGL_SAMPLE_BUFFERS 0x2041
#define SK_WGL_SAMPLES 0x2042
#define SK_WGL_COVERAGE_SAMPLES 0x2042 /* same as SAMPLES */
#define SK_WGL_COLOR_SAMPLES 0x20B9
#define SK_WGL_CONTEXT_MAJOR_VERSION 0x2091
#define SK_WGL_CONTEXT_MINOR_VERSION 0x2092
#define SK_WGL_CONTEXT_LAYER_PLANE 0x2093
#define SK_WGL_CONTEXT_FLAGS 0x2094
#define SK_WGL_CONTEXT_PROFILE_MASK 0x9126
#define SK_WGL_CONTEXT_DEBUG_BIT 0x0001
#define SK_WGL_CONTEXT_FORWARD_COMPATIBLE_BIT 0x0002
#define SK_WGL_CONTEXT_CORE_PROFILE_BIT 0x00000001
#define SK_WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
#define SK_WGL_CONTEXT_ES2_PROFILE_BIT 0x00000004
#define SK_ERROR_INVALID_VERSION 0x2095
#define SK_ERROR_INVALID_PROFILE 0x2096
class SkWGLExtensions {
public:
@ -73,6 +75,23 @@ public:
BOOL getPixelFormatAttribfv(HDC hdc, int, int, UINT, const int*, FLOAT*) const;
HGLRC createContextAttribs(HDC, HGLRC, const int *) const;
/**
* WGL doesn't have precise rules for the ordering of formats returned
* by wglChoosePixelFormat. This function helps choose among the set of
* formats returned by wglChoosePixelFormat. The rules in decreasing
* priority are:
* * Choose formats with the smallest sample count that is >=
* desiredSampleCount (or the largest sample count if all formats have
* fewer samples than desiredSampleCount.)
* * Choose formats with the fewest color samples when coverage sampling
* is available.
* * If the above rules leave multiple formats, choose the one that
* appears first in the formats array parameter.
*/
int selectFormat(const int formats[],
int formatCount,
HDC dc,
int desiredSampleCount);
private:
typedef const char* (WINAPI *GetExtensionsStringProc)(HDC hdc);
typedef BOOL (WINAPI *ChoosePixelFormatProc)(HDC hdc, const int *, const FLOAT *, UINT, int *, UINT *);

Просмотреть файл

@ -8,6 +8,9 @@
#include "SkWGL.h"
#include "SkTDArray.h"
#include "SkTSearch.h"
bool SkWGLExtensions::hasExtension(HDC dc, const char* ext) const {
if (NULL == this->fGetExtensionsString) {
return false;
@ -71,6 +74,81 @@ HGLRC SkWGLExtensions::createContextAttribs(HDC hDC,
return fCreateContextAttribs(hDC, hShareContext, attribList);
}
namespace {
struct PixelFormat {
int fFormat;
int fCoverageSamples;
int fColorSamples;
int fChoosePixelFormatRank;
};
int compare_pf(const PixelFormat* a, const PixelFormat* b) {
if (a->fCoverageSamples < b->fCoverageSamples) {
return -1;
} else if (b->fCoverageSamples < a->fCoverageSamples) {
return 1;
} else if (a->fColorSamples < b->fColorSamples) {
return -1;
} else if (b->fColorSamples < a->fColorSamples) {
return 1;
} else if (a->fChoosePixelFormatRank < b->fChoosePixelFormatRank) {
return -1;
} else if (b->fChoosePixelFormatRank < a->fChoosePixelFormatRank) {
return 1;
}
return 0;
}
}
int SkWGLExtensions::selectFormat(const int formats[],
int formatCount,
HDC dc,
int desiredSampleCount) {
PixelFormat desiredFormat = {
0,
desiredSampleCount,
0,
0,
};
SkTDArray<PixelFormat> rankedFormats;
rankedFormats.setCount(formatCount);
bool supportsCoverage = this->hasExtension(dc,
"WGL_NV_multisample_coverage");
for (int i = 0; i < formatCount; ++i) {
static const int queryAttrs[] = {
SK_WGL_COVERAGE_SAMPLES,
SK_WGL_COLOR_SAMPLES,
};
int answers[2];
int queryAttrCnt = supportsCoverage ? 2 : 1;
this->getPixelFormatAttribiv(dc,
formats[i],
0,
SK_ARRAY_COUNT(queryAttrs),
queryAttrs,
answers);
rankedFormats[i].fFormat = formats[i];
rankedFormats[i].fCoverageSamples = answers[0];
rankedFormats[i].fColorSamples = answers[supportsCoverage ? 1 : 0];
rankedFormats[i].fChoosePixelFormatRank = i;
}
SkQSort(rankedFormats.begin(),
rankedFormats.count(),
sizeof(PixelFormat),
(SkQSortCompareProc)compare_pf);
int idx = SkTSearch<PixelFormat>(rankedFormats.begin(),
rankedFormats.count(),
desiredFormat,
sizeof(PixelFormat),
compare_pf);
if (idx < 0) {
idx = ~idx;
}
return rankedFormats[idx].fFormat;
}
namespace {
#if defined(UNICODE)

Просмотреть файл

@ -305,9 +305,6 @@ void SkEvent::SignalQueueTimer(SkMSec delay)
}
}
#define USE_MSAA 0
HGLRC create_gl(HWND hwnd, int msaaSampleCount) {
HDC dc = GetDC(hwnd);
@ -339,7 +336,7 @@ HGLRC create_gl(HWND hwnd, int msaaSampleCount) {
if (msaaSampleCount > 0 &&
extensions.hasExtension(dc, "WGL_ARB_multisample")) {
static const int kIAttrsCount = SK_ARRAY_COUNT(iAttrs);
GLint msaaIAttrs[kIAttrsCount + 4];
GLint msaaIAttrs[kIAttrsCount + 6];
memcpy(msaaIAttrs, iAttrs, sizeof(GLint) * kIAttrsCount);
SkASSERT(0 == msaaIAttrs[kIAttrsCount - 2] &&
0 == msaaIAttrs[kIAttrsCount - 1]);
@ -347,18 +344,29 @@ HGLRC create_gl(HWND hwnd, int msaaSampleCount) {
msaaIAttrs[kIAttrsCount - 1] = TRUE;
msaaIAttrs[kIAttrsCount + 0] = SK_WGL_SAMPLES;
msaaIAttrs[kIAttrsCount + 1] = msaaSampleCount;
msaaIAttrs[kIAttrsCount + 2] = 0;
msaaIAttrs[kIAttrsCount + 3] = 0;
if (extensions.hasExtension(dc, "WGL_NV_multisample_coverage")) {
msaaIAttrs[kIAttrsCount + 2] = SK_WGL_COLOR_SAMPLES;
// We want the fewest number of color samples possible.
// Passing 0 gives only the formats where all samples are color
// samples.
msaaIAttrs[kIAttrsCount + 3] = 1;
msaaIAttrs[kIAttrsCount + 4] = 0;
msaaIAttrs[kIAttrsCount + 5] = 0;
} else {
msaaIAttrs[kIAttrsCount + 2] = 0;
msaaIAttrs[kIAttrsCount + 3] = 0;
}
GLuint num;
int formats[64];
extensions.choosePixelFormat(dc, msaaIAttrs, fAttrs, 64, formats, &num);
num = min(num,64);
for (GLuint i = 0; i < num; ++i) {
DescribePixelFormat(dc, formats[i], sizeof(pfd), &pfd);
if (SetPixelFormat(dc, formats[i], &pfd)) {
format = formats[i];
break;
}
int formatToTry = extensions.selectFormat(formats,
num,
dc,
msaaSampleCount);
DescribePixelFormat(dc, formatToTry, sizeof(pfd), &pfd);
if (SetPixelFormat(dc, formatToTry, &pfd)) {
format = formatToTry;
}
}