DisplayListRenderer.cpp revision 4aa90573bbf86db0d33a3a790c5dbd0d93b95cfe
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 "DisplayListRenderer.h"
20
21namespace android {
22namespace uirenderer {
23
24///////////////////////////////////////////////////////////////////////////////
25// Base structure
26///////////////////////////////////////////////////////////////////////////////
27
28DisplayListRenderer::DisplayListRenderer():
29        mHeap(HEAP_BLOCK_SIZE), mWriter(MIN_WRITER_SIZE) {
30    mBitmapIndex = mMatrixIndex = mPaintIndex = 1;
31    mPathHeap = NULL;
32}
33
34DisplayListRenderer::~DisplayListRenderer() {
35    reset();
36}
37
38void DisplayListRenderer::reset() {
39    if (mPathHeap) {
40        mPathHeap->unref();
41        mPathHeap = NULL;
42    }
43
44    mBitmaps.reset();
45    mMatrices.reset();
46    mPaints.reset();
47
48    mWriter.reset();
49    mHeap.reset();
50
51    mRCRecorder.reset();
52    mTFRecorder.reset();
53}
54
55///////////////////////////////////////////////////////////////////////////////
56// Operations
57///////////////////////////////////////////////////////////////////////////////
58
59void DisplayListRenderer::acquireContext() {
60    addOp(AcquireContext);
61    OpenGLRenderer::acquireContext();
62}
63
64void DisplayListRenderer::releaseContext() {
65    addOp(ReleaseContext);
66    OpenGLRenderer::releaseContext();
67}
68
69int DisplayListRenderer::save(int flags) {
70    addOp(Save);
71    addInt(flags);
72    return OpenGLRenderer::save(flags);
73}
74
75void DisplayListRenderer::restore() {
76    addOp(Restore);
77    OpenGLRenderer::restore();
78}
79
80void DisplayListRenderer::restoreToCount(int saveCount) {
81    addOp(RestoreToCount);
82    addInt(saveCount);
83    OpenGLRenderer::restoreToCount(saveCount);
84}
85
86int DisplayListRenderer::saveLayer(float left, float top, float right, float bottom,
87        const SkPaint* p, int flags) {
88    addOp(SaveLayer);
89    addBounds(left, top, right, bottom);
90    addPaint(p);
91    addInt(flags);
92    return OpenGLRenderer::saveLayer(left, top, right, bottom, p, flags);
93}
94
95void DisplayListRenderer::translate(float dx, float dy) {
96    addOp(Translate);
97    addPoint(dx, dy);
98    OpenGLRenderer::translate(dx, dy);
99}
100
101void DisplayListRenderer::rotate(float degrees) {
102    addOp(Rotate);
103    addFloat(degrees);
104    OpenGLRenderer::rotate(degrees);
105}
106
107void DisplayListRenderer::scale(float sx, float sy) {
108    addOp(Scale);
109    addPoint(sx, sy);
110    OpenGLRenderer::scale(sx, sy);
111}
112
113void DisplayListRenderer::setMatrix(SkMatrix* matrix) {
114    addOp(SetMatrix);
115    addMatrix(matrix);
116    OpenGLRenderer::setMatrix(matrix);
117}
118
119void DisplayListRenderer::concatMatrix(SkMatrix* matrix) {
120    addOp(ConcatMatrix);
121    addMatrix(matrix);
122    OpenGLRenderer::concatMatrix(matrix);
123}
124
125bool DisplayListRenderer::clipRect(float left, float top, float right, float bottom,
126        SkRegion::Op op) {
127    addOp(ClipRect);
128    addBounds(left, top, right, bottom);
129    addInt(op);
130    return OpenGLRenderer::clipRect(left, top, right, bottom, op);
131}
132
133void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float left, float top,
134        const SkPaint* paint) {
135    addOp(DrawBitmap);
136    addBitmap(bitmap);
137    addPoint(left, top);
138    addPaint(paint);
139    OpenGLRenderer::drawBitmap(bitmap, left, top, paint);
140}
141
142void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix,
143        const SkPaint* paint) {
144    addOp(DrawBitmapMatrix);
145    addBitmap(bitmap);
146    addMatrix(matrix);
147    addPaint(paint);
148    OpenGLRenderer::drawBitmap(bitmap, matrix, paint);
149}
150
151void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
152        float srcRight, float srcBottom, float dstLeft, float dstTop,
153        float dstRight, float dstBottom, const SkPaint* paint) {
154    addOp(DrawBitmapRect);
155    addBitmap(bitmap);
156    addBounds(srcLeft, srcTop, srcRight, srcBottom);
157    addBounds(dstLeft, dstTop, dstRight, dstBottom);
158    addPaint(paint);
159    OpenGLRenderer::drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom,
160            dstLeft, dstTop, dstRight, dstBottom, paint);
161}
162
163void DisplayListRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
164        uint32_t width, uint32_t height, float left, float top, float right, float bottom,
165        const SkPaint* paint) {
166    addOp(DrawPatch);
167    addBitmap(bitmap);
168    addInts(xDivs, width);
169    addInts(yDivs, height);
170    addBounds(left, top, right, bottom);
171    addPaint(paint);
172    OpenGLRenderer::drawPatch(bitmap, xDivs, yDivs, width, height,
173            left, top, right, bottom, paint);
174}
175
176void DisplayListRenderer::drawColor(int color, SkXfermode::Mode mode) {
177    addOp(DrawColor);
178    addInt(color);
179    addInt(mode);
180    OpenGLRenderer::drawColor(color, mode);
181}
182
183void DisplayListRenderer::drawRect(float left, float top, float right, float bottom,
184        const SkPaint* paint) {
185    addOp(DrawRect);
186    addBounds(left, top, right, bottom);
187    addPaint(paint);
188    OpenGLRenderer::drawRect(left, top, right, bottom, paint);
189}
190
191void DisplayListRenderer::drawPath(SkPath* path, SkPaint* paint) {
192    addOp(DrawPath);
193    addPath(path);
194    addPaint(paint);
195    OpenGLRenderer::drawPath(path, paint);
196}
197
198void DisplayListRenderer::drawLines(float* points, int count, const SkPaint* paint) {
199    addOp(DrawLines);
200    addFloats(points, count);
201    addPaint(paint);
202    OpenGLRenderer::drawLines(points, count, paint);
203}
204
205void DisplayListRenderer::drawText(const char* text, int bytesCount, int count,
206        float x, float y, SkPaint* paint) {
207    addOp(DrawText);
208    addText(text, bytesCount);
209    addInt(count);
210    addPoint(x, y);
211    addPaint(paint);
212    OpenGLRenderer::drawText(text, bytesCount, count, x, y, paint);
213}
214
215void DisplayListRenderer::resetShader() {
216    addOp(ResetShader);
217    OpenGLRenderer::resetShader();
218}
219
220void DisplayListRenderer::setupShader(SkiaShader* shader) {
221    // TODO: Implement
222    OpenGLRenderer::setupShader(shader);
223}
224
225void DisplayListRenderer::resetColorFilter() {
226    addOp(ResetColorFilter);
227    OpenGLRenderer::resetColorFilter();
228}
229
230void DisplayListRenderer::setupColorFilter(SkiaColorFilter* filter) {
231    // TODO: Implement
232    OpenGLRenderer::setupColorFilter(filter);
233}
234
235void DisplayListRenderer::resetShadow() {
236    addOp(ResetShadow);
237    OpenGLRenderer::resetShadow();
238}
239
240void DisplayListRenderer::setupShadow(float radius, float dx, float dy, int color) {
241    addOp(SetupShadow);
242    addFloat(radius);
243    addPoint(dx, dy);
244    addInt(color);
245    OpenGLRenderer::setupShadow(radius, dx, dy, color);
246}
247
248///////////////////////////////////////////////////////////////////////////////
249// Recording management
250///////////////////////////////////////////////////////////////////////////////
251
252int DisplayListRenderer::find(SkTDArray<const SkFlatPaint*>& paints, const SkPaint* paint) {
253    if (paint == NULL) {
254        return 0;
255    }
256
257    SkFlatPaint* flat = SkFlatPaint::Flatten(&mHeap, *paint, mPaintIndex,
258            &mRCRecorder, &mTFRecorder);
259    int index = SkTSearch<SkFlatData>((const SkFlatData**) paints.begin(),
260            paints.count(), (SkFlatData*) flat, sizeof(flat), &SkFlatData::Compare);
261    if (index >= 0) {
262        (void) mHeap.unalloc(flat);
263        return paints[index]->index();
264    }
265
266    index = ~index;
267    *paints.insert(index) = flat;
268    return mPaintIndex++;
269}
270
271int DisplayListRenderer::find(SkTDArray<const SkFlatMatrix*>& matrices, const SkMatrix* matrix) {
272    if (matrix == NULL) {
273        return 0;
274    }
275
276    SkFlatMatrix* flat = SkFlatMatrix::Flatten(&mHeap, *matrix, mMatrixIndex);
277    int index = SkTSearch<SkFlatData>((const SkFlatData**) matrices.begin(),
278            matrices.count(), (SkFlatData*) flat, sizeof(flat), &SkFlatData::Compare);
279    if (index >= 0) {
280        (void) mHeap.unalloc(flat);
281        return matrices[index]->index();
282    }
283    index = ~index;
284    *matrices.insert(index) = flat;
285    return mMatrixIndex++;
286}
287
288int DisplayListRenderer::find(SkTDArray<const SkFlatBitmap*>& bitmaps, const SkBitmap& bitmap) {
289    SkFlatBitmap* flat = SkFlatBitmap::Flatten(&mHeap, bitmap, mBitmapIndex, &mRCRecorder);
290    int index = SkTSearch<SkFlatData>((const SkFlatData**) bitmaps.begin(),
291            bitmaps.count(), (SkFlatData*) flat, sizeof(flat), &SkFlatData::Compare);
292    if (index >= 0) {
293        (void) mHeap.unalloc(flat);
294        return bitmaps[index]->index();
295    }
296    index = ~index;
297    *bitmaps.insert(index) = flat;
298    return mBitmapIndex++;
299}
300
301}; // namespace uirenderer
302}; // namespace android
303