blob: d1cb9ad0f8665fd2152ad419621d33df483d2995 [file] [log] [blame]
robertphillips@google.com9241e332013-08-21 13:54:44 +00001
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkBitmapDevice_DEFINED
10#define SkBitmapDevice_DEFINED
11
bungemand3ebb482015-08-05 13:57:49 -070012#include "SkBitmap.h"
13#include "SkCanvas.h"
14#include "SkColor.h"
robertphillips@google.com9241e332013-08-21 13:54:44 +000015#include "SkDevice.h"
bungemand3ebb482015-08-05 13:57:49 -070016#include "SkImageFilter.h"
17#include "SkImageInfo.h"
18#include "SkRect.h"
19#include "SkScalar.h"
20#include "SkSize.h"
21#include "SkSurfaceProps.h"
22#include "SkTypes.h"
23
24class SkDraw;
25class SkMatrix;
26class SkPaint;
27class SkPath;
28class SkPixelRef;
29class SkPixmap;
30class SkRRect;
31class SkSurface;
32class SkXfermode;
33struct SkPoint;
robertphillips@google.com9241e332013-08-21 13:54:44 +000034
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000035///////////////////////////////////////////////////////////////////////////////
36class SK_API SkBitmapDevice : public SkBaseDevice {
37public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000038 /**
39 * Construct a new device with the specified bitmap as its backend. It is
40 * valid for the bitmap to have no pixels associated with it. In that case,
41 * any drawing to this device will have no effect.
robertphillips9a53fd72015-06-22 09:46:59 -070042 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000043 SkBitmapDevice(const SkBitmap& bitmap);
robertphillips9a53fd72015-06-22 09:46:59 -070044
45 /**
46 * Create a new device along with its requisite pixel memory using
47 * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
48 * Note: this entry point is slated for removal - no one should call it.
49 */
50 static SkBitmapDevice* Create(const SkImageInfo& info);
51
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000052 /**
53 * Construct a new device with the specified bitmap as its backend. It is
54 * valid for the bitmap to have no pixels associated with it. In that case,
55 * any drawing to this device will have no effect.
robertphillips9a53fd72015-06-22 09:46:59 -070056 */
robertphillipsfcf78292015-06-19 11:49:52 -070057 SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps);
robertphillips9a53fd72015-06-22 09:46:59 -070058
59 static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 SkImageInfo imageInfo() const override;
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000062
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000063protected:
mtklein36352bf2015-03-25 18:17:31 -070064 bool onShouldDisableLCD(const SkPaint&) const override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000065
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000066 /** These are called inside the per-device-layer loop for each draw call.
67 When these are called, we have already applied any saveLayer operations,
68 and are handling any looping from the paint, and any effects from the
69 DrawFilter.
70 */
mtklein36352bf2015-03-25 18:17:31 -070071 void drawPaint(const SkDraw&, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000072 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
mtklein36352bf2015-03-25 18:17:31 -070073 const SkPoint[], const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000074 virtual void drawRect(const SkDraw&, const SkRect& r,
mtklein36352bf2015-03-25 18:17:31 -070075 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000076 virtual void drawOval(const SkDraw&, const SkRect& oval,
mtklein36352bf2015-03-25 18:17:31 -070077 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000078 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
mtklein36352bf2015-03-25 18:17:31 -070079 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000080
81 /**
82 * If pathIsMutable, then the implementation is allowed to cast path to a
83 * non-const pointer and modify it in place (as an optimization). Canvas
84 * may do this to implement helpers such as drawOval, by placing a temp
85 * path on the stack to hold the representation of the oval.
86 *
87 * If prePathMatrix is not null, it should logically be applied before any
88 * stroking or other effects. If there are no effects on the paint that
89 * affect the geometry/rasterization, then the pre matrix can just be
90 * pre-concated with the current matrix.
91 */
92 virtual void drawPath(const SkDraw&, const SkPath& path,
93 const SkPaint& paint,
94 const SkMatrix* prePathMatrix = NULL,
mtklein36352bf2015-03-25 18:17:31 -070095 bool pathIsMutable = false) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000096 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
mtklein36352bf2015-03-25 18:17:31 -070097 const SkMatrix& matrix, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000098 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
mtklein36352bf2015-03-25 18:17:31 -070099 int x, int y, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000100
101 /**
102 * The default impl. will create a bitmap-shader from the bitmap,
103 * and call drawRect with it.
104 */
reeda5517e22015-07-14 10:54:12 -0700105 void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*, const SkRect&,
reed562fe472015-07-28 07:35:14 -0700106 const SkPaint&, SkCanvas::SrcRectConstraint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000107
108 /**
109 * Does not handle text decoration.
110 * Decorations (underline and stike-thru) will be handled by SkCanvas.
111 */
112 virtual void drawText(const SkDraw&, const void* text, size_t len,
mtklein36352bf2015-03-25 18:17:31 -0700113 SkScalar x, SkScalar y, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000114 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -0700115 const SkScalar pos[], int scalarsPerPos,
mtklein36352bf2015-03-25 18:17:31 -0700116 const SkPoint& offset, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000117 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
118 const SkPoint verts[], const SkPoint texs[],
119 const SkColor colors[], SkXfermode* xmode,
120 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700121 const SkPaint& paint) override;
122 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, const SkPaint&) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000123
124 ///////////////////////////////////////////////////////////////////////////
125
126 /** Update as needed the pixel value in the bitmap, so that the caller can
127 access the pixels directly. Note: only the pixels field should be
128 altered. The config/width/height/rowbytes must remain unchanged.
129 @return the device contents as a bitmap
130 */
mtklein36352bf2015-03-25 18:17:31 -0700131 const SkBitmap& onAccessBitmap() override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000132
133 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
134 // just for subclasses, to assign a custom pixelref
reed@google.com672588b2014-01-08 15:42:01 +0000135 SkPixelRef* setPixelRef(SkPixelRef* pr) {
136 fBitmap.setPixelRef(pr);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000137 return pr;
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) override;
141 bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
reed884e97c2015-05-26 11:31:54 -0700142 bool onPeekPixels(SkPixmap*) override;
143 bool onAccessPixels(SkPixmap*) override;
reedcd1d41e2015-05-25 21:21:27 -0700144 void onAttachToCanvas(SkCanvas*) override;
145 void onDetachFromCanvas() override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000146
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000147private:
148 friend class SkCanvas;
149 friend struct DeviceCM; //for setMatrixClip
150 friend class SkDraw;
151 friend class SkDrawIter;
152 friend class SkDeviceFilteredPaint;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000153
154 friend class SkSurface_Raster;
155
156 // used to change the backend's pixels (and possibly config/rowbytes)
157 // but cannot change the width/height, so there should be no change to
158 // any clip information.
mtklein36352bf2015-03-25 18:17:31 -0700159 void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000160
mtklein36352bf2015-03-25 18:17:31 -0700161 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000162
mtklein36352bf2015-03-25 18:17:31 -0700163 SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000164
mtklein36352bf2015-03-25 18:17:31 -0700165 SkImageFilter::Cache* getImageFilterCache() override;
senorblanco55b6d8b2014-07-30 11:26:46 -0700166
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000167 SkBitmap fBitmap;
168
mtkleinfeaadee2015-04-08 11:25:48 -0700169 void setNewSize(const SkISize&); // Used by SkCanvas for resetForNextPicture().
170
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000171 typedef SkBaseDevice INHERITED;
172};
robertphillips@google.com9241e332013-08-21 13:54:44 +0000173
174#endif // SkBitmapDevice_DEFINED