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