blob: 17cb9847b19699a5758ff11762b0c24c05efc4ab [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 "SkImageInfo.h"
17#include "SkRect.h"
18#include "SkScalar.h"
19#include "SkSize.h"
20#include "SkSurfaceProps.h"
21#include "SkTypes.h"
22
23class SkDraw;
senorblanco900c3672016-04-27 11:31:23 -070024class SkImageFilterCache;
bungemand3ebb482015-08-05 13:57:49 -070025class SkMatrix;
26class SkPaint;
27class SkPath;
28class SkPixelRef;
29class SkPixmap;
30class SkRRect;
31class SkSurface;
bungemand3ebb482015-08-05 13:57:49 -070032struct SkPoint;
robertphillips@google.com9241e332013-08-21 13:54:44 +000033
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000034///////////////////////////////////////////////////////////////////////////////
35class SK_API SkBitmapDevice : public SkBaseDevice {
36public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000037 /**
38 * Construct a new device with the specified bitmap as its backend. It is
39 * valid for the bitmap to have no pixels associated with it. In that case,
40 * any drawing to this device will have no effect.
robertphillips9a53fd72015-06-22 09:46:59 -070041 */
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000042 SkBitmapDevice(const SkBitmap& bitmap);
robertphillips9a53fd72015-06-22 09:46:59 -070043
44 /**
45 * Create a new device along with its requisite pixel memory using
46 * default SkSurfaceProps (i.e., kLegacyFontHost_InitType-style).
47 * Note: this entry point is slated for removal - no one should call it.
48 */
49 static SkBitmapDevice* Create(const SkImageInfo& info);
50
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000051 /**
52 * Construct a new device with the specified bitmap as its backend. It is
53 * valid for the bitmap to have no pixels associated with it. In that case,
54 * any drawing to this device will have no effect.
robertphillips9a53fd72015-06-22 09:46:59 -070055 */
robertphillipsfcf78292015-06-19 11:49:52 -070056 SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps);
robertphillips9a53fd72015-06-22 09:46:59 -070057
58 static SkBitmapDevice* Create(const SkImageInfo&, const SkSurfaceProps&);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000059
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000060protected:
mtklein36352bf2015-03-25 18:17:31 -070061 bool onShouldDisableLCD(const SkPaint&) const override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000062
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000063 /** These are called inside the per-device-layer loop for each draw call.
64 When these are called, we have already applied any saveLayer operations,
65 and are handling any looping from the paint, and any effects from the
66 DrawFilter.
67 */
mtklein36352bf2015-03-25 18:17:31 -070068 void drawPaint(const SkDraw&, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000069 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
mtklein36352bf2015-03-25 18:17:31 -070070 const SkPoint[], const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000071 virtual void drawRect(const SkDraw&, const SkRect& r,
mtklein36352bf2015-03-25 18:17:31 -070072 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000073 virtual void drawOval(const SkDraw&, const SkRect& oval,
mtklein36352bf2015-03-25 18:17:31 -070074 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000075 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
mtklein36352bf2015-03-25 18:17:31 -070076 const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000077
78 /**
79 * If pathIsMutable, then the implementation is allowed to cast path to a
80 * non-const pointer and modify it in place (as an optimization). Canvas
81 * may do this to implement helpers such as drawOval, by placing a temp
82 * path on the stack to hold the representation of the oval.
83 *
84 * If prePathMatrix is not null, it should logically be applied before any
85 * stroking or other effects. If there are no effects on the paint that
86 * affect the geometry/rasterization, then the pre matrix can just be
87 * pre-concated with the current matrix.
88 */
89 virtual void drawPath(const SkDraw&, const SkPath& path,
90 const SkPaint& paint,
91 const SkMatrix* prePathMatrix = NULL,
mtklein36352bf2015-03-25 18:17:31 -070092 bool pathIsMutable = false) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000093 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
mtklein36352bf2015-03-25 18:17:31 -070094 const SkMatrix& matrix, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000095 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
mtklein36352bf2015-03-25 18:17:31 -070096 int x, int y, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000097
98 /**
99 * The default impl. will create a bitmap-shader from the bitmap,
100 * and call drawRect with it.
101 */
reeda5517e22015-07-14 10:54:12 -0700102 void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*, const SkRect&,
reed562fe472015-07-28 07:35:14 -0700103 const SkPaint&, SkCanvas::SrcRectConstraint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000104
105 /**
106 * Does not handle text decoration.
107 * Decorations (underline and stike-thru) will be handled by SkCanvas.
108 */
109 virtual void drawText(const SkDraw&, const void* text, size_t len,
mtklein36352bf2015-03-25 18:17:31 -0700110 SkScalar x, SkScalar y, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000111 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
fmalita05c4a432014-09-29 06:29:53 -0700112 const SkScalar pos[], int scalarsPerPos,
mtklein36352bf2015-03-25 18:17:31 -0700113 const SkPoint& offset, const SkPaint& paint) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000114 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
115 const SkPoint verts[], const SkPoint texs[],
Mike Reedfaba3712016-11-03 14:45:31 -0400116 const SkColor colors[], SkBlendMode,
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000117 const uint16_t indices[], int indexCount,
mtklein36352bf2015-03-25 18:17:31 -0700118 const SkPaint& paint) override;
119 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, const SkPaint&) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000120
121 ///////////////////////////////////////////////////////////////////////////
reede51c3562016-07-19 14:33:20 -0700122
123 void drawSpecial(const SkDraw&, SkSpecialImage*, int x, int y, const SkPaint&) override;
124 sk_sp<SkSpecialImage> makeSpecial(const SkBitmap&) override;
125 sk_sp<SkSpecialImage> makeSpecial(const SkImage*) override;
126 sk_sp<SkSpecialImage> snapSpecial() override;
127
128 ///////////////////////////////////////////////////////////////////////////
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000129
130 /** Update as needed the pixel value in the bitmap, so that the caller can
131 access the pixels directly. Note: only the pixels field should be
132 altered. The config/width/height/rowbytes must remain unchanged.
133 @return the device contents as a bitmap
134 */
robertphillips1f3923e2016-07-21 07:17:54 -0700135#ifdef SK_SUPPORT_LEGACY_ACCESSBITMAP
mtklein36352bf2015-03-25 18:17:31 -0700136 const SkBitmap& onAccessBitmap() override;
robertphillips1f3923e2016-07-21 07:17:54 -0700137#else
138 const SkBitmap& onAccessBitmap();
139#endif
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000140
141 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
142 // just for subclasses, to assign a custom pixelref
reed@google.com672588b2014-01-08 15:42:01 +0000143 SkPixelRef* setPixelRef(SkPixelRef* pr) {
144 fBitmap.setPixelRef(pr);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000145 return pr;
146 }
147
mtklein36352bf2015-03-25 18:17:31 -0700148 bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) override;
149 bool onWritePixels(const SkImageInfo&, const void*, size_t, int, int) override;
reed884e97c2015-05-26 11:31:54 -0700150 bool onPeekPixels(SkPixmap*) override;
151 bool onAccessPixels(SkPixmap*) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000152
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000153private:
154 friend class SkCanvas;
155 friend struct DeviceCM; //for setMatrixClip
156 friend class SkDraw;
157 friend class SkDrawIter;
158 friend class SkDeviceFilteredPaint;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000159
160 friend class SkSurface_Raster;
161
162 // used to change the backend's pixels (and possibly config/rowbytes)
163 // but cannot change the width/height, so there should be no change to
164 // any clip information.
mtklein36352bf2015-03-25 18:17:31 -0700165 void replaceBitmapBackendForRasterSurface(const SkBitmap&) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000166
mtklein36352bf2015-03-25 18:17:31 -0700167 SkBaseDevice* onCreateDevice(const CreateInfo&, const SkPaint*) override;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000168
reede8f30622016-03-23 18:59:25 -0700169 sk_sp<SkSurface> makeSurface(const SkImageInfo&, const SkSurfaceProps&) override;
reed@google.com76f10a32014-02-05 15:32:21 +0000170
senorblanco900c3672016-04-27 11:31:23 -0700171 SkImageFilterCache* getImageFilterCache() override;
senorblanco55b6d8b2014-07-30 11:26:46 -0700172
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000173 SkBitmap fBitmap;
174
mtkleinfeaadee2015-04-08 11:25:48 -0700175 void setNewSize(const SkISize&); // Used by SkCanvas for resetForNextPicture().
176
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000177 typedef SkBaseDevice INHERITED;
178};
robertphillips@google.com9241e332013-08-21 13:54:44 +0000179
180#endif // SkBitmapDevice_DEFINED