blob: 091abb058c9ee237bb2657756bc2259dc9e0ae53 [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 Guyc1396e92010-06-30 17:56:19 -070057 FV(0.0f, 0.0f, 0.0f, 0.0f),
58 FV(1.0f, 0.0f, 1.0f, 0.0f),
59 FV(0.0f, 1.0f, 0.0f, 1.0f),
60 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070061};
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
Romain Guy8ba548f2010-06-30 19:21:21 -0700208 resetDrawTextureTexCoords(u1, v1, u2, v2);
Romain Guyd55a8612010-06-28 17:42:46 -0700209
210 drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700211 current->texture, current->alpha, current->mode, true, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700212
Romain Guy8ba548f2010-06-30 19:21:21 -0700213 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyd55a8612010-06-28 17:42:46 -0700214
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 Guy8ba548f2010-06-30 19:21:21 -0700266
Romain Guy5cbbce52010-06-27 22:59:20 -0700267 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
268 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700269
Romain Guy8ba548f2010-06-30 19:21:21 -0700270 // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
Romain Guybd6b79b2010-06-26 00:13:53 -0700271 // TODO ***** IMPORTANT *****
272 // Creating a texture-backed FBO works only if the texture is the same size
273 // as the original rendering buffer (in this case, mWidth and mHeight.)
274 // This is expensive and wasteful and must be fixed.
Romain Guy5cbbce52010-06-27 22:59:20 -0700275 // TODO Additionally we should use an FBO cache
Romain Guybd6b79b2010-06-26 00:13:53 -0700276
277 const GLsizei width = mWidth; //right - left;
278 const GLsizei height = mHeight; //bottom - right;
279
280 const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
281 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
282 glBindTexture(GL_TEXTURE_2D, 0);
283
284 // Bind texture to FBO
285 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyd55a8612010-06-28 17:42:46 -0700286 snapshot->texture, 0);
Romain Guybd6b79b2010-06-26 00:13:53 -0700287
288 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
289 if (status != GL_FRAMEBUFFER_COMPLETE) {
290 LOGD("Framebuffer incomplete %d", status);
291
Romain Guyd55a8612010-06-28 17:42:46 -0700292 glDeleteFramebuffers(1, &snapshot->fbo);
293 glDeleteTextures(1, &snapshot->texture);
294
295 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700296 }
297
Romain Guyd55a8612010-06-28 17:42:46 -0700298 snapshot->flags |= Snapshot::kFlagIsLayer;
299 snapshot->mode = mode;
300 snapshot->alpha = alpha / 255.0f;
301 snapshot->layer.set(left, top, right, bottom);
302
303 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700304}
305
306///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700307// Transforms
308///////////////////////////////////////////////////////////////////////////////
309
310void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700311 mSnapshot->transform.translate(dx, dy, 0.0f);
312 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700313}
314
315void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700316 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
317 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700318}
319
320void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700321 mSnapshot->transform.scale(sx, sy, 1.0f);
322 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700323}
324
325void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700326 mSnapshot->transform.load(*matrix);
327 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700328}
329
330void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700331 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700332}
333
334void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700335 mat4 m(*matrix);
336 mSnapshot->transform.multiply(m);
337 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700338}
339
340///////////////////////////////////////////////////////////////////////////////
341// Clipping
342///////////////////////////////////////////////////////////////////////////////
343
Romain Guybb9524b2010-06-22 18:56:38 -0700344void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700345 const Rect& clip = mSnapshot->getMappedClip();
346 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700347}
348
349const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700350 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700351}
352
Romain Guyc7d53492010-06-25 13:41:57 -0700353bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
354 /*
355 * The documentation of quickReject() indicates that the specified rect
356 * is transformed before being compared to the clip rect. However, the
357 * clip rect is not stored transformed in the snapshot and can thus be
358 * compared directly
359 *
360 * The following code can be used instead to performed a mapped comparison:
361 *
362 * mSnapshot->transform.mapRect(r);
363 * const Rect& clip = mSnapshot->getMappedClip();
364 * return !clip.intersects(r);
365 */
Romain Guyc7d53492010-06-25 13:41:57 -0700366 Rect r(left, top, right, bottom);
367 return !mSnapshot->clipRect.intersects(r);
368}
369
Romain Guybb9524b2010-06-22 18:56:38 -0700370bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700371 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
372 if (clipped) {
373 mSnapshot->flags |= Snapshot::kFlagClipSet;
374 setScissorFromClip();
375 }
376 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700377}
378
Romain Guyf6a11b82010-06-23 17:47:49 -0700379///////////////////////////////////////////////////////////////////////////////
380// Drawing
381///////////////////////////////////////////////////////////////////////////////
382
Romain Guyc1396e92010-06-30 17:56:19 -0700383void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
384 Texture* texture = mTextureCache.get(bitmap);
385
Romain Guyc1396e92010-06-30 17:56:19 -0700386 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700387 SkXfermode::Mode mode;
388 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700389
390 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
391 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700392}
393
Romain Guy8ba548f2010-06-30 19:21:21 -0700394void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
395 float srcLeft, float srcTop, float srcRight, float srcBottom,
396 float dstLeft, float dstTop, float dstRight, float dstBottom,
397 const SkMatrix* matrix, const SkPaint* paint) {
398 Texture* texture = mTextureCache.get(bitmap);
399
400 int alpha;
401 SkXfermode::Mode mode;
402 getAlphaAndMode(paint, &alpha, &mode);
403
404 const float width = texture->width;
405 const float height = texture->height;
406
407 const float u1 = srcLeft / width;
408 const float v1 = srcTop / height;
409 const float u2 = srcRight / width;
410 const float v2 = srcBottom / height;
411
412 resetDrawTextureTexCoords(u1, v1, u2, v2);
413
414 // TODO: implement Matrix
415
416 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
417 alpha / 255.0f, mode, texture->blend, true);
418
419 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
420}
421
Romain Guy85bf02f2010-06-22 13:11:24 -0700422void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700423 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700424 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700425}
Romain Guy9d5316e2010-06-24 19:30:36 -0700426
Romain Guybd6b79b2010-06-26 00:13:53 -0700427void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700428 SkXfermode::Mode mode;
429
430 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
431 if (!isMode) {
432 // Assume SRC_OVER
433 mode = SkXfermode::kSrcOver_Mode;
434 }
435
436 // Skia draws using the color's alpha channel if < 255
437 // Otherwise, it uses the paint's alpha
438 int color = p->getColor();
439 if (((color >> 24) & 0xFF) == 255) {
440 color |= p->getAlpha() << 24;
441 }
442
443 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700444}
Romain Guy9d5316e2010-06-24 19:30:36 -0700445
Romain Guy026c5e162010-06-28 17:12:22 -0700446void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
447 int color, SkXfermode::Mode mode) {
448 const int alpha = (color >> 24) & 0xFF;
449 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
450
451 const GLfloat a = alpha / 255.0f;
452 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
453 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
454 const GLfloat b = ((color ) & 0xFF) / 255.0f;
455
456 if (blend) {
457 glEnable(GL_BLEND);
458 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
459 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700460
Romain Guyc7d53492010-06-25 13:41:57 -0700461 mModelView.loadTranslate(left, top, 0.0f);
462 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700463
Romain Guyc7d53492010-06-25 13:41:57 -0700464 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700465
Romain Guyc7d53492010-06-25 13:41:57 -0700466 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700467
Romain Guyc7d53492010-06-25 13:41:57 -0700468 glEnableVertexAttribArray(mDrawColorShader->position);
469 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
470 gDrawColorVertexStride, p);
471 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700472
Romain Guyc7d53492010-06-25 13:41:57 -0700473 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700474
Romain Guyc7d53492010-06-25 13:41:57 -0700475 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700476
477 if (blend) {
478 glDisable(GL_BLEND);
479 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700480}
481
Romain Guybd6b79b2010-06-26 00:13:53 -0700482void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700483 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700484 mModelView.loadTranslate(left, top, 0.0f);
485 mModelView.scale(right - left, bottom - top, 1.0f);
486
487 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
488
Romain Guyc1396e92010-06-30 17:56:19 -0700489 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
490 GLenum sourceMode = gBlends[mode].src;
491 if (!isPremultiplied && sourceMode == GL_ONE) {
492 sourceMode = GL_SRC_ALPHA;
493 }
494
495 glEnable(GL_BLEND);
496 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700497 }
498
Romain Guybd6b79b2010-06-26 00:13:53 -0700499 glBindTexture(GL_TEXTURE_2D, texture);
500
Romain Guyc1396e92010-06-30 17:56:19 -0700501 // TODO handle tiling and filtering here
502
Romain Guybd6b79b2010-06-26 00:13:53 -0700503 glActiveTexture(GL_TEXTURE0);
504 glUniform1i(mDrawTextureShader->sampler, 0);
505
Romain Guy026c5e162010-06-28 17:12:22 -0700506 const GLvoid* p = &mDrawTextureVertices[0].position[0];
507 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700508
509 glEnableVertexAttribArray(mDrawTextureShader->position);
510 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
511 gDrawTextureVertexStride, p);
512
513 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
514 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
515 gDrawTextureVertexStride, t);
516
517 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
518
519 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
520
521 glDisableVertexAttribArray(mDrawTextureShader->position);
522 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
523
524 glBindTexture(GL_TEXTURE_2D, 0);
525 glDisable(GL_BLEND);
526}
527
Romain Guy026c5e162010-06-28 17:12:22 -0700528void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
529 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700530 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700531 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700532 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700533 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700534 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700535 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700536 mDrawTextureVertices[3].texture[1] = v2;
537}
538
539void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
540 if (paint) {
541 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
542 if (!isMode) {
543 // Assume SRC_OVER
544 *mode = SkXfermode::kSrcOver_Mode;
545 }
546
547 // Skia draws using the color's alpha channel if < 255
548 // Otherwise, it uses the paint's alpha
549 int color = paint->getColor();
550 *alpha = (color >> 24) & 0xFF;
551 if (*alpha == 255) {
552 *alpha = paint->getAlpha();
553 }
554 } else {
555 *mode = SkXfermode::kSrcOver_Mode;
556 *alpha = 255;
557 }
Romain Guy026c5e162010-06-28 17:12:22 -0700558}
559
Romain Guy9d5316e2010-06-24 19:30:36 -0700560}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700561}; // namespace android