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;
64 int mPaintFilterClearBits;
65 int mPaintFilterSetBits;
68 enum StateDeferFlags {
69 kStateDeferFlag_Draw = 0x1,
70 kStateDeferFlag_Clip = 0x2
73 struct DeferredDisplayState {
74 Rect mBounds; // local bounds, mapped with matrix to be in screen space coordinates, clipped.
76 // the below are set and used by the OpenGLRenderer at record and deferred playback
79 DrawModifiers mDrawModifiers;
83 ///////////////////////////////////////////////////////////////////////////////
85 ///////////////////////////////////////////////////////////////////////////////
88 class TextSetupFunctor;
92 * OpenGL renderer used to draw accelerated 2D graphics. The API is a
93 * simplified version of Skia's Canvas API.
95 class OpenGLRenderer {
97 ANDROID_API OpenGLRenderer();
98 virtual ~OpenGLRenderer();
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.
105 ANDROID_API void setName(const char* name);
108 * Returns the name of this renderer as UTF8 string.
109 * The returned pointer is never null.
111 ANDROID_API const char* getName() const;
114 * Read externally defined properties to control the behavior
117 ANDROID_API void initProperties();
120 * Indicates whether this renderer executes drawing commands immediately.
121 * If this method returns true, the drawing commands will be executed
124 virtual bool isDeferred();
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.
130 * @param width The width in pixels of the underlysing surface
131 * @param height The height in pixels of the underlysing surface
133 virtual void setViewport(int width, int height);
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.
140 * @param opaque If true, the target surface is considered opaque
141 * and will not be cleared. If false, the target surface
144 ANDROID_API status_t prepare(bool opaque);
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.
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
160 virtual status_t prepareDirty(float left, float top, float right, float bottom, bool opaque);
163 * Indicates the end of a frame. This method must be invoked whenever
164 * the caller is done rendering a frame.
166 virtual void finish();
169 * This method must be invoked before handing control over to a draw functor.
170 * See callDrawGLFunction() for instance.
172 * This command must not be recorded inside display lists.
174 virtual void interrupt();
177 * This method must be invoked after getting control back from a draw functor.
179 * This command must not be recorded inside display lists.
181 virtual void resume();
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);
188 ANDROID_API void pushLayerUpdate(Layer* layer);
189 ANDROID_API void clearLayerUpdates();
191 ANDROID_API int getSaveCount() const;
192 virtual int save(int flags);
193 virtual void restore();
194 virtual void restoreToCount(int saveCount);
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);
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);
206 virtual int saveLayer(float left, float top, float right, float bottom,
207 int alpha, SkXfermode::Mode mode, int flags);
209 int saveLayerDeferred(float left, float top, float right, float bottom,
210 int alpha, SkXfermode::Mode mode, int flags);
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);
217 bool hasRectToRectTransform();
218 ANDROID_API void getMatrix(SkMatrix* matrix);
219 virtual void setMatrix(SkMatrix* matrix);
220 virtual void concatMatrix(SkMatrix* matrix);
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();
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);
266 virtual void resetShader();
267 virtual void setupShader(SkiaShader* shader);
269 virtual void resetColorFilter();
270 virtual void setupColorFilter(SkiaColorFilter* filter);
272 virtual void resetShadow();
273 virtual void setupShadow(float radius, float dx, float dy, int color);
275 virtual void resetPaintFilter();
276 virtual void setupPaintFilter(int clearBits, int setBits);
278 SkPaint* filterPaint(SkPaint* paint);
280 bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags);
281 void restoreDisplayState(const DeferredDisplayState& state);
283 const DrawModifiers& getDrawModifiers() { return mDrawModifiers; }
284 void setDrawModifiers(const DrawModifiers& drawModifiers) { mDrawModifiers = drawModifiers; }
286 // TODO: what does this mean? no perspective? no rotate?
287 ANDROID_API bool isCurrentTransformSimple() {
288 return mSnapshot->transform->isSimple();
291 Caches& getCaches() {
296 bool isCurrentClipSimple() {
297 return mSnapshot->clipRegion->isEmpty();
301 * Scales the alpha on the current snapshot. This alpha value will be modulated
302 * with other alpha values when drawing primitives.
304 void scaleAlpha(float alpha) {
305 mSnapshot->alpha *= alpha;
309 * Inserts a named event marker in the stream of GL commands.
311 void eventMark(const char* name) const;
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().
318 void startMark(const char* name) const;
321 * Closes the last group marker opened by startMark().
323 void endMark() const;
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.
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
334 static inline void getAlphaAndModeDirect(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
336 *mode = getXfermode(paint->getXfermode());
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;
343 *alpha = paint->getAlpha();
346 *mode = SkXfermode::kSrcOver_Mode;
352 * Return the best transform to use to rasterize text given a full
355 mat4 findBestFontTransform(const mat4& transform) const;
359 * Computes the projection matrix, initialize the first snapshot
360 * and stores the dimensions of the render target.
362 void initViewport(int width, int height);
365 * Perform the setup specific to a frame. This method does not
366 * issue any OpenGL commands.
368 void setupFrameState(float left, float top, float right, float bottom, bool opaque);
371 * Indicates the start of rendering. This method will setup the
372 * initial OpenGL state (viewport, clearing the buffer, etc.)
374 status_t startFrame();
377 * Clears the underlying surface if needed.
379 virtual status_t clear(float left, float top, float right, float bottom, bool opaque);
382 * Call this method after updating a layer during a drawing pass.
384 void resumeAfterLayer();
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).
391 virtual void ensureStencilBuffer();
394 * Obtains a stencil render buffer (allocating it if necessary) and
395 * attaches it to the specified layer.
397 void attachStencilBufferToLayer(Layer* layer);
400 * Compose the layer defined in the current snapshot with the layer
401 * defined by the previous snapshot.
403 * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
405 * @param curent The current snapshot containing the layer to compose
406 * @param previous The previous snapshot to compose the current layer with
408 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous);
411 * Marks the specified region as dirty at the specified bounds.
413 void dirtyLayerUnchecked(Rect& bounds, Region* region);
416 * Returns the current snapshot.
418 sp<Snapshot> getSnapshot() const {
423 * Returns the region of the current layer.
425 virtual Region* getRegion() const {
426 return mSnapshot->region;
430 * Indicates whether rendering is currently targeted at a layer.
432 virtual bool hasLayer() const {
433 return (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region;
437 * Returns the name of the FBO this renderer is rendering into.
439 virtual GLint getTargetFbo() const {
444 * Renders the specified layer as a textured quad.
446 * @param layer The layer to render
447 * @param rect The bounds of the layer
449 void drawTextureLayer(Layer* layer, const Rect& rect);
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.
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
459 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode);
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.
465 static inline SkXfermode::Mode getXfermode(SkXfermode* mode) {
466 SkXfermode::Mode resultMode;
467 if (!SkXfermode::AsMode(mode, &resultMode)) {
468 resultMode = SkXfermode::kSrcOver_Mode;
474 * Set to true to suppress error checks at the end of a frame.
476 virtual bool suppressErrorChecks() const {
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.
486 void discardFramebuffer(float left, float top, float right, float bottom);
489 * Ensures the state of the renderer is the same as the state of
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
498 * This method needs to be invoked every time getTargetFbo() is
501 void startTiling(const sp<Snapshot>& snapshot, bool opaque = false);
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
508 void startTiling(const Rect& clip, int windowHeight, bool opaque = false);
511 * Tells the GPU that we are done drawing the frame or that we
512 * are switching to another render target.
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.
521 * @param flags The save flags; see SkCanvas for more information
523 * @return The new save count. This value can be passed to #restoreToCount()
525 int saveSnapshot(int flags);
528 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous.
530 * @return True if the clip was modified.
532 bool restoreSnapshot();
535 * Sets the clipping rectangle using glScissor. The clip is defined by
536 * the current snapshot's clipRect member.
538 void setScissorFromClip();
541 * Sets the clipping region using the stencil buffer. The clip region
542 * is defined by the current snapshot's clipRegion member.
544 void setStencilFromClip();
547 * Performs a quick reject but does not affect the scissor. Returns
548 * the transformed rect to test and the current clip.
550 bool quickRejectNoScissor(float left, float top, float right, float bottom,
551 Rect& transformed, Rect& clip);
554 * Performs a quick reject but adjust the bounds to account for stroke width if necessary
556 bool quickRejectPreStroke(float left, float top, float right, float bottom, SkPaint* paint);
559 * Given the local bounds of the layer, calculates ...
561 void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
564 * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
566 void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
567 bool fboLayer, int alpha);
570 * Creates a new layer stored in the specified snapshot.
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
582 * @return True if the layer was successfully created, false otherwise
584 bool createLayer(float left, float top, float right, float bottom,
585 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo);
588 * Creates a new layer stored in the specified snapshot as an FBO.
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
595 bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo);
598 * Compose the specified layer as a region.
600 * @param layer The layer to compose
601 * @param rect The layer's bounds
603 void composeLayerRegion(Layer* layer, const Rect& rect);
606 * Compose the specified layer as a simple rectangle.
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
612 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
615 * Clears all the regions corresponding to the current list of layers.
616 * This method MUST be invoked before any drawing operation.
618 void clearLayerRegions();
621 * Mark the layer as dirty at the specified coordinates. The coordinates
622 * are transformed with the supplied matrix.
624 void dirtyLayer(const float left, const float top,
625 const float right, const float bottom, const mat4 transform);
628 * Mark the layer as dirty at the specified coordinates.
630 void dirtyLayer(const float left, const float top,
631 const float right, const float bottom);
634 * Draws a colored rectangle with the specified color. The specified coordinates
635 * are transformed by the current snapshot's transform matrix unless specified
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
646 void drawColorRect(float left, float top, float right, float bottom,
647 int color, SkXfermode::Mode mode, bool ignoreTransform = false);
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.
654 * @param rects A list of rectangles, 4 floats (left, top, right, bottom)
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
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);
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.
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
677 status_t drawShape(float left, float top, const PathTexture* texture, SkPaint* paint);
680 * Draws the specified texture as an alpha bitmap. Alpha bitmaps obey
681 * different compositing rules.
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
688 void drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint);
691 * Renders a strip of polygons with the specified paint, used for tessellated geometry.
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)
697 status_t drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
698 bool useOffset = false);
701 * Renders the convex hull defined by the specified path as a strip of polygons.
703 * @param path The hull of the path to draw
704 * @param paint The paint to render with
706 status_t drawConvexPath(const SkPath& path, SkPaint* paint);
709 * Draws a textured rectangle with the specified texture. The specified coordinates
710 * are transformed by the current snapshot's transform matrix.
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
721 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture,
722 float alpha, SkXfermode::Mode mode, bool blend);
725 * Draws a textured rectangle with the specified texture. The specified coordinates
726 * are transformed by the current snapshot's transform matrix.
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.
735 void drawTextureRect(float left, float top, float right, float bottom,
736 Texture* texture, SkPaint* paint);
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
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
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);
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);
772 * Draws text underline and strike-through if needed.
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
781 void drawTextDecorations(const char* text, int bytesCount, float length,
782 float x, float y, SkPaint* paint);
785 * Draws shadow layer on text (with optional positions).
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
798 void drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
799 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
803 * Draws a path texture. Path textures are alpha8 bitmaps that need special
804 * compositing to apply colors/filters/etc.
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
811 void drawPathTexture(const PathTexture* texture, float x, float y, SkPaint* paint);
814 * Resets the texture coordinates stored in mMeshVertices. Setting the values
815 * back to default is achieved by calling:
817 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
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
824 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
827 * Returns true if the specified paint will draw invisible text.
829 bool canSkipText(const SkPaint* paint) const;
832 * Binds the specified texture. The texture unit must have been selected
833 * prior to calling this method.
835 inline void bindTexture(GLuint texture) {
836 glBindTexture(GL_TEXTURE_2D, texture);
840 * Binds the specified EGLImage texture. The texture unit must have been selected
841 * prior to calling this method.
843 inline void bindExternalTexture(GLuint texture) {
844 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
848 * Enable or disable blending as necessary. This function sets the appropriate
849 * blend function based on the specified xfermode.
851 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description,
852 bool swapSrcDst = false);
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
860 * @param program The program to use
862 * @return true If the specified program was already in use, false otherwise.
864 inline bool useProgram(Program* program);
867 * Invoked before any drawing operation. This sets required state.
869 void setupDraw(bool clear = true);
872 * Various methods to setup OpenGL rendering.
874 void setupDrawWithTexture(bool isAlpha8 = false);
875 void setupDrawWithTextureAndColor(bool isAlpha8 = false);
876 void setupDrawWithExternalTexture();
877 void setupDrawNoTexture();
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);
916 bool updateLayer(Layer* layer, bool inFrame);
921 * Renders the specified region as a series of rectangles. This method
922 * is used for debugging only.
924 void drawRegionRects(const Region& region);
927 * Renders the specified region as a series of rectangles. The region
928 * must be in screen-space coordinates.
930 void drawRegionRects(const SkRegion& region, int color, SkXfermode::Mode mode,
934 * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
939 void debugOverdraw(bool enable, bool clear);
940 void renderOverdraw();
943 * Should be invoked every time the glScissor is modified.
945 inline void dirtyClip() {
949 inline mat4& currentTransform() const {
950 return *mSnapshot->transform;
953 // Dimensions of the drawing surface
956 // Matrix used for ortho projection in shaders
959 // Model-view matrix used to position/size objects
962 // Number of saved states
965 sp<Snapshot> mFirstSnapshot;
967 sp<Snapshot> mSnapshot;
968 // State used to define the clipping region
970 // Is the target render surface opaque
972 // Is a frame currently being rendered
975 // Used to draw textured quads
976 TextureVertex mMeshVertices[4];
978 // shader, filters, and shadow
979 DrawModifiers mDrawModifiers;
980 SkPaint mFilteredPaint;
984 Extensions& mExtensions;
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;
993 // Indicates whether the clip must be restored
996 // The following fields are used to setup drawing
997 // Used to describe the shaders to generate
998 ProgramDescription mDescription;
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
1011 // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
1013 bool mScissorOptimizationDisabled;
1015 // No-ops start/endTiling when set
1016 bool mSuppressTiling;
1018 // Optional name of the renderer
1021 friend class DisplayListRenderer;
1023 friend class TextSetupFunctor;
1025 }; // class OpenGLRenderer
1027 }; // namespace uirenderer
1028 }; // namespace android
1030 #endif // ANDROID_HWUI_OPENGL_RENDERER_H