OpenGLRenderer.h revision da8d8b9699fe8e9557389465007e333d3f03dcc1
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#ifndef ANDROID_HWUI_OPENGL_RENDERER_H
18#define ANDROID_HWUI_OPENGL_RENDERER_H
19
20#include <GLES2/gl2.h>
21#include <GLES2/gl2ext.h>
22
23#include <SkBitmap.h>
24#include <SkMatrix.h>
25#include <SkPaint.h>
26#include <SkRegion.h>
27#include <SkShader.h>
28#include <SkXfermode.h>
29
30#include <utils/Functor.h>
31#include <utils/RefBase.h>
32#include <utils/SortedVector.h>
33#include <utils/Vector.h>
34
35#include <cutils/compiler.h>
36
37#include "Debug.h"
38#include "Extensions.h"
39#include "Matrix.h"
40#include "Program.h"
41#include "Rect.h"
42#include "Snapshot.h"
43#include "Vertex.h"
44#include "SkiaShader.h"
45#include "SkiaColorFilter.h"
46#include "Caches.h"
47
48namespace android {
49namespace uirenderer {
50
51///////////////////////////////////////////////////////////////////////////////
52// Renderer
53///////////////////////////////////////////////////////////////////////////////
54
55class DisplayList;
56class VertexBuffer;
57
58/**
59 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
60 * simplified version of Skia's Canvas API.
61 */
62class OpenGLRenderer {
63public:
64    ANDROID_API OpenGLRenderer();
65    virtual ~OpenGLRenderer();
66
67    /**
68     * Sets the name of this renderer. The name is optional and
69     * empty by default. If the pointer is null the name is set
70     * to the empty string.
71     */
72    ANDROID_API void setName(const char* name);
73
74    /**
75     * Returns the name of this renderer as UTF8 string.
76     * The returned pointer is never null.
77     */
78    ANDROID_API const char* getName() const;
79
80    /**
81     * Read externally defined properties to control the behavior
82     * of the renderer.
83     */
84    ANDROID_API void initProperties();
85
86    /**
87     * Indicates whether this renderer executes drawing commands immediately.
88     * If this method returns true, the drawing commands will be executed
89     * later.
90     */
91    virtual bool isDeferred();
92
93    /**
94     * Sets the dimension of the underlying drawing surface. This method must
95     * be called at least once every time the drawing surface changes size.
96     *
97     * @param width The width in pixels of the underlysing surface
98     * @param height The height in pixels of the underlysing surface
99     */
100    virtual void setViewport(int width, int height);
101
102    /**
103     * Prepares the renderer to draw a frame. This method must be invoked
104     * at the beginning of each frame. When this method is invoked, the
105     * entire drawing surface is assumed to be redrawn.
106     *
107     * @param opaque If true, the target surface is considered opaque
108     *               and will not be cleared. If false, the target surface
109     *               will be cleared
110     */
111    ANDROID_API status_t prepare(bool opaque);
112
113    /**
114     * Prepares the renderer to draw a frame. This method must be invoked
115     * at the beginning of each frame. Only the specified rectangle of the
116     * frame is assumed to be dirty. A clip will automatically be set to
117     * the specified rectangle.
118     *
119     * @param left The left coordinate of the dirty rectangle
120     * @param top The top coordinate of the dirty rectangle
121     * @param right The right coordinate of the dirty rectangle
122     * @param bottom The bottom coordinate of the dirty rectangle
123     * @param opaque If true, the target surface is considered opaque
124     *               and will not be cleared. If false, the target surface
125     *               will be cleared in the specified dirty rectangle
126     */
127    virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
128
129    /**
130     * Indicates the end of a frame. This method must be invoked whenever
131     * the caller is done rendering a frame.
132     */
133    virtual void finish();
134
135    /**
136     * This method must be invoked before handing control over to a draw functor.
137     * See callDrawGLFunction() for instance.
138     *
139     * This command must not be recorded inside display lists.
140     */
141    virtual void interrupt();
142
143    /**
144     * This method must be invoked after getting control back from a draw functor.
145     *
146     * This command must not be recorded inside display lists.
147     */
148    virtual void resume();
149
150    ANDROID_API status_t invokeFunctors(Rect& dirty);
151    ANDROID_API void detachFunctor(Functor* functor);
152    ANDROID_API void attachFunctor(Functor* functor);
153    virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty);
154
155    ANDROID_API void pushLayerUpdate(Layer* layer);
156    ANDROID_API void clearLayerUpdates();
157
158    ANDROID_API int getSaveCount() const;
159    virtual int save(int flags);
160    virtual void restore();
161    virtual void restoreToCount(int saveCount);
162
163    virtual int saveLayer(float left, float top, float right, float bottom,
164            SkPaint* p, int flags);
165    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
166            int alpha, int flags);
167
168    virtual void translate(float dx, float dy);
169    virtual void rotate(float degrees);
170    virtual void scale(float sx, float sy);
171    virtual void skew(float sx, float sy);
172
173    ANDROID_API void getMatrix(SkMatrix* matrix);
174    virtual void setMatrix(SkMatrix* matrix);
175    virtual void concatMatrix(SkMatrix* matrix);
176
177    ANDROID_API const Rect& getClipBounds();
178    ANDROID_API bool quickReject(float left, float top, float right, float bottom);
179    bool quickRejectNoScissor(float left, float top, float right, float bottom);
180    virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
181    virtual bool clipPath(SkPath* path, SkRegion::Op op);
182    virtual bool clipRegion(SkRegion* region, SkRegion::Op op);
183    virtual Rect* getClipRect();
184
185    virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t flags,
186            uint32_t level = 0);
187    virtual void outputDisplayList(DisplayList* displayList, uint32_t level = 0);
188    virtual status_t drawLayer(Layer* layer, float x, float y, SkPaint* paint);
189    virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
190    virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
191    virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
192            float srcRight, float srcBottom, float dstLeft, float dstTop,
193            float dstRight, float dstBottom, SkPaint* paint);
194    virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
195    virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
196            float* vertices, int* colors, SkPaint* paint);
197    virtual status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
198            const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
199            float left, float top, float right, float bottom, SkPaint* paint);
200    status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
201            const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
202            float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode);
203    virtual status_t drawColor(int color, SkXfermode::Mode mode);
204    virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint);
205    virtual status_t drawRoundRect(float left, float top, float right, float bottom,
206            float rx, float ry, SkPaint* paint);
207    virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint);
208    virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint);
209    virtual status_t drawArc(float left, float top, float right, float bottom,
210            float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
211    virtual status_t drawPath(SkPath* path, SkPaint* paint);
212    virtual status_t drawLines(float* points, int count, SkPaint* paint);
213    virtual status_t drawPoints(float* points, int count, SkPaint* paint);
214    virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
215            float hOffset, float vOffset, SkPaint* paint);
216    virtual status_t drawPosText(const char* text, int bytesCount, int count,
217            const float* positions, SkPaint* paint);
218    virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
219            const float* positions, SkPaint* paint, float length = -1.0f);
220    virtual status_t drawRects(const float* rects, int count, SkPaint* paint);
221
222    virtual void resetShader();
223    virtual void setupShader(SkiaShader* shader);
224
225    virtual void resetColorFilter();
226    virtual void setupColorFilter(SkiaColorFilter* filter);
227
228    virtual void resetShadow();
229    virtual void setupShadow(float radius, float dx, float dy, int color);
230
231    virtual void resetPaintFilter();
232    virtual void setupPaintFilter(int clearBits, int setBits);
233
234    SkPaint* filterPaint(SkPaint* paint);
235
236    ANDROID_API bool isCurrentTransformSimple() {
237        return mSnapshot->transform->isSimple();
238    }
239
240    /**
241     * Sets the alpha on the current snapshot. This alpha value will be modulated
242     * with other alpha values when drawing primitives.
243     */
244    void setAlpha(float alpha) {
245        mSnapshot->alpha = alpha;
246    }
247
248    /**
249     * Inserts a named group marker in the stream of GL commands. This marker
250     * can be used by tools to group commands into logical groups. A call to
251     * this method must always be followed later on by a call to endMark().
252     */
253    void startMark(const char* name) const;
254
255    /**
256     * Closes the last group marker opened by startMark().
257     */
258    void endMark() const;
259
260    /**
261     * Gets the alpha and xfermode out of a paint object. If the paint is null
262     * alpha will be 255 and the xfermode will be SRC_OVER. This method does
263     * not multiply the paint's alpha by the current snapshot's alpha.
264     *
265     * @param paint The paint to extract values from
266     * @param alpha Where to store the resulting alpha
267     * @param mode Where to store the resulting xfermode
268     */
269    static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
270        if (paint) {
271            *mode = getXfermode(paint->getXfermode());
272
273            // Skia draws using the color's alpha channel if < 255
274            // Otherwise, it uses the paint's alpha
275            int color = paint->getColor();
276            *alpha = (color >> 24) & 0xFF;
277            if (*alpha == 255) {
278                *alpha = paint->getAlpha();
279            }
280        } else {
281            *mode = SkXfermode::kSrcOver_Mode;
282            *alpha = 255;
283        }
284    }
285
286protected:
287    /**
288     * Computes the projection matrix, initialize the first snapshot
289     * and stores the dimensions of the render target.
290     */
291    void initViewport(int width, int height);
292
293    /**
294     * Clears the underlying surface if needed.
295     */
296    virtual status_t clear(float left, float top, float right, float bottom, bool opaque);
297
298    /**
299     * Call this method after updating a layer during a drawing pass.
300     */
301    void resumeAfterLayer();
302
303    /**
304     * This method is called whenever a stencil buffer is required. Subclasses
305     * should override this method and call attachStencilBufferToLayer() on the
306     * appropriate layer(s).
307     */
308    virtual void ensureStencilBuffer();
309
310    /**
311     * Obtains a stencil render buffer (allocating it if necessary) and
312     * attaches it to the specified layer.
313     */
314    void attachStencilBufferToLayer(Layer* layer);
315
316    /**
317     * Compose the layer defined in the current snapshot with the layer
318     * defined by the previous snapshot.
319     *
320     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
321     *
322     * @param curent The current snapshot containing the layer to compose
323     * @param previous The previous snapshot to compose the current layer with
324     */
325    virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
326
327    /**
328     * Marks the specified region as dirty at the specified bounds.
329     */
330    void dirtyLayerUnchecked(Rect& bounds, Region* region);
331
332    /**
333     * Returns the current snapshot.
334     */
335    sp<Snapshot> getSnapshot() {
336        return mSnapshot;
337    }
338
339    /**
340     * Returns the region of the current layer.
341     */
342    virtual Region* getRegion() {
343        return mSnapshot->region;
344    }
345
346    /**
347     * Indicates whether rendering is currently targeted at a layer.
348     */
349    virtual bool hasLayer() {
350        return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
351    }
352
353    /**
354     * Returns the name of the FBO this renderer is rendering into.
355     */
356    virtual GLint getTargetFbo() {
357        return 0;
358    }
359
360    /**
361     * Renders the specified layer as a textured quad.
362     *
363     * @param layer The layer to render
364     * @param rect The bounds of the layer
365     */
366    void drawTextureLayer(Layer* layer, const Rect& rect);
367
368    /**
369     * Gets the alpha and xfermode out of a paint object. If the paint is null
370     * alpha will be 255 and the xfermode will be SRC_OVER.
371     *
372     * @param paint The paint to extract values from
373     * @param alpha Where to store the resulting alpha
374     * @param mode Where to store the resulting xfermode
375     */
376    inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
377
378    /**
379     * Safely retrieves the mode from the specified xfermode. If the specified
380     * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
381     */
382    static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
383        SkXfermode::Mode resultMode;
384        if (!SkXfermode::AsMode(mode, &resultMode)) {
385            resultMode = SkXfermode::kSrcOver_Mode;
386        }
387        return resultMode;
388    }
389
390    /**
391     * Set to true to suppress error checks at the end of a frame.
392     */
393    virtual bool suppressErrorChecks() {
394        return false;
395    }
396
397    Caches& getCaches() {
398        return mCaches;
399    }
400
401private:
402    /**
403     * Discards the content of the framebuffer if supported by the driver.
404     * This method should be called at the beginning of a frame to optimize
405     * rendering on some tiler architectures.
406     */
407    void discardFramebuffer(float left, float top, float right, float bottom);
408
409    /**
410     * Ensures the state of the renderer is the same as the state of
411     * the GL context.
412     */
413    void syncState();
414
415    /**
416     * Tells the GPU what part of the screen is about to be redrawn.
417     * This method will use the clip rect that we started drawing the
418     * frame with.
419     * This method needs to be invoked every time getTargetFbo() is
420     * bound again.
421     */
422    void startTiling(const sp<Snapshot>& snapshot, bool opaque = false);
423
424    /**
425     * Tells the GPU what part of the screen is about to be redrawn.
426     * This method needs to be invoked every time getTargetFbo() is
427     * bound again.
428     */
429    void startTiling(const Rect& clip, int windowHeight, bool opaque = false);
430
431    /**
432     * Tells the GPU that we are done drawing the frame or that we
433     * are switching to another render target.
434     */
435    void endTiling();
436
437    /**
438     * Saves the current state of the renderer as a new snapshot.
439     * The new snapshot is saved in mSnapshot and the previous snapshot
440     * is linked from mSnapshot->previous.
441     *
442     * @param flags The save flags; see SkCanvas for more information
443     *
444     * @return The new save count. This value can be passed to #restoreToCount()
445     */
446    int saveSnapshot(int flags);
447
448    /**
449     * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
450     *
451     * @return True if the clip was modified.
452     */
453    bool restoreSnapshot();
454
455    /**
456     * Sets the clipping rectangle using glScissor. The clip is defined by
457     * the current snapshot's clipRect member.
458     */
459    void setScissorFromClip();
460
461    /**
462     * Sets the clipping region using the stencil buffer. The clip region
463     * is defined by the current snapshot's clipRegion member.
464     */
465    void setStencilFromClip();
466
467    /**
468     * Performs a quick reject but does not affect the scissor. Returns
469     * the transformed rect to test and the current clip.
470     */
471    bool quickRejectNoScissor(float left, float top, float right, float bottom,
472            Rect& transformed, Rect& clip);
473
474    /**
475     * Performs a quick reject but adjust the bounds to account for stroke width if necessary
476     */
477    bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint);
478
479    /**
480     * Creates a new layer stored in the specified snapshot.
481     *
482     * @param snapshot The snapshot associated with the new layer
483     * @param left The left coordinate of the layer
484     * @param top The top coordinate of the layer
485     * @param right The right coordinate of the layer
486     * @param bottom The bottom coordinate of the layer
487     * @param alpha The translucency of the layer
488     * @param mode The blending mode of the layer
489     * @param flags The layer save flags
490     * @param previousFbo The name of the current framebuffer
491     *
492     * @return True if the layer was successfully created, false otherwise
493     */
494    bool createLayer(float left, float top, float right, float bottom,
495            int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
496
497    /**
498     * Creates a new layer stored in the specified snapshot as an FBO.
499     *
500     * @param layer The layer to store as an FBO
501     * @param snapshot The snapshot associated with the new layer
502     * @param bounds The bounds of the layer
503     * @param previousFbo The name of the current framebuffer
504     */
505    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
506
507    /**
508     * Compose the specified layer as a region.
509     *
510     * @param layer The layer to compose
511     * @param rect The layer's bounds
512     */
513    void composeLayerRegion(Layer* layer, const Rect& rect);
514
515    /**
516     * Compose the specified layer as a simple rectangle.
517     *
518     * @param layer The layer to compose
519     * @param rect The layer's bounds
520     * @param swap If true, the source and destination are swapped
521     */
522    void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
523
524    /**
525     * Clears all the regions corresponding to the current list of layers.
526     * This method MUST be invoked before any drawing operation.
527     */
528    void clearLayerRegions();
529
530    /**
531     * Mark the layer as dirty at the specified coordinates. The coordinates
532     * are transformed with the supplied matrix.
533     */
534    void dirtyLayer(const float left, const float top,
535            const float right, const float bottom, const mat4 transform);
536
537    /**
538     * Mark the layer as dirty at the specified coordinates.
539     */
540    void dirtyLayer(const float left, const float top,
541            const float right, const float bottom);
542
543    /**
544     * Draws a colored rectangle with the specified color. The specified coordinates
545     * are transformed by the current snapshot's transform matrix unless specified
546     * otherwise.
547     *
548     * @param left The left coordinate of the rectangle
549     * @param top The top coordinate of the rectangle
550     * @param right The right coordinate of the rectangle
551     * @param bottom The bottom coordinate of the rectangle
552     * @param color The rectangle's ARGB color, defined as a packed 32 bits word
553     * @param mode The Skia xfermode to use
554     * @param ignoreTransform True if the current transform should be ignored
555     */
556    void drawColorRect(float left, float top, float right, float bottom,
557            int color, SkXfermode::Mode mode, bool ignoreTransform = false);
558
559    /**
560     * Draws a series of colored rectangles with the specified color. The specified
561     * coordinates are transformed by the current snapshot's transform matrix unless
562     * specified otherwise.
563     *
564     * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
565     *              per rectangle
566     * @param color The rectangles' ARGB color, defined as a packed 32 bits word
567     * @param mode The Skia xfermode to use
568     * @param ignoreTransform True if the current transform should be ignored
569     * @param dirty True if calling this method should dirty the current layer
570     * @param clip True if the rects should be clipped, false otherwise
571     */
572    status_t drawColorRects(const float* rects, int count, int color,
573            SkXfermode::Mode mode, bool ignoreTransform = false,
574            bool dirty = true, bool clip = true);
575
576    /**
577     * Draws the shape represented by the specified path texture.
578     * This method invokes drawPathTexture() but takes into account
579     * the extra left/top offset and the texture offset to correctly
580     * position the final shape.
581     *
582     * @param left The left coordinate of the shape to render
583     * @param top The top coordinate of the shape to render
584     * @param texture The texture reprsenting the shape
585     * @param paint The paint to draw the shape with
586     */
587    status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
588
589    /**
590     * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
591     * different compositing rules.
592     *
593     * @param texture The texture to draw with
594     * @param left The x coordinate of the bitmap
595     * @param top The y coordinate of the bitmap
596     * @param paint The paint to render with
597     */
598    void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
599
600    /**
601     * Renders a strip of polygons with the specified paint, used for tessellated geometry.
602     *
603     * @param vertexBuffer The VertexBuffer to be drawn
604     * @param paint The paint to render with
605     * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines)
606     */
607    status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
608            bool useOffset = false);
609
610    /**
611     * Renders the convex hull defined by the specified path as a strip of polygons.
612     *
613     * @param path The hull of the path to draw
614     * @param paint The paint to render with
615     */
616    status_t drawConvexPath(const SkPath& path, SkPaint* paint);
617
618    /**
619     * Draws a textured rectangle with the specified texture. The specified coordinates
620     * are transformed by the current snapshot's transform matrix.
621     *
622     * @param left The left coordinate of the rectangle
623     * @param top The top coordinate of the rectangle
624     * @param right The right coordinate of the rectangle
625     * @param bottom The bottom coordinate of the rectangle
626     * @param texture The texture name to map onto the rectangle
627     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
628     * @param mode The blending mode
629     * @param blend True if the texture contains an alpha channel
630     */
631    void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
632            float alpha, SkXfermode::Mode mode, bool blend);
633
634    /**
635     * Draws a textured rectangle with the specified texture. The specified coordinates
636     * are transformed by the current snapshot's transform matrix.
637     *
638     * @param left The left coordinate of the rectangle
639     * @param top The top coordinate of the rectangle
640     * @param right The right coordinate of the rectangle
641     * @param bottom The bottom coordinate of the rectangle
642     * @param texture The texture to use
643     * @param paint The paint containing the alpha, blending mode, etc.
644     */
645    void drawTextureRect(float left, float top, float right, float bottom,
646            Texture* texture, SkPaint* paint);
647
648    /**
649     * Draws a textured mesh with the specified texture. If the indices are omitted,
650     * the mesh is drawn as a simple quad. The mesh pointers become offsets when a
651     * VBO is bound.
652     *
653     * @param left The left coordinate of the rectangle
654     * @param top The top coordinate of the rectangle
655     * @param right The right coordinate of the rectangle
656     * @param bottom The bottom coordinate of the rectangle
657     * @param texture The texture name to map onto the rectangle
658     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
659     * @param mode The blending mode
660     * @param blend True if the texture contains an alpha channel
661     * @param vertices The vertices that define the mesh
662     * @param texCoords The texture coordinates of each vertex
663     * @param elementsCount The number of elements in the mesh, required by indices
664     * @param swapSrcDst Whether or not the src and dst blending operations should be swapped
665     * @param ignoreTransform True if the current transform should be ignored
666     * @param vbo The VBO used to draw the mesh
667     * @param ignoreScale True if the model view matrix should not be scaled
668     * @param dirty True if calling this method should dirty the current layer
669     */
670    void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
671            float alpha, SkXfermode::Mode mode, bool blend,
672            GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
673            bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0,
674            bool ignoreScale = false, bool dirty = true);
675
676    void drawAlpha8TextureMesh(float left, float top, float right, float bottom,
677            GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
678            GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
679            bool ignoreTransform, bool dirty = true);
680
681    /**
682     * Draws text underline and strike-through if needed.
683     *
684     * @param text The text to decor
685     * @param bytesCount The number of bytes in the text
686     * @param length The length in pixels of the text, can be <= 0.0f to force a measurement
687     * @param x The x coordinate where the text will be drawn
688     * @param y The y coordinate where the text will be drawn
689     * @param paint The paint to draw the text with
690     */
691    void drawTextDecorations(const char* text, int bytesCount, float length,
692            float x, float y, SkPaint* paint);
693
694   /**
695     * Draws shadow layer on text (with optional positions).
696     *
697     * @param paint The paint to draw the shadow with
698     * @param text The text to draw
699     * @param bytesCount The number of bytes in the text
700     * @param count The number of glyphs in the text
701     * @param positions The x, y positions of individual glyphs (or NULL)
702     * @param fontRenderer The font renderer object
703     * @param alpha The alpha value for drawing the shadow
704     * @param mode The xfermode for drawing the shadow
705     * @param x The x coordinate where the shadow will be drawn
706     * @param y The y coordinate where the shadow will be drawn
707     */
708    void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
709            const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
710            float x, float y);
711
712    /**
713     * Draws a path texture. Path textures are alpha8 bitmaps that need special
714     * compositing to apply colors/filters/etc.
715     *
716     * @param texture The texture to render
717     * @param x The x coordinate where the texture will be drawn
718     * @param y The y coordinate where the texture will be drawn
719     * @param paint The paint to draw the texture with
720     */
721     void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
722
723    /**
724     * Resets the texture coordinates stored in mMeshVertices. Setting the values
725     * back to default is achieved by calling:
726     *
727     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
728     *
729     * @param u1 The left coordinate of the texture
730     * @param v1 The bottom coordinate of the texture
731     * @param u2 The right coordinate of the texture
732     * @param v2 The top coordinate of the texture
733     */
734    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
735
736    /**
737     * Binds the specified texture. The texture unit must have been selected
738     * prior to calling this method.
739     */
740    inline void bindTexture(GLuint texture) {
741        glBindTexture(GL_TEXTURE_2D, texture);
742    }
743
744    /**
745     * Binds the specified EGLImage texture. The texture unit must have been selected
746     * prior to calling this method.
747     */
748    inline void bindExternalTexture(GLuint texture) {
749        glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
750    }
751
752    /**
753     * Enable or disable blending as necessary. This function sets the appropriate
754     * blend function based on the specified xfermode.
755     */
756    inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
757            bool swapSrcDst = false);
758
759    /**
760     * Use the specified program with the current GL context. If the program is already
761     * in use, it will not be bound again. If it is not in use, the current program is
762     * marked unused and the specified program becomes used and becomes the new
763     * current program.
764     *
765     * @param program The program to use
766     *
767     * @return true If the specified program was already in use, false otherwise.
768     */
769    inline bool useProgram(Program* program);
770
771    /**
772     * Invoked before any drawing operation. This sets required state.
773     */
774    void setupDraw(bool clear = true);
775
776    /**
777     * Various methods to setup OpenGL rendering.
778     */
779    void setupDrawWithTexture(bool isAlpha8 = false);
780    void setupDrawWithExternalTexture();
781    void setupDrawNoTexture();
782    void setupDrawAA();
783    void setupDrawPoint(float pointSize);
784    void setupDrawColor(int color, int alpha);
785    void setupDrawColor(float r, float g, float b, float a);
786    void setupDrawAlpha8Color(int color, int alpha);
787    void setupDrawTextGamma(const SkPaint* paint);
788    void setupDrawShader();
789    void setupDrawColorFilter();
790    void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
791            bool swapSrcDst = false);
792    void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
793            bool swapSrcDst = false);
794    void setupDrawProgram();
795    void setupDrawDirtyRegionsDisabled();
796    void setupDrawModelViewIdentity(bool offset = false);
797    void setupDrawModelView(float left, float top, float right, float bottom,
798            bool ignoreTransform = false, bool ignoreModelView = false);
799    void setupDrawModelViewTranslate(float left, float top, float right, float bottom,
800            bool ignoreTransform = false);
801    void setupDrawPointUniforms();
802    void setupDrawColorUniforms();
803    void setupDrawPureColorUniforms();
804    void setupDrawShaderIdentityUniforms();
805    void setupDrawShaderUniforms(bool ignoreTransform = false);
806    void setupDrawColorFilterUniforms();
807    void setupDrawSimpleMesh();
808    void setupDrawTexture(GLuint texture);
809    void setupDrawExternalTexture(GLuint texture);
810    void setupDrawTextureTransform();
811    void setupDrawTextureTransformUniforms(mat4& transform);
812    void setupDrawTextGammaUniforms();
813    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
814    void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
815    void setupDrawVertices(GLvoid* vertices);
816    void finishDrawTexture();
817    void accountForClear(SkXfermode::Mode mode);
818
819    bool updateLayer(Layer* layer, bool inFrame);
820    void updateLayers();
821
822    /**
823     * Renders the specified region as a series of rectangles. This method
824     * is used for debugging only.
825     */
826    void drawRegionRects(const Region& region);
827
828    /**
829     * Renders the specified region as a series of rectangles. The region
830     * must be in screen-space coordinates.
831     */
832    void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode,
833            bool dirty = false);
834
835    /**
836     * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
837     * is turned on.
838     */
839    void debugClip();
840
841    void debugOverdraw(bool enable, bool clear);
842    void renderOverdraw();
843
844    /**
845     * Should be invoked every time the glScissor is modified.
846     */
847    inline void dirtyClip() {
848        mDirtyClip = true;
849    }
850
851    // Dimensions of the drawing surface
852    int mWidth, mHeight;
853
854    // Matrix used for ortho projection in shaders
855    mat4 mOrthoMatrix;
856
857    // Model-view matrix used to position/size objects
858    mat4 mModelView;
859
860    // Number of saved states
861    int mSaveCount;
862    // Base state
863    sp<Snapshot> mFirstSnapshot;
864    // Current state
865    sp<Snapshot> mSnapshot;
866    // State used to define the clipping region
867    sp<Snapshot> mTilingSnapshot;
868
869    // Shaders
870    SkiaShader* mShader;
871
872    // Color filters
873    SkiaColorFilter* mColorFilter;
874
875    // Used to draw textured quads
876    TextureVertex mMeshVertices[4];
877
878    // Drop shadow
879    bool mHasShadow;
880    float mShadowRadius;
881    float mShadowDx;
882    float mShadowDy;
883    int mShadowColor;
884
885    // Draw filters
886    bool mHasDrawFilter;
887    int mPaintFilterClearBits;
888    int mPaintFilterSetBits;
889    SkPaint mFilteredPaint;
890
891    // Various caches
892    Caches& mCaches;
893    Extensions& mExtensions;
894
895    // List of rectangles to clear after saveLayer() is invoked
896    Vector<Rect*> mLayers;
897    // List of functors to invoke after a frame is drawn
898    SortedVector<Functor*> mFunctors;
899    // List of layers to update at the beginning of a frame
900    Vector<Layer*> mLayerUpdates;
901
902    // Indentity matrix
903    const mat4 mIdentity;
904
905    // Indicates whether the clip must be restored
906    bool mDirtyClip;
907
908    // The following fields are used to setup drawing
909    // Used to describe the shaders to generate
910    ProgramDescription mDescription;
911    // Color description
912    bool mColorSet;
913    float mColorA, mColorR, mColorG, mColorB;
914    // Indicates that the shader should get a color
915    bool mSetShaderColor;
916    // Current texture unit
917    GLuint mTextureUnit;
918    // Track dirty regions, true by default
919    bool mTrackDirtyRegions;
920    // Indicate whether we are drawing an opaque frame
921    bool mOpaqueFrame;
922
923    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
924    // Properties.h
925    bool mScissorOptimizationDisabled;
926
927    // No-ops start/endTiling when set
928    bool mSuppressTiling;
929
930    // Optional name of the renderer
931    String8 mName;
932
933    friend class DisplayListRenderer;
934
935}; // class OpenGLRenderer
936
937}; // namespace uirenderer
938}; // namespace android
939
940#endif // ANDROID_HWUI_OPENGL_RENDERER_H
941