blob: 8b4fb9bca24a046659cc638fa19e39eda66c40c4 [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 Guyf86ef572010-07-01 11:05:42 -0700114 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700115}
116
Romain Guy85bf02f2010-06-22 13:11:24 -0700117void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700118 mSnapshot = &mFirstSnapshot;
119 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700120
Romain Guy08ae3172010-06-21 19:35:50 -0700121 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700122
Romain Guy08ae3172010-06-21 19:35:50 -0700123 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
124 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700125
Romain Guy08ae3172010-06-21 19:35:50 -0700126 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700127 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700128
Romain Guybb9524b2010-06-22 18:56:38 -0700129 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
130}
131
Romain Guyf6a11b82010-06-23 17:47:49 -0700132///////////////////////////////////////////////////////////////////////////////
133// State management
134///////////////////////////////////////////////////////////////////////////////
135
Romain Guybb9524b2010-06-22 18:56:38 -0700136int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700137 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700138}
139
140int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700141 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700142}
143
144void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700145 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700146
Romain Guy7ae7ac42010-06-25 13:46:18 -0700147 if (restoreSnapshot()) {
148 setScissorFromClip();
149 }
Romain Guybb9524b2010-06-22 18:56:38 -0700150}
151
152void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700153 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700154
Romain Guy7ae7ac42010-06-25 13:46:18 -0700155 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700156
Romain Guy7ae7ac42010-06-25 13:46:18 -0700157 while (mSaveCount != saveCount - 1) {
158 restoreClip |= restoreSnapshot();
159 }
Romain Guybb9524b2010-06-22 18:56:38 -0700160
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 if (restoreClip) {
162 setScissorFromClip();
163 }
Romain Guybb9524b2010-06-22 18:56:38 -0700164}
165
166int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700167 mSnapshot = new Snapshot(mSnapshot);
168 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700169}
170
171bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700172 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700173 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700174 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700175
Romain Guybd6b79b2010-06-26 00:13:53 -0700176 sp<Snapshot> current = mSnapshot;
177 sp<Snapshot> previous = mSnapshot->previous;
178
Romain Guyf86ef572010-07-01 11:05:42 -0700179 if (restoreOrtho) {
180 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
181 }
182
Romain Guybd6b79b2010-06-26 00:13:53 -0700183 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700184 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700185 }
186
187 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700188 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700189
Romain Guy7ae7ac42010-06-25 13:46:18 -0700190 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700191}
192
Romain Guyd55a8612010-06-28 17:42:46 -0700193void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
194 // Unbind current FBO and restore previous one
195 // Most of the time, previous->fbo will be 0 to bind the default buffer
196 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
197
198 // Restore the clip from the previous snapshot
199 const Rect& clip = previous->getMappedClip();
200 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
201
202 // Compute the correct texture coordinates for the FBO texture
203 // The texture is currently as big as the window but drawn with
204 // a quad of the appropriate size
205 const Rect& layer = current->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700206
207 drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700208 current->texture, current->alpha, current->mode, true, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700209
Romain Guyf86ef572010-07-01 11:05:42 -0700210 // TODO Don't delete these things, but cache them
Romain Guyd55a8612010-06-28 17:42:46 -0700211 glDeleteFramebuffers(1, &current->fbo);
212 glDeleteTextures(1, &current->texture);
213}
214
Romain Guyf6a11b82010-06-23 17:47:49 -0700215///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700216// Layers
217///////////////////////////////////////////////////////////////////////////////
218
219int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
220 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700221 int count = saveSnapshot();
222
223 int alpha = 255;
224 SkXfermode::Mode mode;
225
226 if (p) {
227 alpha = p->getAlpha();
228 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
229 if (!isMode) {
230 // Assume SRC_OVER
231 mode = SkXfermode::kSrcOver_Mode;
232 }
233 } else {
234 mode = SkXfermode::kSrcOver_Mode;
235 }
236
237 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
238
239 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700240}
241
242int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
243 int alpha, int flags) {
244 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700245 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
246 return count;
247}
Romain Guybd6b79b2010-06-26 00:13:53 -0700248
Romain Guyd55a8612010-06-28 17:42:46 -0700249bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
250 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700251 // Generate the FBO and attach the texture
Romain Guyd55a8612010-06-28 17:42:46 -0700252 glGenFramebuffers(1, &snapshot->fbo);
253 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guybd6b79b2010-06-26 00:13:53 -0700254
255 // Generate the texture in which the FBO will draw
Romain Guyd55a8612010-06-28 17:42:46 -0700256 glGenTextures(1, &snapshot->texture);
257 glBindTexture(GL_TEXTURE_2D, snapshot->texture);
Romain Guybd6b79b2010-06-26 00:13:53 -0700258
259 // The FBO will not be scaled, so we can use lower quality filtering
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy8ba548f2010-06-30 19:21:21 -0700262
Romain Guy5cbbce52010-06-27 22:59:20 -0700263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
264 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700265
Romain Guy8ba548f2010-06-30 19:21:21 -0700266 // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
Romain Guyf86ef572010-07-01 11:05:42 -0700267 // TODO Use an FBO cache
Romain Guybd6b79b2010-06-26 00:13:53 -0700268
Romain Guyf86ef572010-07-01 11:05:42 -0700269 const GLsizei width = right - left;
270 const GLsizei height = bottom - top;
Romain Guybd6b79b2010-06-26 00:13:53 -0700271
272 const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
273 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
274 glBindTexture(GL_TEXTURE_2D, 0);
275
276 // Bind texture to FBO
277 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyd55a8612010-06-28 17:42:46 -0700278 snapshot->texture, 0);
Romain Guybd6b79b2010-06-26 00:13:53 -0700279
280 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
281 if (status != GL_FRAMEBUFFER_COMPLETE) {
Romain Guyf86ef572010-07-01 11:05:42 -0700282 LOGD("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guybd6b79b2010-06-26 00:13:53 -0700283
Romain Guyd55a8612010-06-28 17:42:46 -0700284 glDeleteFramebuffers(1, &snapshot->fbo);
285 glDeleteTextures(1, &snapshot->texture);
286
287 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700288 }
289
Romain Guyf86ef572010-07-01 11:05:42 -0700290 // Clear the FBO
291 glDisable(GL_SCISSOR_TEST);
292 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
293 glClear(GL_COLOR_BUFFER_BIT);
294 glEnable(GL_SCISSOR_TEST);
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
Romain Guyf86ef572010-07-01 11:05:42 -0700301 // Creates a new snapshot to draw into the FBO
302 saveSnapshot();
303 // TODO: This doesn't preserve other transformations (check Skia first)
304 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
305 mSnapshot->clipRect.set(left, top, right, bottom);
306 mSnapshot->height = bottom - top;
307 setScissorFromClip();
308
309 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
310 Snapshot::kFlagClipSet;
311 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
312
313 // Change the ortho projection
314 mat4 ortho;
315 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
316 ortho.copyTo(mOrthoMatrix);
317
Romain Guyd55a8612010-06-28 17:42:46 -0700318 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700319}
320
321///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700322// Transforms
323///////////////////////////////////////////////////////////////////////////////
324
325void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700326 mSnapshot->transform.translate(dx, dy, 0.0f);
327 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700328}
329
330void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700331 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
332 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700333}
334
335void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700336 mSnapshot->transform.scale(sx, sy, 1.0f);
337 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700338}
339
340void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700341 mSnapshot->transform.load(*matrix);
342 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700343}
344
345void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700346 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700347}
348
349void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700350 mat4 m(*matrix);
351 mSnapshot->transform.multiply(m);
352 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700353}
354
355///////////////////////////////////////////////////////////////////////////////
356// Clipping
357///////////////////////////////////////////////////////////////////////////////
358
Romain Guybb9524b2010-06-22 18:56:38 -0700359void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700360 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700361 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700362}
363
364const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700365 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700366}
367
Romain Guyc7d53492010-06-25 13:41:57 -0700368bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
369 /*
370 * The documentation of quickReject() indicates that the specified rect
371 * is transformed before being compared to the clip rect. However, the
372 * clip rect is not stored transformed in the snapshot and can thus be
373 * compared directly
374 *
375 * The following code can be used instead to performed a mapped comparison:
376 *
377 * mSnapshot->transform.mapRect(r);
378 * const Rect& clip = mSnapshot->getMappedClip();
379 * return !clip.intersects(r);
380 */
Romain Guyc7d53492010-06-25 13:41:57 -0700381 Rect r(left, top, right, bottom);
382 return !mSnapshot->clipRect.intersects(r);
383}
384
Romain Guybb9524b2010-06-22 18:56:38 -0700385bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700386 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
387 if (clipped) {
388 mSnapshot->flags |= Snapshot::kFlagClipSet;
389 setScissorFromClip();
390 }
391 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700392}
393
Romain Guyf6a11b82010-06-23 17:47:49 -0700394///////////////////////////////////////////////////////////////////////////////
395// Drawing
396///////////////////////////////////////////////////////////////////////////////
397
Romain Guyc1396e92010-06-30 17:56:19 -0700398void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700399 const Texture* texture = mTextureCache.get(bitmap);
Romain Guyc1396e92010-06-30 17:56:19 -0700400
Romain Guyc1396e92010-06-30 17:56:19 -0700401 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700402 SkXfermode::Mode mode;
403 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700404
405 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
406 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700407}
408
Romain Guyf86ef572010-07-01 11:05:42 -0700409void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
410 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
411 const mat4 transform(*matrix);
412 transform.mapRect(r);
413
414 const Texture* texture = mTextureCache.get(bitmap);
415
416 int alpha;
417 SkXfermode::Mode mode;
418 getAlphaAndMode(paint, &alpha, &mode);
419
420 drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
421 alpha / 255.0f, mode, texture->blend, true);
422}
423
Romain Guy8ba548f2010-06-30 19:21:21 -0700424void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
425 float srcLeft, float srcTop, float srcRight, float srcBottom,
426 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700427 const SkPaint* paint) {
428 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700429
430 int alpha;
431 SkXfermode::Mode mode;
432 getAlphaAndMode(paint, &alpha, &mode);
433
434 const float width = texture->width;
435 const float height = texture->height;
436
437 const float u1 = srcLeft / width;
438 const float v1 = srcTop / height;
439 const float u2 = srcRight / width;
440 const float v2 = srcBottom / height;
441
442 resetDrawTextureTexCoords(u1, v1, u2, v2);
443
Romain Guy8ba548f2010-06-30 19:21:21 -0700444 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
445 alpha / 255.0f, mode, texture->blend, true);
446
447 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
448}
449
Romain Guy85bf02f2010-06-22 13:11:24 -0700450void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700451 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700452 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700453}
Romain Guy9d5316e2010-06-24 19:30:36 -0700454
Romain Guybd6b79b2010-06-26 00:13:53 -0700455void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700456 SkXfermode::Mode mode;
457
458 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
459 if (!isMode) {
460 // Assume SRC_OVER
461 mode = SkXfermode::kSrcOver_Mode;
462 }
463
464 // Skia draws using the color's alpha channel if < 255
465 // Otherwise, it uses the paint's alpha
466 int color = p->getColor();
467 if (((color >> 24) & 0xFF) == 255) {
468 color |= p->getAlpha() << 24;
469 }
470
471 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700472}
Romain Guy9d5316e2010-06-24 19:30:36 -0700473
Romain Guy026c5e162010-06-28 17:12:22 -0700474void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
475 int color, SkXfermode::Mode mode) {
476 const int alpha = (color >> 24) & 0xFF;
477 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
478
479 const GLfloat a = alpha / 255.0f;
480 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
481 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
482 const GLfloat b = ((color ) & 0xFF) / 255.0f;
483
484 if (blend) {
485 glEnable(GL_BLEND);
486 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
487 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700488
Romain Guyc7d53492010-06-25 13:41:57 -0700489 mModelView.loadTranslate(left, top, 0.0f);
490 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700491
Romain Guyc7d53492010-06-25 13:41:57 -0700492 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700493
Romain Guyc7d53492010-06-25 13:41:57 -0700494 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700495
Romain Guyc7d53492010-06-25 13:41:57 -0700496 glEnableVertexAttribArray(mDrawColorShader->position);
497 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
498 gDrawColorVertexStride, p);
499 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700500
Romain Guyc7d53492010-06-25 13:41:57 -0700501 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700502
Romain Guyc7d53492010-06-25 13:41:57 -0700503 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700504
505 if (blend) {
506 glDisable(GL_BLEND);
507 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700508}
509
Romain Guybd6b79b2010-06-26 00:13:53 -0700510void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700511 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700512 mModelView.loadTranslate(left, top, 0.0f);
513 mModelView.scale(right - left, bottom - top, 1.0f);
514
515 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
516
Romain Guyc1396e92010-06-30 17:56:19 -0700517 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
518 GLenum sourceMode = gBlends[mode].src;
519 if (!isPremultiplied && sourceMode == GL_ONE) {
520 sourceMode = GL_SRC_ALPHA;
521 }
522
523 glEnable(GL_BLEND);
524 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700525 }
526
Romain Guybd6b79b2010-06-26 00:13:53 -0700527 glBindTexture(GL_TEXTURE_2D, texture);
528
Romain Guyc1396e92010-06-30 17:56:19 -0700529 // TODO handle tiling and filtering here
530
Romain Guybd6b79b2010-06-26 00:13:53 -0700531 glActiveTexture(GL_TEXTURE0);
532 glUniform1i(mDrawTextureShader->sampler, 0);
533
Romain Guy026c5e162010-06-28 17:12:22 -0700534 const GLvoid* p = &mDrawTextureVertices[0].position[0];
535 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700536
537 glEnableVertexAttribArray(mDrawTextureShader->position);
538 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
539 gDrawTextureVertexStride, p);
540
541 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
542 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
543 gDrawTextureVertexStride, t);
544
545 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
546
547 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
548
549 glDisableVertexAttribArray(mDrawTextureShader->position);
550 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
551
552 glBindTexture(GL_TEXTURE_2D, 0);
553 glDisable(GL_BLEND);
554}
555
Romain Guy026c5e162010-06-28 17:12:22 -0700556void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
557 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700558 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700559 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700560 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700561 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700562 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700563 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700564 mDrawTextureVertices[3].texture[1] = v2;
565}
566
567void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
568 if (paint) {
569 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
570 if (!isMode) {
571 // Assume SRC_OVER
572 *mode = SkXfermode::kSrcOver_Mode;
573 }
574
575 // Skia draws using the color's alpha channel if < 255
576 // Otherwise, it uses the paint's alpha
577 int color = paint->getColor();
578 *alpha = (color >> 24) & 0xFF;
579 if (*alpha == 255) {
580 *alpha = paint->getAlpha();
581 }
582 } else {
583 *mode = SkXfermode::kSrcOver_Mode;
584 *alpha = 255;
585 }
Romain Guy026c5e162010-06-28 17:12:22 -0700586}
587
Romain Guy9d5316e2010-06-24 19:30:36 -0700588}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700589}; // namespace android