onCreateDevice -> NULL now means the caller should create its own (bitmap) device

BUG=skia:

Review URL: https://codereview.chromium.org/1116453002
This commit is contained in:
reed 2015-04-29 08:34:00 -07:00 committed by Commit bot
parent aec2510125
commit 61f501f8c6
6 changed files with 46 additions and 21 deletions

View File

@ -1257,7 +1257,7 @@ private:
MCRec* fMCRec; MCRec* fMCRec;
// the first N recs that can fit here mean we won't call malloc // the first N recs that can fit here mean we won't call malloc
enum { enum {
kMCRecSize = 128, // most recent measurement kMCRecSize = 136, // most recent measurement
kMCRecCount = 8, // common depth for save/restores kMCRecCount = 8, // common depth for save/restores
}; };
intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)]; intptr_t fMCRecStorage[kMCRecSize * kMCRecCount / sizeof(intptr_t)];
@ -1330,7 +1330,7 @@ private:
const SkRect& dst, const SkPaint* paint); const SkRect& dst, const SkPaint* paint);
void internalDrawPaint(const SkPaint& paint); void internalDrawPaint(const SkPaint& paint);
void internalSaveLayer(const SkRect* bounds, const SkPaint*, SaveFlags, SaveLayerStrategy); void internalSaveLayer(const SkRect* bounds, const SkPaint*, SaveFlags, SaveLayerStrategy);
void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*); void internalDrawDevice(SkBaseDevice*, int x, int y, const SkPaint*, bool isBitmapDevice);
// shared by save() and saveLayer() // shared by save() and saveLayer()
void internalSave(); void internalSave();

View File

@ -348,6 +348,13 @@ protected:
/** /**
* Create a new device based on CreateInfo. If the paint is not null, then it represents a * Create a new device based on CreateInfo. If the paint is not null, then it represents a
* preview of how the new device will be composed with its creator device (this). * preview of how the new device will be composed with its creator device (this).
*
* The subclass may be handed this device in drawDevice(), so it must always return
* a device that it knows how to draw, and that it knows how to identify if it is not of the
* same subclass (since drawDevice is passed a SkBaseDevice*). If the subclass cannot fulfill
* that contract (e.g. PDF cannot support some settings on the paint) it should return NULL,
* and the caller may then decide to explicitly create a bitmapdevice, knowing that later
* it could not call drawDevice with it (but it could call drawSprite or drawBitmap).
*/ */
virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) { virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) {
return NULL; return NULL;

View File

@ -111,13 +111,16 @@ struct DeviceCM {
DeviceCM* fNext; DeviceCM* fNext;
SkBaseDevice* fDevice; SkBaseDevice* fDevice;
SkRasterClip fClip; SkRasterClip fClip;
const SkMatrix* fMatrix;
SkPaint* fPaint; // may be null (in the future) SkPaint* fPaint; // may be null (in the future)
const SkMatrix* fMatrix;
SkMatrix fMatrixStorage;
const bool fDeviceIsBitmapDevice;
DeviceCM(SkBaseDevice* device, const SkPaint* paint, SkCanvas* canvas, DeviceCM(SkBaseDevice* device, const SkPaint* paint, SkCanvas* canvas,
bool conservativeRasterClip) bool conservativeRasterClip, bool deviceIsBitmapDevice)
: fNext(NULL) : fNext(NULL)
, fClip(conservativeRasterClip) , fClip(conservativeRasterClip)
, fDeviceIsBitmapDevice(deviceIsBitmapDevice)
{ {
if (NULL != device) { if (NULL != device) {
device->ref(); device->ref();
@ -180,9 +183,6 @@ struct DeviceCM {
} }
#endif #endif
} }
private:
SkMatrix fMatrixStorage;
}; };
/* This is the record we keep for each save/restore level in the stack. /* This is the record we keep for each save/restore level in the stack.
@ -486,7 +486,7 @@ SkBaseDevice* SkCanvas::init(SkBaseDevice* device, InitFlags flags) {
SkASSERT(sizeof(DeviceCM) <= sizeof(fBaseLayerStorage)); SkASSERT(sizeof(DeviceCM) <= sizeof(fBaseLayerStorage));
fMCRec->fLayer = (DeviceCM*)fBaseLayerStorage; fMCRec->fLayer = (DeviceCM*)fBaseLayerStorage;
new (fBaseLayerStorage) DeviceCM(NULL, NULL, NULL, fConservativeRasterClip); new (fBaseLayerStorage) DeviceCM(NULL, NULL, NULL, fConservativeRasterClip, false);
fMCRec->fTopLayer = fMCRec->fLayer; fMCRec->fTopLayer = fMCRec->fLayer;
@ -985,16 +985,27 @@ void SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint, Sav
return; return;
} }
SkBaseDevice::TileUsage usage = SkBaseDevice::kNever_TileUsage; bool forceSpriteOnRestore = false;
device = device->onCreateDevice(SkBaseDevice::CreateInfo(info, usage, geo), paint); {
if (NULL == device) { const SkBaseDevice::TileUsage usage = SkBaseDevice::kNever_TileUsage;
SkErrorInternals::SetError( kInternalError_SkError, const SkBaseDevice::CreateInfo createInfo = SkBaseDevice::CreateInfo(info, usage, geo);
"Unable to create device for layer."); SkBaseDevice* newDev = device->onCreateDevice(createInfo, paint);
return; if (NULL == newDev) {
// If onCreateDevice didn't succeed, try raster (e.g. PDF couldn't handle the paint)
newDev = SkBitmapDevice::Create(createInfo.fInfo);
if (NULL == newDev) {
SkErrorInternals::SetError(kInternalError_SkError,
"Unable to create device for layer.");
return;
}
forceSpriteOnRestore = true;
}
device = newDev;
} }
device->setOrigin(ir.fLeft, ir.fTop); device->setOrigin(ir.fLeft, ir.fTop);
DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, paint, this, fConservativeRasterClip)); DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, paint, this, fConservativeRasterClip,
forceSpriteOnRestore));
device->unref(); device->unref();
layer->fNext = fMCRec->fTopLayer; layer->fNext = fMCRec->fTopLayer;
@ -1043,7 +1054,7 @@ void SkCanvas::internalRestore() {
if (layer->fNext) { if (layer->fNext) {
const SkIPoint& origin = layer->fDevice->getOrigin(); const SkIPoint& origin = layer->fDevice->getOrigin();
this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(),
layer->fPaint); layer->fPaint, layer->fDeviceIsBitmapDevice);
// reset this, since internalDrawDevice will have set it to true // reset this, since internalDrawDevice will have set it to true
fDeviceCMDirty = true; fDeviceCMDirty = true;
SkDELETE(layer); SkDELETE(layer);
@ -1155,7 +1166,7 @@ void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap,
} }
void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y,
const SkPaint* paint) { const SkPaint* paint, bool deviceIsBitmapDevice) {
SkPaint tmp; SkPaint tmp;
if (NULL == paint) { if (NULL == paint) {
paint = &tmp; paint = &tmp;
@ -1183,6 +1194,9 @@ void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y,
dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(),
tmpUnfiltered); tmpUnfiltered);
} }
} else if (deviceIsBitmapDevice) {
const SkBitmap& src = srcDev->accessBitmap(false);
dstDev->drawSprite(iter, src, pos.x(), pos.y(), *paint);
} else { } else {
dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint); dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint);
} }

View File

@ -8,6 +8,7 @@
#ifndef SkDeviceImageFilterProxy_DEFINED #ifndef SkDeviceImageFilterProxy_DEFINED
#define SkDeviceImageFilterProxy_DEFINED #define SkDeviceImageFilterProxy_DEFINED
#include "SkBitmapDevice.h"
#include "SkDevice.h" #include "SkDevice.h"
#include "SkImageFilter.h" #include "SkImageFilter.h"
#include "SkSurfaceProps.h" #include "SkSurfaceProps.h"
@ -24,7 +25,11 @@ public:
SkBaseDevice::kNever_TileUsage, SkBaseDevice::kNever_TileUsage,
kUnknown_SkPixelGeometry, kUnknown_SkPixelGeometry,
true /*forImageFilter*/); true /*forImageFilter*/);
return fDevice->onCreateDevice(cinfo, NULL); SkBaseDevice* dev = fDevice->onCreateDevice(cinfo, NULL);
if (NULL == dev) {
dev = SkBitmapDevice::Create(cinfo.fInfo);
}
return dev;
} }
bool canHandleImageFilter(const SkImageFilter* filter) override { bool canHandleImageFilter(const SkImageFilter* filter) override {
return fDevice->canHandleImageFilter(filter); return fDevice->canHandleImageFilter(filter);

View File

@ -1959,7 +1959,7 @@ SkBaseDevice* SkGpuDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint
texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags); texture->asRenderTarget(), cinfo.fInfo.width(), cinfo.fInfo.height(), &props, flags);
} else { } else {
SkErrorInternals::SetError( kInternalError_SkError, SkErrorInternals::SetError( kInternalError_SkError,
"---- failed to create compatible device texture [%d %d]\n", "---- failed to create gpu device texture [%d %d]\n",
cinfo.fInfo.width(), cinfo.fInfo.height()); cinfo.fInfo.width(), cinfo.fInfo.height());
return NULL; return NULL;
} }

View File

@ -8,7 +8,6 @@
#include "SkPDFDevice.h" #include "SkPDFDevice.h"
#include "SkAnnotation.h" #include "SkAnnotation.h"
#include "SkBitmapDevice.h"
#include "SkColor.h" #include "SkColor.h"
#include "SkClipStack.h" #include "SkClipStack.h"
#include "SkData.h" #include "SkData.h"
@ -579,7 +578,7 @@ static bool not_supported_for_layers(const SkPaint& layerPaint) {
SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) { SkBaseDevice* SkPDFDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint* layerPaint) {
if (cinfo.fForImageFilter || if (cinfo.fForImageFilter ||
(layerPaint && not_supported_for_layers(*layerPaint))) { (layerPaint && not_supported_for_layers(*layerPaint))) {
return SkBitmapDevice::Create(cinfo.fInfo); return NULL;
} }
SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height()); SkISize size = SkISize::Make(cinfo.fInfo.width(), cinfo.fInfo.height());
return SkPDFDevice::Create(size, fRasterDpi, fCanon); return SkPDFDevice::Create(size, fRasterDpi, fCanon);