OpenGLRenderer.cpp revision 7d7b5490a0b0763e831b31bc11f17d8159b5914a
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
295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#include <ui/Rect.h>
305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
3185bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy#include "OpenGLRenderer.h"
320fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy#include "DisplayListRenderer.h"
33a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy#include "Vector.h"
34e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
35e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guynamespace android {
369d5316e3f56d138504565ff311145ac01621dff4Romain Guynamespace uirenderer {
379d5316e3f56d138504565ff311145ac01621dff4Romain Guy
389d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
399d5316e3f56d138504565ff311145ac01621dff4Romain Guy// Defines
409d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
419d5316e3f56d138504565ff311145ac01621dff4Romain Guy
42759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy#define RAD_TO_DEG (180.0f / 3.14159265f)
43759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy#define MIN_ANGLE 0.001f
44759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
45dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy// TODO: This should be set in properties
46dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy#define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH)
47dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
489d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
499d5316e3f56d138504565ff311145ac01621dff4Romain Guy// Globals
509d5316e3f56d138504565ff311145ac01621dff4Romain Guy///////////////////////////////////////////////////////////////////////////////
519d5316e3f56d138504565ff311145ac01621dff4Romain Guy
52889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy/**
53889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy * Structure mapping Skia xfermodes to OpenGL blending factors.
54889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy */
55889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guystruct Blender {
56889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    SkXfermode::Mode mode;
57889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    GLenum src;
58889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy    GLenum dst;
59889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy}; // struct Blender
60889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guy
61026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy// In this array, the index of each Blender equals the value of the first
62026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
63026c5e16704e817cac7d9c382914c947e34f87e0Romain Guystatic const Blender gBlends[] = {
64b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kClear_Mode,   GL_ZERO,                 GL_ZERO },
65b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrc_Mode,     GL_ONE,                  GL_ZERO },
66b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDst_Mode,     GL_ZERO,                 GL_ONE },
67b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOver_Mode, GL_ONE,                  GL_ONE_MINUS_SRC_ALPHA },
68b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_ONE },
69b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcIn_Mode,   GL_DST_ALPHA,            GL_ZERO },
70b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstIn_Mode,   GL_ZERO,                 GL_SRC_ALPHA },
71b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOut_Mode,  GL_ONE_MINUS_DST_ALPHA,  GL_ZERO },
72b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOut_Mode,  GL_ZERO,                 GL_ONE_MINUS_SRC_ALPHA },
73b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA,            GL_ONE_MINUS_SRC_ALPHA },
74b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_SRC_ALPHA },
75b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kXor_Mode,     GL_ONE_MINUS_DST_ALPHA,  GL_ONE_MINUS_SRC_ALPHA }
76026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy};
77e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
7887a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// This array contains the swapped version of each SkXfermode. For instance
7987a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// this array's SrcOver blending mode is actually DstOver. You can refer to
8087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy// createLayer() for more information on the purpose of this array.
81f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guystatic const Blender gBlendsSwap[] = {
82b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kClear_Mode,   GL_ZERO,                 GL_ZERO },
83b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrc_Mode,     GL_ZERO,                 GL_ONE },
84b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDst_Mode,     GL_ONE,                  GL_ZERO },
85b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_ONE },
86b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOver_Mode, GL_ONE,                  GL_ONE_MINUS_SRC_ALPHA },
87b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcIn_Mode,   GL_ZERO,                 GL_SRC_ALPHA },
88b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstIn_Mode,   GL_DST_ALPHA,            GL_ZERO },
89b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcOut_Mode,  GL_ZERO,                 GL_ONE_MINUS_SRC_ALPHA },
90b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstOut_Mode,  GL_ONE_MINUS_DST_ALPHA,  GL_ZERO },
91b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_SRC_ALPHA },
92b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kDstATop_Mode, GL_DST_ALPHA,            GL_ONE_MINUS_SRC_ALPHA },
93b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    { SkXfermode::kXor_Mode,     GL_ONE_MINUS_DST_ALPHA,  GL_ONE_MINUS_SRC_ALPHA }
94f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy};
95f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
96889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guystatic const GLenum gTextureUnits[] = {
97b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE0,
98b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE1,
99b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    GL_TEXTURE2
100d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy};
101d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
102f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
103f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Constructors/destructor
104f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
105f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
106fb8b763f762ae21923c58d64caa729b012f40e05Romain GuyOpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) {
10706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
108db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1091e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
110026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
111ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
112ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy
113ae5575b3421c8fbe590ab046d7d5f2b36ecfd821Romain Guy    mFirstSnapshot = new Snapshot;
114e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
115e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
11685bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain GuyOpenGLRenderer::~OpenGLRenderer() {
11729d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    // The context has already been destroyed at this point, do not call
11829d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    // GL APIs. All GL state should be kept in Caches.h
119e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
120e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
121f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
122f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Setup
123f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
124f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
12585bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::setViewport(int width, int height) {
12608ae317c21ec3086b5017672bba87420cc38a407Romain Guy    glViewport(0, 0, width, height);
127260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
128bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
129bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy    mWidth = width;
130bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy    mHeight = height;
131eb99356a0548684a501766e6a524529ab93304c8Romain Guy
132eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mFirstSnapshot->height = height;
133eb99356a0548684a501766e6a524529ab93304c8Romain Guy    mFirstSnapshot->viewport.set(0, 0, width, height);
134746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
135746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
136e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
137e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
1386b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guyvoid OpenGLRenderer::prepare(bool opaque) {
1397d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
1407d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy}
1417d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1427d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guyvoid OpenGLRenderer::prepareDirty(float left, float top, float right, float bottom, bool opaque) {
143fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy    mCaches.clearGarbage();
144fe48f65922d4a3cc4aefe058cee5acec51504a20Romain Guy
1458aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mFirstSnapshot,
1468aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy            SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
1478fb954263dd2f918ad339045cc6d82e346515599Romain Guy    mSaveCount = 1;
148f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
149fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glViewport(0, 0, mWidth, mHeight);
150fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy
151d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    glDisable(GL_DITHER);
152bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
1537d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glEnable(GL_SCISSOR_TEST);
1547d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
1557d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    mSnapshot->setClip(left, top, right, bottom);
1567d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1576b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    if (!opaque) {
1586b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1596b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClear(GL_COLOR_BUFFER_BIT);
1606b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    }
161bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
162bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
163b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guyvoid OpenGLRenderer::finish() {
164b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#if DEBUG_OPENGL
165b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    GLenum status = GL_NO_ERROR;
166b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    while ((status = glGetError()) != GL_NO_ERROR) {
167b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        LOGD("GL error from OpenGLRenderer: 0x%x", status);
168a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        switch (status) {
169a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy            case GL_OUT_OF_MEMORY:
170a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                LOGE("  OpenGLRenderer is out of memory!");
171a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                break;
172a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        }
173b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
174b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#endif
175c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#if DEBUG_MEMORY_USAGE
176c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    mCaches.dumpMemoryUsage();
177e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy#else
178e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    if (mCaches.getDebugLevel() & kDebugMemory) {
179e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy        mCaches.dumpMemoryUsage();
180e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    }
181c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#endif
182b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy}
183b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy
1846c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::interrupt() {
185da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    if (mCaches.currentProgram) {
186da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        if (mCaches.currentProgram->isInUse()) {
187da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram->remove();
188da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram = NULL;
189da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        }
190da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    }
19150c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.unbindMeshBuffer();
192da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
193da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
1946c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::acquireContext() {
1956c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    interrupt();
1966c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
1976c319ca1275c8db892c39b48fc54864c949f9171Romain 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
2156c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::releaseContext() {
2166c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    resume();
2176c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
2186c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
219daf98e941e140e8739458126640183b9f296a2abChet Haasebool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
220daf98e941e140e8739458126640183b9f296a2abChet Haase    interrupt();
221daf98e941e140e8739458126640183b9f296a2abChet Haase    status_t result = (*functor)();
222daf98e941e140e8739458126640183b9f296a2abChet Haase    resume();
223daf98e941e140e8739458126640183b9f296a2abChet Haase    return (result == 0) ? false : true;
224daf98e941e140e8739458126640183b9f296a2abChet Haase}
225daf98e941e140e8739458126640183b9f296a2abChet Haase
226f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
227f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// State management
228f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
229f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
230bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::getSaveCount() const {
2317ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    return mSaveCount;
232bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
233bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
234bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::save(int flags) {
2358aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return saveSnapshot(flags);
236bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
237bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
238bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restore() {
2392542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (mSaveCount > 1) {
2402542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2417ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
242bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
243bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
244bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restoreToCount(int saveCount) {
2458fb954263dd2f918ad339045cc6d82e346515599Romain Guy    if (saveCount < 1) saveCount = 1;
246bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2478fb954263dd2f918ad339045cc6d82e346515599Romain Guy    while (mSaveCount > saveCount) {
2482542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2497ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
250bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
251bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2528aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guyint OpenGLRenderer::saveSnapshot(int flags) {
2538aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mSnapshot, flags);
2548fb954263dd2f918ad339045cc6d82e346515599Romain Guy    return mSaveCount++;
255bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
256bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
257bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guybool OpenGLRenderer::restoreSnapshot() {
2587ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
259bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
260eb99356a0548684a501766e6a524529ab93304c8Romain Guy    bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
261bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
262bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    sp<Snapshot> current = mSnapshot;
2637ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    sp<Snapshot> previous = mSnapshot->previous;
264bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
265eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (restoreOrtho) {
266eb99356a0548684a501766e6a524529ab93304c8Romain Guy        Rect& r = previous->viewport;
267eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glViewport(r.left, r.top, r.right, r.bottom);
268eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mOrthoMatrix.load(current->orthoMatrix);
269eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
270eb99356a0548684a501766e6a524529ab93304c8Romain Guy
2718b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSaveCount--;
2728b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSnapshot = previous;
2738b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
2742542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (restoreClip) {
275746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
2768fb954263dd2f918ad339045cc6d82e346515599Romain Guy    }
2772542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy
2785ec9924d24495822b589f1a17996655d66273b30Romain Guy    if (restoreLayer) {
2795ec9924d24495822b589f1a17996655d66273b30Romain Guy        composeLayer(current, previous);
2805ec9924d24495822b589f1a17996655d66273b30Romain Guy    }
2815ec9924d24495822b589f1a17996655d66273b30Romain Guy
2822542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    return restoreClip;
283d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
2845cbbce535744b89df5ecea95de21ee3733298260Romain Guy
285f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
286bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// Layers
287bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
288bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
289bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
2905c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        SkPaint* p, int flags) {
291eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const GLuint previousFbo = mSnapshot->fbo;
292eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const int count = saveSnapshot(flags);
293d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
294af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (!mSnapshot->isIgnored()) {
295e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        int alpha = 255;
296e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        SkXfermode::Mode mode;
297e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
298e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        if (p) {
299e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            alpha = p->getAlpha();
300e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            if (!mCaches.extensions.hasFramebufferFetch()) {
301e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
302e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                if (!isMode) {
303e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    // Assume SRC_OVER
304e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    mode = SkXfermode::kSrcOver_Mode;
305e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                }
306e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            } else {
307e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                mode = getXfermode(p->getXfermode());
308a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
309a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
310e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            mode = SkXfermode::kSrcOver_Mode;
311d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy        }
312d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
313dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
314dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
315d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
316d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return count;
317bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
318bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
319bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
320bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        int alpha, int flags) {
3215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (alpha >= 255 - ALPHA_THRESHOLD) {
3228aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, NULL, flags);
3238b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    } else {
3248aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        SkPaint paint;
3258aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        paint.setAlpha(alpha);
3268aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, &paint, flags);
3278b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    }
328d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
329bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
3301c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
3311c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Layers are viewed by Skia are slightly different than layers in image editing
3321c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * programs (for instance.) When a layer is created, previously created layers
3331c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * and the frame buffer still receive every drawing command. For instance, if a
3341c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * layer is created and a shape intersecting the bounds of the layers and the
3351c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * framebuffer is draw, the shape will be drawn on both (unless the layer was
3361c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
3371c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3381c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * A way to implement layers is to create an FBO for each layer, backed by an RGBA
3391c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * texture. Unfortunately, this is inefficient as it requires every primitive to
3401c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be drawn n + 1 times, where n is the number of active layers. In practice this
3411c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * means, for every primitive:
3421c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Switch active frame buffer
3431c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Change viewport, clip and projection matrix
3441c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Issue the drawing
3451c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3461c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Switching rendering target n + 1 times per drawn primitive is extremely costly.
3476b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * To avoid this, layers are implemented in a different way here, at least in the
3486b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * general case. FBOs are used, as an optimization, when the "clip to layer" flag
3496b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * is set. When this flag is set we can redirect all drawing operations into a
3506b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * single FBO.
3511c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3521c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * This implementation relies on the frame buffer being at least RGBA 8888. When
3531c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * a layer is created, only a texture is created, not an FBO. The content of the
3541c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer contained within the layer's bounds is copied into this texture
35587a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
3561c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * buffer and drawing continues as normal. This technique therefore treats the
3571c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer as a scratch buffer for the layers.
3581c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3591c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * To compose the layers back onto the frame buffer, each layer texture
3601c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * (containing the original frame buffer data) is drawn as a simple quad over
3611c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * the frame buffer. The trick is that the quad is set as the composition
3621c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * destination in the blending equation, and the frame buffer becomes the source
3631c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * of the composition.
3641c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3651c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Drawing layers with an alpha value requires an extra step before composition.
3661c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * An empty quad is drawn over the layer's region in the frame buffer. This quad
3671c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
3681c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * quad is used to multiply the colors in the frame buffer. This is achieved by
3691c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * changing the GL blend functions for the GL_FUNC_ADD blend equation to
3701c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * GL_ZERO, GL_SRC_ALPHA.
3711c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3721c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Because glCopyTexImage2D() can be slow, an alternative implementation might
3731c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be use to draw a single clipped layer. The implementation described above
3741c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is correct in every case.
37587a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *
37687a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * (1) The frame buffer is actually not cleared right away. To allow the GPU
37787a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     to potentially optimize series of calls to glCopyTexImage2D, the frame
37887a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     buffer is left untouched until the first drawing operation. Only when
37987a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     something actually gets drawn are the layers regions cleared.
3801c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
381d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guybool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
382eb99356a0548684a501766e6a524529ab93304c8Romain Guy        float right, float bottom, int alpha, SkXfermode::Mode mode,
383eb99356a0548684a501766e6a524529ab93304c8Romain Guy        int flags, GLuint previousFbo) {
384eb99356a0548684a501766e6a524529ab93304c8Romain Guy    LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
385fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
386dda570201ac851dd85af3861f7e575721d3345daRomain Guy
387eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
388eb99356a0548684a501766e6a524529ab93304c8Romain Guy
389f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    // Window coordinates of the layer
3908aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    Rect bounds(left, top, right, bottom);
391ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    if (fboLayer) {
392ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        // Clear the previous layer regions before we change the viewport
393ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        clearLayerRegions();
394ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    } else {
395eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mSnapshot->transform->mapRect(bounds);
396ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
397eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Layers only make sense if they are in the framebuffer's bounds
39858ae6db4ff8a9d0910e1183ee8be9a038a2712a6Romain Guy        bounds.intersect(*snapshot->clipRect);
399ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
400ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        // We cannot work with sub-pixels in this case
401ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        bounds.snapToPixelBoundaries();
402ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy
403ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // When the layer is not an FBO, we may use glCopyTexImage so we
404ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // need to make sure the layer does not extend outside the bounds
405ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // of the framebuffer
406ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        bounds.intersect(snapshot->previous->viewport);
407eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
408bf434114cbf55b216fdc76fc8d65a75e84c9dab5Romain Guy
409746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
410746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            bounds.getHeight() > mCaches.maxTextureSize) {
41132963c393a804db2cd86c24443c2f1c6eee6064bRomain Guy        snapshot->empty = fboLayer;
412dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    } else {
413e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
414dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
415dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
416dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    // Bail out if we won't draw in this snapshot
417af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (snapshot->invisible || snapshot->empty) {
418b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        return false;
419b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
420dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glActiveTexture(gTextureUnits[0]);
4228550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
423f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy    if (!layer) {
424f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy        return false;
425bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    }
426bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
427dda570201ac851dd85af3861f7e575721d3345daRomain Guy    layer->mode = mode;
428f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    layer->alpha = alpha;
4298aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    layer->layer.set(bounds);
4308550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
4318550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy            bounds.getWidth() / float(layer->width), 0.0f);
432171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy    layer->colorFilter = mColorFilter;
433dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4348fb954263dd2f918ad339045cc6d82e346515599Romain Guy    // Save the layer in the snapshot
4358fb954263dd2f918ad339045cc6d82e346515599Romain Guy    snapshot->flags |= Snapshot::kFlagIsLayer;
436dda570201ac851dd85af3861f7e575721d3345daRomain Guy    snapshot->layer = layer;
4371d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
438eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
4395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return createFboLayer(layer, bounds, snapshot, previousFbo);
440eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
441eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Copy the framebuffer into the layer
442eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindTexture(GL_TEXTURE_2D, layer->texture);
443514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!bounds.isEmpty()) {
444514fb18827186591d66973c2362c859b64b63556Romain Guy            if (layer->empty) {
445514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
446514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, layer->width, layer->height, 0);
447514fb18827186591d66973c2362c859b64b63556Romain Guy                layer->empty = false;
448514fb18827186591d66973c2362c859b64b63556Romain Guy            } else {
449514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
450514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
451514fb18827186591d66973c2362c859b64b63556Romain Guy            }
452514fb18827186591d66973c2362c859b64b63556Romain Guy            // Enqueue the buffer coordinates to clear the corresponding region later
453514fb18827186591d66973c2362c859b64b63556Romain Guy            mLayers.push(new Rect(bounds));
454ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        }
455eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
456f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
457d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return true;
458bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
459bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
4605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guybool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
4615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLuint previousFbo) {
4625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    layer->fbo = mCaches.fboCache.get();
4635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
4655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->region = &snapshot->layer->region;
4665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagFboTarget;
4675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
4685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clip(bounds);
4705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->transform->mapRect(clip);
4715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(*snapshot->clipRect);
4725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
4735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(snapshot->previous->viewport);
4745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mat4 inverse;
4765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.loadInverse(*mSnapshot->transform);
4775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.mapRect(clip);
4795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
4805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(bounds);
4815ec9924d24495822b589f1a17996655d66273b30Romain Guy    clip.translate(-bounds.left, -bounds.top);
4825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagIsFboLayer;
4845b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->fbo = layer->fbo;
4855b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
4865b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
4875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
4885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->height = bounds.getHeight();
4895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagDirtyOrtho;
4905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->orthoMatrix.load(mOrthoMatrix);
4915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Bind texture to FBO
4935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
4945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindTexture(GL_TEXTURE_2D, layer->texture);
4955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Initialize the texture if needed
4975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->empty) {
4985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->empty = false;
4995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
5005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
5015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
5045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->texture, 0);
5055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
5075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (status != GL_FRAMEBUFFER_COMPLETE) {
5095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
5105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
5125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glDeleteTextures(1, &layer->texture);
5135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        mCaches.fboCache.put(layer->fbo);
5145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        delete layer;
5165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return false;
5185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
5225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
5235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
5245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
5255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClear(GL_COLOR_BUFFER_BIT);
5265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    dirtyClip();
5285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Change the ortho projection
5305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
5315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
5325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    return true;
5345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
5355b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5361c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
5371c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Read the documentation of createLayer() before doing anything in this method.
5381c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
5391d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guyvoid OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
5401d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    if (!current->layer) {
5411d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LOGE("Attempting to compose a layer that does not exist");
5421d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        return;
5431d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
5441d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
5455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
546eb99356a0548684a501766e6a524529ab93304c8Romain Guy
547eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
548eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Unbind current FBO and restore previous one
549eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
550eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
551eb99356a0548684a501766e6a524529ab93304c8Romain Guy
5521d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Layer* layer = current->layer;
5531d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    const Rect& rect = layer->layer;
5541d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
555eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (!fboLayer && layer->alpha < 255) {
556f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
5571c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy                layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
5585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Required below, composeLayerRect() will divide by 255
5595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->alpha = 255;
560f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    }
561f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
56203750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
5638b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
564746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
5655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // When the layer is stored in an FBO, we can save a bit of fillrate by
5675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // drawing only the dirty region
568eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
5695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
570171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
571171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            setupColorFilter(layer->colorFilter);
572171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
5735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRegion(layer, rect);
574171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
575171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            resetColorFilter();
576171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
577eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
578514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!rect.isEmpty()) {
579514fb18827186591d66973c2362c859b64b63556Romain Guy            dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
580514fb18827186591d66973c2362c859b64b63556Romain Guy            composeLayerRect(layer, rect, true);
581514fb18827186591d66973c2362c859b64b63556Romain Guy        }
582eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
5831d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
584eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
585eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Detach the texture from the FBO
586eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
587eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
588eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
589eb99356a0548684a501766e6a524529ab93304c8Romain Guy
590eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
591eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mCaches.fboCache.put(current->fbo);
592eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
593eb99356a0548684a501766e6a524529ab93304c8Romain Guy
594746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
595746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
596eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // Failing to add the layer to the cache should happen only if the layer is too large
5978550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    if (!mCaches.layerCache.put(layer)) {
5981d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LAYER_LOGD("Deleting layer");
5991d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        glDeleteTextures(1, &layer->texture);
6001d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        delete layer;
6011d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
6021d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy}
6031d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
6045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
6055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const Rect& texCoords = layer->texCoords;
6065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
6075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
6095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
6105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
6115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
6135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
6145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
6165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
6175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->region.isRect()) {
6185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRect(layer, rect);
6195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
6205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return;
6215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
6225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!layer->region.isEmpty()) {
6245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        size_t count;
6255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const android::Rect* rects = layer->region.getArray(&count);
6265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float alpha = layer->alpha / 255.0f;
6285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texX = 1.0f / float(layer->width);
6295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texY = 1.0f / float(layer->height);
630f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float height = rect.getHeight();
6315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        TextureVertex* mesh = mCaches.getRegionMesh();
6335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLsizei numQuads = 0;
6345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6357230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDraw();
6367230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawWithTexture();
6377230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawColor(alpha, alpha, alpha, alpha);
638ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilter();
6397230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
6407230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawProgram();
6417230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawDirtyRegionsDisabled();
6427230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawPureColorUniforms();
643ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilterUniforms();
6447230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawTexture(layer->texture);
645c038ea358aadca082198f3effc550d33135bf426Romain Guy        setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
6467230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
6475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        for (size_t i = 0; i < count; i++) {
6495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const android::Rect* r = &rects[i];
6505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u1 = r->left * texX;
652f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v1 = (height - r->top) * texY;
6535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u2 = r->right * texX;
654f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v2 = (height - r->bottom) * texY;
6555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // TODO: Reject quads outside of the clip
6575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->top, u1, v1);
6585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->top, u2, v1);
6595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
6605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
6615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            numQuads++;
6635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            if (numQuads >= REGION_MESH_QUAD_COUNT) {
6655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
6665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                numQuads = 0;
6675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                mesh = mCaches.getRegionMesh();
6685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
6695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
6705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (numQuads > 0) {
6725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
6735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
6745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6767230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        finishDrawTexture();
6775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
6795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        uint32_t colors[] = {
6805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                0x7fff0000, 0x7f00ff00,
6815b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                0x7f0000ff, 0x7fff00ff,
6825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        };
6835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6845b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        int offset = 0;
6855b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        int32_t top = rects[0].top;
6865b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        int i = 0;
6875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        for (size_t i = 0; i < count; i++) {
6895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            if (top != rects[i].top) {
6905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                offset ^= 0x2;
6915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                top = rects[i].top;
6925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
6935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
6955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
6965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                    SkXfermode::kSrcOver_Mode);
6975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
6985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
6995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
7015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
7035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    composeLayerRect(layer, rect);
7045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom, const mat4 transform) {
7095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
710f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
7125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        transform.mapRect(bounds);
713f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom) {
7205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
721f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
723f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7241bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    }
7251bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#endif
7261bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy}
7271bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy
7281bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guyvoid OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
7291bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#if RENDER_LAYERS_AS_REGIONS
7301bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    if (bounds.intersect(*mSnapshot->clipRect)) {
7311bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        bounds.snapToPixelBoundaries();
7321bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
7331bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        if (!dirty.isEmpty()) {
7341bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy            region->orSelf(dirty);
7355b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7365b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7375b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7385b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7408694230ff25fa0a60e480d424843e56b718f0516Romain Guyvoid OpenGLRenderer::clearLayerRegions() {
741af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
7428694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7435b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clipRect(*mSnapshot->clipRect);
7445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clipRect.snapToPixelBoundaries();
7455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7468694230ff25fa0a60e480d424843e56b718f0516Romain Guy    for (uint32_t i = 0; i < mLayers.size(); i++) {
7478694230ff25fa0a60e480d424843e56b718f0516Romain Guy        Rect* bounds = mLayers.itemAt(i);
7485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (clipRect.intersects(*bounds)) {
7495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // Clear the framebuffer where the layer will draw
7505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glScissor(bounds->left, mSnapshot->height - bounds->bottom,
7515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                    bounds->getWidth(), bounds->getHeight());
7525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
7535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glClear(GL_COLOR_BUFFER_BIT);
7545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // Restore the clip
7565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            dirtyClip();
7575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7588694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7598694230ff25fa0a60e480d424843e56b718f0516Romain Guy        delete bounds;
7608694230ff25fa0a60e480d424843e56b718f0516Romain Guy    }
7618694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mLayers.clear();
7638694230ff25fa0a60e480d424843e56b718f0516Romain Guy}
7648694230ff25fa0a60e480d424843e56b718f0516Romain Guy
765bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
766f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Transforms
767f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
768f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
769f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::translate(float dx, float dy) {
7708aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->translate(dx, dy, 0.0f);
771f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
772f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
773f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::rotate(float degrees) {
7748aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
775f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
776f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
777f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::scale(float sx, float sy) {
7788aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->scale(sx, sy, 1.0f);
779f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
780f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
781807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guyvoid OpenGLRenderer::skew(float sx, float sy) {
782807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy    mSnapshot->transform->skew(sx, sy);
783807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy}
784807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy
785f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::setMatrix(SkMatrix* matrix) {
7868aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->load(*matrix);
787f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
788f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
78941030da16856c8869e1e51d4a0405432fa96614eRomain Guyconst float* OpenGLRenderer::getMatrix() const {
79099bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    if (mSnapshot->fbo != 0) {
79199bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy        return &mSnapshot->transform->data[0];
79299bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    }
79399bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    return &mIdentity.data[0];
79441030da16856c8869e1e51d4a0405432fa96614eRomain Guy}
79541030da16856c8869e1e51d4a0405432fa96614eRomain Guy
796f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::getMatrix(SkMatrix* matrix) {
7978aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->copyTo(*matrix);
798f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
799f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
800f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
801e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    SkMatrix transform;
802e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->copyTo(transform);
803e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    transform.preConcat(*matrix);
804e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->load(transform);
805f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
806f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
807f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
808f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Clipping
809f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
810f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
811bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::setScissorFromClip() {
812e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    Rect clip(*mSnapshot->clipRect);
813e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    clip.snapToPixelBoundaries();
814eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
815746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
8169d5316e3f56d138504565ff311145ac01621dff4Romain Guy}
8179d5316e3f56d138504565ff311145ac01621dff4Romain Guy
8189d5316e3f56d138504565ff311145ac01621dff4Romain Guyconst Rect& OpenGLRenderer::getClipBounds() {
819079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return mSnapshot->getLocalClip();
820bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
821bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
822c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guybool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
823af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) {
824dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        return true;
825dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
826dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
8271d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Rect r(left, top, right, bottom);
8288aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->mapRect(r);
829d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    r.snapToPixelBoundaries();
830d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
831d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    Rect clipRect(*mSnapshot->clipRect);
832d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    clipRect.snapToPixelBoundaries();
833d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
834d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    return !clipRect.intersects(r);
835c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
836c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
837079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guybool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
838079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    bool clipped = mSnapshot->clip(left, top, right, bottom, op);
8397ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    if (clipped) {
840746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
8417ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
8428aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return !mSnapshot->clipRect->isEmpty();
843e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
844e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
845f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
84670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy// Drawing commands
84770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
84870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
84970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDraw() {
85070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    clearLayerRegions();
85170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mDirtyClip) {
85270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setScissorFromClip();
85370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
85470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.reset();
85570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = false;
85670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = false;
85770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = mColorR = mColorG = mColorB = 0.0f;
85870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTextureUnit = 0;
85970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = true;
8608d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mTexCoordsSlot = -1;
86170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
86270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
86370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
86470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasTexture = true;
86570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasAlpha8Texture = isAlpha8;
86670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
86770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
86870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(int color) {
8698d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(color, (color >> 24) & 0xFF);
8708d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
8718d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
8728d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawColor(int color, int alpha) {
8738d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mColorA = alpha / 255.0f;
87470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    const float a = mColorA / 255.0f;
875fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorR = a * ((color >> 16) & 0xFF);
876fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorG = a * ((color >>  8) & 0xFF);
877fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorB = a * ((color      ) & 0xFF);
87870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
87970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
88070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
88170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
88286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
88386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = alpha / 255.0f;
88486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    const float a = mColorA / 255.0f;
88586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = a * ((color >> 16) & 0xFF);
88686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = a * ((color >>  8) & 0xFF);
88786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = a * ((color      ) & 0xFF);
88886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
88986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
89086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
89186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
89270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
89370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = a;
89470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorR = r;
89570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorG = g;
89670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorB = b;
89770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
89870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(r, g, b, a);
89970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
90070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
90186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
90286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = a;
90386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = r;
90486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = g;
90586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = b;
90686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
90786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
90886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
90986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
91070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShader() {
91170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
91270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->describe(mDescription, mCaches.extensions);
91370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
91470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
91570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
91670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilter() {
91770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
91870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->describe(mDescription, mCaches.extensions);
91970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
92070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
92170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
92270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
92370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
92470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
92570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
92670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
92770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
92870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
92970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
93070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawProgram() {
93370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    useProgram(mCaches.programCache.get(mDescription));
93470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
93770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = false;
93870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
94170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform) {
94270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mModelView.loadTranslate(left, top, 0.0f);
94370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreTransform) {
94470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
94570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
94670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
94770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
94870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
94970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
95070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
9528d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawModelViewIdentity() {
9538d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
9548d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
9558d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
95670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
95770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform, bool ignoreModelView) {
95870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreModelView) {
95970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadTranslate(left, top, 0.0f);
96070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.scale(right - left, bottom - top, 1.0f);
96170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
96270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadIdentity();
96370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
96486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    bool dirty = right - left > 0.0f && bottom - top > 0.0f;
96586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    if (!ignoreTransform) {
96686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
96786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) {
96886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy            dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
96986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        }
97086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    } else {
97186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
97286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
97386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    }
97470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
97570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
97670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorUniforms() {
9775536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mColorSet || (mShader && mSetShaderColor)) {
97870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
97970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
98070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
98170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
98286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawPureColorUniforms() {
9835536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mSetShaderColor) {
98486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
9855536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    }
9865536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy}
9875536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy
98870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
98970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
99070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (ignoreTransform) {
99170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mModelView.loadInverse(*mSnapshot->transform);
99270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        }
99370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
99470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
99570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
99670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
9978d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawShaderIdentityUniforms() {
9988d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mShader) {
9998d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
10008d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
10018d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
10028d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
100370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilterUniforms() {
100470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
100570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->setupProgram(mCaches.currentProgram);
100670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
100770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
100870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
100970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawSimpleMesh() {
101070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mCaches.bindMeshBuffer();
101170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
101270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, 0);
101370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
101470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
101570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawTexture(GLuint texture) {
101670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    bindTexture(texture);
101770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
101870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
101970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
102070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glEnableVertexAttribArray(mTexCoordsSlot);
102170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
102270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
102370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
102470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!vertices) {
102570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
102670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
102770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.unbindMeshBuffer();
102870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
102970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
103070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, vertices);
10318d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mTexCoordsSlot > 0) {
10328d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
10338d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
103470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::finishDrawTexture() {
103770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glDisableVertexAttribArray(mTexCoordsSlot);
103870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
104070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
1041f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Drawing
1042f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
1043f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
1044daf98e941e140e8739458126640183b9f296a2abChet Haasebool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
10450fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // All the usual checks and setup operations (quickReject, setupDraw, etc.)
10460fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // will be performed by the display list itself
10470fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    if (displayList) {
1048daf98e941e140e8739458126640183b9f296a2abChet Haase        return displayList->replay(*this, level);
10490fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    }
1050daf98e941e140e8739458126640183b9f296a2abChet Haase    return false;
10510fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy}
10520fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy
10535c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
10546926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float right = left + bitmap->width();
10556926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float bottom = top + bitmap->height();
10566926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
10576926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
10586926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10596926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10606926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
106186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10628164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10639cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
106422158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
106522158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10666926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    drawTextureRect(left, top, right, bottom, texture, paint);
10678ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
10688ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
10695c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
1070f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1071f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    const mat4 transform(*matrix);
1072f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    transform.mapRect(r);
1073f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
10746926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(r.left, r.top, r.right, r.bottom)) {
10756926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10766926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10776926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
107886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10798164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10809cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
108122158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
108222158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // This could be done in a cheaper way, all we need is pass the matrix
10845b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // to the vertex shader. The save/restore is a bit overkill.
10855b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    save(SkCanvas::kMatrix_SaveFlag);
10865b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    concatMatrix(matrix);
10875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
10885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    restore();
1089f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy}
1090f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
10915a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guyvoid OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
10925a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        float* vertices, int* colors, SkPaint* paint) {
10935a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    // TODO: Do a quickReject
10945a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!vertices || mSnapshot->isIgnored()) {
10955a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        return;
10965a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
10975a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
10985a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    glActiveTexture(gTextureUnits[0]);
10995a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11005a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!texture) return;
11015a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const AutoTexture autoCleanup(texture);
11025a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11035a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11045a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    int alpha;
11055a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    SkXfermode::Mode mode;
11065a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
11075a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11085a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const uint32_t count = meshWidth * meshHeight * 6;
11095a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1110a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    // TODO: Support the colors array
1111a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    TextureVertex mesh[count];
11125a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    TextureVertex* vertex = mesh;
11135a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    for (int32_t y = 0; y < meshHeight; y++) {
11145a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        for (int32_t x = 0; x < meshWidth; x++) {
11155a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            uint32_t i = (y * (meshWidth + 1) + x) * 2;
11165a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11175a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u1 = float(x) / meshWidth;
11185a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u2 = float(x + 1) / meshWidth;
11195a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v1 = float(y) / meshHeight;
11205a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v2 = float(y + 1) / meshHeight;
11215a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11225a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ax = i + (meshWidth + 1) * 2;
11235a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ay = ax + 1;
11245a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int bx = i;
11255a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int by = bx + 1;
11265a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cx = i + 2;
11275a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cy = cx + 1;
11285a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dx = i + (meshWidth + 1) * 2 + 2;
11295a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dy = dx + 1;
11305a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11315a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11325a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
11335a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11345a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11355a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11365a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11375a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
11385a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        }
11395a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11405a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11415a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
11425a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
11435a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            GL_TRIANGLES, count);
11445a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy}
11455a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11468ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guyvoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
11478ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float srcLeft, float srcTop, float srcRight, float srcBottom,
11488ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float dstLeft, float dstTop, float dstRight, float dstBottom,
11495c13d89c1332fcc499379b9064b891187b75ca32Chet Haase         SkPaint* paint) {
11506926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
11516926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
11526926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
11536926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1154746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
11558164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11569cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
115722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
11588164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11598ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
11608ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float width = texture->width;
11618ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float height = texture->height;
1162c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
11638ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u1 = srcLeft / width;
11648ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v1 = srcTop / height;
11658ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u2 = srcRight / width;
11668ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v2 = srcBottom / height;
1167c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
116803750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
11698ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(u1, v1, u2, v2);
11708ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
117103750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    int alpha;
117203750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    SkXfermode::Mode mode;
117303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
117403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy
11756620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
11766620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
11776620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
11786620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
11796620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
11806620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->id, alpha / 255.0f, mode, texture->blend,
11816620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
11826620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount, false, true);
11836620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
11846620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
11856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
11866620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
11876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
11888ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
11898ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1190ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
1191ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
11924aa90573bbf86db0d33a3a790c5dbd0d93b95cfeRomain Guyvoid OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
11934bb942083a0d4db746adf95349108dd8ef842e32Romain Guy        const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
11945c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        float left, float top, float right, float bottom, SkPaint* paint) {
11956926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
11966926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
11976926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
11986926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1199746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12008164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12019cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
120222158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12038164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1204f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1205f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    int alpha;
1206f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    SkXfermode::Mode mode;
1207f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
1208f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12092728f961614a385df1f056fc24803a9f65c90fabRomain Guy    const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
12104bb942083a0d4db746adf95349108dd8ef842e32Romain Guy            right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
1211f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1212a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy    if (mesh && mesh->verticesCount > 0) {
12136620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1214a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#if RENDER_LAYERS_AS_REGIONS
12155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Mark the current layer dirty where we are going to draw the patch
12165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if ((mSnapshot->flags & Snapshot::kFlagFboTarget) &&
12175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                mSnapshot->region && mesh->hasEmptyQuads) {
12185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const size_t count = mesh->quads.size();
12195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            for (size_t i = 0; i < count; i++) {
12208ab4079ca27e36e5c584495bcd71b573598ac021Romain Guy                const Rect& bounds = mesh->quads.itemAt(i);
12216620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                if (pureTranslate) {
12226620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    const float x = (int) floorf(bounds.left + 0.5f);
12236620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    const float y = (int) floorf(bounds.top + 0.5f);
12248ab4079ca27e36e5c584495bcd71b573598ac021Romain Guy                    dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
12256620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                            *mSnapshot->transform);
12266620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                } else {
12276620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom,
12286620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                            *mSnapshot->transform);
12296620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                }
12305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
12315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
1232a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#endif
12335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
12346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        if (pureTranslate) {
12356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
12366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
12376620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12386620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
12396620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12406620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
12416620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12426620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        } else {
12436620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
12446620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12456620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
12466620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12476620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        }
1248054dc1840941665e32036f9523df51720ad069c8Romain Guy    }
1249f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1250f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12515c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1252af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1253dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
1254a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isAA = paint->isAntiAlias();
1255a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1256a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // A stroke width of 0 has a special meaningin Skia:
1257a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // it draws an unscaled 1px wide line
1258a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1259a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1260759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    int alpha;
1261759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    SkXfermode::Mode mode;
1262759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
1263759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1264a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    int verticesCount = count >> 2;
12657230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy    int generatedVerticesCount = 0;
1266a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1267a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA needs more vertices
1268a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 6;
1269a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1270a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA will be different
1271a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 2;
1272a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
1273759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1274a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex lines[verticesCount];
1275a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex* vertex = &lines[0];
1276759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
12778d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDraw();
12788d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(paint->getColor(), alpha);
12798d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilter();
12808d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShader();
12818d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawBlending(mode);
12828d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawProgram();
12838d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawModelViewIdentity();
12848d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorUniforms();
12858d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilterUniforms();
12868d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShaderIdentityUniforms();
12878d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawMesh(vertex);
1288759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1289a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1290a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1291a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
1292a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // a = start point, b = end point
1293a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 a(points[i], points[i + 1]);
1294a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 b(points[i + 2], points[i + 3]);
1295a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1296a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Bias to snap to the same pixels as Skia
1297a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            a += 0.375;
1298a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            b += 0.375;
1299a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1300a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Find the normal to the line
1301a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 n = (b - a).copyNormalized() * strokeWidth;
1302a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            float x = n.x;
1303a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.x = -n.y;
1304a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.y = x;
1305a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1306a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Four corners of the rectangle defining a thick line
1307a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p1 = a - n;
1308a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p2 = a + n;
1309a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p3 = b + n;
1310a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p4 = b - n;
1311a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13127230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
13137230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
13147230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
13157230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
13167230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13177230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13187230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Draw the line as 2 triangles, could be optimized
13197230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // by using only 4 vertices and the correct indices
13207230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Also we should probably used non textured vertices
13217230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // when line AA is disabled to save on bandwidth
13227230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13237230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
13247230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13257230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13267230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13277230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
13287230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13297230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 6;
13307230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13317230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13327230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1333a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
1334a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13357230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13367230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            // GL_LINE does not give the result we want to match Skia
13377230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
13387230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
1339a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1340a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1341a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
13427230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(points[i], points[i + 1]);
13437230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(points[i], points[i + 1]);
13447230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(points[i + 2], points[i + 3]);
13457230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(points[i + 2], points[i + 3]);
13467230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13477230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13487230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
13497230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
13507230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13517230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 2;
13527230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13537230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13547230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1355a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
13568d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
13577230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13587230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glLineWidth(1.0f);
13597230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_LINES, 0, generatedVerticesCount);
13607230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
136129d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    }
1362759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy}
1363759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
136485bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
1365e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy    // No need to check against the clip, we fill the clip region
1366af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1367e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
1368ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    Rect& clip(*mSnapshot->clipRect);
1369ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    clip.snapToPixelBoundaries();
137070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
13713d58c03de0d8877b36cdb78b0ca8b5cac7f600e2Romain Guy    drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
1372c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
13739d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1374c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
137501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (!texture) return;
137601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const AutoTexture autoCleanup(texture);
137701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
137801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float x = left + texture->left - texture->offset;
137901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float y = top + texture->top - texture->offset;
138001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
138101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
138201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
138301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1384c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1385c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        float rx, float ry, SkPaint* paint) {
138601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (mSnapshot->isIgnored()) return;
138701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
138801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
1389c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1390c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy            right - left, bottom - top, rx, ry, paint);
1391c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1392c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
139301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1394c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1395c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1396c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1397c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
139801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
1399c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(x - radius, y - radius, texture, paint);
1400c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
140101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1402c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1403c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
140401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1405c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1406c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1407c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1408c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
1409c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14108b2f5267f16c295f12faab810527cd6311997e34Romain Guyvoid OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
14118b2f5267f16c295f12faab810527cd6311997e34Romain Guy        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
14128b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (mSnapshot->isIgnored()) return;
14138b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14148b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (fabs(sweepAngle) >= 360.0f) {
14158b2f5267f16c295f12faab810527cd6311997e34Romain Guy        drawOval(left, top, right, bottom, paint);
14168b2f5267f16c295f12faab810527cd6311997e34Romain Guy        return;
14178b2f5267f16c295f12faab810527cd6311997e34Romain Guy    }
14188b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14198b2f5267f16c295f12faab810527cd6311997e34Romain Guy    glActiveTexture(gTextureUnits[0]);
14208b2f5267f16c295f12faab810527cd6311997e34Romain Guy    const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
14218b2f5267f16c295f12faab810527cd6311997e34Romain Guy            startAngle, sweepAngle, useCenter, paint);
14228b2f5267f16c295f12faab810527cd6311997e34Romain Guy    drawShape(left, top, texture, paint);
14238b2f5267f16c295f12faab810527cd6311997e34Romain Guy}
14248b2f5267f16c295f12faab810527cd6311997e34Romain Guy
1425c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1426c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        SkPaint* paint) {
1427c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1428c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1429c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1430c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1431c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
143201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
143301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
14345c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
1435c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (p->getStyle() != SkPaint::kFill_Style) {
1436c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        drawRectAsShape(left, top, right, bottom, p);
1437c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        return;
1438c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    }
1439c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14406926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
14416926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
14426926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
14436926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1444026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    SkXfermode::Mode mode;
1445746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (!mCaches.extensions.hasFramebufferFetch()) {
1446a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1447a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (!isMode) {
1448a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // Assume SRC_OVER
1449a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            mode = SkXfermode::kSrcOver_Mode;
1450a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        }
1451a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    } else {
1452a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        mode = getXfermode(p->getXfermode());
1453026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    }
1454026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1455026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    // Skia draws using the color's alpha channel if < 255
1456026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    // Otherwise, it uses the paint's alpha
1457026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    int color = p->getColor();
1458d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    if (((color >> 24) & 0xff) == 255) {
1459026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy        color |= p->getAlpha() << 24;
1460026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    }
1461026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1462026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    drawColorRect(left, top, right, bottom, color, mode);
1463c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
14649d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1465e8e62a4a032a80409114a37908b5f18ab0080848Romain Guyvoid OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1466e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        float x, float y, SkPaint* paint) {
1467b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (text == NULL || count == 0) {
1468e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        return;
1469e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1470af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1471e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
1472f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    paint->setAntiAlias(true);
1473e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
1474a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy    float length = -1.0f;
1475e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    switch (paint->getTextAlign()) {
1476e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kCenter_Align:
1477e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1478e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length / 2.0f;
1479e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1480e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kRight_Align:
1481e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1482e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length;
1483e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1484e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        default:
1485e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1486e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1487e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
14886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // TODO: Handle paint->getTextScaleX()
14893a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldX = x;
14903a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldY = y;
14916620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const bool pureTranslate = mSnapshot->transform->isPureTranslate();
14926620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate) {
14936620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
14946620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
14956620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
14966620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
1497b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1498b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
1499fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy            paint->getTextSize());
1500e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
150186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    int alpha;
150286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    SkXfermode::Mode mode;
150386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
15049d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
15051e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    if (mHasShadow) {
1506b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
1507fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
15081e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy                count, mShadowRadius);
15091e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        const AutoTexture autoCleanup(shadow);
15100a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
151186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sx = x - shadow->left + mShadowDx;
151286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sy = y - shadow->top + mShadowDy;
151386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
151486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
151586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
151686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        glActiveTexture(gTextureUnits[0]);
151786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDraw();
151886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawWithTexture(true);
151986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
152086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawBlending(true, mode);
152186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawProgram();
152286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
152386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawTexture(shadow->id);
152486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawPureColorUniforms();
152586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
15260a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
15270a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
152886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        finishDrawTexture();
15291e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    }
15301e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
1531b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1532b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy        return;
1533b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    }
1534b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy
15356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // Pick the appropriate texture filtering
15366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    bool linearFilter = mSnapshot->transform->changesBounds();
15376620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate && !linearFilter) {
15386620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
15396620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
154186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
154286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDraw();
154386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawDirtyRegionsDisabled();
154486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawWithTexture(true);
154586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
154686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilter();
154786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShader();
154886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawBlending(true, mode);
154986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawProgram();
155086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawModelView(x, y, x, y, pureTranslate, true);
155186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawTexture(fontRenderer.getTexture(linearFilter));
155286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
155386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilterUniforms();
155486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShaderUniforms(pureTranslate);
155506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
15566620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
15575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
15585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
15595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1560f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = hasLayer();
15615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
1562f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = false;
15635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
15649d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
156503750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
15666620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
1567f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            hasActiveLayer ? &bounds : NULL)) {
15685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1569f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        if (hasActiveLayer) {
15706620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            if (!pureTranslate) {
15716620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mSnapshot->transform->mapRect(bounds);
15726620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            }
1573f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            dirtyLayerUnchecked(bounds, getRegion());
15745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
15755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
15765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
1577694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1578694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1579fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1580a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy
15813a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
1582694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}
1583694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
15847fbcc0492fca03857e3c45064f4aa040af817d55Romain Guyvoid OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
1585af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1586dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
158701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
15887fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1589fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    const PathTexture* texture = mCaches.pathCache.get(path, paint);
15909cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
159122158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
15927fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
15938b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float x = texture->left - texture->offset;
15948b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float y = texture->top - texture->offset;
15958b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
159601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
15977fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
15987fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1599ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1600ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
16016c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return;
16026c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
16036c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16046c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glActiveTexture(gTextureUnits[0]);
16056c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16066c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int alpha;
16076c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    SkXfermode::Mode mode;
16086c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
16096c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1610ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->alpha = alpha;
1611ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->mode = mode;
16126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1613f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy
1614f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
1615c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy    if (!layer->region.isEmpty()) {
1616c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        if (layer->region.isRect()) {
1617c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1618c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            composeLayerRect(layer, r);
1619c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        } else if (layer->mesh) {
1620c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect& rect = layer->layer;
1621c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1622c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDraw();
1623c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawWithTexture();
1624c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColor(alpha, alpha, alpha, alpha);
1625c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilter();
1626c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1627c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawProgram();
1628c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawDirtyRegionsDisabled();
1629c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawPureColorUniforms();
1630c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilterUniforms();
1631c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawTexture(layer->texture);
1632c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1633c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
1634c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1635c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1636c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy                    GL_UNSIGNED_SHORT, layer->meshIndices);
1637c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1638c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            finishDrawTexture();
1639c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        }
1640f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
1641f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#else
1642ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1643ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    composeLayerRect(layer, r);
1644f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
16456c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
16466c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16476926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
1648d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// Shaders
1649d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1650d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
1651d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyvoid OpenGLRenderer::resetShader() {
165206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
165306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}
165406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
165506f96e2652e4855b6520ad9dd70583677605b79aRomain Guyvoid OpenGLRenderer::setupShader(SkiaShader* shader) {
165606f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = shader;
165706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1658fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
165906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    }
16607fac2e18339f765320d759e8d4c090f92431959eRomain Guy}
16617fac2e18339f765320d759e8d4c090f92431959eRomain Guy
1662d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1663db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy// Color filters
1664db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
1665db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1666db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::resetColorFilter() {
1667db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1668db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1669db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1670db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1671db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = filter;
1672db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1673db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1674db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
16751e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy// Drop shadow
16761e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
16771e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16781e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::resetShadow() {
16791e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
16801e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
16811e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16821e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
16831e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = true;
16841e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowRadius = radius;
16851e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDx = dx;
16861e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDy = dy;
16871e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowColor = color;
16881e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
16891e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16901e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
16916926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy// Drawing implementation
16926926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
16936926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
169401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guyvoid OpenGLRenderer::drawPathTexture(const PathTexture* texture,
169501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        float x, float y, SkPaint* paint) {
169601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (quickReject(x, y, x + texture->width, y + texture->height)) {
169701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        return;
169801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    }
169901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
170001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    int alpha;
170101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    SkXfermode::Mode mode;
170201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
170301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
170401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDraw();
170501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawWithTexture(true);
170601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
170701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilter();
170801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShader();
170901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawBlending(true, mode);
171001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawProgram();
171101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawModelView(x, y, x + texture->width, y + texture->height);
171201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawTexture(texture->id);
171301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawPureColorUniforms();
171401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilterUniforms();
171501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShaderUniforms();
171601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
171701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
171801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
171901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
172001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    finishDrawTexture();
172101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
172201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1723f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy// Same values used by Skia
17240a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdStrikeThru_Offset   (-6.0f / 21.0f)
17250a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Offset    (1.0f / 9.0f)
17260a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Thickness (1.0f / 18.0f)
17270a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17280a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guyvoid OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
17290a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float x, float y, SkPaint* paint) {
17300a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    // Handle underline and strike-through
17310a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    uint32_t flags = paint->getFlags();
17320a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
17330a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float underlineWidth = length;
17340a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        // If length is > 0.0f, we already measured the text for the text alignment
17350a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (length <= 0.0f) {
17360a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            underlineWidth = paint->measureText(text, bytesCount);
17370a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17380a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17390a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float offsetX = 0;
17400a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        switch (paint->getTextAlign()) {
17410a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kCenter_Align:
17420a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth * 0.5f;
17430a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17440a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kRight_Align:
17450a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth;
17460a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17470a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            default:
17480a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17490a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17500a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17510a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (underlineWidth > 0.0f) {
1752e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float textSize = paint->getTextSize();
1753f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            // TODO: Support stroke width < 1.0f when we have AA lines
1754f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
17550a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1756e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float left = x - offsetX;
17570a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            float top = 0.0f;
1758e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1759f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            int linesCount = 0;
1760f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1761f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1762f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy
1763f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const int pointsCount = 4 * linesCount;
1764e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            float points[pointsCount];
1765e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            int currentPoint = 0;
17660a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17670a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) {
17680a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdUnderline_Offset;
1769e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1770e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1771e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1772e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
17730a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
17740a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17750a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) {
17760a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdStrikeThru_Offset;
1777e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1778e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1779e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1780e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
17810a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
1782e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1783e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            SkPaint linesPaint(*paint);
1784e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            linesPaint.setStrokeWidth(strokeWidth);
1785e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1786e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            drawLines(&points[0], pointsCount, &linesPaint);
17870a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17880a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    }
17890a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy}
17900a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1791026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
17921c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy        int color, SkXfermode::Mode mode, bool ignoreTransform) {
1793d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    // If a shader is set, preserve only the alpha
179406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1795d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        color |= 0x00ffffff;
1796d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
1797d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
179870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
179970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(color);
180070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShader();
180170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
180270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(mode);
180370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
180470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawModelView(left, top, right, bottom, ignoreTransform);
180570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorUniforms();
180670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShaderUniforms(ignoreTransform);
180770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
180870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawSimpleMesh();
1809c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
1810c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1811c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy}
1812c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
181382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
18148164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        Texture* texture, SkPaint* paint) {
181582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    int alpha;
181682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    SkXfermode::Mode mode;
181782ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
181882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
18198164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
18208164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy
18216620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
18226620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
18236620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
18246620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
18256620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
18266620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
18276620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
18286620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
18296620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
18306620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
18316620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
18326620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
183385bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy}
183485bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
1835bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
1836a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1837a979474f15b454c8e2963f239a3770e200bb227cRomain Guy    drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
183803750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy            (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
1839f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1840f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1841f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guyvoid OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
1842a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
18436820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy        GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
18445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
18458694230ff25fa0a60e480d424843e56b718f0516Romain Guy
184670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
184770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawWithTexture();
184870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(alpha, alpha, alpha, alpha);
184970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
185070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(blend, mode, swapSrcDst);
185170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
185270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!dirty) {
185370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawDirtyRegionsDisabled();
1854db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
18555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!ignoreScale) {
185670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelView(left, top, right, bottom, ignoreTransform);
185703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    } else {
185870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
1859db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
186086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
186170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
186270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawTexture(texture);
186370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawMesh(vertices, texCoords, vbo);
1864db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
18656820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy    glDrawArrays(drawMode, 0, elementsCount);
186670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
186770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    finishDrawTexture();
186882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy}
186982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1870a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guyvoid OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
1871f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        ProgramDescription& description, bool swapSrcDst) {
187282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    blend = blend || mode != SkXfermode::kSrcOver_Mode;
187382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    if (blend) {
1874a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (mode < SkXfermode::kPlus_Mode) {
1875a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!mCaches.blend) {
1876a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glEnable(GL_BLEND);
1877a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
187882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1879f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1880f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
188182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1882a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1883a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glBlendFunc(sourceMode, destMode);
1884a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastSrcMode = sourceMode;
1885a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastDstMode = destMode;
1886a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1887a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1888a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // These blend modes are not supported by OpenGL directly and have
1889a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // to be implemented using shaders. Since the shader will perform
1890a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // the blending, turn blending off here
1891746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            if (mCaches.extensions.hasFramebufferFetch()) {
1892a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                description.framebufferMode = mode;
1893f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy                description.swapSrcDst = swapSrcDst;
1894a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1895a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1896a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (mCaches.blend) {
1897a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glDisable(GL_BLEND);
1898a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1899a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            blend = false;
190082ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        }
1901fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    } else if (mCaches.blend) {
190282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        glDisable(GL_BLEND);
190382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    }
1904fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    mCaches.blend = blend;
1905bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
1906bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
1907889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guybool OpenGLRenderer::useProgram(Program* program) {
1908d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    if (!program->isInUse()) {
1909fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
1910d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        program->use();
1911fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mCaches.currentProgram = program;
19126926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return false;
1913260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    }
19146926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    return true;
1915260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy}
1916260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
1917026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
1918ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    TextureVertex* v = &mMeshVertices[0];
191982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v1);
192082ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v1);
192182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v2);
192282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v2);
19238ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
19248ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19255c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
19268ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    if (paint) {
1927746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        if (!mCaches.extensions.hasFramebufferFetch()) {
1928a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1929a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!isMode) {
1930a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                // Assume SRC_OVER
1931a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                *mode = SkXfermode::kSrcOver_Mode;
1932a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1933a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1934a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            *mode = getXfermode(paint->getXfermode());
19358ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
19368ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19378ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Skia draws using the color's alpha channel if < 255
19388ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Otherwise, it uses the paint's alpha
19398ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        int color = paint->getColor();
19408ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = (color >> 24) & 0xFF;
19418ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        if (*alpha == 255) {
19428ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy            *alpha = paint->getAlpha();
19438ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
19448ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    } else {
19458ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *mode = SkXfermode::kSrcOver_Mode;
19468ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = 255;
19478ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    }
1948026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy}
1949026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1950a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain GuySkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1951a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    if (mode == NULL) {
1952a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        return SkXfermode::kSrcOver_Mode;
1953a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    }
1954a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    return mode->fMode;
1955a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy}
1956a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1957746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guyvoid OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
19588164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    bool bound = false;
19598164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapS != texture->wrapS) {
19608164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glBindTexture(GL_TEXTURE_2D, texture->id);
19618164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        bound = true;
19628164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
19638164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapS = wrapS;
19648164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
19658164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapT != texture->wrapT) {
19663e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        if (!bound) {
19673e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy            glBindTexture(GL_TEXTURE_2D, texture->id);
19683e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        }
19698164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
19708164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapT = wrapT;
19718164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
1972a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
1973a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
19749d5316e3f56d138504565ff311145ac01621dff4Romain Guy}; // namespace uirenderer
1975e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}; // namespace android
1976