OpenGLRenderer.h revision 54c1a64d5441a964890b44280e4457e11f4f924a
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 int 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 int 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     * Call this method after updating a layer during a drawing pass.
274     */
275    void resumeAfterLayer();
276
277    /**
278     * Compose the layer defined in the current snapshot with the layer
279     * defined by the previous snapshot.
280     *
281     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
282     *
283     * @param curent The current snapshot containing the layer to compose
284     * @param previous The previous snapshot to compose the current layer with
285     */
286    virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
287
288    /**
289     * Marks the specified region as dirty at the specified bounds.
290     */
291    void dirtyLayerUnchecked(Rect& bounds, Region* region);
292
293    /**
294     * Returns the current snapshot.
295     */
296    sp<Snapshot> getSnapshot() {
297        return mSnapshot;
298    }
299
300    /**
301     * Returns the region of the current layer.
302     */
303    virtual Region* getRegion() {
304        return mSnapshot->region;
305    }
306
307    /**
308     * Indicates whether rendering is currently targeted at a layer.
309     */
310    virtual bool hasLayer() {
311        return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
312    }
313
314    /**
315     * Returns the name of the FBO this renderer is rendering into.
316     */
317    virtual GLint getTargetFbo() {
318        return 0;
319    }
320
321    /**
322     * Renders the specified layer as a textured quad.
323     *
324     * @param layer The layer to render
325     * @param rect The bounds of the layer
326     */
327    void drawTextureLayer(Layer* layer, const Rect& rect);
328
329    /**
330     * Gets the alpha and xfermode out of a paint object. If the paint is null
331     * alpha will be 255 and the xfermode will be SRC_OVER.
332     *
333     * @param paint The paint to extract values from
334     * @param alpha Where to store the resulting alpha
335     * @param mode Where to store the resulting xfermode
336     */
337    inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
338
339    /**
340     * Safely retrieves the mode from the specified xfermode. If the specified
341     * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
342     */
343    static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
344        SkXfermode::Mode resultMode;
345        if (!SkXfermode::AsMode(mode, &resultMode)) {
346            resultMode = SkXfermode::kSrcOver_Mode;
347        }
348        return resultMode;
349    }
350
351    /**
352     * Set to true to suppress error checks at the end of a frame.
353     */
354    virtual bool suppressErrorChecks() {
355        return false;
356    }
357
358private:
359    /**
360     * Ensures the state of the renderer is the same as the state of
361     * the GL context.
362     */
363    void syncState();
364
365    /**
366     * Tells the GPU what part of the screen is about to be redrawn.
367     * This method needs to be invoked every time getTargetFbo() is
368     * bound again.
369     */
370    void startTiling(const sp<Snapshot>& snapshot, bool opaque = false);
371
372    /**
373     * Tells the GPU that we are done drawing the frame or that we
374     * are switching to another render target.
375     */
376    void endTiling();
377
378    /**
379     * Saves the current state of the renderer as a new snapshot.
380     * The new snapshot is saved in mSnapshot and the previous snapshot
381     * is linked from mSnapshot->previous.
382     *
383     * @param flags The save flags; see SkCanvas for more information
384     *
385     * @return The new save count. This value can be passed to #restoreToCount()
386     */
387    int saveSnapshot(int flags);
388
389    /**
390     * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
391     *
392     * @return True if the clip was modified.
393     */
394    bool restoreSnapshot();
395
396    /**
397     * Sets the clipping rectangle using glScissor. The clip is defined by
398     * the current snapshot's clipRect member.
399     */
400    void setScissorFromClip();
401
402    /**
403     * Performs a quick reject but does not affect the scissor. Returns
404     * the transformed rect to test and the current clip.
405     */
406    bool quickRejectNoScissor(float left, float top, float right, float bottom,
407            Rect& transformed, Rect& clip);
408
409    /**
410     * Creates a new layer stored in the specified snapshot.
411     *
412     * @param snapshot The snapshot associated with the new layer
413     * @param left The left coordinate of the layer
414     * @param top The top coordinate of the layer
415     * @param right The right coordinate of the layer
416     * @param bottom The bottom coordinate of the layer
417     * @param alpha The translucency of the layer
418     * @param mode The blending mode of the layer
419     * @param flags The layer save flags
420     * @param previousFbo The name of the current framebuffer
421     *
422     * @return True if the layer was successfully created, false otherwise
423     */
424    bool createLayer(float left, float top, float right, float bottom,
425            int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
426
427    /**
428     * Creates a new layer stored in the specified snapshot as an FBO.
429     *
430     * @param layer The layer to store as an FBO
431     * @param snapshot The snapshot associated with the new layer
432     * @param bounds The bounds of the layer
433     * @param previousFbo The name of the current framebuffer
434     */
435    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
436
437    /**
438     * Compose the specified layer as a region.
439     *
440     * @param layer The layer to compose
441     * @param rect The layer's bounds
442     */
443    void composeLayerRegion(Layer* layer, const Rect& rect);
444
445    /**
446     * Compose the specified layer as a simple rectangle.
447     *
448     * @param layer The layer to compose
449     * @param rect The layer's bounds
450     * @param swap If true, the source and destination are swapped
451     */
452    void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
453
454    /**
455     * Clears all the regions corresponding to the current list of layers.
456     * This method MUST be invoked before any drawing operation.
457     */
458    void clearLayerRegions();
459
460    /**
461     * Mark the layer as dirty at the specified coordinates. The coordinates
462     * are transformed with the supplied matrix.
463     */
464    void dirtyLayer(const float left, const float top,
465            const float right, const float bottom, const mat4 transform);
466
467    /**
468     * Mark the layer as dirty at the specified coordinates.
469     */
470    void dirtyLayer(const float left, const float top,
471            const float right, const float bottom);
472
473    /**
474     * Draws a colored rectangle with the specified color. The specified coordinates
475     * are transformed by the current snapshot's transform matrix.
476     *
477     * @param left The left coordinate of the rectangle
478     * @param top The top coordinate of the rectangle
479     * @param right The right coordinate of the rectangle
480     * @param bottom The bottom coordinate of the rectangle
481     * @param color The rectangle's ARGB color, defined as a packed 32 bits word
482     * @param mode The Skia xfermode to use
483     * @param ignoreTransform True if the current transform should be ignored
484     */
485    void drawColorRect(float left, float top, float right, float bottom,
486            int color, SkXfermode::Mode mode, bool ignoreTransform = false);
487
488    /**
489     * Draws the shape represented by the specified path texture.
490     * This method invokes drawPathTexture() but takes into account
491     * the extra left/top offset and the texture offset to correctly
492     * position the final shape.
493     *
494     * @param left The left coordinate of the shape to render
495     * @param top The top coordinate of the shape to render
496     * @param texture The texture reprsenting the shape
497     * @param paint The paint to draw the shape with
498     */
499    status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
500
501    /**
502     * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
503     * different compositing rules.
504     *
505     * @param texture The texture to draw with
506     * @param left The x coordinate of the bitmap
507     * @param top The y coordinate of the bitmap
508     * @param paint The paint to render with
509     */
510    void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
511
512    /**
513     * Renders the convex hull defined by the specified path as a strip of polygons.
514     *
515     * @param path The hull of the path to draw
516     * @param color The color of the rect
517     * @param mode The blending mode to draw the path
518     * @param isAA True if the drawing should be anti-aliased
519     */
520    void drawConvexPath(const SkPath& path, int color, SkXfermode::Mode mode, bool isAA);
521
522    /**
523     * Draws a textured rectangle with the specified texture. The specified coordinates
524     * are transformed by the current snapshot's transform matrix.
525     *
526     * @param left The left coordinate of the rectangle
527     * @param top The top coordinate of the rectangle
528     * @param right The right coordinate of the rectangle
529     * @param bottom The bottom coordinate of the rectangle
530     * @param texture The texture name to map onto the rectangle
531     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
532     * @param mode The blending mode
533     * @param blend True if the texture contains an alpha channel
534     */
535    void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
536            float alpha, SkXfermode::Mode mode, bool blend);
537
538    /**
539     * Draws a textured rectangle with the specified texture. The specified coordinates
540     * are transformed by the current snapshot's transform matrix.
541     *
542     * @param left The left coordinate of the rectangle
543     * @param top The top coordinate of the rectangle
544     * @param right The right coordinate of the rectangle
545     * @param bottom The bottom coordinate of the rectangle
546     * @param texture The texture to use
547     * @param paint The paint containing the alpha, blending mode, etc.
548     */
549    void drawTextureRect(float left, float top, float right, float bottom,
550            Texture* texture, SkPaint* paint);
551
552    /**
553     * Draws a textured mesh with the specified texture. If the indices are omitted,
554     * the mesh is drawn as a simple quad. The mesh pointers become offsets when a
555     * VBO is bound.
556     *
557     * @param left The left coordinate of the rectangle
558     * @param top The top coordinate of the rectangle
559     * @param right The right coordinate of the rectangle
560     * @param bottom The bottom coordinate of the rectangle
561     * @param texture The texture name to map onto the rectangle
562     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
563     * @param mode The blending mode
564     * @param blend True if the texture contains an alpha channel
565     * @param vertices The vertices that define the mesh
566     * @param texCoords The texture coordinates of each vertex
567     * @param elementsCount The number of elements in the mesh, required by indices
568     * @param swapSrcDst Whether or not the src and dst blending operations should be swapped
569     * @param ignoreTransform True if the current transform should be ignored
570     * @param vbo The VBO used to draw the mesh
571     * @param ignoreScale True if the model view matrix should not be scaled
572     * @param dirty True if calling this method should dirty the current layer
573     */
574    void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
575            float alpha, SkXfermode::Mode mode, bool blend,
576            GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
577            bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0,
578            bool ignoreScale = false, bool dirty = true);
579
580    /**
581     * Draws text underline and strike-through if needed.
582     *
583     * @param text The text to decor
584     * @param bytesCount The number of bytes in the text
585     * @param length The length in pixels of the text, can be <= 0.0f to force a measurement
586     * @param x The x coordinate where the text will be drawn
587     * @param y The y coordinate where the text will be drawn
588     * @param paint The paint to draw the text with
589     */
590    void drawTextDecorations(const char* text, int bytesCount, float length,
591            float x, float y, SkPaint* paint);
592
593   /**
594     * Draws shadow layer on text (with optional positions).
595     *
596     * @param paint The paint to draw the shadow with
597     * @param text The text to draw
598     * @param bytesCount The number of bytes in the text
599     * @param count The number of glyphs in the text
600     * @param positions The x, y positions of individual glyphs (or NULL)
601     * @param fontRenderer The font renderer object
602     * @param alpha The alpha value for drawing the shadow
603     * @param mode The xfermode for drawing the shadow
604     * @param x The x coordinate where the shadow will be drawn
605     * @param y The y coordinate where the shadow will be drawn
606     */
607    void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
608            const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
609            float x, float y);
610
611    /**
612     * Draws a path texture. Path textures are alpha8 bitmaps that need special
613     * compositing to apply colors/filters/etc.
614     *
615     * @param texture The texture to render
616     * @param x The x coordinate where the texture will be drawn
617     * @param y The y coordinate where the texture will be drawn
618     * @param paint The paint to draw the texture with
619     */
620     void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
621
622    /**
623     * Resets the texture coordinates stored in mMeshVertices. Setting the values
624     * back to default is achieved by calling:
625     *
626     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
627     *
628     * @param u1 The left coordinate of the texture
629     * @param v1 The bottom coordinate of the texture
630     * @param u2 The right coordinate of the texture
631     * @param v2 The top coordinate of the texture
632     */
633    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
634
635    /**
636     * Binds the specified texture. The texture unit must have been selected
637     * prior to calling this method.
638     */
639    inline void bindTexture(GLuint texture) {
640        glBindTexture(GL_TEXTURE_2D, texture);
641    }
642
643    /**
644     * Binds the specified EGLImage texture. The texture unit must have been selected
645     * prior to calling this method.
646     */
647    inline void bindExternalTexture(GLuint texture) {
648        glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
649    }
650
651    /**
652     * Enable or disable blending as necessary. This function sets the appropriate
653     * blend function based on the specified xfermode.
654     */
655    inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
656            bool swapSrcDst = false);
657
658    /**
659     * Use the specified program with the current GL context. If the program is already
660     * in use, it will not be bound again. If it is not in use, the current program is
661     * marked unused and the specified program becomes used and becomes the new
662     * current program.
663     *
664     * @param program The program to use
665     *
666     * @return true If the specified program was already in use, false otherwise.
667     */
668    inline bool useProgram(Program* program);
669
670    /**
671     * Invoked before any drawing operation. This sets required state.
672     */
673    void setupDraw(bool clear = true);
674
675    /**
676     * Various methods to setup OpenGL rendering.
677     */
678    void setupDrawWithTexture(bool isAlpha8 = false);
679    void setupDrawWithExternalTexture();
680    void setupDrawNoTexture();
681    void setupDrawAA();
682    void setupDrawVertexShape();
683    void setupDrawPoint(float pointSize);
684    void setupDrawColor(int color);
685    void setupDrawColor(int color, int alpha);
686    void setupDrawColor(float r, float g, float b, float a);
687    void setupDrawAlpha8Color(int color, int alpha);
688    void setupDrawTextGamma(const SkPaint* paint);
689    void setupDrawShader();
690    void setupDrawColorFilter();
691    void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
692            bool swapSrcDst = false);
693    void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
694            bool swapSrcDst = false);
695    void setupDrawProgram();
696    void setupDrawDirtyRegionsDisabled();
697    void setupDrawModelViewIdentity(bool offset = false);
698    void setupDrawModelView(float left, float top, float right, float bottom,
699            bool ignoreTransform = false, bool ignoreModelView = false);
700    void setupDrawModelViewTranslate(float left, float top, float right, float bottom,
701            bool ignoreTransform = false);
702    void setupDrawPointUniforms();
703    void setupDrawColorUniforms();
704    void setupDrawPureColorUniforms();
705    void setupDrawShaderIdentityUniforms();
706    void setupDrawShaderUniforms(bool ignoreTransform = false);
707    void setupDrawColorFilterUniforms();
708    void setupDrawSimpleMesh();
709    void setupDrawTexture(GLuint texture);
710    void setupDrawExternalTexture(GLuint texture);
711    void setupDrawTextureTransform();
712    void setupDrawTextureTransformUniforms(mat4& transform);
713    void setupDrawTextGammaUniforms();
714    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
715    void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
716    void setupDrawVertices(GLvoid* vertices);
717    void setupDrawAALine(GLvoid* vertices, GLvoid* distanceCoords, GLvoid* lengthCoords,
718            float strokeWidth, int& widthSlot, int& lengthSlot);
719    void finishDrawAALine(const int widthSlot, const int lengthSlot);
720    void finishDrawTexture();
721    void accountForClear(SkXfermode::Mode mode);
722
723    bool updateLayer(Layer* layer, bool inFrame);
724    void updateLayers();
725
726    /**
727     * Renders the specified region as a series of rectangles. This method
728     * is used for debugging only.
729     */
730    void drawRegionRects(const Region& region);
731
732    void debugOverdraw(bool enable, bool clear);
733    void renderOverdraw();
734
735    /**
736     * Should be invoked every time the glScissor is modified.
737     */
738    inline void dirtyClip() {
739        mDirtyClip = true;
740    }
741
742    // Dimensions of the drawing surface
743    int mWidth, mHeight;
744
745    // Matrix used for ortho projection in shaders
746    mat4 mOrthoMatrix;
747
748    // Model-view matrix used to position/size objects
749    mat4 mModelView;
750
751    // Number of saved states
752    int mSaveCount;
753    // Base state
754    sp<Snapshot> mFirstSnapshot;
755    // Current state
756    sp<Snapshot> mSnapshot;
757    // State used to define the clipping region
758    sp<Snapshot> mTilingSnapshot;
759
760    // Shaders
761    SkiaShader* mShader;
762
763    // Color filters
764    SkiaColorFilter* mColorFilter;
765
766    // Used to draw textured quads
767    TextureVertex mMeshVertices[4];
768
769    // Drop shadow
770    bool mHasShadow;
771    float mShadowRadius;
772    float mShadowDx;
773    float mShadowDy;
774    int mShadowColor;
775
776    // Draw filters
777    bool mHasDrawFilter;
778    int mPaintFilterClearBits;
779    int mPaintFilterSetBits;
780    SkPaint mFilteredPaint;
781
782    // Various caches
783    Caches& mCaches;
784
785    // List of rectangles to clear after saveLayer() is invoked
786    Vector<Rect*> mLayers;
787    // List of functors to invoke after a frame is drawn
788    SortedVector<Functor*> mFunctors;
789    // List of layers to update at the beginning of a frame
790    Vector<Layer*> mLayerUpdates;
791
792    // Indentity matrix
793    const mat4 mIdentity;
794
795    // Indicates whether the clip must be restored
796    bool mDirtyClip;
797
798    // The following fields are used to setup drawing
799    // Used to describe the shaders to generate
800    ProgramDescription mDescription;
801    // Color description
802    bool mColorSet;
803    float mColorA, mColorR, mColorG, mColorB;
804    // Indicates that the shader should get a color
805    bool mSetShaderColor;
806    // Current texture unit
807    GLuint mTextureUnit;
808    // Track dirty regions, true by default
809    bool mTrackDirtyRegions;
810    // Indicate whether we are drawing an opaque frame
811    bool mOpaqueFrame;
812
813    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
814    // Properties.h
815    bool mScissorOptimizationDisabled;
816
817    // No-ops start/endTiling when set
818    bool mSuppressTiling;
819
820    friend class DisplayListRenderer;
821
822}; // class OpenGLRenderer
823
824}; // namespace uirenderer
825}; // namespace android
826
827#endif // ANDROID_HWUI_OPENGL_RENDERER_H
828