reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | #include "SampleCode.h" |
| 2 | #include "SkView.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "Sk64.h" |
| 5 | #include "SkGradientShader.h" |
| 6 | #include "SkGraphics.h" |
| 7 | #include "SkImageDecoder.h" |
| 8 | #include "SkKernel33MaskFilter.h" |
| 9 | #include "SkPath.h" |
| 10 | #include "SkRandom.h" |
| 11 | #include "SkRegion.h" |
| 12 | #include "SkShader.h" |
| 13 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkColorPriv.h" |
| 15 | #include "SkColorFilter.h" |
| 16 | #include "SkTime.h" |
| 17 | #include "SkTypeface.h" |
| 18 | #include "SkXfermode.h" |
| 19 | |
| 20 | // effects |
| 21 | #include "SkGradientShader.h" |
| 22 | #include "SkShaderExtras.h" |
| 23 | #include "SkUnitMappers.h" |
| 24 | |
| 25 | #include "SkStream.h" |
| 26 | #include "SkXMLParser.h" |
| 27 | |
| 28 | #include "SkGLCanvas.h" |
| 29 | |
| 30 | #include <AGL/agl.h> |
| 31 | #include <OpenGL/gl.h> |
| 32 | |
| 33 | extern void* gSampleWind; |
| 34 | |
| 35 | static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) |
| 36 | { |
| 37 | bm->setConfig(config, w, h); |
| 38 | bm->allocPixels(); |
| 39 | bm->eraseColor(0); |
| 40 | |
| 41 | SkCanvas canvas(*bm); |
| 42 | SkPoint pts[] = { 0, 0, SkIntToScalar(w), SkIntToScalar(h) }; |
| 43 | SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
| 44 | SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 }; |
| 45 | SkPaint paint; |
| 46 | |
| 47 | SkUnitMapper* um = NULL; |
| 48 | |
| 49 | // um = new SkCosineMapper; |
| 50 | // um = new SkDiscreteMapper(12); |
| 51 | |
| 52 | SkAutoUnref au(um); |
| 53 | |
| 54 | paint.setAntiAlias(true); |
| 55 | paint.setShader(SkGradientShader::CreateLinear(pts, colors, pos, |
| 56 | SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode, um))->unref(); |
| 57 | |
| 58 | SkRect r; |
| 59 | r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); |
| 60 | canvas.drawOval(r, paint); |
| 61 | } |
| 62 | |
| 63 | static void premulBitmap(const SkBitmap& bm) { |
| 64 | for (int y = 0; y < bm.height(); y++) { |
| 65 | SkPMColor* p = bm.getAddr32(0, y); |
| 66 | for (int x = 0; x < bm.width(); x++) { |
| 67 | SkPMColor c = *p; |
| 68 | unsigned a = SkGetPackedA32(c); |
| 69 | unsigned r = SkGetPackedR32(c); |
| 70 | unsigned g = SkGetPackedG32(c); |
| 71 | unsigned b = SkGetPackedB32(c); |
| 72 | |
| 73 | unsigned scale = SkAlpha255To256(a); |
| 74 | r = SkAlphaMul(r, scale); |
| 75 | g = SkAlphaMul(g, scale); |
| 76 | b = SkAlphaMul(b, scale); |
| 77 | *p++ = SkPackARGB32(a, r, g, b); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | class GLView : public SkView { |
| 83 | public: |
| 84 | AGLContext fCtx; |
| 85 | SkBitmap fOffscreen; |
| 86 | SkBitmap fTexture[3]; |
| 87 | |
| 88 | GLView() { |
| 89 | makebm(&fTexture[0], SkBitmap::kARGB_8888_Config, 64, 100); |
| 90 | makebm(&fTexture[1], SkBitmap::kRGB_565_Config, 64, 100); |
| 91 | makebm(&fTexture[2], SkBitmap::kARGB_4444_Config, 64, 100); |
| 92 | |
| 93 | GLint major, minor; |
| 94 | |
| 95 | aglGetVersion(&major, &minor); |
| 96 | SkDebugf("---- version %d %d\n", major, minor); |
| 97 | |
| 98 | GLint attr[] = { |
| 99 | AGL_RGBA, |
| 100 | AGL_DEPTH_SIZE, 32, |
| 101 | AGL_OFFSCREEN, |
| 102 | AGL_NONE |
| 103 | }; |
| 104 | |
| 105 | SkDebugf("------ attr %p %d\n", attr, sizeof(attr)); |
| 106 | AGLPixelFormat format = aglCreatePixelFormat(attr); |
| 107 | SkDebugf("----- format %p\n", format); |
| 108 | fCtx = aglCreateContext(format, 0); |
| 109 | SkDebugf("----- context %p\n", fCtx); |
| 110 | GLboolean success; //= aglSetWindowRef(fCtx, (WindowRef)gSampleWind); |
| 111 | // SkDebugf("----- aglSetWindowRef %d\n", success); |
| 112 | |
| 113 | aglEnable(fCtx, GL_BLEND); |
| 114 | aglEnable(fCtx, GL_LINE_SMOOTH); |
| 115 | aglEnable(fCtx, GL_POINT_SMOOTH); |
| 116 | aglEnable(fCtx, GL_POLYGON_SMOOTH); |
| 117 | |
| 118 | fOffscreen.setConfig(SkBitmap::kARGB_8888_Config, 300, 300); |
| 119 | fOffscreen.allocPixels(); |
| 120 | |
| 121 | success = aglSetOffScreen(fCtx, |
| 122 | fOffscreen.width(), |
| 123 | fOffscreen.height(), |
| 124 | fOffscreen.rowBytes(), |
| 125 | fOffscreen.getPixels()); |
| 126 | GLenum err = aglGetError(); |
| 127 | SkDebugf("---- setoffscreen %d %d %s\n", success, err, aglErrorString(err)); |
| 128 | |
| 129 | aglSetCurrentContext(fCtx); |
| 130 | glOrtho(0, fOffscreen.width(), |
| 131 | fOffscreen.height(), 0, |
| 132 | -1, 1); |
| 133 | |
| 134 | glEnable(GL_BLEND); |
| 135 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 136 | glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE); |
| 137 | |
| 138 | glEnable(GL_TEXTURE_2D); |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | // overrides from SkEventSink |
| 143 | virtual bool onQuery(SkEvent* evt) { |
| 144 | if (SampleCode::TitleQ(*evt)) { |
| 145 | SampleCode::TitleR(evt, "GL"); |
| 146 | return true; |
| 147 | } |
| 148 | return this->INHERITED::onQuery(evt); |
| 149 | } |
| 150 | |
| 151 | void drawBG(SkCanvas* canvas) { |
| 152 | canvas->drawColor(0xFFDDDDDD); |
| 153 | } |
| 154 | |
| 155 | virtual void onDraw(SkCanvas* canvas) { |
| 156 | this->drawBG(canvas); |
| 157 | |
| 158 | SkGLCanvas c(fOffscreen.width(), fOffscreen.height()); |
| 159 | |
| 160 | glClearColor(0, 0, 0, 0); |
| 161 | glClear(GL_COLOR_BUFFER_BIT); |
| 162 | |
| 163 | SkPaint p; |
| 164 | |
| 165 | p.setAntiAlias(true); |
| 166 | |
| 167 | c.drawColor(SK_ColorWHITE); |
| 168 | |
| 169 | p.setColor(SK_ColorRED); |
| 170 | c.drawCircle(SkIntToScalar(40), SkIntToScalar(40), SkIntToScalar(20), p); |
| 171 | |
| 172 | p.setColor(SK_ColorGREEN); |
| 173 | p.setStrokeWidth(SkIntToScalar(6)); |
| 174 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 175 | c.drawLine(SkIntToScalar(10), SkIntToScalar(10), SkIntToScalar(40), SkIntToScalar(50), p); |
| 176 | |
| 177 | // c.scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2); |
| 178 | p.setColor(0x880000FF); |
| 179 | c.drawCircle(SkIntToScalar(40), SkIntToScalar(40), SkIntToScalar(20), p); |
| 180 | |
| 181 | for (int i = 0; i < SK_ARRAY_COUNT(fTexture); i++) { |
| 182 | c.drawBitmap(fTexture[i], SkIntToScalar(10), SkIntToScalar(100), NULL); |
| 183 | c.translate(SkIntToScalar(fTexture[i].width()), 0); |
| 184 | } |
| 185 | p.setColor(SK_ColorBLUE); |
| 186 | c.drawRectCoords(SkIntToScalar(10), SkIntToScalar(100), |
| 187 | SkIntToScalar(10+fTexture[0].width()), |
| 188 | SkIntToScalar(100+fTexture[0].height()), |
| 189 | p); |
| 190 | |
| 191 | //////// |
| 192 | glFlush(); |
| 193 | premulBitmap(fOffscreen); |
| 194 | canvas->drawBitmap(fOffscreen, SkIntToScalar(10), SkIntToScalar(10), NULL); |
| 195 | } |
| 196 | |
| 197 | private: |
| 198 | |
| 199 | typedef SkView INHERITED; |
| 200 | }; |
| 201 | |
| 202 | ////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | static SkView* MyFactory() { return new GLView; } |
| 205 | static SkViewRegister reg(MyFactory); |
| 206 | |