Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2014 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef SkGatherPixelRefsAndRects_DEFINED |
michael@0 | 9 | #define SkGatherPixelRefsAndRects_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "SkBitmap.h" |
michael@0 | 12 | #include "SkDevice.h" |
michael@0 | 13 | #include "SkDraw.h" |
michael@0 | 14 | #include "SkPictureUtils.h" |
michael@0 | 15 | #include "SkRasterClip.h" |
michael@0 | 16 | #include "SkRefCnt.h" |
michael@0 | 17 | #include "SkRRect.h" |
michael@0 | 18 | #include "SkTypes.h" |
michael@0 | 19 | |
michael@0 | 20 | // This GatherPixelRefs device passes all discovered pixel refs and their |
michael@0 | 21 | // device bounds to the user provided SkPixelRefContainer-derived object |
michael@0 | 22 | class SkGatherPixelRefsAndRectsDevice : public SkBaseDevice { |
michael@0 | 23 | public: |
michael@0 | 24 | SK_DECLARE_INST_COUNT(SkGatherPixelRefsAndRectsDevice) |
michael@0 | 25 | |
michael@0 | 26 | SkGatherPixelRefsAndRectsDevice(int width, int height, |
michael@0 | 27 | SkPictureUtils::SkPixelRefContainer* prCont) { |
michael@0 | 28 | fSize.set(width, height); |
michael@0 | 29 | fPRCont = prCont; |
michael@0 | 30 | SkSafeRef(fPRCont); |
michael@0 | 31 | fEmptyBitmap.setConfig(SkImageInfo::Make(width, height, |
michael@0 | 32 | kUnknown_SkColorType, |
michael@0 | 33 | kIgnore_SkAlphaType)); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | virtual ~SkGatherPixelRefsAndRectsDevice() { |
michael@0 | 37 | SkSafeUnref(fPRCont); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | virtual int width() const SK_OVERRIDE { return fSize.width(); } |
michael@0 | 41 | virtual int height() const SK_OVERRIDE { return fSize.height(); } |
michael@0 | 42 | virtual bool isOpaque() const SK_OVERRIDE { return false; } |
michael@0 | 43 | virtual SkBitmap::Config config() const SK_OVERRIDE { |
michael@0 | 44 | return SkBitmap::kNo_Config; |
michael@0 | 45 | } |
michael@0 | 46 | virtual SkImageInfo imageInfo() const SK_OVERRIDE { |
michael@0 | 47 | return fEmptyBitmap.info(); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG |
michael@0 | 51 | virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
michael@0 | 52 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
michael@0 | 53 | NotSupported(); |
michael@0 | 54 | } |
michael@0 | 55 | #endif |
michael@0 | 56 | virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
michael@0 | 57 | |
michael@0 | 58 | protected: |
michael@0 | 59 | virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { |
michael@0 | 60 | return false; |
michael@0 | 61 | } |
michael@0 | 62 | virtual void clear(SkColor color) SK_OVERRIDE { |
michael@0 | 63 | NothingToDo(); |
michael@0 | 64 | } |
michael@0 | 65 | virtual void drawPaint(const SkDraw& draw, const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 66 | SkBitmap bm; |
michael@0 | 67 | |
michael@0 | 68 | if (GetBitmapFromPaint(paint, &bm)) { |
michael@0 | 69 | SkRect clipRect = SkRect::Make(draw.fRC->getBounds()); |
michael@0 | 70 | fPRCont->add(bm.pixelRef(), clipRect); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | virtual void drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, size_t count, |
michael@0 | 74 | const SkPoint points[], const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 75 | SkBitmap bm; |
michael@0 | 76 | if (!GetBitmapFromPaint(paint, &bm)) { |
michael@0 | 77 | return; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | if (0 == count) { |
michael@0 | 81 | return; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | SkPoint min = points[0]; |
michael@0 | 85 | SkPoint max = points[0]; |
michael@0 | 86 | for (size_t i = 1; i < count; ++i) { |
michael@0 | 87 | const SkPoint& point = points[i]; |
michael@0 | 88 | |
michael@0 | 89 | min.set(SkMinScalar(min.x(), point.x()), SkMinScalar(min.y(), point.y())); |
michael@0 | 90 | max.set(SkMaxScalar(max.x(), point.x()), SkMaxScalar(max.y(), point.y())); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | SkRect bounds = SkRect::MakeLTRB(min.x(), min.y(), max.x()+1, max.y()+1); |
michael@0 | 94 | |
michael@0 | 95 | this->drawRect(draw, bounds, paint); |
michael@0 | 96 | } |
michael@0 | 97 | virtual void drawRect(const SkDraw& draw, const SkRect& rect, |
michael@0 | 98 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 99 | SkBitmap bm; |
michael@0 | 100 | if (GetBitmapFromPaint(paint, &bm)) { |
michael@0 | 101 | SkRect mappedRect; |
michael@0 | 102 | draw.fMatrix->mapRect(&mappedRect, rect); |
michael@0 | 103 | SkRect clipRect = SkRect::Make(draw.fRC->getBounds()); |
michael@0 | 104 | mappedRect.intersect(clipRect); |
michael@0 | 105 | fPRCont->add(bm.pixelRef(), mappedRect); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | virtual void drawOval(const SkDraw& draw, const SkRect& rect, |
michael@0 | 109 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 110 | this->drawRect(draw, rect, paint); |
michael@0 | 111 | } |
michael@0 | 112 | virtual void drawRRect(const SkDraw& draw, const SkRRect& rrect, |
michael@0 | 113 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 114 | this->drawRect(draw, rrect.rect(), paint); |
michael@0 | 115 | } |
michael@0 | 116 | virtual void drawPath(const SkDraw& draw, const SkPath& path, |
michael@0 | 117 | const SkPaint& paint, const SkMatrix* prePathMatrix, |
michael@0 | 118 | bool pathIsMutable) SK_OVERRIDE { |
michael@0 | 119 | SkBitmap bm; |
michael@0 | 120 | if (!GetBitmapFromPaint(paint, &bm)) { |
michael@0 | 121 | return; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | SkRect pathBounds = path.getBounds(); |
michael@0 | 125 | if (NULL != prePathMatrix) { |
michael@0 | 126 | prePathMatrix->mapRect(&pathBounds); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | this->drawRect(draw, pathBounds, paint); |
michael@0 | 130 | } |
michael@0 | 131 | virtual void drawBitmap(const SkDraw& draw, const SkBitmap& bitmap, |
michael@0 | 132 | const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 133 | SkMatrix totMatrix; |
michael@0 | 134 | totMatrix.setConcat(*draw.fMatrix, matrix); |
michael@0 | 135 | |
michael@0 | 136 | SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()), |
michael@0 | 137 | SkIntToScalar(bitmap.height())); |
michael@0 | 138 | SkRect mappedRect; |
michael@0 | 139 | totMatrix.mapRect(&mappedRect, bitmapRect); |
michael@0 | 140 | fPRCont->add(bitmap.pixelRef(), mappedRect); |
michael@0 | 141 | |
michael@0 | 142 | SkBitmap paintBitmap; |
michael@0 | 143 | if (GetBitmapFromPaint(paint, &paintBitmap)) { |
michael@0 | 144 | fPRCont->add(paintBitmap.pixelRef(), mappedRect); |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap, |
michael@0 | 148 | int x, int y, const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 149 | // Sprites aren't affected by current matrix, so we can't reuse drawRect. |
michael@0 | 150 | SkMatrix matrix; |
michael@0 | 151 | matrix.setTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
michael@0 | 152 | |
michael@0 | 153 | SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()), |
michael@0 | 154 | SkIntToScalar(bitmap.height())); |
michael@0 | 155 | SkRect mappedRect; |
michael@0 | 156 | matrix.mapRect(&mappedRect, bitmapRect); |
michael@0 | 157 | fPRCont->add(bitmap.pixelRef(), mappedRect); |
michael@0 | 158 | |
michael@0 | 159 | SkBitmap paintBitmap; |
michael@0 | 160 | if (GetBitmapFromPaint(paint, &paintBitmap)) { |
michael@0 | 161 | fPRCont->add(paintBitmap.pixelRef(), mappedRect); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | virtual void drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap, |
michael@0 | 165 | const SkRect* srcOrNull, const SkRect& dst, |
michael@0 | 166 | const SkPaint& paint, |
michael@0 | 167 | SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE { |
michael@0 | 168 | SkRect bitmapRect = SkRect::MakeWH(SkIntToScalar(bitmap.width()), |
michael@0 | 169 | SkIntToScalar(bitmap.height())); |
michael@0 | 170 | SkMatrix matrix; |
michael@0 | 171 | matrix.setRectToRect(bitmapRect, dst, SkMatrix::kFill_ScaleToFit); |
michael@0 | 172 | this->drawBitmap(draw, bitmap, matrix, paint); |
michael@0 | 173 | } |
michael@0 | 174 | virtual void drawText(const SkDraw& draw, const void* text, size_t len, |
michael@0 | 175 | SkScalar x, SkScalar y, |
michael@0 | 176 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 177 | SkBitmap bitmap; |
michael@0 | 178 | if (!GetBitmapFromPaint(paint, &bitmap)) { |
michael@0 | 179 | return; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | // Math is borrowed from SkBBoxRecord |
michael@0 | 183 | SkRect bounds; |
michael@0 | 184 | paint.measureText(text, len, &bounds); |
michael@0 | 185 | SkPaint::FontMetrics metrics; |
michael@0 | 186 | paint.getFontMetrics(&metrics); |
michael@0 | 187 | |
michael@0 | 188 | if (paint.isVerticalText()) { |
michael@0 | 189 | SkScalar h = bounds.fBottom - bounds.fTop; |
michael@0 | 190 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
michael@0 | 191 | bounds.fTop -= h / 2; |
michael@0 | 192 | bounds.fBottom -= h / 2; |
michael@0 | 193 | } |
michael@0 | 194 | bounds.fBottom += metrics.fBottom; |
michael@0 | 195 | bounds.fTop += metrics.fTop; |
michael@0 | 196 | } else { |
michael@0 | 197 | SkScalar w = bounds.fRight - bounds.fLeft; |
michael@0 | 198 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
michael@0 | 199 | bounds.fLeft -= w / 2; |
michael@0 | 200 | bounds.fRight -= w / 2; |
michael@0 | 201 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
michael@0 | 202 | bounds.fLeft -= w; |
michael@0 | 203 | bounds.fRight -= w; |
michael@0 | 204 | } |
michael@0 | 205 | bounds.fTop = metrics.fTop; |
michael@0 | 206 | bounds.fBottom = metrics.fBottom; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | SkScalar pad = (metrics.fBottom - metrics.fTop) / 2; |
michael@0 | 210 | bounds.fLeft -= pad; |
michael@0 | 211 | bounds.fRight += pad; |
michael@0 | 212 | bounds.offset(x, y); |
michael@0 | 213 | |
michael@0 | 214 | this->drawRect(draw, bounds, paint); |
michael@0 | 215 | } |
michael@0 | 216 | virtual void drawPosText(const SkDraw& draw, const void* text, size_t len, |
michael@0 | 217 | const SkScalar pos[], SkScalar constY, |
michael@0 | 218 | int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 219 | SkBitmap bitmap; |
michael@0 | 220 | if (!GetBitmapFromPaint(paint, &bitmap)) { |
michael@0 | 221 | return; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | if (0 == len) { |
michael@0 | 225 | return; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | // Similar to SkDraw asserts. |
michael@0 | 229 | SkASSERT(scalarsPerPos == 1 || scalarsPerPos == 2); |
michael@0 | 230 | |
michael@0 | 231 | SkScalar y = scalarsPerPos == 1 ? constY : constY + pos[1]; |
michael@0 | 232 | |
michael@0 | 233 | SkPoint min, max; |
michael@0 | 234 | min.set(pos[0], y); |
michael@0 | 235 | max.set(pos[0], y); |
michael@0 | 236 | |
michael@0 | 237 | for (size_t i = 1; i < len; ++i) { |
michael@0 | 238 | SkScalar x = pos[i * scalarsPerPos]; |
michael@0 | 239 | SkScalar y = constY; |
michael@0 | 240 | if (2 == scalarsPerPos) { |
michael@0 | 241 | y += pos[i * scalarsPerPos + 1]; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | min.set(SkMinScalar(x, min.x()), SkMinScalar(y, min.y())); |
michael@0 | 245 | max.set(SkMaxScalar(x, max.x()), SkMaxScalar(y, max.y())); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | SkRect bounds = SkRect::MakeLTRB(min.x(), min.y(), max.x(), max.y()); |
michael@0 | 249 | |
michael@0 | 250 | // Math is borrowed from SkBBoxRecord |
michael@0 | 251 | SkPaint::FontMetrics metrics; |
michael@0 | 252 | paint.getFontMetrics(&metrics); |
michael@0 | 253 | |
michael@0 | 254 | bounds.fTop += metrics.fTop; |
michael@0 | 255 | bounds.fBottom += metrics.fBottom; |
michael@0 | 256 | |
michael@0 | 257 | SkScalar pad = (metrics.fTop - metrics.fBottom) / 2; |
michael@0 | 258 | bounds.fLeft -= pad; |
michael@0 | 259 | bounds.fRight += pad; |
michael@0 | 260 | |
michael@0 | 261 | this->drawRect(draw, bounds, paint); |
michael@0 | 262 | } |
michael@0 | 263 | virtual void drawTextOnPath(const SkDraw& draw, const void* text, size_t len, |
michael@0 | 264 | const SkPath& path, const SkMatrix* matrix, |
michael@0 | 265 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 266 | SkBitmap bitmap; |
michael@0 | 267 | if (!GetBitmapFromPaint(paint, &bitmap)) { |
michael@0 | 268 | return; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | // Math is borrowed from SkBBoxRecord |
michael@0 | 272 | SkRect bounds = path.getBounds(); |
michael@0 | 273 | SkPaint::FontMetrics metrics; |
michael@0 | 274 | paint.getFontMetrics(&metrics); |
michael@0 | 275 | |
michael@0 | 276 | SkScalar pad = metrics.fTop; |
michael@0 | 277 | // TODO: inset?! |
michael@0 | 278 | bounds.fLeft += pad; |
michael@0 | 279 | bounds.fRight -= pad; |
michael@0 | 280 | bounds.fTop += pad; |
michael@0 | 281 | bounds.fBottom -= pad; |
michael@0 | 282 | |
michael@0 | 283 | this->drawRect(draw, bounds, paint); |
michael@0 | 284 | } |
michael@0 | 285 | virtual void drawVertices(const SkDraw& draw, SkCanvas::VertexMode, int vertexCount, |
michael@0 | 286 | const SkPoint verts[], const SkPoint texs[], |
michael@0 | 287 | const SkColor colors[], SkXfermode* xmode, |
michael@0 | 288 | const uint16_t indices[], int indexCount, |
michael@0 | 289 | const SkPaint& paint) SK_OVERRIDE { |
michael@0 | 290 | this->drawPoints(draw, SkCanvas::kPolygon_PointMode, vertexCount, verts, paint); |
michael@0 | 291 | } |
michael@0 | 292 | virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
michael@0 | 293 | const SkPaint&) SK_OVERRIDE { |
michael@0 | 294 | NothingToDo(); |
michael@0 | 295 | } |
michael@0 | 296 | // TODO: allow this call to return failure, or move to SkBitmapDevice only. |
michael@0 | 297 | virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { |
michael@0 | 298 | return fEmptyBitmap; |
michael@0 | 299 | } |
michael@0 | 300 | virtual bool onReadPixels(const SkBitmap& bitmap, |
michael@0 | 301 | int x, int y, |
michael@0 | 302 | SkCanvas::Config8888 config8888) SK_OVERRIDE { |
michael@0 | 303 | NotSupported(); |
michael@0 | 304 | return false; |
michael@0 | 305 | } |
michael@0 | 306 | virtual void lockPixels() SK_OVERRIDE { NothingToDo(); } |
michael@0 | 307 | virtual void unlockPixels() SK_OVERRIDE { NothingToDo(); } |
michael@0 | 308 | virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
michael@0 | 309 | virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; } |
michael@0 | 310 | virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&, |
michael@0 | 311 | SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { |
michael@0 | 312 | return false; |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | private: |
michael@0 | 316 | SkPictureUtils::SkPixelRefContainer* fPRCont; |
michael@0 | 317 | SkISize fSize; |
michael@0 | 318 | |
michael@0 | 319 | SkBitmap fEmptyBitmap; // legacy -- need to remove |
michael@0 | 320 | |
michael@0 | 321 | static bool GetBitmapFromPaint(const SkPaint &paint, SkBitmap* bitmap) { |
michael@0 | 322 | SkShader* shader = paint.getShader(); |
michael@0 | 323 | if (NULL != shader) { |
michael@0 | 324 | if (SkShader::kNone_GradientType == shader->asAGradient(NULL)) { |
michael@0 | 325 | return SkShader::kNone_BitmapType != shader->asABitmap(bitmap, NULL, NULL); |
michael@0 | 326 | } |
michael@0 | 327 | } |
michael@0 | 328 | return false; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE { |
michael@0 | 332 | NotSupported(); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE { |
michael@0 | 336 | // we expect to only get called via savelayer, in which case it is fine. |
michael@0 | 337 | SkASSERT(kSaveLayer_Usage == usage); |
michael@0 | 338 | return SkNEW_ARGS(SkGatherPixelRefsAndRectsDevice, |
michael@0 | 339 | (info.width(), info.height(), fPRCont)); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | virtual void flush() SK_OVERRIDE {} |
michael@0 | 343 | |
michael@0 | 344 | static void NotSupported() { |
michael@0 | 345 | SkDEBUGFAIL("this method should never be called"); |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | static void NothingToDo() {} |
michael@0 | 349 | |
michael@0 | 350 | typedef SkBaseDevice INHERITED; |
michael@0 | 351 | }; |
michael@0 | 352 | |
michael@0 | 353 | #endif // SkGatherPixelRefsAndRects_DEFINED |