void SkCanvas::predrawNotify() {
if (fSurfaceBase) {
- fSurfaceBase->aboutToDraw(this);
+ fSurfaceBase->aboutToDraw();
}
}
return fCachedImage;
}
-void SkSurface_Base::aboutToDraw(SkCanvas* canvas) {
+void SkSurface_Base::aboutToDraw() {
this->dirtyGenerationID();
- if (canvas) {
- SkASSERT(canvas == fCachedCanvas);
- SkASSERT(canvas->getSurfaceBase() == this);
- canvas->setSurfaceBase(NULL);
+ if (NULL != fCachedCanvas) {
+ SkASSERT(fCachedCanvas->getSurfaceBase() == this || \
+ NULL == fCachedCanvas->getSurfaceBase());
+ fCachedCanvas->setSurfaceBase(NULL);
}
- if (fCachedImage) {
+ if (NULL != fCachedImage) {
// the surface may need to fork its backend, if its sharing it with
// the cached image. Note: we only call if there is an outstanding owner
// on the image (besides us).
if (fCachedImage->getRefCnt() > 1) {
- this->onCopyOnWrite(fCachedImage, canvas);
+ this->onCopyOnWrite();
}
// regardless of copy-on-write, we must drop our cached image now, so
}
void SkSurface::notifyContentChanged() {
- asSB(this)->aboutToDraw(NULL);
+ asSB(this)->aboutToDraw();
}
SkCanvas* SkSurface::getCanvas() {
*
* The default implementation does nothing.
*/
- virtual void onCopyOnWrite(SkImage* cachedImage, SkCanvas*) = 0;
+ virtual void onCopyOnWrite() = 0;
inline SkCanvas* getCachedCanvas();
inline SkImage* getCachedImage();
SkCanvas* fCachedCanvas;
SkImage* fCachedImage;
- void aboutToDraw(SkCanvas*);
+ void aboutToDraw();
friend class SkCanvas;
friend class SkSurface;
virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
const SkPaint*) SK_OVERRIDE;
- virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE;
+ virtual void onCopyOnWrite() SK_OVERRIDE;
private:
SkGpuDevice* fDevice;
// Create a new SkGpuDevice and, if necessary, copy the contents of the old
// device into it. Note that this flushes the SkGpuDevice but
// doesn't force an OpenGL flush.
-void SkSurface_Gpu::onCopyOnWrite(SkImage* image, SkCanvas* canvas) {
+void SkSurface_Gpu::onCopyOnWrite() {
GrRenderTarget* rt = (GrRenderTarget*) fDevice->accessRenderTarget();
// are we sharing our render target with the image?
- if (rt->asTexture() == SkTextureImageGetTexture(image)) {
+ SkASSERT(NULL != this->getCachedImage());
+ if (rt->asTexture() == SkTextureImageGetTexture(this->getCachedImage())) {
SkGpuDevice* newDevice = static_cast<SkGpuDevice*>(
fDevice->createCompatibleDevice(fDevice->config(), fDevice->width(),
fDevice->height(), fDevice->isOpaque()));
SkAutoTUnref<SkGpuDevice> aurd(newDevice);
fDevice->context()->copyTexture(rt->asTexture(),
(GrRenderTarget*)newDevice->accessRenderTarget());
- SkASSERT(NULL != canvas);
- SkASSERT(canvas->getDevice() == fDevice);
- canvas->setDevice(newDevice);
+ SkASSERT(NULL != this->getCachedCanvas());
+ SkASSERT(this->getCachedCanvas()->getDevice() == fDevice);
+ this->getCachedCanvas()->setDevice(newDevice);
SkRefCnt_SafeAssign(fDevice, newDevice);
}
}
virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
const SkPaint*) SK_OVERRIDE;
- virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE;
+ virtual void onCopyOnWrite() SK_OVERRIDE;
private:
SkPicture* fPicture;
SkImagePrivDrawPicture(canvas, fPicture, x, y, paint);
}
-void SkSurface_Picture::onCopyOnWrite(SkImage* cachedImage, SkCanvas*) {
+void SkSurface_Picture::onCopyOnWrite() {
// We always spawn a copy of the recording picture when we
// are asked for a snapshot, so we never need to do anything here.
}
virtual SkImage* onNewImageSnapshot() SK_OVERRIDE;
virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y,
const SkPaint*) SK_OVERRIDE;
- virtual void onCopyOnWrite(SkImage*, SkCanvas*) SK_OVERRIDE;
+ virtual void onCopyOnWrite() SK_OVERRIDE;
private:
SkBitmap fBitmap;
return SkNewImageFromBitmap(fBitmap, fWeOwnThePixels);
}
-void SkSurface_Raster::onCopyOnWrite(SkImage* image, SkCanvas* canvas) {
+void SkSurface_Raster::onCopyOnWrite() {
// are we sharing pixelrefs with the image?
- if (SkBitmapImageGetPixelRef(image) == fBitmap.pixelRef()) {
+ SkASSERT(NULL !=this->getCachedImage());
+ if (SkBitmapImageGetPixelRef(this->getCachedImage()) == fBitmap.pixelRef()) {
SkASSERT(fWeOwnThePixels);
SkBitmap prev(fBitmap);
prev.deepCopyTo(&fBitmap, prev.config());
// Now fBitmap is a deep copy of itself (and therefore different from
// what is being used by the image. Next we update the canvas to use
// this as its backend, so we can't modify the image's pixels anymore.
- canvas->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
+ SkASSERT(NULL != this->getCachedCanvas());
+ this->getCachedCanvas()->getDevice()->replaceBitmapBackendForRasterSurface(fBitmap);
}
}
surface->newImageSnapshot()->unref(); // Create and destroy SkImage
canvas->clear(2);
}
+static void TestSurfaceNoCanvas(skiatest::Reporter* reporter,
+ SurfaceType surfaceType,
+ GrContext* context) {
+ // Verifies the robustness of SkSurface for handling use cases where calls
+ // are made before a canvas is created.
+ {
+ // Test passes by not asserting
+ SkSurface* surface = createSurface(surfaceType, context);
+ SkAutoTUnref<SkSurface> aur_surface(surface);
+ surface->notifyContentChanged();
+ surface->validate();
+ }
+ {
+ SkSurface* surface = createSurface(surfaceType, context);
+ SkAutoTUnref<SkSurface> aur_surface(surface);
+ SkImage* image1 = surface->newImageSnapshot();
+ SkAutoTUnref<SkImage> aur_image1(image1);
+ image1->validate();
+ surface->validate();
+ surface->notifyContentChanged();
+ image1->validate();
+ surface->validate();
+ SkImage* image2 = surface->newImageSnapshot();
+ SkAutoTUnref<SkImage> aur_image2(image2);
+ image2->validate();
+ surface->validate();
+ REPORTER_ASSERT(reporter, image1 != image2);
+ }
+
+}
static void TestSurface(skiatest::Reporter* reporter, GrContextFactory* factory) {
TestSurfaceCopyOnWrite(reporter, kRaster_SurfaceType, NULL);
TestSurfaceCopyOnWrite(reporter, kPicture_SurfaceType, NULL);
TestSurfaceWritableAfterSnapshotRelease(reporter, kRaster_SurfaceType, NULL);
TestSurfaceWritableAfterSnapshotRelease(reporter, kPicture_SurfaceType, NULL);
+ TestSurfaceNoCanvas(reporter, kRaster_SurfaceType, NULL);
#if SK_SUPPORT_GPU
if (NULL != factory) {
GrContext* context = factory->get(GrContextFactory::kNative_GLContextType);
TestSurfaceCopyOnWrite(reporter, kGpu_SurfaceType, context);
TestSurfaceWritableAfterSnapshotRelease(reporter, kGpu_SurfaceType, context);
+ TestSurfaceNoCanvas(reporter, kGpu_SurfaceType, context);
}
#endif
}