Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SkDevice.h
1 /*
2  * Copyright 2010 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkDevice_DEFINED
9 #define SkDevice_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "SkCanvas.h"
13 #include "SkColor.h"
14 #include "SkSurfaceProps.h"
15 
16 class SkBitmap;
17 class SkClipStack;
18 class SkDraw;
19 class SkDrawFilter;
20 class SkImageFilterCache;
21 struct SkIRect;
22 class SkMatrix;
23 class SkMetaData;
24 class SkRegion;
25 class SkSpecialImage;
26 class GrRenderTarget;
27 
28 class SK_API SkBaseDevice : public SkRefCnt {
29 public:
33  explicit SkBaseDevice(const SkImageInfo&, const SkSurfaceProps&);
34  virtual ~SkBaseDevice();
35 
36  SkMetaData& getMetaData();
37 
42  const SkImageInfo& imageInfo() const { return fInfo; }
43 
47  const SkSurfaceProps& surfaceProps() const {
48  return fSurfaceProps;
49  }
50 
56  void getGlobalBounds(SkIRect* bounds) const {
57  SkASSERT(bounds);
58  const SkIPoint& origin = this->getOrigin();
59  bounds->setXYWH(origin.x(), origin.y(), this->width(), this->height());
60  }
61 
62  SkIRect getGlobalBounds() const {
63  SkIRect bounds;
64  this->getGlobalBounds(&bounds);
65  return bounds;
66  }
67 
68  int width() const {
69  return this->imageInfo().width();
70  }
71 
72  int height() const {
73  return this->imageInfo().height();
74  }
75 
76  bool isOpaque() const {
77  return this->imageInfo().isOpaque();
78  }
79 
80 #ifdef SK_SUPPORT_LEGACY_ACCESSBITMAP
81 
87  const SkBitmap& accessBitmap(bool changePixels);
88 #endif
89 
90  bool writePixels(const SkImageInfo&, const void*, size_t rowBytes, int x, int y);
91 
99  bool accessPixels(SkPixmap* pmap);
100 
107  bool peekPixels(SkPixmap*);
108 
113  const SkIPoint& getOrigin() const { return fOrigin; }
114 
115 protected:
116  enum TileUsage {
119  };
120 
121  struct TextFlags {
122  uint32_t fFlags; // SkPaint::getFlags()
123  };
124 
129  uint32_t filterTextFlags(const SkPaint&) const;
130 
131  virtual bool onShouldDisableLCD(const SkPaint&) const { return false; }
132 
149  virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
150  const SkClipStack&) {};
151 
157  virtual void drawPaint(const SkDraw&, const SkPaint& paint) = 0;
158  virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
159  const SkPoint[], const SkPaint& paint) = 0;
160  virtual void drawRect(const SkDraw&, const SkRect& r,
161  const SkPaint& paint) = 0;
162  virtual void drawOval(const SkDraw&, const SkRect& oval,
163  const SkPaint& paint) = 0;
165  virtual void drawArc(const SkDraw&, const SkRect& oval, SkScalar startAngle,
166  SkScalar sweepAngle, bool useCenter, const SkPaint& paint);
167  virtual void drawRRect(const SkDraw&, const SkRRect& rr,
168  const SkPaint& paint) = 0;
169 
170  // Default impl calls drawPath()
171  virtual void drawDRRect(const SkDraw&, const SkRRect& outer,
172  const SkRRect& inner, const SkPaint&);
173 
185  virtual void drawPath(const SkDraw&, const SkPath& path,
186  const SkPaint& paint,
187  const SkMatrix* prePathMatrix = NULL,
188  bool pathIsMutable = false) = 0;
189  virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
190  const SkMatrix& matrix, const SkPaint& paint) = 0;
191  virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
192  int x, int y, const SkPaint& paint) = 0;
193 
198  virtual void drawBitmapRect(const SkDraw&, const SkBitmap&,
199  const SkRect* srcOrNull, const SkRect& dst,
200  const SkPaint& paint,
202  virtual void drawBitmapNine(const SkDraw&, const SkBitmap&, const SkIRect& center,
203  const SkRect& dst, const SkPaint&);
204  virtual void drawBitmapLattice(const SkDraw&, const SkBitmap&, const SkCanvas::Lattice&,
205  const SkRect& dst, const SkPaint&);
206 
207  virtual void drawImage(const SkDraw&, const SkImage*, SkScalar x, SkScalar y, const SkPaint&);
208  virtual void drawImageRect(const SkDraw&, const SkImage*, const SkRect* src, const SkRect& dst,
210  virtual void drawImageNine(const SkDraw&, const SkImage*, const SkIRect& center,
211  const SkRect& dst, const SkPaint&);
212  virtual void drawImageLattice(const SkDraw&, const SkImage*, const SkCanvas::Lattice&,
213  const SkRect& dst, const SkPaint&);
214 
219  virtual void drawText(const SkDraw&, const void* text, size_t len,
220  SkScalar x, SkScalar y, const SkPaint& paint) = 0;
221  virtual void drawPosText(const SkDraw&, const void* text, size_t len,
222  const SkScalar pos[], int scalarsPerPos,
223  const SkPoint& offset, const SkPaint& paint) = 0;
224  virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
225  const SkPoint verts[], const SkPoint texs[],
226  const SkColor colors[], SkXfermode* xmode,
227  const uint16_t indices[], int indexCount,
228  const SkPaint& paint) = 0;
229  // default implementation unrolls the blob runs.
230  virtual void drawTextBlob(const SkDraw&, const SkTextBlob*, SkScalar x, SkScalar y,
231  const SkPaint& paint, SkDrawFilter* drawFilter);
232  // default implementation calls drawVertices
233  virtual void drawPatch(const SkDraw&, const SkPoint cubics[12], const SkColor colors[4],
234  const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint);
235 
236  // default implementation calls drawPath
237  virtual void drawAtlas(const SkDraw&, const SkImage* atlas, const SkRSXform[], const SkRect[],
238  const SkColor[], int count, SkXfermode::Mode, const SkPaint&);
239 
240  virtual void drawAnnotation(const SkDraw&, const SkRect&, const char[], SkData*) {}
241 
245  virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
246  const SkPaint&) = 0;
247 
248  virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath&,
249  const SkMatrix*, const SkPaint&);
250  virtual void drawTextRSXform(const SkDraw&, const void* text, size_t len, const SkRSXform[],
251  const SkPaint&);
252 
253  virtual void drawSpecial(const SkDraw&, SkSpecialImage*, int x, int y, const SkPaint&);
254  virtual sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&);
255  virtual sk_sp<SkSpecialImage> makeSpecial(const SkImage*);
256  virtual sk_sp<SkSpecialImage> snapSpecial();
257 
258  bool readPixels(const SkImageInfo&, void* dst, size_t rowBytes, int x, int y);
259 
261 
262 #ifdef SK_SUPPORT_LEGACY_ACCESSBITMAP
263 
267  virtual const SkBitmap& onAccessBitmap() {
268  SkASSERT(0);
269  return fLegacyBitmap;
270  }
271 #endif
272 
273  virtual GrContext* context() const { return nullptr; }
274 
275 protected:
276  virtual sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&);
277  virtual bool onPeekPixels(SkPixmap*) { return false; }
278 
285  virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y);
286 
293  virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y);
294 
295  virtual bool onAccessPixels(SkPixmap*) { return false; }
296 
297  struct CreateInfo {
298  static SkPixelGeometry AdjustGeometry(const SkImageInfo&, TileUsage, SkPixelGeometry,
299  bool preserveLCDText);
300 
301  // The constructor may change the pixel geometry based on other parameters.
302  CreateInfo(const SkImageInfo& info,
303  TileUsage tileUsage,
304  SkPixelGeometry geo)
305  : fInfo(info)
306  , fTileUsage(tileUsage)
307  , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, false))
308  {}
309 
310  CreateInfo(const SkImageInfo& info,
311  TileUsage tileUsage,
312  SkPixelGeometry geo,
313  bool preserveLCDText)
314  : fInfo(info)
315  , fTileUsage(tileUsage)
316  , fPixelGeometry(AdjustGeometry(info, tileUsage, geo, preserveLCDText))
317  {}
318 
319  const SkImageInfo fInfo;
320  const TileUsage fTileUsage;
321  const SkPixelGeometry fPixelGeometry;
322  };
323 
335  virtual SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) {
336  return NULL;
337  }
338 
339  // A helper function used by derived classes to log the scale factor of a bitmap or image draw.
340  static void LogDrawScaleFactor(const SkMatrix&, SkFilterQuality);
341 
342 private:
343  friend class SkCanvas;
344  friend struct DeviceCM; //for setMatrixClip
345  friend class SkDraw;
346  friend class SkDrawIter;
347  friend class SkDeviceFilteredPaint;
348  friend class SkNoPixelsBitmapDevice;
349  friend class SkSurface_Raster;
350  friend class DeviceTestingAccess;
351 
352  // used to change the backend's pixels (and possibly config/rowbytes)
353  // but cannot change the width/height, so there should be no change to
354  // any clip information.
355  // TODO: move to SkBitmapDevice
356  virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) {}
357 
358  virtual bool forceConservativeRasterClip() const { return false; }
359 
363  virtual GrDrawContext* accessDrawContext() { return nullptr; }
364 
365  // just called by SkCanvas when built as a layer
366  void setOrigin(int x, int y) { fOrigin.set(x, y); }
367 
370  virtual void flush() {}
371 
372  virtual SkImageFilterCache* getImageFilterCache() { return NULL; }
373 
374  friend class SkBitmapDevice;
375  void privateResize(int w, int h) {
376  *const_cast<SkImageInfo*>(&fInfo) = fInfo.makeWH(w, h);
377  }
378 
379  SkIPoint fOrigin;
380  SkMetaData* fMetaData;
381  const SkImageInfo fInfo;
382  const SkSurfaceProps fSurfaceProps;
383 
384 #ifdef SK_SUPPORT_LEGACY_ACCESSBITMAP
385  SkBitmap fLegacyBitmap;
386 #endif
387 
388  typedef SkRefCnt INHERITED;
389 };
390 
391 #endif
the created device will never be drawn tiled
Definition: SkDevice.h:118
A Canvas encapsulates all of the state about drawing into a device (bitmap).
Definition: SkCanvas.h:58
The SkRRect class represents a rounded rect with a potentially different radii for each corner...
Definition: SkRRect.h:48
The SkPath class encapsulates compound (multiple contour) geometric paths consisting of straight line...
Definition: SkPath.h:27
Describe an image's dimensions and pixel type.
Definition: SkImageInfo.h:181
SkTextBlob combines multiple text runs into an immutable, ref-counted structure.
Definition: SkTextBlob.h:22
Mode
List of predefined xfermodes.
Definition: SkXfermode.h:71
const SkIPoint & getOrigin() const
Return the device's origin: its offset in device coordinates from the default origin in its canvas' m...
Definition: SkDevice.h:113
PointMode
Definition: SkCanvas.h:620
Describes properties and constraints of a given SkSurface.
Definition: SkSurfaceProps.h:51
Pairs SkImageInfo with actual pixels and rowbytes.
Definition: SkPixmap.h:23
Definition: SkPoint.h:156
Definition: SkDevice.h:297
The SkMatrix class holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:26
Definition: SkRefCnt.h:135
The SkPaint class holds the style and color information about how to draw geometries, text and bitmaps.
Definition: SkPaint.h:46
virtual SkBaseDevice * onCreateDevice(const CreateInfo &, const SkPaint *)
Create a new device based on CreateInfo.
Definition: SkDevice.h:335
Definition: SkMetaData.h:17
SkXfermode is the base class for objects that are called to implement custom "transfer-modes" in the ...
Definition: SkXfermode.h:34
SkData holds an immutable data buffer.
Definition: SkData.h:22
A compressed form of a rotation+scale matrix.
Definition: SkRSXform.h:20
const SkImageInfo & imageInfo() const
Return ImageInfo for this device.
Definition: SkDevice.h:42
Definition: SkDevice.h:121
Specifies coordinates to divide a bitmap into (xCount*yCount) rects.
Definition: SkCanvas.h:926
The SkBitmap class specifies a raster bitmap.
Definition: SkBitmap.h:41
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:258
Definition: GrDrawContext.h:51
DEPRECATED - use SkPaintFilterCanvas instead.
Definition: SkDrawFilter.h:26
Definition: GrContext.h:48
virtual void flush()
Causes any deferred drawing to the device to be completed.
Definition: SkDevice.h:370
SrcRectConstraint
Controls the behavior at the edge of the src-rect, when specified in drawImageRect, trading off speed for exactness.
Definition: SkCanvas.h:796
The SkRegion class encapsulates the geometric region used to specify clipping areas for drawing...
Definition: SkRegion.h:30
the created device may be drawn tiled
Definition: SkDevice.h:117
uint32_t SkColor
32 bit ARGB color value, not premultiplied.
Definition: SkColor.h:28
Definition: SkDevice.h:28
Definition: SkDraw.h:30
const SkSurfaceProps & surfaceProps() const
Return SurfaceProps for this device.
Definition: SkDevice.h:47
int width() const
Returns the rectangle's width.
Definition: SkRect.h:72
Definition: SkRect.h:390
virtual GrDrawContext * accessDrawContext()
Don't call this!
Definition: SkDevice.h:363
GrRenderTarget represents a 2D buffer of pixels that can be rendered to.
Definition: GrRenderTarget.h:26
virtual void setMatrixClip(const SkMatrix &, const SkRegion &, const SkClipStack &)
DEPRECATED: This will be removed in a future change.
Definition: SkDevice.h:149
TileUsage
Definition: SkDevice.h:116
SkIRect holds four 32 bit integer coordinates for a rectangle.
Definition: SkRect.h:20
SkImageInfo makeWH(int newWidth, int newHeight) const
Return a new ImageInfo with the same colortype and alphatype as this info, but with the specified wid...
Definition: SkImageInfo.h:255
Definition: SkClipStack.h:26
SkImage is an abstraction for drawing a rectagle of pixels, though the particular type of image could...
Definition: SkImage.h:45
SkIPoint holds two 32 bit integer coordinates.
Definition: SkPoint.h:40
void getGlobalBounds(SkIRect *bounds) const
Return the bounds of the device in the coordinate space of the root canvas.
Definition: SkDevice.h:56
Types and macros for colors.
Definition: SkBitmapDevice.h:36