1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/image/SkSurface_Base.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkSurface_Base_DEFINED 1.12 +#define SkSurface_Base_DEFINED 1.13 + 1.14 +#include "SkSurface.h" 1.15 +#include "SkCanvas.h" 1.16 + 1.17 +class SkSurface_Base : public SkSurface { 1.18 +public: 1.19 + SkSurface_Base(int width, int height); 1.20 + explicit SkSurface_Base(const SkImageInfo&); 1.21 + virtual ~SkSurface_Base(); 1.22 + 1.23 + /** 1.24 + * Allocate a canvas that will draw into this surface. We will cache this 1.25 + * canvas, to return the same object to the caller multiple times. We 1.26 + * take ownership, and will call unref() on the canvas when we go out of 1.27 + * scope. 1.28 + */ 1.29 + virtual SkCanvas* onNewCanvas() = 0; 1.30 + 1.31 + virtual SkSurface* onNewSurface(const SkImageInfo&) = 0; 1.32 + 1.33 + /** 1.34 + * Allocate an SkImage that represents the current contents of the surface. 1.35 + * This needs to be able to outlive the surface itself (if need be), and 1.36 + * must faithfully represent the current contents, even if the surface 1.37 + * is chaged after this calle (e.g. it is drawn to via its canvas). 1.38 + */ 1.39 + virtual SkImage* onNewImageSnapshot() = 0; 1.40 + 1.41 + /** 1.42 + * Default implementation: 1.43 + * 1.44 + * image = this->newImageSnapshot(); 1.45 + * if (image) { 1.46 + * image->draw(canvas, ...); 1.47 + * image->unref(); 1.48 + * } 1.49 + */ 1.50 + virtual void onDraw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*); 1.51 + 1.52 + /** 1.53 + * If the surface is about to change, we call this so that our subclass 1.54 + * can optionally fork their backend (copy-on-write) in case it was 1.55 + * being shared with the cachedImage. 1.56 + */ 1.57 + virtual void onCopyOnWrite(ContentChangeMode) = 0; 1.58 + 1.59 + inline SkCanvas* getCachedCanvas(); 1.60 + inline SkImage* getCachedImage(); 1.61 + 1.62 + // called by SkSurface to compute a new genID 1.63 + uint32_t newGenerationID(); 1.64 + 1.65 +private: 1.66 + SkCanvas* fCachedCanvas; 1.67 + SkImage* fCachedImage; 1.68 + 1.69 + void aboutToDraw(ContentChangeMode mode); 1.70 + friend class SkCanvas; 1.71 + friend class SkSurface; 1.72 + 1.73 + inline void installIntoCanvasForDirtyNotification(); 1.74 + 1.75 + typedef SkSurface INHERITED; 1.76 +}; 1.77 + 1.78 +SkCanvas* SkSurface_Base::getCachedCanvas() { 1.79 + if (NULL == fCachedCanvas) { 1.80 + fCachedCanvas = this->onNewCanvas(); 1.81 + this->installIntoCanvasForDirtyNotification(); 1.82 + } 1.83 + return fCachedCanvas; 1.84 +} 1.85 + 1.86 +SkImage* SkSurface_Base::getCachedImage() { 1.87 + if (NULL == fCachedImage) { 1.88 + fCachedImage = this->onNewImageSnapshot(); 1.89 + this->installIntoCanvasForDirtyNotification(); 1.90 + } 1.91 + return fCachedImage; 1.92 +} 1.93 + 1.94 +void SkSurface_Base::installIntoCanvasForDirtyNotification() { 1.95 + if (NULL != fCachedCanvas) { 1.96 + fCachedCanvas->setSurfaceBase(this); 1.97 + } 1.98 +} 1.99 + 1.100 +#endif