blob: a698e7979d93f7a3b6fb1d5c5a9ff5f106a14486 [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 Guyce0537b2010-06-29 21:05:21 -070023#include <SkBitmap.h>
Romain Guyf6a11b82010-06-23 17:47:49 -070024#include <SkMatrix.h>
Romain Guyce0537b2010-06-29 21:05:21 -070025#include <SkPaint.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070026#include <SkXfermode.h>
Romain Guye4d01122010-06-16 18:44:05 -070027
Romain Guybb9524b2010-06-22 18:56:38 -070028#include <utils/RefBase.h>
29
Romain Guyf6a11b82010-06-23 17:47:49 -070030#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070031#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070032#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070033#include "Snapshot.h"
Romain Guyce0537b2010-06-29 21:05:21 -070034#include "Texture.h"
35#include "TextureCache.h"
Romain Guybb9524b2010-06-22 18:56:38 -070036
Romain Guye4d01122010-06-16 18:44:05 -070037namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070038namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070039
Romain Guyf6a11b82010-06-23 17:47:49 -070040///////////////////////////////////////////////////////////////////////////////
41// Support
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy5cbbce52010-06-27 22:59:20 -070044/**
45 * Simple structure to describe a vertex with a position.
46 * This is used to draw filled rectangles without a texture.
47 */
Romain Guyc7d53492010-06-25 13:41:57 -070048struct SimpleVertex {
Romain Guy7ae7ac42010-06-25 13:46:18 -070049 float position[2];
Romain Guyc7d53492010-06-25 13:41:57 -070050}; // struct SimpleVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070051
Romain Guy5cbbce52010-06-27 22:59:20 -070052/**
53 * Simple structure to describe a vertex with a position and a texture.
54 */
Romain Guybd6b79b2010-06-26 00:13:53 -070055struct TextureVertex {
56 float position[2];
57 float texture[2];
58}; // struct TextureVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070059
Romain Guy026c5e162010-06-28 17:12:22 -070060/**
61 * Structure mapping Skia xfermodes to OpenGL blending factors.
62 */
63struct Blender {
64 SkXfermode::Mode mode;
65 GLenum src;
66 GLenum dst;
67}; // struct Blender
68
Romain Guyf6a11b82010-06-23 17:47:49 -070069///////////////////////////////////////////////////////////////////////////////
70// Renderer
71///////////////////////////////////////////////////////////////////////////////
72
Romain Guy5cbbce52010-06-27 22:59:20 -070073/**
74 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
75 * simplified version of Skia's Canvas API.
76 */
Romain Guy85bf02f2010-06-22 13:11:24 -070077class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070078public:
Romain Guy85bf02f2010-06-22 13:11:24 -070079 OpenGLRenderer();
80 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070081
82 void setViewport(int width, int height);
83 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070084
Romain Guybb9524b2010-06-22 18:56:38 -070085 int getSaveCount() const;
86 int save(int flags);
87 void restore();
88 void restoreToCount(int saveCount);
89
Romain Guybd6b79b2010-06-26 00:13:53 -070090 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
91 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
92
Romain Guyf6a11b82010-06-23 17:47:49 -070093 void translate(float dx, float dy);
94 void rotate(float degrees);
95 void scale(float sx, float sy);
96
97 void setMatrix(SkMatrix* matrix);
98 void getMatrix(SkMatrix* matrix);
99 void concatMatrix(SkMatrix* matrix);
100
Romain Guy9d5316e2010-06-24 19:30:36 -0700101 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -0700102 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -0700103 bool clipRect(float left, float top, float right, float bottom);
104
Romain Guyc1396e92010-06-30 17:56:19 -0700105 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700106 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
107 float dstLeft, float dstTop, float dstRight, float dstBottom,
108 const SkMatrix* matrix, const SkPaint* paint);
Romain Guy85bf02f2010-06-22 13:11:24 -0700109 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700110 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700111
Romain Guy85bf02f2010-06-22 13:11:24 -0700112private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700113 /**
114 * Saves the current state of the renderer as a new snapshot.
115 * The new snapshot is saved in mSnapshot and the previous snapshot
116 * is linked from mSnapshot->previous.
117 *
118 * @return The new save count. This value can be passed to #restoreToCount()
119 */
Romain Guybb9524b2010-06-22 18:56:38 -0700120 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700121
122 /**
123 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
124 *
125 * @return True if the clip should be also reapplied by calling
126 * #setScissorFromClip().
127 */
Romain Guybb9524b2010-06-22 18:56:38 -0700128 bool restoreSnapshot();
129
Romain Guy5cbbce52010-06-27 22:59:20 -0700130 /**
131 * Sets the clipping rectangle using glScissor. The clip is defined by
132 * the current snapshot's clipRect member.
133 */
Romain Guybb9524b2010-06-22 18:56:38 -0700134 void setScissorFromClip();
135
Romain Guy5cbbce52010-06-27 22:59:20 -0700136 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700137 * Compose the layer defined in the current snapshot with the layer
138 * defined by the previous snapshot.
139 *
140 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
141 *
142 * @param curent The current snapshot containing the layer to compose
143 * @param previous The previous snapshot to compose the current layer with
144 */
145 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
146
147 /**
148 * Creates a new layer stored in the specified snapshot.
149 *
150 * @param snapshot The snapshot associated with the new layer
151 * @param left The left coordinate of the layer
152 * @param top The top coordinate of the layer
153 * @param right The right coordinate of the layer
154 * @param bottom The bottom coordinate of the layer
155 * @param alpha The translucency of the layer
156 * @param mode The blending mode of the layer
157 * @param flags The layer save flags
158 *
159 * @return True if the layer was successfully created, false otherwise
160 */
161 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
162 int alpha, SkXfermode::Mode mode, int flags);
163
164 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700165 * Draws a colored rectangle with the specified color. The specified coordinates
166 * are transformed by the current snapshot's transform matrix.
167 *
168 * @param left The left coordinate of the rectangle
169 * @param top The top coordinate of the rectangle
170 * @param right The right coordinate of the rectangle
171 * @param bottom The bottom coordinate of the rectangle
172 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700173 * @param mode The Skia xfermode to use
Romain Guy5cbbce52010-06-27 22:59:20 -0700174 */
Romain Guy026c5e162010-06-28 17:12:22 -0700175 void drawColorRect(float left, float top, float right, float bottom,
176 int color, SkXfermode::Mode mode);
Romain Guy5cbbce52010-06-27 22:59:20 -0700177
178 /**
179 * Draws a textured rectangle with the specified texture. The specified coordinates
180 * are transformed by the current snapshot's transform matrix.
181 *
182 * @param left The left coordinate of the rectangle
183 * @param top The top coordinate of the rectangle
184 * @param right The right coordinate of the rectangle
185 * @param bottom The bottom coordinate of the rectangle
186 * @param texture The texture name to map onto the rectangle
187 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
Romain Guyd55a8612010-06-28 17:42:46 -0700188 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700189 * @param blend True if the texture contains an alpha channel
Romain Guyd55a8612010-06-28 17:42:46 -0700190 * @param isPremultiplied Indicates whether the texture has premultiplied alpha
Romain Guy5cbbce52010-06-27 22:59:20 -0700191 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700192 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guyc1396e92010-06-30 17:56:19 -0700193 float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied = false);
Romain Guyc7d53492010-06-25 13:41:57 -0700194
Romain Guy026c5e162010-06-28 17:12:22 -0700195 /**
196 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
197 * back to default is achieved by calling:
198 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700199 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700200 *
201 * @param u1 The left coordinate of the texture
202 * @param v1 The bottom coordinate of the texture
203 * @param u2 The right coordinate of the texture
204 * @param v2 The top coordinate of the texture
205 */
206 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
207
Romain Guy8ba548f2010-06-30 19:21:21 -0700208 /**
209 * Gets the alpha and xfermode out of a paint object. If the paint is null
210 * alpha will be 255 and the xfermode will be SRC_OVER.
211 *
212 * @param paint The paint to extract values from
213 * @param alpha Where to store the resulting alpha
214 * @param mode Where to store the resulting xfermode
215 */
216 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
217
Romain Guybb9524b2010-06-22 18:56:38 -0700218 // Dimensions of the drawing surface
219 int mWidth, mHeight;
220
Romain Guy85bf02f2010-06-22 13:11:24 -0700221 // Matrix used for ortho projection in shaders
222 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700223
Romain Guyc7d53492010-06-25 13:41:57 -0700224 // Model-view matrix used to position/size objects
225 mat4 mModelView;
226
Romain Guybb9524b2010-06-22 18:56:38 -0700227 // Number of saved states
228 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700229 // Base state
230 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700231 // Current state
232 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700233
234 // Shaders
235 sp<DrawColorProgram> mDrawColorShader;
Romain Guybd6b79b2010-06-26 00:13:53 -0700236 sp<DrawTextureProgram> mDrawTextureShader;
Romain Guy026c5e162010-06-28 17:12:22 -0700237
238 // Used to draw textured quads
239 TextureVertex mDrawTextureVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -0700240
241 // Used to cache all drawBitmap textures
242 TextureCache mTextureCache;
Romain Guybb9524b2010-06-22 18:56:38 -0700243}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700244
Romain Guy9d5316e2010-06-24 19:30:36 -0700245}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700246}; // namespace android
247
Romain Guy9d5316e2010-06-24 19:30:36 -0700248#endif // ANDROID_UI_OPENGL_RENDERER_H