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