OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012, Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
8 * * Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * * Redistributions in binary form must reproduce the above | |
11 * copyright notice, this list of conditions and the following disclaimer | |
12 * in the documentation and/or other materials provided with the | |
13 * distribution. | |
14 * * Neither the name of Google Inc. nor the names of its | |
15 * contributors may be used to endorse or promote products derived from | |
16 * this software without specific prior written permission. | |
17 * | |
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 #include "platform/graphics/skia/OpaqueRegionSkia.h" | |
34 | |
35 #include "platform/graphics/GraphicsContext.h" | |
36 | |
37 #include "SkColorFilter.h" | |
38 #include "SkShader.h" | |
39 | |
40 namespace blink { | |
41 | |
42 OpaqueRegionSkia::OpaqueRegionSkia() | |
43 : m_opaqueRect(SkRect::MakeEmpty()) | |
44 { | |
45 } | |
46 | |
47 void OpaqueRegionSkia::reset() | |
48 { | |
49 ASSERT(m_canvasLayerStack.isEmpty()); | |
50 m_opaqueRect = SkRect::MakeEmpty(); | |
51 } | |
52 | |
53 IntRect OpaqueRegionSkia::asRect() const | |
54 { | |
55 // Returns the largest enclosed rect. | |
56 // TODO: actually, this logic looks like its returning the smallest. | |
57 // to return largest, shouldn't we take floor of left/top | |
58 // and the ceil of right/bottom? | |
59 int left = SkScalarCeilToInt(m_opaqueRect.fLeft); | |
60 int top = SkScalarCeilToInt(m_opaqueRect.fTop); | |
61 int right = SkScalarFloorToInt(m_opaqueRect.fRight); | |
62 int bottom = SkScalarFloorToInt(m_opaqueRect.fBottom); | |
63 return IntRect(left, top, right-left, bottom-top); | |
64 } | |
65 | |
66 // Returns true if the xfermode will force the dst to be opaque, regardless of t
he current dst. | |
67 static inline bool xfermodeIsOpaque(const SkPaint& paint, bool srcIsOpaque) | |
68 { | |
69 if (!srcIsOpaque) | |
70 return false; | |
71 | |
72 SkXfermode* xfermode = paint.getXfermode(); | |
73 if (!xfermode) | |
74 return true; // default to kSrcOver_Mode | |
75 SkXfermode::Mode mode; | |
76 if (!xfermode->asMode(&mode)) | |
77 return false; | |
78 | |
79 switch (mode) { | |
80 case SkXfermode::kSrc_Mode: // source | |
81 case SkXfermode::kSrcOver_Mode: // source + dest - source*dest | |
82 case SkXfermode::kDstOver_Mode: // source + dest - source*dest | |
83 case SkXfermode::kDstATop_Mode: // source | |
84 case SkXfermode::kPlus_Mode: // source+dest | |
85 default: // the rest are all source + dest - source*dest | |
86 return true; | |
87 case SkXfermode::kClear_Mode: // 0 | |
88 case SkXfermode::kDst_Mode: // dest | |
89 case SkXfermode::kSrcIn_Mode: // source * dest | |
90 case SkXfermode::kDstIn_Mode: // dest * source | |
91 case SkXfermode::kSrcOut_Mode: // source * (1-dest) | |
92 case SkXfermode::kDstOut_Mode: // dest * (1-source) | |
93 case SkXfermode::kSrcATop_Mode: // dest | |
94 case SkXfermode::kXor_Mode: // source + dest - 2*(source*dest) | |
95 return false; | |
96 } | |
97 } | |
98 | |
99 // Returns true if the xfermode will keep the dst opaque, assuming the dst is al
ready opaque. | |
100 static inline bool xfermodePreservesOpaque(const SkPaint& paint, bool srcIsOpaqu
e) | |
101 { | |
102 SkXfermode* xfermode = paint.getXfermode(); | |
103 if (!xfermode) | |
104 return true; // default to kSrcOver_Mode | |
105 SkXfermode::Mode mode; | |
106 if (!xfermode->asMode(&mode)) | |
107 return false; | |
108 | |
109 switch (mode) { | |
110 case SkXfermode::kDst_Mode: // dest | |
111 case SkXfermode::kSrcOver_Mode: // source + dest - source*dest | |
112 case SkXfermode::kDstOver_Mode: // source + dest - source*dest | |
113 case SkXfermode::kSrcATop_Mode: // dest | |
114 case SkXfermode::kPlus_Mode: // source+dest | |
115 default: // the rest are all source + dest - source*dest | |
116 return true; | |
117 case SkXfermode::kClear_Mode: // 0 | |
118 case SkXfermode::kSrcOut_Mode: // source * (1-dest) | |
119 case SkXfermode::kDstOut_Mode: // dest * (1-source) | |
120 case SkXfermode::kXor_Mode: // source + dest - 2*(source*dest) | |
121 return false; | |
122 case SkXfermode::kSrc_Mode: // source | |
123 case SkXfermode::kSrcIn_Mode: // source * dest | |
124 case SkXfermode::kDstIn_Mode: // dest * source | |
125 case SkXfermode::kDstATop_Mode: // source | |
126 return srcIsOpaque; | |
127 } | |
128 } | |
129 | |
130 // Returns true if all pixels painted will be opaque. | |
131 static inline bool paintIsOpaque(const SkPaint& paint, OpaqueRegionSkia::DrawTyp
e drawType, const SkBitmap* bitmap) | |
132 { | |
133 if (paint.getAlpha() < 0xFF) | |
134 return false; | |
135 bool checkFillOnly = drawType != OpaqueRegionSkia::FillOrStroke; | |
136 if (!checkFillOnly && paint.getStyle() != SkPaint::kFill_Style && paint.isAn
tiAlias()) | |
137 return false; | |
138 SkShader* shader = paint.getShader(); | |
139 if (shader && !shader->isOpaque()) | |
140 return false; | |
141 if (bitmap && !bitmap->isOpaque()) | |
142 return false; | |
143 if (paint.getLooper()) | |
144 return false; | |
145 if (paint.getImageFilter()) | |
146 return false; | |
147 if (paint.getMaskFilter()) | |
148 return false; | |
149 SkColorFilter* colorFilter = paint.getColorFilter(); | |
150 if (colorFilter && !(colorFilter->getFlags() & SkColorFilter::kAlphaUnchange
d_Flag)) | |
151 return false; | |
152 return true; | |
153 } | |
154 | |
155 // Returns true if there is a rectangular clip, with the result in |deviceClipRe
ct|. | |
156 static inline bool getDeviceClipAsRect(const GraphicsContext* context, SkRect& d
eviceClipRect) | |
157 { | |
158 // Get the current clip in device coordinate space. | |
159 if (!context->canvas()->isClipRect()) | |
160 return false; | |
161 | |
162 SkIRect deviceClipIRect; | |
163 if (context->canvas()->getClipDeviceBounds(&deviceClipIRect)) | |
164 deviceClipRect.set(deviceClipIRect); | |
165 else | |
166 deviceClipRect.setEmpty(); | |
167 | |
168 return true; | |
169 } | |
170 | |
171 void OpaqueRegionSkia::pushCanvasLayer(const SkPaint* paint) | |
172 { | |
173 CanvasLayerState state; | |
174 if (paint) | |
175 state.paint = *paint; | |
176 m_canvasLayerStack.append(state); | |
177 } | |
178 | |
179 void OpaqueRegionSkia::popCanvasLayer(const GraphicsContext* context) | |
180 { | |
181 ASSERT(!context->paintingDisabled()); | |
182 ASSERT(!m_canvasLayerStack.isEmpty()); | |
183 if (m_canvasLayerStack.isEmpty()) | |
184 return; | |
185 | |
186 const CanvasLayerState& canvasLayer = m_canvasLayerStack.last(); | |
187 SkRect layerOpaqueRect = canvasLayer.opaqueRect; | |
188 SkPaint layerPaint = canvasLayer.paint; | |
189 | |
190 // Apply the image mask. | |
191 if (canvasLayer.hasImageMask && !layerOpaqueRect.intersect(canvasLayer.image
OpaqueRect)) | |
192 layerOpaqueRect.setEmpty(); | |
193 | |
194 m_canvasLayerStack.removeLast(); | |
195 | |
196 applyOpaqueRegionFromLayer(context, layerOpaqueRect, layerPaint); | |
197 } | |
198 | |
199 void OpaqueRegionSkia::setImageMask(const SkRect& imageOpaqueRect) | |
200 { | |
201 ASSERT(!m_canvasLayerStack.isEmpty()); | |
202 m_canvasLayerStack.last().hasImageMask = true; | |
203 m_canvasLayerStack.last().imageOpaqueRect = imageOpaqueRect; | |
204 } | |
205 | |
206 void OpaqueRegionSkia::didDrawRect(const GraphicsContext* context, const SkRect&
fillRect, const SkPaint& paint, const SkBitmap* sourceBitmap) | |
207 { | |
208 ASSERT(!context->paintingDisabled()); | |
209 // Any stroking may put alpha in pixels even if the filling part does not. | |
210 if (paint.getStyle() != SkPaint::kFill_Style) { | |
211 bool fillsBounds = false; | |
212 | |
213 if (!paint.canComputeFastBounds()) | |
214 didDrawUnbounded(context, paint, FillOrStroke); | |
215 else { | |
216 SkRect strokeRect; | |
217 strokeRect = paint.computeFastBounds(fillRect, &strokeRect); | |
218 didDraw(context, strokeRect, paint, sourceBitmap, fillsBounds, FillO
rStroke); | |
219 } | |
220 } | |
221 | |
222 bool fillsBounds = paint.getStyle() != SkPaint::kStroke_Style; | |
223 didDraw(context, fillRect, paint, sourceBitmap, fillsBounds, FillOnly); | |
224 } | |
225 | |
226 void OpaqueRegionSkia::didDrawPath(const GraphicsContext* context, const SkPath&
path, const SkPaint& paint) | |
227 { | |
228 ASSERT(!context->paintingDisabled()); | |
229 SkRect rect; | |
230 if (path.isRect(&rect)) { | |
231 didDrawRect(context, rect, paint, 0); | |
232 return; | |
233 } | |
234 | |
235 bool fillsBounds = false; | |
236 | |
237 if (!paint.canComputeFastBounds()) | |
238 didDrawUnbounded(context, paint, FillOrStroke); | |
239 else { | |
240 rect = paint.computeFastBounds(path.getBounds(), &rect); | |
241 didDraw(context, rect, paint, 0, fillsBounds, FillOrStroke); | |
242 } | |
243 } | |
244 | |
245 void OpaqueRegionSkia::didDrawPoints(const GraphicsContext* context, SkCanvas::P
ointMode mode, int numPoints, const SkPoint points[], const SkPaint& paint) | |
246 { | |
247 ASSERT(!context->paintingDisabled()); | |
248 if (!numPoints) | |
249 return; | |
250 | |
251 SkRect rect; | |
252 rect.fLeft = points[0].fX; | |
253 rect.fRight = points[0].fX + 1; | |
254 rect.fTop = points[0].fY; | |
255 rect.fBottom = points[0].fY + 1; | |
256 | |
257 for (int i = 1; i < numPoints; ++i) { | |
258 rect.fLeft = std::min(rect.fLeft, points[i].fX); | |
259 rect.fRight = std::max(rect.fRight, points[i].fX + 1); | |
260 rect.fTop = std::min(rect.fTop, points[i].fY); | |
261 rect.fBottom = std::max(rect.fBottom, points[i].fY + 1); | |
262 } | |
263 | |
264 bool fillsBounds = false; | |
265 | |
266 if (!paint.canComputeFastBounds()) | |
267 didDrawUnbounded(context, paint, FillOrStroke); | |
268 else { | |
269 rect = paint.computeFastBounds(rect, &rect); | |
270 didDraw(context, rect, paint, 0, fillsBounds, FillOrStroke); | |
271 } | |
272 } | |
273 | |
274 void OpaqueRegionSkia::didDrawBounded(const GraphicsContext* context, const SkRe
ct& bounds, const SkPaint& paint) | |
275 { | |
276 ASSERT(!context->paintingDisabled()); | |
277 bool fillsBounds = false; | |
278 | |
279 if (!paint.canComputeFastBounds()) | |
280 didDrawUnbounded(context, paint, FillOrStroke); | |
281 else { | |
282 SkRect rect; | |
283 rect = paint.computeFastBounds(bounds, &rect); | |
284 didDraw(context, rect, paint, 0, fillsBounds, FillOrStroke); | |
285 } | |
286 } | |
287 | |
288 void OpaqueRegionSkia::didDraw(const GraphicsContext* context, const SkRect& rec
t, const SkPaint& paint, const SkBitmap* sourceBitmap, bool fillsBounds, DrawTyp
e drawType) | |
289 { | |
290 ASSERT(!context->paintingDisabled()); | |
291 SkRect targetRect = rect; | |
292 | |
293 // Apply the transform to device coordinate space. | |
294 SkMatrix canvasTransform = context->canvas()->getTotalMatrix(); | |
295 if (!canvasTransform.mapRect(&targetRect)) | |
296 fillsBounds = false; | |
297 | |
298 // Apply the current clip. | |
299 SkRect deviceClipRect; | |
300 if (!getDeviceClipAsRect(context, deviceClipRect)) | |
301 fillsBounds = false; | |
302 else if (!targetRect.intersect(deviceClipRect)) | |
303 return; | |
304 | |
305 bool drawsOpaque = paintIsOpaque(paint, drawType, sourceBitmap); | |
306 bool xfersOpaque = xfermodeIsOpaque(paint, drawsOpaque); | |
307 bool preservesOpaque = xfermodePreservesOpaque(paint, drawsOpaque); | |
308 | |
309 if (fillsBounds && xfersOpaque) | |
310 markRectAsOpaque(targetRect); | |
311 else if (!preservesOpaque) | |
312 markRectAsNonOpaque(targetRect); | |
313 } | |
314 | |
315 void OpaqueRegionSkia::didDrawUnbounded(const GraphicsContext* context, const Sk
Paint& paint, DrawType drawType) | |
316 { | |
317 ASSERT(!context->paintingDisabled()); | |
318 bool drawsOpaque = paintIsOpaque(paint, drawType, 0); | |
319 bool preservesOpaque = xfermodePreservesOpaque(paint, drawsOpaque); | |
320 | |
321 if (preservesOpaque) | |
322 return; | |
323 | |
324 SkRect deviceClipRect; | |
325 getDeviceClipAsRect(context, deviceClipRect); | |
326 markRectAsNonOpaque(deviceClipRect); | |
327 } | |
328 | |
329 void OpaqueRegionSkia::applyOpaqueRegionFromLayer(const GraphicsContext* context
, const SkRect& layerOpaqueRect, const SkPaint& paint) | |
330 { | |
331 SkRect deviceClipRect; | |
332 bool deviceClipIsARect = getDeviceClipAsRect(context, deviceClipRect); | |
333 | |
334 if (deviceClipRect.isEmpty()) | |
335 return; | |
336 | |
337 SkRect sourceOpaqueRect = layerOpaqueRect; | |
338 // Save the opaque area in the destination, so we can preserve the parts of
it under the source opaque area if possible. | |
339 SkRect destinationOpaqueRect = currentTrackingOpaqueRect(); | |
340 | |
341 bool outsideSourceOpaqueRectPreservesOpaque = xfermodePreservesOpaque(paint,
false); | |
342 if (!outsideSourceOpaqueRectPreservesOpaque) | |
343 markRectAsNonOpaque(deviceClipRect); | |
344 | |
345 if (!deviceClipIsARect) | |
346 return; | |
347 if (!sourceOpaqueRect.intersect(deviceClipRect)) | |
348 return; | |
349 | |
350 bool sourceOpaqueRectDrawsOpaque = paintIsOpaque(paint, FillOnly, 0); | |
351 bool sourceOpaqueRectXfersOpaque = xfermodeIsOpaque(paint, sourceOpaqueRectD
rawsOpaque); | |
352 bool sourceOpaqueRectPreservesOpaque = xfermodePreservesOpaque(paint, source
OpaqueRectDrawsOpaque); | |
353 | |
354 // If the layer's opaque area is being drawn opaque in the layer below, then
mark it opaque. Otherwise, | |
355 // if it preserves opaque then keep the intersection of the two. | |
356 if (sourceOpaqueRectXfersOpaque) | |
357 markRectAsOpaque(sourceOpaqueRect); | |
358 else if (sourceOpaqueRectPreservesOpaque && sourceOpaqueRect.intersect(desti
nationOpaqueRect)) | |
359 markRectAsOpaque(sourceOpaqueRect); | |
360 } | |
361 | |
362 void OpaqueRegionSkia::markRectAsOpaque(const SkRect& rect) | |
363 { | |
364 // We want to keep track of an opaque region but bound its complexity at a c
onstant size. | |
365 // We keep track of the largest rectangle seen by area. If we can add the ne
w rect to this | |
366 // rectangle then we do that, as that is the cheapest way to increase the ar
ea returned | |
367 // without increasing the complexity. | |
368 | |
369 SkRect& opaqueRect = currentTrackingOpaqueRect(); | |
370 | |
371 if (rect.isEmpty()) | |
372 return; | |
373 if (opaqueRect.contains(rect)) | |
374 return; | |
375 if (rect.contains(opaqueRect)) { | |
376 opaqueRect = rect; | |
377 return; | |
378 } | |
379 | |
380 if (rect.fTop <= opaqueRect.fTop && rect.fBottom >= opaqueRect.fBottom) { | |
381 if (rect.fLeft < opaqueRect.fLeft && rect.fRight >= opaqueRect.fLeft) | |
382 opaqueRect.fLeft = rect.fLeft; | |
383 if (rect.fRight > opaqueRect.fRight && rect.fLeft <= opaqueRect.fRight) | |
384 opaqueRect.fRight = rect.fRight; | |
385 } else if (rect.fLeft <= opaqueRect.fLeft && rect.fRight >= opaqueRect.fRigh
t) { | |
386 if (rect.fTop < opaqueRect.fTop && rect.fBottom >= opaqueRect.fTop) | |
387 opaqueRect.fTop = rect.fTop; | |
388 if (rect.fBottom > opaqueRect.fBottom && rect.fTop <= opaqueRect.fBottom
) | |
389 opaqueRect.fBottom = rect.fBottom; | |
390 } | |
391 | |
392 long opaqueArea = (long)opaqueRect.width() * (long)opaqueRect.height(); | |
393 long area = (long)rect.width() * (long)rect.height(); | |
394 if (area > opaqueArea) | |
395 opaqueRect = rect; | |
396 } | |
397 | |
398 void OpaqueRegionSkia::markRectAsNonOpaque(const SkRect& rect) | |
399 { | |
400 // We want to keep as much of the current opaque rectangle as we can, so fin
d the one largest | |
401 // rectangle inside m_opaqueRect that does not intersect with |rect|. | |
402 | |
403 SkRect& opaqueRect = currentTrackingOpaqueRect(); | |
404 | |
405 if (!SkRect::Intersects(rect, opaqueRect)) | |
406 return; | |
407 if (rect.contains(opaqueRect)) { | |
408 markAllAsNonOpaque(); | |
409 return; | |
410 } | |
411 | |
412 int deltaLeft = rect.fLeft - opaqueRect.fLeft; | |
413 int deltaRight = opaqueRect.fRight - rect.fRight; | |
414 int deltaTop = rect.fTop - opaqueRect.fTop; | |
415 int deltaBottom = opaqueRect.fBottom - rect.fBottom; | |
416 | |
417 // horizontal is the larger of the two rectangles to the left or to the righ
t of |rect| and inside opaqueRect. | |
418 // vertical is the larger of the two rectangles above or below |rect| and in
side opaqueRect. | |
419 SkRect horizontal = opaqueRect; | |
420 if (deltaTop > deltaBottom) | |
421 horizontal.fBottom = rect.fTop; | |
422 else | |
423 horizontal.fTop = rect.fBottom; | |
424 SkRect vertical = opaqueRect; | |
425 if (deltaLeft > deltaRight) | |
426 vertical.fRight = rect.fLeft; | |
427 else | |
428 vertical.fLeft = rect.fRight; | |
429 | |
430 if ((long)horizontal.width() * (long)horizontal.height() > (long)vertical.wi
dth() * (long)vertical.height()) | |
431 opaqueRect = horizontal; | |
432 else | |
433 opaqueRect = vertical; | |
434 } | |
435 | |
436 void OpaqueRegionSkia::markAllAsNonOpaque() | |
437 { | |
438 SkRect& opaqueRect = currentTrackingOpaqueRect(); | |
439 opaqueRect.setEmpty(); | |
440 } | |
441 | |
442 SkRect& OpaqueRegionSkia::currentTrackingOpaqueRect() | |
443 { | |
444 // If we are drawing into a canvas layer, then track the opaque rect in that
layer. | |
445 return m_canvasLayerStack.isEmpty() ? m_opaqueRect : m_canvasLayerStack.last
().opaqueRect; | |
446 } | |
447 | |
448 } // namespace blink | |
OLD | NEW |