blob: a0ad888d5c9587e28b36f478cc2cc82748383992 [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 Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_OPENGL_RENDERER_H
18#define ANDROID_HWUI_OPENGL_RENDERER_H
Romain Guy9d5316e2010-06-24 19:30:36 -070019
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 Guy079ba2c2010-07-16 14:12:24 -070026#include <SkRegion.h>
Romain Guyd27977d2010-07-14 19:18:51 -070027#include <SkShader.h>
Romain Guy85bf02f2010-06-22 13:11:24 -070028#include <SkXfermode.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Chet Haasedaf98e92011-01-10 14:10:36 -080030#include <utils/Functor.h>
Romain Guybb9524b2010-06-22 18:56:38 -070031#include <utils/RefBase.h>
Romain Guyba6be8a2012-04-23 18:22:09 -070032#include <utils/SortedVector.h>
Romain Guy86942302010-09-12 13:02:16 -070033#include <utils/Vector.h>
Romain Guybb9524b2010-06-22 18:56:38 -070034
Romain Guy79537452011-10-12 13:48:51 -070035#include <cutils/compiler.h>
36
Romain Guyc15008e2010-11-10 11:59:15 -080037#include "Debug.h"
Romain Guy51769a62010-07-23 00:28:00 -070038#include "Extensions.h"
Romain Guyf6a11b82010-06-23 17:47:49 -070039#include "Matrix.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070040#include "Program.h"
Romain Guybb9524b2010-06-22 18:56:38 -070041#include "Rect.h"
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include "Snapshot.h"
Romain Guyf7f93552010-07-08 19:17:03 -070043#include "Vertex.h"
Romain Guy06f96e22010-07-30 19:18:16 -070044#include "SkiaShader.h"
Romain Guydb1938e2010-08-02 18:50:22 -070045#include "SkiaColorFilter.h"
Romain Guyfb8b7632010-08-23 21:05:08 -070046#include "Caches.h"
Romain Guybb9524b2010-06-22 18:56:38 -070047
Romain Guye4d01122010-06-16 18:44:05 -070048namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070049namespace uirenderer {
Romain Guye4d01122010-06-16 18:44:05 -070050
Chris Craikc3566d02013-02-04 16:16:33 -080051struct DrawModifiers {
52 SkiaShader* mShader;
53 SkiaColorFilter* mColorFilter;
Chris Craik16ecda52013-03-29 10:59:59 -070054 float mOverrideLayerAlpha;
Chris Craikc3566d02013-02-04 16:16:33 -080055
56 // Drop shadow
57 bool mHasShadow;
58 float mShadowRadius;
59 float mShadowDx;
60 float mShadowDy;
61 int mShadowColor;
62
63 // Draw filters
64 bool mHasDrawFilter;
65 int mPaintFilterClearBits;
66 int mPaintFilterSetBits;
67};
68
Chris Craikff785832013-03-08 13:12:16 -080069enum StateDeferFlags {
70 kStateDeferFlag_Draw = 0x1,
71 kStateDeferFlag_Clip = 0x2
72};
73
Chris Craik527a3aa2013-03-04 10:19:31 -080074enum DrawOpMode {
75 kDrawOpMode_Immediate,
76 kDrawOpMode_Defer,
77 kDrawOpMode_Flush
78};
79
Chris Craikc3566d02013-02-04 16:16:33 -080080struct DeferredDisplayState {
Chris Craik527a3aa2013-03-04 10:19:31 -080081 Rect mBounds; // global op bounds, mapped by mMatrix to be in screen space coordinates, clipped.
Chris Craikc3566d02013-02-04 16:16:33 -080082
83 // the below are set and used by the OpenGLRenderer at record and deferred playback
Chris Craik527a3aa2013-03-04 10:19:31 -080084 bool mClipValid;
Chris Craikc3566d02013-02-04 16:16:33 -080085 Rect mClip;
86 mat4 mMatrix;
Chris Craikc3566d02013-02-04 16:16:33 -080087 DrawModifiers mDrawModifiers;
Chris Craikff785832013-03-08 13:12:16 -080088 float mAlpha;
Chris Craikc3566d02013-02-04 16:16:33 -080089};
90
Romain Guyf6a11b82010-06-23 17:47:49 -070091///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -070092// Renderer
93///////////////////////////////////////////////////////////////////////////////
94
Romain Guy0fe478e2010-11-08 12:08:41 -080095class DisplayList;
Romain Guy257ae352013-03-20 16:31:12 -070096class TextSetupFunctor;
Chris Craik65cd6122012-12-10 17:56:27 -080097class VertexBuffer;
Romain Guyb051e892010-09-28 19:09:36 -070098
Romain Guy5cbbce52010-06-27 22:59:20 -070099/**
100 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
101 * simplified version of Skia's Canvas API.
102 */
Romain Guy85bf02f2010-06-22 13:11:24 -0700103class OpenGLRenderer {
Romain Guye4d01122010-06-16 18:44:05 -0700104public:
Romain Guy79537452011-10-12 13:48:51 -0700105 ANDROID_API OpenGLRenderer();
Romain Guye2d345e2010-09-24 18:39:22 -0700106 virtual ~OpenGLRenderer();
Romain Guye4d01122010-06-16 18:44:05 -0700107
Romain Guy17112ad2012-08-07 11:24:39 -0700108 /**
Romain Guyef359272013-01-31 19:07:29 -0800109 * Sets the name of this renderer. The name is optional and
110 * empty by default. If the pointer is null the name is set
111 * to the empty string.
112 */
113 ANDROID_API void setName(const char* name);
114
115 /**
116 * Returns the name of this renderer as UTF8 string.
117 * The returned pointer is never null.
118 */
119 ANDROID_API const char* getName() const;
120
121 /**
Romain Guy87e2f7572012-09-24 11:37:12 -0700122 * Read externally defined properties to control the behavior
123 * of the renderer.
124 */
125 ANDROID_API void initProperties();
126
127 /**
Romain Guy17112ad2012-08-07 11:24:39 -0700128 * Indicates whether this renderer executes drawing commands immediately.
129 * If this method returns true, the drawing commands will be executed
130 * later.
131 */
Romain Guy49c5fc02012-05-15 11:10:01 -0700132 virtual bool isDeferred();
133
Romain Guy17112ad2012-08-07 11:24:39 -0700134 /**
135 * Sets the dimension of the underlying drawing surface. This method must
136 * be called at least once every time the drawing surface changes size.
137 *
138 * @param width The width in pixels of the underlysing surface
139 * @param height The height in pixels of the underlysing surface
140 */
Romain Guyb051e892010-09-28 19:09:36 -0700141 virtual void setViewport(int width, int height);
Romain Guye2d345e2010-09-24 18:39:22 -0700142
Romain Guy17112ad2012-08-07 11:24:39 -0700143 /**
144 * Prepares the renderer to draw a frame. This method must be invoked
145 * at the beginning of each frame. When this method is invoked, the
146 * entire drawing surface is assumed to be redrawn.
147 *
148 * @param opaque If true, the target surface is considered opaque
149 * and will not be cleared. If false, the target surface
150 * will be cleared
151 */
Romain Guy7c25aab2012-10-18 15:05:02 -0700152 ANDROID_API status_t prepare(bool opaque);
Romain Guye2d345e2010-09-24 18:39:22 -0700153
Romain Guy17112ad2012-08-07 11:24:39 -0700154 /**
155 * Prepares the renderer to draw a frame. This method must be invoked
156 * at the beginning of each frame. Only the specified rectangle of the
157 * frame is assumed to be dirty. A clip will automatically be set to
158 * the specified rectangle.
159 *
160 * @param left The left coordinate of the dirty rectangle
161 * @param top The top coordinate of the dirty rectangle
162 * @param right The right coordinate of the dirty rectangle
163 * @param bottom The bottom coordinate of the dirty rectangle
164 * @param opaque If true, the target surface is considered opaque
165 * and will not be cleared. If false, the target surface
166 * will be cleared in the specified dirty rectangle
167 */
Romain Guy7c25aab2012-10-18 15:05:02 -0700168 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
Romain Guy17112ad2012-08-07 11:24:39 -0700169
170 /**
171 * Indicates the end of a frame. This method must be invoked whenever
172 * the caller is done rendering a frame.
173 */
174 virtual void finish();
Romain Guy6c319ca2011-01-11 14:29:25 -0800175
Romain Guyc89b14b2012-08-08 14:53:48 -0700176 /**
177 * This method must be invoked before handing control over to a draw functor.
178 * See callDrawGLFunction() for instance.
179 *
180 * This command must not be recorded inside display lists.
181 */
182 virtual void interrupt();
183
184 /**
185 * This method must be invoked after getting control back from a draw functor.
186 *
187 * This command must not be recorded inside display lists.
188 */
189 virtual void resume();
190
Romain Guy8f3b8e32012-03-27 16:33:45 -0700191 ANDROID_API status_t invokeFunctors(Rect& dirty);
Romain Guyba6be8a2012-04-23 18:22:09 -0700192 ANDROID_API void detachFunctor(Functor* functor);
193 ANDROID_API void attachFunctor(Functor* functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700194 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty);
Romain Guy08ae3172010-06-21 19:35:50 -0700195
Romain Guy11cb6422012-09-21 00:39:43 -0700196 ANDROID_API void pushLayerUpdate(Layer* layer);
197 ANDROID_API void clearLayerUpdates();
198
Romain Guy79537452011-10-12 13:48:51 -0700199 ANDROID_API int getSaveCount() const;
Romain Guy4aa90572010-09-26 18:40:37 -0700200 virtual int save(int flags);
201 virtual void restore();
202 virtual void restoreToCount(int saveCount);
Romain Guybb9524b2010-06-22 18:56:38 -0700203
Chris Craikff785832013-03-08 13:12:16 -0800204 ANDROID_API int saveLayer(float left, float top, float right, float bottom,
205 SkPaint* paint, int flags) {
206 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
207 if (paint) mode = getXfermode(paint->getXfermode());
208 return saveLayer(left, top, right, bottom, paint ? paint->getAlpha() : 255, mode, flags);
209 }
210 ANDROID_API int saveLayerAlpha(float left, float top, float right, float bottom,
211 int alpha, int flags) {
212 return saveLayer(left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
213 }
Romain Guye2d345e2010-09-24 18:39:22 -0700214 virtual int saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800215 int alpha, SkXfermode::Mode mode, int flags);
Romain Guybd6b79b2010-06-26 00:13:53 -0700216
Chris Craikd90144d2013-03-19 15:03:48 -0700217 int saveLayerDeferred(float left, float top, float right, float bottom,
218 int alpha, SkXfermode::Mode mode, int flags);
219
Romain Guy4aa90572010-09-26 18:40:37 -0700220 virtual void translate(float dx, float dy);
221 virtual void rotate(float degrees);
222 virtual void scale(float sx, float sy);
Romain Guy807daf72011-01-18 11:19:19 -0800223 virtual void skew(float sx, float sy);
Romain Guyf6a11b82010-06-23 17:47:49 -0700224
Chris Craikb98a0162013-02-21 11:30:22 -0800225 bool hasRectToRectTransform();
Romain Guy79537452011-10-12 13:48:51 -0700226 ANDROID_API void getMatrix(SkMatrix* matrix);
Romain Guy4aa90572010-09-26 18:40:37 -0700227 virtual void setMatrix(SkMatrix* matrix);
228 virtual void concatMatrix(SkMatrix* matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -0700229
Romain Guy79537452011-10-12 13:48:51 -0700230 ANDROID_API const Rect& getClipBounds();
231 ANDROID_API bool quickReject(float left, float top, float right, float bottom);
Romain Guy8a4ac612012-07-17 17:32:48 -0700232 bool quickRejectNoScissor(float left, float top, float right, float bottom);
Romain Guy4aa90572010-09-26 18:40:37 -0700233 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
Romain Guy735738c2012-12-03 12:34:51 -0800234 virtual bool clipPath(SkPath* path, SkRegion::Op op);
235 virtual bool clipRegion(SkRegion* region, SkRegion::Op op);
Chet Haasea23eed82012-04-12 15:19:04 -0700236 virtual Rect* getClipRect();
Romain Guybb9524b2010-06-22 18:56:38 -0700237
Chris Craikff785832013-03-08 13:12:16 -0800238 virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t replayFlags);
Chris Craikc3566d02013-02-04 16:16:33 -0800239 virtual void outputDisplayList(DisplayList* displayList);
Chris Craika08f95c2013-03-15 17:24:33 -0700240 virtual status_t drawLayer(Layer* layer, float x, float y);
Chet Haase48659092012-05-31 15:21:51 -0700241 virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
Chris Craik527a3aa2013-03-04 10:19:31 -0800242 status_t drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
243 const Rect& bounds, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700244 virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
245 virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
Romain Guye2d345e2010-09-24 18:39:22 -0700246 float srcRight, float srcBottom, float dstLeft, float dstTop,
Chet Haase5c13d892010-10-08 08:37:55 -0700247 float dstRight, float dstBottom, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700248 virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
249 virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -0800250 float* vertices, int* colors, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700251 virtual status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -0700252 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -0700253 float left, float top, float right, float bottom, SkPaint* paint);
Romain Guybe6f9dc2012-07-16 12:41:17 -0700254 status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
255 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
256 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode);
Chet Haase48659092012-05-31 15:21:51 -0700257 virtual status_t drawColor(int color, SkXfermode::Mode mode);
258 virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint);
259 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
Romain Guy01d58e42011-01-19 21:54:02 -0800260 float rx, float ry, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700261 virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint);
262 virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint);
263 virtual status_t drawArc(float left, float top, float right, float bottom,
Romain Guy8b2f5262011-01-23 16:15:02 -0800264 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700265 virtual status_t drawPath(SkPath* path, SkPaint* paint);
266 virtual status_t drawLines(float* points, int count, SkPaint* paint);
267 virtual status_t drawPoints(float* points, int count, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700268 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -0800269 float hOffset, float vOffset, SkPaint* paint);
Chet Haase48659092012-05-31 15:21:51 -0700270 virtual status_t drawPosText(const char* text, int bytesCount, int count,
Romain Guy325740f2012-02-24 16:48:34 -0800271 const float* positions, SkPaint* paint);
Romain Guyc2525952012-07-27 16:41:22 -0700272 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craik527a3aa2013-03-04 10:19:31 -0800273 const float* positions, SkPaint* paint, float length = -1.0f,
274 DrawOpMode drawOpMode = kDrawOpMode_Immediate);
Romain Guy672433d2013-01-04 19:05:13 -0800275 virtual status_t drawRects(const float* rects, int count, SkPaint* paint);
Romain Guy08ae3172010-06-21 19:35:50 -0700276
Romain Guy4aa90572010-09-26 18:40:37 -0700277 virtual void resetShader();
278 virtual void setupShader(SkiaShader* shader);
Romain Guyd27977d2010-07-14 19:18:51 -0700279
Romain Guy4aa90572010-09-26 18:40:37 -0700280 virtual void resetColorFilter();
281 virtual void setupColorFilter(SkiaColorFilter* filter);
Romain Guydb1938e2010-08-02 18:50:22 -0700282
Romain Guy4aa90572010-09-26 18:40:37 -0700283 virtual void resetShadow();
284 virtual void setupShadow(float radius, float dx, float dy, int color);
Romain Guy1e45aae2010-08-13 19:39:53 -0700285
Romain Guy5ff9df62012-01-23 17:09:05 -0800286 virtual void resetPaintFilter();
287 virtual void setupPaintFilter(int clearBits, int setBits);
288
Chris Craik16ecda52013-03-29 10:59:59 -0700289 // If this value is set to < 1.0, it overrides alpha set on layer (see drawBitmap, drawLayer)
290 void setOverrideLayerAlpha(float alpha) { mDrawModifiers.mOverrideLayerAlpha = alpha; }
291
Chris Craika08f95c2013-03-15 17:24:33 -0700292 SkPaint* filterPaint(SkPaint* paint);
Romain Guy5ff9df62012-01-23 17:09:05 -0800293
Chris Craikff785832013-03-08 13:12:16 -0800294 bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags);
Chris Craik527a3aa2013-03-04 10:19:31 -0800295 void restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore = false);
296 void setFullScreenClip();
Chris Craikc3566d02013-02-04 16:16:33 -0800297
Chris Craikd90144d2013-03-19 15:03:48 -0700298 const DrawModifiers& getDrawModifiers() { return mDrawModifiers; }
299 void setDrawModifiers(const DrawModifiers& drawModifiers) { mDrawModifiers = drawModifiers; }
300
Romain Guy672433d2013-01-04 19:05:13 -0800301 ANDROID_API bool isCurrentTransformSimple() {
302 return mSnapshot->transform->isSimple();
303 }
304
Romain Guy0f667532013-03-01 14:31:04 -0800305 Caches& getCaches() {
306 return mCaches;
307 }
308
Chris Craikff785832013-03-08 13:12:16 -0800309 // simple rect clip
310 bool isCurrentClipSimple() {
311 return mSnapshot->clipRegion->isEmpty();
312 }
313
Romain Guy17112ad2012-08-07 11:24:39 -0700314 /**
Chris Craika08f95c2013-03-15 17:24:33 -0700315 * Scales the alpha on the current snapshot. This alpha value will be modulated
Romain Guy17112ad2012-08-07 11:24:39 -0700316 * with other alpha values when drawing primitives.
317 */
Chris Craika08f95c2013-03-15 17:24:33 -0700318 void scaleAlpha(float alpha) {
319 mSnapshot->alpha *= alpha;
Romain Guy17112ad2012-08-07 11:24:39 -0700320 }
321
322 /**
Romain Guy0f667532013-03-01 14:31:04 -0800323 * Inserts a named event marker in the stream of GL commands.
324 */
325 void eventMark(const char* name) const;
326
327 /**
Romain Guy17112ad2012-08-07 11:24:39 -0700328 * Inserts a named group marker in the stream of GL commands. This marker
329 * can be used by tools to group commands into logical groups. A call to
330 * this method must always be followed later on by a call to endMark().
331 */
Romain Guy13631f32012-01-30 17:41:55 -0800332 void startMark(const char* name) const;
Romain Guy17112ad2012-08-07 11:24:39 -0700333
334 /**
335 * Closes the last group marker opened by startMark().
336 */
Romain Guy13631f32012-01-30 17:41:55 -0800337 void endMark() const;
338
Chet Haased15ebf22012-09-05 11:40:29 -0700339 /**
340 * Gets the alpha and xfermode out of a paint object. If the paint is null
341 * alpha will be 255 and the xfermode will be SRC_OVER. This method does
Chris Craik16ecda52013-03-29 10:59:59 -0700342 * not multiply the paint's alpha by the current snapshot's alpha, and does
343 * not replace the alpha with the overrideLayerAlpha
Chet Haased15ebf22012-09-05 11:40:29 -0700344 *
345 * @param paint The paint to extract values from
346 * @param alpha Where to store the resulting alpha
347 * @param mode Where to store the resulting xfermode
348 */
349 static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800350 *mode = getXfermodeDirect(paint);
351 *alpha = getAlphaDirect(paint);
352 }
Chet Haased15ebf22012-09-05 11:40:29 -0700353
Chris Craik527a3aa2013-03-04 10:19:31 -0800354 static inline SkXfermode::Mode getXfermodeDirect(SkPaint* paint) {
355 if (!paint) return SkXfermode::kSrcOver_Mode;
356 return getXfermode(paint->getXfermode());
357 }
358
359 static inline int getAlphaDirect(SkPaint* paint) {
360 if (!paint) return 255;
361 return paint->getAlpha();
Chet Haased15ebf22012-09-05 11:40:29 -0700362 }
363
Romain Guy624234f2013-03-05 16:43:31 -0800364 /**
365 * Return the best transform to use to rasterize text given a full
366 * transform matrix.
367 */
368 mat4 findBestFontTransform(const mat4& transform) const;
369
Chris Craik527a3aa2013-03-04 10:19:31 -0800370#if DEBUG_MERGE_BEHAVIOR
371 void drawScreenSpaceColorRect(float left, float top, float right, float bottom, int color) {
372 mCaches.setScissorEnabled(false);
373
374 // should only be called outside of other draw ops, so stencil can only be in test state
375 bool stencilWasEnabled = mCaches.stencil.isTestEnabled();
376 mCaches.stencil.disable();
377
378 drawColorRect(left, top, right, bottom, color, SkXfermode::kSrcOver_Mode, true);
379
380 if (stencilWasEnabled) mCaches.stencil.enableTest();
381 }
382#endif
383
Romain Guye2d345e2010-09-24 18:39:22 -0700384protected:
385 /**
Romain Guy35643dd2012-09-18 15:40:58 -0700386 * Computes the projection matrix, initialize the first snapshot
387 * and stores the dimensions of the render target.
388 */
389 void initViewport(int width, int height);
390
391 /**
Romain Guy96885eb2013-03-26 15:05:58 -0700392 * Perform the setup specific to a frame. This method does not
393 * issue any OpenGL commands.
394 */
395 void setupFrameState(float left, float top, float right, float bottom, bool opaque);
396
397 /**
398 * Indicates the start of rendering. This method will setup the
399 * initial OpenGL state (viewport, clearing the buffer, etc.)
400 */
401 status_t startFrame();
402
403 /**
Romain Guy7c25aab2012-10-18 15:05:02 -0700404 * Clears the underlying surface if needed.
405 */
406 virtual status_t clear(float left, float top, float right, float bottom, bool opaque);
407
408 /**
Romain Guy35643dd2012-09-18 15:40:58 -0700409 * Call this method after updating a layer during a drawing pass.
410 */
411 void resumeAfterLayer();
412
413 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800414 * This method is called whenever a stencil buffer is required. Subclasses
415 * should override this method and call attachStencilBufferToLayer() on the
416 * appropriate layer(s).
417 */
418 virtual void ensureStencilBuffer();
419
420 /**
421 * Obtains a stencil render buffer (allocating it if necessary) and
422 * attaches it to the specified layer.
423 */
424 void attachStencilBufferToLayer(Layer* layer);
425
426 /**
Romain Guye2d345e2010-09-24 18:39:22 -0700427 * Compose the layer defined in the current snapshot with the layer
428 * defined by the previous snapshot.
429 *
430 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
431 *
432 * @param curent The current snapshot containing the layer to compose
433 * @param previous The previous snapshot to compose the current layer with
434 */
435 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
Romain Guy694b5192010-07-21 21:33:20 -0700436
Romain Guyada830f2011-01-13 12:13:20 -0800437 /**
Romain Guyf219da52011-01-16 12:54:25 -0800438 * Marks the specified region as dirty at the specified bounds.
Romain Guyada830f2011-01-13 12:13:20 -0800439 */
Romain Guyf219da52011-01-16 12:54:25 -0800440 void dirtyLayerUnchecked(Rect& bounds, Region* region);
Romain Guyada830f2011-01-13 12:13:20 -0800441
442 /**
Romain Guyf219da52011-01-16 12:54:25 -0800443 * Returns the current snapshot.
Romain Guyada830f2011-01-13 12:13:20 -0800444 */
Romain Guy624234f2013-03-05 16:43:31 -0800445 sp<Snapshot> getSnapshot() const {
Romain Guyf219da52011-01-16 12:54:25 -0800446 return mSnapshot;
447 }
Romain Guyada830f2011-01-13 12:13:20 -0800448
Romain Guy42f3a4b2011-01-19 13:42:26 -0800449 /**
450 * Returns the region of the current layer.
451 */
Romain Guy624234f2013-03-05 16:43:31 -0800452 virtual Region* getRegion() const {
Romain Guyf219da52011-01-16 12:54:25 -0800453 return mSnapshot->region;
454 }
455
Romain Guy42f3a4b2011-01-19 13:42:26 -0800456 /**
457 * Indicates whether rendering is currently targeted at a layer.
458 */
Romain Guy624234f2013-03-05 16:43:31 -0800459 virtual bool hasLayer() const {
Romain Guyf219da52011-01-16 12:54:25 -0800460 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
461 }
Romain Guy1bd1bad2011-01-14 20:07:20 -0800462
Romain Guy42f3a4b2011-01-19 13:42:26 -0800463 /**
464 * Returns the name of the FBO this renderer is rendering into.
465 */
Romain Guy624234f2013-03-05 16:43:31 -0800466 virtual GLint getTargetFbo() const {
Romain Guy42f3a4b2011-01-19 13:42:26 -0800467 return 0;
468 }
469
Romain Guy77a81162011-06-14 16:45:55 -0700470 /**
471 * Renders the specified layer as a textured quad.
472 *
473 * @param layer The layer to render
474 * @param rect The bounds of the layer
475 */
476 void drawTextureLayer(Layer* layer, const Rect& rect);
477
Romain Guybe6f9dc2012-07-16 12:41:17 -0700478 /**
479 * Gets the alpha and xfermode out of a paint object. If the paint is null
Chris Craik16ecda52013-03-29 10:59:59 -0700480 * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for both
481 * snapshot alpha, and overrideLayerAlpha
Romain Guybe6f9dc2012-07-16 12:41:17 -0700482 *
483 * @param paint The paint to extract values from
484 * @param alpha Where to store the resulting alpha
485 * @param mode Where to store the resulting xfermode
486 */
Chris Craik16ecda52013-03-29 10:59:59 -0700487 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const;
488
489 /**
490 * Gets the alpha from a layer, accounting for snapshot alpha and overrideLayerAlpha
491 *
492 * @param layer The layer from which the alpha is extracted
493 */
494 inline float getLayerAlpha(Layer* layer) const;
Romain Guybe6f9dc2012-07-16 12:41:17 -0700495
496 /**
Romain Guybe6f9dc2012-07-16 12:41:17 -0700497 * Safely retrieves the mode from the specified xfermode. If the specified
498 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
499 */
500 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
501 SkXfermode::Mode resultMode;
502 if (!SkXfermode::AsMode(mode, &resultMode)) {
503 resultMode = SkXfermode::kSrcOver_Mode;
504 }
505 return resultMode;
506 }
507
Romain Guy11cb6422012-09-21 00:39:43 -0700508 /**
509 * Set to true to suppress error checks at the end of a frame.
510 */
Romain Guy624234f2013-03-05 16:43:31 -0800511 virtual bool suppressErrorChecks() const {
Romain Guy11cb6422012-09-21 00:39:43 -0700512 return false;
513 }
514
Romain Guy85bf02f2010-06-22 13:11:24 -0700515private:
Romain Guy5cbbce52010-06-27 22:59:20 -0700516 /**
Romain Guydcfc8362013-01-03 13:08:57 -0800517 * Discards the content of the framebuffer if supported by the driver.
518 * This method should be called at the beginning of a frame to optimize
519 * rendering on some tiler architectures.
520 */
521 void discardFramebuffer(float left, float top, float right, float bottom);
522
523 /**
Romain Guyddf74372012-05-22 14:07:07 -0700524 * Ensures the state of the renderer is the same as the state of
525 * the GL context.
526 */
527 void syncState();
528
529 /**
Romain Guy2b7028e2012-09-19 17:25:38 -0700530 * Tells the GPU what part of the screen is about to be redrawn.
Romain Guyc3fedaf2013-01-29 17:26:25 -0800531 * This method will use the clip rect that we started drawing the
532 * frame with.
Romain Guy2b7028e2012-09-19 17:25:38 -0700533 * This method needs to be invoked every time getTargetFbo() is
534 * bound again.
535 */
Romain Guy57b52682012-09-20 17:38:46 -0700536 void startTiling(const sp<Snapshot>& snapshot, bool opaque = false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700537
538 /**
Romain Guyc3fedaf2013-01-29 17:26:25 -0800539 * Tells the GPU what part of the screen is about to be redrawn.
540 * This method needs to be invoked every time getTargetFbo() is
541 * bound again.
542 */
543 void startTiling(const Rect& clip, int windowHeight, bool opaque = false);
544
545 /**
Romain Guy2b7028e2012-09-19 17:25:38 -0700546 * Tells the GPU that we are done drawing the frame or that we
547 * are switching to another render target.
548 */
549 void endTiling();
550
551 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700552 * Saves the current state of the renderer as a new snapshot.
553 * The new snapshot is saved in mSnapshot and the previous snapshot
554 * is linked from mSnapshot->previous.
555 *
Romain Guy8aef54f2010-09-01 15:13:49 -0700556 * @param flags The save flags; see SkCanvas for more information
557 *
Romain Guy5cbbce52010-06-27 22:59:20 -0700558 * @return The new save count. This value can be passed to #restoreToCount()
559 */
Romain Guy8aef54f2010-09-01 15:13:49 -0700560 int saveSnapshot(int flags);
Romain Guy5cbbce52010-06-27 22:59:20 -0700561
562 /**
563 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
564 *
Romain Guy2542d192010-08-18 11:47:12 -0700565 * @return True if the clip was modified.
Romain Guy5cbbce52010-06-27 22:59:20 -0700566 */
Romain Guybb9524b2010-06-22 18:56:38 -0700567 bool restoreSnapshot();
568
Romain Guy5cbbce52010-06-27 22:59:20 -0700569 /**
570 * Sets the clipping rectangle using glScissor. The clip is defined by
571 * the current snapshot's clipRect member.
572 */
Romain Guybb9524b2010-06-22 18:56:38 -0700573 void setScissorFromClip();
574
Romain Guy5cbbce52010-06-27 22:59:20 -0700575 /**
Romain Guy8ce00302013-01-15 18:51:42 -0800576 * Sets the clipping region using the stencil buffer. The clip region
577 * is defined by the current snapshot's clipRegion member.
578 */
579 void setStencilFromClip();
580
581 /**
Romain Guy35643dd2012-09-18 15:40:58 -0700582 * Performs a quick reject but does not affect the scissor. Returns
583 * the transformed rect to test and the current clip.
584 */
585 bool quickRejectNoScissor(float left, float top, float right, float bottom,
586 Rect& transformed, Rect& clip);
587
588 /**
Chris Craikcb4d6002012-09-25 12:00:29 -0700589 * Performs a quick reject but adjust the bounds to account for stroke width if necessary
590 */
591 bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint);
592
593 /**
Chris Craik408eb122013-03-26 18:55:15 -0700594 * Given the local bounds of the layer, calculates ...
Chris Craikd90144d2013-03-19 15:03:48 -0700595 */
596 void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
597
598 /**
Chris Craik408eb122013-03-26 18:55:15 -0700599 * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
600 */
601 void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
602 bool fboLayer, int alpha);
603
604 /**
Romain Guyd55a8612010-06-28 17:42:46 -0700605 * Creates a new layer stored in the specified snapshot.
606 *
607 * @param snapshot The snapshot associated with the new layer
608 * @param left The left coordinate of the layer
609 * @param top The top coordinate of the layer
610 * @param right The right coordinate of the layer
611 * @param bottom The bottom coordinate of the layer
612 * @param alpha The translucency of the layer
613 * @param mode The blending mode of the layer
614 * @param flags The layer save flags
Romain Guyeb993562010-10-05 18:14:38 -0700615 * @param previousFbo The name of the current framebuffer
Romain Guyd55a8612010-06-28 17:42:46 -0700616 *
617 * @return True if the layer was successfully created, false otherwise
618 */
Chet Haased48885a2012-08-28 17:43:28 -0700619 bool createLayer(float left, float top, float right, float bottom,
Romain Guyeb993562010-10-05 18:14:38 -0700620 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
Romain Guyd55a8612010-06-28 17:42:46 -0700621
622 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700623 * Creates a new layer stored in the specified snapshot as an FBO.
624 *
625 * @param layer The layer to store as an FBO
626 * @param snapshot The snapshot associated with the new layer
627 * @param bounds The bounds of the layer
628 * @param previousFbo The name of the current framebuffer
629 */
Chet Haased48885a2012-08-28 17:43:28 -0700630 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
Romain Guy5b3b3522010-10-27 18:57:51 -0700631
632 /**
633 * Compose the specified layer as a region.
634 *
635 * @param layer The layer to compose
636 * @param rect The layer's bounds
637 */
638 void composeLayerRegion(Layer* layer, const Rect& rect);
639
640 /**
641 * Compose the specified layer as a simple rectangle.
642 *
643 * @param layer The layer to compose
644 * @param rect The layer's bounds
645 * @param swap If true, the source and destination are swapped
646 */
647 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
648
Romain Guy54be1cd2011-06-13 19:04:27 -0700649 /**
650 * Clears all the regions corresponding to the current list of layers.
651 * This method MUST be invoked before any drawing operation.
652 */
653 void clearLayerRegions();
654
655 /**
Romain Guyf219da52011-01-16 12:54:25 -0800656 * Mark the layer as dirty at the specified coordinates. The coordinates
657 * are transformed with the supplied matrix.
658 */
659 void dirtyLayer(const float left, const float top,
660 const float right, const float bottom, const mat4 transform);
661
662 /**
663 * Mark the layer as dirty at the specified coordinates.
664 */
665 void dirtyLayer(const float left, const float top,
666 const float right, const float bottom);
667
668 /**
Romain Guy5cbbce52010-06-27 22:59:20 -0700669 * Draws a colored rectangle with the specified color. The specified coordinates
Romain Guy735738c2012-12-03 12:34:51 -0800670 * are transformed by the current snapshot's transform matrix unless specified
671 * otherwise.
Romain Guy5cbbce52010-06-27 22:59:20 -0700672 *
673 * @param left The left coordinate of the rectangle
674 * @param top The top coordinate of the rectangle
675 * @param right The right coordinate of the rectangle
676 * @param bottom The bottom coordinate of the rectangle
677 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
Romain Guy026c5e162010-06-28 17:12:22 -0700678 * @param mode The Skia xfermode to use
Romain Guy3d58c032010-07-14 16:34:53 -0700679 * @param ignoreTransform True if the current transform should be ignored
Romain Guy5cbbce52010-06-27 22:59:20 -0700680 */
Romain Guy026c5e162010-06-28 17:12:22 -0700681 void drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -0700682 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
Romain Guy5cbbce52010-06-27 22:59:20 -0700683
Romain Guy54be1cd2011-06-13 19:04:27 -0700684 /**
Romain Guy735738c2012-12-03 12:34:51 -0800685 * Draws a series of colored rectangles with the specified color. The specified
686 * coordinates are transformed by the current snapshot's transform matrix unless
687 * specified otherwise.
688 *
689 * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
690 * per rectangle
691 * @param color The rectangles' ARGB color, defined as a packed 32 bits word
692 * @param mode The Skia xfermode to use
693 * @param ignoreTransform True if the current transform should be ignored
Romain Guy8ce00302013-01-15 18:51:42 -0800694 * @param dirty True if calling this method should dirty the current layer
Romain Guy3bbacf22013-02-06 16:51:04 -0800695 * @param clip True if the rects should be clipped, false otherwise
Romain Guy735738c2012-12-03 12:34:51 -0800696 */
697 status_t drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -0800698 SkXfermode::Mode mode, bool ignoreTransform = false,
699 bool dirty = true, bool clip = true);
Romain Guy735738c2012-12-03 12:34:51 -0800700
701 /**
Romain Guy54be1cd2011-06-13 19:04:27 -0700702 * Draws the shape represented by the specified path texture.
703 * This method invokes drawPathTexture() but takes into account
704 * the extra left/top offset and the texture offset to correctly
705 * position the final shape.
706 *
707 * @param left The left coordinate of the shape to render
708 * @param top The top coordinate of the shape to render
709 * @param texture The texture reprsenting the shape
710 * @param paint The paint to draw the shape with
711 */
Chet Haase48659092012-05-31 15:21:51 -0700712 status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
Romain Guy54be1cd2011-06-13 19:04:27 -0700713
714 /**
Romain Guy54be1cd2011-06-13 19:04:27 -0700715 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
716 * different compositing rules.
717 *
718 * @param texture The texture to draw with
719 * @param left The x coordinate of the bitmap
720 * @param top The y coordinate of the bitmap
721 * @param paint The paint to render with
722 */
Romain Guya168d732011-03-18 16:50:13 -0700723 void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
724
Romain Guy54be1cd2011-06-13 19:04:27 -0700725 /**
Chris Craik65cd6122012-12-10 17:56:27 -0800726 * Renders a strip of polygons with the specified paint, used for tessellated geometry.
727 *
728 * @param vertexBuffer The VertexBuffer to be drawn
729 * @param paint The paint to render with
730 * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines)
731 */
732 status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
733 bool useOffset = false);
734
735 /**
Chris Craik710f46d2012-09-17 17:25:49 -0700736 * Renders the convex hull defined by the specified path as a strip of polygons.
Romain Guy54be1cd2011-06-13 19:04:27 -0700737 *
Chris Craik710f46d2012-09-17 17:25:49 -0700738 * @param path The hull of the path to draw
Chris Craikcb4d6002012-09-25 12:00:29 -0700739 * @param paint The paint to render with
Romain Guy54be1cd2011-06-13 19:04:27 -0700740 */
Chris Craik65cd6122012-12-10 17:56:27 -0800741 status_t drawConvexPath(const SkPath& path, SkPaint* paint);
Chet Haase858aa932011-05-12 09:06:00 -0700742
Romain Guy5cbbce52010-06-27 22:59:20 -0700743 /**
744 * Draws a textured rectangle with the specified texture. The specified coordinates
745 * are transformed by the current snapshot's transform matrix.
746 *
747 * @param left The left coordinate of the rectangle
748 * @param top The top coordinate of the rectangle
749 * @param right The right coordinate of the rectangle
750 * @param bottom The bottom coordinate of the rectangle
751 * @param texture The texture name to map onto the rectangle
752 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
Romain Guyd55a8612010-06-28 17:42:46 -0700753 * @param mode The blending mode
Romain Guyc1396e92010-06-30 17:56:19 -0700754 * @param blend True if the texture contains an alpha channel
Romain Guy5cbbce52010-06-27 22:59:20 -0700755 */
Romain Guybd6b79b2010-06-26 00:13:53 -0700756 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700757 float alpha, SkXfermode::Mode mode, bool blend);
Romain Guyc7d53492010-06-25 13:41:57 -0700758
Romain Guy026c5e162010-06-28 17:12:22 -0700759 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700760 * Draws a textured rectangle with the specified texture. The specified coordinates
761 * are transformed by the current snapshot's transform matrix.
762 *
763 * @param left The left coordinate of the rectangle
764 * @param top The top coordinate of the rectangle
765 * @param right The right coordinate of the rectangle
766 * @param bottom The bottom coordinate of the rectangle
767 * @param texture The texture to use
768 * @param paint The paint containing the alpha, blending mode, etc.
Romain Guy82ba8142010-07-09 13:25:56 -0700769 */
Romain Guya9794742010-07-13 11:37:54 -0700770 void drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -0700771 Texture* texture, SkPaint* paint);
Romain Guy82ba8142010-07-09 13:25:56 -0700772
773 /**
Romain Guy03750a02010-10-18 14:06:08 -0700774 * Draws a textured mesh with the specified texture. If the indices are omitted,
775 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a
776 * VBO is bound.
Romain Guy82ba8142010-07-09 13:25:56 -0700777 *
778 * @param left The left coordinate of the rectangle
779 * @param top The top coordinate of the rectangle
780 * @param right The right coordinate of the rectangle
781 * @param bottom The bottom coordinate of the rectangle
782 * @param texture The texture name to map onto the rectangle
783 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
784 * @param mode The blending mode
785 * @param blend True if the texture contains an alpha channel
Romain Guy82ba8142010-07-09 13:25:56 -0700786 * @param vertices The vertices that define the mesh
787 * @param texCoords The texture coordinates of each vertex
Romain Guy82ba8142010-07-09 13:25:56 -0700788 * @param elementsCount The number of elements in the mesh, required by indices
Romain Guyf607bdc2010-09-10 19:20:06 -0700789 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped
790 * @param ignoreTransform True if the current transform should be ignored
Romain Guy03750a02010-10-18 14:06:08 -0700791 * @param vbo The VBO used to draw the mesh
Romain Guy5b3b3522010-10-27 18:57:51 -0700792 * @param ignoreScale True if the model view matrix should not be scaled
793 * @param dirty True if calling this method should dirty the current layer
Romain Guyf7f93552010-07-08 19:17:03 -0700794 */
795 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
Romain Guya9794742010-07-13 11:37:54 -0700796 float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -0700797 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -0700798 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0,
799 bool ignoreScale = false, bool dirty = true);
Romain Guyf7f93552010-07-08 19:17:03 -0700800
Romain Guy886b2752013-01-04 12:26:18 -0800801 void drawAlpha8TextureMesh(float left, float top, float right, float bottom,
802 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
803 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -0800804 bool ignoreTransform, bool ignoreScale = false, bool dirty = true);
Romain Guy886b2752013-01-04 12:26:18 -0800805
Romain Guyf7f93552010-07-08 19:17:03 -0700806 /**
Romain Guy0a417492010-08-16 20:26:20 -0700807 * Draws text underline and strike-through if needed.
808 *
809 * @param text The text to decor
810 * @param bytesCount The number of bytes in the text
811 * @param length The length in pixels of the text, can be <= 0.0f to force a measurement
812 * @param x The x coordinate where the text will be drawn
813 * @param y The y coordinate where the text will be drawn
814 * @param paint The paint to draw the text with
815 */
816 void drawTextDecorations(const char* text, int bytesCount, float length,
817 float x, float y, SkPaint* paint);
Romain Guy1e45aae2010-08-13 19:39:53 -0700818
Raph Levien416a8472012-07-19 22:48:17 -0700819 /**
820 * Draws shadow layer on text (with optional positions).
821 *
822 * @param paint The paint to draw the shadow with
823 * @param text The text to draw
824 * @param bytesCount The number of bytes in the text
825 * @param count The number of glyphs in the text
826 * @param positions The x, y positions of individual glyphs (or NULL)
827 * @param fontRenderer The font renderer object
828 * @param alpha The alpha value for drawing the shadow
829 * @param mode The xfermode for drawing the shadow
830 * @param x The x coordinate where the shadow will be drawn
831 * @param y The y coordinate where the shadow will be drawn
832 */
833 void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
834 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
835 float x, float y);
836
Romain Guy54be1cd2011-06-13 19:04:27 -0700837 /**
838 * Draws a path texture. Path textures are alpha8 bitmaps that need special
839 * compositing to apply colors/filters/etc.
840 *
841 * @param texture The texture to render
842 * @param x The x coordinate where the texture will be drawn
843 * @param y The y coordinate where the texture will be drawn
844 * @param paint The paint to draw the texture with
845 */
Raph Levien416a8472012-07-19 22:48:17 -0700846 void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
Romain Guy01d58e42011-01-19 21:54:02 -0800847
Romain Guy1e45aae2010-08-13 19:39:53 -0700848 /**
Romain Guyac670c02010-07-27 17:39:27 -0700849 * Resets the texture coordinates stored in mMeshVertices. Setting the values
Romain Guy026c5e162010-06-28 17:12:22 -0700850 * back to default is achieved by calling:
851 *
Romain Guy8ba548f2010-06-30 19:21:21 -0700852 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy026c5e162010-06-28 17:12:22 -0700853 *
854 * @param u1 The left coordinate of the texture
855 * @param v1 The bottom coordinate of the texture
856 * @param u2 The right coordinate of the texture
857 * @param v2 The top coordinate of the texture
858 */
859 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
860
Romain Guy8ba548f2010-06-30 19:21:21 -0700861 /**
Romain Guy768bffc2013-02-27 13:50:45 -0800862 * Returns true if the specified paint will draw invisible text.
863 */
864 bool canSkipText(const SkPaint* paint) const;
865
866 /**
Romain Guy746b7402010-10-26 16:27:31 -0700867 * Binds the specified texture. The texture unit must have been selected
868 * prior to calling this method.
Romain Guya1db5742010-07-20 13:09:13 -0700869 */
Romain Guy746b7402010-10-26 16:27:31 -0700870 inline void bindTexture(GLuint texture) {
871 glBindTexture(GL_TEXTURE_2D, texture);
872 }
873
874 /**
Romain Guyaa6c24c2011-04-28 18:40:04 -0700875 * Binds the specified EGLImage texture. The texture unit must have been selected
876 * prior to calling this method.
877 */
878 inline void bindExternalTexture(GLuint texture) {
879 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
880 }
881
882 /**
Romain Guy82ba8142010-07-09 13:25:56 -0700883 * Enable or disable blending as necessary. This function sets the appropriate
884 * blend function based on the specified xfermode.
885 */
Romain Guyf607bdc2010-09-10 19:20:06 -0700886 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
887 bool swapSrcDst = false);
Romain Guya5aed0d2010-09-09 14:42:43 -0700888
Romain Guyf607bdc2010-09-10 19:20:06 -0700889 /**
Romain Guyd27977d2010-07-14 19:18:51 -0700890 * Use the specified program with the current GL context. If the program is already
891 * in use, it will not be bound again. If it is not in use, the current program is
892 * marked unused and the specified program becomes used and becomes the new
893 * current program.
Romain Guy6926c722010-07-12 20:20:03 -0700894 *
Romain Guyd27977d2010-07-14 19:18:51 -0700895 * @param program The program to use
896 *
897 * @return true If the specified program was already in use, false otherwise.
Romain Guy260e1022010-07-12 14:41:06 -0700898 */
Romain Guy889f8d12010-07-29 14:37:42 -0700899 inline bool useProgram(Program* program);
Romain Guy260e1022010-07-12 14:41:06 -0700900
Romain Guy746b7402010-10-26 16:27:31 -0700901 /**
902 * Invoked before any drawing operation. This sets required state.
903 */
Romain Guy54be1cd2011-06-13 19:04:27 -0700904 void setupDraw(bool clear = true);
Romain Guy17112ad2012-08-07 11:24:39 -0700905
Romain Guy70ca14e2010-12-13 18:24:33 -0800906 /**
907 * Various methods to setup OpenGL rendering.
908 */
909 void setupDrawWithTexture(bool isAlpha8 = false);
Romain Guyff316ec2013-02-13 18:39:43 -0800910 void setupDrawWithTextureAndColor(bool isAlpha8 = false);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700911 void setupDrawWithExternalTexture();
Romain Guy15bc6432011-12-13 13:11:32 -0800912 void setupDrawNoTexture();
Chris Craik710f46d2012-09-17 17:25:49 -0700913 void setupDrawAA();
Romain Guyed6fcb02011-03-21 13:11:28 -0700914 void setupDrawPoint(float pointSize);
Romain Guy8d0d4782010-12-14 20:13:35 -0800915 void setupDrawColor(int color, int alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -0800916 void setupDrawColor(float r, float g, float b, float a);
Romain Guy86568192010-12-14 15:55:39 -0800917 void setupDrawAlpha8Color(int color, int alpha);
Romain Guy41210632012-07-16 17:04:24 -0700918 void setupDrawTextGamma(const SkPaint* paint);
Romain Guy70ca14e2010-12-13 18:24:33 -0800919 void setupDrawShader();
920 void setupDrawColorFilter();
921 void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
922 bool swapSrcDst = false);
923 void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
924 bool swapSrcDst = false);
925 void setupDrawProgram();
926 void setupDrawDirtyRegionsDisabled();
Chet Haase8a5cc922011-04-26 07:28:09 -0700927 void setupDrawModelViewIdentity(bool offset = false);
Romain Guy70ca14e2010-12-13 18:24:33 -0800928 void setupDrawModelView(float left, float top, float right, float bottom,
929 bool ignoreTransform = false, bool ignoreModelView = false);
930 void setupDrawModelViewTranslate(float left, float top, float right, float bottom,
931 bool ignoreTransform = false);
Romain Guyed6fcb02011-03-21 13:11:28 -0700932 void setupDrawPointUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -0800933 void setupDrawColorUniforms();
Romain Guy86568192010-12-14 15:55:39 -0800934 void setupDrawPureColorUniforms();
Romain Guy8d0d4782010-12-14 20:13:35 -0800935 void setupDrawShaderIdentityUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -0800936 void setupDrawShaderUniforms(bool ignoreTransform = false);
937 void setupDrawColorFilterUniforms();
938 void setupDrawSimpleMesh();
939 void setupDrawTexture(GLuint texture);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 void setupDrawExternalTexture(GLuint texture);
Romain Guy8f0095c2011-05-02 17:24:22 -0700941 void setupDrawTextureTransform();
942 void setupDrawTextureTransformUniforms(mat4& transform);
Romain Guy41210632012-07-16 17:04:24 -0700943 void setupDrawTextGammaUniforms();
Romain Guy8d0d4782010-12-14 20:13:35 -0800944 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
Romain Guyff316ec2013-02-13 18:39:43 -0800945 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors);
Romain Guy15bc6432011-12-13 13:11:32 -0800946 void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
Chet Haase5b0200b2011-04-13 17:58:08 -0700947 void setupDrawVertices(GLvoid* vertices);
Romain Guy70ca14e2010-12-13 18:24:33 -0800948 void finishDrawTexture();
Romain Guyf09ef512011-05-27 11:43:46 -0700949 void accountForClear(SkXfermode::Mode mode);
Romain Guy746b7402010-10-26 16:27:31 -0700950
Romain Guy11cb6422012-09-21 00:39:43 -0700951 bool updateLayer(Layer* layer, bool inFrame);
952 void updateLayers();
Romain Guy96885eb2013-03-26 15:05:58 -0700953 void flushLayers();
Romain Guy11cb6422012-09-21 00:39:43 -0700954
Romain Guy17112ad2012-08-07 11:24:39 -0700955 /**
956 * Renders the specified region as a series of rectangles. This method
957 * is used for debugging only.
958 */
Romain Guy3a3133d2011-02-01 22:59:58 -0800959 void drawRegionRects(const Region& region);
960
Romain Guy8ce00302013-01-15 18:51:42 -0800961 /**
962 * Renders the specified region as a series of rectangles. The region
963 * must be in screen-space coordinates.
964 */
965 void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode,
966 bool dirty = false);
967
968 /**
969 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
970 * is turned on.
971 */
972 void debugClip();
973
Romain Guy7c450aa2012-09-21 19:15:00 -0700974 void debugOverdraw(bool enable, bool clear);
975 void renderOverdraw();
976
Romain Guy746b7402010-10-26 16:27:31 -0700977 /**
978 * Should be invoked every time the glScissor is modified.
979 */
980 inline void dirtyClip() {
981 mDirtyClip = true;
982 }
983
Romain Guy3b753822013-03-05 10:27:35 -0800984 inline mat4& currentTransform() const {
985 return *mSnapshot->transform;
986 }
987
Romain Guybb9524b2010-06-22 18:56:38 -0700988 // Dimensions of the drawing surface
989 int mWidth, mHeight;
990
Romain Guy85bf02f2010-06-22 13:11:24 -0700991 // Matrix used for ortho projection in shaders
Romain Guy260e1022010-07-12 14:41:06 -0700992 mat4 mOrthoMatrix;
Romain Guybb9524b2010-06-22 18:56:38 -0700993
Romain Guyc7d53492010-06-25 13:41:57 -0700994 // Model-view matrix used to position/size objects
995 mat4 mModelView;
996
Romain Guybb9524b2010-06-22 18:56:38 -0700997 // Number of saved states
998 int mSaveCount;
Romain Guyf6a11b82010-06-23 17:47:49 -0700999 // Base state
Romain Guyae5575b2010-07-29 18:48:04 -07001000 sp<Snapshot> mFirstSnapshot;
Romain Guybb9524b2010-06-22 18:56:38 -07001001 // Current state
1002 sp<Snapshot> mSnapshot;
Romain Guy2b7028e2012-09-19 17:25:38 -07001003 // State used to define the clipping region
Chris Craik5f803622013-03-21 14:39:04 -07001004 Rect mTilingClip;
Romain Guy96885eb2013-03-26 15:05:58 -07001005 // Is the target render surface opaque
1006 bool mOpaque;
1007 // Is a frame currently being rendered
1008 bool mFrameStarted;
Romain Guy9d5316e2010-06-24 19:30:36 -07001009
Romain Guy026c5e162010-06-28 17:12:22 -07001010 // Used to draw textured quads
Romain Guyac670c02010-07-27 17:39:27 -07001011 TextureVertex mMeshVertices[4];
Romain Guyce0537b2010-06-29 21:05:21 -07001012
Chris Craikc3566d02013-02-04 16:16:33 -08001013 // shader, filters, and shadow
1014 DrawModifiers mDrawModifiers;
Romain Guy5ff9df62012-01-23 17:09:05 -08001015 SkPaint mFilteredPaint;
1016
Romain Guy82ba8142010-07-09 13:25:56 -07001017 // Various caches
Romain Guyfb8b7632010-08-23 21:05:08 -07001018 Caches& mCaches;
Romain Guy3bbacf22013-02-06 16:51:04 -08001019 Extensions& mExtensions;
Romain Guy86942302010-09-12 13:02:16 -07001020
Romain Guy8f3b8e32012-03-27 16:33:45 -07001021 // List of rectangles to clear after saveLayer() is invoked
Romain Guy54be1cd2011-06-13 19:04:27 -07001022 Vector<Rect*> mLayers;
Romain Guy8f3b8e32012-03-27 16:33:45 -07001023 // List of functors to invoke after a frame is drawn
Romain Guyba6be8a2012-04-23 18:22:09 -07001024 SortedVector<Functor*> mFunctors;
Romain Guy11cb6422012-09-21 00:39:43 -07001025 // List of layers to update at the beginning of a frame
1026 Vector<Layer*> mLayerUpdates;
Romain Guy54be1cd2011-06-13 19:04:27 -07001027
Romain Guy746b7402010-10-26 16:27:31 -07001028 // Indicates whether the clip must be restored
1029 bool mDirtyClip;
1030
Romain Guy70ca14e2010-12-13 18:24:33 -08001031 // The following fields are used to setup drawing
1032 // Used to describe the shaders to generate
1033 ProgramDescription mDescription;
1034 // Color description
1035 bool mColorSet;
1036 float mColorA, mColorR, mColorG, mColorB;
1037 // Indicates that the shader should get a color
1038 bool mSetShaderColor;
1039 // Current texture unit
1040 GLuint mTextureUnit;
1041 // Track dirty regions, true by default
1042 bool mTrackDirtyRegions;
Romain Guy2b7028e2012-09-19 17:25:38 -07001043 // Indicate whether we are drawing an opaque frame
1044 bool mOpaqueFrame;
Romain Guy70ca14e2010-12-13 18:24:33 -08001045
Romain Guy87e2f7572012-09-24 11:37:12 -07001046 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
1047 // Properties.h
1048 bool mScissorOptimizationDisabled;
1049
Romain Guy54c1a642012-09-27 17:55:46 -07001050 // No-ops start/endTiling when set
1051 bool mSuppressTiling;
1052
Romain Guyef359272013-01-31 19:07:29 -08001053 // Optional name of the renderer
1054 String8 mName;
1055
Romain Guyb051e892010-09-28 19:09:36 -07001056 friend class DisplayListRenderer;
Romain Guy96885eb2013-03-26 15:05:58 -07001057 friend class Layer;
Romain Guy257ae352013-03-20 16:31:12 -07001058 friend class TextSetupFunctor;
Romain Guyb051e892010-09-28 19:09:36 -07001059
Romain Guybb9524b2010-06-22 18:56:38 -07001060}; // class OpenGLRenderer
Romain Guye4d01122010-06-16 18:44:05 -07001061
Romain Guy9d5316e2010-06-24 19:30:36 -07001062}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07001063}; // namespace android
1064
Romain Guy5b3b3522010-10-27 18:57:51 -07001065#endif // ANDROID_HWUI_OPENGL_RENDERER_H