OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The Android Open Source Project | 2 * Copyright 2013 The Android Open Source Project |
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 "SkPictureImageFilter.h" | 8 #include "SkPictureImageFilter.h" |
9 #include "SkDevice.h" | 9 #include "SkSurface.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
12 #include "SkSurfaceProps.h" | 12 #include "SkSurfaceProps.h" |
13 #include "SkWriteBuffer.h" | 13 #include "SkWriteBuffer.h" |
14 #include "SkValidationUtils.h" | 14 #include "SkValidationUtils.h" |
15 | 15 |
16 SkPictureImageFilter::SkPictureImageFilter(const SkPicture* picture, uint32_t un
iqueID) | 16 SkPictureImageFilter::SkPictureImageFilter(const SkPicture* picture, uint32_t un
iqueID) |
17 : INHERITED(0, 0, NULL, uniqueID) | 17 : INHERITED(0, 0, NULL, uniqueID) |
18 , fPicture(SkSafeRef(picture)) | 18 , fPicture(SkSafeRef(picture)) |
19 , fCropRect(picture ? picture->cullRect() : SkRect::MakeEmpty()) | 19 , fCropRect(picture ? picture->cullRect() : SkRect::MakeEmpty()) |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 fPicture->flatten(buffer); | 83 fPicture->flatten(buffer); |
84 } | 84 } |
85 } | 85 } |
86 buffer.writeRect(fCropRect); | 86 buffer.writeRect(fCropRect); |
87 buffer.writeInt(fPictureResolution); | 87 buffer.writeInt(fPictureResolution); |
88 if (kLocalSpace_PictureResolution == fPictureResolution) { | 88 if (kLocalSpace_PictureResolution == fPictureResolution) { |
89 buffer.writeInt(fFilterLevel); | 89 buffer.writeInt(fFilterLevel); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 bool SkPictureImageFilter::onFilterImage(Proxy* proxy, const SkBitmap&, const Co
ntext& ctx, | 93 bool SkPictureImageFilter::onFilterImage(Proxy* proxy, SkImage&, const Context&
ctx, |
94 SkBitmap* result, SkIPoint* offset) con
st { | 94 SkAutoTUnref<SkImage>& result, SkIPoint
* offset) const { |
95 if (!fPicture) { | 95 if (!fPicture) { |
96 offset->fX = offset->fY = 0; | 96 offset->fX = offset->fY = 0; |
97 return true; | 97 return true; |
98 } | 98 } |
99 | 99 |
100 SkRect floatBounds; | 100 SkRect floatBounds; |
101 ctx.ctm().mapRect(&floatBounds, fCropRect); | 101 ctx.ctm().mapRect(&floatBounds, fCropRect); |
102 SkIRect bounds = floatBounds.roundOut(); | 102 SkIRect bounds = floatBounds.roundOut(); |
103 if (!bounds.intersect(ctx.clipBounds())) { | 103 if (!bounds.intersect(ctx.clipBounds())) { |
104 return false; | 104 return false; |
105 } | 105 } |
106 | 106 |
107 if (bounds.isEmpty()) { | 107 if (bounds.isEmpty()) { |
108 offset->fX = offset->fY = 0; | 108 offset->fX = offset->fY = 0; |
109 return true; | 109 return true; |
110 } | 110 } |
111 | 111 |
112 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(bounds.width(), bounds
.height())); | 112 SkAutoTUnref<SkSurface> surface(proxy->createSurface(bounds.width(), bounds.
height())); |
113 if (NULL == device.get()) { | 113 if (NULL == surface.get()) { |
114 return false; | 114 return false; |
115 } | 115 } |
116 | 116 |
117 if (kDeviceSpace_PictureResolution == fPictureResolution || | 117 if (kDeviceSpace_PictureResolution == fPictureResolution || |
118 0 == (ctx.ctm().getType() & ~SkMatrix::kTranslate_Mask)) { | 118 0 == (ctx.ctm().getType() & ~SkMatrix::kTranslate_Mask)) { |
119 drawPictureAtDeviceResolution(proxy, device.get(), bounds, ctx); | 119 drawPictureAtSurfaceResolution(proxy, surface.get(), bounds, ctx);
|
120 } else { | 120 } else { |
121 drawPictureAtLocalResolution(proxy, device.get(), bounds, ctx); | 121 drawPictureAtLocalResolution(proxy, surface.get(), bounds, ctx); |
122 } | 122 } |
123 | 123 |
124 *result = device.get()->accessBitmap(false); | 124 SkImage* snapshot = surface->newImageSnapshot(SkSurface::kYes_Budgeted); |
| 125 if (NULL == snapshot) { |
| 126 return false; |
| 127 } |
| 128 result.reset(snapshot); |
| 129 |
125 offset->fX = bounds.fLeft; | 130 offset->fX = bounds.fLeft; |
126 offset->fY = bounds.fTop; | 131 offset->fY = bounds.fTop; |
127 return true; | 132 return true; |
128 } | 133 } |
129 | 134 |
130 void SkPictureImageFilter::drawPictureAtDeviceResolution(Proxy* proxy, SkBaseDev
ice* device, | 135 void SkPictureImageFilter::drawPictureAtSurfaceResolution(Proxy* proxy, SkSurfac
e* surface, |
131 const SkIRect& deviceBo
unds, | 136 const SkIRect& surfaceB
ounds, |
132 const Context& ctx) con
st { | 137 const Context& ctx) con
st { |
133 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. | 138 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. |
134 // FIXME: switch back to the public constructor (and unfriend) after | 139 // FIXME: switch back to the public constructor (and unfriend) after |
135 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. | 140 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. |
136 SkCanvas canvas(device, proxy->surfaceProps(), SkCanvas::kDefault_InitFlags)
; | 141 SkCanvas* canvas = surface->getCanvas(); //TODO:(surface, proxy->surfaceProp
s(), SkCanvas::kDefault_InitFlags); |
137 | 142 |
138 canvas.translate(-SkIntToScalar(deviceBounds.fLeft), -SkIntToScalar(deviceBo
unds.fTop)); | 143 canvas->translate(-SkIntToScalar(surfaceBounds.fLeft), -SkIntToScalar(surfac
eBounds.fTop)); |
139 canvas.concat(ctx.ctm()); | 144 canvas->concat(ctx.ctm()); |
140 canvas.drawPicture(fPicture); | 145 canvas->drawPicture(fPicture); |
141 } | 146 } |
142 | 147 |
143 void SkPictureImageFilter::drawPictureAtLocalResolution(Proxy* proxy, SkBaseDevi
ce* device, | 148 void SkPictureImageFilter::drawPictureAtLocalResolution(Proxy* proxy, SkSurface*
surface, |
144 const SkIRect& deviceBou
nds, | 149 const SkIRect& surfaceBo
unds, |
145 const Context& ctx) cons
t { | 150 const Context& ctx) cons
t { |
146 SkMatrix inverseCtm; | 151 SkMatrix inverseCtm; |
147 if (!ctx.ctm().invert(&inverseCtm)) | 152 if (!ctx.ctm().invert(&inverseCtm)) |
148 return; | 153 return; |
149 SkRect localBounds = SkRect::Make(ctx.clipBounds()); | 154 SkRect localBounds = SkRect::Make(ctx.clipBounds()); |
150 inverseCtm.mapRect(&localBounds); | 155 inverseCtm.mapRect(&localBounds); |
151 if (!localBounds.intersect(fCropRect)) | 156 if (!localBounds.intersect(fCropRect)) |
152 return; | 157 return; |
153 SkIRect localIBounds = localBounds.roundOut(); | 158 SkIRect localIBounds = localBounds.roundOut(); |
154 SkAutoTUnref<SkBaseDevice> localDevice(proxy->createDevice(localIBounds.widt
h(), localIBounds.height())); | 159 SkAutoTUnref<SkSurface> localSurface(proxy->createSurface(localIBounds.width
(), localIBounds.height())); |
| 160 |
155 | 161 |
156 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. | 162 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. |
157 // FIXME: switch back to the public constructor (and unfriend) after | 163 // FIXME: switch back to the public constructor (and unfriend) after |
158 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. | 164 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. |
159 SkCanvas localCanvas(localDevice, proxy->surfaceProps(), SkCanvas::kDefault_
InitFlags); | 165 SkCanvas* localCanvas = localSurface->getCanvas(); // TODO :PROPS! |
160 localCanvas.translate(-SkIntToScalar(localIBounds.fLeft), -SkIntToScalar(loc
alIBounds.fTop)); | 166 localCanvas->translate(-SkIntToScalar(localIBounds.fLeft), -SkIntToScalar(lo
calIBounds.fTop)); |
161 localCanvas.drawPicture(fPicture); | 167 localCanvas->drawPicture(fPicture); |
| 168 |
| 169 SkAutoTUnref<SkImage> image(localSurface->newImageSnapshot(SkSurface::kYes_B
udgeted)); |
| 170 if (NULL == image) { |
| 171 return; |
| 172 } |
162 | 173 |
163 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. | 174 // Pass explicit surface props, as the simplified canvas constructor discard
s device properties. |
164 // FIXME: switch back to the public constructor (and unfriend) after | 175 // FIXME: switch back to the public constructor (and unfriend) after |
165 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. | 176 // https://code.google.com/p/skia/issues/detail?id=3142 is fixed. |
166 SkCanvas canvas(device, proxy->surfaceProps(), SkCanvas::kDefault_InitFlags)
; | 177 SkCanvas* canvas = surface->getCanvas(); // TODO: (device, proxy->surfacePro
ps(), SkCanvas::kDefault_InitFlags); |
167 | 178 canvas->translate(-SkIntToScalar(surfaceBounds.fLeft), -SkIntToScalar(surfac
eBounds.fTop)); |
168 canvas.translate(-SkIntToScalar(deviceBounds.fLeft), -SkIntToScalar(deviceBo
unds.fTop)); | 179 canvas->concat(ctx.ctm()); |
169 canvas.concat(ctx.ctm()); | |
170 SkPaint paint; | 180 SkPaint paint; |
171 paint.setFilterLevel(fFilterLevel); | 181 paint.setFilterLevel(fFilterLevel); |
172 canvas.drawBitmap(localDevice.get()->accessBitmap(false), SkIntToScalar(loca
lIBounds.fLeft), | 182 canvas->drawImage(image, SkIntToScalar(localIBounds.fLeft), |
173 SkIntToScalar(localIBounds.fTop), &paint); | 183 SkIntToScalar(localIBounds.fTop), &paint); |
174 //canvas.drawPicture(fPicture); | 184 //canvas.drawPicture(fPicture); |
175 } | 185 } |
176 | 186 |
177 #ifndef SK_IGNORE_TO_STRING | 187 #ifndef SK_IGNORE_TO_STRING |
178 void SkPictureImageFilter::toString(SkString* str) const { | 188 void SkPictureImageFilter::toString(SkString* str) const { |
179 str->appendf("SkPictureImageFilter: ("); | 189 str->appendf("SkPictureImageFilter: ("); |
180 str->appendf("crop: (%f,%f,%f,%f) ", | 190 str->appendf("crop: (%f,%f,%f,%f) ", |
181 fCropRect.fLeft, fCropRect.fTop, fCropRect.fRight, fCropRect.fB
ottom); | 191 fCropRect.fLeft, fCropRect.fTop, fCropRect.fRight, fCropRect.fB
ottom); |
182 if (fPicture) { | 192 if (fPicture) { |
183 str->appendf("picture: (%f,%f,%f,%f)", | 193 str->appendf("picture: (%f,%f,%f,%f)", |
184 fPicture->cullRect().fLeft, fPicture->cullRect().fTop, | 194 fPicture->cullRect().fLeft, fPicture->cullRect().fTop, |
185 fPicture->cullRect().fRight, fPicture->cullRect().fBottom); | 195 fPicture->cullRect().fRight, fPicture->cullRect().fBottom); |
186 } | 196 } |
187 str->append(")"); | 197 str->append(")"); |
188 } | 198 } |
189 #endif | 199 #endif |
OLD | NEW |