OpenGLRenderer.cpp revision 121e2242565d5f09ad83a2d33ecd2225838802c5
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <SkCanvas.h>
24
25#include <cutils/properties.h>
26#include <utils/Log.h>
27
28#include "OpenGLRenderer.h"
29
30namespace android {
31namespace uirenderer {
32
33///////////////////////////////////////////////////////////////////////////////
34// Defines
35///////////////////////////////////////////////////////////////////////////////
36
37// These properties are defined in mega-bytes
38#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
39#define PROPERTY_LAYER_CACHE_SIZE "ro.hwui.layer_cache_size"
40
41// Converts a number of mega-bytes into bytes
42#define MB(s) s * 1024 * 1024
43
44#define DEFAULT_TEXTURE_CACHE_SIZE MB(20)
45#define DEFAULT_LAYER_CACHE_SIZE MB(10)
46
47#define SV(x, y) { { x, y } }
48#define FV(x, y, u, v) { { x, y }, { u, v } }
49
50///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
54static const SimpleVertex gDrawColorVertices[] = {
55        SV(0.0f, 0.0f),
56        SV(1.0f, 0.0f),
57        SV(0.0f, 1.0f),
58        SV(1.0f, 1.0f)
59};
60static const GLsizei gDrawColorVertexStride = sizeof(SimpleVertex);
61static const GLsizei gDrawColorVertexCount = 4;
62
63// This array is never used directly but used as a memcpy source in the
64// OpenGLRenderer constructor
65static const TextureVertex gDrawTextureVertices[] = {
66        FV(0.0f, 0.0f, 0.0f, 0.0f),
67        FV(1.0f, 0.0f, 1.0f, 0.0f),
68        FV(0.0f, 1.0f, 0.0f, 1.0f),
69        FV(1.0f, 1.0f, 1.0f, 1.0f)
70};
71static const GLsizei gDrawTextureVertexStride = sizeof(TextureVertex);
72static const GLsizei gDrawTextureVertexCount = 4;
73
74// In this array, the index of each Blender equals the value of the first
75// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
76static const Blender gBlends[] = {
77        { SkXfermode::kClear_Mode,   GL_ZERO,                 GL_ZERO },
78        { SkXfermode::kSrc_Mode,     GL_ONE,                  GL_ZERO },
79        { SkXfermode::kDst_Mode,     GL_ZERO,                 GL_ONE },
80        { SkXfermode::kSrcOver_Mode, GL_ONE,                  GL_ONE_MINUS_SRC_ALPHA },
81        { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_ONE },
82        { SkXfermode::kSrcIn_Mode,   GL_DST_ALPHA,            GL_ZERO },
83        { SkXfermode::kDstIn_Mode,   GL_ZERO,                 GL_SRC_ALPHA },
84        { SkXfermode::kSrcOut_Mode,  GL_ONE_MINUS_DST_ALPHA,  GL_ZERO },
85        { SkXfermode::kDstOut_Mode,  GL_ZERO,                 GL_ONE_MINUS_SRC_ALPHA },
86        { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA,            GL_ONE_MINUS_SRC_ALPHA },
87        { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA,  GL_SRC_ALPHA },
88        { SkXfermode::kXor_Mode,     GL_ONE_MINUS_DST_ALPHA,  GL_ONE_MINUS_SRC_ALPHA }
89};
90
91///////////////////////////////////////////////////////////////////////////////
92// Constructors/destructor
93///////////////////////////////////////////////////////////////////////////////
94
95OpenGLRenderer::OpenGLRenderer(): mTextureCache(DEFAULT_TEXTURE_CACHE_SIZE) {
96    LOGD("Create OpenGLRenderer");
97
98    char property[PROPERTY_VALUE_MAX];
99    if (property_get(PROPERTY_TEXTURE_CACHE_SIZE, property, NULL) > 0) {
100        mTextureCache.setMaxSize(MB(atoi(property)));
101    }
102
103    mDrawColorShader = new DrawColorProgram;
104    mDrawTextureShader = new DrawTextureProgram;
105
106    memcpy(mDrawTextureVertices, gDrawTextureVertices, sizeof(gDrawTextureVertices));
107}
108
109OpenGLRenderer::~OpenGLRenderer() {
110    LOGD("Destroy OpenGLRenderer");
111
112    mTextureCache.clear();
113}
114
115///////////////////////////////////////////////////////////////////////////////
116// Setup
117///////////////////////////////////////////////////////////////////////////////
118
119void OpenGLRenderer::setViewport(int width, int height) {
120    glViewport(0, 0, width, height);
121
122    mat4 ortho;
123    ortho.loadOrtho(0, width, height, 0, -1, 1);
124    ortho.copyTo(mOrthoMatrix);
125
126    mWidth = width;
127    mHeight = height;
128    mFirstSnapshot.height = height;
129}
130
131void OpenGLRenderer::prepare() {
132    mSnapshot = &mFirstSnapshot;
133    mSaveCount = 0;
134
135    glDisable(GL_SCISSOR_TEST);
136
137    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
138    glClear(GL_COLOR_BUFFER_BIT);
139
140    glEnable(GL_SCISSOR_TEST);
141    glScissor(0, 0, mWidth, mHeight);
142
143    mSnapshot->clipRect.set(0.0f, 0.0f, mWidth, mHeight);
144}
145
146///////////////////////////////////////////////////////////////////////////////
147// State management
148///////////////////////////////////////////////////////////////////////////////
149
150int OpenGLRenderer::getSaveCount() const {
151    return mSaveCount;
152}
153
154int OpenGLRenderer::save(int flags) {
155    return saveSnapshot();
156}
157
158void OpenGLRenderer::restore() {
159    if (mSaveCount == 0) return;
160
161    if (restoreSnapshot()) {
162        setScissorFromClip();
163    }
164}
165
166void OpenGLRenderer::restoreToCount(int saveCount) {
167    if (saveCount <= 0 || saveCount > mSaveCount) return;
168
169    bool restoreClip = false;
170
171    while (mSaveCount != saveCount - 1) {
172        restoreClip |= restoreSnapshot();
173    }
174
175    if (restoreClip) {
176        setScissorFromClip();
177    }
178}
179
180int OpenGLRenderer::saveSnapshot() {
181    mSnapshot = new Snapshot(mSnapshot);
182    return ++mSaveCount;
183}
184
185bool OpenGLRenderer::restoreSnapshot() {
186    bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
187    bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
188    bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
189
190    sp<Snapshot> current = mSnapshot;
191    sp<Snapshot> previous = mSnapshot->previous;
192
193    if (restoreOrtho) {
194        memcpy(mOrthoMatrix, current->orthoMatrix, sizeof(mOrthoMatrix));
195    }
196
197    if (restoreLayer) {
198        composeLayer(current, previous);
199    }
200
201    mSnapshot = previous;
202    mSaveCount--;
203
204    return restoreClip;
205}
206
207void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
208    // Unbind current FBO and restore previous one
209    // Most of the time, previous->fbo will be 0 to bind the default buffer
210    glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
211
212    // Restore the clip from the previous snapshot
213    const Rect& clip = previous->getMappedClip();
214    glScissor(clip.left, mHeight - clip.bottom, clip.getWidth(), clip.getHeight());
215
216    // Compute the correct texture coordinates for the FBO texture
217    // The texture is currently as big as the window but drawn with
218    // a quad of the appropriate size
219    const Rect& layer = current->layer;
220
221    drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
222            current->texture, current->alpha, current->mode, true, true);
223
224    // TODO Don't delete these things, but cache them
225    glDeleteFramebuffers(1, &current->fbo);
226    glDeleteTextures(1, &current->texture);
227}
228
229///////////////////////////////////////////////////////////////////////////////
230// Layers
231///////////////////////////////////////////////////////////////////////////////
232
233int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
234        const SkPaint* p, int flags) {
235    int count = saveSnapshot();
236
237    int alpha = 255;
238    SkXfermode::Mode mode;
239
240    if (p) {
241        alpha = p->getAlpha();
242        const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
243        if (!isMode) {
244            // Assume SRC_OVER
245            mode = SkXfermode::kSrcOver_Mode;
246        }
247    } else {
248        mode = SkXfermode::kSrcOver_Mode;
249    }
250
251    createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags);
252
253    return count;
254}
255
256int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom,
257        int alpha, int flags) {
258    int count = saveSnapshot();
259    createLayer(mSnapshot, left, top, right, bottom, alpha, SkXfermode::kSrcOver_Mode, flags);
260    return count;
261}
262
263bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
264        float right, float bottom, int alpha, SkXfermode::Mode mode,int flags) {
265    // Generate the FBO and attach the texture
266    glGenFramebuffers(1, &snapshot->fbo);
267    glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
268
269    // Generate the texture in which the FBO will draw
270    glGenTextures(1, &snapshot->texture);
271    glBindTexture(GL_TEXTURE_2D, snapshot->texture);
272
273    // The FBO will not be scaled, so we can use lower quality filtering
274    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
275    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
276
277    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
278    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
279
280    // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
281    // TODO Use an FBO cache
282
283    const GLsizei width = right - left;
284    const GLsizei height = bottom - top;
285
286    const GLint format = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) ? GL_RGBA : GL_RGB;
287    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
288    glBindTexture(GL_TEXTURE_2D, 0);
289
290    // Bind texture to FBO
291    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
292            snapshot->texture, 0);
293
294    GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
295    if (status != GL_FRAMEBUFFER_COMPLETE) {
296        LOGD("Framebuffer incomplete (GL error code 0x%x)", status);
297
298        glDeleteFramebuffers(1, &snapshot->fbo);
299        glDeleteTextures(1, &snapshot->texture);
300
301        return false;
302    }
303
304    // Clear the FBO
305    glDisable(GL_SCISSOR_TEST);
306    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
307    glClear(GL_COLOR_BUFFER_BIT);
308    glEnable(GL_SCISSOR_TEST);
309
310    snapshot->flags |= Snapshot::kFlagIsLayer;
311    snapshot->mode = mode;
312    snapshot->alpha = alpha / 255.0f;
313    snapshot->layer.set(left, top, right, bottom);
314
315    // Creates a new snapshot to draw into the FBO
316    saveSnapshot();
317    // TODO: This doesn't preserve other transformations (check Skia first)
318    mSnapshot->transform.loadTranslate(-left, -top, 0.0f);
319    mSnapshot->clipRect.set(left, top, right, bottom);
320    mSnapshot->height = bottom - top;
321    setScissorFromClip();
322
323    mSnapshot->flags = Snapshot::kFlagDirtyTransform | Snapshot::kFlagDirtyOrtho |
324            Snapshot::kFlagClipSet;
325    memcpy(mSnapshot->orthoMatrix, mOrthoMatrix, sizeof(mOrthoMatrix));
326
327    // Change the ortho projection
328    mat4 ortho;
329    ortho.loadOrtho(0.0f, right - left, bottom - top, 0.0f, 0.0f, 1.0f);
330    ortho.copyTo(mOrthoMatrix);
331
332    return true;
333}
334
335///////////////////////////////////////////////////////////////////////////////
336// Transforms
337///////////////////////////////////////////////////////////////////////////////
338
339void OpenGLRenderer::translate(float dx, float dy) {
340    mSnapshot->transform.translate(dx, dy, 0.0f);
341    mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
342}
343
344void OpenGLRenderer::rotate(float degrees) {
345    mSnapshot->transform.rotate(degrees, 0.0f, 0.0f, 1.0f);
346    mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
347}
348
349void OpenGLRenderer::scale(float sx, float sy) {
350    mSnapshot->transform.scale(sx, sy, 1.0f);
351    mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
352}
353
354void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
355    mSnapshot->transform.load(*matrix);
356    mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
357}
358
359void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
360    mSnapshot->transform.copyTo(*matrix);
361}
362
363void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
364    mat4 m(*matrix);
365    mSnapshot->transform.multiply(m);
366    mSnapshot->flags |= Snapshot::kFlagDirtyTransform;
367}
368
369///////////////////////////////////////////////////////////////////////////////
370// Clipping
371///////////////////////////////////////////////////////////////////////////////
372
373void OpenGLRenderer::setScissorFromClip() {
374    const Rect& clip = mSnapshot->getMappedClip();
375    glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight());
376}
377
378const Rect& OpenGLRenderer::getClipBounds() {
379    return mSnapshot->clipRect;
380}
381
382bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
383    /*
384     * The documentation of quickReject() indicates that the specified rect
385     * is transformed before being compared to the clip rect. However, the
386     * clip rect is not stored transformed in the snapshot and can thus be
387     * compared directly
388     *
389     * The following code can be used instead to performed a mapped comparison:
390     *
391     *     mSnapshot->transform.mapRect(r);
392     *     const Rect& clip = mSnapshot->getMappedClip();
393     *     return !clip.intersects(r);
394     */
395    Rect r(left, top, right, bottom);
396    return !mSnapshot->clipRect.intersects(r);
397}
398
399bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom) {
400    bool clipped = mSnapshot->clipRect.intersect(left, top, right, bottom);
401    if (clipped) {
402        mSnapshot->flags |= Snapshot::kFlagClipSet;
403        setScissorFromClip();
404    }
405    return clipped;
406}
407
408///////////////////////////////////////////////////////////////////////////////
409// Drawing
410///////////////////////////////////////////////////////////////////////////////
411
412void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
413    const Texture* texture = mTextureCache.get(bitmap);
414    LOGD("Texture cache size %d", mTextureCache.getSize());
415    LOGD("          max size %d", mTextureCache.getMaxSize());
416
417    int alpha;
418    SkXfermode::Mode mode;
419    getAlphaAndMode(paint, &alpha, &mode);
420
421    drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
422            alpha / 255.0f, mode, texture->blend, true);
423}
424
425void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) {
426    Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
427    const mat4 transform(*matrix);
428    transform.mapRect(r);
429
430    const Texture* texture = mTextureCache.get(bitmap);
431
432    int alpha;
433    SkXfermode::Mode mode;
434    getAlphaAndMode(paint, &alpha, &mode);
435
436    drawTextureRect(r.left, r.top, r.right, r.bottom, texture->id,
437            alpha / 255.0f, mode, texture->blend, true);
438}
439
440void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
441         float srcLeft, float srcTop, float srcRight, float srcBottom,
442         float dstLeft, float dstTop, float dstRight, float dstBottom,
443         const SkPaint* paint) {
444    const Texture* texture = mTextureCache.get(bitmap);
445
446    int alpha;
447    SkXfermode::Mode mode;
448    getAlphaAndMode(paint, &alpha, &mode);
449
450    const float width = texture->width;
451    const float height = texture->height;
452
453    const float u1 = srcLeft / width;
454    const float v1 = srcTop / height;
455    const float u2 = srcRight / width;
456    const float v2 = srcBottom / height;
457
458    resetDrawTextureTexCoords(u1, v1, u2, v2);
459
460    drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
461            alpha / 255.0f, mode, texture->blend, true);
462
463    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
464}
465
466void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
467    const Rect& clip = mSnapshot->clipRect;
468    drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);
469}
470
471void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) {
472    SkXfermode::Mode mode;
473
474    const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode);
475    if (!isMode) {
476        // Assume SRC_OVER
477        mode = SkXfermode::kSrcOver_Mode;
478    }
479
480    // Skia draws using the color's alpha channel if < 255
481    // Otherwise, it uses the paint's alpha
482    int color = p->getColor();
483    if (((color >> 24) & 0xFF) == 255) {
484        color |= p->getAlpha() << 24;
485    }
486
487    drawColorRect(left, top, right, bottom, color, mode);
488}
489
490void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
491        int color, SkXfermode::Mode mode) {
492    const int alpha = (color >> 24) & 0xFF;
493    const bool blend = alpha < 255 || mode != SkXfermode::kSrcOver_Mode;
494
495    const GLfloat a = alpha                  / 255.0f;
496    const GLfloat r = ((color >> 16) & 0xFF) / 255.0f;
497    const GLfloat g = ((color >>  8) & 0xFF) / 255.0f;
498    const GLfloat b = ((color      ) & 0xFF) / 255.0f;
499
500    if (blend) {
501        glEnable(GL_BLEND);
502        glBlendFunc(gBlends[mode].src, gBlends[mode].dst);
503    }
504
505    mModelView.loadTranslate(left, top, 0.0f);
506    mModelView.scale(right - left, bottom - top, 1.0f);
507
508    mDrawColorShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
509
510    const GLvoid* p = &gDrawColorVertices[0].position[0];
511
512    glEnableVertexAttribArray(mDrawColorShader->position);
513    glVertexAttribPointer(mDrawColorShader->position, 2, GL_FLOAT, GL_FALSE,
514            gDrawColorVertexStride, p);
515    glVertexAttrib4f(mDrawColorShader->color, r, g, b, a);
516
517    glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawColorVertexCount);
518
519    glDisableVertexAttribArray(mDrawColorShader->position);
520
521    if (blend) {
522        glDisable(GL_BLEND);
523    }
524}
525
526void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
527        GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied) {
528    mModelView.loadTranslate(left, top, 0.0f);
529    mModelView.scale(right - left, bottom - top, 1.0f);
530
531    mDrawTextureShader->use(&mOrthoMatrix[0], &mModelView.data[0], &mSnapshot->transform.data[0]);
532
533    if (blend || alpha < 1.0f || mode != SkXfermode::kSrcOver_Mode) {
534        GLenum sourceMode = gBlends[mode].src;
535        if (!isPremultiplied && sourceMode == GL_ONE) {
536            sourceMode = GL_SRC_ALPHA;
537        }
538
539        glEnable(GL_BLEND);
540        glBlendFunc(sourceMode, gBlends[mode].dst);
541    }
542
543    glBindTexture(GL_TEXTURE_2D, texture);
544
545    // TODO handle tiling and filtering here
546
547    glActiveTexture(GL_TEXTURE0);
548    glUniform1i(mDrawTextureShader->sampler, 0);
549
550    const GLvoid* p = &mDrawTextureVertices[0].position[0];
551    const GLvoid* t = &mDrawTextureVertices[0].texture[0];
552
553    glEnableVertexAttribArray(mDrawTextureShader->position);
554    glVertexAttribPointer(mDrawTextureShader->position, 2, GL_FLOAT, GL_FALSE,
555            gDrawTextureVertexStride, p);
556
557    glEnableVertexAttribArray(mDrawTextureShader->texCoords);
558    glVertexAttribPointer(mDrawTextureShader->texCoords, 2, GL_FLOAT, GL_FALSE,
559            gDrawTextureVertexStride, t);
560
561    glVertexAttrib4f(mDrawTextureShader->color, 1.0f, 1.0f, 1.0f, alpha);
562
563    glDrawArrays(GL_TRIANGLE_STRIP, 0, gDrawTextureVertexCount);
564
565    glDisableVertexAttribArray(mDrawTextureShader->position);
566    glDisableVertexAttribArray(mDrawTextureShader->texCoords);
567
568    glBindTexture(GL_TEXTURE_2D, 0);
569    glDisable(GL_BLEND);
570}
571
572void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
573    mDrawTextureVertices[0].texture[0] = u1;
574    mDrawTextureVertices[0].texture[1] = v1;
575    mDrawTextureVertices[1].texture[0] = u2;
576    mDrawTextureVertices[1].texture[1] = v1;
577    mDrawTextureVertices[2].texture[0] = u1;
578    mDrawTextureVertices[2].texture[1] = v2;
579    mDrawTextureVertices[3].texture[0] = u2;
580    mDrawTextureVertices[3].texture[1] = v2;
581}
582
583void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
584    if (paint) {
585        const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
586        if (!isMode) {
587            // Assume SRC_OVER
588            *mode = SkXfermode::kSrcOver_Mode;
589        }
590
591        // Skia draws using the color's alpha channel if < 255
592        // Otherwise, it uses the paint's alpha
593        int color = paint->getColor();
594        *alpha = (color >> 24) & 0xFF;
595        if (*alpha == 255) {
596            *alpha = paint->getAlpha();
597        }
598    } else {
599        *mode = SkXfermode::kSrcOver_Mode;
600        *alpha = 255;
601    }
602}
603
604}; // namespace uirenderer
605}; // namespace android
606