summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContext.cpp
blob: 367abda3edb60989e975525ce38ba2807f8da9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * Copyright 2006, The Android Open Source Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define LOG_TAG "PlatformGraphicsContext"
#define LOG_NDEBUG 1

#include "config.h"
#include "PlatformGraphicsContext.h"

#include "AndroidLog.h"
#include "SkBlurDrawLooper.h"
#include "SkBlurMaskFilter.h"
#include "SkColorPriv.h"
#include "SkDashPathEffect.h"
#include "SkPaint.h"
#include "SkShader.h"
#include "SkiaUtils.h"

namespace WebCore {

//**************************************
// Helper functions
//**************************************

static int RoundToInt(float x)
{
    return (int)roundf(x);
}

template <typename T> T* deepCopyPtr(const T* src)
{
    return src ? new T(*src) : 0;
}

// Set a bitmap shader that mimics dashing by width-on, width-off.
// Returns false if it could not succeed (e.g. there was an existing shader)
static bool setBitmapDash(SkPaint* paint, int width) {
    if (width <= 0 || paint->getShader())
        return false;

    SkColor c = paint->getColor();

    SkBitmap bm;
    bm.setConfig(SkBitmap::kARGB_8888_Config, 2, 1);
    bm.allocPixels();
    bm.lockPixels();

    // set the ON pixel
    *bm.getAddr32(0, 0) = SkPreMultiplyARGB(0xFF, SkColorGetR(c),
                                            SkColorGetG(c), SkColorGetB(c));
    // set the OFF pixel
    *bm.getAddr32(1, 0) = 0;
    bm.unlockPixels();

    SkMatrix matrix;
    matrix.setScale(SkIntToScalar(width), SK_Scalar1);

    SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kRepeat_TileMode,
                                               SkShader::kClamp_TileMode);
    s->setLocalMatrix(matrix);

    paint->setShader(s)->unref();
    paint->setFilterBitmap(true);
    paint->setAntiAlias(true);
    return true;
}

//**************************************
// State implementation
//**************************************

PlatformGraphicsContext::State::State()
    : pathEffect(0)
    , miterLimit(4)
    , alpha(1)
    , strokeThickness(0) // Same as default in GraphicsContextPrivate.h
    , lineCap(SkPaint::kDefault_Cap)
    , lineJoin(SkPaint::kDefault_Join)
    , mode(SkXfermode::kSrcOver_Mode)
    , dashRatio(3)
    , fillColor(SK_ColorBLACK)
    , fillShader(0)
    , strokeColor(SK_ColorBLACK)
    , strokeShader(0)
    , useAA(true)
    , strokeStyle(SolidStroke)
{
}

PlatformGraphicsContext::State::State(const State& other)
    : pathEffect(other.pathEffect)
    , miterLimit(other.miterLimit)
    , alpha(other.alpha)
    , strokeThickness(other.strokeThickness)
    , lineCap(other.lineCap)
    , lineJoin(other.lineJoin)
    , mode(other.mode)
    , dashRatio(other.dashRatio)
    , shadow(other.shadow)
    , fillColor(other.fillColor)
    , fillShader(other.fillShader)
    , strokeColor(other.strokeColor)
    , strokeShader(other.strokeShader)
    , useAA(other.useAA)
    , strokeStyle(other.strokeStyle)
{
    SkSafeRef(pathEffect);
    SkSafeRef(fillShader);
    SkSafeRef(strokeShader);
}

PlatformGraphicsContext::State::~State()
{
    SkSafeUnref(pathEffect);
    SkSafeUnref(fillShader);
    SkSafeUnref(strokeShader);
}

void PlatformGraphicsContext::State::setShadow(int radius, int dx, int dy, SkColor c)
{
    // Cut the radius in half, to visually match the effect seen in
    // safari browser
    shadow.blur = SkScalarHalf(SkIntToScalar(radius));
    shadow.dx = SkIntToScalar(dx);
    shadow.dy = SkIntToScalar(dy);
    shadow.color = c;
}

bool PlatformGraphicsContext::State::setupShadowPaint(SkPaint* paint, SkPoint* offset,
                      bool shadowsIgnoreTransforms)
{
    paint->setAntiAlias(true);
    paint->setDither(true);
    paint->setXfermodeMode(mode);
    paint->setColor(shadow.color);
    offset->set(shadow.dx, shadow.dy);

    // Currently, only GraphicsContexts associated with the
    // HTMLCanvasElement have shadows ignore transforms set.  This
    // allows us to distinguish between CSS and Canvas shadows which
    // have different rendering specifications.
    uint32_t flags = SkBlurMaskFilter::kNone_BlurFlag;
    if (shadowsIgnoreTransforms) {
        offset->fY = -offset->fY;
        flags |= SkBlurMaskFilter::kIgnoreTransform_BlurFlag;
    }

    if (shadow.blur > 0) {
        paint->setMaskFilter(SkBlurMaskFilter::Create(shadow.blur,
                             SkBlurMaskFilter::kNormal_BlurStyle, flags))->unref();
    }
    return SkColorGetA(shadow.color) && (shadow.blur || shadow.dx || shadow.dy);
}

SkColor PlatformGraphicsContext::State::applyAlpha(SkColor c) const
{
    int s = RoundToInt(alpha * 256);
    if (s >= 256)
        return c;
    if (s < 0)
        return 0;

    int a = SkAlphaMul(SkColorGetA(c), s);
    return (c & 0x00FFFFFF) | (a << 24);
}

// Returns a new State with all of this object's inherited properties copied.
PlatformGraphicsContext::State PlatformGraphicsContext::State::cloneInheritedProperties()
{
    return PlatformGraphicsContext::State(*this);
}

//**************************************
// PlatformGraphicsContext
//**************************************

PlatformGraphicsContext::PlatformGraphicsContext()
{
    m_stateStack.append(State());
    m_state = &m_stateStack.last();
}

PlatformGraphicsContext::~PlatformGraphicsContext()
{
}

//**************************************
// State management
//**************************************

void PlatformGraphicsContext::save()
{
    m_stateStack.append(m_state->cloneInheritedProperties());
    m_state = &m_stateStack.last();
}

void PlatformGraphicsContext::restore()
{
    m_stateStack.removeLast();
    m_state = &m_stateStack.last();
}

//**************************************
// State setters
//**************************************

void PlatformGraphicsContext::setAlpha(float alpha)
{
    m_state->alpha = alpha;
}

int PlatformGraphicsContext::getNormalizedAlpha() const
{
    int alpha = roundf(m_state->alpha * 256);
    if (alpha > 255)
        alpha = 255;
    else if (alpha < 0)
        alpha = 0;
    return alpha;
}

void PlatformGraphicsContext::setCompositeOperation(CompositeOperator op)
{
    m_state->mode = WebCoreCompositeToSkiaComposite(op);
}

bool PlatformGraphicsContext::setFillColor(const Color& c)
{
    bool dirty = false;
    if (m_state->fillColor != c.rgb()) {
        m_state->fillColor = c.rgb();
        dirty = true;
    }
    return setFillShader(0) || dirty;
}

bool PlatformGraphicsContext::setFillShader(SkShader* fillShader)
{
    bool dirty = false;
    if (fillShader && m_state->fillColor != Color::black) {
        m_state->fillColor = Color::black;
        dirty = true;
    }

    if (fillShader != m_state->fillShader) {
        SkSafeUnref(m_state->fillShader);
        m_state->fillShader = fillShader;
        SkSafeRef(m_state->fillShader);
        dirty = true;
    }
    return dirty;
}

void PlatformGraphicsContext::setLineCap(LineCap cap)
{
    switch (cap) {
    case ButtCap:
        m_state->lineCap = SkPaint::kButt_Cap;
        break;
    case RoundCap:
        m_state->lineCap = SkPaint::kRound_Cap;
        break;
    case SquareCap:
        m_state->lineCap = SkPaint::kSquare_Cap;
        break;
    default:
        ALOGD("PlatformGraphicsContextSkia::setLineCap: unknown LineCap %d\n", cap);
        break;
    }
}

void PlatformGraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
{
    size_t dashLength = dashes.size();
    if (!dashLength)
        return;

    size_t count = !(dashLength % 2) ? dashLength : dashLength * 2;
    SkScalar* intervals = new SkScalar[count];

    for (unsigned int i = 0; i < count; i++)
        intervals[i] = SkFloatToScalar(dashes[i % dashLength]);
    SkPathEffect **effectPtr = &m_state->pathEffect;
    SkSafeUnref(*effectPtr);
    *effectPtr = new SkDashPathEffect(intervals, count, SkFloatToScalar(dashOffset));

    delete[] intervals;
}

void PlatformGraphicsContext::setLineJoin(LineJoin join)
{
    switch (join) {
    case MiterJoin:
        m_state->lineJoin = SkPaint::kMiter_Join;
        break;
    case RoundJoin:
        m_state->lineJoin = SkPaint::kRound_Join;
        break;
    case BevelJoin:
        m_state->lineJoin = SkPaint::kBevel_Join;
        break;
    default:
        ALOGD("PlatformGraphicsContextSkia::setLineJoin: unknown LineJoin %d\n", join);
        break;
    }
}

void PlatformGraphicsContext::setMiterLimit(float limit)
{
    m_state->miterLimit = limit;
}

void PlatformGraphicsContext::setShadow(int radius, int dx, int dy, SkColor c)
{
    m_state->setShadow(radius, dx, dy, c);
}

void PlatformGraphicsContext::setShouldAntialias(bool useAA)
{
    m_state->useAA = useAA;
}

bool PlatformGraphicsContext::setStrokeColor(const Color& c)
{
    bool dirty = false;
    if (m_state->strokeColor != c.rgb()) {
        m_state->strokeColor = c.rgb();
        dirty = true;
    }
    return setStrokeShader(0) || dirty;
}

bool PlatformGraphicsContext::setStrokeShader(SkShader* strokeShader)
{
    bool dirty = false;
    if (strokeShader && m_state->strokeColor != Color::black) {
        m_state->strokeColor = Color::black;
        dirty = true;
    }

    if (strokeShader != m_state->strokeShader) {
        SkSafeUnref(m_state->strokeShader);
        m_state->strokeShader = strokeShader;
        SkSafeRef(m_state->strokeShader);
        dirty = true;
    }
    return dirty;
}

void PlatformGraphicsContext::setStrokeStyle(StrokeStyle style)
{
    m_state->strokeStyle = style;
}

void PlatformGraphicsContext::setStrokeThickness(float f)
{
    m_state->strokeThickness = f;
}

//**************************************
// Paint setup
//**************************************

void PlatformGraphicsContext::setupPaintCommon(SkPaint* paint) const
{
    paint->setAntiAlias(m_state->useAA);
    paint->setDither(true);
    paint->setXfermodeMode(m_state->mode);
    if (SkColorGetA(m_state->shadow.color) > 0) {

        // Currently, only GraphicsContexts associated with the
        // HTMLCanvasElement have shadows ignore transforms set.  This
        // allows us to distinguish between CSS and Canvas shadows which
        // have different rendering specifications.
        SkScalar dy = m_state->shadow.dy;
        uint32_t flags = SkBlurDrawLooper::kNone_BlurFlag;
        if (shadowsIgnoreTransforms()) {
            dy = -dy;
            flags |= SkBlurDrawLooper::kIgnoreTransform_BlurFlag;
            flags |= SkBlurDrawLooper::kOverrideColor_BlurFlag;
        }

        SkDrawLooper* looper = new SkBlurDrawLooper(m_state->shadow.blur,
                                                    m_state->shadow.dx,
                                                    dy,
                                                    m_state->shadow.color,
                                                    flags);
        paint->setLooper(looper)->unref();
    }
    paint->setFilterBitmap(true);
}

void PlatformGraphicsContext::setupPaintFill(SkPaint* paint) const
{
    this->setupPaintCommon(paint);
    paint->setColor(m_state->applyAlpha(m_state->fillColor));
    paint->setShader(m_state->fillShader);
}

bool PlatformGraphicsContext::setupPaintShadow(SkPaint* paint, SkPoint* offset) const
{
    return m_state->setupShadowPaint(paint, offset, shadowsIgnoreTransforms());
}

bool PlatformGraphicsContext::setupPaintStroke(SkPaint* paint, SkRect* rect,
                                               bool isHLine)
{
    this->setupPaintCommon(paint);
    paint->setColor(m_state->applyAlpha(m_state->strokeColor));
    paint->setShader(m_state->strokeShader);

    float width = m_state->strokeThickness;

    // This allows dashing and dotting to work properly for hairline strokes
    // FIXME: Should we only do this for dashed and dotted strokes?
    if (!width)
        width = 1;

    paint->setStyle(SkPaint::kStroke_Style);
    paint->setStrokeWidth(SkFloatToScalar(width));
    paint->setStrokeCap(m_state->lineCap);
    paint->setStrokeJoin(m_state->lineJoin);
    paint->setStrokeMiter(SkFloatToScalar(m_state->miterLimit));

    if (rect && (RoundToInt(width) & 1))
        rect->inset(-SK_ScalarHalf, -SK_ScalarHalf);

    SkPathEffect* pe = m_state->pathEffect;
    if (pe) {
        paint->setPathEffect(pe);
        return false;
    }
    switch (m_state->strokeStyle) {
    case NoStroke:
    case SolidStroke:
        width = 0;
        break;
    case DashedStroke:
        width = m_state->dashRatio * width;
        break;
        // No break
    case DottedStroke:
        break;
    }

    if (width > 0) {
        // Return true if we're basically a dotted dash of squares
        bool justSqrs = RoundToInt(width) == RoundToInt(paint->getStrokeWidth());

        if (justSqrs || !isHLine || !setBitmapDash(paint, width)) {
#if 0
            // this is slow enough that we just skip it for now
            // see http://b/issue?id=4163023
            SkScalar intervals[] = { width, width };
            pe = new SkDashPathEffect(intervals, 2, 0);
            paint->setPathEffect(pe)->unref();
#endif
        }
        return justSqrs;
    }
    return false;
}

}   // WebCore