106f96e2652e4855b6520ad9dd70583677605b79aRomain Guy/*
206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * Copyright (C) 2010 The Android Open Source Project
306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy *
406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * you may not use this file except in compliance with the License.
606f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * You may obtain a copy of the License at
706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy *
806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy *
1006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * Unless required by applicable law or agreed to in writing, software
1106f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
1206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * See the License for the specific language governing permissions and
1406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy * limitations under the License.
1506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy */
1606f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
17922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik#include "SkiaShader.h"
1806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
19a1d3c91afbd52c7e8b01f4a9060c5459f02ae7a5Romain Guy#include "Caches.h"
202dc236b2bae13b9a0ed9b3f7320502aecd7983b3Tom Hudson#include "Extensions.h"
21d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III#include "Matrix.h"
2206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy#include "Texture.h"
23ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv#include "hwui/Bitmap.h"
2406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
25922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik#include <SkMatrix.h>
26922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik#include <utils/Log.h>
27922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
2806f96e2652e4855b6520ad9dd70583677605b79aRomain Guynamespace android {
2906f96e2652e4855b6520ad9dd70583677605b79aRomain Guynamespace uirenderer {
3006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
3106f96e2652e4855b6520ad9dd70583677605b79aRomain Guy///////////////////////////////////////////////////////////////////////////////
3206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy// Support
3306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy///////////////////////////////////////////////////////////////////////////////
3406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
35216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craikstatic constexpr GLenum gTileModes[] = {
3606f96e2652e4855b6520ad9dd70583677605b79aRomain Guy        GL_CLAMP_TO_EDGE,   // == SkShader::kClamp_TileMode
3706f96e2652e4855b6520ad9dd70583677605b79aRomain Guy        GL_REPEAT,          // == SkShader::kRepeat_Mode
3806f96e2652e4855b6520ad9dd70583677605b79aRomain Guy        GL_MIRRORED_REPEAT  // == SkShader::kMirror_TileMode
3906f96e2652e4855b6520ad9dd70583677605b79aRomain Guy};
4006f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
41216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craikstatic_assert(gTileModes[SkShader::kClamp_TileMode] == GL_CLAMP_TO_EDGE,
42216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craik        "SkShader TileModes have changed");
43216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craikstatic_assert(gTileModes[SkShader::kRepeat_TileMode] == GL_REPEAT,
44216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craik        "SkShader TileModes have changed");
45216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craikstatic_assert(gTileModes[SkShader::kMirror_TileMode] == GL_MIRRORED_REPEAT,
46216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craik        "SkShader TileModes have changed");
47216048ffb0a4c41836c6ddb63f9b2e7a64593837Chris Craik
4842e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy/**
4942e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy * This function does not work for n == 0.
5042e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy */
5142e1e0d482d774cf18a55773e434f02edb9e4462Romain Guystatic inline bool isPowerOfTwo(unsigned int n) {
5242e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy    return !(n & (n - 1));
5342e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy}
5442e1e0d482d774cf18a55773e434f02edb9e4462Romain Guy
55922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikstatic inline void bindUniformColor(int slot, FloatColor color) {
56922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    glUniform4fv(slot, 1, reinterpret_cast<const float*>(&color));
57922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
58922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
59d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins IIIstatic inline void bindTexture(Caches* caches, Texture* texture, GLenum wrapS, GLenum wrapT) {
60554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv    caches->textureState().bindTexture(texture->target(), texture->id());
61d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    texture->setWrapST(wrapS, wrapT);
6224c00216687ac87fe531dc4d4168ac0c0ca04ea6Romain Guy}
6324c00216687ac87fe531dc4d4168ac0c0ca04ea6Romain Guy
64d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III/**
65d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III * Compute the matrix to transform to screen space.
66d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III * @param screenSpace Output param for the computed matrix.
67d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III * @param unitMatrix The unit matrix for gradient shaders, as returned by SkShader::asAGradient,
68d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III *      or identity.
69d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III * @param localMatrix Local matrix, as returned by SkShader::getLocalMatrix().
70d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III * @param modelViewMatrix Model view matrix, as supplied by the OpenGLRenderer.
71d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III */
72d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins IIIstatic void computeScreenSpaceMatrix(mat4& screenSpace, const SkMatrix& unitMatrix,
73d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III        const SkMatrix& localMatrix, const mat4& modelViewMatrix) {
74d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    mat4 shaderMatrix;
75d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    // uses implicit construction
76d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    shaderMatrix.loadInverse(localMatrix);
77d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    // again, uses implicit construction
78d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    screenSpace.loadMultiply(unitMatrix, shaderMatrix);
79d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    screenSpace.multiply(modelViewMatrix);
80d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III}
81d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III
8206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy///////////////////////////////////////////////////////////////////////////////
83a0ed6f03f6f06eb41cbcc15c0a99b4a78fd91befRomain Guy// Gradient shader matrix helpers
8406f96e2652e4855b6520ad9dd70583677605b79aRomain Guy///////////////////////////////////////////////////////////////////////////////
8506f96e2652e4855b6520ad9dd70583677605b79aRomain Guy
86828407356dd5c34a3e441604aaf895cbec7c7e66Chris Craikstatic void toLinearUnitMatrix(const SkPoint pts[2], SkMatrix* matrix) {
87e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    SkVector vec = pts[1] - pts[0];
88e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    const float mag = vec.length();
89e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    const float inv = mag ? 1.0f / mag : 0;
90e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy
91e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    vec.scale(inv);
92e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    matrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
93e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    matrix->postTranslate(-pts[0].fX, -pts[0].fY);
94e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy    matrix->postScale(inv, inv);
95e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy}
96e3095e0c1e2a4a4f34f741aa386eae56536ca5aaRomain Guy
9714830948d02f768c41b97b7a8d15e1b3cab78267Romain Guystatic void toCircularUnitMatrix(const float x, const float y, const float radius,
9814830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy        SkMatrix* matrix) {
9914830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy    const float inv = 1.0f / radius;
10014830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy    matrix->setTranslate(-x, -y);
10114830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy    matrix->postScale(inv, inv);
10214830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy}
10314830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy
10414830948d02f768c41b97b7a8d15e1b3cab78267Romain Guystatic void toSweepUnitMatrix(const float x, const float y, SkMatrix* matrix) {
10514830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy    matrix->setTranslate(-x, -y);
10614830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy}
10714830948d02f768c41b97b7a8d15e1b3cab78267Romain Guy
108d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III///////////////////////////////////////////////////////////////////////////////
109d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III// Common gradient code
110d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III///////////////////////////////////////////////////////////////////////////////
11124c00216687ac87fe531dc4d4168ac0c0ca04ea6Romain Guy
112d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins IIIstatic bool isSimpleGradient(const SkShader::GradientInfo& gradInfo) {
113d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III    return gradInfo.fColorCount == 2 && gradInfo.fTileMode == SkShader::kClamp_TileMode;
114d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III}
115d1ad5e62fda248c6d185cde3cb6d9f01a223066cLeon Scroggins III
116922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik///////////////////////////////////////////////////////////////////////////////
117922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik// Store / apply
118922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik///////////////////////////////////////////////////////////////////////////////
119922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
120922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikbool tryStoreGradient(Caches& caches, const SkShader& shader, const Matrix4 modelViewMatrix,
121922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        GLuint* textureUnit, ProgramDescription* description,
122922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        SkiaShaderData::GradientShaderData* outData) {
123922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    SkShader::GradientInfo gradInfo;
124922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    gradInfo.fColorCount = 0;
125922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    gradInfo.fColors = nullptr;
126922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    gradInfo.fColorOffsets = nullptr;
127922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
128922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    SkMatrix unitMatrix;
129922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    switch (shader.asAGradient(&gradInfo)) {
130922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kLinear_GradientType:
131922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            description->gradientType = ProgramDescription::kGradientLinear;
132922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
133828407356dd5c34a3e441604aaf895cbec7c7e66Chris Craik            toLinearUnitMatrix(gradInfo.fPoint, &unitMatrix);
134922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            break;
135922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kRadial_GradientType:
136922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            description->gradientType = ProgramDescription::kGradientCircular;
137922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
138922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            toCircularUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY,
139922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                    gradInfo.fRadius[0], &unitMatrix);
140922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            break;
141922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kSweep_GradientType:
142922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            description->gradientType = ProgramDescription::kGradientSweep;
143922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
144922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            toSweepUnitMatrix(gradInfo.fPoint[0].fX, gradInfo.fPoint[0].fY, &unitMatrix);
145922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            break;
146922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        default:
147922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            // Do nothing. This shader is unsupported.
148922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            return false;
149922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
150922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    description->hasGradient = true;
151922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    description->isSimpleGradient = isSimpleGradient(gradInfo);
152922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
153922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    computeScreenSpaceMatrix(outData->screenSpace, unitMatrix,
154922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            shader.getLocalMatrix(), modelViewMatrix);
155922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
156922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    // re-query shader to get full color / offset data
157922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    std::unique_ptr<SkColor[]> colorStorage(new SkColor[gradInfo.fColorCount]);
158922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    std::unique_ptr<SkScalar[]> colorOffsets(new SkScalar[gradInfo.fColorCount]);
159922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    gradInfo.fColors = &colorStorage[0];
160922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    gradInfo.fColorOffsets = &colorOffsets[0];
161922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    shader.asAGradient(&gradInfo);
162922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
163a0ed6f03f6f06eb41cbcc15c0a99b4a78fd91befRomain Guy    if (CC_UNLIKELY(!description->isSimpleGradient)) {
164922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->gradientSampler = (*textureUnit)++;
165922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
166922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik#ifndef SK_SCALAR_IS_FLOAT
167922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    #error Need to convert gradInfo.fColorOffsets to float!
168922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik#endif
169922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->gradientTexture = caches.gradientCache.get(
170922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                gradInfo.fColors, gradInfo.fColorOffsets, gradInfo.fColorCount);
171922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->wrapST = gTileModes[gradInfo.fTileMode];
172922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    } else {
173922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->gradientSampler = 0;
174922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->gradientTexture = nullptr;
175922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
176669b15a93548b82135c73196665bcb7f03d87795Derek Sollenberger        outData->startColor.set(gradInfo.fColors[0]);
177669b15a93548b82135c73196665bcb7f03d87795Derek Sollenberger        outData->endColor.set(gradInfo.fColors[1]);
178922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
179922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
180922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    return true;
181922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
182922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
183253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guyvoid applyGradient(Caches& caches, const SkiaShaderData::GradientShaderData& data,
184253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        const GLsizei width, const GLsizei height) {
185253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy
186922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (CC_UNLIKELY(data.gradientTexture)) {
187922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        caches.textureState().activateTexture(data.gradientSampler);
188922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        bindTexture(&caches, data.gradientTexture, data.wrapST, data.wrapST);
189922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        glUniform1i(caches.program().getUniform("gradientSampler"), data.gradientSampler);
190922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    } else {
191922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        bindUniformColor(caches.program().getUniform("startColor"), data.startColor);
192922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        bindUniformColor(caches.program().getUniform("endColor"), data.endColor);
193922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
194922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
195253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy    glUniform2f(caches.program().getUniform("screenSize"), 1.0f / width, 1.0f / height);
196922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    glUniformMatrix4fv(caches.program().getUniform("screenSpace"), 1,
197922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            GL_FALSE, &data.screenSpace.data[0]);
198922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
199922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
200922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikbool tryStoreBitmap(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
201922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        GLuint* textureUnit, ProgramDescription* description,
202922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        SkiaShaderData::BitmapShaderData* outData) {
203922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    SkBitmap bitmap;
204922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    SkShader::TileMode xy[2];
205f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (!shader.isABitmap(&bitmap, nullptr, xy)) {
206922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        return false;
207922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
208922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
209ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    // TODO: create  hwui-owned BitmapShader.
210ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    Bitmap* hwuiBitmap = static_cast<Bitmap*>(bitmap.pixelRef());
211ec4a4b13eae2241d1613890c1c1c096bed891845sergeyv    outData->bitmapTexture = caches.textureCache.get(hwuiBitmap);
212922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (!outData->bitmapTexture) return false;
213922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
214922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    outData->bitmapSampler = (*textureUnit)++;
215922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
21638e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float width = outData->bitmapTexture->width();
21738e0c32852e3b9d8ca4a9d3791577f52536419cbJohn Reck    const float height = outData->bitmapTexture->height();
218922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
219caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    Texture* texture = outData->bitmapTexture;
220caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy
221922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    description->hasBitmap = true;
222caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    description->hasLinearTexture = texture->isLinear();
223caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    description->hasColorSpaceConversion = texture->hasColorSpaceConversion();
224caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    description->transferFunction = texture->getTransferFunctionType();
225caaaa66e57293e4a6f312649bf472eab84d5c7feRomain Guy    description->hasTranslucentConversion = texture->blend;
2269c97e48fbe389180b4b64845f093c53c92c374f3sergeyv    description->isShaderBitmapExternal = hwuiBitmap->isHardware();
227554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv    // gralloc doesn't support non-clamp modes
228554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv    if (hwuiBitmap->isHardware() || (!caches.extensions().hasNPot()
229922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            && (!isPowerOfTwo(width) || !isPowerOfTwo(height))
230554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv            && (xy[0] != SkShader::kClamp_TileMode || xy[1] != SkShader::kClamp_TileMode))) {
231554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv        // need non-clamp mode, but it's not supported for this draw,
232554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv        // so enable custom shader logic to mimic
233554ffeb8b7c836da43a637c59eedfc617895b19dsergeyv        description->useShaderBasedWrap = true;
234922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        description->bitmapWrapS = gTileModes[xy[0]];
235922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        description->bitmapWrapT = gTileModes[xy[1]];
236922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
237922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->wrapS = GL_CLAMP_TO_EDGE;
238922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->wrapT = GL_CLAMP_TO_EDGE;
239922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    } else {
240922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->wrapS = gTileModes[xy[0]];
241922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->wrapT = gTileModes[xy[1]];
242922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
243922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
244922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    computeScreenSpaceMatrix(outData->textureTransform, SkMatrix::I(), shader.getLocalMatrix(),
245922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            modelViewMatrix);
246922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    outData->textureDimension[0] = 1.0f / width;
247922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    outData->textureDimension[1] = 1.0f / height;
248922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
249922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    return true;
250922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
251922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
252922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikvoid applyBitmap(Caches& caches, const SkiaShaderData::BitmapShaderData& data) {
253922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    caches.textureState().activateTexture(data.bitmapSampler);
254922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    bindTexture(&caches, data.bitmapTexture, data.wrapS, data.wrapT);
255922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    data.bitmapTexture->setFilter(GL_LINEAR);
256922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
257922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    glUniform1i(caches.program().getUniform("bitmapSampler"), data.bitmapSampler);
258922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    glUniformMatrix4fv(caches.program().getUniform("textureTransform"), 1, GL_FALSE,
259922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            &data.textureTransform.data[0]);
260922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    glUniform2fv(caches.program().getUniform("textureDimension"), 1, &data.textureDimension[0]);
261922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
262922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
263922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris CraikSkiaShaderType getComposeSubType(const SkShader& shader) {
264922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    // First check for a gradient shader.
265922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    switch (shader.asAGradient(nullptr)) {
266922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kNone_GradientType:
267922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            // Not a gradient shader. Fall through to check for other types.
268922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            break;
269922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kLinear_GradientType:
270922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kRadial_GradientType:
271922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        case SkShader::kSweep_GradientType:
272922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            return kGradient_SkiaShaderType;
273922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        default:
274922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            // This is a Skia gradient that has no SkiaShader equivalent. Return None to skip.
275922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            return kNone_SkiaShaderType;
276922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
277922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
278922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    // The shader is not a gradient. Check for a bitmap shader.
279f35b989d26bb98900f6c5fa2e586326b30b6e161Leon Scroggins III    if (shader.isABitmap()) {
280922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        return kBitmap_SkiaShaderType;
281922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
282922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    return kNone_SkiaShaderType;
283922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
284922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
285922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikvoid storeCompose(Caches& caches, const SkShader& bitmapShader, const SkShader& gradientShader,
286922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        const Matrix4& modelViewMatrix, GLuint* textureUnit,
287922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        ProgramDescription* description, SkiaShaderData* outData) {
288922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    LOG_ALWAYS_FATAL_IF(!tryStoreBitmap(caches, bitmapShader, modelViewMatrix,
289922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                textureUnit, description, &outData->bitmapData),
290922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            "failed storing bitmap shader data");
291922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    LOG_ALWAYS_FATAL_IF(!tryStoreGradient(caches, gradientShader, modelViewMatrix,
292922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                textureUnit, description, &outData->gradientData),
293922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            "failing storing gradient shader data");
294922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
295922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
296922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craikbool tryStoreCompose(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
297922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        GLuint* textureUnit, ProgramDescription* description,
298922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        SkiaShaderData* outData) {
299922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
300922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    SkShader::ComposeRec rec;
301922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (!shader.asACompose(&rec)) return false;
302922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
303922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    const SkiaShaderType shaderAType = getComposeSubType(*rec.fShaderA);
304922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    const SkiaShaderType shaderBType = getComposeSubType(*rec.fShaderB);
305922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
306922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    // check that type enum values are the 2 flags that compose the kCompose value
307922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if ((shaderAType & shaderBType) != 0) return false;
308922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if ((shaderAType | shaderBType) != kCompose_SkiaShaderType) return false;
309922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
310922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    mat4 transform;
311922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    computeScreenSpaceMatrix(transform, SkMatrix::I(), shader.getLocalMatrix(), modelViewMatrix);
312922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (shaderAType == kBitmap_SkiaShaderType) {
313922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        description->isBitmapFirst = true;
314922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        storeCompose(caches, *rec.fShaderA, *rec.fShaderB,
315922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                transform, textureUnit, description, outData);
316922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    } else {
317922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        description->isBitmapFirst = false;
318922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        storeCompose(caches, *rec.fShaderB, *rec.fShaderA,
319922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik                transform, textureUnit, description, outData);
320922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
321c2f31df8b3b9a237e9abffc59c61804ad8495073Mike Reed    description->shadersMode = rec.fBlendMode;
322922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    return true;
323922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
324922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
32553e51e4aa933f9603587e1780f446c18816bf9beChris Craikvoid SkiaShader::store(Caches& caches, const SkShader& shader, const Matrix4& modelViewMatrix,
326922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        GLuint* textureUnit, ProgramDescription* description,
327922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        SkiaShaderData* outData) {
32853e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (tryStoreGradient(caches, shader, modelViewMatrix,
329922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            textureUnit, description, &outData->gradientData)) {
330922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->skiaShaderType = kGradient_SkiaShaderType;
331922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        return;
332922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
333922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
33453e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (tryStoreBitmap(caches, shader, modelViewMatrix,
335922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            textureUnit, description, &outData->bitmapData)) {
336922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->skiaShaderType = kBitmap_SkiaShaderType;
337922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        return;
338922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
339922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
34053e51e4aa933f9603587e1780f446c18816bf9beChris Craik    if (tryStoreCompose(caches, shader, modelViewMatrix,
341922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik            textureUnit, description, outData)) {
342922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        outData->skiaShaderType = kCompose_SkiaShaderType;
343922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        return;
344922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
345922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
346e310f83d591dc3fb7bd5c684239481a586f00662Chris Craik    // Unknown/unsupported type, so explicitly ignore shader
347e310f83d591dc3fb7bd5c684239481a586f00662Chris Craik    outData->skiaShaderType = kNone_SkiaShaderType;
348922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
349922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
350253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guyvoid SkiaShader::apply(Caches& caches, const SkiaShaderData& data,
351253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        const GLsizei width, const GLsizei height) {
352922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (!data.skiaShaderType) return;
353922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
354922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (data.skiaShaderType & kGradient_SkiaShaderType) {
355253f2c213f6ecda63b6872aee77bd30d5ec07c82Romain Guy        applyGradient(caches, data.gradientData, width, height);
356922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
357922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    if (data.skiaShaderType & kBitmap_SkiaShaderType) {
358922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik        applyBitmap(caches, data.bitmapData);
359922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik    }
360922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik}
361922d3a7f6f8c1c05a996ee3e91e8cbadfff560c9Chris Craik
36206f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}; // namespace uirenderer
36306f96e2652e4855b6520ad9dd70583677605b79aRomain Guy}; // namespace android
364