blob: 85fa541faec2f4af0358979762c28c47f46b6d65 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy9d5316e2010-06-24 19:30:36 -070017#ifndef ANDROID_UI_OPENGL_RENDERER_H
18#define ANDROID_UI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070022
Romain Guyf6a11b82010-06-23 17:47:49 -070023#include <SkMatrix.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070024#include <SkXfermode.h>
Romain Guye4d01122010-06-16 18:44:05 -070025
Romain Guybb9524b2010-06-22 18:56:38 -070026#include <utils/RefBase.h>
27
Romain Guyf6a11b82010-06-23 17:47:49 -070028#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070029#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070030#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070031#include "Snapshot.h"
Romain Guybb9524b2010-06-22 18:56:38 -070032
Romain Guye4d01122010-06-16 18:44:05 -070033namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070034namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070035
Romain Guyf6a11b82010-06-23 17:47:49 -070036///////////////////////////////////////////////////////////////////////////////
37// Support
38///////////////////////////////////////////////////////////////////////////////
39
Romain Guy5cbbce52010-06-27 22:59:20 -070040/**
41 * Simple structure to describe a vertex with a position.
42 * This is used to draw filled rectangles without a texture.
43 */
Romain Guyc7d53492010-06-25 13:41:57 -070044struct SimpleVertex {
Romain Guy7ae7ac42010-06-25 13:46:18 -070045 float position[2];
Romain Guyc7d53492010-06-25 13:41:57 -070046}; // struct SimpleVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070047
Romain Guy5cbbce52010-06-27 22:59:20 -070048/**
49 * Simple structure to describe a vertex with a position and a texture.
50 */
Romain Guybd6b79b2010-06-26 00:13:53 -070051struct TextureVertex {
52 float position[2];
53 float texture[2];
54}; // struct TextureVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070055
Romain Guy026c5e162010-06-28 17:12:22 -070056/**
57 * Structure mapping Skia xfermodes to OpenGL blending factors.
58 */
59struct Blender {
60 SkXfermode::Mode mode;
61 GLenum src;
62 GLenum dst;
63}; // struct Blender
64
Romain Guyf6a11b82010-06-23 17:47:49 -070065///////////////////////////////////////////////////////////////////////////////
66// Renderer
67///////////////////////////////////////////////////////////////////////////////
68
Romain Guy5cbbce52010-06-27 22:59:20 -070069/**
70 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
71 * simplified version of Skia's Canvas API.
72 */
Romain Guy85bf02f2010-06-22 13:11:24 -070073class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070074public:
Romain Guy85bf02f2010-06-22 13:11:24 -070075 OpenGLRenderer();
76 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070077
78 void setViewport(int width, int height);
79 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070080
Romain Guybb9524b2010-06-22 18:56:38 -070081 int getSaveCount() const;
82 int save(int flags);
83 void restore();
84 void restoreToCount(int saveCount);
85
Romain Guybd6b79b2010-06-26 00:13:53 -070086 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
87 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
88
Romain Guyf6a11b82010-06-23 17:47:49 -070089 void translate(float dx, float dy);
90 void rotate(float degrees);
91 void scale(float sx, float sy);
92
93 void setMatrix(SkMatrix* matrix);
94 void getMatrix(SkMatrix* matrix);
95 void concatMatrix(SkMatrix* matrix);
96
Romain Guy9d5316e2010-06-24 19:30:36 -070097 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -070098 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -070099 bool clipRect(float left, float top, float right, float bottom);
100
Romain Guy85bf02f2010-06-22 13:11:24 -0700101 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700102 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700103
Romain Guy85bf02f2010-06-22 13:11:24 -0700104private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700105 /**
106 * Saves the current state of the renderer as a new snapshot.
107 * The new snapshot is saved in mSnapshot and the previous snapshot
108 * is linked from mSnapshot->previous.
109 *
110 * @return The new save count. This value can be passed to #restoreToCount()
111 */
Romain Guybb9524b2010-06-22 18:56:38 -0700112 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700113
114 /**
115 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
116 *
117 * @return True if the clip should be also reapplied by calling
118 * #setScissorFromClip().
119 */
Romain Guybb9524b2010-06-22 18:56:38 -0700120 bool restoreSnapshot();
121
Romain Guy5cbbce52010-06-27 22:59:20 -0700122 /**
123 * Sets the clipping rectangle using glScissor. The clip is defined by
124 * the current snapshot's clipRect member.
125 */
Romain Guybb9524b2010-06-22 18:56:38 -0700126 void setScissorFromClip();
127
Romain Guy5cbbce52010-06-27 22:59:20 -0700128 /**
129 * Draws a colored rectangle with the specified color. The specified coordinates
130 * are transformed by the current snapshot's transform matrix.
131 *
132 * @param left The left coordinate of the rectangle
133 * @param top The top coordinate of the rectangle
134 * @param right The right coordinate of the rectangle
135 * @param bottom The bottom coordinate of the rectangle
136 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700137 * @param mode The Skia xfermode to use
Romain Guy5cbbce52010-06-27 22:59:20 -0700138 */
Romain Guy026c5e162010-06-28 17:12:22 -0700139 void drawColorRect(float left, float top, float right, float bottom,
140 int color, SkXfermode::Mode mode);
Romain Guy5cbbce52010-06-27 22:59:20 -0700141
142 /**
143 * Draws a textured rectangle with the specified texture. The specified coordinates
144 * are transformed by the current snapshot's transform matrix.
145 *
146 * @param left The left coordinate of the rectangle
147 * @param top The top coordinate of the rectangle
148 * @param right The right coordinate of the rectangle
149 * @param bottom The bottom coordinate of the rectangle
150 * @param texture The texture name to map onto the rectangle
151 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
152 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700153 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
154 float alpha);
Romain Guyc7d53492010-06-25 13:41:57 -0700155
Romain Guy026c5e162010-06-28 17:12:22 -0700156 /**
157 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
158 * back to default is achieved by calling:
159 *
160 * resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
161 *
162 * @param u1 The left coordinate of the texture
163 * @param v1 The bottom coordinate of the texture
164 * @param u2 The right coordinate of the texture
165 * @param v2 The top coordinate of the texture
166 */
167 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
168
Romain Guybb9524b2010-06-22 18:56:38 -0700169 // Dimensions of the drawing surface
170 int mWidth, mHeight;
171
Romain Guy85bf02f2010-06-22 13:11:24 -0700172 // Matrix used for ortho projection in shaders
173 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700174
Romain Guyc7d53492010-06-25 13:41:57 -0700175 // Model-view matrix used to position/size objects
176 mat4 mModelView;
177
Romain Guybb9524b2010-06-22 18:56:38 -0700178 // Number of saved states
179 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700180 // Base state
181 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700182 // Current state
183 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700184
185 // Shaders
186 sp<DrawColorProgram> mDrawColorShader;
Romain Guybd6b79b2010-06-26 00:13:53 -0700187 sp<DrawTextureProgram> mDrawTextureShader;
Romain Guy026c5e162010-06-28 17:12:22 -0700188
189 // Used to draw textured quads
190 TextureVertex mDrawTextureVertices[4];
Romain Guybb9524b2010-06-22 18:56:38 -0700191}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700192
Romain Guy9d5316e2010-06-24 19:30:36 -0700193}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700194}; // namespace android
195
Romain Guy9d5316e2010-06-24 19:30:36 -0700196#endif // ANDROID_UI_OPENGL_RENDERER_H