OpenGLRenderer.cpp revision c78b5d50f961ac8f696f8282979ae283cacd3574
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::resume() {
195eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
196da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
197da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    glEnable(GL_SCISSOR_TEST);
198746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
199da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
200f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    glDisable(GL_DITHER);
201f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
20242f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
20303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
20450c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy
20550c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.blend = true;
20650c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glEnable(GL_BLEND);
20750c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
20850c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendEquation(GL_FUNC_ADD);
209da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
210da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
211daf98e941e140e8739458126640183b9f296a2abChet Haasebool OpenGLRenderer::callDrawGLFunction(Functor *functor) {
212daf98e941e140e8739458126640183b9f296a2abChet Haase    interrupt();
213f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    if (mDirtyClip) {
214f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy        setScissorFromClip();
215f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    }
216daf98e941e140e8739458126640183b9f296a2abChet Haase    status_t result = (*functor)();
217daf98e941e140e8739458126640183b9f296a2abChet Haase    resume();
218daf98e941e140e8739458126640183b9f296a2abChet Haase    return (result == 0) ? false : true;
219daf98e941e140e8739458126640183b9f296a2abChet Haase}
220daf98e941e140e8739458126640183b9f296a2abChet Haase
221f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
222f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// State management
223f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
224f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
225bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::getSaveCount() const {
2267ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    return mSaveCount;
227bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
228bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
229bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::save(int flags) {
2308aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return saveSnapshot(flags);
231bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
232bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
233bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restore() {
2342542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (mSaveCount > 1) {
2352542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2367ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
237bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
238bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
239bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restoreToCount(int saveCount) {
2408fb954263dd2f918ad339045cc6d82e346515599Romain Guy    if (saveCount < 1) saveCount = 1;
241bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2428fb954263dd2f918ad339045cc6d82e346515599Romain Guy    while (mSaveCount > saveCount) {
2432542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2447ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
245bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
246bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2478aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guyint OpenGLRenderer::saveSnapshot(int flags) {
2488aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mSnapshot, flags);
2498fb954263dd2f918ad339045cc6d82e346515599Romain Guy    return mSaveCount++;
250bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
251bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
252bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guybool OpenGLRenderer::restoreSnapshot() {
2537ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
254bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
255eb99356a0548684a501766e6a524529ab93304c8Romain Guy    bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
256bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
257bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    sp<Snapshot> current = mSnapshot;
2587ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    sp<Snapshot> previous = mSnapshot->previous;
259bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
260eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (restoreOrtho) {
261eb99356a0548684a501766e6a524529ab93304c8Romain Guy        Rect& r = previous->viewport;
262eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glViewport(r.left, r.top, r.right, r.bottom);
263eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mOrthoMatrix.load(current->orthoMatrix);
264eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
265eb99356a0548684a501766e6a524529ab93304c8Romain Guy
2668b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSaveCount--;
2678b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSnapshot = previous;
2688b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
2692542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (restoreClip) {
270746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
2718fb954263dd2f918ad339045cc6d82e346515599Romain Guy    }
2722542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy
2735ec9924d24495822b589f1a17996655d66273b30Romain Guy    if (restoreLayer) {
2745ec9924d24495822b589f1a17996655d66273b30Romain Guy        composeLayer(current, previous);
2755ec9924d24495822b589f1a17996655d66273b30Romain Guy    }
2765ec9924d24495822b589f1a17996655d66273b30Romain Guy
2772542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    return restoreClip;
278d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
2795cbbce535744b89df5ecea95de21ee3733298260Romain Guy
280f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
281bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// Layers
282bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
283bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
284bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
2855c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        SkPaint* p, int flags) {
286eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const GLuint previousFbo = mSnapshot->fbo;
287eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const int count = saveSnapshot(flags);
288d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
289af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (!mSnapshot->isIgnored()) {
290e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        int alpha = 255;
291e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        SkXfermode::Mode mode;
292e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
293e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        if (p) {
294e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            alpha = p->getAlpha();
295e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            if (!mCaches.extensions.hasFramebufferFetch()) {
296e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
297e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                if (!isMode) {
298e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    // Assume SRC_OVER
299e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    mode = SkXfermode::kSrcOver_Mode;
300e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                }
301e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            } else {
302e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                mode = getXfermode(p->getXfermode());
303a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
304a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
305e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            mode = SkXfermode::kSrcOver_Mode;
306d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy        }
307d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
308dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
309dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
310d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
311d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return count;
312bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
313bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
314bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
315bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        int alpha, int flags) {
3165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (alpha >= 255 - ALPHA_THRESHOLD) {
3178aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, NULL, flags);
3188b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    } else {
3198aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        SkPaint paint;
3208aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        paint.setAlpha(alpha);
3218aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, &paint, flags);
3228b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    }
323d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
324bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
3251c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
3261c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Layers are viewed by Skia are slightly different than layers in image editing
3271c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * programs (for instance.) When a layer is created, previously created layers
3281c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * and the frame buffer still receive every drawing command. For instance, if a
3291c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * layer is created and a shape intersecting the bounds of the layers and the
3301c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * framebuffer is draw, the shape will be drawn on both (unless the layer was
3311c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
3321c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3331c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * A way to implement layers is to create an FBO for each layer, backed by an RGBA
3341c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * texture. Unfortunately, this is inefficient as it requires every primitive to
3351c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be drawn n + 1 times, where n is the number of active layers. In practice this
3361c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * means, for every primitive:
3371c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Switch active frame buffer
3381c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Change viewport, clip and projection matrix
3391c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Issue the drawing
3401c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3411c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Switching rendering target n + 1 times per drawn primitive is extremely costly.
3426b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * To avoid this, layers are implemented in a different way here, at least in the
3436b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * general case. FBOs are used, as an optimization, when the "clip to layer" flag
3446b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * is set. When this flag is set we can redirect all drawing operations into a
3456b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * single FBO.
3461c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3471c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * This implementation relies on the frame buffer being at least RGBA 8888. When
3481c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * a layer is created, only a texture is created, not an FBO. The content of the
3491c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer contained within the layer's bounds is copied into this texture
35087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
3511c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * buffer and drawing continues as normal. This technique therefore treats the
3521c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer as a scratch buffer for the layers.
3531c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3541c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * To compose the layers back onto the frame buffer, each layer texture
3551c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * (containing the original frame buffer data) is drawn as a simple quad over
3561c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * the frame buffer. The trick is that the quad is set as the composition
3571c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * destination in the blending equation, and the frame buffer becomes the source
3581c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * of the composition.
3591c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3601c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Drawing layers with an alpha value requires an extra step before composition.
3611c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * An empty quad is drawn over the layer's region in the frame buffer. This quad
3621c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
3631c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * quad is used to multiply the colors in the frame buffer. This is achieved by
3641c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * changing the GL blend functions for the GL_FUNC_ADD blend equation to
3651c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * GL_ZERO, GL_SRC_ALPHA.
3661c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3671c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Because glCopyTexImage2D() can be slow, an alternative implementation might
3681c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be use to draw a single clipped layer. The implementation described above
3691c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is correct in every case.
37087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *
37187a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * (1) The frame buffer is actually not cleared right away. To allow the GPU
37287a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     to potentially optimize series of calls to glCopyTexImage2D, the frame
37387a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     buffer is left untouched until the first drawing operation. Only when
37487a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     something actually gets drawn are the layers regions cleared.
3751c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
376d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guybool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
377eb99356a0548684a501766e6a524529ab93304c8Romain Guy        float right, float bottom, int alpha, SkXfermode::Mode mode,
378eb99356a0548684a501766e6a524529ab93304c8Romain Guy        int flags, GLuint previousFbo) {
379eb99356a0548684a501766e6a524529ab93304c8Romain Guy    LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
380fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
381dda570201ac851dd85af3861f7e575721d3345daRomain Guy
382eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
383eb99356a0548684a501766e6a524529ab93304c8Romain Guy
384f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    // Window coordinates of the layer
3858aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    Rect bounds(left, top, right, bottom);
386ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    if (fboLayer) {
387ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        // Clear the previous layer regions before we change the viewport
388ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        clearLayerRegions();
389ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    } else {
390eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mSnapshot->transform->mapRect(bounds);
391ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
392eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Layers only make sense if they are in the framebuffer's bounds
39358ae6db4ff8a9d0910e1183ee8be9a038a2712a6Romain Guy        bounds.intersect(*snapshot->clipRect);
394ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
395ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        // We cannot work with sub-pixels in this case
396ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        bounds.snapToPixelBoundaries();
397ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy
398ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // When the layer is not an FBO, we may use glCopyTexImage so we
399ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // need to make sure the layer does not extend outside the bounds
400ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        // of the framebuffer
401ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy        bounds.intersect(snapshot->previous->viewport);
402eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
403bf434114cbf55b216fdc76fc8d65a75e84c9dab5Romain Guy
404746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
405746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            bounds.getHeight() > mCaches.maxTextureSize) {
40632963c393a804db2cd86c24443c2f1c6eee6064bRomain Guy        snapshot->empty = fboLayer;
407dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    } else {
408e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
409dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
410dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
411dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    // Bail out if we won't draw in this snapshot
412af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (snapshot->invisible || snapshot->empty) {
413b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        return false;
414b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
415dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glActiveTexture(gTextureUnits[0]);
4178550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
418f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy    if (!layer) {
419f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy        return false;
420bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    }
421bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
422dda570201ac851dd85af3861f7e575721d3345daRomain Guy    layer->mode = mode;
423f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    layer->alpha = alpha;
4248aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    layer->layer.set(bounds);
4258550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
4268550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy            bounds.getWidth() / float(layer->width), 0.0f);
427171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy    layer->colorFilter = mColorFilter;
428dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4298fb954263dd2f918ad339045cc6d82e346515599Romain Guy    // Save the layer in the snapshot
4308fb954263dd2f918ad339045cc6d82e346515599Romain Guy    snapshot->flags |= Snapshot::kFlagIsLayer;
431dda570201ac851dd85af3861f7e575721d3345daRomain Guy    snapshot->layer = layer;
4321d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
433eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
4345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return createFboLayer(layer, bounds, snapshot, previousFbo);
435eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
436eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Copy the framebuffer into the layer
437eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindTexture(GL_TEXTURE_2D, layer->texture);
438514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!bounds.isEmpty()) {
439514fb18827186591d66973c2362c859b64b63556Romain Guy            if (layer->empty) {
440514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
441514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, layer->width, layer->height, 0);
442514fb18827186591d66973c2362c859b64b63556Romain Guy                layer->empty = false;
443514fb18827186591d66973c2362c859b64b63556Romain Guy            } else {
444514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
445514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
446514fb18827186591d66973c2362c859b64b63556Romain Guy            }
447514fb18827186591d66973c2362c859b64b63556Romain Guy            // Enqueue the buffer coordinates to clear the corresponding region later
448514fb18827186591d66973c2362c859b64b63556Romain Guy            mLayers.push(new Rect(bounds));
449ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        }
450eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
451f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
452d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return true;
453bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
454bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
4555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guybool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
4565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLuint previousFbo) {
4575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    layer->fbo = mCaches.fboCache.get();
4585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
4605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->region = &snapshot->layer->region;
4615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagFboTarget;
4625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
4635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clip(bounds);
4655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->transform->mapRect(clip);
4665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(*snapshot->clipRect);
4675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
4685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(snapshot->previous->viewport);
4695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mat4 inverse;
4715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.loadInverse(*mSnapshot->transform);
4725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.mapRect(clip);
4745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
4755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(bounds);
4765ec9924d24495822b589f1a17996655d66273b30Romain Guy    clip.translate(-bounds.left, -bounds.top);
4775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagIsFboLayer;
4795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->fbo = layer->fbo;
4805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
4815b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
4825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
4835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->height = bounds.getHeight();
4845b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagDirtyOrtho;
4855b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->orthoMatrix.load(mOrthoMatrix);
4865b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Bind texture to FBO
4885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
4895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindTexture(GL_TEXTURE_2D, layer->texture);
4905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Initialize the texture if needed
4925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->empty) {
4935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->empty = false;
4945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
4955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
4965b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
4975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
4985b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
4995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->texture, 0);
5005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
5025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (status != GL_FRAMEBUFFER_COMPLETE) {
5045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
5055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
5075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glDeleteTextures(1, &layer->texture);
5085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        mCaches.fboCache.put(layer->fbo);
5095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        delete layer;
5115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return false;
5135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
5175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
5185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
5195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
5205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClear(GL_COLOR_BUFFER_BIT);
5215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    dirtyClip();
5235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Change the ortho projection
5255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
5265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
5275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    return true;
5295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
5305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5311c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
5321c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Read the documentation of createLayer() before doing anything in this method.
5331c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
5341d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guyvoid OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
5351d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    if (!current->layer) {
5361d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LOGE("Attempting to compose a layer that does not exist");
5371d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        return;
5381d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
5391d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
5405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
541eb99356a0548684a501766e6a524529ab93304c8Romain Guy
542eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
543eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Unbind current FBO and restore previous one
544eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
545eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
546eb99356a0548684a501766e6a524529ab93304c8Romain Guy
5471d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Layer* layer = current->layer;
5481d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    const Rect& rect = layer->layer;
5491d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
550eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (!fboLayer && layer->alpha < 255) {
551f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
5521c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy                layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
5535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Required below, composeLayerRect() will divide by 255
5545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->alpha = 255;
555f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    }
556f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
55703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
5588b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
559746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
5605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // When the layer is stored in an FBO, we can save a bit of fillrate by
5625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // drawing only the dirty region
563eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
5645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
565171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
566171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            setupColorFilter(layer->colorFilter);
567171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
5685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRegion(layer, rect);
569171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
570171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            resetColorFilter();
571171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
572eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
573514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!rect.isEmpty()) {
574514fb18827186591d66973c2362c859b64b63556Romain Guy            dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
575514fb18827186591d66973c2362c859b64b63556Romain Guy            composeLayerRect(layer, rect, true);
576514fb18827186591d66973c2362c859b64b63556Romain Guy        }
577eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
5781d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
579eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
580eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Detach the texture from the FBO
581eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
582eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
583eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
584eb99356a0548684a501766e6a524529ab93304c8Romain Guy
585eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
586eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mCaches.fboCache.put(current->fbo);
587eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
588eb99356a0548684a501766e6a524529ab93304c8Romain Guy
589746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
590746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
591eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // Failing to add the layer to the cache should happen only if the layer is too large
5928550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    if (!mCaches.layerCache.put(layer)) {
5931d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LAYER_LOGD("Deleting layer");
5941d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        glDeleteTextures(1, &layer->texture);
5951d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        delete layer;
5961d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
5971d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy}
5981d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
5995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
6005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const Rect& texCoords = layer->texCoords;
6015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
6025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
6045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
6055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
6065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
6085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
6095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
6115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
6125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->region.isRect()) {
6135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRect(layer, rect);
6145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
6155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return;
6165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
6175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!layer->region.isEmpty()) {
6195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        size_t count;
6205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const android::Rect* rects = layer->region.getArray(&count);
6215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float alpha = layer->alpha / 255.0f;
6235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texX = 1.0f / float(layer->width);
6245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texY = 1.0f / float(layer->height);
625f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float height = rect.getHeight();
6265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        TextureVertex* mesh = mCaches.getRegionMesh();
6285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLsizei numQuads = 0;
6295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6307230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDraw();
6317230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawWithTexture();
6327230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawColor(alpha, alpha, alpha, alpha);
633ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilter();
6347230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
6357230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawProgram();
6367230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawDirtyRegionsDisabled();
6377230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawPureColorUniforms();
638ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilterUniforms();
6397230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawTexture(layer->texture);
640c038ea358aadca082198f3effc550d33135bf426Romain Guy        setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
6417230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
6425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6435b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        for (size_t i = 0; i < count; i++) {
6445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const android::Rect* r = &rects[i];
6455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u1 = r->left * texX;
647f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v1 = (height - r->top) * texY;
6485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u2 = r->right * texX;
649f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v2 = (height - r->bottom) * texY;
6505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // TODO: Reject quads outside of the clip
6525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->top, u1, v1);
6535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->top, u2, v1);
6545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
6555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
6565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            numQuads++;
6585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            if (numQuads >= REGION_MESH_QUAD_COUNT) {
6605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
6615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                numQuads = 0;
6625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                mesh = mCaches.getRegionMesh();
6635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
6645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
6655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (numQuads > 0) {
6675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
6685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
6695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6717230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        finishDrawTexture();
6725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
6743a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawRegionRects(layer->region);
6755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
6765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
6785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
6795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
6805b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    composeLayerRect(layer, rect);
6815b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
6825b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
6835b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6843a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guyvoid OpenGLRenderer::drawRegionRects(const Region& region) {
6853a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
6863a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    size_t count;
6873a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    const android::Rect* rects = region.getArray(&count);
6883a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
6893a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    uint32_t colors[] = {
6903a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7fff0000, 0x7f00ff00,
6913a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7f0000ff, 0x7fff00ff,
6923a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    };
6933a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
6943a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int offset = 0;
6953a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int32_t top = rects[0].top;
6963a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
6973a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    for (size_t i = 0; i < count; i++) {
6983a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        if (top != rects[i].top) {
6993a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            offset ^= 0x2;
7003a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            top = rects[i].top;
7013a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        }
7023a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7033a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
7043a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
7053a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy                SkXfermode::kSrcOver_Mode);
7063a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    }
7073a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
7083a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy}
7093a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom, const mat4 transform) {
7125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
713f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
7155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        transform.mapRect(bounds);
716f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom) {
7235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
724f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
726f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7271bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    }
7281bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#endif
7291bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy}
7301bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy
7311bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guyvoid OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
7321bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#if RENDER_LAYERS_AS_REGIONS
7331bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    if (bounds.intersect(*mSnapshot->clipRect)) {
7341bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        bounds.snapToPixelBoundaries();
7351bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
7361bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        if (!dirty.isEmpty()) {
7371bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy            region->orSelf(dirty);
7385b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7415b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7438694230ff25fa0a60e480d424843e56b718f0516Romain Guyvoid OpenGLRenderer::clearLayerRegions() {
744af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mLayers.size() == 0 || mSnapshot->isIgnored()) return;
7458694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clipRect(*mSnapshot->clipRect);
7475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clipRect.snapToPixelBoundaries();
7485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7498694230ff25fa0a60e480d424843e56b718f0516Romain Guy    for (uint32_t i = 0; i < mLayers.size(); i++) {
7508694230ff25fa0a60e480d424843e56b718f0516Romain Guy        Rect* bounds = mLayers.itemAt(i);
7515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (clipRect.intersects(*bounds)) {
7525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // Clear the framebuffer where the layer will draw
7535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glScissor(bounds->left, mSnapshot->height - bounds->bottom,
7545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                    bounds->getWidth(), bounds->getHeight());
7555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
7565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glClear(GL_COLOR_BUFFER_BIT);
7575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // Restore the clip
7595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            dirtyClip();
7605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7618694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7628694230ff25fa0a60e480d424843e56b718f0516Romain Guy        delete bounds;
7638694230ff25fa0a60e480d424843e56b718f0516Romain Guy    }
7648694230ff25fa0a60e480d424843e56b718f0516Romain Guy
7655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mLayers.clear();
7668694230ff25fa0a60e480d424843e56b718f0516Romain Guy}
7678694230ff25fa0a60e480d424843e56b718f0516Romain Guy
768bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
769f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Transforms
770f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
771f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
772f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::translate(float dx, float dy) {
7738aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->translate(dx, dy, 0.0f);
774f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
775f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
776f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::rotate(float degrees) {
7778aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
778f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
779f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
780f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::scale(float sx, float sy) {
7818aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->scale(sx, sy, 1.0f);
782f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
783f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
784807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guyvoid OpenGLRenderer::skew(float sx, float sy) {
785807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy    mSnapshot->transform->skew(sx, sy);
786807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy}
787807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy
788f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::setMatrix(SkMatrix* matrix) {
7898aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->load(*matrix);
790f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
791f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
79241030da16856c8869e1e51d4a0405432fa96614eRomain Guyconst float* OpenGLRenderer::getMatrix() const {
79399bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    if (mSnapshot->fbo != 0) {
79499bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy        return &mSnapshot->transform->data[0];
79599bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    }
79699bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    return &mIdentity.data[0];
79741030da16856c8869e1e51d4a0405432fa96614eRomain Guy}
79841030da16856c8869e1e51d4a0405432fa96614eRomain Guy
799f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::getMatrix(SkMatrix* matrix) {
8008aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->copyTo(*matrix);
801f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
802f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
803f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
804e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    SkMatrix transform;
805e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->copyTo(transform);
806e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    transform.preConcat(*matrix);
807e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->load(transform);
808f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
809f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
810f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
811f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Clipping
812f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
813f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
814bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::setScissorFromClip() {
815e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    Rect clip(*mSnapshot->clipRect);
816e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    clip.snapToPixelBoundaries();
817eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
818746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
8199d5316e3f56d138504565ff311145ac01621dff4Romain Guy}
8209d5316e3f56d138504565ff311145ac01621dff4Romain Guy
8219d5316e3f56d138504565ff311145ac01621dff4Romain Guyconst Rect& OpenGLRenderer::getClipBounds() {
822079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return mSnapshot->getLocalClip();
823bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
824bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
825c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guybool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
826af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) {
827dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        return true;
828dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
829dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
8301d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Rect r(left, top, right, bottom);
8318aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->mapRect(r);
832d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    r.snapToPixelBoundaries();
833d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
834d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    Rect clipRect(*mSnapshot->clipRect);
835d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    clipRect.snapToPixelBoundaries();
836d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
837d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    return !clipRect.intersects(r);
838c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
839c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
840079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guybool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
841079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    bool clipped = mSnapshot->clip(left, top, right, bottom, op);
8427ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    if (clipped) {
843746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
8447ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
8458aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return !mSnapshot->clipRect->isEmpty();
846e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
847e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
848f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
84970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy// Drawing commands
85070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
85170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
85270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDraw() {
85370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    clearLayerRegions();
85470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mDirtyClip) {
85570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setScissorFromClip();
85670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
85770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.reset();
85870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = false;
85970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = false;
86070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = mColorR = mColorG = mColorB = 0.0f;
86170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTextureUnit = 0;
86270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = true;
8638d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mTexCoordsSlot = -1;
86470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
86570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
86670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
86770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasTexture = true;
86870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasAlpha8Texture = isAlpha8;
86970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
87070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
87170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(int color) {
8728d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(color, (color >> 24) & 0xFF);
8738d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
8748d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
8758d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawColor(int color, int alpha) {
8768d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mColorA = alpha / 255.0f;
87770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    const float a = mColorA / 255.0f;
878fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorR = a * ((color >> 16) & 0xFF);
879fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorG = a * ((color >>  8) & 0xFF);
880fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorB = a * ((color      ) & 0xFF);
88170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
88270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
88370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
88470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
88586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
88686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = alpha / 255.0f;
88786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    const float a = mColorA / 255.0f;
88886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = a * ((color >> 16) & 0xFF);
88986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = a * ((color >>  8) & 0xFF);
89086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = a * ((color      ) & 0xFF);
89186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
89286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
89386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
89486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
89570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
89670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = a;
89770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorR = r;
89870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorG = g;
89970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorB = b;
90070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
90170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(r, g, b, a);
90270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
90370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
90486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
90586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = a;
90686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = r;
90786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = g;
90886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = b;
90986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
91086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
91186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
91286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
91370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShader() {
91470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
91570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->describe(mDescription, mCaches.extensions);
91670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
91770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
91870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
91970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilter() {
92070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
92170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->describe(mDescription, mCaches.extensions);
92270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
92370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
92470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
92570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
92670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
92770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
92870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
92970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
93170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
93270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
93370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawProgram() {
93670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    useProgram(mCaches.programCache.get(mDescription));
93770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
93870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
93970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
94070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = false;
94170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
94270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
94470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform) {
94570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mModelView.loadTranslate(left, top, 0.0f);
94670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreTransform) {
94770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
94870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
94970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
95070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
95170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
95270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
95370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
9558d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawModelViewIdentity() {
9568d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
9578d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
9588d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
95970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
96070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform, bool ignoreModelView) {
96170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreModelView) {
96270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadTranslate(left, top, 0.0f);
96370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.scale(right - left, bottom - top, 1.0f);
96470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
96570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadIdentity();
96670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
96786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    bool dirty = right - left > 0.0f && bottom - top > 0.0f;
96886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    if (!ignoreTransform) {
96986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
97086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) {
97186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy            dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
97286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        }
97386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    } else {
97486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
97586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
97686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    }
97770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
97870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
97970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorUniforms() {
9805536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mColorSet || (mShader && mSetShaderColor)) {
98170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
98270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
98370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
98470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
98586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawPureColorUniforms() {
9865536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mSetShaderColor) {
98786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
9885536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    }
9895536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy}
9905536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy
99170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
99270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
99370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (ignoreTransform) {
99470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mModelView.loadInverse(*mSnapshot->transform);
99570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        }
99670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
99770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
99870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
99970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
10008d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawShaderIdentityUniforms() {
10018d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mShader) {
10028d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
10038d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
10048d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
10058d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
100670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilterUniforms() {
100770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
100870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->setupProgram(mCaches.currentProgram);
100970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
101070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
101170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
101270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawSimpleMesh() {
101370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mCaches.bindMeshBuffer();
101470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
101570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, 0);
101670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
101770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
101870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawTexture(GLuint texture) {
101970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    bindTexture(texture);
102070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
102170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
102270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
102370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glEnableVertexAttribArray(mTexCoordsSlot);
102470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
102570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
102670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
102770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!vertices) {
102870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
102970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
103070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.unbindMeshBuffer();
103170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
103270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
103370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, vertices);
10348d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mTexCoordsSlot > 0) {
10358d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
10368d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
103770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::finishDrawTexture() {
104070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glDisableVertexAttribArray(mTexCoordsSlot);
104170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
104270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
104370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
1044f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Drawing
1045f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
1046f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
1047daf98e941e140e8739458126640183b9f296a2abChet Haasebool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t level) {
10480fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // All the usual checks and setup operations (quickReject, setupDraw, etc.)
10490fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // will be performed by the display list itself
10500fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    if (displayList) {
1051daf98e941e140e8739458126640183b9f296a2abChet Haase        return displayList->replay(*this, level);
10520fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    }
1053daf98e941e140e8739458126640183b9f296a2abChet Haase    return false;
10540fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy}
10550fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy
10565c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
10576926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float right = left + bitmap->width();
10586926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float bottom = top + bitmap->height();
10596926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
10606926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
10616926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10626926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10636926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
106486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10658164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10669cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
106722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
106822158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10696926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    drawTextureRect(left, top, right, bottom, texture, paint);
10708ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
10718ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
10725c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
1073f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1074f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    const mat4 transform(*matrix);
1075f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    transform.mapRect(r);
1076f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
10776926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(r.left, r.top, r.right, r.bottom)) {
10786926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10796926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10806926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
108186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10828164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10839cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
108422158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
108522158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10865b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // This could be done in a cheaper way, all we need is pass the matrix
10875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // to the vertex shader. The save/restore is a bit overkill.
10885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    save(SkCanvas::kMatrix_SaveFlag);
10895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    concatMatrix(matrix);
10905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
10915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    restore();
1092f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy}
1093f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
10945a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guyvoid OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
10955a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        float* vertices, int* colors, SkPaint* paint) {
10965a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    // TODO: Do a quickReject
10975a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!vertices || mSnapshot->isIgnored()) {
10985a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        return;
10995a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11005a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11015a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    glActiveTexture(gTextureUnits[0]);
11025a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11035a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!texture) return;
11045a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const AutoTexture autoCleanup(texture);
11055a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11065a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11075a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    int alpha;
11085a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    SkXfermode::Mode mode;
11095a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
11105a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11115a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const uint32_t count = meshWidth * meshHeight * 6;
11125a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1113a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    // TODO: Support the colors array
1114a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    TextureVertex mesh[count];
11155a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    TextureVertex* vertex = mesh;
11165a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    for (int32_t y = 0; y < meshHeight; y++) {
11175a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        for (int32_t x = 0; x < meshWidth; x++) {
11185a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            uint32_t i = (y * (meshWidth + 1) + x) * 2;
11195a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11205a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u1 = float(x) / meshWidth;
11215a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u2 = float(x + 1) / meshWidth;
11225a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v1 = float(y) / meshHeight;
11235a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v2 = float(y + 1) / meshHeight;
11245a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11255a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ax = i + (meshWidth + 1) * 2;
11265a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ay = ax + 1;
11275a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int bx = i;
11285a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int by = bx + 1;
11295a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cx = i + 2;
11305a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cy = cx + 1;
11315a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dx = i + (meshWidth + 1) * 2 + 2;
11325a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dy = dx + 1;
11335a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11345a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11355a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
11365a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11375a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11385a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11395a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11405a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
11415a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        }
11425a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11435a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11445a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
11455a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
11465a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            GL_TRIANGLES, count);
11475a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy}
11485a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11498ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guyvoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
11508ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float srcLeft, float srcTop, float srcRight, float srcBottom,
11518ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float dstLeft, float dstTop, float dstRight, float dstBottom,
11525c13d89c1332fcc499379b9064b891187b75ca32Chet Haase         SkPaint* paint) {
11536926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
11546926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
11556926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
11566926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1157746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
11588164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11599cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
116022158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
11618164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11628ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
11638ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float width = texture->width;
11648ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float height = texture->height;
1165c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
11668ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u1 = srcLeft / width;
11678ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v1 = srcTop / height;
11688ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u2 = srcRight / width;
11698ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v2 = srcBottom / height;
1170c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
117103750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
11728ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(u1, v1, u2, v2);
11738ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
117403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    int alpha;
117503750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    SkXfermode::Mode mode;
117603750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
117703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy
11786620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
11796620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
11806620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
11816620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
11826620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
11836620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->id, alpha / 255.0f, mode, texture->blend,
11846620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
11856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount, false, true);
11866620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
11876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
11886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
11896620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
11906620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
11918ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
11928ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1193ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
1194ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
11954aa90573bbf86db0d33a3a790c5dbd0d93b95cfeRomain Guyvoid OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
11964bb942083a0d4db746adf95349108dd8ef842e32Romain Guy        const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
11975c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        float left, float top, float right, float bottom, SkPaint* paint) {
11986926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
11996926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
12006926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
12016926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1202746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12038164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12049cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
120522158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12068164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1207f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1208f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    int alpha;
1209f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    SkXfermode::Mode mode;
1210f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
1211f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12122728f961614a385df1f056fc24803a9f65c90fabRomain Guy    const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
12134bb942083a0d4db746adf95349108dd8ef842e32Romain Guy            right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
1214f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1215a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy    if (mesh && mesh->verticesCount > 0) {
12166620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1217a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#if RENDER_LAYERS_AS_REGIONS
12185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Mark the current layer dirty where we are going to draw the patch
12198168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy        if (hasLayer() && mesh->hasEmptyQuads) {
1220c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetX = left + mSnapshot->transform->getTranslateX();
1221c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetY = top + mSnapshot->transform->getTranslateY();
12225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const size_t count = mesh->quads.size();
12235b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            for (size_t i = 0; i < count; i++) {
12248ab4079ca27e36e5c584495bcd71b573598ac021Romain Guy                const Rect& bounds = mesh->quads.itemAt(i);
12256620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                if (pureTranslate) {
1226c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1227c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1228c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
12296620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                } else {
1230c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(left + bounds.left, top + bounds.top,
1231c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                            left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
12326620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                }
12335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
12345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
1235a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#endif
12365b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
12376620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        if (pureTranslate) {
12386620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
12396620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
12406620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12416620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
12426620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12436620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
12446620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12456620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        } else {
12466620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
12476620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12486620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
12496620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
12506620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        }
1251054dc1840941665e32036f9523df51720ad069c8Romain Guy    }
1252f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1253f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12545c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1255af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1256dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
1257a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isAA = paint->isAntiAlias();
1258a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1259a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // A stroke width of 0 has a special meaningin Skia:
1260a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // it draws an unscaled 1px wide line
1261a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1262a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1263759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    int alpha;
1264759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    SkXfermode::Mode mode;
1265759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
1266759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1267a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    int verticesCount = count >> 2;
12687230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy    int generatedVerticesCount = 0;
1269a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1270a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA needs more vertices
1271a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 6;
1272a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1273a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA will be different
1274a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 2;
1275a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
1276759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1277a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex lines[verticesCount];
1278a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex* vertex = &lines[0];
1279759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
12808d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDraw();
12818d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(paint->getColor(), alpha);
12828d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilter();
12838d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShader();
12848d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawBlending(mode);
12858d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawProgram();
12868d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawModelViewIdentity();
12878d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorUniforms();
12888d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilterUniforms();
12898d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShaderIdentityUniforms();
12908d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawMesh(vertex);
1291759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1292a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1293a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1294a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
1295a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // a = start point, b = end point
1296a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 a(points[i], points[i + 1]);
1297a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 b(points[i + 2], points[i + 3]);
1298a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1299a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Bias to snap to the same pixels as Skia
1300a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            a += 0.375;
1301a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            b += 0.375;
1302a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1303a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Find the normal to the line
1304a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 n = (b - a).copyNormalized() * strokeWidth;
1305a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            float x = n.x;
1306a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.x = -n.y;
1307a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.y = x;
1308a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1309a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Four corners of the rectangle defining a thick line
1310a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p1 = a - n;
1311a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p2 = a + n;
1312a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p3 = b + n;
1313a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p4 = b - n;
1314a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13157230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
13167230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
13177230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
13187230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
13197230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13207230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13217230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Draw the line as 2 triangles, could be optimized
13227230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // by using only 4 vertices and the correct indices
13237230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Also we should probably used non textured vertices
13247230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // when line AA is disabled to save on bandwidth
13257230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13267230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
13277230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13287230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13297230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13307230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
13317230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13327230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 6;
13337230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13347230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13357230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1336a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
1337a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13387230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13397230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            // GL_LINE does not give the result we want to match Skia
13407230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
13417230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
1342a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1343a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1344a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
13457230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(points[i], points[i + 1]);
13467230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(points[i], points[i + 1]);
13477230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(points[i + 2], points[i + 3]);
13487230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(points[i + 2], points[i + 3]);
13497230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13507230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13517230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
13527230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
13537230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13547230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 2;
13557230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13567230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13577230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1358a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
13598d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
13607230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13617230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glLineWidth(1.0f);
13627230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_LINES, 0, generatedVerticesCount);
13637230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
136429d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    }
1365759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy}
1366759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
136785bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
1368e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy    // No need to check against the clip, we fill the clip region
1369af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1370e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
1371ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    Rect& clip(*mSnapshot->clipRect);
1372ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    clip.snapToPixelBoundaries();
137370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
13743d58c03de0d8877b36cdb78b0ca8b5cac7f600e2Romain Guy    drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
1375c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
13769d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1377c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
137801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (!texture) return;
137901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const AutoTexture autoCleanup(texture);
138001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
138101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float x = left + texture->left - texture->offset;
138201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float y = top + texture->top - texture->offset;
138301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
138401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
138501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
138601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1387c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1388c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        float rx, float ry, SkPaint* paint) {
138901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (mSnapshot->isIgnored()) return;
139001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
139101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
1392c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1393c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy            right - left, bottom - top, rx, ry, paint);
1394c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1395c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
139601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1397c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1398c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1399c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1400c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
140101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
1402c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(x - radius, y - radius, texture, paint);
1403c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
140401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1405c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1406c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
140701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1408c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1409c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1410c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1411c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
1412c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14138b2f5267f16c295f12faab810527cd6311997e34Romain Guyvoid OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
14148b2f5267f16c295f12faab810527cd6311997e34Romain Guy        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
14158b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (mSnapshot->isIgnored()) return;
14168b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14178b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (fabs(sweepAngle) >= 360.0f) {
14188b2f5267f16c295f12faab810527cd6311997e34Romain Guy        drawOval(left, top, right, bottom, paint);
14198b2f5267f16c295f12faab810527cd6311997e34Romain Guy        return;
14208b2f5267f16c295f12faab810527cd6311997e34Romain Guy    }
14218b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14228b2f5267f16c295f12faab810527cd6311997e34Romain Guy    glActiveTexture(gTextureUnits[0]);
14238b2f5267f16c295f12faab810527cd6311997e34Romain Guy    const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
14248b2f5267f16c295f12faab810527cd6311997e34Romain Guy            startAngle, sweepAngle, useCenter, paint);
14258b2f5267f16c295f12faab810527cd6311997e34Romain Guy    drawShape(left, top, texture, paint);
14268b2f5267f16c295f12faab810527cd6311997e34Romain Guy}
14278b2f5267f16c295f12faab810527cd6311997e34Romain Guy
1428c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1429c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        SkPaint* paint) {
1430c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1431c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1432c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1433c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1434c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
143501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
143601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
14375c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
1438c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (p->getStyle() != SkPaint::kFill_Style) {
1439c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        drawRectAsShape(left, top, right, bottom, p);
1440c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        return;
1441c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    }
1442c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14436926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
14446926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
14456926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
14466926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1447026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    SkXfermode::Mode mode;
1448746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (!mCaches.extensions.hasFramebufferFetch()) {
1449a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1450a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (!isMode) {
1451a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // Assume SRC_OVER
1452a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            mode = SkXfermode::kSrcOver_Mode;
1453a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        }
1454a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    } else {
1455a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        mode = getXfermode(p->getXfermode());
1456026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    }
1457026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1458026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    int color = p->getColor();
1459026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    drawColorRect(left, top, right, bottom, color, mode);
1460c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
14619d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1462e8e62a4a032a80409114a37908b5f18ab0080848Romain Guyvoid OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1463e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        float x, float y, SkPaint* paint) {
1464b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (text == NULL || count == 0) {
1465e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        return;
1466e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1467af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1468e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
1469f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    paint->setAntiAlias(true);
1470e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
1471a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy    float length = -1.0f;
1472e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    switch (paint->getTextAlign()) {
1473e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kCenter_Align:
1474e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1475e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length / 2.0f;
1476e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1477e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kRight_Align:
1478e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1479e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length;
1480e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1481e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        default:
1482e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1483e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1484e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
14856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // TODO: Handle paint->getTextScaleX()
14863a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldX = x;
14873a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldY = y;
14886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const bool pureTranslate = mSnapshot->transform->isPureTranslate();
14896620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate) {
14906620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
14916620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
14926620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
14936620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
1494b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1495b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
1496fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy            paint->getTextSize());
1497e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
149886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    int alpha;
149986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    SkXfermode::Mode mode;
150086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
15019d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
15021e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    if (mHasShadow) {
1503b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
1504fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
15051e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy                count, mShadowRadius);
15061e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        const AutoTexture autoCleanup(shadow);
15070a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
150886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sx = x - shadow->left + mShadowDx;
150986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sy = y - shadow->top + mShadowDy;
151086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
151186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
151286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
151386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        glActiveTexture(gTextureUnits[0]);
151486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDraw();
151586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawWithTexture(true);
151686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
151786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawBlending(true, mode);
151886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawProgram();
151986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
152086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawTexture(shadow->id);
152186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawPureColorUniforms();
152286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
15230a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
15240a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
152586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        finishDrawTexture();
15261e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    }
15271e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
1528b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1529b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy        return;
1530b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    }
1531b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy
15326620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // Pick the appropriate texture filtering
15336620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    bool linearFilter = mSnapshot->transform->changesBounds();
15346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate && !linearFilter) {
15356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
15366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15375b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
153886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
153986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDraw();
154086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawDirtyRegionsDisabled();
154186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawWithTexture(true);
154286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
154386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilter();
154486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShader();
154586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawBlending(true, mode);
154686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawProgram();
154786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawModelView(x, y, x, y, pureTranslate, true);
154886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawTexture(fontRenderer.getTexture(linearFilter));
154986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
155086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilterUniforms();
155186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShaderUniforms(pureTranslate);
155206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
15536620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
15545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
15555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
15565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1557f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = hasLayer();
15585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
1559f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = false;
15605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
15619d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
156203750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
15636620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
1564f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            hasActiveLayer ? &bounds : NULL)) {
15655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1566f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        if (hasActiveLayer) {
15676620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            if (!pureTranslate) {
15686620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mSnapshot->transform->mapRect(bounds);
15696620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            }
1570f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            dirtyLayerUnchecked(bounds, getRegion());
15715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
15725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
15735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
1574694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1575694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1576fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1577a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy
15783a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
1579694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}
1580694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
15817fbcc0492fca03857e3c45064f4aa040af817d55Romain Guyvoid OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
1582af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1583dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
158401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
15857fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1586fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    const PathTexture* texture = mCaches.pathCache.get(path, paint);
15879cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
158822158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
15897fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
15908b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float x = texture->left - texture->offset;
15918b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float y = texture->top - texture->offset;
15928b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
159301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
15947fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
15957fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1596ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1597ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
15986c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return;
15996c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
16006c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16016c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glActiveTexture(gTextureUnits[0]);
16026c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16036c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int alpha;
16046c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    SkXfermode::Mode mode;
16056c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
16066c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1607ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->alpha = alpha;
1608ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->mode = mode;
16096c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1610f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
1611c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy    if (!layer->region.isEmpty()) {
1612c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        if (layer->region.isRect()) {
1613c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1614c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            composeLayerRect(layer, r);
1615c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        } else if (layer->mesh) {
16168168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            const float a = alpha / 255.0f;
1617c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect& rect = layer->layer;
1618c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1619c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDraw();
1620c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawWithTexture();
16218168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            setupDrawColor(a, a, a, a);
1622c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilter();
1623c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1624c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawProgram();
1625c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawPureColorUniforms();
1626c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilterUniforms();
1627c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawTexture(layer->texture);
16284f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            // TODO: The current layer, if any, will be dirtied with the bounding box
16294f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       of the layer we are drawing. Since the layer we are drawing has
16304f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       a mesh, we know the dirty region, we should use it instead
1631c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1632c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
1633c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1634c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1635c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy                    GL_UNSIGNED_SHORT, layer->meshIndices);
1636c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1637c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            finishDrawTexture();
16383a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
16393a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
16403a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            drawRegionRects(layer->region);
16413a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
1642c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        }
1643f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
1644f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#else
1645ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1646ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    composeLayerRect(layer, r);
1647f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
16486c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
16496c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16506926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
1651d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// Shaders
1652d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1653d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
1654d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyvoid OpenGLRenderer::resetShader() {
165506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
165606f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}
165706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
165806f96e2652e4855b6520ad9dd70583677605b79aRomain Guyvoid OpenGLRenderer::setupShader(SkiaShader* shader) {
165906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = shader;
166006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1661fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
166206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    }
16637fac2e18339f765320d759e8d4c090f92431959eRomain Guy}
16647fac2e18339f765320d759e8d4c090f92431959eRomain Guy
1665d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1666db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy// Color filters
1667db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
1668db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1669db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::resetColorFilter() {
1670db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1671db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1672db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1673db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1674db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = filter;
1675db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1676db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1677db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
16781e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy// Drop shadow
16791e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
16801e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16811e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::resetShadow() {
16821e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
16831e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
16841e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16851e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
16861e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = true;
16871e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowRadius = radius;
16881e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDx = dx;
16891e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDy = dy;
16901e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowColor = color;
16911e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
16921e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
16931e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
16946926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy// Drawing implementation
16956926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
16966926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
169701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guyvoid OpenGLRenderer::drawPathTexture(const PathTexture* texture,
169801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        float x, float y, SkPaint* paint) {
169901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (quickReject(x, y, x + texture->width, y + texture->height)) {
170001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        return;
170101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    }
170201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
170301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    int alpha;
170401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    SkXfermode::Mode mode;
170501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
170601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
170701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDraw();
170801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawWithTexture(true);
170901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
171001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilter();
171101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShader();
171201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawBlending(true, mode);
171301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawProgram();
171401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawModelView(x, y, x + texture->width, y + texture->height);
171501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawTexture(texture->id);
171601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawPureColorUniforms();
171701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilterUniforms();
171801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShaderUniforms();
171901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
172001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
172101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
172201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
172301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    finishDrawTexture();
172401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
172501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1726f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy// Same values used by Skia
17270a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdStrikeThru_Offset   (-6.0f / 21.0f)
17280a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Offset    (1.0f / 9.0f)
17290a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Thickness (1.0f / 18.0f)
17300a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17310a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guyvoid OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
17320a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float x, float y, SkPaint* paint) {
17330a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    // Handle underline and strike-through
17340a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    uint32_t flags = paint->getFlags();
17350a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
17360a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float underlineWidth = length;
17370a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        // If length is > 0.0f, we already measured the text for the text alignment
17380a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (length <= 0.0f) {
17390a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            underlineWidth = paint->measureText(text, bytesCount);
17400a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17410a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17420a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float offsetX = 0;
17430a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        switch (paint->getTextAlign()) {
17440a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kCenter_Align:
17450a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth * 0.5f;
17460a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17470a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kRight_Align:
17480a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth;
17490a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17500a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            default:
17510a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
17520a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17530a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17540a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (underlineWidth > 0.0f) {
1755e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float textSize = paint->getTextSize();
1756f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            // TODO: Support stroke width < 1.0f when we have AA lines
1757f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
17580a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1759e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float left = x - offsetX;
17600a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            float top = 0.0f;
1761e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1762f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            int linesCount = 0;
1763f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1764f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1765f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy
1766f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const int pointsCount = 4 * linesCount;
1767e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            float points[pointsCount];
1768e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            int currentPoint = 0;
17690a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17700a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) {
17710a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdUnderline_Offset;
1772e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1773e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1774e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1775e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
17760a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
17770a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17780a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) {
17790a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdStrikeThru_Offset;
1780e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1781e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1782e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1783e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
17840a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
1785e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1786e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            SkPaint linesPaint(*paint);
1787e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            linesPaint.setStrokeWidth(strokeWidth);
1788e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1789e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            drawLines(&points[0], pointsCount, &linesPaint);
17900a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
17910a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    }
17920a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy}
17930a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1794026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
17951c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy        int color, SkXfermode::Mode mode, bool ignoreTransform) {
1796d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    // If a shader is set, preserve only the alpha
179706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1798d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        color |= 0x00ffffff;
1799d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
1800d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
180170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
180270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(color);
180370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShader();
180470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
180570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(mode);
180670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
180770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawModelView(left, top, right, bottom, ignoreTransform);
180870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorUniforms();
180970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShaderUniforms(ignoreTransform);
181070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
181170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawSimpleMesh();
1812c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
1813c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1814c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy}
1815c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
181682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
18178164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        Texture* texture, SkPaint* paint) {
181882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    int alpha;
181982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    SkXfermode::Mode mode;
182082ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
182182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
18228164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
18238164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy
18246620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
18256620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
18266620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
18276620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
18286620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
18296620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
18306620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
18316620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
18326620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
18336620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
18346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
18356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
183685bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy}
183785bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
1838bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
1839a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1840a979474f15b454c8e2963f239a3770e200bb227cRomain Guy    drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
184103750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy            (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
1842f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1843f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1844f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guyvoid OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
1845a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
18466820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy        GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
18475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
18488694230ff25fa0a60e480d424843e56b718f0516Romain Guy
184970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
185070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawWithTexture();
185170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(alpha, alpha, alpha, alpha);
185270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
185370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(blend, mode, swapSrcDst);
185470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
185570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!dirty) {
185670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawDirtyRegionsDisabled();
1857db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
18585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!ignoreScale) {
185970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelView(left, top, right, bottom, ignoreTransform);
186003750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    } else {
186170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
1862db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
186386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
186470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
186570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawTexture(texture);
186670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawMesh(vertices, texCoords, vbo);
1867db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
18686820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy    glDrawArrays(drawMode, 0, elementsCount);
186970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
187070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    finishDrawTexture();
187182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy}
187282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1873a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guyvoid OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
1874f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        ProgramDescription& description, bool swapSrcDst) {
187582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    blend = blend || mode != SkXfermode::kSrcOver_Mode;
187682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    if (blend) {
1877a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (mode < SkXfermode::kPlus_Mode) {
1878a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!mCaches.blend) {
1879a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glEnable(GL_BLEND);
1880a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
188182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1882f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1883f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
188482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1885a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1886a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glBlendFunc(sourceMode, destMode);
1887a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastSrcMode = sourceMode;
1888a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastDstMode = destMode;
1889a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1890a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1891a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // These blend modes are not supported by OpenGL directly and have
1892a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // to be implemented using shaders. Since the shader will perform
1893a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // the blending, turn blending off here
1894746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            if (mCaches.extensions.hasFramebufferFetch()) {
1895a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                description.framebufferMode = mode;
1896f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy                description.swapSrcDst = swapSrcDst;
1897a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1898a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1899a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (mCaches.blend) {
1900a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glDisable(GL_BLEND);
1901a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1902a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            blend = false;
190382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        }
1904fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    } else if (mCaches.blend) {
190582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        glDisable(GL_BLEND);
190682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    }
1907fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    mCaches.blend = blend;
1908bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
1909bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
1910889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guybool OpenGLRenderer::useProgram(Program* program) {
1911d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    if (!program->isInUse()) {
1912fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
1913d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        program->use();
1914fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mCaches.currentProgram = program;
19156926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return false;
1916260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    }
19176926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    return true;
1918260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy}
1919260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
1920026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
1921ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    TextureVertex* v = &mMeshVertices[0];
192282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v1);
192382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v1);
192482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v2);
192582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v2);
19268ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
19278ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19285c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
19298ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    if (paint) {
1930746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        if (!mCaches.extensions.hasFramebufferFetch()) {
1931a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1932a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!isMode) {
1933a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                // Assume SRC_OVER
1934a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                *mode = SkXfermode::kSrcOver_Mode;
1935a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1936a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1937a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            *mode = getXfermode(paint->getXfermode());
19388ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
19398ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19408ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Skia draws using the color's alpha channel if < 255
19418ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Otherwise, it uses the paint's alpha
19428ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        int color = paint->getColor();
19438ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = (color >> 24) & 0xFF;
19448ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        if (*alpha == 255) {
19458ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy            *alpha = paint->getAlpha();
19468ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
19478ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    } else {
19488ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *mode = SkXfermode::kSrcOver_Mode;
19498ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = 255;
19508ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    }
1951026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy}
1952026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1953a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain GuySkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
1954a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    if (mode == NULL) {
1955a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        return SkXfermode::kSrcOver_Mode;
1956a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    }
1957a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    return mode->fMode;
1958a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy}
1959a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1960746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guyvoid OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
19618164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    bool bound = false;
19628164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapS != texture->wrapS) {
19638164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glBindTexture(GL_TEXTURE_2D, texture->id);
19648164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        bound = true;
19658164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
19668164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapS = wrapS;
19678164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
19688164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapT != texture->wrapT) {
19693e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        if (!bound) {
19703e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy            glBindTexture(GL_TEXTURE_2D, texture->id);
19713e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        }
19728164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
19738164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapT = wrapT;
19748164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
1975a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
1976a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
19779d5316e3f56d138504565ff311145ac01621dff4Romain Guy}; // namespace uirenderer
1978e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}; // namespace android
1979