Skip to content

Commit d679b57

Browse files
author
Romain Guy
committed
Pre-multiply color components for 2-stop gradients
Bug #7033344 Change-Id: Ia168501f1dc56ba7a1bb0c55078320432309a66a
1 parent 2c577f1 commit d679b57

File tree

4 files changed

+24
-50
lines changed

4 files changed

+24
-50
lines changed

core/java/android/view/View.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17610,23 +17610,27 @@ public ScrollabilityCache(ViewConfiguration configuration, View host) {
1761017610
// use use a height of 1, and then wack the matrix each time we
1761117611
// actually use it.
1761217612
shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17613-
1761417613
paint.setShader(shader);
1761517614
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17615+
1761617616
this.host = host;
1761717617
}
1761817618

1761917619
public void setFadeColor(int color) {
17620-
if (color != 0 && color != mLastColor) {
17620+
if (color != mLastColor) {
1762117621
mLastColor = color;
17622-
color |= 0xFF000000;
17623-
17624-
shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17625-
color & 0x00FFFFFF, Shader.TileMode.CLAMP);
1762617622

17627-
paint.setShader(shader);
17628-
// Restore the default transfer mode (src_over)
17629-
paint.setXfermode(null);
17623+
if (color != 0) {
17624+
shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
17625+
color & 0x00FFFFFF, Shader.TileMode.CLAMP);
17626+
paint.setShader(shader);
17627+
// Restore the default transfer mode (src_over)
17628+
paint.setXfermode(null);
17629+
} else {
17630+
shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
17631+
paint.setShader(shader);
17632+
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
17633+
}
1763017634
}
1763117635
}
1763217636

libs/hwui/GradientCache.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ void GradientCache::generateTexture(uint32_t* colors, float* positions,
217217
float amount = (pos - start) / distance;
218218
float oppAmount = 1.0f - amount;
219219

220-
*p++ = uint8_t(startR * oppAmount + endR * amount);
221-
*p++ = uint8_t(startG * oppAmount + endG * amount);
222-
*p++ = uint8_t(startB * oppAmount + endB * amount);
223-
*p++ = uint8_t(startA * oppAmount + endA * amount);
220+
const float alpha = startA * oppAmount + endA * amount;
221+
const float a = alpha / 255.0f;
222+
*p++ = uint8_t(a * (startR * oppAmount + endR * amount));
223+
*p++ = uint8_t(a * (startG * oppAmount + endG * amount));
224+
*p++ = uint8_t(a * (startB * oppAmount + endB * amount));
225+
*p++ = uint8_t(alpha);
224226
}
225227

226228
for (int i = 1; i < GRADIENT_TEXTURE_HEIGHT; i++) {

libs/hwui/SkiaShader.cpp

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ static inline bool isPowerOfTwo(unsigned int n) {
4646
}
4747

4848
static inline void bindUniformColor(int slot, uint32_t color) {
49+
const float a = ((color >> 24) & 0xff) / 255.0f;
4950
glUniform4f(slot,
50-
((color >> 16) & 0xff) / 255.0f,
51-
((color >> 8) & 0xff) / 255.0f,
52-
((color ) & 0xff) / 255.0f,
53-
((color >> 24) & 0xff) / 255.0f);
51+
a * ((color >> 16) & 0xff) / 255.0f,
52+
a * ((color >> 8) & 0xff) / 255.0f,
53+
a * ((color ) & 0xff) / 255.0f,
54+
a);
5455
}
5556

5657
///////////////////////////////////////////////////////////////////////////////
@@ -154,10 +155,6 @@ void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
154155

155156
// Uniforms
156157
bindTexture(texture, mWrapS, mWrapT);
157-
// Assume linear here; we should really check the transform in
158-
// ::updateTransforms() but we don't have the texture object
159-
// available at that point. The optimization is not worth the
160-
// effort for now.
161158
texture->setFilter(GL_LINEAR);
162159

163160
glUniform1i(program->getUniform("bitmapSampler"), textureSlot);
@@ -166,14 +163,6 @@ void SkiaBitmapShader::setupProgram(Program* program, const mat4& modelView,
166163
glUniform2f(program->getUniform("textureDimension"), 1.0f / width, 1.0f / height);
167164
}
168165

169-
void SkiaBitmapShader::updateTransforms(Program* program, const mat4& modelView,
170-
const Snapshot& snapshot) {
171-
mat4 textureTransform;
172-
computeScreenSpaceMatrix(textureTransform, modelView);
173-
glUniformMatrix4fv(program->getUniform("textureTransform"), 1,
174-
GL_FALSE, &textureTransform.data[0]);
175-
}
176-
177166
///////////////////////////////////////////////////////////////////////////////
178167
// Linear gradient shader
179168
///////////////////////////////////////////////////////////////////////////////
@@ -257,13 +246,6 @@ void SkiaLinearGradientShader::setupProgram(Program* program, const mat4& modelV
257246
glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
258247
}
259248

260-
void SkiaLinearGradientShader::updateTransforms(Program* program, const mat4& modelView,
261-
const Snapshot& snapshot) {
262-
mat4 screenSpace;
263-
computeScreenSpaceMatrix(screenSpace, modelView);
264-
glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
265-
}
266-
267249
///////////////////////////////////////////////////////////////////////////////
268250
// Circular gradient shader
269251
///////////////////////////////////////////////////////////////////////////////
@@ -384,13 +366,6 @@ void SkiaSweepGradientShader::setupProgram(Program* program, const mat4& modelVi
384366
glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
385367
}
386368

387-
void SkiaSweepGradientShader::updateTransforms(Program* program, const mat4& modelView,
388-
const Snapshot& snapshot) {
389-
mat4 screenSpace;
390-
computeScreenSpaceMatrix(screenSpace, modelView);
391-
glUniformMatrix4fv(program->getUniform("screenSpace"), 1, GL_FALSE, &screenSpace.data[0]);
392-
}
393-
394369
///////////////////////////////////////////////////////////////////////////////
395370
// Compose shader
396371
///////////////////////////////////////////////////////////////////////////////

libs/hwui/SkiaShader.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ struct SkiaShader {
8282
mGradientCache = gradientCache;
8383
}
8484

85-
virtual void updateTransforms(Program* program, const mat4& modelView,
86-
const Snapshot& snapshot) {
87-
}
88-
8985
uint32_t getGenerationId() {
9086
return mGenerationId;
9187
}
@@ -148,7 +144,6 @@ struct SkiaBitmapShader: public SkiaShader {
148144
void describe(ProgramDescription& description, const Extensions& extensions);
149145
void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
150146
GLuint* textureUnit);
151-
void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
152147

153148
private:
154149
SkiaBitmapShader() {
@@ -172,7 +167,6 @@ struct SkiaLinearGradientShader: public SkiaShader {
172167
void describe(ProgramDescription& description, const Extensions& extensions);
173168
void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
174169
GLuint* textureUnit);
175-
void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
176170

177171
private:
178172
SkiaLinearGradientShader() {
@@ -197,7 +191,6 @@ struct SkiaSweepGradientShader: public SkiaShader {
197191
virtual void describe(ProgramDescription& description, const Extensions& extensions);
198192
void setupProgram(Program* program, const mat4& modelView, const Snapshot& snapshot,
199193
GLuint* textureUnit);
200-
void updateTransforms(Program* program, const mat4& modelView, const Snapshot& snapshot);
201194

202195
protected:
203196
SkiaSweepGradientShader(Type type, float x, float y, uint32_t* colors, float* positions,

0 commit comments

Comments
 (0)