2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBitmapDevice.h"
11 #include "SkNoSaveLayerCanvas.h"
12 #include "SkPictureUtils.h"
13 #include "SkPixelRef.h"
19 PixelRefSet(SkTDArray<SkPixelRef*>* array) : fArray(array) {}
21 // This does a linear search on existing pixelrefs, so if this list gets big
22 // we should use a more complex sorted/hashy thing.
24 void add(SkPixelRef* pr) {
25 uint32_t genID = pr->getGenerationID();
26 if (fGenID.find(genID) < 0) {
27 *fArray->append() = pr;
28 *fGenID.append() = genID;
29 // SkDebugf("--- adding [%d] %x %d\n", fArray->count() - 1, pr, genID);
31 // SkDebugf("--- already have %x %d\n", pr, genID);
36 SkTDArray<SkPixelRef*>* fArray;
37 SkTDArray<uint32_t> fGenID;
40 static void not_supported() {
41 SkDEBUGFAIL("this method should never be called");
44 static void nothing_to_do() {}
47 * This device will route all bitmaps (primitives and in shaders) to its PRSet.
48 * It should never actually draw anything, so there need not be any pixels
51 class GatherPixelRefDevice : public SkBaseDevice {
53 SK_DECLARE_INST_COUNT(GatherPixelRefDevice)
55 GatherPixelRefDevice(int width, int height, PixelRefSet* prset) {
56 fSize.set(width, height);
57 fEmptyBitmap.setInfo(SkImageInfo::MakeUnknown(width, height));
61 virtual SkImageInfo imageInfo() const SK_OVERRIDE {
62 return SkImageInfo::MakeUnknown(fSize.width(), fSize.height());
64 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE { return NULL; }
65 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
68 // TODO: allow this call to return failure, or move to SkBitmapDevice only.
69 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE {
72 virtual void lockPixels() SK_OVERRIDE { nothing_to_do(); }
73 virtual void unlockPixels() SK_OVERRIDE { nothing_to_do(); }
74 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
75 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE { return false; }
76 virtual bool filterImage(const SkImageFilter*, const SkBitmap&, const SkImageFilter::Context&,
77 SkBitmap* result, SkIPoint* offset) SK_OVERRIDE {
81 virtual void clear(SkColor color) SK_OVERRIDE {
85 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE {
86 this->addBitmapFromPaint(paint);
88 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count,
89 const SkPoint[], const SkPaint& paint) SK_OVERRIDE {
90 this->addBitmapFromPaint(paint);
92 virtual void drawRect(const SkDraw&, const SkRect&,
93 const SkPaint& paint) SK_OVERRIDE {
94 this->addBitmapFromPaint(paint);
96 virtual void drawRRect(const SkDraw&, const SkRRect&,
97 const SkPaint& paint) SK_OVERRIDE {
98 this->addBitmapFromPaint(paint);
100 virtual void drawOval(const SkDraw&, const SkRect&,
101 const SkPaint& paint) SK_OVERRIDE {
102 this->addBitmapFromPaint(paint);
104 virtual void drawPath(const SkDraw&, const SkPath& path,
105 const SkPaint& paint, const SkMatrix* prePathMatrix,
106 bool pathIsMutable) SK_OVERRIDE {
107 this->addBitmapFromPaint(paint);
109 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
110 const SkMatrix&, const SkPaint& paint) SK_OVERRIDE {
111 this->addBitmap(bitmap);
112 if (kAlpha_8_SkColorType == bitmap.colorType()) {
113 this->addBitmapFromPaint(paint);
116 virtual void drawBitmapRect(const SkDraw&, const SkBitmap& bitmap,
117 const SkRect* srcOrNull, const SkRect& dst,
118 const SkPaint& paint,
119 SkCanvas::DrawBitmapRectFlags flags) SK_OVERRIDE {
120 this->addBitmap(bitmap);
121 if (kAlpha_8_SkColorType == bitmap.colorType()) {
122 this->addBitmapFromPaint(paint);
125 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
126 int x, int y, const SkPaint& paint) SK_OVERRIDE {
127 this->addBitmap(bitmap);
129 virtual void drawText(const SkDraw&, const void* text, size_t len,
130 SkScalar x, SkScalar y,
131 const SkPaint& paint) SK_OVERRIDE {
132 this->addBitmapFromPaint(paint);
134 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
135 const SkScalar pos[], SkScalar constY,
136 int, const SkPaint& paint) SK_OVERRIDE {
137 this->addBitmapFromPaint(paint);
139 virtual void drawTextOnPath(const SkDraw&, const void* text, size_t len,
140 const SkPath& path, const SkMatrix* matrix,
141 const SkPaint& paint) SK_OVERRIDE {
142 this->addBitmapFromPaint(paint);
144 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount,
145 const SkPoint verts[], const SkPoint texs[],
146 const SkColor colors[], SkXfermode* xmode,
147 const uint16_t indices[], int indexCount,
148 const SkPaint& paint) SK_OVERRIDE {
149 this->addBitmapFromPaint(paint);
151 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
152 const SkPaint&) SK_OVERRIDE {
157 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {
160 virtual SkBaseDevice* onCreateDevice(const SkImageInfo& info, Usage usage) SK_OVERRIDE {
161 // we expect to only get called via savelayer, in which case it is fine.
162 SkASSERT(kSaveLayer_Usage == usage);
163 return SkNEW_ARGS(GatherPixelRefDevice, (info.width(), info.height(), fPRSet));
165 virtual void flush() SK_OVERRIDE {}
169 SkBitmap fEmptyBitmap; // legacy -- need to remove the need for this guy
172 void addBitmap(const SkBitmap& bm) {
173 fPRSet->add(bm.pixelRef());
176 void addBitmapFromPaint(const SkPaint& paint) {
177 SkShader* shader = paint.getShader();
180 // Check whether the shader is a gradient in order to short-circuit
181 // call to asABitmap to prevent generation of bitmaps from
182 // gradient shaders, which implement asABitmap.
183 if (SkShader::kNone_GradientType == shader->asAGradient(NULL) &&
184 shader->asABitmap(&bm, NULL, NULL)) {
185 fPRSet->add(bm.pixelRef());
190 typedef SkBaseDevice INHERITED;
193 SkData* SkPictureUtils::GatherPixelRefs(SkPicture* pict, const SkRect& area) {
198 // this test also handles if either area or pict's width/height are empty
199 if (!SkRect::Intersects(area,
200 SkRect::MakeWH(SkIntToScalar(pict->width()),
201 SkIntToScalar(pict->height())))) {
205 SkTDArray<SkPixelRef*> array;
206 PixelRefSet prset(&array);
208 GatherPixelRefDevice device(pict->width(), pict->height(), &prset);
209 SkNoSaveLayerCanvas canvas(&device);
211 canvas.clipRect(area, SkRegion::kIntersect_Op, false);
212 canvas.drawPicture(pict);
215 int count = array.count();
217 data = SkData::NewFromMalloc(array.detach(), count * sizeof(SkPixelRef*));