blob: e19795e79a20e8146d2e017036aa4deead0c996f [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 Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/Log.h>
25
26#include <GLES2/gl2.h>
27#include <GLES2/gl2ext.h>
28
Romain Guy85bf02f2010-06-22 13:11:24 -070029#include <SkXfermode.h>
30
31#include "OpenGLRenderer.h"
32#include "Matrix.h"
Romain Guye4d01122010-06-16 18:44:05 -070033
34namespace android {
35
Romain Guyf6a11b82010-06-23 17:47:49 -070036///////////////////////////////////////////////////////////////////////////////
37// Constructors/destructor
38///////////////////////////////////////////////////////////////////////////////
39
Romain Guy85bf02f2010-06-22 13:11:24 -070040OpenGLRenderer::OpenGLRenderer() {
41 LOGD("Create OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -070042}
43
Romain Guy85bf02f2010-06-22 13:11:24 -070044OpenGLRenderer::~OpenGLRenderer() {
45 LOGD("Destroy OpenGLRenderer");
Romain Guye4d01122010-06-16 18:44:05 -070046}
47
Romain Guyf6a11b82010-06-23 17:47:49 -070048///////////////////////////////////////////////////////////////////////////////
49// Setup
50///////////////////////////////////////////////////////////////////////////////
51
Romain Guy85bf02f2010-06-22 13:11:24 -070052void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy08ae3172010-06-21 19:35:50 -070053 glViewport(0, 0, width, height);
54
55 mat4 ortho;
56 ortho.loadOrtho(0, width, height, 0, 0, 1);
57 ortho.copyTo(mOrthoMatrix);
Romain Guybb9524b2010-06-22 18:56:38 -070058
59 mWidth = width;
60 mHeight = height;
Romain Guye4d01122010-06-16 18:44:05 -070061}
62
Romain Guy85bf02f2010-06-22 13:11:24 -070063void OpenGLRenderer::prepare() {
Romain Guyf6a11b82010-06-23 17:47:49 -070064 mSnapshot = &mFirstSnapshot;
65 mSaveCount = 0;
66
Romain Guy08ae3172010-06-21 19:35:50 -070067 glDisable(GL_SCISSOR_TEST);
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy08ae3172010-06-21 19:35:50 -070069 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
70 glClear(GL_COLOR_BUFFER_BIT);
Romain Guybb9524b2010-06-22 18:56:38 -070071
Romain Guy08ae3172010-06-21 19:35:50 -070072 glEnable(GL_SCISSOR_TEST);
Romain Guyf6a11b82010-06-23 17:47:49 -070073
Romain Guybb9524b2010-06-22 18:56:38 -070074 mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
75}
76
Romain Guyf6a11b82010-06-23 17:47:49 -070077///////////////////////////////////////////////////////////////////////////////
78// State management
79///////////////////////////////////////////////////////////////////////////////
80
Romain Guybb9524b2010-06-22 18:56:38 -070081int OpenGLRenderer::getSaveCount() const {
82 return mSaveCount;
83}
84
85int OpenGLRenderer::save(int flags) {
86 return saveSnapshot();
87}
88
89void OpenGLRenderer::restore() {
90 if (mSaveCount == 0) return;
91
92 if (restoreSnapshot()) {
93 setScissorFromClip();
94 }
95}
96
97void OpenGLRenderer::restoreToCount(int saveCount) {
98 if (saveCount <= 0 || saveCount > mSaveCount) return;
99
100 bool restoreClip = false;
101
102 while (mSaveCount != saveCount - 1) {
103 restoreClip |= restoreSnapshot();
104 }
105
106 if (restoreClip) {
107 setScissorFromClip();
108 }
109}
110
111int OpenGLRenderer::saveSnapshot() {
112 mSnapshot = new Snapshot(mSnapshot);
Romain Guyf6a11b82010-06-23 17:47:49 -0700113 return ++mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700114}
115
116bool OpenGLRenderer::restoreSnapshot() {
117 // TODO: handle local transformations
118 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
119
120 mSaveCount--;
Romain Guyf6a11b82010-06-23 17:47:49 -0700121
122 // Do not merge these two lines!
123 sp<Snapshot> previous = mSnapshot->previous;
124 mSnapshot = previous;
Romain Guybb9524b2010-06-22 18:56:38 -0700125
126 return restoreClip;
127}
128
Romain Guyf6a11b82010-06-23 17:47:49 -0700129///////////////////////////////////////////////////////////////////////////////
130// Transforms
131///////////////////////////////////////////////////////////////////////////////
132
133void OpenGLRenderer::translate(float dx, float dy) {
134 mSnapshot->transform.translate(dx, dy, 0.0f);
135}
136
137void OpenGLRenderer::rotate(float degrees) {
138 mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
139}
140
141void OpenGLRenderer::scale(float sx, float sy) {
142 mSnapshot->transform.scale(sx, sy, 1.0f);
143}
144
145void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
146 mSnapshot->transform.load(*matrix);
147}
148
149void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
150 mSnapshot->transform.copyTo(*matrix);
151}
152
153void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
154 mat4 m(*matrix);
155 mSnapshot->transform.multiply(m);
156}
157
158///////////////////////////////////////////////////////////////////////////////
159// Clipping
160///////////////////////////////////////////////////////////////////////////////
161
Romain Guybb9524b2010-06-22 18:56:38 -0700162void OpenGLRenderer::setScissorFromClip() {
Romain Guyf6a11b82010-06-23 17:47:49 -0700163 Rect* clip = &(mSnapshot->clipRect);
164 glScissor(clip->left, clip->top, clip->getWidth(), clip->getHeight());
Romain Guybb9524b2010-06-22 18:56:38 -0700165}
166
167bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
168 // TODO: take local translate transform into account
169 bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
170 if (clipped) {
171 mSnapshot->flags |= Snapshot::kFlagClipSet;
172 setScissorFromClip();
173 }
174 return clipped;
Romain Guye4d01122010-06-16 18:44:05 -0700175}
176
Romain Guyf6a11b82010-06-23 17:47:49 -0700177///////////////////////////////////////////////////////////////////////////////
178// Drawing
179///////////////////////////////////////////////////////////////////////////////
180
Romain Guy85bf02f2010-06-22 13:11:24 -0700181void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
182 LOGD("Drawing color");
183}
184
Romain Guye4d01122010-06-16 18:44:05 -0700185}; // namespace android