blob: d80238599648ad4442375b67df8423a136dcabd8 [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 Guyce0537b2010-06-29 21:05:21 -070036#define MAX_TEXTURE_COUNT 128
37
Romain Guybd6b79b2010-06-26 00:13:53 -070038#define SV(x, y) { { x, y } }
39#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070040
41///////////////////////////////////////////////////////////////////////////////
42// Globals
43///////////////////////////////////////////////////////////////////////////////
44
Romain Guy026c5e162010-06-28 17:12:22 -070045static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070046 SV(0.0f, 0.0f),
47 SV(1.0f, 0.0f),
48 SV(0.0f, 1.0f),
49 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070050};
Romain Guy026c5e162010-06-28 17:12:22 -070051static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
52static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070053
Romain Guy026c5e162010-06-28 17:12:22 -070054// This array is never used directly but used as a memcpy source in the
55// OpenGLRenderer constructor
56static const TextureVertex gDrawTextureVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070057 FV(0.0f, 0.0f, 0.0f, 1.0f),
58 FV(1.0f, 0.0f, 1.0f, 1.0f),
59 FV(0.0f, 1.0f, 0.0f, 0.0f),
60 FV(1.0f, 1.0f, 1.0f, 0.0f)
61};
Romain Guy026c5e162010-06-28 17:12:22 -070062static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
63static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070064
Romain Guy026c5e162010-06-28 17:12:22 -070065// In this array, the index of each Blender equals the value of the first
66// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
67static const Blender gBlends[] = {
68 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
69 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
70 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
71 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
73 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
74 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
75 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
78 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
79 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
80};
Romain Guye4d01122010-06-16 18:44:05 -070081
Romain Guyf6a11b82010-06-23 17:47:49 -070082///////////////////////////////////////////////////////////////////////////////
83// Constructors/destructor
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guyce0537b2010-06-29 21:05:21 -070086OpenGLRenderer::OpenGLRenderer(): mTextureCache(MAX_TEXTURE_COUNT) {
Romain Guy85bf02f2010-06-22 13:11:24 -070087 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070088
89 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -070090 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -070091
92 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -070093}
94
Romain Guy85bf02f2010-06-22 13:11:24 -070095OpenGLRenderer::~OpenGLRenderer() {
96 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -070097
98 mTextureCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -070099}
100
Romain Guyf6a11b82010-06-23 17:47:49 -0700101///////////////////////////////////////////////////////////////////////////////
102// Setup
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guy85bf02f2010-06-22 13:11:24 -0700105void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700106 glViewport(0, 0, width, height);
107
108 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700109 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700110 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700111
112 mWidth = width;
113 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -0700114}
115
Romain Guy85bf02f2010-06-22 13:11:24 -0700116void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700117 mSnapshot = &mFirstSnapshot;
118 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700119
Romain Guy08ae3172010-06-21 19:35:50 -0700120 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700121
Romain Guy08ae3172010-06-21 19:35:50 -0700122 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
123 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700124
Romain Guy08ae3172010-06-21 19:35:50 -0700125 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700126 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700127
Romain Guybb9524b2010-06-22 18:56:38 -0700128 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
129}
130
Romain Guyf6a11b82010-06-23 17:47:49 -0700131///////////////////////////////////////////////////////////////////////////////
132// State management
133///////////////////////////////////////////////////////////////////////////////
134
Romain Guybb9524b2010-06-22 18:56:38 -0700135int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700136 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700137}
138
139int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700140 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700141}
142
143void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700144 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700145
Romain Guy7ae7ac42010-06-25 13:46:18 -0700146 if (restoreSnapshot()) {
147 setScissorFromClip();
148 }
Romain Guybb9524b2010-06-22 18:56:38 -0700149}
150
151void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700152 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700153
Romain Guy7ae7ac42010-06-25 13:46:18 -0700154 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700155
Romain Guy7ae7ac42010-06-25 13:46:18 -0700156 while (mSaveCount != saveCount - 1) {
157 restoreClip |= restoreSnapshot();
158 }
Romain Guybb9524b2010-06-22 18:56:38 -0700159
Romain Guy7ae7ac42010-06-25 13:46:18 -0700160 if (restoreClip) {
161 setScissorFromClip();
162 }
Romain Guybb9524b2010-06-22 18:56:38 -0700163}
164
165int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700166 mSnapshot = new Snapshot(mSnapshot);
167 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700168}
169
170bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700171 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700172 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guybb9524b2010-06-22 18:56:38 -0700173
Romain Guybd6b79b2010-06-26 00:13:53 -0700174 sp<Snapshot> current = mSnapshot;
175 sp<Snapshot> previous = mSnapshot->previous;
176
177 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700178 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700179 }
180
181 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700182 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700183
Romain Guy7ae7ac42010-06-25 13:46:18 -0700184 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700185}
186
Romain Guyd55a8612010-06-28 17:42:46 -0700187void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
188 // Unbind current FBO and restore previous one
189 // Most of the time, previous->fbo will be 0 to bind the default buffer
190 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
191
192 // Restore the clip from the previous snapshot
193 const Rect& clip = previous->getMappedClip();
194 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
195
196 // Compute the correct texture coordinates for the FBO texture
197 // The texture is currently as big as the window but drawn with
198 // a quad of the appropriate size
199 const Rect& layer = current->layer;
200 Rect texCoords(current->layer);
201 mSnapshot->transform.mapRect(texCoords);
202
203 const float u1 = texCoords.left / float(mWidth);
204 const float v1 = (mHeight - texCoords.top) / float(mHeight);
205 const float u2 = texCoords.right / float(mWidth);
206 const float v2 = (mHeight - texCoords.bottom) / float(mHeight);
207
208 resetDrawTextureTexCoords(u1, v1, u2, v1);
209
210 drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
211 current->texture, current->alpha, current->mode, true);
212
213 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
214
215 glDeleteFramebuffers(1, &current->fbo);
216 glDeleteTextures(1, &current->texture);
217}
218
Romain Guyf6a11b82010-06-23 17:47:49 -0700219///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700220// Layers
221///////////////////////////////////////////////////////////////////////////////
222
223int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
224 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700225 int count = saveSnapshot();
226
227 int alpha = 255;
228 SkXfermode::Mode mode;
229
230 if (p) {
231 alpha = p->getAlpha();
232 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
233 if (!isMode) {
234 // Assume SRC_OVER
235 mode = SkXfermode::kSrcOver_Mode;
236 }
237 } else {
238 mode = SkXfermode::kSrcOver_Mode;
239 }
240
241 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
242
243 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700244}
245
246int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
247 int alpha, int flags) {
248 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700249 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
250 return count;
251}
Romain Guybd6b79b2010-06-26 00:13:53 -0700252
Romain Guyd55a8612010-06-28 17:42:46 -0700253bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
254 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700255 // Generate the FBO and attach the texture
Romain Guyd55a8612010-06-28 17:42:46 -0700256 glGenFramebuffers(1, &snapshot->fbo);
257 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guybd6b79b2010-06-26 00:13:53 -0700258
259 // Generate the texture in which the FBO will draw
Romain Guyd55a8612010-06-28 17:42:46 -0700260 glGenTextures(1, &snapshot->texture);
261 glBindTexture(GL_TEXTURE_2D, snapshot->texture);
Romain Guybd6b79b2010-06-26 00:13:53 -0700262
263 // The FBO will not be scaled, so we can use lower quality filtering
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
265 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy5cbbce52010-06-27 22:59:20 -0700266 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700268
269 // TODO ***** IMPORTANT *****
270 // Creating a texture-backed FBO works only if the texture is the same size
271 // as the original rendering buffer (in this case, mWidth and mHeight.)
272 // This is expensive and wasteful and must be fixed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700273 // TODO Additionally we should use an FBO cache
Romain Guybd6b79b2010-06-26 00:13:53 -0700274
275 const GLsizei width = mWidth; //right - left;
276 const GLsizei height = mHeight; //bottom - right;
277
278 const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
279 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
280 glBindTexture(GL_TEXTURE_2D, 0);
281
282 // Bind texture to FBO
283 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyd55a8612010-06-28 17:42:46 -0700284 snapshot->texture, 0);
Romain Guybd6b79b2010-06-26 00:13:53 -0700285
286 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
287 if (status != GL_FRAMEBUFFER_COMPLETE) {
288 LOGD("Framebuffer incomplete %d", status);
289
Romain Guyd55a8612010-06-28 17:42:46 -0700290 glDeleteFramebuffers(1, &snapshot->fbo);
291 glDeleteTextures(1, &snapshot->texture);
292
293 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700294 }
295
Romain Guyd55a8612010-06-28 17:42:46 -0700296 snapshot->flags |= Snapshot::kFlagIsLayer;
297 snapshot->mode = mode;
298 snapshot->alpha = alpha / 255.0f;
299 snapshot->layer.set(left, top, right, bottom);
300
301 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700302}
303
304///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700305// Transforms
306///////////////////////////////////////////////////////////////////////////////
307
308void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700309 mSnapshot->transform.translate(dx, dy, 0.0f);
310 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700311}
312
313void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700314 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
315 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700316}
317
318void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700319 mSnapshot->transform.scale(sx, sy, 1.0f);
320 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700321}
322
323void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700324 mSnapshot->transform.load(*matrix);
325 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700326}
327
328void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700329 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700330}
331
332void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700333 mat4 m(*matrix);
334 mSnapshot->transform.multiply(m);
335 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700336}
337
338///////////////////////////////////////////////////////////////////////////////
339// Clipping
340///////////////////////////////////////////////////////////////////////////////
341
Romain Guybb9524b2010-06-22 18:56:38 -0700342void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700343 const Rect& clip = mSnapshot->getMappedClip();
344 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700345}
346
347const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700348 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700349}
350
Romain Guyc7d53492010-06-25 13:41:57 -0700351bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
352 /*
353 * The documentation of quickReject() indicates that the specified rect
354 * is transformed before being compared to the clip rect. However, the
355 * clip rect is not stored transformed in the snapshot and can thus be
356 * compared directly
357 *
358 * The following code can be used instead to performed a mapped comparison:
359 *
360 * mSnapshot->transform.mapRect(r);
361 * const Rect& clip = mSnapshot->getMappedClip();
362 * return !clip.intersects(r);
363 */
Romain Guyc7d53492010-06-25 13:41:57 -0700364 Rect r(left, top, right, bottom);
365 return !mSnapshot->clipRect.intersects(r);
366}
367
Romain Guybb9524b2010-06-22 18:56:38 -0700368bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700369 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
370 if (clipped) {
371 mSnapshot->flags |= Snapshot::kFlagClipSet;
372 setScissorFromClip();
373 }
374 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700375}
376
Romain Guyf6a11b82010-06-23 17:47:49 -0700377///////////////////////////////////////////////////////////////////////////////
378// Drawing
379///////////////////////////////////////////////////////////////////////////////
380
Romain Guyce0537b2010-06-29 21:05:21 -0700381void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
382 LOGD("Drawing bitmap!");
383}
384
Romain Guy85bf02f2010-06-22 13:11:24 -0700385void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700386 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700387 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700388}
Romain Guy9d5316e2010-06-24 19:30:36 -0700389
Romain Guybd6b79b2010-06-26 00:13:53 -0700390void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700391 SkXfermode::Mode mode;
392
393 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
394 if (!isMode) {
395 // Assume SRC_OVER
396 mode = SkXfermode::kSrcOver_Mode;
397 }
398
399 // Skia draws using the color's alpha channel if < 255
400 // Otherwise, it uses the paint's alpha
401 int color = p->getColor();
402 if (((color >> 24) & 0xFF) == 255) {
403 color |= p->getAlpha() << 24;
404 }
405
406 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700407}
Romain Guy9d5316e2010-06-24 19:30:36 -0700408
Romain Guy026c5e162010-06-28 17:12:22 -0700409void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
410 int color, SkXfermode::Mode mode) {
411 const int alpha = (color >> 24) & 0xFF;
412 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
413
414 const GLfloat a = alpha / 255.0f;
415 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
416 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
417 const GLfloat b = ((color ) & 0xFF) / 255.0f;
418
419 if (blend) {
420 glEnable(GL_BLEND);
421 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
422 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700423
Romain Guyc7d53492010-06-25 13:41:57 -0700424 mModelView.loadTranslate(left, top, 0.0f);
425 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700426
Romain Guyc7d53492010-06-25 13:41:57 -0700427 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700428
Romain Guyc7d53492010-06-25 13:41:57 -0700429 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700430
Romain Guyc7d53492010-06-25 13:41:57 -0700431 glEnableVertexAttribArray(mDrawColorShader->position);
432 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
433 gDrawColorVertexStride, p);
434 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700435
Romain Guyc7d53492010-06-25 13:41:57 -0700436 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700437
Romain Guyc7d53492010-06-25 13:41:57 -0700438 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700439
440 if (blend) {
441 glDisable(GL_BLEND);
442 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700443}
444
Romain Guybd6b79b2010-06-26 00:13:53 -0700445void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyd55a8612010-06-28 17:42:46 -0700446 GLuint texture, float alpha, SkXfermode::Mode mode, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700447 mModelView.loadTranslate(left, top, 0.0f);
448 mModelView.scale(right - left, bottom - top, 1.0f);
449
450 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
451
Romain Guyd55a8612010-06-28 17:42:46 -0700452 GLenum sourceMode = gBlends[mode].src;
453 if (!isPremultiplied && sourceMode == GL_ONE) {
454 sourceMode = GL_SRC_ALPHA;
455 }
456
457 // TODO: Try to disable blending when the texture is opaque and alpha == 1.0f
Romain Guybd6b79b2010-06-26 00:13:53 -0700458 glEnable(GL_BLEND);
Romain Guyd55a8612010-06-28 17:42:46 -0700459 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guybd6b79b2010-06-26 00:13:53 -0700460
461 glBindTexture(GL_TEXTURE_2D, texture);
462
463 glActiveTexture(GL_TEXTURE0);
464 glUniform1i(mDrawTextureShader->sampler, 0);
465
Romain Guy026c5e162010-06-28 17:12:22 -0700466 const GLvoid* p = &mDrawTextureVertices[0].position[0];
467 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700468
469 glEnableVertexAttribArray(mDrawTextureShader->position);
470 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
471 gDrawTextureVertexStride, p);
472
473 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
474 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
475 gDrawTextureVertexStride, t);
476
477 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
478
479 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
480
481 glDisableVertexAttribArray(mDrawTextureShader->position);
482 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
483
484 glBindTexture(GL_TEXTURE_2D, 0);
485 glDisable(GL_BLEND);
486}
487
Romain Guy026c5e162010-06-28 17:12:22 -0700488void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
489 mDrawTextureVertices[0].texture[0] = u1;
490 mDrawTextureVertices[0].texture[1] = v2;
491 mDrawTextureVertices[1].texture[0] = u2;
492 mDrawTextureVertices[1].texture[1] = v2;
493 mDrawTextureVertices[2].texture[0] = u1;
494 mDrawTextureVertices[2].texture[1] = v1;
495 mDrawTextureVertices[3].texture[0] = u2;
496 mDrawTextureVertices[3].texture[1] = v1;
497}
498
Romain Guy9d5316e2010-06-24 19:30:36 -0700499}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700500}; // namespace android