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