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