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