update to try out programmable shaders

git-svn-id: http://skia.googlecode.com/svn/trunk@478 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@android.com 2010-01-25 22:02:44 +00:00
parent b8fd84b803
commit 04d86c6a6b
4 changed files with 196 additions and 2 deletions

View File

@ -7,6 +7,32 @@
#include "SkBlurMaskFilter.h" #include "SkBlurMaskFilter.h"
static void test_huge_stroke(SkCanvas* canvas) {
SkRect srcR = { 0, 0, 72000, 54000 };
SkRect dstR = { 0, 0, 640, 480 };
SkPath path;
path.moveTo(17600, 8000);
path.lineTo(52800, 8000);
path.lineTo(52800, 41600);
path.lineTo(17600, 41600);
path.close();
SkPaint paint;
paint.setAntiAlias(true);
paint.setStrokeWidth(8000);
paint.setStrokeMiter(10);
paint.setStrokeCap(SkPaint::kButt_Cap);
paint.setStrokeJoin(SkPaint::kRound_Join);
paint.setStyle(SkPaint::kStroke_Style);
SkMatrix matrix;
matrix.setRectToRect(srcR, dstR, SkMatrix::kCenter_ScaleToFit);
canvas->concat(matrix);
canvas->drawPath(path, paint);
}
#if 0 #if 0
#include "SkBlurMask.h" #include "SkBlurMask.h"
static void test_blur() { static void test_blur() {
@ -118,6 +144,7 @@ protected:
virtual void onDraw(SkCanvas* canvas) { virtual void onDraw(SkCanvas* canvas) {
drawBG(canvas); drawBG(canvas);
//return; //return;
test_huge_stroke(canvas); return;
canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
SkPaint paint; SkPaint paint;

View File

@ -9,6 +9,164 @@
#include "SkShader.h" #include "SkShader.h"
#include "SkUtils.h" #include "SkUtils.h"
#include <AGL/agl.h>
#include <OpenGL/gl.h>
static void test_draw_gl(SkCanvas* canvas) {
const float verts[] = {
10, 10, 250, 250, 490, 10
};
const float texs[] = {
0, 0, 0.5f, 0, 1, 1
};
const uint8_t colors[] = {
0, 0, 0, 1,
128, 0, 0, 1,
255, 255, 0, 1
};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, verts);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
// glTexCoordPointer(2, GL_FLOAT, 0, texs);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
static const char* gVertShaderText =
"varying vec2 uv;"
"void main(void) {"
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
" uv = vec2(gl_Color);"
"}";
static const char* gFragShaderText =
"varying vec2 uv;"
"void main(void) {"
" float u = uv.x;"
" float v = uv.y;"
" if (u*u > v) {"
" gl_FragColor = vec4(1.0, 0, 0, 1.0);"
" } else {"
" gl_FragColor = vec4(0, 1.0, 0, 1.0);"
"}"
"}";
static bool compile_shader(GLuint shader) {
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar buffer[256];
glGetShaderInfoLog(shader, sizeof(buffer), NULL, buffer);
SkDebugf("---- glCompileShader failed: %s\n", buffer);
return false;
}
return true;
}
static bool link_program(GLuint program) {
glLinkProgram(program);
GLint success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
GLchar buffer[256];
glGetProgramInfoLog(program, sizeof(buffer), NULL, buffer);
SkDebugf("---- glLinkProgram failed: %s\n", buffer);
return false;
}
return true;
}
static void test_glshader(SkCanvas* canvas) {
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertShader, 1, &gVertShaderText, NULL);
glShaderSource(fragShader, 1, &gFragShaderText, NULL);
if (!compile_shader(vertShader)) { return; }
if (!compile_shader(fragShader)) { return; }
GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
if (link_program(program)) {
glUseProgram(program);
test_draw_gl(canvas);
glUseProgram(0);
}
glDeleteProgram(program);
glDeleteShader(vertShader);
glDeleteShader(fragShader);
}
static void setmatrix6(SkMatrix* matrix, const float val[]) {
matrix->reset();
for (int i = 0; i < 6; i++) {
matrix->set(i, val[i]);
}
}
static void testinvert() {
SkMatrix matrix;
const float vals[] = { 0,9,.000001,10000,0,0 };
setmatrix6(&matrix, vals);
const float vals2[] = { 0,100,71,9,0,7 };
SkMatrix tmp;
setmatrix6(&tmp, vals2);
matrix.preConcat(tmp);
matrix.dump();
SkMatrix inverse;
matrix.invert(&inverse);
inverse.dump();
matrix.preConcat(inverse);
matrix.dump();
// o2dContext.setTransform(0,9,.000001,10000,0,0);
// o2dContext.transform(0,100,71,9,0,7);
// o2dContext.setTransform(0,6,95,4,1,0);
}
/*
[0] 9.9999997e-005 float
[1] 9.0000003e-006 float
[2] 7.0000001e-006 float
[3] 1000000.0 float
[4] 90639.000 float
[5] 70000.000 float
[6] 0.00000000 float
[7] 0.00000000 float
[8] 1.0000000 float
*/
static void testinvert2() {
const float val[] = {
9.9999997e-005, 9.0000003e-006, 7.0000001e-006,
1000000.0, 90639.000, 70000.000
};
SkMatrix matrix;
setmatrix6(&matrix, val);
matrix.dump();
SkMatrix inverse;
matrix.invert(&inverse);
inverse.dump();
matrix.preConcat(inverse);
matrix.dump();
// result is that matrix[3] is 49550 instead of 0 :(
}
static void show_ramp(SkCanvas* canvas, const SkRect& r) { static void show_ramp(SkCanvas* canvas, const SkRect& r) {
SkPoint pts[] = { r.fLeft, 0, r.fRight, 0 }; SkPoint pts[] = { r.fLeft, 0, r.fRight, 0 };
SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; SkColor colors[] = { SK_ColorRED, SK_ColorBLUE };
@ -25,6 +183,7 @@ static void show_ramp(SkCanvas* canvas, const SkRect& r) {
class TestGLView : public SkView { class TestGLView : public SkView {
public: public:
TestGLView() { TestGLView() {
testinvert2();
} }
protected: protected:
@ -44,6 +203,9 @@ protected:
virtual void onDraw(SkCanvas* canvas) { virtual void onDraw(SkCanvas* canvas) {
drawBG(canvas); drawBG(canvas);
test_glshader(canvas);
return;
SkRect r; SkRect r;
r.set(0, 0, 100, 100); r.set(0, 0, 100, 100);

View File

@ -708,7 +708,7 @@ protected:
// canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint); // canvas->drawText(style, strlen(style), SkIntToScalar(20), SkIntToScalar(20), paint);
// paint.setTypeface(SkTypeface::Create(NULL, SkTypeface::kItalic))->unref(); SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samplefont.ttf")));
paint.setAntiAlias(true); paint.setAntiAlias(true);
paint.setFlags(paint.getFlags() | gHints[index].fFlags); paint.setFlags(paint.getFlags() | gHints[index].fFlags);

View File

@ -66,7 +66,12 @@ protected:
SkPaint paint; SkPaint paint;
paint.setAntiAlias(true); paint.setAntiAlias(true);
paint.setTextSize(SkIntToScalar(30)); paint.setTextSize(SkIntToScalar(30));
if (false) {
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(1));
}
const char* text = "Hamburgefons"; const char* text = "Hamburgefons";
const size_t textLen = strlen(text); const size_t textLen = strlen(text);