move responsibility for creating raster-device into pdf

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2146333004

NOTRY=True
win failure seems unrelated

Review-Url: https://codereview.chromium.org/2146333004
diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h
index 35a8d1c..e3b4ffb 100644
--- a/include/core/SkCanvas.h
+++ b/include/core/SkCanvas.h
@@ -1513,7 +1513,7 @@
                                 SrcRectConstraint);
     void internalDrawPaint(const SkPaint& paint);
     void internalSaveLayer(const SaveLayerRec&, SaveLayerStrategy);
-    void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*, bool isBitmapDevice);
+    void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*);
 
     // shared by save() and saveLayer()
     void internalSave();
diff --git a/src/core/SkCanvas.cpp b/src/core/SkCanvas.cpp
index 87db569..60bda8d 100644
--- a/src/core/SkCanvas.cpp
+++ b/src/core/SkCanvas.cpp
@@ -200,14 +200,12 @@
     const SkMatrix*     fMatrix;
     SkMatrix            fMatrixStorage;
     SkMatrix            fStashedMatrix; // original CTM; used by imagefilter in saveLayer
-    const bool          fDeviceIsBitmapDevice;
 
     DeviceCM(SkBaseDevice* device, const SkPaint* paint, SkCanvas* canvas,
-             bool conservativeRasterClip, bool deviceIsBitmapDevice, const SkMatrix& stashed)
+             bool conservativeRasterClip, const SkMatrix& stashed)
         : fNext(nullptr)
         , fClip(conservativeRasterClip)
         , fStashedMatrix(stashed)
-        , fDeviceIsBitmapDevice(deviceIsBitmapDevice)
     {
         if (nullptr != device) {
             device->ref();
@@ -673,7 +671,7 @@
 
     SkASSERT(sizeof(DeviceCM) <= sizeof(fDeviceCMStorage));
     fMCRec->fLayer = (DeviceCM*)fDeviceCMStorage;
-    new (fDeviceCMStorage) DeviceCM(nullptr, nullptr, nullptr, fConservativeRasterClip, false,
+    new (fDeviceCMStorage) DeviceCM(nullptr, nullptr, nullptr, fConservativeRasterClip,
                                     fMCRec->fMatrix);
 
     fMCRec->fTopLayer = fMCRec->fLayer;
@@ -1280,7 +1278,6 @@
     SkImageInfo info = make_layer_info(device->imageInfo(), ir.width(), ir.height(), isOpaque,
                                        paint);
 
-    bool forceSpriteOnRestore = false;
     {
         const bool preserveLCDText = kOpaque_SkAlphaType == info.alphaType() ||
                                      (saveLayerFlags & kPreserveLCDText_SaveLayerFlag);
@@ -1289,15 +1286,9 @@
                                                                              preserveLCDText);
         SkBaseDevice* newDev = device->onCreateDevice(createInfo, paint);
         if (nullptr == newDev) {
-            // If onCreateDevice didn't succeed, try raster (e.g. PDF couldn't handle the paint)
-            const SkSurfaceProps surfaceProps(fProps.flags(), createInfo.fPixelGeometry);
-            newDev = SkBitmapDevice::Create(createInfo.fInfo, surfaceProps);
-            if (nullptr == newDev) {
-                SkErrorInternals::SetError(kInternalError_SkError,
-                                           "Unable to create device for layer.");
-                return;
-            }
-            forceSpriteOnRestore = true;
+            SkErrorInternals::SetError(kInternalError_SkError,
+                                       "Unable to create device for layer.");
+            return;
         }
         device = newDev;
     }
@@ -1307,8 +1298,7 @@
         draw_filter_into_device(fMCRec->fTopLayer->fDevice, rec.fBackdrop, device, fMCRec->fMatrix);
     }
 
-    DeviceCM* layer = new DeviceCM(device, paint, this, fConservativeRasterClip,
-                                   forceSpriteOnRestore, stashedMatrix);
+    DeviceCM* layer = new DeviceCM(device, paint, this, fConservativeRasterClip, stashedMatrix);
     device->unref();
 
     layer->fNext = fMCRec->fTopLayer;
@@ -1351,8 +1341,7 @@
     if (layer) {
         if (layer->fNext) {
             const SkIPoint& origin = layer->fDevice->getOrigin();
-            this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(),
-                                     layer->fPaint, layer->fDeviceIsBitmapDevice);
+            this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), layer->fPaint);
             // restore what we smashed in internalSaveLayer
             fMCRec->fMatrix = layer->fStashedMatrix;
             // reset this, since internalDrawDevice will have set it to true
@@ -1457,8 +1446,7 @@
 
 /////////////////////////////////////////////////////////////////////////////
 
-void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y,
-                                  const SkPaint* paint, bool deviceIsBitmapDevice) {
+void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, const SkPaint* paint) {
     SkPaint tmp;
     if (nullptr == paint) {
         paint = &tmp;
@@ -1473,9 +1461,6 @@
         if (filter) {
             const SkBitmap& srcBM = srcDev->accessBitmap(false);
             dstDev->drawSpriteWithFilter(iter, srcBM, pos.x(), pos.y(), *paint);
-        } else if (deviceIsBitmapDevice) {
-            const SkBitmap& src = static_cast<SkBitmapDevice*>(srcDev)->fBitmap;
-            dstDev->drawSprite(iter, src, pos.x(), pos.y(), *paint);
         } else {
             dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint);
         }
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 973ebea..33ae8a4 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -6,8 +6,8 @@
  */
 
 #include "SkPDFDevice.h"
-
 #include "SkAnnotationKeys.h"
+#include "SkBitmapDevice.h"
 #include "SkBitmapKey.h"
 #include "SkColor.h"
 #include "SkColorFilter.h"
@@ -543,7 +543,8 @@
 
 SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
     if (layerPaint && not_supported_for_layers(*layerPaint)) {
-        return nullptr;
+        // need to return a raster device, which we will detect in drawDevice()
+        return SkBitmapDevice::Create(cinfo.fInfo, SkSurfaceProps(0, kUnknown_SkPixelGeometry));
     }
     SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
     return SkPDFDevice::Create(size, fRasterDpi, fDocument);
@@ -1340,6 +1341,20 @@
 
 void SkPDFDevice::drawDevice(const SkDraw& d, SkBaseDevice* device,
                              int x, int y, const SkPaint& paint) {
+    // Check if the source device is really a bitmapdevice (because that's what we returned
+    // from createDevice (likely due to an imagefilter)
+    SkPixmap pmap;
+    if (device->peekPixels(&pmap)) {
+        SkBitmap bitmap;
+        bitmap.installPixels(pmap);
+        if (paint.getImageFilter()) {
+            this->drawSpriteWithFilter(d, bitmap, x, y, paint);
+        } else {
+            this->drawSprite(d, bitmap, x, y, paint);
+        }
+        return;
+    }
+
     // our onCreateCompatibleDevice() always creates SkPDFDevice subclasses.
     SkPDFDevice* pdfDevice = static_cast<SkPDFDevice*>(device);