OpenGLRenderer.h revision 87f9c42f5411d1b00f9babe7873b8200c3a25e60
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);
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 setGlobalMatrix(const Matrix4& matrix) {
371        mState.setMatrix(matrix);
372    }
373    void setLocalMatrix(const Matrix4& matrix);
374    void setLocalMatrix(const SkMatrix& matrix);
375    void concatMatrix(const SkMatrix& matrix) { mState.concatMatrix(matrix); }
376
377    void translate(float dx, float dy, float dz = 0.0f);
378    void rotate(float degrees);
379    void scale(float sx, float sy);
380    void skew(float sx, float sy);
381
382    void setMatrix(const Matrix4& matrix); // internal only convenience method
383    void concatMatrix(const Matrix4& matrix); // internal only convenience method
384
385    const Rect& getLocalClipBounds() const { return mState.getLocalClipBounds(); }
386    const Rect& getRenderTargetClipBounds() const { return mState.getRenderTargetClipBounds(); }
387    bool quickRejectConservative(float left, float top,
388            float right, float bottom) const {
389        return mState.quickRejectConservative(left, top, right, bottom);
390    }
391
392    bool clipRect(float left, float top,
393            float right, float bottom, SkRegion::Op op);
394    bool clipPath(const SkPath* path, SkRegion::Op op);
395    bool clipRegion(const SkRegion* region, SkRegion::Op op);
396
397    /**
398     * Does not support different clipping Ops (that is, every call to setClippingOutline is
399     * effectively using SkRegion::kReplaceOp)
400     *
401     * The clipping outline is independent from the regular clip.
402     */
403    void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
404    void setClippingRoundRect(LinearAllocator& allocator,
405            const Rect& rect, float radius, bool highPriority = true);
406    void setProjectionPathMask(LinearAllocator& allocator, const SkPath* path);
407
408    inline bool hasRectToRectTransform() const { return mState.hasRectToRectTransform(); }
409    inline const mat4* currentTransform() const { return mState.currentTransform(); }
410
411    ///////////////////////////////////////////////////////////////////
412    /// CanvasStateClient interface
413
414    virtual void onViewportInitialized() override;
415    virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
416    virtual GLuint getTargetFbo() const override { return 0; }
417
418    SkPath* allocPathForFrame() {
419        std::unique_ptr<SkPath> path(new SkPath());
420        SkPath* returnPath = path.get();
421        mTempPaths.push_back(std::move(path));
422        return returnPath;
423    }
424
425    void setBaseTransform(const Matrix4& matrix) { mBaseTransform = matrix; }
426
427protected:
428    /**
429     * Perform the setup specific to a frame. This method does not
430     * issue any OpenGL commands.
431     */
432    void setupFrameState(float left, float top, float right, float bottom, bool opaque);
433
434    /**
435     * Indicates the start of rendering. This method will setup the
436     * initial OpenGL state (viewport, clearing the buffer, etc.)
437     */
438    void startFrame();
439
440    /**
441     * Clears the underlying surface if needed.
442     */
443    virtual void clear(float left, float top, float right, float bottom, bool opaque);
444
445    /**
446     * Call this method after updating a layer during a drawing pass.
447     */
448    void resumeAfterLayer();
449
450    /**
451     * This method is called whenever a stencil buffer is required. Subclasses
452     * should override this method and call attachStencilBufferToLayer() on the
453     * appropriate layer(s).
454     */
455    virtual void ensureStencilBuffer();
456
457    /**
458     * Obtains a stencil render buffer (allocating it if necessary) and
459     * attaches it to the specified layer.
460     */
461    void attachStencilBufferToLayer(Layer* layer);
462
463    /**
464     * Draw a rectangle list. Currently only used for the the stencil buffer so that the stencil
465     * will have a value of 'n' in every unclipped pixel, where 'n' is the number of rectangles
466     * in the list.
467     */
468    void drawRectangleList(const RectangleList& rectangleList);
469
470    bool quickRejectSetupScissor(float left, float top, float right, float bottom,
471            const SkPaint* paint = nullptr);
472    bool quickRejectSetupScissor(const Rect& bounds, const SkPaint* paint = nullptr) {
473        return quickRejectSetupScissor(bounds.left, bounds.top,
474                bounds.right, bounds.bottom, paint);
475    }
476
477    /**
478     * Compose the layer defined in the current snapshot with the layer
479     * defined by the previous snapshot.
480     *
481     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
482     *
483     * @param curent The current snapshot containing the layer to compose
484     * @param previous The previous snapshot to compose the current layer with
485     */
486    virtual void composeLayer(const Snapshot& current, const Snapshot& previous);
487
488    /**
489     * Marks the specified region as dirty at the specified bounds.
490     */
491    void dirtyLayerUnchecked(Rect& bounds, Region* region);
492
493    /**
494     * Returns the region of the current layer.
495     */
496    virtual Region* getRegion() const {
497        return mState.currentRegion();
498    }
499
500    /**
501     * Indicates whether rendering is currently targeted at a layer.
502     */
503    virtual bool hasLayer() const {
504        return (mState.currentFlags() & Snapshot::kFlagFboTarget) && mState.currentRegion();
505    }
506
507    /**
508     * Renders the specified layer as a textured quad.
509     *
510     * @param layer The layer to render
511     * @param rect The bounds of the layer
512     */
513    void drawTextureLayer(Layer* layer, const Rect& rect);
514
515    /**
516     * Gets the alpha and xfermode out of a paint object. If the paint is null
517     * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for snapshot alpha.
518     *
519     * @param paint The paint to extract values from
520     * @param alpha Where to store the resulting alpha
521     * @param mode Where to store the resulting xfermode
522     */
523    inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const;
524
525    /**
526     * Gets the alpha from a layer, accounting for snapshot alpha
527     *
528     * @param layer The layer from which the alpha is extracted
529     */
530    inline float getLayerAlpha(const Layer* layer) const;
531
532    /**
533     * Set to true to suppress error checks at the end of a frame.
534     */
535    virtual bool suppressErrorChecks() const {
536        return false;
537    }
538
539    CanvasState mState;
540    Caches& mCaches;
541    RenderState& mRenderState;
542
543private:
544    enum class GlopRenderType {
545        Standard,
546        Multi,
547        LayerClear
548    };
549
550    void renderGlop(const Glop& glop, GlopRenderType type = GlopRenderType::Standard);
551
552    /**
553     * Discards the content of the framebuffer if supported by the driver.
554     * This method should be called at the beginning of a frame to optimize
555     * rendering on some tiler architectures.
556     */
557    void discardFramebuffer(float left, float top, float right, float bottom);
558
559    /**
560     * Sets the clipping rectangle using glScissor. The clip is defined by
561     * the current snapshot's clipRect member.
562     */
563    void setScissorFromClip();
564
565    /**
566     * Sets the clipping region using the stencil buffer. The clip region
567     * is defined by the current snapshot's clipRegion member.
568     */
569    void setStencilFromClip();
570
571    /**
572     * Given the local bounds of the layer, calculates ...
573     */
574    void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
575
576    /**
577     * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
578     */
579    void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
580            bool fboLayer, int alpha);
581
582    /**
583     * Creates a new layer stored in the specified snapshot.
584     *
585     * @param snapshot The snapshot associated with the new layer
586     * @param left The left coordinate of the layer
587     * @param top The top coordinate of the layer
588     * @param right The right coordinate of the layer
589     * @param bottom The bottom coordinate of the layer
590     * @param alpha The translucency of the layer
591     * @param mode The blending mode of the layer
592     * @param flags The layer save flags
593     * @param mask A mask to use when drawing the layer back, may be empty
594     *
595     * @return True if the layer was successfully created, false otherwise
596     */
597    bool createLayer(float left, float top, float right, float bottom,
598            const SkPaint* paint, int flags, const SkPath* convexMask);
599
600    /**
601     * Creates a new layer stored in the specified snapshot as an FBO.
602     *
603     * @param layer The layer to store as an FBO
604     * @param snapshot The snapshot associated with the new layer
605     * @param bounds The bounds of the layer
606     */
607    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip);
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     * Restores the content in layer to the screen, swapping the blend mode,
619     * specifically used in the restore() of a saveLayerAlpha().
620     *
621     * This allows e.g. a layer that would have been drawn on top of existing content (with SrcOver)
622     * to be drawn underneath.
623     *
624     * This will always ignore the canvas transform.
625     */
626    void composeLayerRectSwapped(Layer* layer, const Rect& rect);
627
628    /**
629     * Draws the content in layer to the screen.
630     */
631    void composeLayerRect(Layer* layer, const Rect& rect);
632
633    /**
634     * Clears all the regions corresponding to the current list of layers.
635     * This method MUST be invoked before any drawing operation.
636     */
637    void clearLayerRegions();
638
639    /**
640     * Mark the layer as dirty at the specified coordinates. The coordinates
641     * are transformed with the supplied matrix.
642     */
643    void dirtyLayer(const float left, const float top,
644            const float right, const float bottom, const Matrix4& transform);
645
646    /**
647     * Mark the layer as dirty at the specified coordinates.
648     */
649    void dirtyLayer(const float left, const float top,
650            const float right, const float bottom);
651
652    /**
653     * Draws a colored rectangle with the specified color. The specified coordinates
654     * are transformed by the current snapshot's transform matrix unless specified
655     * otherwise.
656     *
657     * @param left The left coordinate of the rectangle
658     * @param top The top coordinate of the rectangle
659     * @param right The right coordinate of the rectangle
660     * @param bottom The bottom coordinate of the rectangle
661     * @param paint The paint containing the color, blending mode, etc.
662     * @param ignoreTransform True if the current transform should be ignored
663     */
664    void drawColorRect(float left, float top, float right, float bottom,
665            const SkPaint* paint, bool ignoreTransform = false);
666
667    /**
668     * Draws a series of colored rectangles with the specified color. The specified
669     * coordinates are transformed by the current snapshot's transform matrix unless
670     * specified otherwise.
671     *
672     * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
673     *              per rectangle
674     * @param paint The paint containing the color, blending mode, etc.
675     * @param ignoreTransform True if the current transform should be ignored
676     * @param dirty True if calling this method should dirty the current layer
677     * @param clip True if the rects should be clipped, false otherwise
678     */
679    void drawColorRects(const float* rects, int count, const SkPaint* paint,
680            bool ignoreTransform = false, bool dirty = true, bool clip = true);
681
682    /**
683     * Draws the shape represented by the specified path texture.
684     * This method invokes drawPathTexture() but takes into account
685     * the extra left/top offset and the texture offset to correctly
686     * position the final shape.
687     *
688     * @param left The left coordinate of the shape to render
689     * @param top The top coordinate of the shape to render
690     * @param texture The texture reprsenting the shape
691     * @param paint The paint to draw the shape with
692     */
693    void drawShape(float left, float top, PathTexture* texture, const SkPaint* paint);
694
695    /**
696     * Renders a strip of polygons with the specified paint, used for tessellated geometry.
697     *
698     * @param vertexBuffer The VertexBuffer to be drawn
699     * @param paint The paint to render with
700     * @param flags flags with which to draw
701     */
702    void drawVertexBuffer(float translateX, float translateY, const VertexBuffer& vertexBuffer,
703            const SkPaint* paint, int flags = 0);
704
705    /**
706     * Convenience for translating method
707     */
708    void drawVertexBuffer(const VertexBuffer& vertexBuffer,
709            const SkPaint* paint, int flags = 0) {
710        drawVertexBuffer(0.0f, 0.0f, vertexBuffer, paint, flags);
711    }
712
713    /**
714     * Renders the convex hull defined by the specified path as a strip of polygons.
715     *
716     * @param path The hull of the path to draw
717     * @param paint The paint to render with
718     */
719    void drawConvexPath(const SkPath& path, const SkPaint* paint);
720
721    /**
722     * Draws text underline and strike-through if needed.
723     *
724     * @param text The text to decor
725     * @param bytesCount The number of bytes in the text
726     * @param totalAdvance The total advance in pixels, defines underline/strikethrough length
727     * @param x The x coordinate where the text will be drawn
728     * @param y The y coordinate where the text will be drawn
729     * @param paint The paint to draw the text with
730     */
731    void drawTextDecorations(float totalAdvance, float x, float y, const SkPaint* paint);
732
733   /**
734     * Draws shadow layer on text (with optional positions).
735     *
736     * @param paint The paint to draw the shadow with
737     * @param text The text to draw
738     * @param bytesCount The number of bytes in the text
739     * @param count The number of glyphs in the text
740     * @param positions The x, y positions of individual glyphs (or NULL)
741     * @param fontRenderer The font renderer object
742     * @param alpha The alpha value for drawing the shadow
743     * @param x The x coordinate where the shadow will be drawn
744     * @param y The y coordinate where the shadow will be drawn
745     */
746    void drawTextShadow(const SkPaint* paint, const char* text, int bytesCount, int count,
747            const float* positions, FontRenderer& fontRenderer, int alpha,
748            float x, float y);
749
750    /**
751     * Draws a path texture. Path textures are alpha8 bitmaps that need special
752     * compositing to apply colors/filters/etc.
753     *
754     * @param texture The texture to render
755     * @param x The x coordinate where the texture will be drawn
756     * @param y The y coordinate where the texture will be drawn
757     * @param paint The paint to draw the texture with
758     */
759     void drawPathTexture(PathTexture* texture, float x, float y, const SkPaint* paint);
760
761    /**
762     * Resets the texture coordinates stored in mMeshVertices. Setting the values
763     * back to default is achieved by calling:
764     *
765     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
766     *
767     * @param u1 The left coordinate of the texture
768     * @param v1 The bottom coordinate of the texture
769     * @param u2 The right coordinate of the texture
770     * @param v2 The top coordinate of the texture
771     */
772    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
773
774    /**
775     * Returns true if the specified paint will draw invisible text.
776     */
777    bool canSkipText(const SkPaint* paint) const;
778
779    bool updateLayer(Layer* layer, bool inFrame);
780    void updateLayers();
781    void flushLayers();
782
783#if DEBUG_LAYERS_AS_REGIONS
784    /**
785     * Renders the specified region as a series of rectangles. This method
786     * is used for debugging only.
787     */
788    void drawRegionRectsDebug(const Region& region);
789#endif
790
791    /**
792     * Renders the specified region as a series of rectangles. The region
793     * must be in screen-space coordinates.
794     */
795    void drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty = false);
796
797    /**
798     * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
799     * is turned on.
800     */
801    void debugClip();
802
803    void debugOverdraw(bool enable, bool clear);
804    void renderOverdraw();
805    void countOverdraw();
806
807    /**
808     * Should be invoked every time the glScissor is modified.
809     */
810    inline void dirtyClip() { mState.setDirtyClip(true); }
811
812    inline const UvMapper& getMapper(const Texture* texture) {
813        return texture && texture->uvMapper ? *texture->uvMapper : mUvMapper;
814    }
815
816    /**
817     * Returns a texture object for the specified bitmap. The texture can
818     * come from the texture cache or an atlas. If this method returns
819     * NULL, the texture could not be found and/or allocated.
820     */
821    Texture* getTexture(const SkBitmap* bitmap);
822
823    bool reportAndClearDirty() { bool ret = mDirty; mDirty = false; return ret; }
824    inline Snapshot* writableSnapshot() { return mState.writableSnapshot(); }
825    inline const Snapshot* currentSnapshot() const { return mState.currentSnapshot(); }
826
827    // State used to define the clipping region
828    Rect mTilingClip;
829    // Is the target render surface opaque
830    bool mOpaque;
831    // Is a frame currently being rendered
832    bool mFrameStarted;
833
834    // Default UV mapper
835    const UvMapper mUvMapper;
836
837    // List of rectangles to clear after saveLayer() is invoked
838    std::vector<Rect> mLayers;
839    // List of layers to update at the beginning of a frame
840    std::vector< sp<Layer> > mLayerUpdates;
841
842    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
843    // Properties.h
844    bool mScissorOptimizationDisabled;
845
846    bool mSkipOutlineClip;
847
848    // True if anything has been drawn since the last call to
849    // reportAndClearDirty()
850    bool mDirty;
851
852    // Lighting + shadows
853    Vector3 mLightCenter;
854    float mLightRadius;
855    uint8_t mAmbientShadowAlpha;
856    uint8_t mSpotShadowAlpha;
857
858    // Paths kept alive for the duration of the frame
859    std::vector<std::unique_ptr<SkPath>> mTempPaths;
860
861    /**
862     * Initial transform for a rendering pass; transform from global device
863     * coordinates to the current RenderNode's drawing content coordinates,
864     * with the RenderNode's RenderProperty transforms already applied.
865     * Calling setMatrix(mBaseTransform) will result in drawing at the origin
866     * of the DisplayList's recorded surface prior to any Canvas
867     * transformation.
868     */
869    Matrix4 mBaseTransform;
870
871    friend class Layer;
872    friend class TextDrawFunctor;
873    friend class DrawBitmapOp;
874    friend class DrawPatchOp;
875
876}; // class OpenGLRenderer
877
878}; // namespace uirenderer
879}; // namespace android
880
881#endif // ANDROID_HWUI_OPENGL_RENDERER_H
882