| 1 |
/* |
| 2 |
* Copyright 2018 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkBitmap.h" |
| 9 |
#include "include/core/SkImageInfo.h" |
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkRegion.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkTo.h" |
| 15 |
#include "src/base/SkArenaAlloc.h" |
| 16 |
#include "src/base/SkZip.h" |
| 17 |
#include "src/core/SkAAClip.h" |
| 18 |
#include "src/core/SkBlitter.h" |
| 19 |
#include "src/core/SkDraw.h" |
| 20 |
#include "src/core/SkDrawTypes.h" |
| 21 |
#include "src/core/SkGlyph.h" |
| 22 |
#include "src/core/SkGlyphRunPainter.h" |
| 23 |
#include "src/core/SkMask.h" |
| 24 |
#include "src/core/SkRasterClip.h" |
| 25 |
#include "src/core/SkSurfacePriv.h" |
| 26 |
|
| 27 |
#include <cstdint> |
| 28 |
#include <climits> |
| 29 |
|
| 30 |
class SkCanvas; |
| 31 |
class SkPaint; |
| 32 |
namespace sktext { class GlyphRunList; } |
| 33 |
|
| 34 |
// disable warning : local variable used without having been initialized |
| 35 |
#if defined _WIN32 |
| 36 |
#pragma warning ( push ) |
| 37 |
#pragma warning ( disable : 4701 ) |
| 38 |
#endif |
| 39 |
|
| 40 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
| 41 |
|
| 42 |
static bool check_glyph_position(SkPoint position) { |
| 43 |
// Prevent glyphs from being drawn outside of or straddling the edge of device space. |
| 44 |
// Comparisons written a little weirdly so that NaN coordinates are treated safely. |
| 45 |
auto gt = [](float a, int b) { return !(a <= (float)b); }; |
| 46 |
auto lt = [](float a, int b) { return !(a >= (float)b); }; |
| 47 |
return !(gt(position.fX, INT_MAX - (INT16_MAX + SkTo<int>(UINT16_MAX))) || |
| 48 |
lt(position.fX, INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/)) || |
| 49 |
gt(position.fY, INT_MAX - (INT16_MAX + SkTo<int>(UINT16_MAX))) || |
| 50 |
lt(position.fY, INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/))); |
| 51 |
} |
| 52 |
|
| 53 |
namespace skcpu { |
| 54 |
void Draw::paintMasks(SkZip<const SkGlyph*, SkPoint> accepted, const SkPaint& paint) const { |
| 55 |
SkSTArenaAlloc<kSkBlitterContextSize> alloc; |
| 56 |
SkBlitter* blitter = SkBlitter::Choose(fDst, |
| 57 |
*fCTM, |
| 58 |
paint, |
| 59 |
&alloc, |
| 60 |
SkDrawCoverage::kNo, |
| 61 |
fRC->clipShader(), |
| 62 |
SkSurfacePropsCopyOrDefault(fProps), |
| 63 |
SkRect::MakeEmpty()); |
| 64 |
|
| 65 |
SkAAClipBlitterWrapper wrapper{*fRC, blitter}; |
| 66 |
blitter = wrapper.getBlitter(); |
| 67 |
|
| 68 |
bool useRegion = fRC->isBW() && !fRC->isRect(); |
| 69 |
|
| 70 |
if (useRegion) { |
| 71 |
for (auto [glyph, pos] : accepted) { |
| 72 |
if (check_glyph_position(pos)) { |
| 73 |
SkMask mask = glyph->mask(pos); |
| 74 |
|
| 75 |
SkRegion::Cliperator clipper(fRC->bwRgn(), mask.fBounds); |
| 76 |
|
| 77 |
if (!clipper.done()) { |
| 78 |
if (SkMask::kARGB32_Format == mask.fFormat) { |
| 79 |
SkBitmap bm; |
| 80 |
bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.size()), |
| 81 |
const_cast<uint8_t*>(mask.fImage), |
| 82 |
mask.fRowBytes); |
| 83 |
bm.setImmutable(); |
| 84 |
this->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), paint); |
| 85 |
} else { |
| 86 |
const SkIRect& cr = clipper.rect(); |
| 87 |
do { |
| 88 |
blitter->blitMask(mask, cr); |
| 89 |
clipper.next(); |
| 90 |
} while (!clipper.done()); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} else { |
| 96 |
SkIRect clipBounds = fRC->isBW() ? fRC->bwRgn().getBounds() |
| 97 |
: fRC->aaRgn().getBounds(); |
| 98 |
for (auto [glyph, pos] : accepted) { |
| 99 |
if (check_glyph_position(pos)) { |
| 100 |
SkMask mask = glyph->mask(pos); |
| 101 |
SkIRect storage; |
| 102 |
const SkIRect* bounds = &mask.fBounds; |
| 103 |
|
| 104 |
// this extra test is worth it, assuming that most of the time it succeeds |
| 105 |
// since we can avoid writing to storage |
| 106 |
if (!clipBounds.containsNoEmptyCheck(mask.fBounds)) { |
| 107 |
if (!storage.intersect(mask.fBounds, clipBounds)) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
bounds = &storage; |
| 111 |
} |
| 112 |
|
| 113 |
if (SkMask::kARGB32_Format == mask.fFormat) { |
| 114 |
SkBitmap bm; |
| 115 |
bm.installPixels(SkImageInfo::MakeN32Premul(mask.fBounds.size()), |
| 116 |
const_cast<uint8_t*>(mask.fImage), |
| 117 |
mask.fRowBytes); |
| 118 |
bm.setImmutable(); |
| 119 |
this->drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), paint); |
| 120 |
} else { |
| 121 |
blitter->blitMask(mask, *bounds); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void Draw::drawGlyphRunList(SkCanvas* canvas, |
| 129 |
GlyphRunListPainter* glyphPainter, |
| 130 |
const sktext::GlyphRunList& glyphRunList, |
| 131 |
const SkPaint& paint) const { |
| 132 |
SkDEBUGCODE(this->validate();) |
| 133 |
|
| 134 |
if (fRC->isEmpty()) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
glyphPainter->drawForBitmapDevice(canvas, this, glyphRunList, paint, *fCTM); |
| 139 |
} |
| 140 |
} // namespace skcpu |
| 141 |
|
| 142 |
#if defined _WIN32 |
| 143 |
#pragma warning ( pop ) |
| 144 |
#endif |
| 145 |
|