OpenGLRenderer.h revision 408eb12631376cbdc96803e918decf6ea804d346
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     * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
553     */
554    void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
555            bool fboLayer, int alpha);
556
557    /**
558     * Creates a new layer stored in the specified snapshot.
559     *
560     * @param snapshot The snapshot associated with the new layer
561     * @param left The left coordinate of the layer
562     * @param top The top coordinate of the layer
563     * @param right The right coordinate of the layer
564     * @param bottom The bottom coordinate of the layer
565     * @param alpha The translucency of the layer
566     * @param mode The blending mode of the layer
567     * @param flags The layer save flags
568     * @param previousFbo The name of the current framebuffer
569     *
570     * @return True if the layer was successfully created, false otherwise
571     */
572    bool createLayer(float left, float top, float right, float bottom,
573            int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
574
575    /**
576     * Creates a new layer stored in the specified snapshot as an FBO.
577     *
578     * @param layer The layer to store as an FBO
579     * @param snapshot The snapshot associated with the new layer
580     * @param bounds The bounds of the layer
581     * @param previousFbo The name of the current framebuffer
582     */
583    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
584
585    /**
586     * Compose the specified layer as a region.
587     *
588     * @param layer The layer to compose
589     * @param rect The layer's bounds
590     */
591    void composeLayerRegion(Layer* layer, const Rect& rect);
592
593    /**
594     * Compose the specified layer as a simple rectangle.
595     *
596     * @param layer The layer to compose
597     * @param rect The layer's bounds
598     * @param swap If true, the source and destination are swapped
599     */
600    void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
601
602    /**
603     * Clears all the regions corresponding to the current list of layers.
604     * This method MUST be invoked before any drawing operation.
605     */
606    void clearLayerRegions();
607
608    /**
609     * Mark the layer as dirty at the specified coordinates. The coordinates
610     * are transformed with the supplied matrix.
611     */
612    void dirtyLayer(const float left, const float top,
613            const float right, const float bottom, const mat4 transform);
614
615    /**
616     * Mark the layer as dirty at the specified coordinates.
617     */
618    void dirtyLayer(const float left, const float top,
619            const float right, const float bottom);
620
621    /**
622     * Draws a colored rectangle with the specified color. The specified coordinates
623     * are transformed by the current snapshot's transform matrix unless specified
624     * otherwise.
625     *
626     * @param left The left coordinate of the rectangle
627     * @param top The top coordinate of the rectangle
628     * @param right The right coordinate of the rectangle
629     * @param bottom The bottom coordinate of the rectangle
630     * @param color The rectangle's ARGB color, defined as a packed 32 bits word
631     * @param mode The Skia xfermode to use
632     * @param ignoreTransform True if the current transform should be ignored
633     */
634    void drawColorRect(float left, float top, float right, float bottom,
635            int color, SkXfermode::Mode mode, bool ignoreTransform = false);
636
637    /**
638     * Draws a series of colored rectangles with the specified color. The specified
639     * coordinates are transformed by the current snapshot's transform matrix unless
640     * specified otherwise.
641     *
642     * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
643     *              per rectangle
644     * @param color The rectangles' ARGB color, defined as a packed 32 bits word
645     * @param mode The Skia xfermode to use
646     * @param ignoreTransform True if the current transform should be ignored
647     * @param dirty True if calling this method should dirty the current layer
648     * @param clip True if the rects should be clipped, false otherwise
649     */
650    status_t drawColorRects(const float* rects, int count, int color,
651            SkXfermode::Mode mode, bool ignoreTransform = false,
652            bool dirty = true, bool clip = true);
653
654    /**
655     * Draws the shape represented by the specified path texture.
656     * This method invokes drawPathTexture() but takes into account
657     * the extra left/top offset and the texture offset to correctly
658     * position the final shape.
659     *
660     * @param left The left coordinate of the shape to render
661     * @param top The top coordinate of the shape to render
662     * @param texture The texture reprsenting the shape
663     * @param paint The paint to draw the shape with
664     */
665    status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
666
667    /**
668     * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
669     * different compositing rules.
670     *
671     * @param texture The texture to draw with
672     * @param left The x coordinate of the bitmap
673     * @param top The y coordinate of the bitmap
674     * @param paint The paint to render with
675     */
676    void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
677
678    /**
679     * Renders a strip of polygons with the specified paint, used for tessellated geometry.
680     *
681     * @param vertexBuffer The VertexBuffer to be drawn
682     * @param paint The paint to render with
683     * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines)
684     */
685    status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
686            bool useOffset = false);
687
688    /**
689     * Renders the convex hull defined by the specified path as a strip of polygons.
690     *
691     * @param path The hull of the path to draw
692     * @param paint The paint to render with
693     */
694    status_t drawConvexPath(const SkPath& path, SkPaint* paint);
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 name to map onto the rectangle
705     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
706     * @param mode The blending mode
707     * @param blend True if the texture contains an alpha channel
708     */
709    void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
710            float alpha, SkXfermode::Mode mode, bool blend);
711
712    /**
713     * Draws a textured rectangle with the specified texture. The specified coordinates
714     * are transformed by the current snapshot's transform matrix.
715     *
716     * @param left The left coordinate of the rectangle
717     * @param top The top coordinate of the rectangle
718     * @param right The right coordinate of the rectangle
719     * @param bottom The bottom coordinate of the rectangle
720     * @param texture The texture to use
721     * @param paint The paint containing the alpha, blending mode, etc.
722     */
723    void drawTextureRect(float left, float top, float right, float bottom,
724            Texture* texture, SkPaint* paint);
725
726    /**
727     * Draws a textured mesh with the specified texture. If the indices are omitted,
728     * the mesh is drawn as a simple quad. The mesh pointers become offsets when a
729     * VBO is bound.
730     *
731     * @param left The left coordinate of the rectangle
732     * @param top The top coordinate of the rectangle
733     * @param right The right coordinate of the rectangle
734     * @param bottom The bottom coordinate of the rectangle
735     * @param texture The texture name to map onto the rectangle
736     * @param alpha An additional translucency parameter, between 0.0f and 1.0f
737     * @param mode The blending mode
738     * @param blend True if the texture contains an alpha channel
739     * @param vertices The vertices that define the mesh
740     * @param texCoords The texture coordinates of each vertex
741     * @param elementsCount The number of elements in the mesh, required by indices
742     * @param swapSrcDst Whether or not the src and dst blending operations should be swapped
743     * @param ignoreTransform True if the current transform should be ignored
744     * @param vbo The VBO used to draw the mesh
745     * @param ignoreScale True if the model view matrix should not be scaled
746     * @param dirty True if calling this method should dirty the current layer
747     */
748    void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
749            float alpha, SkXfermode::Mode mode, bool blend,
750            GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
751            bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0,
752            bool ignoreScale = false, bool dirty = true);
753
754    void drawAlpha8TextureMesh(float left, float top, float right, float bottom,
755            GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
756            GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
757            bool ignoreTransform, bool dirty = true);
758
759    /**
760     * Draws text underline and strike-through if needed.
761     *
762     * @param text The text to decor
763     * @param bytesCount The number of bytes in the text
764     * @param length The length in pixels of the text, can be <= 0.0f to force a measurement
765     * @param x The x coordinate where the text will be drawn
766     * @param y The y coordinate where the text will be drawn
767     * @param paint The paint to draw the text with
768     */
769    void drawTextDecorations(const char* text, int bytesCount, float length,
770            float x, float y, SkPaint* paint);
771
772   /**
773     * Draws shadow layer on text (with optional positions).
774     *
775     * @param paint The paint to draw the shadow with
776     * @param text The text to draw
777     * @param bytesCount The number of bytes in the text
778     * @param count The number of glyphs in the text
779     * @param positions The x, y positions of individual glyphs (or NULL)
780     * @param fontRenderer The font renderer object
781     * @param alpha The alpha value for drawing the shadow
782     * @param mode The xfermode for drawing the shadow
783     * @param x The x coordinate where the shadow will be drawn
784     * @param y The y coordinate where the shadow will be drawn
785     */
786    void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
787            const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
788            float x, float y);
789
790    /**
791     * Draws a path texture. Path textures are alpha8 bitmaps that need special
792     * compositing to apply colors/filters/etc.
793     *
794     * @param texture The texture to render
795     * @param x The x coordinate where the texture will be drawn
796     * @param y The y coordinate where the texture will be drawn
797     * @param paint The paint to draw the texture with
798     */
799     void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
800
801    /**
802     * Resets the texture coordinates stored in mMeshVertices. Setting the values
803     * back to default is achieved by calling:
804     *
805     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
806     *
807     * @param u1 The left coordinate of the texture
808     * @param v1 The bottom coordinate of the texture
809     * @param u2 The right coordinate of the texture
810     * @param v2 The top coordinate of the texture
811     */
812    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
813
814    /**
815     * Returns true if the specified paint will draw invisible text.
816     */
817    bool canSkipText(const SkPaint* paint) const;
818
819    /**
820     * Binds the specified texture. The texture unit must have been selected
821     * prior to calling this method.
822     */
823    inline void bindTexture(GLuint texture) {
824        glBindTexture(GL_TEXTURE_2D, texture);
825    }
826
827    /**
828     * Binds the specified EGLImage texture. The texture unit must have been selected
829     * prior to calling this method.
830     */
831    inline void bindExternalTexture(GLuint texture) {
832        glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
833    }
834
835    /**
836     * Enable or disable blending as necessary. This function sets the appropriate
837     * blend function based on the specified xfermode.
838     */
839    inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
840            bool swapSrcDst = false);
841
842    /**
843     * Use the specified program with the current GL context. If the program is already
844     * in use, it will not be bound again. If it is not in use, the current program is
845     * marked unused and the specified program becomes used and becomes the new
846     * current program.
847     *
848     * @param program The program to use
849     *
850     * @return true If the specified program was already in use, false otherwise.
851     */
852    inline bool useProgram(Program* program);
853
854    /**
855     * Invoked before any drawing operation. This sets required state.
856     */
857    void setupDraw(bool clear = true);
858
859    /**
860     * Various methods to setup OpenGL rendering.
861     */
862    void setupDrawWithTexture(bool isAlpha8 = false);
863    void setupDrawWithTextureAndColor(bool isAlpha8 = false);
864    void setupDrawWithExternalTexture();
865    void setupDrawNoTexture();
866    void setupDrawAA();
867    void setupDrawPoint(float pointSize);
868    void setupDrawColor(int color, int alpha);
869    void setupDrawColor(float r, float g, float b, float a);
870    void setupDrawAlpha8Color(int color, int alpha);
871    void setupDrawTextGamma(const SkPaint* paint);
872    void setupDrawShader();
873    void setupDrawColorFilter();
874    void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
875            bool swapSrcDst = false);
876    void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
877            bool swapSrcDst = false);
878    void setupDrawProgram();
879    void setupDrawDirtyRegionsDisabled();
880    void setupDrawModelViewIdentity(bool offset = false);
881    void setupDrawModelView(float left, float top, float right, float bottom,
882            bool ignoreTransform = false, bool ignoreModelView = false);
883    void setupDrawModelViewTranslate(float left, float top, float right, float bottom,
884            bool ignoreTransform = false);
885    void setupDrawPointUniforms();
886    void setupDrawColorUniforms();
887    void setupDrawPureColorUniforms();
888    void setupDrawShaderIdentityUniforms();
889    void setupDrawShaderUniforms(bool ignoreTransform = false);
890    void setupDrawColorFilterUniforms();
891    void setupDrawSimpleMesh();
892    void setupDrawTexture(GLuint texture);
893    void setupDrawExternalTexture(GLuint texture);
894    void setupDrawTextureTransform();
895    void setupDrawTextureTransformUniforms(mat4& transform);
896    void setupDrawTextGammaUniforms();
897    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
898    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors);
899    void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
900    void setupDrawVertices(GLvoid* vertices);
901    void finishDrawTexture();
902    void accountForClear(SkXfermode::Mode mode);
903
904    bool updateLayer(Layer* layer, bool inFrame);
905    void updateLayers();
906
907    /**
908     * Renders the specified region as a series of rectangles. This method
909     * is used for debugging only.
910     */
911    void drawRegionRects(const Region& region);
912
913    /**
914     * Renders the specified region as a series of rectangles. The region
915     * must be in screen-space coordinates.
916     */
917    void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode,
918            bool dirty = false);
919
920    /**
921     * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
922     * is turned on.
923     */
924    void debugClip();
925
926    void debugOverdraw(bool enable, bool clear);
927    void renderOverdraw();
928
929    /**
930     * Should be invoked every time the glScissor is modified.
931     */
932    inline void dirtyClip() {
933        mDirtyClip = true;
934    }
935
936    inline mat4& currentTransform() const {
937        return *mSnapshot->transform;
938    }
939
940    // Dimensions of the drawing surface
941    int mWidth, mHeight;
942
943    // Matrix used for ortho projection in shaders
944    mat4 mOrthoMatrix;
945
946    // Model-view matrix used to position/size objects
947    mat4 mModelView;
948
949    // Number of saved states
950    int mSaveCount;
951    // Base state
952    sp<Snapshot> mFirstSnapshot;
953    // Current state
954    sp<Snapshot> mSnapshot;
955    // State used to define the clipping region
956    Rect mTilingClip;
957
958    // Used to draw textured quads
959    TextureVertex mMeshVertices[4];
960
961    // shader, filters, and shadow
962    DrawModifiers mDrawModifiers;
963    SkPaint mFilteredPaint;
964
965    // Various caches
966    Caches& mCaches;
967    Extensions& mExtensions;
968
969    // List of rectangles to clear after saveLayer() is invoked
970    Vector<Rect*> mLayers;
971    // List of functors to invoke after a frame is drawn
972    SortedVector<Functor*> mFunctors;
973    // List of layers to update at the beginning of a frame
974    Vector<Layer*> mLayerUpdates;
975
976    // Indicates whether the clip must be restored
977    bool mDirtyClip;
978
979    // The following fields are used to setup drawing
980    // Used to describe the shaders to generate
981    ProgramDescription mDescription;
982    // Color description
983    bool mColorSet;
984    float mColorA, mColorR, mColorG, mColorB;
985    // Indicates that the shader should get a color
986    bool mSetShaderColor;
987    // Current texture unit
988    GLuint mTextureUnit;
989    // Track dirty regions, true by default
990    bool mTrackDirtyRegions;
991    // Indicate whether we are drawing an opaque frame
992    bool mOpaqueFrame;
993
994    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
995    // Properties.h
996    bool mScissorOptimizationDisabled;
997
998    // No-ops start/endTiling when set
999    bool mSuppressTiling;
1000
1001    // Optional name of the renderer
1002    String8 mName;
1003
1004    friend class DisplayListRenderer;
1005    friend class TextSetupFunctor;
1006
1007}; // class OpenGLRenderer
1008
1009}; // namespace uirenderer
1010}; // namespace android
1011
1012#endif // ANDROID_HWUI_OPENGL_RENDERER_H
1013