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 27 matching lines...) Expand all Loading... |
38 | 38 |
39 static void not_supported() { | 39 static void not_supported() { |
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. |
49 * FIXME: Derive from SkBaseDevice. | |
50 */ | 49 */ |
51 class GatherPixelRefDevice : public SkBitmapDevice { | 50 class GatherPixelRefDevice : public SkBaseDevice { |
52 private: | |
53 PixelRefSet* fPRSet; | |
54 | |
55 void addBitmap(const SkBitmap& bm) { | |
56 fPRSet->add(bm.pixelRef()); | |
57 } | |
58 | |
59 void addBitmapFromPaint(const SkPaint& paint) { | |
60 SkShader* shader = paint.getShader(); | |
61 if (shader) { | |
62 SkBitmap bm; | |
63 // Check whether the shader is a gradient in order to short-circuit | |
64 // call to asABitmap to prevent generation of bitmaps from | |
65 // gradient shaders, which implement asABitmap. | |
66 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && | |
67 shader->asABitmap(&bm, NULL, NULL)) { | |
68 fPRSet->add(bm.pixelRef()); | |
69 } | |
70 } | |
71 } | |
72 | |
73 public: | 51 public: |
74 GatherPixelRefDevice(const SkBitmap& bm, PixelRefSet* prset) : SkBitmapDevic
e(bm) { | 52 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) { |
| 53 fSize.set(width, height); |
| 54 fEmptyBitmap.setConfig(SkBitmap::kNo_Config, width, height); |
75 fPRSet = prset; | 55 fPRSet = prset; |
76 } | 56 } |
77 | 57 |
| 58 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE { return 0; } |
| 59 virtual int width() const SK_OVERRIDE { return fSize.width(); } |
| 60 virtual int height() const SK_OVERRIDE { return fSize.height(); } |
| 61 virtual bool isOpaque() const SK_OVERRIDE { return false; } |
| 62 virtual SkBitmap::Config config() const SK_OVERRIDE { |
| 63 return SkBitmap::kNo_Config; |
| 64 } |
| 65 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; } |
| 66 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE { |
| 67 return true; |
| 68 } |
| 69 // TODO: allow this call to return failure, or move to SkBitmapDevice only. |
| 70 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE { |
| 71 return fEmptyBitmap; |
| 72 } |
| 73 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); } |
| 74 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); } |
| 75 virtual bool allowImageFilter(SkImageFilter*) SK_OVERRIDE { return false; } |
| 76 virtual bool canHandleImageFilter(SkImageFilter*) SK_OVERRIDE { return false
; } |
| 77 virtual bool filterImage(SkImageFilter*, const SkBitmap&, const SkMatrix&, |
| 78 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE { |
| 79 return false; |
| 80 } |
| 81 |
78 virtual void clear(SkColor color) SK_OVERRIDE { | 82 virtual void clear(SkColor color) SK_OVERRIDE { |
79 nothing_to_do(); | 83 nothing_to_do(); |
80 } | 84 } |
81 virtual void writePixels(const SkBitmap& bitmap, int x, int y, | 85 virtual void writePixels(const SkBitmap& bitmap, int x, int y, |
82 SkCanvas::Config8888 config8888) SK_OVERRIDE { | 86 SkCanvas::Config8888 config8888) SK_OVERRIDE { |
83 not_supported(); | 87 not_supported(); |
84 } | 88 } |
85 | 89 |
86 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { | 90 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE { |
87 this->addBitmapFromPaint(paint); | 91 this->addBitmapFromPaint(paint); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 153 } |
150 | 154 |
151 protected: | 155 protected: |
152 virtual bool onReadPixels(const SkBitmap& bitmap, | 156 virtual bool onReadPixels(const SkBitmap& bitmap, |
153 int x, int y, | 157 int x, int y, |
154 SkCanvas::Config8888 config8888) SK_OVERRIDE { | 158 SkCanvas::Config8888 config8888) SK_OVERRIDE { |
155 not_supported(); | 159 not_supported(); |
156 return false; | 160 return false; |
157 } | 161 } |
158 | 162 |
| 163 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRI
DE { |
| 164 not_supported(); |
| 165 } |
| 166 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config, |
| 167 int width, int height, |
| 168 bool isOpaque, |
| 169 Usage usage) SK_OVERRIDE { |
| 170 // we expect to only get called via savelayer, in which case it is fine. |
| 171 SkASSERT(kSaveLayer_Usage == usage); |
| 172 return SkNEW_ARGS(GatherPixelRefDevice, (width, height, fPRSet)); |
| 173 } |
| 174 virtual void flush() SK_OVERRIDE {} |
| 175 |
159 private: | 176 private: |
160 typedef SkBitmapDevice INHERITED; | 177 PixelRefSet* fPRSet; |
| 178 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy |
| 179 SkISize fSize; |
| 180 |
| 181 void addBitmap(const SkBitmap& bm) { |
| 182 fPRSet->add(bm.pixelRef()); |
| 183 } |
| 184 |
| 185 void addBitmapFromPaint(const SkPaint& paint) { |
| 186 SkShader* shader = paint.getShader(); |
| 187 if (shader) { |
| 188 SkBitmap bm; |
| 189 // Check whether the shader is a gradient in order to short-circuit |
| 190 // call to asABitmap to prevent generation of bitmaps from |
| 191 // gradient shaders, which implement asABitmap. |
| 192 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) && |
| 193 shader->asABitmap(&bm, NULL, NULL)) { |
| 194 fPRSet->add(bm.pixelRef()); |
| 195 } |
| 196 } |
| 197 } |
| 198 |
| 199 typedef SkBaseDevice INHERITED; |
161 }; | 200 }; |
162 | 201 |
163 class NoSaveLayerCanvas : public SkCanvas { | 202 class NoSaveLayerCanvas : public SkCanvas { |
164 public: | 203 public: |
165 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {} | 204 NoSaveLayerCanvas(SkBaseDevice* device) : INHERITED(device) {} |
166 | 205 |
167 // turn saveLayer() into save() for speed, should not affect correctness. | 206 // turn saveLayer() into save() for speed, should not affect correctness. |
168 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, | 207 virtual int saveLayer(const SkRect* bounds, const SkPaint* paint, |
169 SaveFlags flags) SK_OVERRIDE { | 208 SaveFlags flags) SK_OVERRIDE { |
170 | 209 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // this test also handles if either area or pict's width/height are empty | 246 // this test also handles if either area or pict's width/height are empty |
208 if (!SkRect::Intersects(area, | 247 if (!SkRect::Intersects(area, |
209 SkRect::MakeWH(SkIntToScalar(pict->width()), | 248 SkRect::MakeWH(SkIntToScalar(pict->width()), |
210 SkIntToScalar(pict->height())))) { | 249 SkIntToScalar(pict->height())))) { |
211 return NULL; | 250 return NULL; |
212 } | 251 } |
213 | 252 |
214 SkTDArray<SkPixelRef*> array; | 253 SkTDArray<SkPixelRef*> array; |
215 PixelRefSet prset(&array); | 254 PixelRefSet prset(&array); |
216 | 255 |
217 SkBitmap emptyBitmap; | 256 GatherPixelRefDevice device(pict->width(), pict->height(), &prset); |
218 emptyBitmap.setConfig(SkBitmap::kARGB_8888_Config, pict->width(), pict->heig
ht()); | |
219 // note: we do not set any pixels (shouldn't need to) | |
220 | |
221 GatherPixelRefDevice device(emptyBitmap, &prset); | |
222 NoSaveLayerCanvas canvas(&device); | 257 NoSaveLayerCanvas canvas(&device); |
223 | 258 |
224 canvas.clipRect(area, SkRegion::kIntersect_Op, false); | 259 canvas.clipRect(area, SkRegion::kIntersect_Op, false); |
225 canvas.drawPicture(*pict); | 260 canvas.drawPicture(*pict); |
226 | 261 |
227 SkData* data = NULL; | 262 SkData* data = NULL; |
228 int count = array.count(); | 263 int count = array.count(); |
229 if (count > 0) { | 264 if (count > 0) { |
230 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*)
); | 265 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*)
); |
231 } | 266 } |
232 return data; | 267 return data; |
233 } | 268 } |
OLD | NEW |