gfx/skia/trunk/src/effects/SkLayerRasterizer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2006 The Android Open Source Project
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8
michael@0 9
michael@0 10 #include "SkLayerRasterizer.h"
michael@0 11 #include "SkDraw.h"
michael@0 12 #include "SkReadBuffer.h"
michael@0 13 #include "SkWriteBuffer.h"
michael@0 14 #include "SkMask.h"
michael@0 15 #include "SkMaskFilter.h"
michael@0 16 #include "SkPaint.h"
michael@0 17 #include "SkPath.h"
michael@0 18 #include "SkPathEffect.h"
michael@0 19 #include "../core/SkRasterClip.h"
michael@0 20 #include "SkXfermode.h"
michael@0 21 #include <new>
michael@0 22
michael@0 23 struct SkLayerRasterizer_Rec {
michael@0 24 SkPaint fPaint;
michael@0 25 SkVector fOffset;
michael@0 26 };
michael@0 27
michael@0 28 SkLayerRasterizer::SkLayerRasterizer()
michael@0 29 : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec))))
michael@0 30 {
michael@0 31 }
michael@0 32
michael@0 33 SkLayerRasterizer::SkLayerRasterizer(SkDeque* layers) : fLayers(layers)
michael@0 34 {
michael@0 35 }
michael@0 36
michael@0 37 SkLayerRasterizer::~SkLayerRasterizer() {
michael@0 38 SkASSERT(fLayers);
michael@0 39 SkDeque::F2BIter iter(*fLayers);
michael@0 40 SkLayerRasterizer_Rec* rec;
michael@0 41
michael@0 42 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL)
michael@0 43 rec->fPaint.~SkPaint();
michael@0 44
michael@0 45 SkDELETE(fLayers);
michael@0 46 }
michael@0 47
michael@0 48 #ifdef SK_SUPPORT_LEGACY_LAYERRASTERIZER_API
michael@0 49 void SkLayerRasterizer::addLayer(const SkPaint& paint, SkScalar dx,
michael@0 50 SkScalar dy) {
michael@0 51 SkASSERT(fLayers);
michael@0 52 SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back();
michael@0 53
michael@0 54 SkNEW_PLACEMENT_ARGS(&rec->fPaint, SkPaint, (paint));
michael@0 55 rec->fOffset.set(dx, dy);
michael@0 56 }
michael@0 57 #endif
michael@0 58
michael@0 59 static bool compute_bounds(const SkDeque& layers, const SkPath& path,
michael@0 60 const SkMatrix& matrix,
michael@0 61 const SkIRect* clipBounds, SkIRect* bounds) {
michael@0 62 SkDeque::F2BIter iter(layers);
michael@0 63 SkLayerRasterizer_Rec* rec;
michael@0 64
michael@0 65 bounds->set(SK_MaxS32, SK_MaxS32, SK_MinS32, SK_MinS32);
michael@0 66
michael@0 67 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
michael@0 68 const SkPaint& paint = rec->fPaint;
michael@0 69 SkPath fillPath, devPath;
michael@0 70 const SkPath* p = &path;
michael@0 71
michael@0 72 if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) {
michael@0 73 paint.getFillPath(path, &fillPath);
michael@0 74 p = &fillPath;
michael@0 75 }
michael@0 76 if (p->isEmpty()) {
michael@0 77 continue;
michael@0 78 }
michael@0 79
michael@0 80 // apply the matrix and offset
michael@0 81 {
michael@0 82 SkMatrix m = matrix;
michael@0 83 m.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
michael@0 84 p->transform(m, &devPath);
michael@0 85 }
michael@0 86
michael@0 87 SkMask mask;
michael@0 88 if (!SkDraw::DrawToMask(devPath, clipBounds, paint.getMaskFilter(),
michael@0 89 &matrix, &mask,
michael@0 90 SkMask::kJustComputeBounds_CreateMode,
michael@0 91 SkPaint::kFill_Style)) {
michael@0 92 return false;
michael@0 93 }
michael@0 94
michael@0 95 bounds->join(mask.fBounds);
michael@0 96 }
michael@0 97 return true;
michael@0 98 }
michael@0 99
michael@0 100 bool SkLayerRasterizer::onRasterize(const SkPath& path, const SkMatrix& matrix,
michael@0 101 const SkIRect* clipBounds,
michael@0 102 SkMask* mask, SkMask::CreateMode mode) const {
michael@0 103 SkASSERT(fLayers);
michael@0 104 if (fLayers->empty()) {
michael@0 105 return false;
michael@0 106 }
michael@0 107
michael@0 108 if (SkMask::kJustRenderImage_CreateMode != mode) {
michael@0 109 if (!compute_bounds(*fLayers, path, matrix, clipBounds, &mask->fBounds))
michael@0 110 return false;
michael@0 111 }
michael@0 112
michael@0 113 if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) {
michael@0 114 mask->fFormat = SkMask::kA8_Format;
michael@0 115 mask->fRowBytes = mask->fBounds.width();
michael@0 116 size_t size = mask->computeImageSize();
michael@0 117 if (0 == size) {
michael@0 118 return false; // too big to allocate, abort
michael@0 119 }
michael@0 120 mask->fImage = SkMask::AllocImage(size);
michael@0 121 memset(mask->fImage, 0, size);
michael@0 122 }
michael@0 123
michael@0 124 if (SkMask::kJustComputeBounds_CreateMode != mode) {
michael@0 125 SkBitmap device;
michael@0 126 SkRasterClip rectClip;
michael@0 127 SkDraw draw;
michael@0 128 SkMatrix translatedMatrix; // this translates us to our local pixels
michael@0 129 SkMatrix drawMatrix; // this translates the path by each layer's offset
michael@0 130
michael@0 131 rectClip.setRect(SkIRect::MakeWH(mask->fBounds.width(), mask->fBounds.height()));
michael@0 132
michael@0 133 translatedMatrix = matrix;
michael@0 134 translatedMatrix.postTranslate(-SkIntToScalar(mask->fBounds.fLeft),
michael@0 135 -SkIntToScalar(mask->fBounds.fTop));
michael@0 136
michael@0 137 device.installMaskPixels(*mask);
michael@0 138
michael@0 139 draw.fBitmap = &device;
michael@0 140 draw.fMatrix = &drawMatrix;
michael@0 141 draw.fRC = &rectClip;
michael@0 142 draw.fClip = &rectClip.bwRgn();
michael@0 143 // we set the matrixproc in the loop, as the matrix changes each time (potentially)
michael@0 144 draw.fBounder = NULL;
michael@0 145
michael@0 146 SkDeque::F2BIter iter(*fLayers);
michael@0 147 SkLayerRasterizer_Rec* rec;
michael@0 148
michael@0 149 while ((rec = (SkLayerRasterizer_Rec*)iter.next()) != NULL) {
michael@0 150 drawMatrix = translatedMatrix;
michael@0 151 drawMatrix.preTranslate(rec->fOffset.fX, rec->fOffset.fY);
michael@0 152 draw.drawPath(path, rec->fPaint);
michael@0 153 }
michael@0 154 }
michael@0 155 return true;
michael@0 156 }
michael@0 157
michael@0 158 SkLayerRasterizer::SkLayerRasterizer(SkReadBuffer& buffer)
michael@0 159 : SkRasterizer(buffer), fLayers(ReadLayers(buffer)) {}
michael@0 160
michael@0 161 SkDeque* SkLayerRasterizer::ReadLayers(SkReadBuffer& buffer) {
michael@0 162 int count = buffer.readInt();
michael@0 163
michael@0 164 SkDeque* layers = SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec)));
michael@0 165 for (int i = 0; i < count; i++) {
michael@0 166 SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)layers->push_back();
michael@0 167
michael@0 168 SkNEW_PLACEMENT(&rec->fPaint, SkPaint);
michael@0 169 buffer.readPaint(&rec->fPaint);
michael@0 170 buffer.readPoint(&rec->fOffset);
michael@0 171 }
michael@0 172 return layers;
michael@0 173 }
michael@0 174
michael@0 175 void SkLayerRasterizer::flatten(SkWriteBuffer& buffer) const {
michael@0 176 this->INHERITED::flatten(buffer);
michael@0 177
michael@0 178 SkASSERT(fLayers);
michael@0 179 buffer.writeInt(fLayers->count());
michael@0 180
michael@0 181 SkDeque::F2BIter iter(*fLayers);
michael@0 182 const SkLayerRasterizer_Rec* rec;
michael@0 183
michael@0 184 while ((rec = (const SkLayerRasterizer_Rec*)iter.next()) != NULL) {
michael@0 185 buffer.writePaint(rec->fPaint);
michael@0 186 buffer.writePoint(rec->fOffset);
michael@0 187 }
michael@0 188 }
michael@0 189
michael@0 190 SkLayerRasterizer::Builder::Builder()
michael@0 191 : fLayers(SkNEW_ARGS(SkDeque, (sizeof(SkLayerRasterizer_Rec))))
michael@0 192 {
michael@0 193 }
michael@0 194
michael@0 195 SkLayerRasterizer::Builder::~Builder()
michael@0 196 {
michael@0 197 SkDELETE(fLayers);
michael@0 198 }
michael@0 199
michael@0 200 void SkLayerRasterizer::Builder::addLayer(const SkPaint& paint, SkScalar dx,
michael@0 201 SkScalar dy) {
michael@0 202 SkASSERT(fLayers);
michael@0 203 SkLayerRasterizer_Rec* rec = (SkLayerRasterizer_Rec*)fLayers->push_back();
michael@0 204
michael@0 205 SkNEW_PLACEMENT_ARGS(&rec->fPaint, SkPaint, (paint));
michael@0 206 rec->fOffset.set(dx, dy);
michael@0 207 }
michael@0 208
michael@0 209 SkLayerRasterizer* SkLayerRasterizer::Builder::detachRasterizer() {
michael@0 210 SkLayerRasterizer* rasterizer = SkNEW_ARGS(SkLayerRasterizer, (fLayers));
michael@0 211 fLayers = NULL;
michael@0 212 return rasterizer;
michael@0 213 }

mercurial