blob: 095d58b81a89b7369efe40a4fc0c7142792367d6 [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 Guy121e22422010-07-01 18:26:52 -070025#include <cutils/properties.h>
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
27
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include "OpenGLRenderer.h"
Romain Guye4d01122010-06-16 18:44:05 -070029
30namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070031namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
Romain Guy121e22422010-07-01 18:26:52 -070037// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
40
41// Converts a number of mega-bytes into bytes
42#define MB(s) s * 1024 * 1024
43
44#define DEFAULT_TEXTURE_CACHE_SIZE MB(20)
45#define DEFAULT_LAYER_CACHE_SIZE MB(10)
Romain Guyce0537b2010-06-29 21:05:21 -070046
Romain Guybd6b79b2010-06-26 00:13:53 -070047#define SV(x, y) { { x, y } }
48#define FV(x, y, u, v) { { x, y }, { u, v } }
Romain Guy9d5316e2010-06-24 19:30:36 -070049
50///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
Romain Guy026c5e162010-06-28 17:12:22 -070054static const SimpleVertex gDrawColorVertices[] = {
Romain Guybd6b79b2010-06-26 00:13:53 -070055 SV(0.0f, 0.0f),
56 SV(1.0f, 0.0f),
57 SV(0.0f, 1.0f),
58 SV(1.0f, 1.0f)
Romain Guy9d5316e2010-06-24 19:30:36 -070059};
Romain Guy026c5e162010-06-28 17:12:22 -070060static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
61static const GLsizei gDrawColorVertexCount = 4;
Romain Guy9d5316e2010-06-24 19:30:36 -070062
Romain Guy026c5e162010-06-28 17:12:22 -070063// This array is never used directly but used as a memcpy source in the
64// OpenGLRenderer constructor
65static const TextureVertex gDrawTextureVertices[] = {
Romain Guyc1396e92010-06-30 17:56:19 -070066 FV(0.0f, 0.0f, 0.0f, 0.0f),
67 FV(1.0f, 0.0f, 1.0f, 0.0f),
68 FV(0.0f, 1.0f, 0.0f, 1.0f),
69 FV(1.0f, 1.0f, 1.0f, 1.0f)
Romain Guybd6b79b2010-06-26 00:13:53 -070070};
Romain Guy026c5e162010-06-28 17:12:22 -070071static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
72static const GLsizei gDrawTextureVertexCount = 4;
Romain Guybd6b79b2010-06-26 00:13:53 -070073
Romain Guy026c5e162010-06-28 17:12:22 -070074// In this array, the index of each Blender equals the value of the first
75// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
76static const Blender gBlends[] = {
77 { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO },
78 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
79 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
80 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
82 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
83 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
84 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
88 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }
89};
Romain Guye4d01122010-06-16 18:44:05 -070090
Romain Guyf6a11b82010-06-23 17:47:49 -070091///////////////////////////////////////////////////////////////////////////////
92// Constructors/destructor
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guy121e22422010-07-01 18:26:52 -070095OpenGLRenderer::OpenGLRenderer(): mTextureCache(DEFAULT_TEXTURE_CACHE_SIZE) {
Romain Guy85bf02f2010-06-22 13:11:24 -070096 LOGD("Create OpenGLRenderer");
Romain Guy9d5316e2010-06-24 19:30:36 -070097
Romain Guy121e22422010-07-01 18:26:52 -070098 char property[PROPERTY_VALUE_MAX];
99 if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
100 mTextureCache.setMaxSize(MB(atoi(property)));
101 }
102
Romain Guy9d5316e2010-06-24 19:30:36 -0700103 mDrawColorShader = new DrawColorProgram;
Romain Guybd6b79b2010-06-26 00:13:53 -0700104 mDrawTextureShader = new DrawTextureProgram;
Romain Guy026c5e162010-06-28 17:12:22 -0700105
106 memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700107}
108
Romain Guy85bf02f2010-06-22 13:11:24 -0700109OpenGLRenderer::~OpenGLRenderer() {
110 LOGD("Destroy OpenGLRenderer");
Romain Guyce0537b2010-06-29 21:05:21 -0700111
112 mTextureCache.clear();
Romain Guye4d01122010-06-16 18:44:05 -0700113}
114
Romain Guyf6a11b82010-06-23 17:47:49 -0700115///////////////////////////////////////////////////////////////////////////////
116// Setup
117///////////////////////////////////////////////////////////////////////////////
118
Romain Guy85bf02f2010-06-22 13:11:24 -0700119void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -0700120 glViewport(0, 0, width, height);
121
122 mat4 ortho;
Romain Guyc7d53492010-06-25 13:41:57 -0700123 ortho.loadOrtho(0, width, height, 0, -1, 1);
Romain Guy08ae3172010-06-21 19:35:50 -0700124 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -0700125
126 mWidth = width;
127 mHeight = height;
Romain Guyf86ef572010-07-01 11:05:42 -0700128 mFirstSnapshot.height = height;
Romain Guye4d01122010-06-16 18:44:05 -0700129}
130
Romain Guy85bf02f2010-06-22 13:11:24 -0700131void OpenGLRenderer::prepare() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700132 mSnapshot = &mFirstSnapshot;
133 mSaveCount = 0;
Romain Guyf6a11b82010-06-23 17:47:49 -0700134
Romain Guy08ae3172010-06-21 19:35:50 -0700135 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -0700136
Romain Guy08ae3172010-06-21 19:35:50 -0700137 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
138 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -0700139
Romain Guy08ae3172010-06-21 19:35:50 -0700140 glEnable(GL_SCISSOR_TEST);
Romain Guyc7d53492010-06-25 13:41:57 -0700141 glScissor(0, 0, mWidth, mHeight);
Romain Guyf6a11b82010-06-23 17:47:49 -0700142
Romain Guybb9524b2010-06-22 18:56:38 -0700143 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
144}
145
Romain Guyf6a11b82010-06-23 17:47:49 -0700146///////////////////////////////////////////////////////////////////////////////
147// State management
148///////////////////////////////////////////////////////////////////////////////
149
Romain Guybb9524b2010-06-22 18:56:38 -0700150int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700151 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700152}
153
154int OpenGLRenderer::save(int flags) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700155 return saveSnapshot();
Romain Guybb9524b2010-06-22 18:56:38 -0700156}
157
158void OpenGLRenderer::restore() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700159 if (mSaveCount == 0) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700160
Romain Guy7ae7ac42010-06-25 13:46:18 -0700161 if (restoreSnapshot()) {
162 setScissorFromClip();
163 }
Romain Guybb9524b2010-06-22 18:56:38 -0700164}
165
166void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700167 if (saveCount <= 0 || saveCount > mSaveCount) return;
Romain Guybb9524b2010-06-22 18:56:38 -0700168
Romain Guy7ae7ac42010-06-25 13:46:18 -0700169 bool restoreClip = false;
Romain Guybb9524b2010-06-22 18:56:38 -0700170
Romain Guy7ae7ac42010-06-25 13:46:18 -0700171 while (mSaveCount != saveCount - 1) {
172 restoreClip |= restoreSnapshot();
173 }
Romain Guybb9524b2010-06-22 18:56:38 -0700174
Romain Guy7ae7ac42010-06-25 13:46:18 -0700175 if (restoreClip) {
176 setScissorFromClip();
177 }
Romain Guybb9524b2010-06-22 18:56:38 -0700178}
179
180int OpenGLRenderer::saveSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700181 mSnapshot = new Snapshot(mSnapshot);
182 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700183}
184
185bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700186 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700187 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyf86ef572010-07-01 11:05:42 -0700188 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700189
Romain Guybd6b79b2010-06-26 00:13:53 -0700190 sp<Snapshot> current = mSnapshot;
191 sp<Snapshot> previous = mSnapshot->previous;
192
Romain Guyf86ef572010-07-01 11:05:42 -0700193 if (restoreOrtho) {
194 memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
195 }
196
Romain Guybd6b79b2010-06-26 00:13:53 -0700197 if (restoreLayer) {
Romain Guyd55a8612010-06-28 17:42:46 -0700198 composeLayer(current, previous);
Romain Guybd6b79b2010-06-26 00:13:53 -0700199 }
200
201 mSnapshot = previous;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700202 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700203
Romain Guy7ae7ac42010-06-25 13:46:18 -0700204 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700205}
206
Romain Guyd55a8612010-06-28 17:42:46 -0700207void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
208 // Unbind current FBO and restore previous one
209 // Most of the time, previous->fbo will be 0 to bind the default buffer
210 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
211
212 // Restore the clip from the previous snapshot
213 const Rect& clip = previous->getMappedClip();
214 glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
215
216 // Compute the correct texture coordinates for the FBO texture
217 // The texture is currently as big as the window but drawn with
218 // a quad of the appropriate size
219 const Rect& layer = current->layer;
Romain Guyd55a8612010-06-28 17:42:46 -0700220
221 drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700222 current->texture, current->alpha, current->mode, true, true);
Romain Guyd55a8612010-06-28 17:42:46 -0700223
Romain Guyf86ef572010-07-01 11:05:42 -0700224 // TODO Don't delete these things, but cache them
Romain Guyd55a8612010-06-28 17:42:46 -0700225 glDeleteFramebuffers(1, &current->fbo);
226 glDeleteTextures(1, &current->texture);
227}
228
Romain Guyf6a11b82010-06-23 17:47:49 -0700229///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700230// Layers
231///////////////////////////////////////////////////////////////////////////////
232
233int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
234 const SkPaint* p, int flags) {
Romain Guyd55a8612010-06-28 17:42:46 -0700235 int count = saveSnapshot();
236
237 int alpha = 255;
238 SkXfermode::Mode mode;
239
240 if (p) {
241 alpha = p->getAlpha();
242 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
243 if (!isMode) {
244 // Assume SRC_OVER
245 mode = SkXfermode::kSrcOver_Mode;
246 }
247 } else {
248 mode = SkXfermode::kSrcOver_Mode;
249 }
250
251 createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
252
253 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700254}
255
256int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
257 int alpha, int flags) {
258 int count = saveSnapshot();
Romain Guyd55a8612010-06-28 17:42:46 -0700259 createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
260 return count;
261}
Romain Guybd6b79b2010-06-26 00:13:53 -0700262
Romain Guyd55a8612010-06-28 17:42:46 -0700263bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
264 float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700265 // Generate the FBO and attach the texture
Romain Guyd55a8612010-06-28 17:42:46 -0700266 glGenFramebuffers(1, &snapshot->fbo);
267 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guybd6b79b2010-06-26 00:13:53 -0700268
269 // Generate the texture in which the FBO will draw
Romain Guyd55a8612010-06-28 17:42:46 -0700270 glGenTextures(1, &snapshot->texture);
271 glBindTexture(GL_TEXTURE_2D, snapshot->texture);
Romain Guybd6b79b2010-06-26 00:13:53 -0700272
273 // The FBO will not be scaled, so we can use lower quality filtering
274 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
Romain Guy8ba548f2010-06-30 19:21:21 -0700276
Romain Guy5cbbce52010-06-27 22:59:20 -0700277 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
278 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Romain Guybd6b79b2010-06-26 00:13:53 -0700279
Romain Guy8ba548f2010-06-30 19:21:21 -0700280 // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
Romain Guyf86ef572010-07-01 11:05:42 -0700281 // TODO Use an FBO cache
Romain Guybd6b79b2010-06-26 00:13:53 -0700282
Romain Guyf86ef572010-07-01 11:05:42 -0700283 const GLsizei width = right - left;
284 const GLsizei height = bottom - top;
Romain Guybd6b79b2010-06-26 00:13:53 -0700285
286 const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
287 glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
288 glBindTexture(GL_TEXTURE_2D, 0);
289
290 // Bind texture to FBO
291 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guyd55a8612010-06-28 17:42:46 -0700292 snapshot->texture, 0);
Romain Guybd6b79b2010-06-26 00:13:53 -0700293
294 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
295 if (status != GL_FRAMEBUFFER_COMPLETE) {
Romain Guyf86ef572010-07-01 11:05:42 -0700296 LOGD("Framebuffer incomplete (GL error code 0x%x)", status);
Romain Guybd6b79b2010-06-26 00:13:53 -0700297
Romain Guyd55a8612010-06-28 17:42:46 -0700298 glDeleteFramebuffers(1, &snapshot->fbo);
299 glDeleteTextures(1, &snapshot->texture);
300
301 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700302 }
303
Romain Guyf86ef572010-07-01 11:05:42 -0700304 // Clear the FBO
305 glDisable(GL_SCISSOR_TEST);
306 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
307 glClear(GL_COLOR_BUFFER_BIT);
308 glEnable(GL_SCISSOR_TEST);
309
Romain Guyd55a8612010-06-28 17:42:46 -0700310 snapshot->flags |= Snapshot::kFlagIsLayer;
311 snapshot->mode = mode;
312 snapshot->alpha = alpha / 255.0f;
313 snapshot->layer.set(left, top, right, bottom);
314
Romain Guyf86ef572010-07-01 11:05:42 -0700315 // Creates a new snapshot to draw into the FBO
316 saveSnapshot();
317 // TODO: This doesn't preserve other transformations (check Skia first)
318 mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
319 mSnapshot->clipRect.set(left, top, right, bottom);
320 mSnapshot->height = bottom - top;
321 setScissorFromClip();
322
323 mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
324 Snapshot::kFlagClipSet;
325 memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
326
327 // Change the ortho projection
328 mat4 ortho;
329 ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
330 ortho.copyTo(mOrthoMatrix);
331
Romain Guyd55a8612010-06-28 17:42:46 -0700332 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700333}
334
335///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700336// Transforms
337///////////////////////////////////////////////////////////////////////////////
338
339void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700340 mSnapshot->transform.translate(dx, dy, 0.0f);
341 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700342}
343
344void OpenGLRenderer::rotate(float degrees) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700345 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
346 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700347}
348
349void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700350 mSnapshot->transform.scale(sx, sy, 1.0f);
351 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700352}
353
354void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700355 mSnapshot->transform.load(*matrix);
356 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700357}
358
359void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700360 mSnapshot->transform.copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700361}
362
363void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700364 mat4 m(*matrix);
365 mSnapshot->transform.multiply(m);
366 mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
Romain Guyf6a11b82010-06-23 17:47:49 -0700367}
368
369///////////////////////////////////////////////////////////////////////////////
370// Clipping
371///////////////////////////////////////////////////////////////////////////////
372
Romain Guybb9524b2010-06-22 18:56:38 -0700373void OpenGLRenderer::setScissorFromClip() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700374 const Rect& clip = mSnapshot->getMappedClip();
Romain Guyf86ef572010-07-01 11:05:42 -0700375 glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
Romain Guy9d5316e2010-06-24 19:30:36 -0700376}
377
378const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700379 return mSnapshot->clipRect;
Romain Guybb9524b2010-06-22 18:56:38 -0700380}
381
Romain Guyc7d53492010-06-25 13:41:57 -0700382bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
383 /*
384 * The documentation of quickReject() indicates that the specified rect
385 * is transformed before being compared to the clip rect. However, the
386 * clip rect is not stored transformed in the snapshot and can thus be
387 * compared directly
388 *
389 * The following code can be used instead to performed a mapped comparison:
390 *
391 * mSnapshot->transform.mapRect(r);
392 * const Rect& clip = mSnapshot->getMappedClip();
393 * return !clip.intersects(r);
394 */
Romain Guyc7d53492010-06-25 13:41:57 -0700395 Rect r(left, top, right, bottom);
396 return !mSnapshot->clipRect.intersects(r);
397}
398
Romain Guybb9524b2010-06-22 18:56:38 -0700399bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700400 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
401 if (clipped) {
402 mSnapshot->flags |= Snapshot::kFlagClipSet;
403 setScissorFromClip();
404 }
405 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700406}
407
Romain Guyf6a11b82010-06-23 17:47:49 -0700408///////////////////////////////////////////////////////////////////////////////
409// Drawing
410///////////////////////////////////////////////////////////////////////////////
411
Romain Guyc1396e92010-06-30 17:56:19 -0700412void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -0700413 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy121e22422010-07-01 18:26:52 -0700414 LOGD("Texture cache size %d", mTextureCache.getSize());
415 LOGD(" max size %d", mTextureCache.getMaxSize());
Romain Guyc1396e92010-06-30 17:56:19 -0700416
Romain Guyc1396e92010-06-30 17:56:19 -0700417 int alpha;
Romain Guy8ba548f2010-06-30 19:21:21 -0700418 SkXfermode::Mode mode;
419 getAlphaAndMode(paint, &alpha, &mode);
Romain Guyc1396e92010-06-30 17:56:19 -0700420
421 drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
422 alpha / 255.0f, mode, texture->blend, true);
Romain Guyce0537b2010-06-29 21:05:21 -0700423}
424
Romain Guyf86ef572010-07-01 11:05:42 -0700425void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
426 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
427 const mat4 transform(*matrix);
428 transform.mapRect(r);
429
430 const Texture* texture = mTextureCache.get(bitmap);
431
432 int alpha;
433 SkXfermode::Mode mode;
434 getAlphaAndMode(paint, &alpha, &mode);
435
436 drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
437 alpha / 255.0f, mode, texture->blend, true);
438}
439
Romain Guy8ba548f2010-06-30 19:21:21 -0700440void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
441 float srcLeft, float srcTop, float srcRight, float srcBottom,
442 float dstLeft, float dstTop, float dstRight, float dstBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700443 const SkPaint* paint) {
444 const Texture* texture = mTextureCache.get(bitmap);
Romain Guy8ba548f2010-06-30 19:21:21 -0700445
446 int alpha;
447 SkXfermode::Mode mode;
448 getAlphaAndMode(paint, &alpha, &mode);
449
450 const float width = texture->width;
451 const float height = texture->height;
452
453 const float u1 = srcLeft / width;
454 const float v1 = srcTop / height;
455 const float u2 = srcRight / width;
456 const float v2 = srcBottom / height;
457
458 resetDrawTextureTexCoords(u1, v1, u2, v2);
459
Romain Guy8ba548f2010-06-30 19:21:21 -0700460 drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
461 alpha / 255.0f, mode, texture->blend, true);
462
463 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
464}
465
Romain Guy85bf02f2010-06-22 13:11:24 -0700466void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guyc7d53492010-06-25 13:41:57 -0700467 const Rect& clip = mSnapshot->clipRect;
Romain Guy026c5e162010-06-28 17:12:22 -0700468 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700469}
Romain Guy9d5316e2010-06-24 19:30:36 -0700470
Romain Guybd6b79b2010-06-26 00:13:53 -0700471void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
Romain Guy026c5e162010-06-28 17:12:22 -0700472 SkXfermode::Mode mode;
473
474 const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
475 if (!isMode) {
476 // Assume SRC_OVER
477 mode = SkXfermode::kSrcOver_Mode;
478 }
479
480 // Skia draws using the color's alpha channel if < 255
481 // Otherwise, it uses the paint's alpha
482 int color = p->getColor();
483 if (((color >> 24) & 0xFF) == 255) {
484 color |= p->getAlpha() << 24;
485 }
486
487 drawColorRect(left, top, right, bottom, color, mode);
Romain Guyc7d53492010-06-25 13:41:57 -0700488}
Romain Guy9d5316e2010-06-24 19:30:36 -0700489
Romain Guy026c5e162010-06-28 17:12:22 -0700490void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
491 int color, SkXfermode::Mode mode) {
492 const int alpha = (color >> 24) & 0xFF;
493 const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
494
495 const GLfloat a = alpha / 255.0f;
496 const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
497 const GLfloat g = ((color >> 8) & 0xFF) / 255.0f;
498 const GLfloat b = ((color ) & 0xFF) / 255.0f;
499
500 if (blend) {
501 glEnable(GL_BLEND);
502 glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
503 }
Romain Guy9d5316e2010-06-24 19:30:36 -0700504
Romain Guyc7d53492010-06-25 13:41:57 -0700505 mModelView.loadTranslate(left, top, 0.0f);
506 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy9d5316e2010-06-24 19:30:36 -0700507
Romain Guyc7d53492010-06-25 13:41:57 -0700508 mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
Romain Guy9d5316e2010-06-24 19:30:36 -0700509
Romain Guyc7d53492010-06-25 13:41:57 -0700510 const GLvoid* p = &gDrawColorVertices[0].position[0];
Romain Guy9d5316e2010-06-24 19:30:36 -0700511
Romain Guyc7d53492010-06-25 13:41:57 -0700512 glEnableVertexAttribArray(mDrawColorShader->position);
513 glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
514 gDrawColorVertexStride, p);
515 glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
Romain Guy9d5316e2010-06-24 19:30:36 -0700516
Romain Guyc7d53492010-06-25 13:41:57 -0700517 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
Romain Guy9d5316e2010-06-24 19:30:36 -0700518
Romain Guyc7d53492010-06-25 13:41:57 -0700519 glDisableVertexAttribArray(mDrawColorShader->position);
Romain Guy026c5e162010-06-28 17:12:22 -0700520
521 if (blend) {
522 glDisable(GL_BLEND);
523 }
Romain Guy85bf02f2010-06-22 13:11:24 -0700524}
525
Romain Guybd6b79b2010-06-26 00:13:53 -0700526void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guyc1396e92010-06-30 17:56:19 -0700527 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
Romain Guybd6b79b2010-06-26 00:13:53 -0700528 mModelView.loadTranslate(left, top, 0.0f);
529 mModelView.scale(right - left, bottom - top, 1.0f);
530
531 mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
532
Romain Guyc1396e92010-06-30 17:56:19 -0700533 if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
534 GLenum sourceMode = gBlends[mode].src;
535 if (!isPremultiplied && sourceMode == GL_ONE) {
536 sourceMode = GL_SRC_ALPHA;
537 }
538
539 glEnable(GL_BLEND);
540 glBlendFunc(sourceMode, gBlends[mode].dst);
Romain Guyd55a8612010-06-28 17:42:46 -0700541 }
542
Romain Guybd6b79b2010-06-26 00:13:53 -0700543 glBindTexture(GL_TEXTURE_2D, texture);
544
Romain Guyc1396e92010-06-30 17:56:19 -0700545 // TODO handle tiling and filtering here
546
Romain Guybd6b79b2010-06-26 00:13:53 -0700547 glActiveTexture(GL_TEXTURE0);
548 glUniform1i(mDrawTextureShader->sampler, 0);
549
Romain Guy026c5e162010-06-28 17:12:22 -0700550 const GLvoid* p = &mDrawTextureVertices[0].position[0];
551 const GLvoid* t = &mDrawTextureVertices[0].texture[0];
Romain Guybd6b79b2010-06-26 00:13:53 -0700552
553 glEnableVertexAttribArray(mDrawTextureShader->position);
554 glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
555 gDrawTextureVertexStride, p);
556
557 glEnableVertexAttribArray(mDrawTextureShader->texCoords);
558 glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
559 gDrawTextureVertexStride, t);
560
561 glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
562
563 glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
564
565 glDisableVertexAttribArray(mDrawTextureShader->position);
566 glDisableVertexAttribArray(mDrawTextureShader->texCoords);
567
568 glBindTexture(GL_TEXTURE_2D, 0);
569 glDisable(GL_BLEND);
570}
571
Romain Guy026c5e162010-06-28 17:12:22 -0700572void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
573 mDrawTextureVertices[0].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700574 mDrawTextureVertices[0].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700575 mDrawTextureVertices[1].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700576 mDrawTextureVertices[1].texture[1] = v1;
Romain Guy026c5e162010-06-28 17:12:22 -0700577 mDrawTextureVertices[2].texture[0] = u1;
Romain Guy8ba548f2010-06-30 19:21:21 -0700578 mDrawTextureVertices[2].texture[1] = v2;
Romain Guy026c5e162010-06-28 17:12:22 -0700579 mDrawTextureVertices[3].texture[0] = u2;
Romain Guy8ba548f2010-06-30 19:21:21 -0700580 mDrawTextureVertices[3].texture[1] = v2;
581}
582
583void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
584 if (paint) {
585 const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
586 if (!isMode) {
587 // Assume SRC_OVER
588 *mode = SkXfermode::kSrcOver_Mode;
589 }
590
591 // Skia draws using the color's alpha channel if < 255
592 // Otherwise, it uses the paint's alpha
593 int color = paint->getColor();
594 *alpha = (color >> 24) & 0xFF;
595 if (*alpha == 255) {
596 *alpha = paint->getAlpha();
597 }
598 } else {
599 *mode = SkXfermode::kSrcOver_Mode;
600 *alpha = 255;
601 }
Romain Guy026c5e162010-06-28 17:12:22 -0700602}
603
Romain Guy9d5316e2010-06-24 19:30:36 -0700604}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700605}; // namespace android