From 04d86c6a6b8eb8631752b3680f1292fa0a2c7119 Mon Sep 17 00:00:00 2001 From: "reed@android.com" Date: Mon, 25 Jan 2010 22:02:44 +0000 Subject: [PATCH] update to try out programmable shaders git-svn-id: http://skia.googlecode.com/svn/trunk@478 2bbb7eff-a529-9590-31e7-b0007b416f81 --- samplecode/SampleStrokePath.cpp | 27 ++++++ samplecode/SampleTestGL.cpp | 162 ++++++++++++++++++++++++++++++++ samplecode/SampleText.cpp | 2 +- samplecode/SampleTypeface.cpp | 7 +- 4 files changed, 196 insertions(+), 2 deletions(-) diff --git a/samplecode/SampleStrokePath.cpp b/samplecode/SampleStrokePath.cpp index 6808556dec..e7914dbe30 100644 --- a/samplecode/SampleStrokePath.cpp +++ b/samplecode/SampleStrokePath.cpp @@ -7,6 +7,32 @@ #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 #include "SkBlurMask.h" static void test_blur() { @@ -118,6 +144,7 @@ protected: virtual void onDraw(SkCanvas* canvas) { drawBG(canvas); //return; + test_huge_stroke(canvas); return; canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); SkPaint paint; diff --git a/samplecode/SampleTestGL.cpp b/samplecode/SampleTestGL.cpp index 210d7799e1..04e47b6b3e 100644 --- a/samplecode/SampleTestGL.cpp +++ b/samplecode/SampleTestGL.cpp @@ -9,6 +9,164 @@ #include "SkShader.h" #include "SkUtils.h" +#include +#include + +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) { SkPoint pts[] = { r.fLeft, 0, r.fRight, 0 }; SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; @@ -25,6 +183,7 @@ static void show_ramp(SkCanvas* canvas, const SkRect& r) { class TestGLView : public SkView { public: TestGLView() { + testinvert2(); } protected: @@ -44,6 +203,9 @@ protected: virtual void onDraw(SkCanvas* canvas) { drawBG(canvas); + test_glshader(canvas); + return; + SkRect r; r.set(0, 0, 100, 100); diff --git a/samplecode/SampleText.cpp b/samplecode/SampleText.cpp index 6deb5d0cb8..153726ac84 100644 --- a/samplecode/SampleText.cpp +++ b/samplecode/SampleText.cpp @@ -708,7 +708,7 @@ protected: // 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.setFlags(paint.getFlags() | gHints[index].fFlags); diff --git a/samplecode/SampleTypeface.cpp b/samplecode/SampleTypeface.cpp index b1289bdbec..2fadb418ff 100644 --- a/samplecode/SampleTypeface.cpp +++ b/samplecode/SampleTypeface.cpp @@ -66,7 +66,12 @@ protected: SkPaint paint; paint.setAntiAlias(true); paint.setTextSize(SkIntToScalar(30)); - + + if (false) { + paint.setStyle(SkPaint::kStroke_Style); + paint.setStrokeWidth(SkIntToScalar(1)); + } + const char* text = "Hamburgefons"; const size_t textLen = strlen(text); -- 2.34.1