2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_HWUI_OPENGL_RENDERER_H
18 #define ANDROID_HWUI_OPENGL_RENDERER_H
20 #include <GLES2/gl2.h>
21 #include <GLES2/gl2ext.h>
28 #include <SkXfermode.h>
30 #include <utils/Functor.h>
31 #include <utils/RefBase.h>
32 #include <utils/SortedVector.h>
33 #include <utils/Vector.h>
35 #include <cutils/compiler.h>
38 #include "Extensions.h"
44 #include "SkiaShader.h"
45 #include "SkiaColorFilter.h"
49 namespace uirenderer {
51 struct DrawModifiers {
53 SkiaColorFilter* mColorFilter;
54 float mOverrideLayerAlpha;
65 int mPaintFilterClearBits;
66 int mPaintFilterSetBits;
69 enum StateDeferFlags {
70 kStateDeferFlag_Draw = 0x1,
71 kStateDeferFlag_Clip = 0x2
74 struct DeferredDisplayState {
75 Rect mBounds; // local bounds, mapped with matrix to be in screen space coordinates, clipped.
77 // the below are set and used by the OpenGLRenderer at record and deferred playback
80 DrawModifiers mDrawModifiers;
84 ///////////////////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////////////////
89 class TextSetupFunctor;
93 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
94 * simplified version of Skia's Canvas API.
96 class OpenGLRenderer {
98 ANDROID_API OpenGLRenderer();
99 virtual ~OpenGLRenderer();
102 * Sets the name of this renderer. The name is optional and
103 * empty by default. If the pointer is null the name is set
104 * to the empty string.
106 ANDROID_API void setName(const char* name);
109 * Returns the name of this renderer as UTF8 string.
110 * The returned pointer is never null.
112 ANDROID_API const char* getName() const;
115 * Read externally defined properties to control the behavior
118 ANDROID_API void initProperties();
121 * Indicates whether this renderer executes drawing commands immediately.
122 * If this method returns true, the drawing commands will be executed
125 virtual bool isDeferred();
128 * Sets the dimension of the underlying drawing surface. This method must
129 * be called at least once every time the drawing surface changes size.
131 * @param width The width in pixels of the underlysing surface
132 * @param height The height in pixels of the underlysing surface
134 virtual void setViewport(int width, int height);
137 * Prepares the renderer to draw a frame. This method must be invoked
138 * at the beginning of each frame. When this method is invoked, the
139 * entire drawing surface is assumed to be redrawn.
141 * @param opaque If true, the target surface is considered opaque
142 * and will not be cleared. If false, the target surface
145 ANDROID_API status_t prepare(bool opaque);
148 * Prepares the renderer to draw a frame. This method must be invoked
149 * at the beginning of each frame. Only the specified rectangle of the
150 * frame is assumed to be dirty. A clip will automatically be set to
151 * the specified rectangle.
153 * @param left The left coordinate of the dirty rectangle
154 * @param top The top coordinate of the dirty rectangle
155 * @param right The right coordinate of the dirty rectangle
156 * @param bottom The bottom coordinate of the dirty rectangle
157 * @param opaque If true, the target surface is considered opaque
158 * and will not be cleared. If false, the target surface
159 * will be cleared in the specified dirty rectangle
161 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
164 * Indicates the end of a frame. This method must be invoked whenever
165 * the caller is done rendering a frame.
167 virtual void finish();
170 * This method must be invoked before handing control over to a draw functor.
171 * See callDrawGLFunction() for instance.
173 * This command must not be recorded inside display lists.
175 virtual void interrupt();
178 * This method must be invoked after getting control back from a draw functor.
180 * This command must not be recorded inside display lists.
182 virtual void resume();
184 ANDROID_API status_t invokeFunctors(Rect& dirty);
185 ANDROID_API void detachFunctor(Functor* functor);
186 ANDROID_API void attachFunctor(Functor* functor);
187 virtual status_t callDrawGLFunction(Functor* functor, Rect& dirty);
189 ANDROID_API void pushLayerUpdate(Layer* layer);
190 ANDROID_API void clearLayerUpdates();
192 ANDROID_API int getSaveCount() const;
193 virtual int save(int flags);
194 virtual void restore();
195 virtual void restoreToCount(int saveCount);
197 ANDROID_API int saveLayer(float left, float top, float right, float bottom,
198 SkPaint* paint, int flags) {
199 SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode;
200 if (paint) mode = getXfermode(paint->getXfermode());
201 return saveLayer(left, top, right, bottom, paint ? paint->getAlpha() : 255, mode, flags);
203 ANDROID_API int saveLayerAlpha(float left, float top, float right, float bottom,
204 int alpha, int flags) {
205 return saveLayer(left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
207 virtual int saveLayer(float left, float top, float right, float bottom,
208 int alpha, SkXfermode::Mode mode, int flags);
210 int saveLayerDeferred(float left, float top, float right, float bottom,
211 int alpha, SkXfermode::Mode mode, int flags);
213 virtual void translate(float dx, float dy);
214 virtual void rotate(float degrees);
215 virtual void scale(float sx, float sy);
216 virtual void skew(float sx, float sy);
218 bool hasRectToRectTransform();
219 ANDROID_API void getMatrix(SkMatrix* matrix);
220 virtual void setMatrix(SkMatrix* matrix);
221 virtual void concatMatrix(SkMatrix* matrix);
223 ANDROID_API const Rect& getClipBounds();
224 ANDROID_API bool quickReject(float left, float top, float right, float bottom);
225 bool quickRejectNoScissor(float left, float top, float right, float bottom);
226 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op);
227 virtual bool clipPath(SkPath* path, SkRegion::Op op);
228 virtual bool clipRegion(SkRegion* region, SkRegion::Op op);
229 virtual Rect* getClipRect();
231 virtual status_t drawDisplayList(DisplayList* displayList, Rect& dirty, int32_t replayFlags);
232 virtual void outputDisplayList(DisplayList* displayList);
233 virtual status_t drawLayer(Layer* layer, float x, float y);
234 virtual status_t drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint);
235 virtual status_t drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint);
236 virtual status_t drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
237 float srcRight, float srcBottom, float dstLeft, float dstTop,
238 float dstRight, float dstBottom, SkPaint* paint);
239 virtual status_t drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
240 virtual status_t drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
241 float* vertices, int* colors, SkPaint* paint);
242 virtual status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
243 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
244 float left, float top, float right, float bottom, SkPaint* paint);
245 status_t drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
246 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
247 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode);
248 virtual status_t drawColor(int color, SkXfermode::Mode mode);
249 virtual status_t drawRect(float left, float top, float right, float bottom, SkPaint* paint);
250 virtual status_t drawRoundRect(float left, float top, float right, float bottom,
251 float rx, float ry, SkPaint* paint);
252 virtual status_t drawCircle(float x, float y, float radius, SkPaint* paint);
253 virtual status_t drawOval(float left, float top, float right, float bottom, SkPaint* paint);
254 virtual status_t drawArc(float left, float top, float right, float bottom,
255 float startAngle, float sweepAngle, bool useCenter, SkPaint* paint);
256 virtual status_t drawPath(SkPath* path, SkPaint* paint);
257 virtual status_t drawLines(float* points, int count, SkPaint* paint);
258 virtual status_t drawPoints(float* points, int count, SkPaint* paint);
259 virtual status_t drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
260 float hOffset, float vOffset, SkPaint* paint);
261 virtual status_t drawPosText(const char* text, int bytesCount, int count,
262 const float* positions, SkPaint* paint);
263 virtual status_t drawText(const char* text, int bytesCount, int count, float x, float y,
264 const float* positions, SkPaint* paint, float length = -1.0f);
265 virtual status_t drawRects(const float* rects, int count, SkPaint* paint);
267 virtual void resetShader();
268 virtual void setupShader(SkiaShader* shader);
270 virtual void resetColorFilter();
271 virtual void setupColorFilter(SkiaColorFilter* filter);
273 virtual void resetShadow();
274 virtual void setupShadow(float radius, float dx, float dy, int color);
276 virtual void resetPaintFilter();
277 virtual void setupPaintFilter(int clearBits, int setBits);
279 // If this value is set to < 1.0, it overrides alpha set on layer (see drawBitmap, drawLayer)
280 void setOverrideLayerAlpha(float alpha) { mDrawModifiers.mOverrideLayerAlpha = alpha; }
282 SkPaint* filterPaint(SkPaint* paint);
284 bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags);
285 void restoreDisplayState(const DeferredDisplayState& state);
287 const DrawModifiers& getDrawModifiers() { return mDrawModifiers; }
288 void setDrawModifiers(const DrawModifiers& drawModifiers) { mDrawModifiers = drawModifiers; }
290 ANDROID_API bool isCurrentTransformSimple() {
291 return mSnapshot->transform->isSimple();
294 Caches& getCaches() {
299 bool isCurrentClipSimple() {
300 return mSnapshot->clipRegion->isEmpty();
304 * Scales the alpha on the current snapshot. This alpha value will be modulated
305 * with other alpha values when drawing primitives.
307 void scaleAlpha(float alpha) {
308 mSnapshot->alpha *= alpha;
312 * Inserts a named event marker in the stream of GL commands.
314 void eventMark(const char* name) const;
317 * Inserts a named group marker in the stream of GL commands. This marker
318 * can be used by tools to group commands into logical groups. A call to
319 * this method must always be followed later on by a call to endMark().
321 void startMark(const char* name) const;
324 * Closes the last group marker opened by startMark().
326 void endMark() const;
329 * Gets the alpha and xfermode out of a paint object. If the paint is null
330 * alpha will be 255 and the xfermode will be SRC_OVER. This method does
331 * not multiply the paint's alpha by the current snapshot's alpha, and does
332 * not replace the alpha with the overrideLayerAlpha
334 * @param paint The paint to extract values from
335 * @param alpha Where to store the resulting alpha
336 * @param mode Where to store the resulting xfermode
338 static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
340 *mode = getXfermode(paint->getXfermode());
342 // Skia draws using the color's alpha channel if < 255
343 // Otherwise, it uses the paint's alpha
344 int color = paint->getColor();
345 *alpha = (color >> 24) & 0xFF;
347 *alpha = paint->getAlpha();
350 *mode = SkXfermode::kSrcOver_Mode;
356 * Return the best transform to use to rasterize text given a full
359 mat4 findBestFontTransform(const mat4& transform) const;
363 * Computes the projection matrix, initialize the first snapshot
364 * and stores the dimensions of the render target.
366 void initViewport(int width, int height);
369 * Perform the setup specific to a frame. This method does not
370 * issue any OpenGL commands.
372 void setupFrameState(float left, float top, float right, float bottom, bool opaque);
375 * Indicates the start of rendering. This method will setup the
376 * initial OpenGL state (viewport, clearing the buffer, etc.)
378 status_t startFrame();
381 * Clears the underlying surface if needed.
383 virtual status_t clear(float left, float top, float right, float bottom, bool opaque);
386 * Call this method after updating a layer during a drawing pass.
388 void resumeAfterLayer();
391 * This method is called whenever a stencil buffer is required. Subclasses
392 * should override this method and call attachStencilBufferToLayer() on the
393 * appropriate layer(s).
395 virtual void ensureStencilBuffer();
398 * Obtains a stencil render buffer (allocating it if necessary) and
399 * attaches it to the specified layer.
401 void attachStencilBufferToLayer(Layer* layer);
404 * Compose the layer defined in the current snapshot with the layer
405 * defined by the previous snapshot.
407 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
409 * @param curent The current snapshot containing the layer to compose
410 * @param previous The previous snapshot to compose the current layer with
412 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
415 * Marks the specified region as dirty at the specified bounds.
417 void dirtyLayerUnchecked(Rect& bounds, Region* region);
420 * Returns the current snapshot.
422 sp<Snapshot> getSnapshot() const {
427 * Returns the region of the current layer.
429 virtual Region* getRegion() const {
430 return mSnapshot->region;
434 * Indicates whether rendering is currently targeted at a layer.
436 virtual bool hasLayer() const {
437 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
441 * Returns the name of the FBO this renderer is rendering into.
443 virtual GLint getTargetFbo() const {
448 * Renders the specified layer as a textured quad.
450 * @param layer The layer to render
451 * @param rect The bounds of the layer
453 void drawTextureLayer(Layer* layer, const Rect& rect);
456 * Gets the alpha and xfermode out of a paint object. If the paint is null
457 * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for both
458 * snapshot alpha, and overrideLayerAlpha
460 * @param paint The paint to extract values from
461 * @param alpha Where to store the resulting alpha
462 * @param mode Where to store the resulting xfermode
464 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const;
467 * Gets the alpha from a layer, accounting for snapshot alpha and overrideLayerAlpha
469 * @param layer The layer from which the alpha is extracted
471 inline float getLayerAlpha(Layer* layer) const;
474 * Safely retrieves the mode from the specified xfermode. If the specified
475 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode.
477 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
478 SkXfermode::Mode resultMode;
479 if (!SkXfermode::AsMode(mode, &resultMode)) {
480 resultMode = SkXfermode::kSrcOver_Mode;
486 * Set to true to suppress error checks at the end of a frame.
488 virtual bool suppressErrorChecks() const {
494 * Discards the content of the framebuffer if supported by the driver.
495 * This method should be called at the beginning of a frame to optimize
496 * rendering on some tiler architectures.
498 void discardFramebuffer(float left, float top, float right, float bottom);
501 * Ensures the state of the renderer is the same as the state of
507 * Tells the GPU what part of the screen is about to be redrawn.
508 * This method will use the clip rect that we started drawing the
510 * This method needs to be invoked every time getTargetFbo() is
513 void startTiling(const sp<Snapshot>& snapshot, bool opaque = false);
516 * Tells the GPU what part of the screen is about to be redrawn.
517 * This method needs to be invoked every time getTargetFbo() is
520 void startTiling(const Rect& clip, int windowHeight, bool opaque = false);
523 * Tells the GPU that we are done drawing the frame or that we
524 * are switching to another render target.
529 * Saves the current state of the renderer as a new snapshot.
530 * The new snapshot is saved in mSnapshot and the previous snapshot
531 * is linked from mSnapshot->previous.
533 * @param flags The save flags; see SkCanvas for more information
535 * @return The new save count. This value can be passed to #restoreToCount()
537 int saveSnapshot(int flags);
540 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
542 * @return True if the clip was modified.
544 bool restoreSnapshot();
547 * Sets the clipping rectangle using glScissor. The clip is defined by
548 * the current snapshot's clipRect member.
550 void setScissorFromClip();
553 * Sets the clipping region using the stencil buffer. The clip region
554 * is defined by the current snapshot's clipRegion member.
556 void setStencilFromClip();
559 * Performs a quick reject but does not affect the scissor. Returns
560 * the transformed rect to test and the current clip.
562 bool quickRejectNoScissor(float left, float top, float right, float bottom,
563 Rect& transformed, Rect& clip);
566 * Performs a quick reject but adjust the bounds to account for stroke width if necessary
568 bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint);
571 * Given the local bounds of the layer, calculates ...
573 void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
576 * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
578 void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
579 bool fboLayer, int alpha);
582 * Creates a new layer stored in the specified snapshot.
584 * @param snapshot The snapshot associated with the new layer
585 * @param left The left coordinate of the layer
586 * @param top The top coordinate of the layer
587 * @param right The right coordinate of the layer
588 * @param bottom The bottom coordinate of the layer
589 * @param alpha The translucency of the layer
590 * @param mode The blending mode of the layer
591 * @param flags The layer save flags
592 * @param previousFbo The name of the current framebuffer
594 * @return True if the layer was successfully created, false otherwise
596 bool createLayer(float left, float top, float right, float bottom,
597 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
600 * Creates a new layer stored in the specified snapshot as an FBO.
602 * @param layer The layer to store as an FBO
603 * @param snapshot The snapshot associated with the new layer
604 * @param bounds The bounds of the layer
605 * @param previousFbo The name of the current framebuffer
607 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
610 * Compose the specified layer as a region.
612 * @param layer The layer to compose
613 * @param rect The layer's bounds
615 void composeLayerRegion(Layer* layer, const Rect& rect);
618 * Compose the specified layer as a simple rectangle.
620 * @param layer The layer to compose
621 * @param rect The layer's bounds
622 * @param swap If true, the source and destination are swapped
624 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
627 * Clears all the regions corresponding to the current list of layers.
628 * This method MUST be invoked before any drawing operation.
630 void clearLayerRegions();
633 * Mark the layer as dirty at the specified coordinates. The coordinates
634 * are transformed with the supplied matrix.
636 void dirtyLayer(const float left, const float top,
637 const float right, const float bottom, const mat4 transform);
640 * Mark the layer as dirty at the specified coordinates.
642 void dirtyLayer(const float left, const float top,
643 const float right, const float bottom);
646 * Draws a colored rectangle with the specified color. The specified coordinates
647 * are transformed by the current snapshot's transform matrix unless specified
650 * @param left The left coordinate of the rectangle
651 * @param top The top coordinate of the rectangle
652 * @param right The right coordinate of the rectangle
653 * @param bottom The bottom coordinate of the rectangle
654 * @param color The rectangle's ARGB color, defined as a packed 32 bits word
655 * @param mode The Skia xfermode to use
656 * @param ignoreTransform True if the current transform should be ignored
658 void drawColorRect(float left, float top, float right, float bottom,
659 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
662 * Draws a series of colored rectangles with the specified color. The specified
663 * coordinates are transformed by the current snapshot's transform matrix unless
664 * specified otherwise.
666 * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
668 * @param color The rectangles' ARGB color, defined as a packed 32 bits word
669 * @param mode The Skia xfermode to use
670 * @param ignoreTransform True if the current transform should be ignored
671 * @param dirty True if calling this method should dirty the current layer
672 * @param clip True if the rects should be clipped, false otherwise
674 status_t drawColorRects(const float* rects, int count, int color,
675 SkXfermode::Mode mode, bool ignoreTransform = false,
676 bool dirty = true, bool clip = true);
679 * Draws the shape represented by the specified path texture.
680 * This method invokes drawPathTexture() but takes into account
681 * the extra left/top offset and the texture offset to correctly
682 * position the final shape.
684 * @param left The left coordinate of the shape to render
685 * @param top The top coordinate of the shape to render
686 * @param texture The texture reprsenting the shape
687 * @param paint The paint to draw the shape with
689 status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
692 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
693 * different compositing rules.
695 * @param texture The texture to draw with
696 * @param left The x coordinate of the bitmap
697 * @param top The y coordinate of the bitmap
698 * @param paint The paint to render with
700 void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
703 * Renders a strip of polygons with the specified paint, used for tessellated geometry.
705 * @param vertexBuffer The VertexBuffer to be drawn
706 * @param paint The paint to render with
707 * @param useOffset Offset the vertexBuffer (used in drawing non-AA lines)
709 status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
710 bool useOffset = false);
713 * Renders the convex hull defined by the specified path as a strip of polygons.
715 * @param path The hull of the path to draw
716 * @param paint The paint to render with
718 status_t drawConvexPath(const SkPath& path, SkPaint* paint);
721 * Draws a textured rectangle with the specified texture. The specified coordinates
722 * are transformed by the current snapshot's transform matrix.
724 * @param left The left coordinate of the rectangle
725 * @param top The top coordinate of the rectangle
726 * @param right The right coordinate of the rectangle
727 * @param bottom The bottom coordinate of the rectangle
728 * @param texture The texture name to map onto the rectangle
729 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
730 * @param mode The blending mode
731 * @param blend True if the texture contains an alpha channel
733 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
734 float alpha, SkXfermode::Mode mode, bool blend);
737 * Draws a textured rectangle with the specified texture. The specified coordinates
738 * are transformed by the current snapshot's transform matrix.
740 * @param left The left coordinate of the rectangle
741 * @param top The top coordinate of the rectangle
742 * @param right The right coordinate of the rectangle
743 * @param bottom The bottom coordinate of the rectangle
744 * @param texture The texture to use
745 * @param paint The paint containing the alpha, blending mode, etc.
747 void drawTextureRect(float left, float top, float right, float bottom,
748 Texture* texture, SkPaint* paint);
751 * Draws a textured mesh with the specified texture. If the indices are omitted,
752 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a
755 * @param left The left coordinate of the rectangle
756 * @param top The top coordinate of the rectangle
757 * @param right The right coordinate of the rectangle
758 * @param bottom The bottom coordinate of the rectangle
759 * @param texture The texture name to map onto the rectangle
760 * @param alpha An additional translucency parameter, between 0.0f and 1.0f
761 * @param mode The blending mode
762 * @param blend True if the texture contains an alpha channel
763 * @param vertices The vertices that define the mesh
764 * @param texCoords The texture coordinates of each vertex
765 * @param elementsCount The number of elements in the mesh, required by indices
766 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped
767 * @param ignoreTransform True if the current transform should be ignored
768 * @param vbo The VBO used to draw the mesh
769 * @param ignoreScale True if the model view matrix should not be scaled
770 * @param dirty True if calling this method should dirty the current layer
772 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture,
773 float alpha, SkXfermode::Mode mode, bool blend,
774 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
775 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0,
776 bool ignoreScale = false, bool dirty = true);
778 void drawAlpha8TextureMesh(float left, float top, float right, float bottom,
779 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
780 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
781 bool ignoreTransform, bool dirty = true);
784 * Draws text underline and strike-through if needed.
786 * @param text The text to decor
787 * @param bytesCount The number of bytes in the text
788 * @param length The length in pixels of the text, can be <= 0.0f to force a measurement
789 * @param x The x coordinate where the text will be drawn
790 * @param y The y coordinate where the text will be drawn
791 * @param paint The paint to draw the text with
793 void drawTextDecorations(const char* text, int bytesCount, float length,
794 float x, float y, SkPaint* paint);
797 * Draws shadow layer on text (with optional positions).
799 * @param paint The paint to draw the shadow with
800 * @param text The text to draw
801 * @param bytesCount The number of bytes in the text
802 * @param count The number of glyphs in the text
803 * @param positions The x, y positions of individual glyphs (or NULL)
804 * @param fontRenderer The font renderer object
805 * @param alpha The alpha value for drawing the shadow
806 * @param mode The xfermode for drawing the shadow
807 * @param x The x coordinate where the shadow will be drawn
808 * @param y The y coordinate where the shadow will be drawn
810 void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
811 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
815 * Draws a path texture. Path textures are alpha8 bitmaps that need special
816 * compositing to apply colors/filters/etc.
818 * @param texture The texture to render
819 * @param x The x coordinate where the texture will be drawn
820 * @param y The y coordinate where the texture will be drawn
821 * @param paint The paint to draw the texture with
823 void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
826 * Resets the texture coordinates stored in mMeshVertices. Setting the values
827 * back to default is achieved by calling:
829 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
831 * @param u1 The left coordinate of the texture
832 * @param v1 The bottom coordinate of the texture
833 * @param u2 The right coordinate of the texture
834 * @param v2 The top coordinate of the texture
836 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
839 * Returns true if the specified paint will draw invisible text.
841 bool canSkipText(const SkPaint* paint) const;
844 * Binds the specified texture. The texture unit must have been selected
845 * prior to calling this method.
847 inline void bindTexture(GLuint texture) {
848 glBindTexture(GL_TEXTURE_2D, texture);
852 * Binds the specified EGLImage texture. The texture unit must have been selected
853 * prior to calling this method.
855 inline void bindExternalTexture(GLuint texture) {
856 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
860 * Enable or disable blending as necessary. This function sets the appropriate
861 * blend function based on the specified xfermode.
863 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
864 bool swapSrcDst = false);
867 * Use the specified program with the current GL context. If the program is already
868 * in use, it will not be bound again. If it is not in use, the current program is
869 * marked unused and the specified program becomes used and becomes the new
872 * @param program The program to use
874 * @return true If the specified program was already in use, false otherwise.
876 inline bool useProgram(Program* program);
879 * Invoked before any drawing operation. This sets required state.
881 void setupDraw(bool clear = true);
884 * Various methods to setup OpenGL rendering.
886 void setupDrawWithTexture(bool isAlpha8 = false);
887 void setupDrawWithTextureAndColor(bool isAlpha8 = false);
888 void setupDrawWithExternalTexture();
889 void setupDrawNoTexture();
891 void setupDrawPoint(float pointSize);
892 void setupDrawColor(int color, int alpha);
893 void setupDrawColor(float r, float g, float b, float a);
894 void setupDrawAlpha8Color(int color, int alpha);
895 void setupDrawTextGamma(const SkPaint* paint);
896 void setupDrawShader();
897 void setupDrawColorFilter();
898 void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
899 bool swapSrcDst = false);
900 void setupDrawBlending(bool blend = true, SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
901 bool swapSrcDst = false);
902 void setupDrawProgram();
903 void setupDrawDirtyRegionsDisabled();
904 void setupDrawModelViewIdentity(bool offset = false);
905 void setupDrawModelView(float left, float top, float right, float bottom,
906 bool ignoreTransform = false, bool ignoreModelView = false);
907 void setupDrawModelViewTranslate(float left, float top, float right, float bottom,
908 bool ignoreTransform = false);
909 void setupDrawPointUniforms();
910 void setupDrawColorUniforms();
911 void setupDrawPureColorUniforms();
912 void setupDrawShaderIdentityUniforms();
913 void setupDrawShaderUniforms(bool ignoreTransform = false);
914 void setupDrawColorFilterUniforms();
915 void setupDrawSimpleMesh();
916 void setupDrawTexture(GLuint texture);
917 void setupDrawExternalTexture(GLuint texture);
918 void setupDrawTextureTransform();
919 void setupDrawTextureTransformUniforms(mat4& transform);
920 void setupDrawTextGammaUniforms();
921 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
922 void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors);
923 void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
924 void setupDrawVertices(GLvoid* vertices);
925 void finishDrawTexture();
926 void accountForClear(SkXfermode::Mode mode);
928 bool updateLayer(Layer* layer, bool inFrame);
933 * Renders the specified region as a series of rectangles. This method
934 * is used for debugging only.
936 void drawRegionRects(const Region& region);
939 * Renders the specified region as a series of rectangles. The region
940 * must be in screen-space coordinates.
942 void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode,
946 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
951 void debugOverdraw(bool enable, bool clear);
952 void renderOverdraw();
955 * Should be invoked every time the glScissor is modified.
957 inline void dirtyClip() {
961 inline mat4& currentTransform() const {
962 return *mSnapshot->transform;
965 // Dimensions of the drawing surface
968 // Matrix used for ortho projection in shaders
971 // Model-view matrix used to position/size objects
974 // Number of saved states
977 sp<Snapshot> mFirstSnapshot;
979 sp<Snapshot> mSnapshot;
980 // State used to define the clipping region
982 // Is the target render surface opaque
984 // Is a frame currently being rendered
987 // Used to draw textured quads
988 TextureVertex mMeshVertices[4];
990 // shader, filters, and shadow
991 DrawModifiers mDrawModifiers;
992 SkPaint mFilteredPaint;
996 Extensions& mExtensions;
998 // List of rectangles to clear after saveLayer() is invoked
999 Vector<Rect*> mLayers;
1000 // List of functors to invoke after a frame is drawn
1001 SortedVector<Functor*> mFunctors;
1002 // List of layers to update at the beginning of a frame
1003 Vector<Layer*> mLayerUpdates;
1005 // Indicates whether the clip must be restored
1008 // The following fields are used to setup drawing
1009 // Used to describe the shaders to generate
1010 ProgramDescription mDescription;
1011 // Color description
1013 float mColorA, mColorR, mColorG, mColorB;
1014 // Indicates that the shader should get a color
1015 bool mSetShaderColor;
1016 // Current texture unit
1017 GLuint mTextureUnit;
1018 // Track dirty regions, true by default
1019 bool mTrackDirtyRegions;
1020 // Indicate whether we are drawing an opaque frame
1023 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
1025 bool mScissorOptimizationDisabled;
1027 // No-ops start/endTiling when set
1028 bool mSuppressTiling;
1030 // Optional name of the renderer
1033 friend class DisplayListRenderer;
1035 friend class TextSetupFunctor;
1037 }; // class OpenGLRenderer
1039 }; // namespace uirenderer
1040 }; // namespace android
1042 #endif // ANDROID_HWUI_OPENGL_RENDERER_H