OpenGLRenderer.h revision d35dcb13115ca1dd8c07e397f43a186cd7fd1a01
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 "CanvasState.h"
21#include "Debug.h"
22#include "Extensions.h"
23#include "Matrix.h"
24#include "Program.h"
25#include "Rect.h"
26#include "Snapshot.h"
27#include "UvMapper.h"
28#include "Vertex.h"
29#include "Caches.h"
30#include "utils/PaintUtils.h"
31
32#include <GLES2/gl2.h>
33#include <GLES2/gl2ext.h>
34
35#include <SkBitmap.h>
36#include <SkCanvas.h>
37#include <SkColorFilter.h>
38#include <SkMatrix.h>
39#include <SkPaint.h>
40#include <SkRegion.h>
41#include <SkXfermode.h>
42
43#include <utils/Blur.h>
44#include <utils/Functor.h>
45#include <utils/RefBase.h>
46#include <utils/SortedVector.h>
47
48#include <cutils/compiler.h>
49
50#include <androidfw/ResourceTypes.h>
51
52#include <vector>
53
54class SkShader;
55
56namespace android {
57namespace uirenderer {
58
59enum class DrawOpMode {
60    kImmediate,
61    kDefer,
62    kFlush
63};
64
65class DeferredDisplayState;
66struct Glop;
67class RenderState;
68class RenderNode;
69class TextDrawFunctor;
70class VertexBuffer;
71
72enum StateDeferFlags {
73    kStateDeferFlag_Draw = 0x1,
74    kStateDeferFlag_Clip = 0x2
75};
76
77enum ClipSideFlags {
78    kClipSide_None = 0x0,
79    kClipSide_Left = 0x1,
80    kClipSide_Top = 0x2,
81    kClipSide_Right = 0x4,
82    kClipSide_Bottom = 0x8,
83    kClipSide_Full = 0xF,
84    kClipSide_ConservativeFull = 0x1F
85};
86
87enum VertexBufferDisplayFlags {
88    kVertexBuffer_Offset = 0x1,
89    kVertexBuffer_ShadowInterp = 0x2,
90};
91
92/**
93 * Defines additional transformation that should be applied by the model view matrix, beyond that of
94 * the currentTransform()
95 */
96enum ModelViewMode {
97    /**
98     * Used when the model view should simply translate geometry passed to the shader. The resulting
99     * matrix will be a simple translation.
100     */
101    kModelViewMode_Translate = 0,
102
103    /**
104     * Used when the model view should translate and scale geometry. The resulting matrix will be a
105     * translation + scale. This is frequently used together with VBO 0, the (0,0,1,1) rect.
106     */
107    kModelViewMode_TranslateAndScale = 1,
108};
109
110///////////////////////////////////////////////////////////////////////////////
111// Renderer
112///////////////////////////////////////////////////////////////////////////////
113/**
114 * OpenGL Renderer implementation.
115 */
116class OpenGLRenderer : public CanvasStateClient {
117public:
118    OpenGLRenderer(RenderState& renderState);
119    virtual ~OpenGLRenderer();
120
121    /**
122     * Sets the dimension of the underlying drawing surface. This method must
123     * be called at least once every time the drawing surface changes size.
124     *
125     * @param width The width in pixels of the underlysing surface
126     * @param height The height in pixels of the underlysing surface
127     */
128    void setViewport(int width, int height) { mState.setViewport(width, height); }
129
130    void initProperties();
131    void initLight(float lightRadius, uint8_t ambientShadowAlpha,
132            uint8_t spotShadowAlpha);
133    void setLightCenter(const Vector3& lightCenter);
134
135    /*
136     * Prepares the renderer to draw a frame. This method must be invoked
137     * at the beginning of each frame. Only the specified rectangle of the
138     * frame is assumed to be dirty. A clip will automatically be set to
139     * the specified rectangle.
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    virtual void prepareDirty(float left, float top, float right, float bottom,
146            bool opaque);
147
148    /**
149     * Prepares the renderer to draw a frame. This method must be invoked
150     * at the beginning of each frame. When this method is invoked, the
151     * entire drawing surface is assumed to be redrawn.
152     *
153     * @param opaque If true, the target surface is considered opaque
154     *               and will not be cleared. If false, the target surface
155     *               will be cleared
156     */
157    void prepare(bool opaque) {
158        prepareDirty(0.0f, 0.0f, mState.getWidth(), mState.getHeight(), opaque);
159    }
160
161    /**
162     * Indicates the end of a frame. This method must be invoked whenever
163     * the caller is done rendering a frame.
164     * Returns true if any drawing was done during the frame (the output
165     * has changed / is "dirty" and should be displayed to the user).
166     */
167    virtual bool finish();
168
169    void callDrawGLFunction(Functor* functor, Rect& dirty);
170
171    void pushLayerUpdate(Layer* layer);
172    void cancelLayerUpdate(Layer* layer);
173    void flushLayerUpdates();
174    void markLayersAsBuildLayers();
175
176    virtual int saveLayer(float left, float top, float right, float bottom,
177            const SkPaint* paint, int flags) {
178        return saveLayer(left, top, right, bottom, paint, flags, nullptr);
179    }
180
181    // Specialized saveLayer implementation, which will pass the convexMask to an FBO layer, if
182    // created, which will in turn clip to that mask when drawn back/restored.
183    int saveLayer(float left, float top, float right, float bottom,
184            const SkPaint* paint, int flags, const SkPath* convexMask);
185
186    int saveLayerDeferred(float left, float top, float right, float bottom,
187            const SkPaint* paint, int flags);
188
189    void drawRenderNode(RenderNode* displayList, Rect& dirty, int32_t replayFlags = 1);
190    void drawLayer(Layer* layer, float x, float y);
191    void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint);
192    void drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
193            TextureVertex* vertices, bool pureTranslate, const Rect& bounds, const SkPaint* paint);
194    void drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst,
195            const SkPaint* paint);
196    void drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
197            const float* vertices, const int* colors, const SkPaint* paint);
198    void drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
199            TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint);
200    void drawPatch(const SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
201            float left, float top, float right, float bottom, const SkPaint* paint);
202    void drawColor(int color, SkXfermode::Mode mode);
203    void drawRect(float left, float top, float right, float bottom,
204            const SkPaint* paint);
205    void drawRoundRect(float left, float top, float right, float bottom,
206            float rx, float ry, const SkPaint* paint);
207    void drawCircle(float x, float y, float radius, const SkPaint* paint);
208    void drawOval(float left, float top, float right, float bottom,
209            const SkPaint* paint);
210    void drawArc(float left, float top, float right, float bottom,
211            float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint);
212    void drawPath(const SkPath* path, const SkPaint* paint);
213    void drawLines(const float* points, int count, const SkPaint* paint);
214    void drawPoints(const float* points, int count, const SkPaint* paint);
215    void drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
216            float hOffset, float vOffset, const SkPaint* paint);
217    void drawPosText(const char* text, int bytesCount, int count,
218            const float* positions, const SkPaint* paint);
219    void drawText(const char* text, int bytesCount, int count, float x, float y,
220            const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
221            DrawOpMode drawOpMode = DrawOpMode::kImmediate);
222    void drawRects(const float* rects, int count, const SkPaint* paint);
223
224    void drawShadow(float casterAlpha,
225            const VertexBuffer* ambientShadowVertexBuffer,
226            const VertexBuffer* spotShadowVertexBuffer);
227
228    void setDrawFilter(SkDrawFilter* filter);
229
230    /**
231     * Store the current display state (most importantly, the current clip and transform), and
232     * additionally map the state's bounds from local to window coordinates.
233     *
234     * Returns true if quick-rejected
235     */
236    bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags);
237    void restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore = false);
238    void setupMergedMultiDraw(const Rect* clipRect);
239
240    bool isCurrentTransformSimple() {
241        return currentTransform()->isSimple();
242    }
243
244    Caches& getCaches() {
245        return mCaches;
246    }
247
248    RenderState& renderState() {
249        return mRenderState;
250    }
251
252    int getViewportWidth() { return mState.getViewportWidth(); }
253    int getViewportHeight() { return mState.getViewportHeight(); }
254
255    /**
256     * Scales the alpha on the current snapshot. This alpha value will be modulated
257     * with other alpha values when drawing primitives.
258     */
259    void scaleAlpha(float alpha) { mState.scaleAlpha(alpha); }
260
261    /**
262     * Inserts a named event marker in the stream of GL commands.
263     */
264    void eventMark(const char* name) const;
265
266    /**
267     * Inserts a formatted event marker in the stream of GL commands.
268     */
269    void eventMarkDEBUG(const char *fmt, ...) const;
270
271    /**
272     * Inserts a named group marker in the stream of GL commands. This marker
273     * can be used by tools to group commands into logical groups. A call to
274     * this method must always be followed later on by a call to endMark().
275     */
276    void startMark(const char* name) const;
277
278    /**
279     * Closes the last group marker opened by startMark().
280     */
281    void endMark() const;
282
283    /**
284     * Gets the alpha and xfermode out of a paint object. If the paint is null
285     * alpha will be 255 and the xfermode will be SRC_OVER. This method does
286     * not multiply the paint's alpha by the current snapshot's alpha, and does
287     * not replace the alpha with the overrideLayerAlpha
288     *
289     * @param paint The paint to extract values from
290     * @param alpha Where to store the resulting alpha
291     * @param mode Where to store the resulting xfermode
292     */
293    static inline void getAlphaAndModeDirect(const SkPaint* paint, int* alpha,
294            SkXfermode::Mode* mode) {
295        *mode = getXfermodeDirect(paint);
296        *alpha = getAlphaDirect(paint);
297    }
298
299    static inline SkXfermode::Mode getXfermodeDirect(const SkPaint* paint) {
300        if (!paint) return SkXfermode::kSrcOver_Mode;
301        return PaintUtils::getXfermode(paint->getXfermode());
302    }
303
304    static inline int getAlphaDirect(const SkPaint* paint) {
305        if (!paint) return 255;
306        return paint->getAlpha();
307    }
308
309    struct TextShadow {
310        SkScalar radius;
311        float dx;
312        float dy;
313        SkColor color;
314    };
315
316    static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
317        SkDrawLooper::BlurShadowRec blur;
318        if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
319            if (textShadow) {
320                textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
321                textShadow->dx = blur.fOffset.fX;
322                textShadow->dy = blur.fOffset.fY;
323                textShadow->color = blur.fColor;
324            }
325            return true;
326        }
327        return false;
328    }
329
330    static inline bool hasTextShadow(const SkPaint* paint) {
331        return getTextShadow(paint, nullptr);
332    }
333
334    /**
335     * Build the best transform to use to rasterize text given a full
336     * transform matrix, and whether filteration is needed.
337     *
338     * Returns whether filtration is needed
339     */
340    bool findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const;
341
342#if DEBUG_MERGE_BEHAVIOR
343    void drawScreenSpaceColorRect(float left, float top, float right, float bottom, int color) {
344        mCaches.setScissorEnabled(false);
345
346        // should only be called outside of other draw ops, so stencil can only be in test state
347        bool stencilWasEnabled = mCaches.stencil.isTestEnabled();
348        mCaches.stencil.disable();
349
350        drawColorRect(left, top, right, bottom, color, SkXfermode::kSrcOver_Mode, true);
351
352        if (stencilWasEnabled) mCaches.stencil.enableTest();
353        mDirty = true;
354    }
355#endif
356
357    const Vector3& getLightCenter() const { return mState.currentLightCenter(); }
358    float getLightRadius() const { return mLightRadius; }
359    uint8_t getAmbientShadowAlpha() const { return mAmbientShadowAlpha; }
360    uint8_t getSpotShadowAlpha() const { return mSpotShadowAlpha; }
361
362    ///////////////////////////////////////////////////////////////////
363    /// State manipulation
364
365    int getSaveCount() const;
366    int save(int flags);
367    void restore();
368    void restoreToCount(int saveCount);
369
370    void getMatrix(SkMatrix* outMatrix) const { mState.getMatrix(outMatrix); }
371    void setMatrix(const SkMatrix& matrix) { mState.setMatrix(matrix); }
372    void setLocalMatrix(const SkMatrix& matrix);
373    void concatMatrix(const SkMatrix& matrix) { mState.concatMatrix(matrix); }
374
375    void translate(float dx, float dy, float dz = 0.0f);
376    void rotate(float degrees);
377    void scale(float sx, float sy);
378    void skew(float sx, float sy);
379
380    void setMatrix(const Matrix4& matrix); // internal only convenience method
381    void concatMatrix(const Matrix4& matrix); // internal only convenience method
382
383    const Rect& getLocalClipBounds() const { return mState.getLocalClipBounds(); }
384    const Rect& getRenderTargetClipBounds() const { return mState.getRenderTargetClipBounds(); }
385    bool quickRejectConservative(float left, float top,
386            float right, float bottom) const {
387        return mState.quickRejectConservative(left, top, right, bottom);
388    }
389
390    bool clipRect(float left, float top,
391            float right, float bottom, SkRegion::Op op);
392    bool clipPath(const SkPath* path, SkRegion::Op op);
393    bool clipRegion(const SkRegion* region, SkRegion::Op op);
394
395    /**
396     * Does not support different clipping Ops (that is, every call to setClippingOutline is
397     * effectively using SkRegion::kReplaceOp)
398     *
399     * The clipping outline is independent from the regular clip.
400     */
401    void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
402    void setClippingRoundRect(LinearAllocator& allocator,
403            const Rect& rect, float radius, bool highPriority = true);
404    void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
405
406    inline bool hasRectToRectTransform() const { return mState.hasRectToRectTransform(); }
407    inline const mat4* currentTransform() const { return mState.currentTransform(); }
408
409    ///////////////////////////////////////////////////////////////////
410    /// CanvasStateClient interface
411
412    virtual void onViewportInitialized() override;
413    virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
414    virtual GLuint getTargetFbo() const override { return 0; }
415
416    SkPath* allocPathForFrame() {
417        std::unique_ptr<SkPath> path(new SkPath());
418        SkPath* returnPath = path.get();
419        mTempPaths.push_back(std::move(path));
420        return returnPath;
421    }
422
423    void setBaseTransform(const Matrix4& matrix) { mBaseTransform = matrix; }
424
425protected:
426    /**
427     * Perform the setup specific to a frame. This method does not
428     * issue any OpenGL commands.
429     */
430    void setupFrameState(float left, float top, float right, float bottom, bool opaque);
431
432    /**
433     * Indicates the start of rendering. This method will setup the
434     * initial OpenGL state (viewport, clearing the buffer, etc.)
435     */
436    void startFrame();
437
438    /**
439     * Clears the underlying surface if needed.
440     */
441    virtual void clear(float left, float top, float right, float bottom, bool opaque);
442
443    /**
444     * Call this method after updating a layer during a drawing pass.
445     */
446    void resumeAfterLayer();
447
448    /**
449     * This method is called whenever a stencil buffer is required. Subclasses
450     * should override this method and call attachStencilBufferToLayer() on the
451     * appropriate layer(s).
452     */
453    virtual void ensureStencilBuffer();
454
455    /**
456     * Obtains a stencil render buffer (allocating it if necessary) and
457     * attaches it to the specified layer.
458     */
459    void attachStencilBufferToLayer(Layer* layer);
460
461    /**
462     * Draw a rectangle list. Currently only used for the the stencil buffer so that the stencil
463     * will have a value of 'n' in every unclipped pixel, where 'n' is the number of rectangles
464     * in the list.
465     */
466    void drawRectangleList(const RectangleList& rectangleList);
467
468    bool quickRejectSetupScissor(float left, float top, float right, float bottom,
469            const SkPaint* paint = nullptr);
470    bool quickRejectSetupScissor(const Rect& bounds, const SkPaint* paint = nullptr) {
471        return quickRejectSetupScissor(bounds.left, bounds.top,
472                bounds.right, bounds.bottom, paint);
473    }
474
475    /**
476     * Compose the layer defined in the current snapshot with the layer
477     * defined by the previous snapshot.
478     *
479     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
480     *
481     * @param curent The current snapshot containing the layer to compose
482     * @param previous The previous snapshot to compose the current layer with
483     */
484    virtual void composeLayer(const Snapshot& current, const Snapshot& previous);
485
486    /**
487     * Marks the specified region as dirty at the specified bounds.
488     */
489    void dirtyLayerUnchecked(Rect& bounds, Region* region);
490
491    /**
492     * Returns the region of the current layer.
493     */
494    virtual Region* getRegion() const {
495        return mState.currentRegion();
496    }
497
498    /**
499     * Indicates whether rendering is currently targeted at a layer.
500     */
501    virtual bool hasLayer() const {
502        return (mState.currentFlags() & Snapshot::kFlagFboTarget) && mState.currentRegion();
503    }
504
505    /**
506     * Renders the specified layer as a textured quad.
507     *
508     * @param layer The layer to render
509     * @param rect The bounds of the layer
510     */
511    void drawTextureLayer(Layer* layer, const Rect& rect);
512
513    /**
514     * Gets the alpha and xfermode out of a paint object. If the paint is null
515     * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for snapshot alpha.
516     *
517     * @param paint The paint to extract values from
518     * @param alpha Where to store the resulting alpha
519     * @param mode Where to store the resulting xfermode
520     */
521    inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const;
522
523    /**
524     * Gets the alpha from a layer, accounting for snapshot alpha
525     *
526     * @param layer The layer from which the alpha is extracted
527     */
528    inline float getLayerAlpha(const Layer* layer) const;
529
530    /**
531     * Set to true to suppress error checks at the end of a frame.
532     */
533    virtual bool suppressErrorChecks() const {
534        return false;
535    }
536
537    CanvasState mState;
538    Caches& mCaches;
539    RenderState& mRenderState;
540
541private:
542    enum class GlopRenderType {
543        Standard,
544        Multi,
545        LayerClear
546    };
547
548    void renderGlop(const Glop& glop, GlopRenderType type = GlopRenderType::Standard);
549
550    /**
551     * Discards the content of the framebuffer if supported by the driver.
552     * This method should be called at the beginning of a frame to optimize
553     * rendering on some tiler architectures.
554     */
555    void discardFramebuffer(float left, float top, float right, float bottom);
556
557    /**
558     * Sets the clipping rectangle using glScissor. The clip is defined by
559     * the current snapshot's clipRect member.
560     */
561    void setScissorFromClip();
562
563    /**
564     * Sets the clipping region using the stencil buffer. The clip region
565     * is defined by the current snapshot's clipRegion member.
566     */
567    void setStencilFromClip();
568
569    /**
570     * Given the local bounds of the layer, calculates ...
571     */
572    void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
573
574    /**
575     * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
576     */
577    void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
578            bool fboLayer, int alpha);
579
580    /**
581     * Creates a new layer stored in the specified snapshot.
582     *
583     * @param snapshot The snapshot associated with the new layer
584     * @param left The left coordinate of the layer
585     * @param top The top coordinate of the layer
586     * @param right The right coordinate of the layer
587     * @param bottom The bottom coordinate of the layer
588     * @param alpha The translucency of the layer
589     * @param mode The blending mode of the layer
590     * @param flags The layer save flags
591     * @param mask A mask to use when drawing the layer back, may be empty
592     *
593     * @return True if the layer was successfully created, false otherwise
594     */
595    bool createLayer(float left, float top, float right, float bottom,
596            const SkPaint* paint, int flags, const SkPath* convexMask);
597
598    /**
599     * Creates a new layer stored in the specified snapshot as an FBO.
600     *
601     * @param layer The layer to store as an FBO
602     * @param snapshot The snapshot associated with the new layer
603     * @param bounds The bounds of the layer
604     */
605    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip);
606
607    /**
608     * Compose the specified layer as a region.
609     *
610     * @param layer The layer to compose
611     * @param rect The layer's bounds
612     */
613    void composeLayerRegion(Layer* layer, const Rect& rect);
614
615    /**
616     * Restores the content in layer to the screen, swapping the blend mode,
617     * specifically used in the restore() of a saveLayerAlpha().
618     *
619     * This allows e.g. a layer that would have been drawn on top of existing content (with SrcOver)
620     * to be drawn underneath.
621     *
622     * This will always ignore the canvas transform.
623     */
624    void composeLayerRectSwapped(Layer* layer, const Rect& rect);
625
626    /**
627     * Draws the content in layer to the screen.
628     */
629    void composeLayerRect(Layer* layer, const Rect& rect);
630
631    /**
632     * Clears all the regions corresponding to the current list of layers.
633     * This method MUST be invoked before any drawing operation.
634     */
635    void clearLayerRegions();
636
637    /**
638     * Mark the layer as dirty at the specified coordinates. The coordinates
639     * are transformed with the supplied matrix.
640     */
641    void dirtyLayer(const float left, const float top,
642            const float right, const float bottom, const Matrix4& transform);
643
644    /**
645     * Mark the layer as dirty at the specified coordinates.
646     */
647    void dirtyLayer(const float left, const float top,
648            const float right, const float bottom);
649
650    /**
651     * Draws a colored rectangle with the specified color. The specified coordinates
652     * are transformed by the current snapshot's transform matrix unless specified
653     * otherwise.
654     *
655     * @param left The left coordinate of the rectangle
656     * @param top The top coordinate of the rectangle
657     * @param right The right coordinate of the rectangle
658     * @param bottom The bottom coordinate of the rectangle
659     * @param paint The paint containing the color, blending mode, etc.
660     * @param ignoreTransform True if the current transform should be ignored
661     */
662    void drawColorRect(float left, float top, float right, float bottom,
663            const SkPaint* paint, bool ignoreTransform = false);
664
665    /**
666     * Draws a series of colored rectangles with the specified color. The specified
667     * coordinates are transformed by the current snapshot's transform matrix unless
668     * specified otherwise.
669     *
670     * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
671     *              per rectangle
672     * @param paint The paint containing the color, blending mode, etc.
673     * @param ignoreTransform True if the current transform should be ignored
674     * @param dirty True if calling this method should dirty the current layer
675     * @param clip True if the rects should be clipped, false otherwise
676     */
677    void drawColorRects(const float* rects, int count, const SkPaint* paint,
678            bool ignoreTransform = false, bool dirty = true, bool clip = true);
679
680    /**
681     * Draws the shape represented by the specified path texture.
682     * This method invokes drawPathTexture() but takes into account
683     * the extra left/top offset and the texture offset to correctly
684     * position the final shape.
685     *
686     * @param left The left coordinate of the shape to render
687     * @param top The top coordinate of the shape to render
688     * @param texture The texture reprsenting the shape
689     * @param paint The paint to draw the shape with
690     */
691    void drawShape(float left, float top, PathTexture* texture, const SkPaint* paint);
692
693    /**
694     * Renders a strip of polygons with the specified paint, used for tessellated geometry.
695     *
696     * @param vertexBuffer The VertexBuffer to be drawn
697     * @param paint The paint to render with
698     * @param flags flags with which to draw
699     */
700    void drawVertexBuffer(float translateX, float translateY, const VertexBuffer& vertexBuffer,
701            const SkPaint* paint, int flags = 0);
702
703    /**
704     * Convenience for translating method
705     */
706    void drawVertexBuffer(const VertexBuffer& vertexBuffer,
707            const SkPaint* paint, int flags = 0) {
708        drawVertexBuffer(0.0f, 0.0f, vertexBuffer, paint, flags);
709    }
710
711    /**
712     * Renders the convex hull defined by the specified path as a strip of polygons.
713     *
714     * @param path The hull of the path to draw
715     * @param paint The paint to render with
716     */
717    void drawConvexPath(const SkPath& path, const SkPaint* paint);
718
719    /**
720     * Draws text underline and strike-through if needed.
721     *
722     * @param text The text to decor
723     * @param bytesCount The number of bytes in the text
724     * @param totalAdvance The total advance in pixels, defines underline/strikethrough length
725     * @param x The x coordinate where the text will be drawn
726     * @param y The y coordinate where the text will be drawn
727     * @param paint The paint to draw the text with
728     */
729    void drawTextDecorations(float totalAdvance, float x, float y, const SkPaint* paint);
730
731   /**
732     * Draws shadow layer on text (with optional positions).
733     *
734     * @param paint The paint to draw the shadow with
735     * @param text The text to draw
736     * @param bytesCount The number of bytes in the text
737     * @param count The number of glyphs in the text
738     * @param positions The x, y positions of individual glyphs (or NULL)
739     * @param fontRenderer The font renderer object
740     * @param alpha The alpha value for drawing the shadow
741     * @param x The x coordinate where the shadow will be drawn
742     * @param y The y coordinate where the shadow will be drawn
743     */
744    void drawTextShadow(const SkPaint* paint, const char* text, int bytesCount, int count,
745            const float* positions, FontRenderer& fontRenderer, int alpha,
746            float x, float y);
747
748    /**
749     * Draws a path texture. Path textures are alpha8 bitmaps that need special
750     * compositing to apply colors/filters/etc.
751     *
752     * @param texture The texture to render
753     * @param x The x coordinate where the texture will be drawn
754     * @param y The y coordinate where the texture will be drawn
755     * @param paint The paint to draw the texture with
756     */
757     void drawPathTexture(PathTexture* texture, float x, float y, const SkPaint* paint);
758
759    /**
760     * Resets the texture coordinates stored in mMeshVertices. Setting the values
761     * back to default is achieved by calling:
762     *
763     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
764     *
765     * @param u1 The left coordinate of the texture
766     * @param v1 The bottom coordinate of the texture
767     * @param u2 The right coordinate of the texture
768     * @param v2 The top coordinate of the texture
769     */
770    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
771
772    /**
773     * Returns true if the specified paint will draw invisible text.
774     */
775    bool canSkipText(const SkPaint* paint) const;
776
777    bool updateLayer(Layer* layer, bool inFrame);
778    void updateLayers();
779    void flushLayers();
780
781#if DEBUG_LAYERS_AS_REGIONS
782    /**
783     * Renders the specified region as a series of rectangles. This method
784     * is used for debugging only.
785     */
786    void drawRegionRectsDebug(const Region& region);
787#endif
788
789    /**
790     * Renders the specified region as a series of rectangles. The region
791     * must be in screen-space coordinates.
792     */
793    void drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty = false);
794
795    /**
796     * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
797     * is turned on.
798     */
799    void debugClip();
800
801    void debugOverdraw(bool enable, bool clear);
802    void renderOverdraw();
803    void countOverdraw();
804
805    /**
806     * Should be invoked every time the glScissor is modified.
807     */
808    inline void dirtyClip() { mState.setDirtyClip(true); }
809
810    inline const UvMapper& getMapper(const Texture* texture) {
811        return texture && texture->uvMapper ? *texture->uvMapper : mUvMapper;
812    }
813
814    /**
815     * Returns a texture object for the specified bitmap. The texture can
816     * come from the texture cache or an atlas. If this method returns
817     * NULL, the texture could not be found and/or allocated.
818     */
819    Texture* getTexture(const SkBitmap* bitmap);
820
821    bool reportAndClearDirty() { bool ret = mDirty; mDirty = false; return ret; }
822    inline Snapshot* writableSnapshot() { return mState.writableSnapshot(); }
823    inline const Snapshot* currentSnapshot() const { return mState.currentSnapshot(); }
824
825    // State used to define the clipping region
826    Rect mTilingClip;
827    // Is the target render surface opaque
828    bool mOpaque;
829    // Is a frame currently being rendered
830    bool mFrameStarted;
831
832    // Default UV mapper
833    const UvMapper mUvMapper;
834
835    // List of rectangles to clear after saveLayer() is invoked
836    std::vector<Rect> mLayers;
837    // List of layers to update at the beginning of a frame
838    std::vector< sp<Layer> > mLayerUpdates;
839
840    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
841    // Properties.h
842    bool mScissorOptimizationDisabled;
843
844    bool mSkipOutlineClip;
845
846    // True if anything has been drawn since the last call to
847    // reportAndClearDirty()
848    bool mDirty;
849
850    // Lighting + shadows
851    Vector3 mLightCenter;
852    float mLightRadius;
853    uint8_t mAmbientShadowAlpha;
854    uint8_t mSpotShadowAlpha;
855
856    // Paths kept alive for the duration of the frame
857    std::vector<std::unique_ptr<SkPath>> mTempPaths;
858
859    /**
860     * Initial transform for a rendering pass; transform from global device
861     * coordinates to the current RenderNode's drawing content coordinates,
862     * with the RenderNode's RenderProperty transforms already applied.
863     * Calling setMatrix(mBaseTransform) will result in drawing at the origin
864     * of the DisplayList's recorded surface prior to any Canvas
865     * transformation.
866     */
867    Matrix4 mBaseTransform;
868
869    friend class Layer;
870    friend class TextDrawFunctor;
871    friend class DrawBitmapOp;
872    friend class DrawPatchOp;
873
874}; // class OpenGLRenderer
875
876}; // namespace uirenderer
877}; // namespace android
878
879#endif // ANDROID_HWUI_OPENGL_RENDERER_H
880