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