blob: 9da4b8e746a2a1b0e658f668ef5831da3d6850ae [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>
Romain Guydeba7852010-07-07 17:54:48 -070029#include <utils/ResourceTypes.h>
Romain Guybb9524b2010-06-22 18:56:38 -070030
Romain Guyf6a11b82010-06-23 17:47:49 -070031#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070032#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070033#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070034#include "Snapshot.h"
Romain Guyce0537b2010-06-29 21:05:21 -070035#include "Texture.h"
Romain Guydda570202010-07-06 11:39:32 -070036#include "Layer.h"
Romain Guyce0537b2010-06-29 21:05:21 -070037#include "TextureCache.h"
Romain Guydda570202010-07-06 11:39:32 -070038#include "LayerCache.h"
Romain Guybb9524b2010-06-22 18:56:38 -070039
Romain Guye4d01122010-06-16 18:44:05 -070040namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070042
Romain Guyf6a11b82010-06-23 17:47:49 -070043///////////////////////////////////////////////////////////////////////////////
44// Support
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy5cbbce52010-06-27 22:59:20 -070047/**
48 * Simple structure to describe a vertex with a position.
49 * This is used to draw filled rectangles without a texture.
50 */
Romain Guyc7d53492010-06-25 13:41:57 -070051struct SimpleVertex {
Romain Guy7ae7ac42010-06-25 13:46:18 -070052 float position[2];
Romain Guyc7d53492010-06-25 13:41:57 -070053}; // struct SimpleVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070054
Romain Guy5cbbce52010-06-27 22:59:20 -070055/**
56 * Simple structure to describe a vertex with a position and a texture.
57 */
Romain Guybd6b79b2010-06-26 00:13:53 -070058struct TextureVertex {
59 float position[2];
60 float texture[2];
61}; // struct TextureVertex
Romain Guy9d5316e2010-06-24 19:30:36 -070062
Romain Guy026c5e162010-06-28 17:12:22 -070063/**
64 * Structure mapping Skia xfermodes to OpenGL blending factors.
65 */
66struct Blender {
67 SkXfermode::Mode mode;
68 GLenum src;
69 GLenum dst;
70}; // struct Blender
71
Romain Guyf6a11b82010-06-23 17:47:49 -070072///////////////////////////////////////////////////////////////////////////////
73// Renderer
74///////////////////////////////////////////////////////////////////////////////
75
Romain Guy5cbbce52010-06-27 22:59:20 -070076/**
77 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
78 * simplified version of Skia's Canvas API.
79 */
Romain Guy85bf02f2010-06-22 13:11:24 -070080class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070081public:
Romain Guy85bf02f2010-06-22 13:11:24 -070082 OpenGLRenderer();
83 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070084
85 void setViewport(int width, int height);
86 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070087
Romain Guybb9524b2010-06-22 18:56:38 -070088 int getSaveCount() const;
89 int save(int flags);
90 void restore();
91 void restoreToCount(int saveCount);
92
Romain Guybd6b79b2010-06-26 00:13:53 -070093 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
94 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
95
Romain Guyf6a11b82010-06-23 17:47:49 -070096 void translate(float dx, float dy);
97 void rotate(float degrees);
98 void scale(float sx, float sy);
99
100 void setMatrix(SkMatrix* matrix);
101 void getMatrix(SkMatrix* matrix);
102 void concatMatrix(SkMatrix* matrix);
103
Romain Guy9d5316e2010-06-24 19:30:36 -0700104 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -0700105 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -0700106 bool clipRect(float left, float top, float right, float bottom);
107
Romain Guyc1396e92010-06-30 17:56:19 -0700108 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
Romain Guyf86ef572010-07-01 11:05:42 -0700109 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
Romain Guy8ba548f2010-06-30 19:21:21 -0700110 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guyf86ef572010-07-01 11:05:42 -0700111 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
Romain Guydeba7852010-07-07 17:54:48 -0700112 void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
113 float right, float bottom, const SkPaint* paint);
Romain Guy85bf02f2010-06-22 13:11:24 -0700114 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -0700115 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700116
Romain Guy85bf02f2010-06-22 13:11:24 -0700117private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700118 /**
119 * Saves the current state of the renderer as a new snapshot.
120 * The new snapshot is saved in mSnapshot and the previous snapshot
121 * is linked from mSnapshot->previous.
122 *
123 * @return The new save count. This value can be passed to #restoreToCount()
124 */
Romain Guybb9524b2010-06-22 18:56:38 -0700125 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700126
127 /**
128 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
129 *
130 * @return True if the clip should be also reapplied by calling
131 * #setScissorFromClip().
132 */
Romain Guybb9524b2010-06-22 18:56:38 -0700133 bool restoreSnapshot();
134
Romain Guy5cbbce52010-06-27 22:59:20 -0700135 /**
136 * Sets the clipping rectangle using glScissor. The clip is defined by
137 * the current snapshot's clipRect member.
138 */
Romain Guybb9524b2010-06-22 18:56:38 -0700139 void setScissorFromClip();
140
Romain Guy5cbbce52010-06-27 22:59:20 -0700141 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700142 * Compose the layer defined in the current snapshot with the layer
143 * defined by the previous snapshot.
144 *
145 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
146 *
147 * @param curent The current snapshot containing the layer to compose
148 * @param previous The previous snapshot to compose the current layer with
149 */
150 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
151
152 /**
153 * Creates a new layer stored in the specified snapshot.
154 *
155 * @param snapshot The snapshot associated with the new layer
156 * @param left The left coordinate of the layer
157 * @param top The top coordinate of the layer
158 * @param right The right coordinate of the layer
159 * @param bottom The bottom coordinate of the layer
160 * @param alpha The translucency of the layer
161 * @param mode The blending mode of the layer
162 * @param flags The layer save flags
163 *
164 * @return True if the layer was successfully created, false otherwise
165 */
166 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom,
167 int alpha, SkXfermode::Mode mode, int flags);
168
169 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700170 * Draws a colored rectangle with the specified color. The specified coordinates
171 * are transformed by the current snapshot's transform matrix.
172 *
173 * @param left The left coordinate of the rectangle
174 * @param top The top coordinate of the rectangle
175 * @param right The right coordinate of the rectangle
176 * @param bottom The bottom coordinate of the rectangle
177 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700178 * @param mode The Skia xfermode to use
Romain Guy5cbbce52010-06-27 22:59:20 -0700179 */
Romain Guy026c5e162010-06-28 17:12:22 -0700180 void drawColorRect(float left, float top, float right, float bottom,
181 int color, SkXfermode::Mode mode);
Romain Guy5cbbce52010-06-27 22:59:20 -0700182
183 /**
184 * Draws a textured rectangle with the specified texture. The specified coordinates
185 * are transformed by the current snapshot's transform matrix.
186 *
187 * @param left The left coordinate of the rectangle
188 * @param top The top coordinate of the rectangle
189 * @param right The right coordinate of the rectangle
190 * @param bottom The bottom coordinate of the rectangle
191 * @param texture The texture name to map onto the rectangle
192 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
Romain Guyd55a8612010-06-28 17:42:46 -0700193 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700194 * @param blend True if the texture contains an alpha channel
Romain Guyd55a8612010-06-28 17:42:46 -0700195 * @param isPremultiplied Indicates whether the texture has premultiplied alpha
Romain Guy5cbbce52010-06-27 22:59:20 -0700196 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700197 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guyc1396e92010-06-30 17:56:19 -0700198 float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied = false);
Romain Guyc7d53492010-06-25 13:41:57 -0700199
Romain Guy026c5e162010-06-28 17:12:22 -0700200 /**
201 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
202 * back to default is achieved by calling:
203 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700204 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700205 *
206 * @param u1 The left coordinate of the texture
207 * @param v1 The bottom coordinate of the texture
208 * @param u2 The right coordinate of the texture
209 * @param v2 The top coordinate of the texture
210 */
211 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
212
Romain Guy8ba548f2010-06-30 19:21:21 -0700213 /**
214 * Gets the alpha and xfermode out of a paint object. If the paint is null
215 * alpha will be 255 and the xfermode will be SRC_OVER.
216 *
217 * @param paint The paint to extract values from
218 * @param alpha Where to store the resulting alpha
219 * @param mode Where to store the resulting xfermode
220 */
221 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
222
Romain Guybb9524b2010-06-22 18:56:38 -0700223 // Dimensions of the drawing surface
224 int mWidth, mHeight;
225
Romain Guy85bf02f2010-06-22 13:11:24 -0700226 // Matrix used for ortho projection in shaders
227 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700228
Romain Guyc7d53492010-06-25 13:41:57 -0700229 // Model-view matrix used to position/size objects
230 mat4 mModelView;
231
Romain Guybb9524b2010-06-22 18:56:38 -0700232 // Number of saved states
233 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700234 // Base state
235 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700236 // Current state
237 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700238
239 // Shaders
240 sp<DrawColorProgram> mDrawColorShader;
Romain Guybd6b79b2010-06-26 00:13:53 -0700241 sp<DrawTextureProgram> mDrawTextureShader;
Romain Guy026c5e162010-06-28 17:12:22 -0700242
243 // Used to draw textured quads
244 TextureVertex mDrawTextureVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -0700245
246 // Used to cache all drawBitmap textures
247 TextureCache mTextureCache;
Romain Guydda570202010-07-06 11:39:32 -0700248 LayerCache mLayerCache;
Romain Guybb9524b2010-06-22 18:56:38 -0700249}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700250
Romain Guy9d5316e2010-06-24 19:30:36 -0700251}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700252}; // namespace android
253
Romain Guy9d5316e2010-06-24 19:30:36 -0700254#endif // ANDROID_UI_OPENGL_RENDERER_H