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