OpenGLRenderer.cpp revision 08aa2cbd5e62e7ca140f78f8bea0477a19880fd9
1e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy/*
2e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Copyright (C) 2010 The Android Open Source Project
3e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
4e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * you may not use this file except in compliance with the License.
6e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * You may obtain a copy of the License at
7e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
8e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy *
10e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * Unless required by applicable law or agreed to in writing, software
11e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * See the License for the specific language governing permissions and
14e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy * limitations under the License.
15e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy */
16e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1785bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy#define LOG_TAG "OpenGLRenderer"
18e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
19e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include <stdlib.h>
20e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include <stdint.h>
21e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy#include <sys/types.h>
22e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
23bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy#include <SkCanvas.h>
24694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy#include <SkTypeface.h>
255cbbce535744b89df5ecea95de21ee3733298260Romain Guy
265cbbce535744b89df5ecea95de21ee3733298260Romain Guy#include <utils/Log.h>
27e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy#include <utils/StopWatch.h>
2885bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
2908aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy#include <private/hwui/DrawGlInfo.h>
3008aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy
315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#include <ui/Rect.h>
325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
3385bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy#include "OpenGLRenderer.h"
340fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy#include "DisplayListRenderer.h"
35a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#include "Vector.h"
36e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
37e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guynamespace android {
389d5316e3f56d138504565ff311145ac01621dff4Romain Guynamespace uirenderer {
399d5316e3f56d138504565ff311145ac01621dff4Romain Guy
409d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
419d5316e3f56d138504565ff311145ac01621dff4Romain Guy// Defines
429d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
439d5316e3f56d138504565ff311145ac01621dff4Romain Guy
44759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy#define RAD_TO_DEG (180.0f / 3.14159265f)
45759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy#define MIN_ANGLE 0.001f
46759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
47dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy// TODO: This should be set in properties
48dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
49dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
509d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
519d5316e3f56d138504565ff311145ac01621dff4Romain Guy// Globals
529d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
539d5316e3f56d138504565ff311145ac01621dff4Romain Guy
54889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy/**
55889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy * Structure mapping Skia xfermodes to OpenGL blending factors.
56889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy */
57889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guystruct Blender {
58889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    SkXfermode::Mode mode;
59889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    GLenum src;
60889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    GLenum dst;
61889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy}; // struct Blender
62889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
63026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy// In this array, the index of each Blender equals the value of the first
64026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
65026c5e16704e817cac7d9c382914c947e34f87e0Romain Guystatic const Blender gBlends[] = {
66b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kClear_Mode,   GL_ZERO,                 GL_ZERO },
67b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrc_Mode,     GL_ONE,                  GL_ZERO },
68b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDst_Mode,     GL_ZERO,                 GL_ONE },
69b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOver_Mode, GL_ONE,                  GL_ONE_MINUS_SRC_ALPHA },
70b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_ONE },
71b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcIn_Mode,   GL_DST_ALPHA,            GL_ZERO },
72b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstIn_Mode,   GL_ZERO,                 GL_SRC_ALPHA },
73b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOut_Mode,  GL_ONE_MINUS_DST_ALPHA,  GL_ZERO },
74b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOut_Mode,  GL_ZERO,                 GL_ONE_MINUS_SRC_ALPHA },
75b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA,            GL_ONE_MINUS_SRC_ALPHA },
76b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_SRC_ALPHA },
77b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kXor_Mode,     GL_ONE_MINUS_DST_ALPHA,  GL_ONE_MINUS_SRC_ALPHA }
78026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy};
79e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
8087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// This array contains the swapped version of each SkXfermode. For instance
8187a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// this array's SrcOver blending mode is actually DstOver. You can refer to
8287a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// createLayer() for more information on the purpose of this array.
83f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guystatic const Blender gBlendsSwap[] = {
84b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kClear_Mode,   GL_ZERO,                 GL_ZERO },
85b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrc_Mode,     GL_ZERO,                 GL_ONE },
86b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDst_Mode,     GL_ONE,                  GL_ZERO },
87b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_ONE },
88b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOver_Mode, GL_ONE,                  GL_ONE_MINUS_SRC_ALPHA },
89b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcIn_Mode,   GL_ZERO,                 GL_SRC_ALPHA },
90b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstIn_Mode,   GL_DST_ALPHA,            GL_ZERO },
91b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOut_Mode,  GL_ZERO,                 GL_ONE_MINUS_SRC_ALPHA },
92b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOut_Mode,  GL_ONE_MINUS_DST_ALPHA,  GL_ZERO },
93b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_SRC_ALPHA },
94b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstATop_Mode, GL_DST_ALPHA,            GL_ONE_MINUS_SRC_ALPHA },
95b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kXor_Mode,     GL_ONE_MINUS_DST_ALPHA,  GL_ONE_MINUS_SRC_ALPHA }
96f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy};
97f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
98889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guystatic const GLenum gTextureUnits[] = {
99b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE0,
100b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE1,
101b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE2
102d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy};
103d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
104f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
105f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Constructors/destructor
106f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
107f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
108fb8b763f762ae21923c58d64caa729b012f40e05Romain GuyOpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
10906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
110db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1111e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
112026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
113ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
114ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy
115ae5575b3421c8fbe590ab046d7d5f2b36ecfd821Romain Guy    mFirstSnapshot = new Snapshot;
116e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
117e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
11885bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain GuyOpenGLRenderer::~OpenGLRenderer() {
11929d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    // The context has already been destroyed at this point, do not call
12029d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    // GL APIs. All GL state should be kept in Caches.h
121e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
122e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
123f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
124f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Setup
125f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
126f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
12785bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::setViewport(int width, int height) {
12808ae317c21ec3086b5017672bba87420cc38a407Romain Guy    glViewport(0, 0, width, height);
129260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
130bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
131bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy    mWidth = width;
132bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy    mHeight = height;
133eb99356a0548684a501766e6a524529ab93304c8Romain Guy
134eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mFirstSnapshot->height = height;
135eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mFirstSnapshot->viewport.set(0, 0, width, height);
136746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
137746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
138e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
139e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1406b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guyvoid OpenGLRenderer::prepare(bool opaque) {
1417d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
1427d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy}
1437d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1447d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guyvoid OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
145fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    mCaches.clearGarbage();
146fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
1478aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mFirstSnapshot,
1488aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
14984962f2fe6ce779c583cc54b11a9de2b6a568117Romain Guy    mSnapshot->fbo = getTargetFbo();
15084962f2fe6ce779c583cc54b11a9de2b6a568117Romain Guy
1518fb954263dd2f918ad339045cc6d82e346515599Romain Guy    mSaveCount = 1;
152f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
153fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glViewport(0, 0, mWidth, mHeight);
154fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy
155d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    glDisable(GL_DITHER);
156bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
1577d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glEnable(GL_SCISSOR_TEST);
1587d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
1597d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    mSnapshot->setClip(left, top, right, bottom);
1607d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1616b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    if (!opaque) {
1626b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1636b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClear(GL_COLOR_BUFFER_BIT);
1646b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    }
165bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
166bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
167b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guyvoid OpenGLRenderer::finish() {
168b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#if DEBUG_OPENGL
169b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    GLenum status = GL_NO_ERROR;
170b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    while ((status = glGetError()) != GL_NO_ERROR) {
171b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        LOGD("GL error from OpenGLRenderer: 0x%x", status);
172a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        switch (status) {
173a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy            case GL_OUT_OF_MEMORY:
174a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                LOGE("  OpenGLRenderer is out of memory!");
175a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                break;
176a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        }
177b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
178b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#endif
179c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#if DEBUG_MEMORY_USAGE
180c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    mCaches.dumpMemoryUsage();
181e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy#else
182e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    if (mCaches.getDebugLevel() & kDebugMemory) {
183e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy        mCaches.dumpMemoryUsage();
184e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    }
185c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#endif
186b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy}
187b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy
1886c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::interrupt() {
189da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    if (mCaches.currentProgram) {
190da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        if (mCaches.currentProgram->isInUse()) {
191da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram->remove();
192da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram = NULL;
193da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        }
194da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    }
19550c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.unbindMeshBuffer();
196da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
197da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
1986c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::resume() {
199eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
200da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
201da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    glEnable(GL_SCISSOR_TEST);
202746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
203da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
204f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    glDisable(GL_DITHER);
205f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
20642f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
20703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
20850c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy
20950c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.blend = true;
21050c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glEnable(GL_BLEND);
21150c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
21250c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendEquation(GL_FUNC_ADD);
213da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
214da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
215cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guybool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
216daf98e941e140e8739458126640183b9f296a2abChet Haase    interrupt();
217f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    if (mDirtyClip) {
218f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy        setScissorFromClip();
219f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    }
220d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy
22180911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    Rect clip(*mSnapshot->clipRect);
22280911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    clip.snapToPixelBoundaries();
22380911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
224d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy#if RENDER_LAYERS_AS_REGIONS
225d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    // Since we don't know what the functor will draw, let's dirty
226d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    // tne entire clip region
227d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    if (hasLayer()) {
228d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy        dirtyLayerUnchecked(clip, getRegion());
229d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    }
230d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy#endif
231d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy
23208aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    DrawGlInfo info;
23308aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    info.clipLeft = clip.left;
23408aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    info.clipTop = clip.top;
23508aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    info.clipRight = clip.right;
23608aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    info.clipBottom = clip.bottom;
23708aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    info.isLayer = hasLayer();
23808aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    getSnapshot()->transform->copyTo(&info.transform[0]);
23980911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
24008aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy    status_t result = (*functor)(0, &info);
241cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy
242cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    if (result != 0) {
24308aa2cbd5e62e7ca140f78f8bea0477a19880fd9Romain Guy        Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
244cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy        dirty.unionWith(localDirty);
245cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    }
246cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy
247daf98e941e140e8739458126640183b9f296a2abChet Haase    resume();
248cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    return result != 0;
249daf98e941e140e8739458126640183b9f296a2abChet Haase}
250daf98e941e140e8739458126640183b9f296a2abChet Haase
251f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
252f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// State management
253f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
254f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
255bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::getSaveCount() const {
2567ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    return mSaveCount;
257bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
258bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
259bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::save(int flags) {
2608aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return saveSnapshot(flags);
261bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
262bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
263bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restore() {
2642542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (mSaveCount > 1) {
2652542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2667ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
267bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
268bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
269bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restoreToCount(int saveCount) {
2708fb954263dd2f918ad339045cc6d82e346515599Romain Guy    if (saveCount < 1) saveCount = 1;
271bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2728fb954263dd2f918ad339045cc6d82e346515599Romain Guy    while (mSaveCount > saveCount) {
2732542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2747ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
275bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
276bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2778aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guyint OpenGLRenderer::saveSnapshot(int flags) {
2788aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mSnapshot, flags);
2798fb954263dd2f918ad339045cc6d82e346515599Romain Guy    return mSaveCount++;
280bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
281bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
282bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guybool OpenGLRenderer::restoreSnapshot() {
2837ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
284bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
285eb99356a0548684a501766e6a524529ab93304c8Romain Guy    bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
286bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
287bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    sp<Snapshot> current = mSnapshot;
2887ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    sp<Snapshot> previous = mSnapshot->previous;
289bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
290eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (restoreOrtho) {
291eb99356a0548684a501766e6a524529ab93304c8Romain Guy        Rect& r = previous->viewport;
292eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glViewport(r.left, r.top, r.right, r.bottom);
293eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mOrthoMatrix.load(current->orthoMatrix);
294eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
295eb99356a0548684a501766e6a524529ab93304c8Romain Guy
2968b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSaveCount--;
2978b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSnapshot = previous;
2988b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
2992542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (restoreClip) {
300746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
3018fb954263dd2f918ad339045cc6d82e346515599Romain Guy    }
3022542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy
3035ec9924d24495822b589f1a17996655d66273b30Romain Guy    if (restoreLayer) {
3045ec9924d24495822b589f1a17996655d66273b30Romain Guy        composeLayer(current, previous);
3055ec9924d24495822b589f1a17996655d66273b30Romain Guy    }
3065ec9924d24495822b589f1a17996655d66273b30Romain Guy
3072542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    return restoreClip;
308d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
3095cbbce535744b89df5ecea95de21ee3733298260Romain Guy
310f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
311bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// Layers
312bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
313bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
314bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
3155c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        SkPaint* p, int flags) {
316eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const GLuint previousFbo = mSnapshot->fbo;
317eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const int count = saveSnapshot(flags);
318d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
319af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (!mSnapshot->isIgnored()) {
320e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        int alpha = 255;
321e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        SkXfermode::Mode mode;
322e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
323e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        if (p) {
324e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            alpha = p->getAlpha();
325e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            if (!mCaches.extensions.hasFramebufferFetch()) {
326e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
327e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                if (!isMode) {
328e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    // Assume SRC_OVER
329e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    mode = SkXfermode::kSrcOver_Mode;
330e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                }
331e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            } else {
332e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                mode = getXfermode(p->getXfermode());
333a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
334a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
335e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            mode = SkXfermode::kSrcOver_Mode;
336d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy        }
337d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
338dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
339dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
340d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
341d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return count;
342bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
343bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
344bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
345bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        int alpha, int flags) {
3465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (alpha >= 255 - ALPHA_THRESHOLD) {
3478aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, NULL, flags);
3488b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    } else {
3498aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        SkPaint paint;
3508aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        paint.setAlpha(alpha);
3518aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, &paint, flags);
3528b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    }
353d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
354bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
3551c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
3561c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Layers are viewed by Skia are slightly different than layers in image editing
3571c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * programs (for instance.) When a layer is created, previously created layers
3581c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * and the frame buffer still receive every drawing command. For instance, if a
3591c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * layer is created and a shape intersecting the bounds of the layers and the
3601c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * framebuffer is draw, the shape will be drawn on both (unless the layer was
3611c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
3621c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3631c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * A way to implement layers is to create an FBO for each layer, backed by an RGBA
3641c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * texture. Unfortunately, this is inefficient as it requires every primitive to
3651c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be drawn n + 1 times, where n is the number of active layers. In practice this
3661c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * means, for every primitive:
3671c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Switch active frame buffer
3681c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Change viewport, clip and projection matrix
3691c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Issue the drawing
3701c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3711c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Switching rendering target n + 1 times per drawn primitive is extremely costly.
3726b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * To avoid this, layers are implemented in a different way here, at least in the
3736b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * general case. FBOs are used, as an optimization, when the "clip to layer" flag
3746b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * is set. When this flag is set we can redirect all drawing operations into a
3756b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * single FBO.
3761c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3771c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * This implementation relies on the frame buffer being at least RGBA 8888. When
3781c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * a layer is created, only a texture is created, not an FBO. The content of the
3791c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer contained within the layer's bounds is copied into this texture
38087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
3811c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * buffer and drawing continues as normal. This technique therefore treats the
3821c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer as a scratch buffer for the layers.
3831c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3841c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * To compose the layers back onto the frame buffer, each layer texture
3851c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * (containing the original frame buffer data) is drawn as a simple quad over
3861c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * the frame buffer. The trick is that the quad is set as the composition
3871c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * destination in the blending equation, and the frame buffer becomes the source
3881c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * of the composition.
3891c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3901c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Drawing layers with an alpha value requires an extra step before composition.
3911c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * An empty quad is drawn over the layer's region in the frame buffer. This quad
3921c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
3931c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * quad is used to multiply the colors in the frame buffer. This is achieved by
3941c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * changing the GL blend functions for the GL_FUNC_ADD blend equation to
3951c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * GL_ZERO, GL_SRC_ALPHA.
3961c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3971c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Because glCopyTexImage2D() can be slow, an alternative implementation might
3981c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be use to draw a single clipped layer. The implementation described above
3991c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is correct in every case.
40087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *
40187a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * (1) The frame buffer is actually not cleared right away. To allow the GPU
40287a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     to potentially optimize series of calls to glCopyTexImage2D, the frame
40387a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     buffer is left untouched until the first drawing operation. Only when
40487a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     something actually gets drawn are the layers regions cleared.
4051c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
406d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guybool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
407eb99356a0548684a501766e6a524529ab93304c8Romain Guy        float right, float bottom, int alpha, SkXfermode::Mode mode,
408eb99356a0548684a501766e6a524529ab93304c8Romain Guy        int flags, GLuint previousFbo) {
409eb99356a0548684a501766e6a524529ab93304c8Romain Guy    LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
410fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
411dda570201ac851dd85af3861f7e575721d3345daRomain Guy
412eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
413eb99356a0548684a501766e6a524529ab93304c8Romain Guy
414f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    // Window coordinates of the layer
4158aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    Rect bounds(left, top, right, bottom);
4167b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    if (!fboLayer) {
417eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mSnapshot->transform->mapRect(bounds);
418ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
419eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Layers only make sense if they are in the framebuffer's bounds
420ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        if (bounds.intersect(*snapshot->clipRect)) {
421ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // We cannot work with sub-pixels in this case
422ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            bounds.snapToPixelBoundaries();
423ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy
424ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // When the layer is not an FBO, we may use glCopyTexImage so we
425ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // need to make sure the layer does not extend outside the bounds
426ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // of the framebuffer
427ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            if (!bounds.intersect(snapshot->previous->viewport)) {
428ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy                bounds.setEmpty();
429ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            }
430ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        } else {
431ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            bounds.setEmpty();
432ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        }
433eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
434bf434114cbf55b216fdc76fc8d65a75e84c9dab5Romain Guy
435746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
436746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            bounds.getHeight() > mCaches.maxTextureSize) {
43732963c393a804db2cd86c24443c2f1c6eee6064bRomain Guy        snapshot->empty = fboLayer;
438dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    } else {
439e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
440dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
441dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
442dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    // Bail out if we won't draw in this snapshot
443af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (snapshot->invisible || snapshot->empty) {
444b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        return false;
445b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
446dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glActiveTexture(gTextureUnits[0]);
4488550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
449f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy    if (!layer) {
450f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy        return false;
451bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    }
452bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
453dda570201ac851dd85af3861f7e575721d3345daRomain Guy    layer->mode = mode;
454f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    layer->alpha = alpha;
4558aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    layer->layer.set(bounds);
4568550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
4578550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy            bounds.getWidth() / float(layer->width), 0.0f);
458171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy    layer->colorFilter = mColorFilter;
459dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4608fb954263dd2f918ad339045cc6d82e346515599Romain Guy    // Save the layer in the snapshot
4618fb954263dd2f918ad339045cc6d82e346515599Romain Guy    snapshot->flags |= Snapshot::kFlagIsLayer;
462dda570201ac851dd85af3861f7e575721d3345daRomain Guy    snapshot->layer = layer;
4631d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
464eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
4655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return createFboLayer(layer, bounds, snapshot, previousFbo);
466eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
467eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Copy the framebuffer into the layer
468eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindTexture(GL_TEXTURE_2D, layer->texture);
469514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!bounds.isEmpty()) {
470514fb18827186591d66973c2362c859b64b63556Romain Guy            if (layer->empty) {
471514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
472514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, layer->width, layer->height, 0);
473514fb18827186591d66973c2362c859b64b63556Romain Guy                layer->empty = false;
474514fb18827186591d66973c2362c859b64b63556Romain Guy            } else {
475514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
476514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
477514fb18827186591d66973c2362c859b64b63556Romain Guy            }
4787b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
4797b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            // Clear the framebuffer where the layer will draw
4807b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glScissor(bounds.left, mSnapshot->height - bounds.bottom,
4817b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy                    bounds.getWidth(), bounds.getHeight());
4827b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
4837b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glClear(GL_COLOR_BUFFER_BIT);
4847b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
4857b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            dirtyClip();
486ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        }
487eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
488f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
489d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return true;
490bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
491bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
4925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guybool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
4935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLuint previousFbo) {
4945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    layer->fbo = mCaches.fboCache.get();
4955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
4975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->region = &snapshot->layer->region;
4985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagFboTarget;
4995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clip(bounds);
5025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->transform->mapRect(clip);
5035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(*snapshot->clipRect);
5045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
5055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(snapshot->previous->viewport);
5065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mat4 inverse;
5085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.loadInverse(*mSnapshot->transform);
5095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.mapRect(clip);
5115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
5125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(bounds);
5135ec9924d24495822b589f1a17996655d66273b30Romain Guy    clip.translate(-bounds.left, -bounds.top);
5145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagIsFboLayer;
5165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->fbo = layer->fbo;
5175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
5185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
5195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
5205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->height = bounds.getHeight();
5215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagDirtyOrtho;
5225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->orthoMatrix.load(mOrthoMatrix);
5235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Bind texture to FBO
5255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
5265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindTexture(GL_TEXTURE_2D, layer->texture);
5275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Initialize the texture if needed
5295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->empty) {
5305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->empty = false;
5315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
5325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
5335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5355b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
5365b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->texture, 0);
5375b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5385b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
5395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (status != GL_FRAMEBUFFER_COMPLETE) {
5415b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
5425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5435b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
5445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glDeleteTextures(1, &layer->texture);
5455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        mCaches.fboCache.put(layer->fbo);
5465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        delete layer;
5485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return false;
5505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
5545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
5555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
5565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
5575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClear(GL_COLOR_BUFFER_BIT);
5585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    dirtyClip();
5605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Change the ortho projection
5625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
5635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
5645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    return true;
5665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
5675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5681c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
5691c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Read the documentation of createLayer() before doing anything in this method.
5701c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
5711d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guyvoid OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
5721d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    if (!current->layer) {
5731d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LOGE("Attempting to compose a layer that does not exist");
5741d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        return;
5751d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
5761d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
5775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
578eb99356a0548684a501766e6a524529ab93304c8Romain Guy
579eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
580eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Unbind current FBO and restore previous one
581eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
582eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
583eb99356a0548684a501766e6a524529ab93304c8Romain Guy
5841d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Layer* layer = current->layer;
5851d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    const Rect& rect = layer->layer;
5861d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
587eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (!fboLayer && layer->alpha < 255) {
588f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
5891c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy                layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
5905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Required below, composeLayerRect() will divide by 255
5915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->alpha = 255;
592f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    }
593f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
59403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
5958b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
596746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
5975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // When the layer is stored in an FBO, we can save a bit of fillrate by
5995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // drawing only the dirty region
600eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
6015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
602171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
603171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            setupColorFilter(layer->colorFilter);
604171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
6055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRegion(layer, rect);
606171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
607171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            resetColorFilter();
608171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
609eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
610514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!rect.isEmpty()) {
611514fb18827186591d66973c2362c859b64b63556Romain Guy            dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
612514fb18827186591d66973c2362c859b64b63556Romain Guy            composeLayerRect(layer, rect, true);
613514fb18827186591d66973c2362c859b64b63556Romain Guy        }
614eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
6151d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
616eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
617eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Detach the texture from the FBO
618eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
619eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
620eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
621eb99356a0548684a501766e6a524529ab93304c8Romain Guy
622eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
623eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mCaches.fboCache.put(current->fbo);
624eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
625eb99356a0548684a501766e6a524529ab93304c8Romain Guy
626746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
627746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
628eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // Failing to add the layer to the cache should happen only if the layer is too large
6298550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    if (!mCaches.layerCache.put(layer)) {
6301d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LAYER_LOGD("Deleting layer");
6311d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        glDeleteTextures(1, &layer->texture);
6321d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        delete layer;
6331d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
6341d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy}
6351d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
6365b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
6375b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const Rect& texCoords = layer->texCoords;
6385b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
6395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
6415b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
6425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
6435b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
6455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
6465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
6485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
6496217a71cd281003a376d998269d577d26a61c206Romain Guy#if RENDER_LAYERS_RECT_AS_RECT
6505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->region.isRect()) {
6515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRect(layer, rect);
6525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
6535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return;
6545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
6556217a71cd281003a376d998269d577d26a61c206Romain Guy#endif
6565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!layer->region.isEmpty()) {
6585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        size_t count;
6595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const android::Rect* rects = layer->region.getArray(&count);
6605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float alpha = layer->alpha / 255.0f;
6625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texX = 1.0f / float(layer->width);
6635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texY = 1.0f / float(layer->height);
664f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float height = rect.getHeight();
6655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        TextureVertex* mesh = mCaches.getRegionMesh();
6675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLsizei numQuads = 0;
6685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6697230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDraw();
6707230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawWithTexture();
6717230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawColor(alpha, alpha, alpha, alpha);
672ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilter();
6737230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
6747230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawProgram();
6757230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawDirtyRegionsDisabled();
6767230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawPureColorUniforms();
677ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilterUniforms();
6787230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawTexture(layer->texture);
679c038ea358aadca082198f3effc550d33135bf426Romain Guy        setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
6807230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
6815b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        for (size_t i = 0; i < count; i++) {
6835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const android::Rect* r = &rects[i];
6845b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6855b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u1 = r->left * texX;
686f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v1 = (height - r->top) * texY;
6875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u2 = r->right * texX;
688f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v2 = (height - r->bottom) * texY;
6895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // TODO: Reject quads outside of the clip
6915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->top, u1, v1);
6925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->top, u2, v1);
6935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
6945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
6955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            numQuads++;
6975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            if (numQuads >= REGION_MESH_QUAD_COUNT) {
6995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
7005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                numQuads = 0;
7015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                mesh = mCaches.getRegionMesh();
7025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
7035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (numQuads > 0) {
7065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
7075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7107230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        finishDrawTexture();
7115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
7133a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawRegionRects(layer->region);
7145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
7175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
7195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    composeLayerRect(layer, rect);
7205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7233a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guyvoid OpenGLRenderer::drawRegionRects(const Region& region) {
7243a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
7253a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    size_t count;
7263a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    const android::Rect* rects = region.getArray(&count);
7273a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7283a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    uint32_t colors[] = {
7293a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7fff0000, 0x7f00ff00,
7303a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7f0000ff, 0x7fff00ff,
7313a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    };
7323a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7333a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int offset = 0;
7343a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int32_t top = rects[0].top;
7353a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7363a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    for (size_t i = 0; i < count; i++) {
7373a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        if (top != rects[i].top) {
7383a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            offset ^= 0x2;
7393a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            top = rects[i].top;
7403a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        }
7413a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7423a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
7433a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
7443a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy                SkXfermode::kSrcOver_Mode);
7453a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    }
7463a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
7473a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy}
7483a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom, const mat4 transform) {
7515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
752f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
7545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        transform.mapRect(bounds);
755f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom) {
7625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
763f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
765f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7661bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    }
7671bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#endif
7681bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy}
7691bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy
7701bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guyvoid OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
7711bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#if RENDER_LAYERS_AS_REGIONS
7721bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    if (bounds.intersect(*mSnapshot->clipRect)) {
7731bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        bounds.snapToPixelBoundaries();
7741bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
7751bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        if (!dirty.isEmpty()) {
7761bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy            region->orSelf(dirty);
7775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7815b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
782bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
783f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Transforms
784f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
785f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
786f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::translate(float dx, float dy) {
7878aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->translate(dx, dy, 0.0f);
788f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
789f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
790f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::rotate(float degrees) {
7918aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
792f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
793f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
794f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::scale(float sx, float sy) {
7958aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->scale(sx, sy, 1.0f);
796f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
797f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
798807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guyvoid OpenGLRenderer::skew(float sx, float sy) {
799807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy    mSnapshot->transform->skew(sx, sy);
800807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy}
801807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy
802f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::setMatrix(SkMatrix* matrix) {
8038aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->load(*matrix);
804f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
805f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
80641030da16856c8869e1e51d4a0405432fa96614eRomain Guyconst float* OpenGLRenderer::getMatrix() const {
80799bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    if (mSnapshot->fbo != 0) {
80899bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy        return &mSnapshot->transform->data[0];
80999bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    }
81099bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    return &mIdentity.data[0];
81141030da16856c8869e1e51d4a0405432fa96614eRomain Guy}
81241030da16856c8869e1e51d4a0405432fa96614eRomain Guy
813f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::getMatrix(SkMatrix* matrix) {
8148aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->copyTo(*matrix);
815f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
816f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
817f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
818e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    SkMatrix transform;
819e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->copyTo(transform);
820e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    transform.preConcat(*matrix);
821e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->load(transform);
822f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
823f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
824f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
825f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Clipping
826f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
827f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
828bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::setScissorFromClip() {
829e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    Rect clip(*mSnapshot->clipRect);
830e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    clip.snapToPixelBoundaries();
831eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
832746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
8339d5316e3f56d138504565ff311145ac01621dff4Romain Guy}
8349d5316e3f56d138504565ff311145ac01621dff4Romain Guy
8359d5316e3f56d138504565ff311145ac01621dff4Romain Guyconst Rect& OpenGLRenderer::getClipBounds() {
836079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return mSnapshot->getLocalClip();
837bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
838bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
839c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guybool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
840af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) {
841dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        return true;
842dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
843dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
8441d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Rect r(left, top, right, bottom);
8458aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->mapRect(r);
846d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    r.snapToPixelBoundaries();
847d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
848d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    Rect clipRect(*mSnapshot->clipRect);
849d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    clipRect.snapToPixelBoundaries();
850d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
851d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    return !clipRect.intersects(r);
852c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
853c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
854079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guybool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
855079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    bool clipped = mSnapshot->clip(left, top, right, bottom, op);
8567ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    if (clipped) {
857746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
8587ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
8598aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return !mSnapshot->clipRect->isEmpty();
860e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
861e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
862f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
86370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy// Drawing commands
86470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
86570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
86670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDraw() {
86770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mDirtyClip) {
86870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setScissorFromClip();
86970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
87070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.reset();
87170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = false;
87270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = false;
87370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = mColorR = mColorG = mColorB = 0.0f;
87470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTextureUnit = 0;
87570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = true;
8768d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mTexCoordsSlot = -1;
87770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
87870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
87970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
88070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasTexture = true;
88170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasAlpha8Texture = isAlpha8;
88270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
88370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
88470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(int color) {
8858d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(color, (color >> 24) & 0xFF);
8868d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
8878d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
8888d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawColor(int color, int alpha) {
8898d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mColorA = alpha / 255.0f;
89070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    const float a = mColorA / 255.0f;
891fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorR = a * ((color >> 16) & 0xFF);
892fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorG = a * ((color >>  8) & 0xFF);
893fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorB = a * ((color      ) & 0xFF);
89470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
89570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
89670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
89770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
89886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
89986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = alpha / 255.0f;
90086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    const float a = mColorA / 255.0f;
90186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = a * ((color >> 16) & 0xFF);
90286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = a * ((color >>  8) & 0xFF);
90386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = a * ((color      ) & 0xFF);
90486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
90586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
90686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
90786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
90870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
90970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = a;
91070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorR = r;
91170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorG = g;
91270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorB = b;
91370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
91470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(r, g, b, a);
91570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
91670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
91786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
91886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = a;
91986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = r;
92086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = g;
92186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = b;
92286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
92386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
92486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
92586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
92670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShader() {
92770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
92870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->describe(mDescription, mCaches.extensions);
92970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
93070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilter() {
93370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
93470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->describe(mDescription, mCaches.extensions);
93570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
93670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
93970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
94070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
94170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
94270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
94470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
94570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
94670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
94770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawProgram() {
94970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    useProgram(mCaches.programCache.get(mDescription));
95070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
95270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
95370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = false;
95470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
95670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
95770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform) {
95870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mModelView.loadTranslate(left, top, 0.0f);
95970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreTransform) {
96070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
96170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
96270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
96370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
96470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
96570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
96670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
96770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
9688d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawModelViewIdentity() {
9698d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
9708d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
9718d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
97270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
97370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform, bool ignoreModelView) {
97470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreModelView) {
97570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadTranslate(left, top, 0.0f);
97670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.scale(right - left, bottom - top, 1.0f);
97770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
97870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadIdentity();
97970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
98086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    bool dirty = right - left > 0.0f && bottom - top > 0.0f;
98186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    if (!ignoreTransform) {
98286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
98386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) {
98486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy            dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
98586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        }
98686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    } else {
98786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
98886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
98986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    }
99070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
99170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
99270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorUniforms() {
9935536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mColorSet || (mShader && mSetShaderColor)) {
99470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
99570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
99670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
99770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
99886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawPureColorUniforms() {
9995536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mSetShaderColor) {
100086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
10015536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    }
10025536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy}
10035536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy
100470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
100570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
100670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (ignoreTransform) {
100770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mModelView.loadInverse(*mSnapshot->transform);
100870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        }
100970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
101070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
101170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
101270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
10138d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawShaderIdentityUniforms() {
10148d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mShader) {
10158d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
10168d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
10178d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
10188d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
101970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilterUniforms() {
102070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
102170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->setupProgram(mCaches.currentProgram);
102270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
102370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
102470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
102570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawSimpleMesh() {
102670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mCaches.bindMeshBuffer();
102770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
102870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, 0);
102970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawTexture(GLuint texture) {
103270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    bindTexture(texture);
103370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
103470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
103670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glEnableVertexAttribArray(mTexCoordsSlot);
103770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
104070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!vertices) {
104170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
104270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
104370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.unbindMeshBuffer();
104470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
104570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
104670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, vertices);
1047cf28957c89127ccfc82d535903c776124665446aDavid Li    if (mTexCoordsSlot >= 0) {
10488d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
10498d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
105070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
105170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
105270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::finishDrawTexture() {
105370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glDisableVertexAttribArray(mTexCoordsSlot);
105470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
105570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
105670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
1057f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Drawing
1058f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
1059f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
10607b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guybool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
10617b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy        Rect& dirty, uint32_t level) {
10627b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    if (quickReject(0.0f, 0.0f, width, height)) {
10637b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy        return false;
10647b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    }
10657b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
10660fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // All the usual checks and setup operations (quickReject, setupDraw, etc.)
10670fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // will be performed by the display list itself
10680fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    if (displayList) {
1069cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy        return displayList->replay(*this, dirty, level);
10700fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    }
10717b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
1072daf98e941e140e8739458126640183b9f296a2abChet Haase    return false;
10730fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy}
10740fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy
10755c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
10766926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float right = left + bitmap->width();
10776926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float bottom = top + bitmap->height();
10786926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
10796926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
10806926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10816926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10826926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
108386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10848164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10859cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
108622158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
108722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10886926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    drawTextureRect(left, top, right, bottom, texture, paint);
10898ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
10908ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
10915c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
1092f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1093f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    const mat4 transform(*matrix);
1094f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    transform.mapRect(r);
1095f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
10966926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(r.left, r.top, r.right, r.bottom)) {
10976926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10986926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10996926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
110086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
11018164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11029cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
110322158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
110422158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
11055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // This could be done in a cheaper way, all we need is pass the matrix
11065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // to the vertex shader. The save/restore is a bit overkill.
11075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    save(SkCanvas::kMatrix_SaveFlag);
11085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    concatMatrix(matrix);
11095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
11105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    restore();
1111f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy}
1112f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
11135a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guyvoid OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
11145a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        float* vertices, int* colors, SkPaint* paint) {
11155a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    // TODO: Do a quickReject
11165a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!vertices || mSnapshot->isIgnored()) {
11175a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        return;
11185a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11195a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11205a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    glActiveTexture(gTextureUnits[0]);
11215a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11225a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!texture) return;
11235a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const AutoTexture autoCleanup(texture);
11245a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11255a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11265a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    int alpha;
11275a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    SkXfermode::Mode mode;
11285a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
11295a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11305a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const uint32_t count = meshWidth * meshHeight * 6;
11315a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1132b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float left = FLT_MAX;
1133b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float top = FLT_MAX;
1134b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float right = FLT_MIN;
1135b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float bottom = FLT_MIN;
1136b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1137b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1138b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    bool hasActiveLayer = hasLayer();
1139b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#else
1140b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    bool hasActiveLayer = false;
1141b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
1142b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1143a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    // TODO: Support the colors array
1144a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    TextureVertex mesh[count];
11455a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    TextureVertex* vertex = mesh;
11465a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    for (int32_t y = 0; y < meshHeight; y++) {
11475a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        for (int32_t x = 0; x < meshWidth; x++) {
11485a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            uint32_t i = (y * (meshWidth + 1) + x) * 2;
11495a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11505a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u1 = float(x) / meshWidth;
11515a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u2 = float(x + 1) / meshWidth;
11525a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v1 = float(y) / meshHeight;
11535a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v2 = float(y + 1) / meshHeight;
11545a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11555a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ax = i + (meshWidth + 1) * 2;
11565a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ay = ax + 1;
11575a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int bx = i;
11585a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int by = bx + 1;
11595a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cx = i + 2;
11605a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cy = cx + 1;
11615a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dx = i + (meshWidth + 1) * 2 + 2;
11625a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dy = dx + 1;
11635a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11645a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11655a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
11665a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11675a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11685a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11695a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11705a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
1171b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1172b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1173b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            if (hasActiveLayer) {
1174b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                // TODO: This could be optimized to avoid unnecessary ops
1175b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1176b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1177b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1178b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1179b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            }
1180b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
11815a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        }
11825a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11835a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1184b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1185b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    if (hasActiveLayer) {
1186b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy        dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1187b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    }
1188b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
1189b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
11905a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
11915a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
1192b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            GL_TRIANGLES, count, false, false, 0, false, false);
11935a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy}
11945a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11958ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guyvoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
11968ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float srcLeft, float srcTop, float srcRight, float srcBottom,
11978ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float dstLeft, float dstTop, float dstRight, float dstBottom,
11985c13d89c1332fcc499379b9064b891187b75ca32Chet Haase         SkPaint* paint) {
11996926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
12006926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
12016926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
12026926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1203746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12048164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12059cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
120622158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12078164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
12088ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
12098ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float width = texture->width;
12108ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float height = texture->height;
1211c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
12128ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u1 = srcLeft / width;
12138ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v1 = srcTop / height;
12148ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u2 = srcRight / width;
12158ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v2 = srcBottom / height;
1216c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
121703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
12188ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(u1, v1, u2, v2);
12198ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
122003750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    int alpha;
122103750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    SkXfermode::Mode mode;
122203750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
122303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy
12246620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
12256620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
12266620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
12276620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12286620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
12296620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->id, alpha / 255.0f, mode, texture->blend,
12306620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
12316620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount, false, true);
12326620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
12336620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
12346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
12356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
12366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
12378ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
12388ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1239ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
1240ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
12414aa90573bbf86db0d33a3a790c5dbd0d93b95cfeRomain Guyvoid OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
12424bb942083a0d4db746adf95349108dd8ef842e32Romain Guy        const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
12435c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        float left, float top, float right, float bottom, SkPaint* paint) {
12446926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
12456926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
12466926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
12476926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1248746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12498164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12509cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
125122158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12528164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1253f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1254f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    int alpha;
1255f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    SkXfermode::Mode mode;
1256f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
1257f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12582728f961614a385df1f056fc24803a9f65c90fabRomain Guy    const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
12594bb942083a0d4db746adf95349108dd8ef842e32Romain Guy            right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
1260f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1261a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy    if (mesh && mesh->verticesCount > 0) {
12626620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1263a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#if RENDER_LAYERS_AS_REGIONS
12645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Mark the current layer dirty where we are going to draw the patch
12658168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy        if (hasLayer() && mesh->hasEmptyQuads) {
1266c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetX = left + mSnapshot->transform->getTranslateX();
1267c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetY = top + mSnapshot->transform->getTranslateY();
12685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const size_t count = mesh->quads.size();
12695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            for (size_t i = 0; i < count; i++) {
12708ab4079ca27e36e5c584495bcd71b573598ac021Romain Guy                const Rect& bounds = mesh->quads.itemAt(i);
12716620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                if (pureTranslate) {
1272c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1273c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1274c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
12756620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                } else {
1276c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(left + bounds.left, top + bounds.top,
1277c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                            left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
12786620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                }
12795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
12805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
1281a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#endif
12825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
12836620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        if (pureTranslate) {
12846620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
12856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
12866620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
12886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12896620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
12906620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12916620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        } else {
12926620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
12936620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12946620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
12956620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12966620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        }
1297054dc1840941665e32036f9523df51720ad069c8Romain Guy    }
1298f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1299f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
13005c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1301af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1302dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
1303a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isAA = paint->isAntiAlias();
1304a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1305a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // A stroke width of 0 has a special meaningin Skia:
1306a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // it draws an unscaled 1px wide line
1307a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1308a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1309759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    int alpha;
1310759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    SkXfermode::Mode mode;
1311759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
1312759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1313a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    int verticesCount = count >> 2;
13147230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy    int generatedVerticesCount = 0;
1315a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1316a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA needs more vertices
1317a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 6;
1318a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1319a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA will be different
1320a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 2;
1321a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
1322759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1323a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex lines[verticesCount];
1324a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex* vertex = &lines[0];
1325759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
13268d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDraw();
13278d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(paint->getColor(), alpha);
13288d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilter();
13298d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShader();
13308d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawBlending(mode);
13318d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawProgram();
13328d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawModelViewIdentity();
13338d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorUniforms();
13348d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilterUniforms();
13358d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShaderIdentityUniforms();
13368d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawMesh(vertex);
1337759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1338a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1339a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1340a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
1341a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // a = start point, b = end point
1342a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 a(points[i], points[i + 1]);
1343a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 b(points[i + 2], points[i + 3]);
1344a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1345a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Bias to snap to the same pixels as Skia
1346a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            a += 0.375;
1347a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            b += 0.375;
1348a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1349a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Find the normal to the line
1350a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 n = (b - a).copyNormalized() * strokeWidth;
1351a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            float x = n.x;
1352a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.x = -n.y;
1353a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.y = x;
1354a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1355a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Four corners of the rectangle defining a thick line
1356a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p1 = a - n;
1357a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p2 = a + n;
1358a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p3 = b + n;
1359a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p4 = b - n;
1360a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13617230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
13627230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
13637230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
13647230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
13657230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13667230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13677230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Draw the line as 2 triangles, could be optimized
13687230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // by using only 4 vertices and the correct indices
13697230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Also we should probably used non textured vertices
13707230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // when line AA is disabled to save on bandwidth
13717230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13727230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
13737230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13747230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13757230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13767230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
13777230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13787230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 6;
13797230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13807230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13817230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1382a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
1383a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13847230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13857230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            // GL_LINE does not give the result we want to match Skia
13867230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
13877230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
1388a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1389a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1390a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
13917230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(points[i], points[i + 1]);
13927230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(points[i], points[i + 1]);
13937230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(points[i + 2], points[i + 3]);
13947230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(points[i + 2], points[i + 3]);
13957230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13967230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13977230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
13987230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
13997230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
14007230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 2;
14017230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
14027230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
14037230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1404a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
14058d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
14067230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
14077230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glLineWidth(1.0f);
14087230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_LINES, 0, generatedVerticesCount);
14097230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
141029d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    }
1411759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy}
1412759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
141385bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
1414e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy    // No need to check against the clip, we fill the clip region
1415af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1416e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
1417ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    Rect& clip(*mSnapshot->clipRect);
1418ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    clip.snapToPixelBoundaries();
141970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
14203d58c03de0d8877b36cdb78b0ca8b5cac7f600e2Romain Guy    drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
1421c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
14229d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1423c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
142401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (!texture) return;
142501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const AutoTexture autoCleanup(texture);
142601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
142701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float x = left + texture->left - texture->offset;
142801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float y = top + texture->top - texture->offset;
142901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
143001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
143101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
143201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1433c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1434c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        float rx, float ry, SkPaint* paint) {
143501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (mSnapshot->isIgnored()) return;
143601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
143701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
1438c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1439c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy            right - left, bottom - top, rx, ry, paint);
1440c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1441c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
144201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1443c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1444c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1445c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1446c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
144701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
1448c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(x - radius, y - radius, texture, paint);
1449c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
145001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1451c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1452c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
145301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1454c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1455c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1456c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1457c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
1458c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14598b2f5267f16c295f12faab810527cd6311997e34Romain Guyvoid OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
14608b2f5267f16c295f12faab810527cd6311997e34Romain Guy        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
14618b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (mSnapshot->isIgnored()) return;
14628b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14638b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (fabs(sweepAngle) >= 360.0f) {
14648b2f5267f16c295f12faab810527cd6311997e34Romain Guy        drawOval(left, top, right, bottom, paint);
14658b2f5267f16c295f12faab810527cd6311997e34Romain Guy        return;
14668b2f5267f16c295f12faab810527cd6311997e34Romain Guy    }
14678b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14688b2f5267f16c295f12faab810527cd6311997e34Romain Guy    glActiveTexture(gTextureUnits[0]);
14698b2f5267f16c295f12faab810527cd6311997e34Romain Guy    const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
14708b2f5267f16c295f12faab810527cd6311997e34Romain Guy            startAngle, sweepAngle, useCenter, paint);
14718b2f5267f16c295f12faab810527cd6311997e34Romain Guy    drawShape(left, top, texture, paint);
14728b2f5267f16c295f12faab810527cd6311997e34Romain Guy}
14738b2f5267f16c295f12faab810527cd6311997e34Romain Guy
1474c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1475c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        SkPaint* paint) {
1476c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1477c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1478c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1479c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1480c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
148101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
148201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
14835c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
1484c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (p->getStyle() != SkPaint::kFill_Style) {
1485c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        drawRectAsShape(left, top, right, bottom, p);
1486c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        return;
1487c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    }
1488c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14896926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
14906926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
14916926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
14926926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1493026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    SkXfermode::Mode mode;
1494746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (!mCaches.extensions.hasFramebufferFetch()) {
1495a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1496a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (!isMode) {
1497a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // Assume SRC_OVER
1498a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            mode = SkXfermode::kSrcOver_Mode;
1499a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        }
1500a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    } else {
1501a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        mode = getXfermode(p->getXfermode());
1502026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    }
1503026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1504026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    int color = p->getColor();
1505026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    drawColorRect(left, top, right, bottom, color, mode);
1506c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
15079d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1508e8e62a4a032a80409114a37908b5f18ab0080848Romain Guyvoid OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1509e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        float x, float y, SkPaint* paint) {
1510b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (text == NULL || count == 0) {
1511e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        return;
1512e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1513af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1514e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
1515f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    paint->setAntiAlias(true);
1516e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
1517a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy    float length = -1.0f;
1518e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    switch (paint->getTextAlign()) {
1519e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kCenter_Align:
1520e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1521e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length / 2.0f;
1522e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1523e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kRight_Align:
1524e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1525e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length;
1526e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1527e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        default:
1528e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1529e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1530e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
15313a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldX = x;
15323a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldY = y;
15336620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const bool pureTranslate = mSnapshot->transform->isPureTranslate();
15346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate) {
15356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
15366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
15376620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15386620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
1539b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1540b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
1541fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy            paint->getTextSize());
1542e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
154386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    int alpha;
154486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    SkXfermode::Mode mode;
154586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
15469d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
15471e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    if (mHasShadow) {
1548b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
1549fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
15501e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy                count, mShadowRadius);
15511e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        const AutoTexture autoCleanup(shadow);
15520a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
155386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sx = x - shadow->left + mShadowDx;
155486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sy = y - shadow->top + mShadowDy;
155586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
155686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
155786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
155886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        glActiveTexture(gTextureUnits[0]);
155986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDraw();
156086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawWithTexture(true);
156186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
156286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawBlending(true, mode);
156386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawProgram();
156486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
156586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawTexture(shadow->id);
156686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawPureColorUniforms();
156786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
15680a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
15690a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
157086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        finishDrawTexture();
15711e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    }
15721e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
1573b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1574b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy        return;
1575b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    }
1576b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy
15776620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // Pick the appropriate texture filtering
15786620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    bool linearFilter = mSnapshot->transform->changesBounds();
15796620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate && !linearFilter) {
15806620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
15816620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
158386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
158486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDraw();
158586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawDirtyRegionsDisabled();
158686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawWithTexture(true);
158786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
158886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilter();
158986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShader();
159086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawBlending(true, mode);
159186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawProgram();
159286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawModelView(x, y, x, y, pureTranslate, true);
159386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawTexture(fontRenderer.getTexture(linearFilter));
159486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
159586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilterUniforms();
159686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShaderUniforms(pureTranslate);
159706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
15986620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
15995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
16005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
16015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1602f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = hasLayer();
16035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
1604f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = false;
16055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
160603750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
1607894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk
1608894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    // Tell font renderer the locations of position and texture coord
1609894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    // attributes so it can bind its data properly
1610894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    int positionSlot = mCaches.currentProgram->position;
1611894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
16126620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
1613f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            hasActiveLayer ? &bounds : NULL)) {
16145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1615f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        if (hasActiveLayer) {
16166620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            if (!pureTranslate) {
16176620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mSnapshot->transform->mapRect(bounds);
16186620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            }
1619f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            dirtyLayerUnchecked(bounds, getRegion());
16205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
16215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
16225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
1623694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1624694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1625fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1626a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy
16273a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
1628694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}
1629694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
16307fbcc0492fca03857e3c45064f4aa040af817d55Romain Guyvoid OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
1631af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1632dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
163301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
16347fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1635fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    const PathTexture* texture = mCaches.pathCache.get(path, paint);
16369cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
163722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
16387fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
16398b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float x = texture->left - texture->offset;
16408b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float y = texture->top - texture->offset;
16418b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
164201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
16437fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
16447fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1645ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1646ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
16476c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return;
16486c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
16496c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16506c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glActiveTexture(gTextureUnits[0]);
16516c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16526c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int alpha;
16536c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    SkXfermode::Mode mode;
16546c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
16556c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1656ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->alpha = alpha;
1657ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->mode = mode;
16586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1659f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
1660c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy    if (!layer->region.isEmpty()) {
16616217a71cd281003a376d998269d577d26a61c206Romain Guy#if RENDER_LAYERS_RECT_AS_RECT
1662c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        if (layer->region.isRect()) {
1663c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1664c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            composeLayerRect(layer, r);
1665c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        } else if (layer->mesh) {
16666217a71cd281003a376d998269d577d26a61c206Romain Guy#else
16676217a71cd281003a376d998269d577d26a61c206Romain Guy        if (layer->mesh) {
16686217a71cd281003a376d998269d577d26a61c206Romain Guy#endif
16698168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            const float a = alpha / 255.0f;
1670c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect& rect = layer->layer;
1671c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1672c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDraw();
1673c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawWithTexture();
16748168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            setupDrawColor(a, a, a, a);
1675c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilter();
1676c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1677c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawProgram();
1678c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawPureColorUniforms();
1679c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilterUniforms();
1680c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawTexture(layer->texture);
16814f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            // TODO: The current layer, if any, will be dirtied with the bounding box
16824f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       of the layer we are drawing. Since the layer we are drawing has
16834f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       a mesh, we know the dirty region, we should use it instead
1684c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1685c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
1686c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1687c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1688c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy                    GL_UNSIGNED_SHORT, layer->meshIndices);
1689c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1690c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            finishDrawTexture();
16913a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
16923a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
16933a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            drawRegionRects(layer->region);
16943a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
1695c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        }
1696f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
1697f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#else
1698ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1699ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    composeLayerRect(layer, r);
1700f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
17016c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
17026c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
17036926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
1704d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// Shaders
1705d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1706d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
1707d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyvoid OpenGLRenderer::resetShader() {
170806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
170906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}
171006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
171106f96e2652e4855b6520ad9dd70583677605b79aRomain Guyvoid OpenGLRenderer::setupShader(SkiaShader* shader) {
171206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = shader;
171306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1714fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
171506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    }
17167fac2e18339f765320d759e8d4c090f92431959eRomain Guy}
17177fac2e18339f765320d759e8d4c090f92431959eRomain Guy
1718d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1719db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy// Color filters
1720db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
1721db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1722db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::resetColorFilter() {
1723db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1724db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1725db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1726db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1727db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = filter;
1728db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1729db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1730db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
17311e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy// Drop shadow
17321e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
17331e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17341e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::resetShadow() {
17351e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
17361e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
17371e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17381e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
17391e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = true;
17401e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowRadius = radius;
17411e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDx = dx;
17421e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDy = dy;
17431e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowColor = color;
17441e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
17451e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17461e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
17476926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy// Drawing implementation
17486926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
17496926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
175001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guyvoid OpenGLRenderer::drawPathTexture(const PathTexture* texture,
175101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        float x, float y, SkPaint* paint) {
175201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (quickReject(x, y, x + texture->width, y + texture->height)) {
175301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        return;
175401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    }
175501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
175601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    int alpha;
175701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    SkXfermode::Mode mode;
175801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
175901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
176001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDraw();
176101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawWithTexture(true);
176201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
176301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilter();
176401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShader();
176501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawBlending(true, mode);
176601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawProgram();
176701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawModelView(x, y, x + texture->width, y + texture->height);
176801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawTexture(texture->id);
176901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawPureColorUniforms();
177001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilterUniforms();
177101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShaderUniforms();
177201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
177301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
177401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
177501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
177601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    finishDrawTexture();
177701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
177801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1779f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy// Same values used by Skia
17800a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdStrikeThru_Offset   (-6.0f / 21.0f)
17810a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Offset    (1.0f / 9.0f)
17820a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Thickness (1.0f / 18.0f)
17830a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17840a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guyvoid OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
17850a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float x, float y, SkPaint* paint) {
17860a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    // Handle underline and strike-through
17870a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    uint32_t flags = paint->getFlags();
17880a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
17890a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float underlineWidth = length;
17900a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        // If length is > 0.0f, we already measured the text for the text alignment
17910a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (length <= 0.0f) {
17920a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            underlineWidth = paint->measureText(text, bytesCount);
17930a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17940a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17950a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float offsetX = 0;
17960a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        switch (paint->getTextAlign()) {
17970a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kCenter_Align:
17980a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth * 0.5f;
17990a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18000a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kRight_Align:
18010a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth;
18020a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18030a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            default:
18040a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18050a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
18060a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18070a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (underlineWidth > 0.0f) {
1808e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float textSize = paint->getTextSize();
1809f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            // TODO: Support stroke width < 1.0f when we have AA lines
1810f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
18110a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1812e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float left = x - offsetX;
18130a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            float top = 0.0f;
1814e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1815f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            int linesCount = 0;
1816f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1817f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1818f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy
1819f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const int pointsCount = 4 * linesCount;
1820e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            float points[pointsCount];
1821e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            int currentPoint = 0;
18220a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18230a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) {
18240a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdUnderline_Offset;
1825e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1826e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1827e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1828e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
18290a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
18300a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18310a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) {
18320a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdStrikeThru_Offset;
1833e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1834e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1835e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1836e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
18370a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
1838e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1839e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            SkPaint linesPaint(*paint);
1840e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            linesPaint.setStrokeWidth(strokeWidth);
1841e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1842e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            drawLines(&points[0], pointsCount, &linesPaint);
18430a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
18440a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    }
18450a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy}
18460a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1847026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
18481c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy        int color, SkXfermode::Mode mode, bool ignoreTransform) {
1849d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    // If a shader is set, preserve only the alpha
185006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1851d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        color |= 0x00ffffff;
1852d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
1853d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
185470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
185570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(color);
185670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShader();
185770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
185870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(mode);
185970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
186070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawModelView(left, top, right, bottom, ignoreTransform);
186170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorUniforms();
186270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShaderUniforms(ignoreTransform);
186370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
186470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawSimpleMesh();
1865c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
1866c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1867c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy}
1868c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
186982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
18708164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        Texture* texture, SkPaint* paint) {
187182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    int alpha;
187282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    SkXfermode::Mode mode;
187382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
187482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
18758164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
18768164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy
18776620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
18786620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
18796620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
18806620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
18816620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
18826620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
18836620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
18846620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
18856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
18866620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
18876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
18886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
188985bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy}
189085bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
1891bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
1892a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1893a979474f15b454c8e2963f239a3770e200bb227cRomain Guy    drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
189403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy            (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
1895f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1896f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1897f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guyvoid OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
1898a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
18996820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy        GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
19005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
19018694230ff25fa0a60e480d424843e56b718f0516Romain Guy
190270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
190370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawWithTexture();
190470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(alpha, alpha, alpha, alpha);
190570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
190670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(blend, mode, swapSrcDst);
190770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
190870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!dirty) {
190970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawDirtyRegionsDisabled();
1910db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
19115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!ignoreScale) {
191270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelView(left, top, right, bottom, ignoreTransform);
191303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    } else {
191470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
1915db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
191686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
191770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
191870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawTexture(texture);
191970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawMesh(vertices, texCoords, vbo);
1920db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
19216820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy    glDrawArrays(drawMode, 0, elementsCount);
192270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
192370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    finishDrawTexture();
192482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy}
192582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1926a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guyvoid OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
1927f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        ProgramDescription& description, bool swapSrcDst) {
192882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    blend = blend || mode != SkXfermode::kSrcOver_Mode;
192982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    if (blend) {
1930a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (mode < SkXfermode::kPlus_Mode) {
1931a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!mCaches.blend) {
1932a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glEnable(GL_BLEND);
1933a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
193482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1935f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1936f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
193782ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1938a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1939a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glBlendFunc(sourceMode, destMode);
1940a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastSrcMode = sourceMode;
1941a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastDstMode = destMode;
1942a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1943a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1944a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // These blend modes are not supported by OpenGL directly and have
1945a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // to be implemented using shaders. Since the shader will perform
1946a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // the blending, turn blending off here
1947746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            if (mCaches.extensions.hasFramebufferFetch()) {
1948a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                description.framebufferMode = mode;
1949f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy                description.swapSrcDst = swapSrcDst;
1950a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1951a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1952a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (mCaches.blend) {
1953a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glDisable(GL_BLEND);
1954a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1955a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            blend = false;
195682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        }
1957fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    } else if (mCaches.blend) {
195882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        glDisable(GL_BLEND);
195982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    }
1960fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    mCaches.blend = blend;
1961bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
1962bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
1963889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guybool OpenGLRenderer::useProgram(Program* program) {
1964d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    if (!program->isInUse()) {
1965fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
1966d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        program->use();
1967fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mCaches.currentProgram = program;
19686926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return false;
1969260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    }
19706926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    return true;
1971260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy}
1972260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
1973026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
1974ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    TextureVertex* v = &mMeshVertices[0];
197582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v1);
197682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v1);
197782ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v2);
197882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v2);
19798ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
19808ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19815c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
19828ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    if (paint) {
1983746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        if (!mCaches.extensions.hasFramebufferFetch()) {
1984a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1985a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!isMode) {
1986a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                // Assume SRC_OVER
1987a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                *mode = SkXfermode::kSrcOver_Mode;
1988a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1989a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1990a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            *mode = getXfermode(paint->getXfermode());
19918ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
19928ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19938ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Skia draws using the color's alpha channel if < 255
19948ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Otherwise, it uses the paint's alpha
19958ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        int color = paint->getColor();
19968ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = (color >> 24) & 0xFF;
19978ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        if (*alpha == 255) {
19988ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy            *alpha = paint->getAlpha();
19998ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
20008ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    } else {
20018ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *mode = SkXfermode::kSrcOver_Mode;
20028ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = 255;
20038ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    }
2004026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy}
2005026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
2006a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain GuySkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
2007b37cbec6d06578a72bdd9c2caa3fd964ade71c53Romain Guy    // In the future we should look at unifying the Porter-Duff modes and
2008b37cbec6d06578a72bdd9c2caa3fd964ade71c53Romain Guy    // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
2009a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    if (mode == NULL) {
2010a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        return SkXfermode::kSrcOver_Mode;
2011a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    }
2012a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    return mode->fMode;
2013a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy}
2014a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
2015746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guyvoid OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
20168164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    bool bound = false;
20178164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapS != texture->wrapS) {
20188164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glBindTexture(GL_TEXTURE_2D, texture->id);
20198164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        bound = true;
20208164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
20218164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapS = wrapS;
20228164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
20238164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapT != texture->wrapT) {
20243e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        if (!bound) {
20253e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy            glBindTexture(GL_TEXTURE_2D, texture->id);
20263e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        }
20278164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
20288164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapT = wrapT;
20298164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
2030a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
2031a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
20329d5316e3f56d138504565ff311145ac01621dff4Romain Guy}; // namespace uirenderer
2033e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}; // namespace android
2034