2 * Copyright 2010 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkDevice_DEFINED
9 #define SkDevice_DEFINED
15 #include "SkImageFilter.h"
23 struct SkDeviceProperties;
26 class SK_API SkBaseDevice : public SkRefCnt {
28 SK_DECLARE_INST_COUNT(SkBaseDevice)
31 * Construct a new device.
34 virtual ~SkBaseDevice();
36 SkBaseDevice* createCompatibleDevice(const SkImageInfo&);
38 SkMetaData& getMetaData();
41 * Return ImageInfo for this device. If the canvas is not backed by pixels
42 * (cpu or gpu), then the info's ColorType will be kUnknown_SkColorType.
44 virtual SkImageInfo imageInfo() const;
47 * Return the bounds of the device in the coordinate space of the root
48 * canvas. The root device will have its top-left at 0,0, but other devices
49 * such as those associated with saveLayer may have a non-zero origin.
51 void getGlobalBounds(SkIRect* bounds) const {
53 const SkIPoint& origin = this->getOrigin();
54 bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height());
58 return this->imageInfo().width();
62 return this->imageInfo().height();
65 bool isOpaque() const {
66 return this->imageInfo().isOpaque();
69 /** Return the bitmap associated with this device. Call this each time you need
70 to access the bitmap, as it notifies the subclass to perform any flushing
71 etc. before you examine the pixels.
72 @param changePixels set to true if the caller plans to change the pixels
73 @return the device's bitmap
75 const SkBitmap& accessBitmap(bool changePixels);
77 bool writePixels(const SkImageInfo&, const void*, size_t rowBytes, int x, int y);
79 void* accessPixels(SkImageInfo* info, size_t* rowBytes);
82 * Return the device's associated gpu render target, or NULL.
84 virtual GrRenderTarget* accessRenderTarget() { return NULL; }
88 * Return the device's origin: its offset in device coordinates from
89 * the default origin in its canvas' matrix/clip
91 const SkIPoint& getOrigin() const { return fOrigin; }
94 * onAttachToCanvas is invoked whenever a device is installed in a canvas
95 * (i.e., setDevice, saveLayer (for the new device created by the save),
96 * and SkCanvas' SkBaseDevice & SkBitmap -taking ctors). It allows the
97 * devices to prepare for drawing (e.g., locking their pixels, etc.)
99 virtual void onAttachToCanvas(SkCanvas*) {
100 SkASSERT(!fAttachedToCanvas);
103 fAttachedToCanvas = true;
108 * onDetachFromCanvas notifies a device that it will no longer be drawn to.
109 * It gives the device a chance to clean up (e.g., unlock its pixels). It
110 * is invoked from setDevice (for the displaced device), restore and
111 * possibly from SkCanvas' dtor.
113 virtual void onDetachFromCanvas() {
114 SkASSERT(fAttachedToCanvas);
115 this->unlockPixels();
117 fAttachedToCanvas = false;
124 kSaveLayer_Usage // <! internal use only
128 uint32_t fFlags; // SkPaint::getFlags()
132 * Device may filter the text flags for drawing text here. If it wants to
133 * make a change to the specified values, it should write them into the
134 * textflags parameter (output) and return true. If the paint is fine as
135 * is, then ignore the textflags parameter and return false.
137 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) { return false; }
141 * DEPRECATED: This will be removed in a future change. Device subclasses
142 * should use the matrix and clip from the SkDraw passed to draw functions.
144 * Called with the correct matrix and clip before this device is drawn
145 * to using those settings. If your subclass overrides this, be sure to
146 * call through to the base class as well.
148 * The clipstack is another view of the clip. It records the actual
149 * geometry that went into building the region. It is present for devices
150 * that want to parse it, but is not required: the region is a complete
151 * picture of the current clip. (i.e. if you regionize all of the geometry
152 * in the clipstack, you will arrive at an equivalent region to the one
155 virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
156 const SkClipStack&) {};
158 /** Clears the entire device to the specified color (including alpha).
161 virtual void clear(SkColor color) = 0;
163 SK_ATTR_DEPRECATED("use clear() instead")
164 void eraseColor(SkColor eraseColor) { this->clear(eraseColor); }
166 /** These are called inside the per-device-layer loop for each draw call.
167 When these are called, we have already applied any saveLayer operations,
168 and are handling any looping from the paint, and any effects from the
171 virtual void drawPaint(const SkDraw&, const SkPaint& paint) = 0;
172 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
173 const SkPoint[], const SkPaint& paint) = 0;
174 virtual void drawRect(const SkDraw&, const SkRect& r,
175 const SkPaint& paint) = 0;
176 virtual void drawOval(const SkDraw&, const SkRect& oval,
177 const SkPaint& paint) = 0;
178 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
179 const SkPaint& paint) = 0;
181 // Default impl calls drawPath()
182 virtual void drawDRRect(const SkDraw&, const SkRRect& outer,
183 const SkRRect& inner, const SkPaint&);
186 * If pathIsMutable, then the implementation is allowed to cast path to a
187 * non-const pointer and modify it in place (as an optimization). Canvas
188 * may do this to implement helpers such as drawOval, by placing a temp
189 * path on the stack to hold the representation of the oval.
191 * If prePathMatrix is not null, it should logically be applied before any
192 * stroking or other effects. If there are no effects on the paint that
193 * affect the geometry/rasterization, then the pre matrix can just be
194 * pre-concated with the current matrix.
196 virtual void drawPath(const SkDraw&, const SkPath& path,
197 const SkPaint& paint,
198 const SkMatrix* prePathMatrix = NULL,
199 bool pathIsMutable = false) = 0;
200 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
201 const SkMatrix& matrix, const SkPaint& paint) = 0;
202 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
203 int x, int y, const SkPaint& paint) = 0;
206 * The default impl. will create a bitmap-shader from the bitmap,
207 * and call drawRect with it.
209 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
210 const SkRect* srcOrNull, const SkRect& dst,
211 const SkPaint& paint,
212 SkCanvas::DrawBitmapRectFlags flags) = 0;
215 * Does not handle text decoration.
216 * Decorations (underline and stike-thru) will be handled by SkCanvas.
218 virtual void drawText(const SkDraw&, const void* text, size_t len,
219 SkScalar x, SkScalar y, const SkPaint& paint) = 0;
220 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
221 const SkScalar pos[], SkScalar constY,
222 int scalarsPerPos, const SkPaint& paint) = 0;
223 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
224 const SkPath& path, const SkMatrix* matrix,
225 const SkPaint& paint) = 0;
226 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
227 const SkPoint verts[], const SkPoint texs[],
228 const SkColor colors[], SkXfermode* xmode,
229 const uint16_t indices[], int indexCount,
230 const SkPaint& paint) = 0;
231 // default implementation unrolls the blob runs.
232 virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
233 const SkPaint& paint);
234 // default implementation calls drawVertices
235 virtual void drawPatch(const SkDraw&, const SkPoint cubics[12], const SkColor colors[4],
236 const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
237 /** The SkDevice passed will be an SkDevice which was returned by a call to
238 onCreateDevice on this device with kSaveLayer_Usage.
240 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
243 bool readPixels(const SkImageInfo&, void* dst, size_t rowBytes, int x, int y);
245 ///////////////////////////////////////////////////////////////////////////
247 /** Update as needed the pixel value in the bitmap, so that the caller can
248 access the pixels directly.
249 @return The device contents as a bitmap
251 virtual const SkBitmap& onAccessBitmap() = 0;
253 /** Called when this device is installed into a Canvas. Balanced by a call
254 to unlockPixels() when the device is removed from a Canvas.
256 virtual void lockPixels() {}
257 virtual void unlockPixels() {}
260 * Returns true if the device allows processing of this imagefilter. If
261 * false is returned, then the filter is ignored. This may happen for
262 * some subclasses that do not support pixel manipulations after drawing
263 * has occurred (e.g. printing). The default implementation returns true.
265 virtual bool allowImageFilter(const SkImageFilter*) { return true; }
268 * Override and return true for filters that the device can handle
269 * intrinsically. Doing so means that SkCanvas will pass-through this
270 * filter to drawSprite and drawDevice (and potentially filterImage).
271 * Returning false means the SkCanvas will have apply the filter itself,
272 * and just pass the resulting image to the device.
274 virtual bool canHandleImageFilter(const SkImageFilter*) { return false; }
277 * Related (but not required) to canHandleImageFilter, this method returns
278 * true if the device could apply the filter to the src bitmap and return
279 * the result (and updates offset as needed).
280 * If the device does not recognize or support this filter,
281 * it just returns false and leaves result and offset unchanged.
283 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
284 const SkImageFilter::Context& ctx,
285 SkBitmap* result, SkIPoint* offset) {
290 // default impl returns NULL
291 virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&);
293 // default impl returns NULL
294 virtual const void* peekPixels(SkImageInfo*, size_t* rowBytes);
297 * The caller is responsible for "pre-clipping" the dst. The impl can assume that the dst
298 * image at the specified x,y offset will fit within the device's bounds.
300 * This is explicitly asserted in readPixels(), the public way to call this.
302 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y);
305 * The caller is responsible for "pre-clipping" the src. The impl can assume that the src
306 * image at the specified x,y offset will fit within the device's bounds.
308 * This is explicitly asserted in writePixelsDirect(), the public way to call this.
310 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y);
313 * Default impl returns NULL.
315 virtual void* onAccessPixels(SkImageInfo* info, size_t* rowBytes);
318 * Leaky properties are those which the device should be applying but it isn't.
319 * These properties will be applied by the draw, when and as it can.
320 * If the device does handle a property, that property should be set to the identity value
321 * for that property, effectively making it non-leaky.
323 const SkDeviceProperties& getLeakyProperties() const {
324 return *fLeakyProperties;
328 * PRIVATE / EXPERIMENTAL -- do not call
329 * Construct an acceleration object and attach it to 'picture'
331 virtual void EXPERIMENTAL_optimize(const SkPicture* picture);
334 * PRIVATE / EXPERIMENTAL -- do not call
335 * This entry point gives the backend an opportunity to take over the rendering
336 * of 'picture'. If optimization data is available (due to an earlier
337 * 'optimize' call) this entry point should make use of it and return true
338 * if all rendering has been done. If false is returned, SkCanvas will
339 * perform its own rendering pass. It is acceptable for the backend
340 * to perform some device-specific warm up tasks and then let SkCanvas
341 * perform the main rendering loop (by return false from here).
343 virtual bool EXPERIMENTAL_drawPicture(SkCanvas*, const SkPicture*, const SkMatrix*,
346 void setPixelGeometry(SkPixelGeometry geo);
349 friend class SkCanvas;
350 friend struct DeviceCM; //for setMatrixClip
352 friend class SkDrawIter;
353 friend class SkDeviceFilteredPaint;
354 friend class SkDeviceImageFilterProxy;
355 friend class SkDeferredDevice; // for newSurface
357 friend class SkSurface_Raster;
359 // used to change the backend's pixels (and possibly config/rowbytes)
360 // but cannot change the width/height, so there should be no change to
361 // any clip information.
362 // TODO: move to SkBitmapDevice
363 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) {}
365 virtual bool forceConservativeRasterClip() const { return false; }
367 // just called by SkCanvas when built as a layer
368 void setOrigin(int x, int y) { fOrigin.set(x, y); }
369 // just called by SkCanvas for saveLayer
370 SkBaseDevice* createCompatibleDeviceForSaveLayer(const SkImageInfo&);
372 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) {
376 /** Causes any deferred drawing to the device to be completed.
378 virtual void flush() {}
380 virtual SkImageFilter::Cache* getImageFilterCache() { return NULL; }
383 SkMetaData* fMetaData;
384 SkDeviceProperties* fLeakyProperties; // will always exist.
387 bool fAttachedToCanvas;
390 typedef SkRefCnt INHERITED;