gfx/skia/trunk/src/utils/SkPictureUtils.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

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.

     1 /*
     2  * Copyright 2012 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  */
     8 #include "SkBitmapDevice.h"
     9 #include "SkCanvas.h"
    10 #include "SkData.h"
    11 #include "SkNoSaveLayerCanvas.h"
    12 #include "SkPictureUtils.h"
    13 #include "SkPixelRef.h"
    14 #include "SkRRect.h"
    15 #include "SkShader.h"
    17 class PixelRefSet {
    18 public:
    19     PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {}
    21     // This does a linear search on existing pixelrefs, so if this list gets big
    22     // we should use a more complex sorted/hashy thing.
    23     //
    24     void add(SkPixelRef* pr) {
    25         uint32_t genID = pr->getGenerationID();
    26         if (fGenID.find(genID) < 0) {
    27             *fArray->append() = pr;
    28             *fGenID.append() = genID;
    29 //            SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID);
    30         } else {
    31 //            SkDebugf("--- already have %x %d\n", pr, genID);
    32         }
    33     }
    35 private:
    36     SkTDArray<SkPixelRef*>* fArray;
    37     SkTDArray<uint32_t>     fGenID;
    38 };
    40 static void not_supported() {
    41     SkDEBUGFAIL("this method should never be called");
    42 }
    44 static void nothing_to_do() {}
    46 /**
    47  *  This device will route all bitmaps (primitives and in shaders) to its PRSet.
    48  *  It should never actually draw anything, so there need not be any pixels
    49  *  behind its device.
    50  */
    51 class GatherPixelRefDevice : public SkBaseDevice {
    52 public:
    53     SK_DECLARE_INST_COUNT(GatherPixelRefDevice)
    55     GatherPixelRefDevice(int width, int height, PixelRefSet* prset) {
    56         fSize.set(width, height);
    57         fEmptyBitmap.setConfig(SkImageInfo::MakeUnknown(width, height));
    58         fPRSet = prset;
    59     }
    61     virtual int width() const SK_OVERRIDE { return fSize.width(); }
    62     virtual int height() const SK_OVERRIDE { return fSize.height(); }
    63     virtual bool isOpaque() const SK_OVERRIDE { return false; }
    64     virtual SkBitmap::Config config() const SK_OVERRIDE {
    65         return SkBitmap::kNo_Config;
    66     }
    67     virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
    68     virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
    69         return false;
    70     }
    71     // TODO: allow this call to return failure, or move to SkBitmapDevice only.
    72     virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
    73         return fEmptyBitmap;
    74     }
    75     virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
    76     virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
    77     virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
    78     virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
    79     virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
    80                              SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
    81         return false;
    82     }
    84     virtual void clear(SkColor color) SK_OVERRIDE {
    85         nothing_to_do();
    86     }
    88 #ifdef SK_SUPPORT_LEGACY_WRITEPIXELSCONFIG
    89     virtual void writePixels(const SkBitmap& bitmap, int x, int y,
    90                              SkCanvas::Config8888 config8888) SK_OVERRIDE {
    91         not_supported();
    92     }
    93 #endif
    95     virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
    96         this->addBitmapFromPaint(paint);
    97     }
    98     virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
    99                             const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
   100         this->addBitmapFromPaint(paint);
   101     }
   102     virtual void drawRect(const SkDraw&, const SkRect&,
   103                           const SkPaint& paint) SK_OVERRIDE {
   104         this->addBitmapFromPaint(paint);
   105     }
   106     virtual void drawRRect(const SkDraw&, const SkRRect&,
   107                            const SkPaint& paint) SK_OVERRIDE {
   108         this->addBitmapFromPaint(paint);
   109     }
   110     virtual void drawOval(const SkDraw&, const SkRect&,
   111                           const SkPaint& paint) SK_OVERRIDE {
   112         this->addBitmapFromPaint(paint);
   113     }
   114     virtual void drawPath(const SkDraw&, const SkPath& path,
   115                           const SkPaint& paint, const SkMatrix* prePathMatrix,
   116                           bool pathIsMutable) SK_OVERRIDE {
   117         this->addBitmapFromPaint(paint);
   118     }
   119     virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
   120                             const SkMatrix&, const SkPaint& paint) SK_OVERRIDE {
   121         this->addBitmap(bitmap);
   122         if (SkBitmap::kA8_Config == bitmap.config()) {
   123             this->addBitmapFromPaint(paint);
   124         }
   125     }
   126     virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
   127                                 const SkRect* srcOrNull, const SkRect& dst,
   128                                 const SkPaint& paint,
   129                                 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
   130         this->addBitmap(bitmap);
   131         if (SkBitmap::kA8_Config == bitmap.config()) {
   132             this->addBitmapFromPaint(paint);
   133         }
   134     }
   135     virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
   136                             int x, int y, const SkPaint& paint) SK_OVERRIDE {
   137         this->addBitmap(bitmap);
   138     }
   139     virtual void drawText(const SkDraw&, const void* text, size_t len,
   140                           SkScalar x, SkScalar y,
   141                           const SkPaint& paint) SK_OVERRIDE {
   142         this->addBitmapFromPaint(paint);
   143     }
   144     virtual void drawPosText(const SkDraw&, const void* text, size_t len,
   145                              const SkScalar pos[], SkScalar constY,
   146                              int, const SkPaint& paint) SK_OVERRIDE {
   147         this->addBitmapFromPaint(paint);
   148     }
   149     virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
   150                                 const SkPath& path, const SkMatrix* matrix,
   151                                 const SkPaint& paint) SK_OVERRIDE {
   152         this->addBitmapFromPaint(paint);
   153     }
   154     virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
   155                               const SkPoint verts[], const SkPoint texs[],
   156                               const SkColor colors[], SkXfermode* xmode,
   157                               const uint16_t indices[], int indexCount,
   158                               const SkPaint& paint) SK_OVERRIDE {
   159         this->addBitmapFromPaint(paint);
   160     }
   161     virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
   162                             const SkPaint&) SK_OVERRIDE {
   163         nothing_to_do();
   164     }
   166 protected:
   167     virtual bool onReadPixels(const SkBitmap& bitmap,
   168                               int x, int y,
   169                               SkCanvas::Config8888 config8888) SK_OVERRIDE {
   170         not_supported();
   171         return false;
   172     }
   174     virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
   175         not_supported();
   176     }
   177     virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE {
   178         // we expect to only get called via savelayer, in which case it is fine.
   179         SkASSERT(kSaveLayer_Usage == usage);
   180         return SkNEW_ARGS(GatherPixelRefDevice, (info.width(), info.height(), fPRSet));
   181     }
   182     virtual void flush() SK_OVERRIDE {}
   184 private:
   185     PixelRefSet*  fPRSet;
   186     SkBitmap fEmptyBitmap;  // legacy -- need to remove the need for this guy
   187     SkISize fSize;
   189     void addBitmap(const SkBitmap& bm) {
   190       fPRSet->add(bm.pixelRef());
   191     }
   193     void addBitmapFromPaint(const SkPaint& paint) {
   194       SkShader* shader = paint.getShader();
   195       if (shader) {
   196           SkBitmap bm;
   197           // Check whether the shader is a gradient in order to short-circuit
   198           // call to asABitmap to prevent generation of bitmaps from
   199           // gradient shaders, which implement asABitmap.
   200           if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
   201               shader->asABitmap(&bm, NULL, NULL)) {
   202               fPRSet->add(bm.pixelRef());
   203           }
   204       }
   205     }
   207     typedef SkBaseDevice INHERITED;
   208 };
   210 SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
   211     if (NULL == pict) {
   212         return NULL;
   213     }
   215     // this test also handles if either area or pict's width/height are empty
   216     if (!SkRect::Intersects(area,
   217                             SkRect::MakeWH(SkIntToScalar(pict->width()),
   218                                            SkIntToScalar(pict->height())))) {
   219         return NULL;
   220     }
   222     SkTDArray<SkPixelRef*> array;
   223     PixelRefSet prset(&array);
   225     GatherPixelRefDevice device(pict->width(), pict->height(), &prset);
   226     SkNoSaveLayerCanvas canvas(&device);
   228     canvas.clipRect(area, SkRegion::kIntersect_Op, false);
   229     canvas.drawPicture(*pict);
   231     SkData* data = NULL;
   232     int count = array.count();
   233     if (count > 0) {
   234         data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));
   235     }
   236     return data;
   237 }

mercurial