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