blob: 1e031b8748171c93c07dc6ca9392910b39637791 [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 Guyf6a11b82010-06-23 17:47:49 -070056///////////////////////////////////////////////////////////////////////////////
57// Renderer
58///////////////////////////////////////////////////////////////////////////////
59
Romain Guy5cbbce52010-06-27 22:59:20 -070060/**
61 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
62 * simplified version of Skia's Canvas API.
63 */
Romain Guy85bf02f2010-06-22 13:11:24 -070064class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -070065public:
Romain Guy85bf02f2010-06-22 13:11:24 -070066 OpenGLRenderer();
67 ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -070068
69 void setViewport(int width, int height);
70 void prepare();
Romain Guy08ae3172010-06-21 19:35:50 -070071
Romain Guybb9524b2010-06-22 18:56:38 -070072 int getSaveCount() const;
73 int save(int flags);
74 void restore();
75 void restoreToCount(int saveCount);
76
Romain Guybd6b79b2010-06-26 00:13:53 -070077 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags);
78 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags);
79
Romain Guyf6a11b82010-06-23 17:47:49 -070080 void translate(float dx, float dy);
81 void rotate(float degrees);
82 void scale(float sx, float sy);
83
84 void setMatrix(SkMatrix* matrix);
85 void getMatrix(SkMatrix* matrix);
86 void concatMatrix(SkMatrix* matrix);
87
Romain Guy9d5316e2010-06-24 19:30:36 -070088 const Rect& getClipBounds();
Romain Guyc7d53492010-06-25 13:41:57 -070089 bool quickReject(float left, float top, float right, float bottom);
Romain Guybb9524b2010-06-22 18:56:38 -070090 bool clipRect(float left, float top, float right, float bottom);
91
Romain Guy85bf02f2010-06-22 13:11:24 -070092 void drawColor(int color, SkXfermode::Mode mode);
Romain Guybd6b79b2010-06-26 00:13:53 -070093 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -070094
Romain Guy85bf02f2010-06-22 13:11:24 -070095private:
Romain Guy5cbbce52010-06-27 22:59:20 -070096 /**
97 * Saves the current state of the renderer as a new snapshot.
98 * The new snapshot is saved in mSnapshot and the previous snapshot
99 * is linked from mSnapshot->previous.
100 *
101 * @return The new save count. This value can be passed to #restoreToCount()
102 */
Romain Guybb9524b2010-06-22 18:56:38 -0700103 int saveSnapshot();
Romain Guy5cbbce52010-06-27 22:59:20 -0700104
105 /**
106 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
107 *
108 * @return True if the clip should be also reapplied by calling
109 * #setScissorFromClip().
110 */
Romain Guybb9524b2010-06-22 18:56:38 -0700111 bool restoreSnapshot();
112
Romain Guy5cbbce52010-06-27 22:59:20 -0700113 /**
114 * Sets the clipping rectangle using glScissor. The clip is defined by
115 * the current snapshot's clipRect member.
116 */
Romain Guybb9524b2010-06-22 18:56:38 -0700117 void setScissorFromClip();
118
Romain Guy5cbbce52010-06-27 22:59:20 -0700119 /**
120 * Draws a colored rectangle with the specified color. The specified coordinates
121 * are transformed by the current snapshot's transform matrix.
122 *
123 * @param left The left coordinate of the rectangle
124 * @param top The top coordinate of the rectangle
125 * @param right The right coordinate of the rectangle
126 * @param bottom The bottom coordinate of the rectangle
127 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
128 */
Romain Guyc7d53492010-06-25 13:41:57 -0700129 void drawColorRect(float left, float top, float right, float bottom, int color);
Romain Guy5cbbce52010-06-27 22:59:20 -0700130
131 /**
132 * Draws a textured rectangle with the specified texture. The specified coordinates
133 * are transformed by the current snapshot's transform matrix.
134 *
135 * @param left The left coordinate of the rectangle
136 * @param top The top coordinate of the rectangle
137 * @param right The right coordinate of the rectangle
138 * @param bottom The bottom coordinate of the rectangle
139 * @param texture The texture name to map onto the rectangle
140 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
141 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700142 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
143 float alpha);
Romain Guyc7d53492010-06-25 13:41:57 -0700144
Romain Guybb9524b2010-06-22 18:56:38 -0700145 // Dimensions of the drawing surface
146 int mWidth, mHeight;
147
Romain Guy85bf02f2010-06-22 13:11:24 -0700148 // Matrix used for ortho projection in shaders
149 float mOrthoMatrix[16];
Romain Guybb9524b2010-06-22 18:56:38 -0700150
Romain Guyc7d53492010-06-25 13:41:57 -0700151 // Model-view matrix used to position/size objects
152 mat4 mModelView;
153
Romain Guybb9524b2010-06-22 18:56:38 -0700154 // Number of saved states
155 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700156 // Base state
157 Snapshot mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -0700158 // Current state
159 sp<Snapshot> mSnapshot;
Romain Guy9d5316e2010-06-24 19:30:36 -0700160
161 // Shaders
162 sp<DrawColorProgram> mDrawColorShader;
Romain Guybd6b79b2010-06-26 00:13:53 -0700163 sp<DrawTextureProgram> mDrawTextureShader;
Romain Guybb9524b2010-06-22 18:56:38 -0700164}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -0700165
Romain Guy9d5316e2010-06-24 19:30:36 -0700166}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -0700167}; // namespace android
168
Romain Guy9d5316e2010-06-24 19:30:36 -0700169#endif // ANDROID_UI_OPENGL_RENDERER_H