OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 SkDEBUGFAIL("this method should never be called"); | 40 SkDEBUGFAIL("this method should never be called"); |
41 } | 41 } |
42 | 42 |
43 static void nothing_to_do() {} | 43 static void nothing_to_do() {} |
44 | 44 |
45 /** | 45 /** |
46 * This device will route all bitmaps (primitives and in shaders) to its PRSet. | 46 * This device will route all bitmaps (primitives and in shaders) to its PRSet. |
47 * It should never actually draw anything, so there need not be any pixels | 47 * It should never actually draw anything, so there need not be any pixels |
48 * behind its device-bitmap. | 48 * behind its device-bitmap. |
49 */ | 49 */ |
50 class GatherPixelRefDevice : public SkBitmapDevice { | 50 class GatherPixelRefDevice : public SkBaseDevice { |
51 private: | 51 private: |
52 PixelRefSet* fPRSet; | 52 PixelRefSet* fPRSet; |
53 | 53 |
54 void addBitmap(const SkBitmap& bm) { | 54 void addBitmap(const SkBitmap& bm) { |
55 fPRSet->add(bm.pixelRef()); | 55 fPRSet->add(bm.pixelRef()); |
56 } | 56 } |
57 | 57 |
58 void addBitmapFromPaint(const SkPaint& paint) { | 58 void addBitmapFromPaint(const SkPaint& paint) { |
59 SkShader* shader = paint.getShader(); | 59 SkShader* shader = paint.getShader(); |
60 if (shader) { | 60 if (shader) { |
61 SkBitmap bm; | 61 SkBitmap bm; |
62 // Check whether the shader is a gradient in order to short-circuit | 62 // Check whether the shader is a gradient in order to short-circuit |
63 // call to asABitmap to prevent generation of bitmaps from | 63 // call to asABitmap to prevent generation of bitmaps from |
64 // gradient shaders, which implement asABitmap. | 64 // gradient shaders, which implement asABitmap. |
65 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && | 65 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && |
66 shader->asABitmap(&bm, NULL, NULL)) { | 66 shader->asABitmap(&bm, NULL, NULL)) { |
67 fPRSet->add(bm.pixelRef()); | 67 fPRSet->add(bm.pixelRef()); |
68 } | 68 } |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 public: | 72 public: |
73 GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkBitmapDevic e(bm) { | 73 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) { |
74 fSize.set(width, height); | |
75 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height); | |
74 fPRSet = prset; | 76 fPRSet = prset; |
75 } | 77 } |
76 | 78 |
79 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; } | |
80 virtual int width() const SK_OVERRIDE { return fSize.width(); } | |
81 virtual int height() const SK_OVERRIDE { return fSize.height(); } | |
82 virtual bool isOpaque() const SK_OVERRIDE { return false; } | |
83 virtual SkBitmap::Config config() const SK_OVERRIDE { | |
84 return SkBitmap::kNo_Config; | |
85 } | |
86 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } | |
87 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { | |
88 return true; | |
89 } | |
90 // TODO: allow this call to return failure, or move to SkBitmapDevice only. | |
robertphillips
2013/11/12 18:36:24
I would like it to be on SkBitmapDevice only (alth
| |
91 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { | |
92 return fEmptyBitmap; | |
93 } | |
94 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); } | |
95 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); } | |
96 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; } | |
97 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false ; } | |
98 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&, | |
99 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { | |
100 return false; | |
101 } | |
102 | |
77 virtual void clear(SkColor color) SK_OVERRIDE { | 103 virtual void clear(SkColor color) SK_OVERRIDE { |
78 nothing_to_do(); | 104 nothing_to_do(); |
79 } | 105 } |
80 virtual void writePixels(const SkBitmap& bitmap, int x, int y, | 106 virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
81 SkCanvas::Config8888 config8888) SK_OVERRIDE { | 107 SkCanvas::Config8888 config8888) SK_OVERRIDE { |
82 not_supported(); | 108 not_supported(); |
83 } | 109 } |
84 | 110 |
85 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { | 111 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { |
86 this->addBitmapFromPaint(paint); | 112 this->addBitmapFromPaint(paint); |
87 } | 113 } |
88 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t coun t, | 114 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t coun t, |
89 const SkPoint[], const SkPaint& paint) SK_OVERRIDE { | 115 const SkPoint[], const SkPaint& paint) SK_OVERRIDE { |
90 this->addBitmapFromPaint(paint); | 116 this->addBitmapFromPaint(paint); |
91 } | 117 } |
92 virtual void drawRect(const SkDraw&, const SkRect&, | 118 virtual void drawRect(const SkDraw&, const SkRect&, |
93 const SkPaint& paint) SK_OVERRIDE { | 119 const SkPaint& paint) SK_OVERRIDE { |
94 this->addBitmapFromPaint(paint); | 120 this->addBitmapFromPaint(paint); |
95 } | 121 } |
122 virtual void drawRRect(const SkDraw&, const SkRRect&, | |
123 const SkPaint& paint) SK_OVERRIDE { | |
124 this->addBitmapFromPaint(paint); | |
125 } | |
96 virtual void drawOval(const SkDraw&, const SkRect&, | 126 virtual void drawOval(const SkDraw&, const SkRect&, |
97 const SkPaint& paint) SK_OVERRIDE { | 127 const SkPaint& paint) SK_OVERRIDE { |
98 this->addBitmapFromPaint(paint); | 128 this->addBitmapFromPaint(paint); |
99 } | 129 } |
100 virtual void drawPath(const SkDraw&, const SkPath& path, | 130 virtual void drawPath(const SkDraw&, const SkPath& path, |
101 const SkPaint& paint, const SkMatrix* prePathMatrix, | 131 const SkPaint& paint, const SkMatrix* prePathMatrix, |
102 bool pathIsMutable) SK_OVERRIDE { | 132 bool pathIsMutable) SK_OVERRIDE { |
103 this->addBitmapFromPaint(paint); | 133 this->addBitmapFromPaint(paint); |
104 } | 134 } |
105 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, | 135 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 nothing_to_do(); | 173 nothing_to_do(); |
144 } | 174 } |
145 | 175 |
146 protected: | 176 protected: |
147 virtual bool onReadPixels(const SkBitmap& bitmap, | 177 virtual bool onReadPixels(const SkBitmap& bitmap, |
148 int x, int y, | 178 int x, int y, |
149 SkCanvas::Config8888 config8888) SK_OVERRIDE { | 179 SkCanvas::Config8888 config8888) SK_OVERRIDE { |
150 not_supported(); | 180 not_supported(); |
151 return false; | 181 return false; |
152 } | 182 } |
153 | 183 |
robertphillips
2013/11/12 18:36:24
Can this move to SkBitmapDevice?
| |
184 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRI DE { | |
185 not_supported(); | |
186 } | |
robertphillips
2013/11/12 18:36:24
I would think this could work - in theory.
| |
187 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config, | |
scroggo
2013/11/12 18:22:00
nit: newline between functions.
| |
188 int width, int height, | |
189 bool isOpaque, | |
190 Usage usage) SK_OVERRIDE { | |
191 return NULL; | |
192 } | |
193 virtual void flush() SK_OVERRIDE {} | |
scroggo
2013/11/12 18:22:00
nit: newline between functions.
| |
194 | |
154 private: | 195 private: |
155 typedef SkBitmapDevice INHERITED; | 196 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy |
197 SkISize fSize; | |
198 typedef SkBaseDevice INHERITED; | |
156 }; | 199 }; |
157 | 200 |
158 class NoSaveLayerCanvas : public SkCanvas { | 201 class NoSaveLayerCanvas : public SkCanvas { |
159 public: | 202 public: |
160 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {} | 203 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {} |
161 | 204 |
162 // turn saveLayer() into save() for speed, should not affect correctness. | 205 // turn saveLayer() into save() for speed, should not affect correctness. |
163 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, | 206 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
164 SaveFlags flags) SK_OVERRIDE { | 207 SaveFlags flags) SK_OVERRIDE { |
165 | 208 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 // this test also handles if either area or pict's width/height are empty | 245 // this test also handles if either area or pict's width/height are empty |
203 if (!SkRect::Intersects(area, | 246 if (!SkRect::Intersects(area, |
204 SkRect::MakeWH(SkIntToScalar(pict->width()), | 247 SkRect::MakeWH(SkIntToScalar(pict->width()), |
205 SkIntToScalar(pict->height())))) { | 248 SkIntToScalar(pict->height())))) { |
206 return NULL; | 249 return NULL; |
207 } | 250 } |
208 | 251 |
209 SkTDArray<SkPixelRef*> array; | 252 SkTDArray<SkPixelRef*> array; |
210 PixelRefSet prset(&array); | 253 PixelRefSet prset(&array); |
211 | 254 |
212 SkBitmap emptyBitmap; | 255 SkBitmap emptyBitmap; |
scroggo
2013/11/12 18:22:00
Can this be removed?
| |
213 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->heig ht()); | 256 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->heig ht()); |
214 // note: we do not set any pixels (shouldn't need to) | 257 // note: we do not set any pixels (shouldn't need to) |
215 | 258 |
216 GatherPixelRefDevice device(emptyBitmap, &prset); | 259 GatherPixelRefDevice device(pict->width(), pict->height(), &prset); |
217 NoSaveLayerCanvas canvas(&device); | 260 NoSaveLayerCanvas canvas(&device); |
218 | 261 |
219 canvas.clipRect(area, SkRegion::kIntersect_Op, false); | 262 canvas.clipRect(area, SkRegion::kIntersect_Op, false); |
220 canvas.drawPicture(*pict); | 263 canvas.drawPicture(*pict); |
221 | 264 |
222 SkData* data = NULL; | 265 SkData* data = NULL; |
223 int count = array.count(); | 266 int count = array.count(); |
224 if (count > 0) { | 267 if (count > 0) { |
225 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*) ); | 268 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*) ); |
226 } | 269 } |
227 return data; | 270 return data; |
228 } | 271 } |
OLD | NEW |