OpenGLRenderer.h revision 50210d912925aef14e4ce69be82e4949122a3cd9
1bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/*
2bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Copyright (C) 2010 The Android Open Source Project
3bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
4489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane * Licensed under the Apache License, Version 2.0 (the "License");
5bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * you may not use this file except in compliance with the License.
6bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * You may obtain a copy of the License at
7bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
8bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *      http://www.apache.org/licenses/LICENSE-2.0
9bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane *
10bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Unless required by applicable law or agreed to in writing, software
11bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * distributed under the License is distributed on an "AS IS" BASIS,
12bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * See the License for the specific language governing permissions and
14bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * limitations under the License.
15bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
16bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
17bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#ifndef ANDROID_HWUI_OPENGL_RENDERER_H
18bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#define ANDROID_HWUI_OPENGL_RENDERER_H
19bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
20bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "CanvasState.h"
21bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Debug.h"
22bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Extensions.h"
23bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Matrix.h"
24bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Program.h"
25bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Rect.h"
26bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Snapshot.h"
27bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "UvMapper.h"
28bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Vertex.h"
29bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "Caches.h"
30bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include "utils/PaintUtils.h"
31bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
32bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <GLES2/gl2.h>
33bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <GLES2/gl2ext.h>
34bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
35bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkBitmap.h>
36bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkCanvas.h>
37489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane#include <SkColorFilter.h>
38bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkMatrix.h>
39bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkPaint.h>
40bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkRegion.h>
41bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <SkXfermode.h>
42bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
43bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <utils/Blur.h>
44e5eaf37440b8e337ab150c017df7c03faf846c51DRC#include <utils/Functor.h>
45bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <utils/RefBase.h>
46bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <utils/SortedVector.h>
47bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <utils/Vector.h>
48bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
49bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <cutils/compiler.h>
50bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
51bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane#include <androidfw/ResourceTypes.h>
52bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
53bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass SkShader;
54bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
55bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanenamespace android {
56bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanenamespace uirenderer {
57bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
58bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneenum class DrawOpMode {
59bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kImmediate,
60bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kDefer,
61bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kFlush
62bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane};
63bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
64bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass DeferredDisplayState;
65bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanestruct Glop;
66bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass RenderState;
67bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass RenderNode;
68bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass TextDrawFunctor;
69bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass VertexBuffer;
70bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
71bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneenum StateDeferFlags {
72bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kStateDeferFlag_Draw = 0x1,
73bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kStateDeferFlag_Clip = 0x2
74bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane};
75bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
76489583f5165e05d37302e8eeec58104ea0109127Thomas G. Laneenum ClipSideFlags {
77bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_None = 0x0,
78e5eaf37440b8e337ab150c017df7c03faf846c51DRC    kClipSide_Left = 0x1,
79bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_Top = 0x2,
80bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_Right = 0x4,
81bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_Bottom = 0x8,
82bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_Full = 0xF,
83bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kClipSide_ConservativeFull = 0x1F
84bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane};
85bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
86bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneenum VertexBufferDisplayFlags {
87bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kVertexBuffer_Offset = 0x1,
88bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kVertexBuffer_ShadowInterp = 0x2,
89bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane};
90bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
91bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/**
92bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * Defines additional transformation that should be applied by the model view matrix, beyond that of
93bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * the currentTransform()
94bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
95bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneenum ModelViewMode {
96bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /**
97bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * Used when the model view should simply translate geometry passed to the shader. The resulting
98bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * matrix will be a simple translation.
99bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
100bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kModelViewMode_Translate = 0,
101bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
102bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /**
103bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * Used when the model view should translate and scale geometry. The resulting matrix will be a
104bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * translation + scale. This is frequently used together with VBO 0, the (0,0,1,1) rect.
105bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
106bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    kModelViewMode_TranslateAndScale = 1,
107bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane};
108bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
109bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane///////////////////////////////////////////////////////////////////////////////
110bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane// Renderer
111bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane///////////////////////////////////////////////////////////////////////////////
112bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane/**
113bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane * OpenGL Renderer implementation.
114bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane */
115bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Laneclass OpenGLRenderer : public CanvasStateClient {
116bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lanepublic:
117bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    OpenGLRenderer(RenderState& renderState);
118bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    virtual ~OpenGLRenderer();
119489583f5165e05d37302e8eeec58104ea0109127Thomas G. Lane
120bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /**
121e5eaf37440b8e337ab150c017df7c03faf846c51DRC     * Sets the dimension of the underlying drawing surface. This method must
122bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * be called at least once every time the drawing surface changes size.
123bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *
124bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * @param width The width in pixels of the underlysing surface
125bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * @param height The height in pixels of the underlysing surface
126bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
127bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    void setViewport(int width, int height) { mState.setViewport(width, height); }
128bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
129bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    void initProperties();
130bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    void initLight(float lightRadius, uint8_t ambientShadowAlpha,
131bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane            uint8_t spotShadowAlpha);
132bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    void setLightCenter(const Vector3& lightCenter);
133bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
134bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /*
135bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * Prepares the renderer to draw a frame. This method must be invoked
136bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * at the beginning of each frame. Only the specified rectangle of the
137bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * frame is assumed to be dirty. A clip will automatically be set to
138bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * the specified rectangle.
139bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *
140bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * @param opaque If true, the target surface is considered opaque
141bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *               and will not be cleared. If false, the target surface
142bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *               will be cleared
143bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
144bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    virtual void prepareDirty(float left, float top, float right, float bottom,
145bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane            bool opaque);
146bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
147bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /**
148bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * Prepares the renderer to draw a frame. This method must be invoked
149bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * at the beginning of each frame. When this method is invoked, the
150bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * entire drawing surface is assumed to be redrawn.
151bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *
152bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * @param opaque If true, the target surface is considered opaque
153bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *               and will not be cleared. If false, the target surface
154bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     *               will be cleared
155bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     */
156bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    void prepare(bool opaque) {
157bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane        prepareDirty(0.0f, 0.0f, mState.getWidth(), mState.getHeight(), opaque);
158bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    }
159bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane
160bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane    /**
161bc79e0680a45d1ca330d690dae0340c8e17ab5e3Thomas G. Lane     * Indicates the end of a frame. This method must be invoked whenever
162     * the caller is done rendering a frame.
163     * Returns true if any drawing was done during the frame (the output
164     * has changed / is "dirty" and should be displayed to the user).
165     */
166    virtual bool finish();
167
168    void callDrawGLFunction(Functor* functor, Rect& dirty);
169
170    void pushLayerUpdate(Layer* layer);
171    void cancelLayerUpdate(Layer* layer);
172    void flushLayerUpdates();
173    void markLayersAsBuildLayers();
174
175    virtual int saveLayer(float left, float top, float right, float bottom,
176            const SkPaint* paint, int flags) {
177        return saveLayer(left, top, right, bottom, paint, flags, nullptr);
178    }
179
180    // Specialized saveLayer implementation, which will pass the convexMask to an FBO layer, if
181    // created, which will in turn clip to that mask when drawn back/restored.
182    int saveLayer(float left, float top, float right, float bottom,
183            const SkPaint* paint, int flags, const SkPath* convexMask);
184
185    int saveLayerDeferred(float left, float top, float right, float bottom,
186            const SkPaint* paint, int flags);
187
188    void drawRenderNode(RenderNode* displayList, Rect& dirty, int32_t replayFlags = 1);
189    void drawLayer(Layer* layer, float x, float y);
190    void drawBitmap(const SkBitmap* bitmap, const SkPaint* paint);
191    void drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
192            TextureVertex* vertices, bool pureTranslate, const Rect& bounds, const SkPaint* paint);
193    void drawBitmap(const SkBitmap* bitmap, Rect src, Rect dst,
194            const SkPaint* paint);
195    void drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
196            const float* vertices, const int* colors, const SkPaint* paint);
197    void drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
198            TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint);
199    void drawPatch(const SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
200            float left, float top, float right, float bottom, const SkPaint* paint);
201    void drawColor(int color, SkXfermode::Mode mode);
202    void drawRect(float left, float top, float right, float bottom,
203            const SkPaint* paint);
204    void drawRoundRect(float left, float top, float right, float bottom,
205            float rx, float ry, const SkPaint* paint);
206    void drawCircle(float x, float y, float radius, const SkPaint* paint);
207    void drawOval(float left, float top, float right, float bottom,
208            const SkPaint* paint);
209    void drawArc(float left, float top, float right, float bottom,
210            float startAngle, float sweepAngle, bool useCenter, const SkPaint* paint);
211    void drawPath(const SkPath* path, const SkPaint* paint);
212    void drawLines(const float* points, int count, const SkPaint* paint);
213    void drawPoints(const float* points, int count, const SkPaint* paint);
214    void drawTextOnPath(const char* text, int bytesCount, int count, const SkPath* path,
215            float hOffset, float vOffset, const SkPaint* paint);
216    void drawPosText(const char* text, int bytesCount, int count,
217            const float* positions, const SkPaint* paint);
218    void drawText(const char* text, int bytesCount, int count, float x, float y,
219            const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
220            DrawOpMode drawOpMode = DrawOpMode::kImmediate);
221    void drawRects(const float* rects, int count, const SkPaint* paint);
222
223    void drawShadow(float casterAlpha,
224            const VertexBuffer* ambientShadowVertexBuffer,
225            const VertexBuffer* spotShadowVertexBuffer);
226
227    void setDrawFilter(SkDrawFilter* filter);
228
229    /**
230     * Store the current display state (most importantly, the current clip and transform), and
231     * additionally map the state's bounds from local to window coordinates.
232     *
233     * Returns true if quick-rejected
234     */
235    bool storeDisplayState(DeferredDisplayState& state, int stateDeferFlags);
236    void restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore = false);
237    void setupMergedMultiDraw(const Rect* clipRect);
238
239    bool isCurrentTransformSimple() {
240        return currentTransform()->isSimple();
241    }
242
243    Caches& getCaches() {
244        return mCaches;
245    }
246
247    RenderState& renderState() {
248        return mRenderState;
249    }
250
251    int getViewportWidth() { return mState.getViewportWidth(); }
252    int getViewportHeight() { return mState.getViewportHeight(); }
253
254    /**
255     * Scales the alpha on the current snapshot. This alpha value will be modulated
256     * with other alpha values when drawing primitives.
257     */
258    void scaleAlpha(float alpha) { mState.scaleAlpha(alpha); }
259
260    /**
261     * Inserts a named event marker in the stream of GL commands.
262     */
263    void eventMark(const char* name) const;
264
265    /**
266     * Inserts a formatted event marker in the stream of GL commands.
267     */
268    void eventMarkDEBUG(const char *fmt, ...) const;
269
270    /**
271     * Inserts a named group marker in the stream of GL commands. This marker
272     * can be used by tools to group commands into logical groups. A call to
273     * this method must always be followed later on by a call to endMark().
274     */
275    void startMark(const char* name) const;
276
277    /**
278     * Closes the last group marker opened by startMark().
279     */
280    void endMark() const;
281
282    /**
283     * Gets the alpha and xfermode out of a paint object. If the paint is null
284     * alpha will be 255 and the xfermode will be SRC_OVER. This method does
285     * not multiply the paint's alpha by the current snapshot's alpha, and does
286     * not replace the alpha with the overrideLayerAlpha
287     *
288     * @param paint The paint to extract values from
289     * @param alpha Where to store the resulting alpha
290     * @param mode Where to store the resulting xfermode
291     */
292    static inline void getAlphaAndModeDirect(const SkPaint* paint, int* alpha,
293            SkXfermode::Mode* mode) {
294        *mode = getXfermodeDirect(paint);
295        *alpha = getAlphaDirect(paint);
296    }
297
298    static inline SkXfermode::Mode getXfermodeDirect(const SkPaint* paint) {
299        if (!paint) return SkXfermode::kSrcOver_Mode;
300        return PaintUtils::getXfermode(paint->getXfermode());
301    }
302
303    static inline int getAlphaDirect(const SkPaint* paint) {
304        if (!paint) return 255;
305        return paint->getAlpha();
306    }
307
308    struct TextShadow {
309        SkScalar radius;
310        float dx;
311        float dy;
312        SkColor color;
313    };
314
315    static inline bool getTextShadow(const SkPaint* paint, TextShadow* textShadow) {
316        SkDrawLooper::BlurShadowRec blur;
317        if (paint && paint->getLooper() && paint->getLooper()->asABlurShadow(&blur)) {
318            if (textShadow) {
319                textShadow->radius = Blur::convertSigmaToRadius(blur.fSigma);
320                textShadow->dx = blur.fOffset.fX;
321                textShadow->dy = blur.fOffset.fY;
322                textShadow->color = blur.fColor;
323            }
324            return true;
325        }
326        return false;
327    }
328
329    static inline bool hasTextShadow(const SkPaint* paint) {
330        return getTextShadow(paint, nullptr);
331    }
332
333    /**
334     * Build the best transform to use to rasterize text given a full
335     * transform matrix, and whether filteration is needed.
336     *
337     * Returns whether filtration is needed
338     */
339    bool findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const;
340
341#if DEBUG_MERGE_BEHAVIOR
342    void drawScreenSpaceColorRect(float left, float top, float right, float bottom, int color) {
343        mCaches.setScissorEnabled(false);
344
345        // should only be called outside of other draw ops, so stencil can only be in test state
346        bool stencilWasEnabled = mCaches.stencil.isTestEnabled();
347        mCaches.stencil.disable();
348
349        drawColorRect(left, top, right, bottom, color, SkXfermode::kSrcOver_Mode, true);
350
351        if (stencilWasEnabled) mCaches.stencil.enableTest();
352        mDirty = true;
353    }
354#endif
355
356    const Vector3& getLightCenter() const { return mState.currentLightCenter(); }
357    float getLightRadius() const { return mLightRadius; }
358    uint8_t getAmbientShadowAlpha() const { return mAmbientShadowAlpha; }
359    uint8_t getSpotShadowAlpha() const { return mSpotShadowAlpha; }
360
361    ///////////////////////////////////////////////////////////////////
362    /// State manipulation
363
364    int getSaveCount() const;
365    int save(int flags);
366    void restore();
367    void restoreToCount(int saveCount);
368
369    void getMatrix(SkMatrix* outMatrix) const { mState.getMatrix(outMatrix); }
370    void setMatrix(const SkMatrix& matrix) { mState.setMatrix(matrix); }
371    void concatMatrix(const SkMatrix& matrix) { mState.concatMatrix(matrix); }
372
373    void translate(float dx, float dy, float dz = 0.0f);
374    void rotate(float degrees);
375    void scale(float sx, float sy);
376    void skew(float sx, float sy);
377
378    void setMatrix(const Matrix4& matrix); // internal only convenience method
379    void concatMatrix(const Matrix4& matrix); // internal only convenience method
380
381    const Rect& getLocalClipBounds() const { return mState.getLocalClipBounds(); }
382    const Rect& getRenderTargetClipBounds() const { return mState.getRenderTargetClipBounds(); }
383    bool quickRejectConservative(float left, float top,
384            float right, float bottom) const {
385        return mState.quickRejectConservative(left, top, right, bottom);
386    }
387
388    bool clipRect(float left, float top,
389            float right, float bottom, SkRegion::Op op);
390    bool clipPath(const SkPath* path, SkRegion::Op op);
391    bool clipRegion(const SkRegion* region, SkRegion::Op op);
392
393    /**
394     * Does not support different clipping Ops (that is, every call to setClippingOutline is
395     * effectively using SkRegion::kReplaceOp)
396     *
397     * The clipping outline is independent from the regular clip.
398     */
399    void setClippingOutline(LinearAllocator& allocator, const Outline* outline);
400    void setClippingRoundRect(LinearAllocator& allocator,
401            const Rect& rect, float radius, bool highPriority = true);
402
403    inline bool hasRectToRectTransform() const { return mState.hasRectToRectTransform(); }
404    inline const mat4* currentTransform() const { return mState.currentTransform(); }
405
406    ///////////////////////////////////////////////////////////////////
407    /// CanvasStateClient interface
408
409    virtual void onViewportInitialized() override;
410    virtual void onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) override;
411    virtual GLuint getTargetFbo() const override { return 0; }
412
413    SkPath* allocPathForFrame() {
414        std::unique_ptr<SkPath> path(new SkPath());
415        SkPath* returnPath = path.get();
416        mTempPaths.push_back(std::move(path));
417        return returnPath;
418    }
419
420protected:
421    /**
422     * Perform the setup specific to a frame. This method does not
423     * issue any OpenGL commands.
424     */
425    void setupFrameState(float left, float top, float right, float bottom, bool opaque);
426
427    /**
428     * Indicates the start of rendering. This method will setup the
429     * initial OpenGL state (viewport, clearing the buffer, etc.)
430     */
431    void startFrame();
432
433    /**
434     * Clears the underlying surface if needed.
435     */
436    virtual void clear(float left, float top, float right, float bottom, bool opaque);
437
438    /**
439     * Call this method after updating a layer during a drawing pass.
440     */
441    void resumeAfterLayer();
442
443    /**
444     * This method is called whenever a stencil buffer is required. Subclasses
445     * should override this method and call attachStencilBufferToLayer() on the
446     * appropriate layer(s).
447     */
448    virtual void ensureStencilBuffer();
449
450    /**
451     * Obtains a stencil render buffer (allocating it if necessary) and
452     * attaches it to the specified layer.
453     */
454    void attachStencilBufferToLayer(Layer* layer);
455
456    /**
457     * Draw a rectangle list. Currently only used for the the stencil buffer so that the stencil
458     * will have a value of 'n' in every unclipped pixel, where 'n' is the number of rectangles
459     * in the list.
460     */
461    void drawRectangleList(const RectangleList& rectangleList);
462
463    bool quickRejectSetupScissor(float left, float top, float right, float bottom,
464            const SkPaint* paint = nullptr);
465    bool quickRejectSetupScissor(const Rect& bounds, const SkPaint* paint = nullptr) {
466        return quickRejectSetupScissor(bounds.left, bounds.top,
467                bounds.right, bounds.bottom, paint);
468    }
469
470    /**
471     * Compose the layer defined in the current snapshot with the layer
472     * defined by the previous snapshot.
473     *
474     * The current snapshot *must* be a layer (flag kFlagIsLayer set.)
475     *
476     * @param curent The current snapshot containing the layer to compose
477     * @param previous The previous snapshot to compose the current layer with
478     */
479    virtual void composeLayer(const Snapshot& current, const Snapshot& previous);
480
481    /**
482     * Marks the specified region as dirty at the specified bounds.
483     */
484    void dirtyLayerUnchecked(Rect& bounds, Region* region);
485
486    /**
487     * Returns the region of the current layer.
488     */
489    virtual Region* getRegion() const {
490        return mState.currentRegion();
491    }
492
493    /**
494     * Indicates whether rendering is currently targeted at a layer.
495     */
496    virtual bool hasLayer() const {
497        return (mState.currentFlags() & Snapshot::kFlagFboTarget) && mState.currentRegion();
498    }
499
500    /**
501     * Renders the specified layer as a textured quad.
502     *
503     * @param layer The layer to render
504     * @param rect The bounds of the layer
505     */
506    void drawTextureLayer(Layer* layer, const Rect& rect);
507
508    /**
509     * Gets the alpha and xfermode out of a paint object. If the paint is null
510     * alpha will be 255 and the xfermode will be SRC_OVER. Accounts for snapshot alpha.
511     *
512     * @param paint The paint to extract values from
513     * @param alpha Where to store the resulting alpha
514     * @param mode Where to store the resulting xfermode
515     */
516    inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const;
517
518    /**
519     * Gets the alpha from a layer, accounting for snapshot alpha
520     *
521     * @param layer The layer from which the alpha is extracted
522     */
523    inline float getLayerAlpha(const Layer* layer) const;
524
525    /**
526     * Safely retrieves the ColorFilter from the given Paint. If the paint is
527     * null then null is returned.
528     */
529    static inline SkColorFilter* getColorFilter(const SkPaint* paint) {
530        return paint ? paint->getColorFilter() : nullptr;
531    }
532
533    /**
534     * Safely retrieves the Shader from the given Paint. If the paint is
535     * null then null is returned.
536     */
537    static inline const SkShader* getShader(const SkPaint* paint) {
538        return paint ? paint->getShader() : nullptr;
539    }
540
541    /**
542     * Set to true to suppress error checks at the end of a frame.
543     */
544    virtual bool suppressErrorChecks() const {
545        return false;
546    }
547
548    CanvasState mState;
549    Caches& mCaches;
550    RenderState& mRenderState;
551
552private:
553    void renderGlop(const Glop& glop, bool clearLayer = true);
554
555    /**
556     * Discards the content of the framebuffer if supported by the driver.
557     * This method should be called at the beginning of a frame to optimize
558     * rendering on some tiler architectures.
559     */
560    void discardFramebuffer(float left, float top, float right, float bottom);
561
562    /**
563     * Tells the GPU what part of the screen is about to be redrawn.
564     * This method will use the current layer space clip rect.
565     * This method needs to be invoked every time getTargetFbo() is
566     * bound again.
567     */
568    void startTilingCurrentClip(bool opaque = false, bool expand = false);
569
570    /**
571     * Tells the GPU what part of the screen is about to be redrawn.
572     * This method needs to be invoked every time getTargetFbo() is
573     * bound again.
574     */
575    void startTiling(const Rect& clip, int windowHeight, bool opaque = false, bool expand = false);
576
577    /**
578     * Tells the GPU that we are done drawing the frame or that we
579     * are switching to another render target.
580     */
581    void endTiling();
582
583    /**
584     * Sets the clipping rectangle using glScissor. The clip is defined by
585     * the current snapshot's clipRect member.
586     */
587    void setScissorFromClip();
588
589    /**
590     * Sets the clipping region using the stencil buffer. The clip region
591     * is defined by the current snapshot's clipRegion member.
592     */
593    void setStencilFromClip();
594
595    /**
596     * Given the local bounds of the layer, calculates ...
597     */
598    void calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer);
599
600    /**
601     * Given the local bounds + clip of the layer, updates current snapshot's empty/invisible
602     */
603    void updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
604            bool fboLayer, int alpha);
605
606    /**
607     * Creates a new layer stored in the specified snapshot.
608     *
609     * @param snapshot The snapshot associated with the new layer
610     * @param left The left coordinate of the layer
611     * @param top The top coordinate of the layer
612     * @param right The right coordinate of the layer
613     * @param bottom The bottom coordinate of the layer
614     * @param alpha The translucency of the layer
615     * @param mode The blending mode of the layer
616     * @param flags The layer save flags
617     * @param mask A mask to use when drawing the layer back, may be empty
618     *
619     * @return True if the layer was successfully created, false otherwise
620     */
621    bool createLayer(float left, float top, float right, float bottom,
622            const SkPaint* paint, int flags, const SkPath* convexMask);
623
624    /**
625     * Creates a new layer stored in the specified snapshot as an FBO.
626     *
627     * @param layer The layer to store as an FBO
628     * @param snapshot The snapshot associated with the new layer
629     * @param bounds The bounds of the layer
630     */
631    bool createFboLayer(Layer* layer, Rect& bounds, Rect& clip);
632
633    /**
634     * Compose the specified layer as a region.
635     *
636     * @param layer The layer to compose
637     * @param rect The layer's bounds
638     */
639    void composeLayerRegion(Layer* layer, const Rect& rect);
640
641    /**
642     * Compose the specified layer as a simple rectangle.
643     *
644     * @param layer The layer to compose
645     * @param rect The layer's bounds
646     * @param swap If true, the source and destination are swapped
647     */
648    void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false);
649
650    /**
651     * Clears all the regions corresponding to the current list of layers.
652     * This method MUST be invoked before any drawing operation.
653     */
654    void clearLayerRegions();
655
656    /**
657     * Mark the layer as dirty at the specified coordinates. The coordinates
658     * are transformed with the supplied matrix.
659     */
660    void dirtyLayer(const float left, const float top,
661            const float right, const float bottom, const Matrix4& transform);
662
663    /**
664     * Mark the layer as dirty at the specified coordinates.
665     */
666    void dirtyLayer(const float left, const float top,
667            const float right, const float bottom);
668
669    /**
670     * Draws a colored rectangle with the specified color. The specified coordinates
671     * are transformed by the current snapshot's transform matrix unless specified
672     * otherwise.
673     *
674     * @param left The left coordinate of the rectangle
675     * @param top The top coordinate of the rectangle
676     * @param right The right coordinate of the rectangle
677     * @param bottom The bottom coordinate of the rectangle
678     * @param paint The paint containing the color, blending mode, etc.
679     * @param ignoreTransform True if the current transform should be ignored
680     */
681    void drawColorRect(float left, float top, float right, float bottom,
682            const SkPaint* paint, 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 paint The paint containing the color, blending mode, etc.
692     * @param ignoreTransform True if the current transform should be ignored
693     * @param dirty True if calling this method should dirty the current layer
694     * @param clip True if the rects should be clipped, false otherwise
695     */
696    void drawColorRects(const float* rects, int count, const SkPaint* paint,
697            bool ignoreTransform = false, bool dirty = true, bool clip = true);
698
699    /**
700     * Draws the shape represented by the specified path texture.
701     * This method invokes drawPathTexture() but takes into account
702     * the extra left/top offset and the texture offset to correctly
703     * position the final shape.
704     *
705     * @param left The left coordinate of the shape to render
706     * @param top The top coordinate of the shape to render
707     * @param texture The texture reprsenting the shape
708     * @param paint The paint to draw the shape with
709     */
710    void drawShape(float left, float top, PathTexture* texture, const SkPaint* paint);
711
712    /**
713     * Renders a strip of polygons with the specified paint, used for tessellated geometry.
714     *
715     * @param vertexBuffer The VertexBuffer to be drawn
716     * @param paint The paint to render with
717     * @param flags flags with which to draw
718     */
719    void drawVertexBuffer(float translateX, float translateY, const VertexBuffer& vertexBuffer,
720            const SkPaint* paint, int flags = 0);
721
722    /**
723     * Convenience for translating method
724     */
725    void drawVertexBuffer(const VertexBuffer& vertexBuffer,
726            const SkPaint* paint, int flags = 0) {
727        drawVertexBuffer(0.0f, 0.0f, vertexBuffer, paint, flags);
728    }
729
730    /**
731     * Renders the convex hull defined by the specified path as a strip of polygons.
732     *
733     * @param path The hull of the path to draw
734     * @param paint The paint to render with
735     */
736    void drawConvexPath(const SkPath& path, const SkPaint* paint);
737
738    /**
739     * Draws text underline and strike-through if needed.
740     *
741     * @param text The text to decor
742     * @param bytesCount The number of bytes in the text
743     * @param totalAdvance The total advance in pixels, defines underline/strikethrough length
744     * @param x The x coordinate where the text will be drawn
745     * @param y The y coordinate where the text will be drawn
746     * @param paint The paint to draw the text with
747     */
748    void drawTextDecorations(float totalAdvance, float x, float y, const SkPaint* paint);
749
750   /**
751     * Draws shadow layer on text (with optional positions).
752     *
753     * @param paint The paint to draw the shadow with
754     * @param text The text to draw
755     * @param bytesCount The number of bytes in the text
756     * @param count The number of glyphs in the text
757     * @param positions The x, y positions of individual glyphs (or NULL)
758     * @param fontRenderer The font renderer object
759     * @param alpha The alpha value for drawing the shadow
760     * @param x The x coordinate where the shadow will be drawn
761     * @param y The y coordinate where the shadow will be drawn
762     */
763    void drawTextShadow(const SkPaint* paint, const char* text, int bytesCount, int count,
764            const float* positions, FontRenderer& fontRenderer, int alpha,
765            float x, float y);
766
767    /**
768     * Draws a path texture. Path textures are alpha8 bitmaps that need special
769     * compositing to apply colors/filters/etc.
770     *
771     * @param texture The texture to render
772     * @param x The x coordinate where the texture will be drawn
773     * @param y The y coordinate where the texture will be drawn
774     * @param paint The paint to draw the texture with
775     */
776     void drawPathTexture(PathTexture* texture, float x, float y, const SkPaint* paint);
777
778    /**
779     * Resets the texture coordinates stored in mMeshVertices. Setting the values
780     * back to default is achieved by calling:
781     *
782     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
783     *
784     * @param u1 The left coordinate of the texture
785     * @param v1 The bottom coordinate of the texture
786     * @param u2 The right coordinate of the texture
787     * @param v2 The top coordinate of the texture
788     */
789    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);
790
791    /**
792     * Returns true if the specified paint will draw invisible text.
793     */
794    bool canSkipText(const SkPaint* paint) const;
795
796    bool updateLayer(Layer* layer, bool inFrame);
797    void updateLayers();
798    void flushLayers();
799
800#if DEBUG_LAYERS_AS_REGIONS
801    /**
802     * Renders the specified region as a series of rectangles. This method
803     * is used for debugging only.
804     */
805    void drawRegionRectsDebug(const Region& region);
806#endif
807
808    /**
809     * Renders the specified region as a series of rectangles. The region
810     * must be in screen-space coordinates.
811     */
812    void drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty = false);
813
814    /**
815     * Draws the current clip region if any. Only when DEBUG_CLIP_REGIONS
816     * is turned on.
817     */
818    void debugClip();
819
820    void debugOverdraw(bool enable, bool clear);
821    void renderOverdraw();
822    void countOverdraw();
823
824    /**
825     * Should be invoked every time the glScissor is modified.
826     */
827    inline void dirtyClip() { mState.setDirtyClip(true); }
828
829    inline const UvMapper& getMapper(const Texture* texture) {
830        return texture && texture->uvMapper ? *texture->uvMapper : mUvMapper;
831    }
832
833    /**
834     * Returns a texture object for the specified bitmap. The texture can
835     * come from the texture cache or an atlas. If this method returns
836     * NULL, the texture could not be found and/or allocated.
837     */
838    Texture* getTexture(const SkBitmap* bitmap);
839
840    bool reportAndClearDirty() { bool ret = mDirty; mDirty = false; return ret; }
841    inline Snapshot* writableSnapshot() { return mState.writableSnapshot(); }
842    inline const Snapshot* currentSnapshot() const { return mState.currentSnapshot(); }
843
844    // State used to define the clipping region
845    Rect mTilingClip;
846    // Is the target render surface opaque
847    bool mOpaque;
848    // Is a frame currently being rendered
849    bool mFrameStarted;
850
851    // Default UV mapper
852    const UvMapper mUvMapper;
853
854    // List of rectangles to clear after saveLayer() is invoked
855    std::vector<Rect> mLayers;
856    // List of layers to update at the beginning of a frame
857    Vector< sp<Layer> > mLayerUpdates;
858
859    // See PROPERTY_DISABLE_SCISSOR_OPTIMIZATION in
860    // Properties.h
861    bool mScissorOptimizationDisabled;
862
863    // No-ops start/endTiling when set
864    bool mSuppressTiling;
865    bool mFirstFrameAfterResize;
866
867    bool mSkipOutlineClip;
868
869    // True if anything has been drawn since the last call to
870    // reportAndClearDirty()
871    bool mDirty;
872
873    // Lighting + shadows
874    Vector3 mLightCenter;
875    float mLightRadius;
876    uint8_t mAmbientShadowAlpha;
877    uint8_t mSpotShadowAlpha;
878
879    // Paths kept alive for the duration of the frame
880    std::vector<std::unique_ptr<SkPath>> mTempPaths;
881
882    friend class Layer;
883    friend class TextDrawFunctor;
884    friend class DrawBitmapOp;
885    friend class DrawPatchOp;
886
887}; // class OpenGLRenderer
888
889}; // namespace uirenderer
890}; // namespace android
891
892#endif // ANDROID_HWUI_OPENGL_RENDERER_H
893