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