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