OpenGLRenderer.cpp revision 80911b851764b073310fd0bffdf4a7db0d8fdd0b
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);
14784962f2fe6ce779c583cc54b11a9de2b6a568117Romain Guy    mSnapshot->fbo = getTargetFbo();
14884962f2fe6ce779c583cc54b11a9de2b6a568117Romain Guy
1498fb954263dd2f918ad339045cc6d82e346515599Romain Guy    mSaveCount = 1;
150f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
151fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glViewport(0, 0, mWidth, mHeight);
152fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy
153d90f23e24a4d1768d5a7ed0e7072e67af6330a45Romain Guy    glDisable(GL_DITHER);
154bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
1557d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glEnable(GL_SCISSOR_TEST);
1567d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    glScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
1577d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy    mSnapshot->setClip(left, top, right, bottom);
1587d7b5490a0b0763e831b31bc11f17d8159b5914aRomain Guy
1596b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    if (!opaque) {
1606b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1616b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy        glClear(GL_COLOR_BUFFER_BIT);
1626b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy    }
163bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
164bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
165b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guyvoid OpenGLRenderer::finish() {
166b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#if DEBUG_OPENGL
167b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    GLenum status = GL_NO_ERROR;
168b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    while ((status = glGetError()) != GL_NO_ERROR) {
169b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        LOGD("GL error from OpenGLRenderer: 0x%x", status);
170a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        switch (status) {
171a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy            case GL_OUT_OF_MEMORY:
172a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                LOGE("  OpenGLRenderer is out of memory!");
173a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy                break;
174a07105b7d2a27e6d69407bf96ddb773bddb5e553Romain Guy        }
175b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
176b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy#endif
177c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#if DEBUG_MEMORY_USAGE
178c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    mCaches.dumpMemoryUsage();
179e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy#else
180e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    if (mCaches.getDebugLevel() & kDebugMemory) {
181e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy        mCaches.dumpMemoryUsage();
182e190aa69756aecfaffabdd4c6d32cb6b3220d842Romain Guy    }
183c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy#endif
184b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy}
185b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy
1866c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::interrupt() {
187da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    if (mCaches.currentProgram) {
188da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        if (mCaches.currentProgram->isInUse()) {
189da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram->remove();
190da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy            mCaches.currentProgram = NULL;
191da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy        }
192da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    }
19350c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.unbindMeshBuffer();
194da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
195da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
1966c319ca1275c8db892c39b48fc54864c949f9171Romain Guyvoid OpenGLRenderer::resume() {
197eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight());
198da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
199da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy    glEnable(GL_SCISSOR_TEST);
200746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
201da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
202f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    glDisable(GL_DITHER);
203f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
20442f3a4b7c35dd1ea1922356eb3611af8cde05179Romain Guy    glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
20503750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
20650c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy
20750c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    mCaches.blend = true;
20850c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glEnable(GL_BLEND);
20950c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
21050c0f093d942a59d4e01b2c76d26c0e9d6ed796cRomain Guy    glBlendEquation(GL_FUNC_ADD);
211da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy}
212da8532c6f48b4c10b5e2ccb9e08690341efa1616Romain Guy
213cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guybool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) {
214daf98e941e140e8739458126640183b9f296a2abChet Haase    interrupt();
215f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    if (mDirtyClip) {
216f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy        setScissorFromClip();
217f90f8171e6acb56f9f87093c01fd586f2140697aRomain Guy    }
218d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy
21980911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    Rect clip(*mSnapshot->clipRect);
22080911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    clip.snapToPixelBoundaries();
22180911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
222d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy#if RENDER_LAYERS_AS_REGIONS
223d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    // Since we don't know what the functor will draw, let's dirty
224d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    // tne entire clip region
225d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    if (hasLayer()) {
226d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy        dirtyLayerUnchecked(clip, getRegion());
227d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy    }
228d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy#endif
229d643bb56fdf21973ea75984f0816b7dc024698dfRomain Guy
23080911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    struct {
23180911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        // Input: current clip rect
23280911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        int clipLeft;
23380911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        int clipTop;
23480911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        int clipRight;
23580911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        int clipBottom;
23680911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
23780911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        // Output: dirty region to redraw
23880911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        float dirtyLeft;
23980911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        float dirtyTop;
24080911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        float dirtyRight;
24180911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        float dirtyBottom;
24280911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    } constraints;
24380911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
24480911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    constraints.clipLeft = clip.left;
24580911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    constraints.clipTop = clip.top;
24680911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    constraints.clipRight = clip.right;
24780911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    constraints.clipBottom = clip.bottom;
24880911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy
24980911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy    status_t result = (*functor)(0, &constraints);
250cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy
251cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    if (result != 0) {
25280911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy        Rect localDirty(constraints.dirtyLeft, constraints.dirtyTop,
25380911b851764b073310fd0bffdf4a7db0d8fdd0bRomain Guy                constraints.dirtyRight, constraints.dirtyBottom);
254cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy        dirty.unionWith(localDirty);
255cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    }
256cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy
257daf98e941e140e8739458126640183b9f296a2abChet Haase    resume();
258cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy    return result != 0;
259daf98e941e140e8739458126640183b9f296a2abChet Haase}
260daf98e941e140e8739458126640183b9f296a2abChet Haase
261f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
262f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// State management
263f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
264f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
265bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::getSaveCount() const {
2667ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    return mSaveCount;
267bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
268bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
269bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyint OpenGLRenderer::save(int flags) {
2708aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return saveSnapshot(flags);
271bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
272bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
273bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restore() {
2742542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (mSaveCount > 1) {
2752542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2767ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
277bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
278bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
279bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::restoreToCount(int saveCount) {
2808fb954263dd2f918ad339045cc6d82e346515599Romain Guy    if (saveCount < 1) saveCount = 1;
281bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2828fb954263dd2f918ad339045cc6d82e346515599Romain Guy    while (mSaveCount > saveCount) {
2832542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy        restoreSnapshot();
2847ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
285bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
286bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
2878aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guyint OpenGLRenderer::saveSnapshot(int flags) {
2888aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot = new Snapshot(mSnapshot, flags);
2898fb954263dd2f918ad339045cc6d82e346515599Romain Guy    return mSaveCount++;
290bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
291bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
292bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guybool OpenGLRenderer::restoreSnapshot() {
2937ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
294bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
295eb99356a0548684a501766e6a524529ab93304c8Romain Guy    bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
296bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
297bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    sp<Snapshot> current = mSnapshot;
2987ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    sp<Snapshot> previous = mSnapshot->previous;
299bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
300eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (restoreOrtho) {
301eb99356a0548684a501766e6a524529ab93304c8Romain Guy        Rect& r = previous->viewport;
302eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glViewport(r.left, r.top, r.right, r.bottom);
303eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mOrthoMatrix.load(current->orthoMatrix);
304eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
305eb99356a0548684a501766e6a524529ab93304c8Romain Guy
3068b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSaveCount--;
3078b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    mSnapshot = previous;
3088b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
3092542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    if (restoreClip) {
310746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
3118fb954263dd2f918ad339045cc6d82e346515599Romain Guy    }
3122542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy
3135ec9924d24495822b589f1a17996655d66273b30Romain Guy    if (restoreLayer) {
3145ec9924d24495822b589f1a17996655d66273b30Romain Guy        composeLayer(current, previous);
3155ec9924d24495822b589f1a17996655d66273b30Romain Guy    }
3165ec9924d24495822b589f1a17996655d66273b30Romain Guy
3172542d199745cdf3ec910b8e3e4cff5851ed24e9bRomain Guy    return restoreClip;
318d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
3195cbbce535744b89df5ecea95de21ee3733298260Romain Guy
320f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
321bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy// Layers
322bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
323bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
324bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
3255c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        SkPaint* p, int flags) {
326eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const GLuint previousFbo = mSnapshot->fbo;
327eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const int count = saveSnapshot(flags);
328d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
329af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (!mSnapshot->isIgnored()) {
330e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        int alpha = 255;
331e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        SkXfermode::Mode mode;
332e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
333e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        if (p) {
334e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            alpha = p->getAlpha();
335e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            if (!mCaches.extensions.hasFramebufferFetch()) {
336e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
337e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                if (!isMode) {
338e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    // Assume SRC_OVER
339e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                    mode = SkXfermode::kSrcOver_Mode;
340e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                }
341e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            } else {
342e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy                mode = getXfermode(p->getXfermode());
343a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
344a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
345e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy            mode = SkXfermode::kSrcOver_Mode;
346d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy        }
347d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
348dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo);
349dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
350d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy
351d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return count;
352bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
353bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
354bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyint OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
355bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        int alpha, int flags) {
3565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (alpha >= 255 - ALPHA_THRESHOLD) {
3578aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, NULL, flags);
3588b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    } else {
3598aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        SkPaint paint;
3608aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        paint.setAlpha(alpha);
3618aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy        return saveLayer(left, top, right, bottom, &paint, flags);
3628b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    }
363d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy}
364bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
3651c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
3661c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Layers are viewed by Skia are slightly different than layers in image editing
3671c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * programs (for instance.) When a layer is created, previously created layers
3681c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * and the frame buffer still receive every drawing command. For instance, if a
3691c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * layer is created and a shape intersecting the bounds of the layers and the
3701c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * framebuffer is draw, the shape will be drawn on both (unless the layer was
3711c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
3721c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3731c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * A way to implement layers is to create an FBO for each layer, backed by an RGBA
3741c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * texture. Unfortunately, this is inefficient as it requires every primitive to
3751c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be drawn n + 1 times, where n is the number of active layers. In practice this
3761c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * means, for every primitive:
3771c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Switch active frame buffer
3781c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Change viewport, clip and projection matrix
3791c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *   - Issue the drawing
3801c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3811c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Switching rendering target n + 1 times per drawn primitive is extremely costly.
3826b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * To avoid this, layers are implemented in a different way here, at least in the
3836b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * general case. FBOs are used, as an optimization, when the "clip to layer" flag
3846b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * is set. When this flag is set we can redirect all drawing operations into a
3856b7bd24659fb175fe1f0e97c86c18969918b496aRomain Guy * single FBO.
3861c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3871c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * This implementation relies on the frame buffer being at least RGBA 8888. When
3881c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * a layer is created, only a texture is created, not an FBO. The content of the
3891c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer contained within the layer's bounds is copied into this texture
39087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
3911c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * buffer and drawing continues as normal. This technique therefore treats the
3921c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * frame buffer as a scratch buffer for the layers.
3931c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
3941c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * To compose the layers back onto the frame buffer, each layer texture
3951c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * (containing the original frame buffer data) is drawn as a simple quad over
3961c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * the frame buffer. The trick is that the quad is set as the composition
3971c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * destination in the blending equation, and the frame buffer becomes the source
3981c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * of the composition.
3991c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
4001c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Drawing layers with an alpha value requires an extra step before composition.
4011c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * An empty quad is drawn over the layer's region in the frame buffer. This quad
4021c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
4031c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * quad is used to multiply the colors in the frame buffer. This is achieved by
4041c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * changing the GL blend functions for the GL_FUNC_ADD blend equation to
4051c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * GL_ZERO, GL_SRC_ALPHA.
4061c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy *
4071c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Because glCopyTexImage2D() can be slow, an alternative implementation might
4081c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * be use to draw a single clipped layer. The implementation described above
4091c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * is correct in every case.
41087a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *
41187a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy * (1) The frame buffer is actually not cleared right away. To allow the GPU
41287a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     to potentially optimize series of calls to glCopyTexImage2D, the frame
41387a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     buffer is left untouched until the first drawing operation. Only when
41487a76578f76f4a6bceb187da6b7a01899ca0d85aRomain Guy *     something actually gets drawn are the layers regions cleared.
4151c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
416d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guybool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
417eb99356a0548684a501766e6a524529ab93304c8Romain Guy        float right, float bottom, int alpha, SkXfermode::Mode mode,
418eb99356a0548684a501766e6a524529ab93304c8Romain Guy        int flags, GLuint previousFbo) {
419eb99356a0548684a501766e6a524529ab93304c8Romain Guy    LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
420fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
421dda570201ac851dd85af3861f7e575721d3345daRomain Guy
422eb99356a0548684a501766e6a524529ab93304c8Romain Guy    const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
423eb99356a0548684a501766e6a524529ab93304c8Romain Guy
424f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    // Window coordinates of the layer
4258aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    Rect bounds(left, top, right, bottom);
4267b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    if (!fboLayer) {
427eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mSnapshot->transform->mapRect(bounds);
428ae517591d96be39f5f18a1716c856ade13a8bcc7Romain Guy
429eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Layers only make sense if they are in the framebuffer's bounds
430ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        if (bounds.intersect(*snapshot->clipRect)) {
431ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // We cannot work with sub-pixels in this case
432ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            bounds.snapToPixelBoundaries();
433ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy
434ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // When the layer is not an FBO, we may use glCopyTexImage so we
435ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // need to make sure the layer does not extend outside the bounds
436ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            // of the framebuffer
437ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            if (!bounds.intersect(snapshot->previous->viewport)) {
438ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy                bounds.setEmpty();
439ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            }
440ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        } else {
441ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy            bounds.setEmpty();
442ad37cd3b5d3de9dd0858af04fbccd102e8ff4b0eRomain Guy        }
443eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
444bf434114cbf55b216fdc76fc8d65a75e84c9dab5Romain Guy
445746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
446746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            bounds.getHeight() > mCaches.maxTextureSize) {
44732963c393a804db2cd86c24443c2f1c6eee6064bRomain Guy        snapshot->empty = fboLayer;
448dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    } else {
449e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy        snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
450dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
451dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
452dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    // Bail out if we won't draw in this snapshot
453af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (snapshot->invisible || snapshot->empty) {
454b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy        return false;
455b025b9c8b4efefadb01937db61a1f8ee7d2452bfRomain Guy    }
456dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glActiveTexture(gTextureUnits[0]);
4588550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
459f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy    if (!layer) {
460f18fd99b5c182329cd8936a9611f0103d8ece44aRomain Guy        return false;
461bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    }
462bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
463dda570201ac851dd85af3861f7e575721d3345daRomain Guy    layer->mode = mode;
464f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    layer->alpha = alpha;
4658aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    layer->layer.set(bounds);
4668550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height),
4678550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy            bounds.getWidth() / float(layer->width), 0.0f);
468171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy    layer->colorFilter = mColorFilter;
469dda570201ac851dd85af3861f7e575721d3345daRomain Guy
4708fb954263dd2f918ad339045cc6d82e346515599Romain Guy    // Save the layer in the snapshot
4718fb954263dd2f918ad339045cc6d82e346515599Romain Guy    snapshot->flags |= Snapshot::kFlagIsLayer;
472dda570201ac851dd85af3861f7e575721d3345daRomain Guy    snapshot->layer = layer;
4731d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
474eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
4755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return createFboLayer(layer, bounds, snapshot, previousFbo);
476eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
477eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Copy the framebuffer into the layer
478eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindTexture(GL_TEXTURE_2D, layer->texture);
479514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!bounds.isEmpty()) {
480514fb18827186591d66973c2362c859b64b63556Romain Guy            if (layer->empty) {
481514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left,
482514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, layer->width, layer->height, 0);
483514fb18827186591d66973c2362c859b64b63556Romain Guy                layer->empty = false;
484514fb18827186591d66973c2362c859b64b63556Romain Guy            } else {
485514fb18827186591d66973c2362c859b64b63556Romain Guy                glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
486514fb18827186591d66973c2362c859b64b63556Romain Guy                        snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
487514fb18827186591d66973c2362c859b64b63556Romain Guy            }
4887b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
4897b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            // Clear the framebuffer where the layer will draw
4907b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glScissor(bounds.left, mSnapshot->height - bounds.bottom,
4917b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy                    bounds.getWidth(), bounds.getHeight());
4927b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
4937b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            glClear(GL_COLOR_BUFFER_BIT);
4947b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
4957b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy            dirtyClip();
496ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy        }
497eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
498f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
499d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guy    return true;
500bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
501bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
5025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guybool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot,
5035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLuint previousFbo) {
5045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    layer->fbo = mCaches.fboCache.get();
5055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
5075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->region = &snapshot->layer->region;
5085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagFboTarget;
5095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect clip(bounds);
5125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->transform->mapRect(clip);
5135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(*snapshot->clipRect);
5145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
5155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(snapshot->previous->viewport);
5165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mat4 inverse;
5185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.loadInverse(*mSnapshot->transform);
5195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    inverse.mapRect(clip);
5215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.snapToPixelBoundaries();
5225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    clip.intersect(bounds);
5235ec9924d24495822b589f1a17996655d66273b30Romain Guy    clip.translate(-bounds.left, -bounds.top);
5245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagIsFboLayer;
5265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->fbo = layer->fbo;
5275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
5285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
5295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
5305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->height = bounds.getHeight();
5315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->flags |= Snapshot::kFlagDirtyOrtho;
5325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    snapshot->orthoMatrix.load(mOrthoMatrix);
5335b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5345b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Bind texture to FBO
5355b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
5365b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glBindTexture(GL_TEXTURE_2D, layer->texture);
5375b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5385b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Initialize the texture if needed
5395b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->empty) {
5405b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->empty = false;
5415b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0,
5425b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                GL_RGBA, GL_UNSIGNED_BYTE, NULL);
5435b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5445b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5455b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
5465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->texture, 0);
5475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
5495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
5505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (status != GL_FRAMEBUFFER_COMPLETE) {
5515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        LOGE("Framebuffer incomplete (GL error code 0x%x)", status);
5525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previousFbo);
5545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glDeleteTextures(1, &layer->texture);
5555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        mCaches.fboCache.put(layer->fbo);
5565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        delete layer;
5585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return false;
5605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
5615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
5625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
5645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
5655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
5665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
5675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glClear(GL_COLOR_BUFFER_BIT);
5685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    dirtyClip();
5705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // Change the ortho projection
5725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
5735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
5745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    return true;
5765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
5775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
5781c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy/**
5791c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy * Read the documentation of createLayer() before doing anything in this method.
5801c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy */
5811d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guyvoid OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
5821d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    if (!current->layer) {
5831d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LOGE("Attempting to compose a layer that does not exist");
5841d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        return;
5851d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
5861d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
5875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
588eb99356a0548684a501766e6a524529ab93304c8Romain Guy
589eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
590eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Unbind current FBO and restore previous one
591eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
592eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
593eb99356a0548684a501766e6a524529ab93304c8Romain Guy
5941d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Layer* layer = current->layer;
5951d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    const Rect& rect = layer->layer;
5961d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
597eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (!fboLayer && layer->alpha < 255) {
598f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
5991c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy                layer->alpha << 24, SkXfermode::kDstIn_Mode, true);
6005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Required below, composeLayerRect() will divide by 255
6015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->alpha = 255;
602f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    }
603f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy
60403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
6058b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
606746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
6075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // When the layer is stored in an FBO, we can save a bit of fillrate by
6095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // drawing only the dirty region
610eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
6115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
612171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
613171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            setupColorFilter(layer->colorFilter);
614171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
6155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRegion(layer, rect);
616171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        if (layer->colorFilter) {
617171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy            resetColorFilter();
618171c592f0b7066acf279863c8a52ddabea49d3dbRomain Guy        }
619eb99356a0548684a501766e6a524529ab93304c8Romain Guy    } else {
620514fb18827186591d66973c2362c859b64b63556Romain Guy        if (!rect.isEmpty()) {
621514fb18827186591d66973c2362c859b64b63556Romain Guy            dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
622514fb18827186591d66973c2362c859b64b63556Romain Guy            composeLayerRect(layer, rect, true);
623514fb18827186591d66973c2362c859b64b63556Romain Guy        }
624eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
6251d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
626eb99356a0548684a501766e6a524529ab93304c8Romain Guy    if (fboLayer) {
627eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Detach the texture from the FBO
628eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, current->fbo);
629eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
630eb99356a0548684a501766e6a524529ab93304c8Romain Guy        glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
631eb99356a0548684a501766e6a524529ab93304c8Romain Guy
632eb99356a0548684a501766e6a524529ab93304c8Romain Guy        // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed
633eb99356a0548684a501766e6a524529ab93304c8Romain Guy        mCaches.fboCache.put(current->fbo);
634eb99356a0548684a501766e6a524529ab93304c8Romain Guy    }
635eb99356a0548684a501766e6a524529ab93304c8Romain Guy
636746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    dirtyClip();
637746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy
638eb99356a0548684a501766e6a524529ab93304c8Romain Guy    // Failing to add the layer to the cache should happen only if the layer is too large
6398550c4c7b5952b7a4e1e0ede95c9492d03099a13Romain Guy    if (!mCaches.layerCache.put(layer)) {
6401d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        LAYER_LOGD("Deleting layer");
6411d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        glDeleteTextures(1, &layer->texture);
6421d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy        delete layer;
6431d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    }
6441d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy}
6451d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy
6465b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
6475b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    const Rect& texCoords = layer->texCoords;
6485b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom);
6495b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6505b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture,
6515b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0],
6525b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap);
6535b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6545b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
6555b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
6565b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6575b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
6585b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
6596217a71cd281003a376d998269d577d26a61c206Romain Guy#if RENDER_LAYERS_RECT_AS_RECT
6605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (layer->region.isRect()) {
6615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        composeLayerRect(layer, rect);
6625b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
6635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        return;
6645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
6656217a71cd281003a376d998269d577d26a61c206Romain Guy#endif
6665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!layer->region.isEmpty()) {
6685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        size_t count;
6695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const android::Rect* rects = layer->region.getArray(&count);
6705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float alpha = layer->alpha / 255.0f;
6725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texX = 1.0f / float(layer->width);
6735b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float texY = 1.0f / float(layer->height);
674f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        const float height = rect.getHeight();
6755b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6765b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        TextureVertex* mesh = mCaches.getRegionMesh();
6775b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        GLsizei numQuads = 0;
6785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6797230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDraw();
6807230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawWithTexture();
6817230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawColor(alpha, alpha, alpha, alpha);
682ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilter();
6837230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
6847230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawProgram();
6857230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawDirtyRegionsDisabled();
6867230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawPureColorUniforms();
687ada830f639591b99c3e40de22b07296c7932a33fRomain Guy        setupDrawColorFilterUniforms();
6887230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawTexture(layer->texture);
689c038ea358aadca082198f3effc550d33135bf426Romain Guy        setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
6907230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]);
6915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        for (size_t i = 0; i < count; i++) {
6935b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const android::Rect* r = &rects[i];
6945b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
6955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u1 = r->left * texX;
696f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v1 = (height - r->top) * texY;
6975b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const float u2 = r->right * texX;
698f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            const float v2 = (height - r->bottom) * texY;
6995b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7005b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            // TODO: Reject quads outside of the clip
7015b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->top, u1, v1);
7025b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->top, u2, v1);
7035b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
7045b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
7055b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7065b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            numQuads++;
7075b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7085b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            if (numQuads >= REGION_MESH_QUAD_COUNT) {
7095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
7105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                numQuads = 0;
7115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy                mesh = mCaches.getRegionMesh();
7125b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
7135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7145b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        if (numQuads > 0) {
7165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
7175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
7207230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        finishDrawTexture();
7215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7225b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
7233a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawRegionRects(layer->region);
7245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7255b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7265b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        layer->region.clear();
7275b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7285b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
7295b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    composeLayerRect(layer, rect);
7305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7333a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guyvoid OpenGLRenderer::drawRegionRects(const Region& region) {
7343a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
7353a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    size_t count;
7363a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    const android::Rect* rects = region.getArray(&count);
7373a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7383a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    uint32_t colors[] = {
7393a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7fff0000, 0x7f00ff00,
7403a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            0x7f0000ff, 0x7fff00ff,
7413a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    };
7423a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7433a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int offset = 0;
7443a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    int32_t top = rects[0].top;
7453a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7463a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    for (size_t i = 0; i < count; i++) {
7473a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        if (top != rects[i].top) {
7483a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            offset ^= 0x2;
7493a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            top = rects[i].top;
7503a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        }
7513a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7523a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
7533a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy        drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
7543a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy                SkXfermode::kSrcOver_Mode);
7553a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy    }
7563a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
7573a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy}
7583a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
7595b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7605b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom, const mat4 transform) {
7615b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
762f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7635b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
7645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        transform.mapRect(bounds);
765f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7665b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7675b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7685b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7695b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
7705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guyvoid OpenGLRenderer::dirtyLayer(const float left, const float top,
7715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        const float right, const float bottom) {
7725b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
773f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    if (hasLayer()) {
7745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        Rect bounds(left, top, right, bottom);
775f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        dirtyLayerUnchecked(bounds, getRegion());
7761bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    }
7771bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#endif
7781bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy}
7791bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy
7801bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guyvoid OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
7811bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy#if RENDER_LAYERS_AS_REGIONS
7821bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy    if (bounds.intersect(*mSnapshot->clipRect)) {
7831bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        bounds.snapToPixelBoundaries();
7841bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
7851bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy        if (!dirty.isEmpty()) {
7861bd1bad0b4a75e633c5edbe802bf6fb4dd765161Romain Guy            region->orSelf(dirty);
7875b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
7885b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
7895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
7905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy}
7915b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
792bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy///////////////////////////////////////////////////////////////////////////////
793f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Transforms
794f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
795f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
796f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::translate(float dx, float dy) {
7978aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->translate(dx, dy, 0.0f);
798f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
799f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
800f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::rotate(float degrees) {
8018aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f);
802f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
803f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
804f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::scale(float sx, float sy) {
8058aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->scale(sx, sy, 1.0f);
806f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
807f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
808807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guyvoid OpenGLRenderer::skew(float sx, float sy) {
809807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy    mSnapshot->transform->skew(sx, sy);
810807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy}
811807daf7df615b60ce6fc41355aabe3aa353cebabRomain Guy
812f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::setMatrix(SkMatrix* matrix) {
8138aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->load(*matrix);
814f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
815f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
81641030da16856c8869e1e51d4a0405432fa96614eRomain Guyconst float* OpenGLRenderer::getMatrix() const {
81799bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    if (mSnapshot->fbo != 0) {
81899bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy        return &mSnapshot->transform->data[0];
81999bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    }
82099bcdc52dcb365ed7d8cfa13540fb33fbcbbac9dRomain Guy    return &mIdentity.data[0];
82141030da16856c8869e1e51d4a0405432fa96614eRomain Guy}
82241030da16856c8869e1e51d4a0405432fa96614eRomain Guy
823f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::getMatrix(SkMatrix* matrix) {
8248aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->copyTo(*matrix);
825f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
826f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
827f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guyvoid OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
828e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    SkMatrix transform;
829e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->copyTo(transform);
830e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    transform.preConcat(*matrix);
831e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    mSnapshot->transform->load(transform);
832f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy}
833f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
834f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
835f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Clipping
836f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
837f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
838bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guyvoid OpenGLRenderer::setScissorFromClip() {
839e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    Rect clip(*mSnapshot->clipRect);
840e5ebcb0107a939395e03592fd44c746cd09e311dRomain Guy    clip.snapToPixelBoundaries();
841eb99356a0548684a501766e6a524529ab93304c8Romain Guy    glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
842746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    mDirtyClip = false;
8439d5316e3f56d138504565ff311145ac01621dff4Romain Guy}
8449d5316e3f56d138504565ff311145ac01621dff4Romain Guy
8459d5316e3f56d138504565ff311145ac01621dff4Romain Guyconst Rect& OpenGLRenderer::getClipBounds() {
846079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    return mSnapshot->getLocalClip();
847bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy}
848bb9524b6bdddc7ac77d8628daa8b366b8a7be4a4Romain Guy
849c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guybool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
850af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) {
851dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy        return true;
852dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy    }
853dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
8541d83e1981c8b89da93dff37a4f8b2b1ad8480b44Romain Guy    Rect r(left, top, right, bottom);
8558aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    mSnapshot->transform->mapRect(r);
856d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    r.snapToPixelBoundaries();
857d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
858d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    Rect clipRect(*mSnapshot->clipRect);
859d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    clipRect.snapToPixelBoundaries();
860d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy
861d2a1ff003b9b1212c9ab4b5f679b5b097720a359Romain Guy    return !clipRect.intersects(r);
862c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
863c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
864079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guybool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
865079ba2c85b15e882629b8d188f5fbdb42f7f8eeaRomain Guy    bool clipped = mSnapshot->clip(left, top, right, bottom, op);
8667ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    if (clipped) {
867746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        dirtyClip();
8687ae7ac48aa2b53453c9805075171ecd5bcafd7deRomain Guy    }
8698aef54fa17f2a3753d9a8f2027629bc480088f69Romain Guy    return !mSnapshot->clipRect->isEmpty();
870e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}
871e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy
872f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
87370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy// Drawing commands
87470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
87570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
87670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDraw() {
87770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mDirtyClip) {
87870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setScissorFromClip();
87970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
88070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.reset();
88170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = false;
88270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = false;
88370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = mColorR = mColorG = mColorB = 0.0f;
88470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTextureUnit = 0;
88570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = true;
8868d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mTexCoordsSlot = -1;
88770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
88870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
88970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
89070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasTexture = true;
89170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mDescription.hasAlpha8Texture = isAlpha8;
89270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
89370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
89470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(int color) {
8958d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(color, (color >> 24) & 0xFF);
8968d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
8978d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
8988d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawColor(int color, int alpha) {
8998d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mColorA = alpha / 255.0f;
90070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    const float a = mColorA / 255.0f;
901fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorR = a * ((color >> 16) & 0xFF);
902fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorG = a * ((color >>  8) & 0xFF);
903fa7952dbb86a8aff0c90418aa77dddee3802c23aRomain Guy    mColorB = a * ((color      ) & 0xFF);
90470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
90570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
90670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
90770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
90886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
90986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = alpha / 255.0f;
91086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    const float a = mColorA / 255.0f;
91186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = a * ((color >> 16) & 0xFF);
91286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = a * ((color >>  8) & 0xFF);
91386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = a * ((color      ) & 0xFF);
91486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
91586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
91686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
91786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
91870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
91970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorA = a;
92070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorR = r;
92170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorG = g;
92270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorB = b;
92370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mColorSet = true;
92470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mSetShaderColor = mDescription.setColor(r, g, b, a);
92570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
92670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
92786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) {
92886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorA = a;
92986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorR = r;
93086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorG = g;
93186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorB = b;
93286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mColorSet = true;
93386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a);
93486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy}
93586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
93670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShader() {
93770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
93870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->describe(mDescription, mCaches.extensions);
93970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
94070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
94170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilter() {
94370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
94470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->describe(mDescription, mCaches.extensions);
94570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
94670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
94770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
94870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
94970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
95070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
95170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
95370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
95470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode,
95570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mDescription, swapSrcDst);
95670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
95770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
95870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawProgram() {
95970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    useProgram(mCaches.programCache.get(mDescription));
96070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
96170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
96270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
96370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTrackDirtyRegions = false;
96470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
96570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
96670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
96770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform) {
96870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mModelView.loadTranslate(left, top, 0.0f);
96970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreTransform) {
97070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
97170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
97270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
97370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
97470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
97570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
97670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
97770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
9788d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawModelViewIdentity() {
9798d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform);
9808d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
9818d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
98270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
98370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        bool ignoreTransform, bool ignoreModelView) {
98470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!ignoreModelView) {
98570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadTranslate(left, top, 0.0f);
98670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.scale(right - left, bottom - top, 1.0f);
98770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
98870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mModelView.loadIdentity();
98970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
99086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    bool dirty = right - left > 0.0f && bottom - top > 0.0f;
99186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    if (!ignoreTransform) {
99286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform);
99386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) {
99486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy            dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
99586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        }
99686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    } else {
99786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity);
99886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
99986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    }
100070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
100170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
100270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorUniforms() {
10035536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mColorSet || (mShader && mSetShaderColor)) {
100470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
100570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
100670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
100770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
100886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guyvoid OpenGLRenderer::setupDrawPureColorUniforms() {
10095536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    if (mSetShaderColor) {
101086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
10115536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy    }
10125536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy}
10135536841e5452172e7772d84ad5f4a2fc7059c9ddRomain Guy
101470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
101570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mShader) {
101670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        if (ignoreTransform) {
101770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            mModelView.loadInverse(*mSnapshot->transform);
101870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        }
101970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit);
102070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
102170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
102270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
10238d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guyvoid OpenGLRenderer::setupDrawShaderIdentityUniforms() {
10248d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    if (mShader) {
10258d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit);
10268d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
10278d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy}
10288d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
102970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawColorFilterUniforms() {
103070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (mColorFilter) {
103170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mColorFilter->setupProgram(mCaches.currentProgram);
103270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
103370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
103470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
103570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawSimpleMesh() {
103670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mCaches.bindMeshBuffer();
103770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
103870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, 0);
103970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
104070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
104170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawTexture(GLuint texture) {
104270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    bindTexture(texture);
104370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);
104470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
104570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
104670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glEnableVertexAttribArray(mTexCoordsSlot);
104770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
104870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
104970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
105070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!vertices) {
105170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
105270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    } else {
105370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        mCaches.unbindMeshBuffer();
105470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    }
105570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
105670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy            gMeshStride, vertices);
1057cf28957c89127ccfc82d535903c776124665446aDavid Li    if (mTexCoordsSlot >= 0) {
10588d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy        glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
10598d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    }
106070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
106170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
106270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guyvoid OpenGLRenderer::finishDrawTexture() {
106370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    glDisableVertexAttribArray(mTexCoordsSlot);
106470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy}
106570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
106670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy///////////////////////////////////////////////////////////////////////////////
1067f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy// Drawing
1068f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy///////////////////////////////////////////////////////////////////////////////
1069f6a11b8a9e25ff9861bbba19251bea84d8a5daf2Romain Guy
10707b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guybool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height,
10717b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy        Rect& dirty, uint32_t level) {
10727b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    if (quickReject(0.0f, 0.0f, width, height)) {
10737b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy        return false;
10747b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy    }
10757b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
10760fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // All the usual checks and setup operations (quickReject, setupDraw, etc.)
10770fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    // will be performed by the display list itself
10780fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    if (displayList) {
1079cabfcc1364eb7e4de0b15b3574fba45027b45cfcRomain Guy        return displayList->replay(*this, dirty, level);
10800fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy    }
10817b5b6abf852c039983eded25ebe43a70fef5a4abRomain Guy
1082daf98e941e140e8739458126640183b9f296a2abChet Haase    return false;
10830fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy}
10840fe478ea04720a57ef3919dbc23711bc7eba517fRomain Guy
10855c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
10866926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float right = left + bitmap->width();
10876926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    const float bottom = top + bitmap->height();
10886926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
10896926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
10906926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
10916926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
10926926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
109386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
10948164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
10959cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
109622158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
109722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
10986926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    drawTextureRect(left, top, right, bottom, texture, paint);
10998ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
11008ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
11015c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
1102f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1103f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    const mat4 transform(*matrix);
1104f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy    transform.mapRect(r);
1105f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
11066926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(r.left, r.top, r.right, r.bottom)) {
11076926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
11086926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
11096926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
111086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
11118164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11129cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
111322158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
111422158e139a3d6c6a9787ca0de224e9368f643284Romain Guy
11155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // This could be done in a cheaper way, all we need is pass the matrix
11165b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    // to the vertex shader. The save/restore is a bit overkill.
11175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    save(SkCanvas::kMatrix_SaveFlag);
11185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    concatMatrix(matrix);
11195b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
11205b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    restore();
1121f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy}
1122f86ef57f8bcd8ba43ce222ec6a8b4f67d3600640Romain Guy
11235a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guyvoid OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
11245a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        float* vertices, int* colors, SkPaint* paint) {
11255a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    // TODO: Do a quickReject
11265a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!vertices || mSnapshot->isIgnored()) {
11275a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        return;
11285a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11295a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11305a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    glActiveTexture(gTextureUnits[0]);
11315a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
11325a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    if (!texture) return;
11335a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const AutoTexture autoCleanup(texture);
11345a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
11355a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11365a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    int alpha;
11375a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    SkXfermode::Mode mode;
11385a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
11395a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11405a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    const uint32_t count = meshWidth * meshHeight * 6;
11415a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1142b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float left = FLT_MAX;
1143b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float top = FLT_MAX;
1144b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float right = FLT_MIN;
1145b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    float bottom = FLT_MIN;
1146b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1147b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1148b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    bool hasActiveLayer = hasLayer();
1149b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#else
1150b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    bool hasActiveLayer = false;
1151b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
1152b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1153a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    // TODO: Support the colors array
1154a566b7c3aada08d37cf08096c972e3e641bed773Romain Guy    TextureVertex mesh[count];
11555a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    TextureVertex* vertex = mesh;
11565a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    for (int32_t y = 0; y < meshHeight; y++) {
11575a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        for (int32_t x = 0; x < meshWidth; x++) {
11585a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            uint32_t i = (y * (meshWidth + 1) + x) * 2;
11595a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11605a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u1 = float(x) / meshWidth;
11615a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float u2 = float(x + 1) / meshWidth;
11625a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v1 = float(y) / meshHeight;
11635a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            float v2 = float(y + 1) / meshHeight;
11645a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11655a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ax = i + (meshWidth + 1) * 2;
11665a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int ay = ax + 1;
11675a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int bx = i;
11685a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int by = bx + 1;
11695a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cx = i + 2;
11705a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int cy = cx + 1;
11715a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dx = i + (meshWidth + 1) * 2 + 2;
11725a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            int dy = dx + 1;
11735a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11745a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11755a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1);
11765a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11775a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
11785a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2);
11795a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1);
11805a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2);
1181b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
1182b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1183b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            if (hasActiveLayer) {
1184b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                // TODO: This could be optimized to avoid unnecessary ops
1185b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
1186b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
1187b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
1188b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy                bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
1189b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            }
1190b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
11915a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy        }
11925a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    }
11935a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
1194b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#if RENDER_LAYERS_AS_REGIONS
1195b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    if (hasActiveLayer) {
1196b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy        dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
1197b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy    }
1198b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy#endif
1199b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy
12005a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy    drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
12015a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy            mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0],
1202b18d2d0079b4dbf5675ab79b7111b3dfb3cc1ad0Romain Guy            GL_TRIANGLES, count, false, false, 0, false, false);
12035a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy}
12045a7b466a2b4b7ced739bd5c31e022de61650545aRomain Guy
12058ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guyvoid OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
12068ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float srcLeft, float srcTop, float srcRight, float srcBottom,
12078ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy         float dstLeft, float dstTop, float dstRight, float dstBottom,
12085c13d89c1332fcc499379b9064b891187b75ca32Chet Haase         SkPaint* paint) {
12096926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
12106926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
12116926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
12126926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1213746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12148164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12159cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
121622158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12178164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
12188ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
12198ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float width = texture->width;
12208ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float height = texture->height;
1221c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
12228ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u1 = srcLeft / width;
12238ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v1 = srcTop / height;
12248ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float u2 = srcRight / width;
12258ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    const float v2 = srcBottom / height;
1226c1396e93b6a5286a5183c00c781b62e940a12c1fRomain Guy
122703750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
12288ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(u1, v1, u2, v2);
12298ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
123003750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    int alpha;
123103750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    SkXfermode::Mode mode;
123203750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
123303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy
12346620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
12356620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f);
12366620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f);
12376620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12386620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop),
12396620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->id, alpha / 255.0f, mode, texture->blend,
12406620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
12416620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount, false, true);
12426620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
12436620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f,
12446620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
12456620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
12466620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
12478ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
12488ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1249ce0537b80087a6225273040a987414b1dd081aa0Romain Guy}
1250ce0537b80087a6225273040a987414b1dd081aa0Romain Guy
12514aa90573bbf86db0d33a3a790c5dbd0d93b95cfeRomain Guyvoid OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
12524bb942083a0d4db746adf95349108dd8ef842e32Romain Guy        const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
12535c13d89c1332fcc499379b9064b891187b75ca32Chet Haase        float left, float top, float right, float bottom, SkPaint* paint) {
12546926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
12556926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
12566926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
12576926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1258746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    glActiveTexture(gTextureUnits[0]);
12598164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    Texture* texture = mCaches.textureCache.get(bitmap);
12609cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
126122158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
12628164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
1263f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1264f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    int alpha;
1265f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    SkXfermode::Mode mode;
1266f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
1267f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
12682728f961614a385df1f056fc24803a9f65c90fabRomain Guy    const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
12694bb942083a0d4db746adf95349108dd8ef842e32Romain Guy            right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
1270f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1271a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy    if (mesh && mesh->verticesCount > 0) {
12726620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const bool pureTranslate = mSnapshot->transform->isPureTranslate();
1273a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#if RENDER_LAYERS_AS_REGIONS
12745b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        // Mark the current layer dirty where we are going to draw the patch
12758168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy        if (hasLayer() && mesh->hasEmptyQuads) {
1276c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetX = left + mSnapshot->transform->getTranslateX();
1277c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy            const float offsetY = top + mSnapshot->transform->getTranslateY();
12785b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            const size_t count = mesh->quads.size();
12795b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            for (size_t i = 0; i < count; i++) {
12808ab4079ca27e36e5c584495bcd71b573598ac021Romain Guy                const Rect& bounds = mesh->quads.itemAt(i);
12816620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                if (pureTranslate) {
1282c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float x = (int) floorf(bounds.left + offsetX + 0.5f);
1283c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    const float y = (int) floorf(bounds.top + offsetY + 0.5f);
1284c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
12856620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                } else {
1286c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                    dirtyLayer(left + bounds.left, top + bounds.top,
1287c78b5d50f961ac8f696f8282979ae283cacd3574Romain Guy                            left + bounds.right, top + bounds.bottom, *mSnapshot->transform);
12886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                }
12895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy            }
12905b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
1291a5ef39a21683189e5906c9f252b997f0508e350dRomain Guy#endif
12925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
12936620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        if (pureTranslate) {
12946620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
12956620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
12966620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
12976620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
12986620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
12996620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
13006620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
13016620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        } else {
13026620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
13036620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
13046620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
13056620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                    true, !mesh->hasEmptyQuads);
13066620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        }
1307054dc1840941665e32036f9523df51720ad069c8Romain Guy    }
1308f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1309f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
13105c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
1311af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1312dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
1313a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isAA = paint->isAntiAlias();
1314a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const float strokeWidth = paint->getStrokeWidth() * 0.5f;
1315a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // A stroke width of 0 has a special meaningin Skia:
1316a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    // it draws an unscaled 1px wide line
1317a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    const bool isHairLine = paint->getStrokeWidth() == 0.0f;
1318a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1319759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    int alpha;
1320759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    SkXfermode::Mode mode;
1321759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
1322759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1323a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    int verticesCount = count >> 2;
13247230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy    int generatedVerticesCount = 0;
1325a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1326a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA needs more vertices
1327a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 6;
1328a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1329a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: AA will be different
1330a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        verticesCount *= 2;
1331a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    }
1332759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1333a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex lines[verticesCount];
1334a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    TextureVertex* vertex = &lines[0];
1335759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
13368d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDraw();
13378d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColor(paint->getColor(), alpha);
13388d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilter();
13398d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShader();
13408d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawBlending(mode);
13418d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawProgram();
13428d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawModelViewIdentity();
13438d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorUniforms();
13448d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawColorFilterUniforms();
13458d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawShaderIdentityUniforms();
13468d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy    setupDrawMesh(vertex);
1347759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
1348a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    if (!isHairLine) {
1349a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1350a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
1351a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // a = start point, b = end point
1352a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 a(points[i], points[i + 1]);
1353a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 b(points[i + 2], points[i + 3]);
1354a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1355a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Bias to snap to the same pixels as Skia
1356a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            a += 0.375;
1357a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            b += 0.375;
1358a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1359a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Find the normal to the line
1360a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 n = (b - a).copyNormalized() * strokeWidth;
1361a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            float x = n.x;
1362a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.x = -n.y;
1363a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            n.y = x;
1364a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
1365a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            // Four corners of the rectangle defining a thick line
1366a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p1 = a - n;
1367a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p2 = a + n;
1368a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p3 = b + n;
1369a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy            vec2 p4 = b - n;
1370a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13717230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x)));
13727230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x)));
13737230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y)));
13747230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y)));
13757230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13767230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
13777230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Draw the line as 2 triangles, could be optimized
13787230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // by using only 4 vertices and the correct indices
13797230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // Also we should probably used non textured vertices
13807230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                // when line AA is disabled to save on bandwidth
13817230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13827230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f);
13837230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13847230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f);
13857230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f);
13867230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f);
13877230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13887230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 6;
13897230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
13907230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
13917230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1392a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
1393a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy
13947230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
13957230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            // GL_LINE does not give the result we want to match Skia
13967230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_TRIANGLES, 0, generatedVerticesCount);
13977230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
1398a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy    } else {
1399a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        // TODO: Handle the AA case
1400a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        for (int i = 0; i < count; i += 4) {
14017230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float left = fmin(points[i], points[i + 1]);
14027230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float right = fmax(points[i], points[i + 1]);
14037230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float top = fmin(points[i + 2], points[i + 3]);
14047230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            const float bottom = fmax(points[i + 2], points[i + 3]);
14057230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
14067230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            if (!quickReject(left, top, right, bottom)) {
14077230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
14087230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f);
14097230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
14107230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                generatedVerticesCount += 2;
14117230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy
14127230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy                dirtyLayer(left, top, right, bottom, *mSnapshot->transform);
14137230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            }
1414a957eea78557cb47a91d44d9e6ee641c58cf1c07Romain Guy        }
14158d0d4783a0206c5884bf0b958d181f450ba5207dRomain Guy
14167230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        if (generatedVerticesCount > 0) {
14177230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glLineWidth(1.0f);
14187230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy            glDrawArrays(GL_LINES, 0, generatedVerticesCount);
14197230a74e9a36dfc6c4346c14e325bf07cd05b380Romain Guy        }
142029d8997bd43b7c4ad37fc3d6f91eaafa74913c88Romain Guy    }
1421759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy}
1422759ea80dca64ad652110a129e0d8bf93fea79f61Romain Guy
142385bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guyvoid OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
1424e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy    // No need to check against the clip, we fill the clip region
1425af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1426e45362cad94c014d8b3765cb102db0f8c0d92500Romain Guy
1427ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    Rect& clip(*mSnapshot->clipRect);
1428ae88e5e8e9cb6c9539314c4360c5b20f8ec1fefcRomain Guy    clip.snapToPixelBoundaries();
142970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
14303d58c03de0d8877b36cdb78b0ca8b5cac7f600e2Romain Guy    drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
1431c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
14329d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1433c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) {
143401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (!texture) return;
143501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const AutoTexture autoCleanup(texture);
143601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
143701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float x = left + texture->left - texture->offset;
143801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const float y = top + texture->top - texture->offset;
143901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
144001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
144101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
144201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1443c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
1444c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        float rx, float ry, SkPaint* paint) {
144501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (mSnapshot->isIgnored()) return;
144601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
144701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
1448c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect(
1449c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy            right - left, bottom - top, rx, ry, paint);
1450c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1451c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
145201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1453c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) {
1454c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1455c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1456c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
145701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint);
1458c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(x - radius, y - radius, texture, paint);
1459c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
146001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1461c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) {
1462c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
146301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1464c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1465c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint);
1466c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
1467c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy}
1468c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14698b2f5267f16c295f12faab810527cd6311997e34Romain Guyvoid OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
14708b2f5267f16c295f12faab810527cd6311997e34Romain Guy        float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) {
14718b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (mSnapshot->isIgnored()) return;
14728b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14738b2f5267f16c295f12faab810527cd6311997e34Romain Guy    if (fabs(sweepAngle) >= 360.0f) {
14748b2f5267f16c295f12faab810527cd6311997e34Romain Guy        drawOval(left, top, right, bottom, paint);
14758b2f5267f16c295f12faab810527cd6311997e34Romain Guy        return;
14768b2f5267f16c295f12faab810527cd6311997e34Romain Guy    }
14778b2f5267f16c295f12faab810527cd6311997e34Romain Guy
14788b2f5267f16c295f12faab810527cd6311997e34Romain Guy    glActiveTexture(gTextureUnits[0]);
14798b2f5267f16c295f12faab810527cd6311997e34Romain Guy    const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top,
14808b2f5267f16c295f12faab810527cd6311997e34Romain Guy            startAngle, sweepAngle, useCenter, paint);
14818b2f5267f16c295f12faab810527cd6311997e34Romain Guy    drawShape(left, top, texture, paint);
14828b2f5267f16c295f12faab810527cd6311997e34Romain Guy}
14838b2f5267f16c295f12faab810527cd6311997e34Romain Guy
1484c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guyvoid OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom,
1485c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        SkPaint* paint) {
1486c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (mSnapshot->isIgnored()) return;
1487c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
1488c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    glActiveTexture(gTextureUnits[0]);
1489c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint);
1490c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    drawShape(left, top, texture, paint);
149101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
149201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
14935c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
1494c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    if (p->getStyle() != SkPaint::kFill_Style) {
1495c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        drawRectAsShape(left, top, right, bottom, p);
1496c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy        return;
1497c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy    }
1498c1cd9ba335b293f11e1082447ef08e474710a05fRomain Guy
14996926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    if (quickReject(left, top, right, bottom)) {
15006926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return;
15016926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    }
15026926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
1503026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    SkXfermode::Mode mode;
1504746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy    if (!mCaches.extensions.hasFramebufferFetch()) {
1505a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
1506a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (!isMode) {
1507a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // Assume SRC_OVER
1508a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            mode = SkXfermode::kSrcOver_Mode;
1509a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        }
1510a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    } else {
1511a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        mode = getXfermode(p->getXfermode());
1512026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    }
1513026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
1514026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    int color = p->getColor();
1515026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy    drawColorRect(left, top, right, bottom, color, mode);
1516c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy}
15179d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1518e8e62a4a032a80409114a37908b5f18ab0080848Romain Guyvoid OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
1519e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        float x, float y, SkPaint* paint) {
1520b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (text == NULL || count == 0) {
1521e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        return;
1522e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1523af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1524e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
1525f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy    paint->setAntiAlias(true);
1526e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
1527a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy    float length = -1.0f;
1528e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    switch (paint->getTextAlign()) {
1529e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kCenter_Align:
1530e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1531e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length / 2.0f;
1532e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1533e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        case SkPaint::kRight_Align:
1534e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            length = paint->measureText(text, bytesCount);
1535e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            x -= length;
1536e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1537e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy        default:
1538e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy            break;
1539e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy    }
1540e8e62a4a032a80409114a37908b5f18ab0080848Romain Guy
15413a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldX = x;
15423a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    const float oldY = y;
15436620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const bool pureTranslate = mSnapshot->transform->isPureTranslate();
15446620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate) {
15456620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f);
15466620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f);
15476620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15486620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
1549b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint);
1550b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()),
1551fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy            paint->getTextSize());
1552e2d345ea67e2960b37bfdc0fc8626d1bfa747404Romain Guy
155386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    int alpha;
155486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    SkXfermode::Mode mode;
155586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
15569d13fe25f4f10b25776b1dc5c858f9ebb0b28b30Romain Guy
15571e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    if (mHasShadow) {
1558b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
1559fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount,
15601e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy                count, mShadowRadius);
15611e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        const AutoTexture autoCleanup(shadow);
15620a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
156386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sx = x - shadow->left + mShadowDx;
156486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const float sy = y - shadow->top + mShadowDy;
156586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
156686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        const int shadowAlpha = ((mShadowColor >> 24) & 0xFF);
156786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy
156886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        glActiveTexture(gTextureUnits[0]);
156986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDraw();
157086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawWithTexture(true);
157186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawAlpha8Color(mShadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
157286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawBlending(true, mode);
157386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawProgram();
157486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height, pureTranslate);
157586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawTexture(shadow->id);
157686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawPureColorUniforms();
157786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
15780a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
15790a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
158086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy        finishDrawTexture();
15811e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    }
15821e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
1583b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) {
1584b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy        return;
1585b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy    }
1586b146b1214ef6e4fc75d98779b22434335764cfdbRomain Guy
15876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    // Pick the appropriate texture filtering
15886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    bool linearFilter = mSnapshot->transform->changesBounds();
15896620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (pureTranslate && !linearFilter) {
15906620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
15916620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
15925b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
159386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    glActiveTexture(gTextureUnits[0]);
159486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDraw();
159586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawDirtyRegionsDisabled();
159686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawWithTexture(true);
159786568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
159886568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilter();
159986568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShader();
160086568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawBlending(true, mode);
160186568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawProgram();
160286568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawModelView(x, y, x, y, pureTranslate, true);
160386568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawTexture(fontRenderer.getTexture(linearFilter));
160486568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
160586568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawColorFilterUniforms();
160686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawShaderUniforms(pureTranslate);
160706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
16086620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
16095b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
16105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy
16115b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1612f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = hasLayer();
16135b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#else
1614f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    bool hasActiveLayer = false;
16155b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
161603750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    mCaches.unbindMeshBuffer();
1617894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk
1618894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    // Tell font renderer the locations of position and texture coord
1619894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    // attributes so it can bind its data properly
1620894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    int positionSlot = mCaches.currentProgram->position;
1621894df17eee708688c8a6e67941add2017239c790Alex Sakhartchouk    fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
16226620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
1623f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            hasActiveLayer ? &bounds : NULL)) {
16245b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#if RENDER_LAYERS_AS_REGIONS
1625f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy        if (hasActiveLayer) {
16266620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            if (!pureTranslate) {
16276620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                mSnapshot->transform->mapRect(bounds);
16286620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy            }
1629f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy            dirtyLayerUnchecked(bounds, getRegion());
16305b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        }
16315b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif
16325b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    }
1633694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1634694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
1635fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
1636a674ab74e359ac73b4c4dd6b1a3a771836ac7e40Romain Guy
16373a3fa1be9ab8e11edc660ecb35ae21ae0b5c8cc2Romain Guy    drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
1638694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}
1639694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
16407fbcc0492fca03857e3c45064f4aa040af817d55Romain Guyvoid OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
1641af636ebf5feb2837683fbfe965040cb706b32ec1Romain Guy    if (mSnapshot->isIgnored()) return;
1642dbc26d2ba13f80a7590c57de2d80530d96832969Romain Guy
164301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glActiveTexture(gTextureUnits[0]);
16447fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1645fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    const PathTexture* texture = mCaches.pathCache.get(path, paint);
16469cccc2b9bdd4850a3f9679569aaec3ab98477a5dRomain Guy    if (!texture) return;
164722158e139a3d6c6a9787ca0de224e9368f643284Romain Guy    const AutoTexture autoCleanup(texture);
16487fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
16498b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float x = texture->left - texture->offset;
16508b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy    const float y = texture->top - texture->offset;
16518b55f377655d13a445b08a0a8ed09b6e95c752b0Romain Guy
165201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    drawPathTexture(texture, x, y, paint);
16537fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy}
16547fbcc0492fca03857e3c45064f4aa040af817d55Romain Guy
1655ada830f639591b99c3e40de22b07296c7932a33fRomain Guyvoid OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) {
1656ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    if (!layer || quickReject(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight())) {
16576c319ca1275c8db892c39b48fc54864c949f9171Romain Guy        return;
16586c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    }
16596c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16606c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    glActiveTexture(gTextureUnits[0]);
16616c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
16626c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    int alpha;
16636c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    SkXfermode::Mode mode;
16646c319ca1275c8db892c39b48fc54864c949f9171Romain Guy    getAlphaAndMode(paint, &alpha, &mode);
16656c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1666ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->alpha = alpha;
1667ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    layer->mode = mode;
16686c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
1669f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#if RENDER_LAYERS_AS_REGIONS
1670c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy    if (!layer->region.isEmpty()) {
16716217a71cd281003a376d998269d577d26a61c206Romain Guy#if RENDER_LAYERS_RECT_AS_RECT
1672c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        if (layer->region.isRect()) {
1673c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1674c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            composeLayerRect(layer, r);
1675c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        } else if (layer->mesh) {
16766217a71cd281003a376d998269d577d26a61c206Romain Guy#else
16776217a71cd281003a376d998269d577d26a61c206Romain Guy        if (layer->mesh) {
16786217a71cd281003a376d998269d577d26a61c206Romain Guy#endif
16798168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            const float a = alpha / 255.0f;
1680c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            const Rect& rect = layer->layer;
1681c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1682c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDraw();
1683c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawWithTexture();
16848168396d1acbcb5fdd29eeda4c30b2803d5283aeRomain Guy            setupDrawColor(a, a, a, a);
1685c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilter();
1686c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawBlending(layer->blend || layer->alpha < 255, layer->mode, false);
1687c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawProgram();
1688c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawPureColorUniforms();
1689c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawColorFilterUniforms();
1690c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawTexture(layer->texture);
16914f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            // TODO: The current layer, if any, will be dirtied with the bounding box
16924f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       of the layer we are drawing. Since the layer we are drawing has
16934f09f5417b5c14fd31336dacfff86972e7885a03Romain Guy            //       a mesh, we know the dirty region, we should use it instead
1694c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1695c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
1696c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1697c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            glDrawElements(GL_TRIANGLES, layer->meshElementCount,
1698c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy                    GL_UNSIGNED_SHORT, layer->meshIndices);
1699c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy
1700c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy            finishDrawTexture();
17013a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy
17023a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#if DEBUG_LAYERS_AS_REGIONS
17033a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy            drawRegionRects(layer->region);
17043a3133d876caf60ebff2176ad75c3dcf0259148dRomain Guy#endif
1705c88e357d1ed9dadfc0efb3dfbe92f24460674ef5Romain Guy        }
1706f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy    }
1707f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#else
1708ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight());
1709ada830f639591b99c3e40de22b07296c7932a33fRomain Guy    composeLayerRect(layer, r);
1710f219da5e32e85deb442468ee9a63bb28eb198557Romain Guy#endif
17116c319ca1275c8db892c39b48fc54864c949f9171Romain Guy}
17126c319ca1275c8db892c39b48fc54864c949f9171Romain Guy
17136926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
1714d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy// Shaders
1715d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1716d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
1717d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guyvoid OpenGLRenderer::resetShader() {
171806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = NULL;
171906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}
172006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
172106f96e2652e4855b6520ad9dd70583677605b79aRomain Guyvoid OpenGLRenderer::setupShader(SkiaShader* shader) {
172206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    mShader = shader;
172306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1724fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
172506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    }
17267fac2e18339f765320d759e8d4c090f92431959eRomain Guy}
17277fac2e18339f765320d759e8d4c090f92431959eRomain Guy
1728d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy///////////////////////////////////////////////////////////////////////////////
1729db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy// Color filters
1730db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
1731db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1732db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::resetColorFilter() {
1733db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = NULL;
1734db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1735db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1736db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guyvoid OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
1737db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    mColorFilter = filter;
1738db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy}
1739db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
1740db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy///////////////////////////////////////////////////////////////////////////////
17411e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy// Drop shadow
17421e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
17431e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17441e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::resetShadow() {
17451e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = false;
17461e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
17471e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17481e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guyvoid OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
17491e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mHasShadow = true;
17501e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowRadius = radius;
17511e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDx = dx;
17521e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowDy = dy;
17531e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy    mShadowColor = color;
17541e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy}
17551e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
17561e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy///////////////////////////////////////////////////////////////////////////////
17576926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy// Drawing implementation
17586926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy///////////////////////////////////////////////////////////////////////////////
17596926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy
176001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guyvoid OpenGLRenderer::drawPathTexture(const PathTexture* texture,
176101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        float x, float y, SkPaint* paint) {
176201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    if (quickReject(x, y, x + texture->width, y + texture->height)) {
176301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy        return;
176401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    }
176501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
176601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    int alpha;
176701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    SkXfermode::Mode mode;
176801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
176901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
177001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDraw();
177101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawWithTexture(true);
177201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawAlpha8Color(paint->getColor(), alpha);
177301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilter();
177401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShader();
177501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawBlending(true, mode);
177601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawProgram();
177701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawModelView(x, y, x + texture->width, y + texture->height);
177801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawTexture(texture->id);
177901d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawPureColorUniforms();
178001d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawColorFilterUniforms();
178101d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawShaderUniforms();
178201d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
178301d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
178401d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
178501d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
178601d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy    finishDrawTexture();
178701d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy}
178801d58e43ede5ca98cbebdd166f9b0c545032c01bRomain Guy
1789f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy// Same values used by Skia
17900a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdStrikeThru_Offset   (-6.0f / 21.0f)
17910a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Offset    (1.0f / 9.0f)
17920a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy#define kStdUnderline_Thickness (1.0f / 18.0f)
17930a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
17940a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guyvoid OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
17950a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float x, float y, SkPaint* paint) {
17960a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    // Handle underline and strike-through
17970a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    uint32_t flags = paint->getFlags();
17980a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
17990a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float underlineWidth = length;
18000a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        // If length is > 0.0f, we already measured the text for the text alignment
18010a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (length <= 0.0f) {
18020a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            underlineWidth = paint->measureText(text, bytesCount);
18030a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
18040a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18050a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        float offsetX = 0;
18060a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        switch (paint->getTextAlign()) {
18070a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kCenter_Align:
18080a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth * 0.5f;
18090a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18100a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            case SkPaint::kRight_Align:
18110a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                offsetX = underlineWidth;
18120a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18130a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            default:
18140a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                break;
18150a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
18160a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18170a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        if (underlineWidth > 0.0f) {
1818e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float textSize = paint->getTextSize();
1819f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            // TODO: Support stroke width < 1.0f when we have AA lines
1820f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
18210a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1822e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            const float left = x - offsetX;
18230a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            float top = 0.0f;
1824e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1825f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            int linesCount = 0;
1826f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
1827f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
1828f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy
1829f6834478b379856d3e5de92ddce7de0e6ba9fa4aRomain Guy            const int pointsCount = 4 * linesCount;
1830e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            float points[pointsCount];
1831e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            int currentPoint = 0;
18320a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18330a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kUnderlineText_Flag) {
18340a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdUnderline_Offset;
1835e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1836e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1837e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1838e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
18390a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
18400a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
18410a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            if (flags & SkPaint::kStrikeThruText_Flag) {
18420a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy                top = y + textSize * kStdStrikeThru_Offset;
1843e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left;
1844e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
1845e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = left + underlineWidth;
1846e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy                points[currentPoint++] = top;
18470a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy            }
1848e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1849e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            SkPaint linesPaint(*paint);
1850e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            linesPaint.setStrokeWidth(strokeWidth);
1851e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy
1852e20ecbd12d26467754a7770d44bcce2ea92335efRomain Guy            drawLines(&points[0], pointsCount, &linesPaint);
18530a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy        }
18540a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy    }
18550a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy}
18560a41749953f35d33f61b3119e3161a82bb5fa59eRomain Guy
1857026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
18581c740bce8a762f02b5283045a0e2de7c8fb41277Romain Guy        int color, SkXfermode::Mode mode, bool ignoreTransform) {
1859d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    // If a shader is set, preserve only the alpha
186006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy    if (mShader) {
1861d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        color |= 0x00ffffff;
1862d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    }
1863d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy
186470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
186570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(color);
186670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShader();
186770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
186870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(mode);
186970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
187070ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawModelView(left, top, right, bottom, ignoreTransform);
187170ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorUniforms();
187270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawShaderUniforms(ignoreTransform);
187370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
187470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawSimpleMesh();
1875c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
1876c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy    glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1877c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy}
1878c95c8d6bf8fda5c4e8ebd0033b789be7868e6e07Romain Guy
187982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
18808164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        Texture* texture, SkPaint* paint) {
188182ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    int alpha;
188282ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    SkXfermode::Mode mode;
188382ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    getAlphaAndMode(paint, &alpha, &mode);
188482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
18858164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
18868164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy
18876620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    if (mSnapshot->transform->isPureTranslate()) {
18886620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f);
18896620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f);
18906620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy
18916620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
18926620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
18936620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
18946620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    } else {
18956620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy        drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
18966620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
18976620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy                GL_TRIANGLE_STRIP, gMeshCount);
18986620c6d413f972819fada92b574f0fa9e96d36c1Romain Guy    }
189985bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy}
190085bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy
1901bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyvoid OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
1902a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
1903a979474f15b454c8e2963f239a3770e200bb227cRomain Guy    drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
190403750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy            (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
1905f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy}
1906f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guy
1907f7f93556c8fcc640ab5adef79d021a80a72a645aRomain Guyvoid OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
1908a979474f15b454c8e2963f239a3770e200bb227cRomain Guy        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
19096820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy        GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
19105b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy        bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
19118694230ff25fa0a60e480d424843e56b718f0516Romain Guy
191270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDraw();
191370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawWithTexture();
191470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColor(alpha, alpha, alpha, alpha);
191570ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilter();
191670ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawBlending(blend, mode, swapSrcDst);
191770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawProgram();
191870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    if (!dirty) {
191970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawDirtyRegionsDisabled();
1920db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
19215b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    if (!ignoreScale) {
192270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelView(left, top, right, bottom, ignoreTransform);
192303750a067e818ca7fbd0f590e2ff6a8fded21e6cRomain Guy    } else {
192470ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy        setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
1925db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy    }
192686568198f2c83d4ce5c4cd692eda074ba9de9ed2Romain Guy    setupDrawPureColorUniforms();
192770ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawColorFilterUniforms();
192870ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawTexture(texture);
192970ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    setupDrawMesh(vertices, texCoords, vbo);
1930db1938e0e6ef816e228c815adccebd5cb05f2aa8Romain Guy
19316820ac8b14b4558f5d8b833dde80895306a3e137Romain Guy    glDrawArrays(drawMode, 0, elementsCount);
193270ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy
193370ca14e08ae197547ac412e8a1210e1ebdfb2eb1Romain Guy    finishDrawTexture();
193482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy}
193582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1936a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guyvoid OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
1937f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy        ProgramDescription& description, bool swapSrcDst) {
193882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    blend = blend || mode != SkXfermode::kSrcOver_Mode;
193982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    if (blend) {
1940a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        if (mode < SkXfermode::kPlus_Mode) {
1941a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!mCaches.blend) {
1942a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glEnable(GL_BLEND);
1943a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
194482ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1945f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
1946f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy            GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
194782ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy
1948a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
1949a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glBlendFunc(sourceMode, destMode);
1950a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastSrcMode = sourceMode;
1951a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                mCaches.lastDstMode = destMode;
1952a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1953a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
1954a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // These blend modes are not supported by OpenGL directly and have
1955a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // to be implemented using shaders. Since the shader will perform
1956a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            // the blending, turn blending off here
1957746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy            if (mCaches.extensions.hasFramebufferFetch()) {
1958a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                description.framebufferMode = mode;
1959f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guy                description.swapSrcDst = swapSrcDst;
1960a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1961a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
1962a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (mCaches.blend) {
1963a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                glDisable(GL_BLEND);
1964a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1965a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            blend = false;
196682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        }
1967fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    } else if (mCaches.blend) {
196882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy        glDisable(GL_BLEND);
196982ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    }
1970fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy    mCaches.blend = blend;
1971bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy}
1972bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
1973889f8d1403761d5668115ced6cbb3f767cfe966dRomain Guybool OpenGLRenderer::useProgram(Program* program) {
1974d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy    if (!program->isInUse()) {
1975fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
1976d27977d1a91d5a6b3cc9fa7664ac7e835e7bd895Romain Guy        program->use();
1977fb8b763f762ae21923c58d64caa729b012f40e05Romain Guy        mCaches.currentProgram = program;
19786926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy        return false;
1979260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy    }
19806926c72e25b8dec3dd4b84af0819fa1937ae7296Romain Guy    return true;
1981260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy}
1982260e102162322958cf17dbd895cd6bd30dc87e32Romain Guy
1983026c5e16704e817cac7d9c382914c947e34f87e0Romain Guyvoid OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
1984ac670c0433d19397d4e36ced2110475b6f54fe26Romain Guy    TextureVertex* v = &mMeshVertices[0];
198582ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v1);
198682ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v1);
198782ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u1, v2);
198882ba814ca0dea659be2cc6523bc0137679d961ceRomain Guy    TextureVertex::setUV(v++, u2, v2);
19898ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy}
19908ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
19915c13d89c1332fcc499379b9064b891187b75ca32Chet Haasevoid OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
19928ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    if (paint) {
1993746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guy        if (!mCaches.extensions.hasFramebufferFetch()) {
1994a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
1995a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            if (!isMode) {
1996a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                // Assume SRC_OVER
1997a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy                *mode = SkXfermode::kSrcOver_Mode;
1998a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            }
1999a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        } else {
2000a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy            *mode = getXfermode(paint->getXfermode());
20018ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
20028ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy
20038ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Skia draws using the color's alpha channel if < 255
20048ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        // Otherwise, it uses the paint's alpha
20058ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        int color = paint->getColor();
20068ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = (color >> 24) & 0xFF;
20078ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        if (*alpha == 255) {
20088ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy            *alpha = paint->getAlpha();
20098ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        }
20108ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    } else {
20118ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *mode = SkXfermode::kSrcOver_Mode;
20128ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy        *alpha = 255;
20138ba548f81d1ab5f1750cbf86098c4a14e0b8beadRomain Guy    }
2014026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy}
2015026c5e16704e817cac7d9c382914c947e34f87e0Romain Guy
2016a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain GuySkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) {
2017b37cbec6d06578a72bdd9c2caa3fd964ade71c53Romain Guy    // In the future we should look at unifying the Porter-Duff modes and
2018b37cbec6d06578a72bdd9c2caa3fd964ade71c53Romain Guy    // SkXferModes so that we can use SkXfermode::IsMode(xfer, &mode).
2019a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    if (mode == NULL) {
2020a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy        return SkXfermode::kSrcOver_Mode;
2021a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    }
2022a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy    return mode->fMode;
2023a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy}
2024a5aed0d58962a24c44728ffc46dc9e1ba2f9fda5Romain Guy
2025746b7401ceb86b5f2805f8c0d3b39ac739152015Romain Guyvoid OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) {
20268164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    bool bound = false;
20278164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapS != texture->wrapS) {
20288164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glBindTexture(GL_TEXTURE_2D, texture->id);
20298164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        bound = true;
20308164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
20318164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapS = wrapS;
20328164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
20338164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    if (wrapT != texture->wrapT) {
20343e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        if (!bound) {
20353e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy            glBindTexture(GL_TEXTURE_2D, texture->id);
20363e3ba155fc0ef7a7f40f06762240d9ba22f62b19Romain Guy        }
20378164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
20388164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy        texture->wrapT = wrapT;
20398164c2d338781c3a3c4a443941070dca5d88f2a7Romain Guy    }
2040a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy}
2041a1db574036c9bc2d397b69f8200594027e1fff16Romain Guy
20429d5316e3f56d138504565ff311145ac01621dff4Romain Guy}; // namespace uirenderer
2043e4d011201cea40d46cb2b2eef401db8fddc5c9c6Romain Guy}; // namespace android
2044