OpenGLRenderer.cpp revision f6a11b8a9e25ff9861bbba19251bea84d8a5daf2
1/*
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
17#define LOG_TAG "OpenGLRenderer"
18
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
29#include <SkXfermode.h>
30
31#include "OpenGLRenderer.h"
32#include "Matrix.h"
33
34namespace android {
35
36///////////////////////////////////////////////////////////////////////////////
37// Constructors/destructor
38///////////////////////////////////////////////////////////////////////////////
39
40OpenGLRenderer::OpenGLRenderer() {
41    LOGD("Create OpenGLRenderer");
42}
43
44OpenGLRenderer::~OpenGLRenderer() {
45    LOGD("Destroy OpenGLRenderer");
46}
47
48///////////////////////////////////////////////////////////////////////////////
49// Setup
50///////////////////////////////////////////////////////////////////////////////
51
52void OpenGLRenderer::setViewport(int width, int height) {
53    glViewport(0, 0, width, height);
54
55    mat4 ortho;
56    ortho.loadOrtho(0, width, height, 0, 0, 1);
57    ortho.copyTo(mOrthoMatrix);
58
59    mWidth = width;
60    mHeight = height;
61}
62
63void OpenGLRenderer::prepare() {
64	mSnapshot = &mFirstSnapshot;
65	mSaveCount = 0;
66
67    glDisable(GL_SCISSOR_TEST);
68
69    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
70    glClear(GL_COLOR_BUFFER_BIT);
71
72    glEnable(GL_SCISSOR_TEST);
73
74    mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
75}
76
77///////////////////////////////////////////////////////////////////////////////
78// State management
79///////////////////////////////////////////////////////////////////////////////
80
81int 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);
113	return ++mSaveCount;
114}
115
116bool OpenGLRenderer::restoreSnapshot() {
117	// TODO: handle local transformations
118	bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
119
120	mSaveCount--;
121
122	// Do not merge these two lines!
123	sp<Snapshot> previous = mSnapshot->previous;
124	mSnapshot = previous;
125
126	return restoreClip;
127}
128
129///////////////////////////////////////////////////////////////////////////////
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
162void OpenGLRenderer::setScissorFromClip() {
163	Rect* clip = &(mSnapshot->clipRect);
164	glScissor(clip->left, clip->top, clip->getWidth(), clip->getHeight());
165}
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;
175}
176
177///////////////////////////////////////////////////////////////////////////////
178// Drawing
179///////////////////////////////////////////////////////////////////////////////
180
181void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
182	LOGD("Drawing color");
183}
184
185}; // namespace android
186