blob: 305c8c5a60e8089c58c0b355ed9ae7d88f654e89 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
24
Romain Guye4d01122010-06-16 18:44:05 -070025#include <utils/Log.h>
26
Romain Guy85bf02f2010-06-22 13:11:24 -070027#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070028
29namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070030namespace uirenderer {
31
32///////////////////////////////////////////////////////////////////////////////
33// Defines
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guybd6b79b2010-06-26 00:13:53 -070036#define SV(x, y) { { x, y } }
37#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070038
39///////////////////////////////////////////////////////////////////////////////
40// Globals
41///////////////////////////////////////////////////////////////////////////////
42
Romain Guy026c5e162010-06-28 17:12:22 -070043static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070044 SV(0.0f, 0.0f),
45 SV(1.0f, 0.0f),
46 SV(0.0f, 1.0f),
47 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070048};
Romain Guy026c5e162010-06-28 17:12:22 -070049static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
50static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070051
Romain Guy026c5e162010-06-28 17:12:22 -070052// This array is never used directly but used as a memcpy source in the
53// OpenGLRenderer constructor
54static const TextureVertex gDrawTextureVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070055 FV(0.0f, 0.0f, 0.0f, 1.0f),
56 FV(1.0f, 0.0f, 1.0f, 1.0f),
57 FV(0.0f, 1.0f, 0.0f, 0.0f),
58 FV(1.0f, 1.0f, 1.0f, 0.0f)
59};
Romain Guy026c5e162010-06-28 17:12:22 -070060static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
61static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070062
Romain Guy026c5e162010-06-28 17:12:22 -070063// In this array, the index of each Blender equals the value of the first
64// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65static const Blender gBlends[] = {
66 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
67 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
68 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
69 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
70 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
71 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
72 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
73 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
76 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
77 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
78};
Romain Guye4d01122010-06-16 18:44:05 -070079
Romain Guyf6a11b82010-06-23 17:47:49 -070080///////////////////////////////////////////////////////////////////////////////
81// Constructors/destructor
82///////////////////////////////////////////////////////////////////////////////
83
Romain Guy85bf02f2010-06-22 13:11:24 -070084OpenGLRenderer::OpenGLRenderer() {
85 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070086
87 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -070088 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -070089
90 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -070091}
92
Romain Guy85bf02f2010-06-22 13:11:24 -070093OpenGLRenderer::~OpenGLRenderer() {
94 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -070095}
96
Romain Guyf6a11b82010-06-23 17:47:49 -070097///////////////////////////////////////////////////////////////////////////////
98// Setup
99///////////////////////////////////////////////////////////////////////////////
100
Romain Guy85bf02f2010-06-22 13:11:24 -0700101void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700102 glViewport(0, 0, width, height);
103
104 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700105 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700106 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700107
108 mWidth = width;
109 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700110}
111
Romain Guy85bf02f2010-06-22 13:11:24 -0700112void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700113 mSnapshot = &mFirstSnapshot;
114 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700115
Romain Guy08ae3172010-06-21 19:35:50 -0700116 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700117
Romain Guy08ae3172010-06-21 19:35:50 -0700118 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
119 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700120
Romain Guy08ae3172010-06-21 19:35:50 -0700121 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700122 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700123
Romain Guybb9524b2010-06-22 18:56:38 -0700124 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
125}
126
Romain Guyf6a11b82010-06-23 17:47:49 -0700127///////////////////////////////////////////////////////////////////////////////
128// State management
129///////////////////////////////////////////////////////////////////////////////
130
Romain Guybb9524b2010-06-22 18:56:38 -0700131int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700132 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700133}
134
135int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700136 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700137}
138
139void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700140 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700141
Romain Guy7ae7ac42010-06-25 13:46:18 -0700142 if (restoreSnapshot()) {
143 setScissorFromClip();
144 }
Romain Guybb9524b2010-06-22 18:56:38 -0700145}
146
147void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700148 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700149
Romain Guy7ae7ac42010-06-25 13:46:18 -0700150 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700151
Romain Guy7ae7ac42010-06-25 13:46:18 -0700152 while (mSaveCount != saveCount - 1) {
153 restoreClip |= restoreSnapshot();
154 }
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guy7ae7ac42010-06-25 13:46:18 -0700156 if (restoreClip) {
157 setScissorFromClip();
158 }
Romain Guybb9524b2010-06-22 18:56:38 -0700159}
160
161int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700162 mSnapshot = new Snapshot(mSnapshot);
163 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700164}
165
166bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700167 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700168 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700169
Romain Guybd6b79b2010-06-26 00:13:53 -0700170 sp<Snapshot> current = mSnapshot;
171 sp<Snapshot> previous = mSnapshot->previous;
172
173 if (restoreLayer) {
174 // Unbind current FBO and restore previous one
175 // Most of the time, previous->fbo will be 0 to bind the default buffer
176 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
177
Romain Guy5cbbce52010-06-27 22:59:20 -0700178 // Restore the clip from the previous snapshot
179 const Rect& clip = previous->getMappedClip();
180 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guybd6b79b2010-06-26 00:13:53 -0700181
Romain Guy5cbbce52010-06-27 22:59:20 -0700182 // Compute the correct texture coordinates for the FBO texture
183 // The texture is currently as big as the window but drawn with
184 // a quad of the appropriate size
185 const Rect& layer = current->layer;
186 Rect texCoords(current->layer);
187 mSnapshot->transform.mapRect(texCoords);
188
189 const float u1 = texCoords.left / float(mWidth);
190 const float v1 = (mHeight - texCoords.top) / float(mHeight);
191 const float u2 = texCoords.right / float(mWidth);
192 const float v2 = (mHeight - texCoords.bottom) / float(mHeight);
193
194 resetDrawTextureTexCoords(u1, v1, u2, v1);
195
196 drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
197 current->texture, current->alpha);
198
199 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
Romain Guybd6b79b2010-06-26 00:13:53 -0700200
201 glDeleteFramebuffers(1, &current->fbo);
202 glDeleteTextures(1, &current->texture);
203 }
204
205 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700206 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700207
Romain Guy7ae7ac42010-06-25 13:46:18 -0700208 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700209}
210
Romain Guyf6a11b82010-06-23 17:47:49 -0700211///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700212// Layers
213///////////////////////////////////////////////////////////////////////////////
214
215int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
216 const SkPaint* p, int flags) {
217 // TODO Implement
218 return saveSnapshot();
219}
220
221int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
222 int alpha, int flags) {
223 int count = saveSnapshot();
224
225 mSnapshot->flags |= Snapshot::kFlagIsLayer;
226 mSnapshot->alpha = alpha / 255.0f;
227 mSnapshot->layer.set(left, top, right, bottom);
228
229 // Generate the FBO and attach the texture
230 glGenFramebuffers(1, &mSnapshot->fbo);
231 glBindFramebuffer(GL_FRAMEBUFFER, mSnapshot->fbo);
232
233 // Generate the texture in which the FBO will draw
234 glGenTextures(1, &mSnapshot->texture);
235 glBindTexture(GL_TEXTURE_2D, mSnapshot->texture);
236
237 // The FBO will not be scaled, so we can use lower quality filtering
238 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy5cbbce52010-06-27 22:59:20 -0700240 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700242
243 // TODO ***** IMPORTANT *****
244 // Creating a texture-backed FBO works only if the texture is the same size
245 // as the original rendering buffer (in this case, mWidth and mHeight.)
246 // This is expensive and wasteful and must be fixed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700247 // TODO Additionally we should use an FBO cache
Romain Guybd6b79b2010-06-26 00:13:53 -0700248
249 const GLsizei width = mWidth; //right - left;
250 const GLsizei height = mHeight; //bottom - right;
251
252 const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
253 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
254 glBindTexture(GL_TEXTURE_2D, 0);
255
256 // Bind texture to FBO
257 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
258 mSnapshot->texture, 0);
259
260 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
261 if (status != GL_FRAMEBUFFER_COMPLETE) {
262 LOGD("Framebuffer incomplete %d", status);
263
264 glDeleteFramebuffers(1, &mSnapshot->fbo);
265 glDeleteTextures(1, &mSnapshot->texture);
266 }
267
268 return count;
269}
270
271///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700272// Transforms
273///////////////////////////////////////////////////////////////////////////////
274
275void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700276 mSnapshot->transform.translate(dx, dy, 0.0f);
277 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700278}
279
280void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700281 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
282 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700283}
284
285void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700286 mSnapshot->transform.scale(sx, sy, 1.0f);
287 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700288}
289
290void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700291 mSnapshot->transform.load(*matrix);
292 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700293}
294
295void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700296 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700297}
298
299void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700300 mat4 m(*matrix);
301 mSnapshot->transform.multiply(m);
302 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700303}
304
305///////////////////////////////////////////////////////////////////////////////
306// Clipping
307///////////////////////////////////////////////////////////////////////////////
308
Romain Guybb9524b2010-06-22 18:56:38 -0700309void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700310 const Rect& clip = mSnapshot->getMappedClip();
311 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700312}
313
314const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700315 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700316}
317
Romain Guyc7d53492010-06-25 13:41:57 -0700318bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
319 /*
320 * The documentation of quickReject() indicates that the specified rect
321 * is transformed before being compared to the clip rect. However, the
322 * clip rect is not stored transformed in the snapshot and can thus be
323 * compared directly
324 *
325 * The following code can be used instead to performed a mapped comparison:
326 *
327 * mSnapshot->transform.mapRect(r);
328 * const Rect& clip = mSnapshot->getMappedClip();
329 * return !clip.intersects(r);
330 */
Romain Guyc7d53492010-06-25 13:41:57 -0700331 Rect r(left, top, right, bottom);
332 return !mSnapshot->clipRect.intersects(r);
333}
334
Romain Guybb9524b2010-06-22 18:56:38 -0700335bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700336 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
337 if (clipped) {
338 mSnapshot->flags |= Snapshot::kFlagClipSet;
339 setScissorFromClip();
340 }
341 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700342}
343
Romain Guyf6a11b82010-06-23 17:47:49 -0700344///////////////////////////////////////////////////////////////////////////////
345// Drawing
346///////////////////////////////////////////////////////////////////////////////
347
Romain Guy85bf02f2010-06-22 13:11:24 -0700348void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700349 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700350 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700351}
Romain Guy9d5316e2010-06-24 19:30:36 -0700352
Romain Guybd6b79b2010-06-26 00:13:53 -0700353void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700354 SkXfermode::Mode mode;
355
356 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
357 if (!isMode) {
358 // Assume SRC_OVER
359 mode = SkXfermode::kSrcOver_Mode;
360 }
361
362 // Skia draws using the color's alpha channel if < 255
363 // Otherwise, it uses the paint's alpha
364 int color = p->getColor();
365 if (((color >> 24) & 0xFF) == 255) {
366 color |= p->getAlpha() << 24;
367 }
368
369 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700370}
Romain Guy9d5316e2010-06-24 19:30:36 -0700371
Romain Guy026c5e162010-06-28 17:12:22 -0700372void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
373 int color, SkXfermode::Mode mode) {
374 const int alpha = (color >> 24) & 0xFF;
375 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
376
377 const GLfloat a = alpha / 255.0f;
378 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
379 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
380 const GLfloat b = ((color ) & 0xFF) / 255.0f;
381
382 if (blend) {
383 glEnable(GL_BLEND);
384 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
385 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700386
Romain Guyc7d53492010-06-25 13:41:57 -0700387 mModelView.loadTranslate(left, top, 0.0f);
388 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700389
Romain Guyc7d53492010-06-25 13:41:57 -0700390 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700391
Romain Guyc7d53492010-06-25 13:41:57 -0700392 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700393
Romain Guyc7d53492010-06-25 13:41:57 -0700394 glEnableVertexAttribArray(mDrawColorShader->position);
395 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
396 gDrawColorVertexStride, p);
397 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700398
Romain Guyc7d53492010-06-25 13:41:57 -0700399 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700400
Romain Guyc7d53492010-06-25 13:41:57 -0700401 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700402
403 if (blend) {
404 glDisable(GL_BLEND);
405 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700406}
407
Romain Guybd6b79b2010-06-26 00:13:53 -0700408void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
409 GLuint texture, float alpha) {
410 mModelView.loadTranslate(left, top, 0.0f);
411 mModelView.scale(right - left, bottom - top, 1.0f);
412
413 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
414
Romain Guy5cbbce52010-06-27 22:59:20 -0700415 // TODO Correctly set the blend function, based on texture format and xfermode
Romain Guybd6b79b2010-06-26 00:13:53 -0700416 glEnable(GL_BLEND);
Romain Guy026c5e162010-06-28 17:12:22 -0700417 // For not pre-multiplied sources
418 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Romain Guybd6b79b2010-06-26 00:13:53 -0700419
420 glBindTexture(GL_TEXTURE_2D, texture);
421
422 glActiveTexture(GL_TEXTURE0);
423 glUniform1i(mDrawTextureShader->sampler, 0);
424
Romain Guy026c5e162010-06-28 17:12:22 -0700425 const GLvoid* p = &mDrawTextureVertices[0].position[0];
426 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700427
428 glEnableVertexAttribArray(mDrawTextureShader->position);
429 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
430 gDrawTextureVertexStride, p);
431
432 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
433 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
434 gDrawTextureVertexStride, t);
435
436 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
437
438 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
439
440 glDisableVertexAttribArray(mDrawTextureShader->position);
441 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
442
443 glBindTexture(GL_TEXTURE_2D, 0);
444 glDisable(GL_BLEND);
445}
446
Romain Guy026c5e162010-06-28 17:12:22 -0700447void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
448 mDrawTextureVertices[0].texture[0] = u1;
449 mDrawTextureVertices[0].texture[1] = v2;
450 mDrawTextureVertices[1].texture[0] = u2;
451 mDrawTextureVertices[1].texture[1] = v2;
452 mDrawTextureVertices[2].texture[0] = u1;
453 mDrawTextureVertices[2].texture[1] = v1;
454 mDrawTextureVertices[3].texture[0] = u2;
455 mDrawTextureVertices[3].texture[1] = v1;
456}
457
Romain Guy9d5316e2010-06-24 19:30:36 -0700458}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700459}; // namespace android