OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2008, 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 #include "platform/graphics/skia/NativeImageSkia.h" | |
33 | |
34 #include "platform/PlatformInstrumentation.h" | |
35 #include "platform/RuntimeEnabledFeatures.h" | |
36 #include "platform/TraceEvent.h" | |
37 #include "platform/geometry/FloatRect.h" | |
38 #include "platform/graphics/DeferredImageDecoder.h" | |
39 #include "platform/graphics/GraphicsContext.h" | |
40 #include "platform/graphics/skia/SkiaUtils.h" | |
41 #include "platform/transforms/AffineTransform.h" | |
42 #include "third_party/skia/include/core/SkCanvas.h" | |
43 #include "third_party/skia/include/core/SkColor.h" | |
44 #include "third_party/skia/include/core/SkImageInfo.h" | |
45 #include "third_party/skia/include/core/SkMatrix.h" | |
46 #include "third_party/skia/include/core/SkPaint.h" | |
47 #include "third_party/skia/include/core/SkRect.h" | |
48 #include "third_party/skia/include/core/SkScalar.h" | |
49 #include "third_party/skia/include/core/SkShader.h" | |
50 | |
51 namespace blink { | |
52 | |
53 void NativeImageSkia::draw( | |
54 GraphicsContext* context, | |
55 const SkRect& srcRect, | |
56 const SkRect& destRect, | |
57 SkXfermode::Mode op) const | |
58 { | |
59 TRACE_EVENT0("skia", "NativeImageSkia::draw"); | |
60 | |
61 bool isLazyDecoded = DeferredImageDecoder::isLazyDecoded(bitmap()); | |
62 bool isOpaque = bitmap().isOpaque(); | |
63 | |
64 { | |
65 SkPaint paint; | |
66 int initialSaveCount = context->preparePaintForDrawRectToRect(&paint, sr
cRect, destRect, op, !isOpaque, isLazyDecoded, isDataComplete()); | |
67 // We want to filter it if we decided to do interpolation above, or if | |
68 // there is something interesting going on with the matrix (like a rotat
ion). | |
69 // Note: for serialization, we will want to subset the bitmap first so w
e | |
70 // don't send extra pixels. | |
71 context->drawBitmapRect(bitmap(), &srcRect, destRect, &paint); | |
72 context->canvas()->restoreToCount(initialSaveCount); | |
73 } | |
74 | |
75 if (isLazyDecoded) | |
76 PlatformInstrumentation::didDrawLazyPixelRef(bitmap().getGenerationID())
; | |
77 } | |
78 | |
79 static SkBitmap createBitmapWithSpace(const SkBitmap& bitmap, int spaceWidth, in
t spaceHeight) | |
80 { | |
81 SkImageInfo info = bitmap.info(); | |
82 info = SkImageInfo::Make(info.width() + spaceWidth, info.height() + spaceHei
ght, info.colorType(), kPremul_SkAlphaType); | |
83 | |
84 SkBitmap result; | |
85 result.allocPixels(info); | |
86 result.eraseColor(SK_ColorTRANSPARENT); | |
87 bitmap.copyPixelsTo(reinterpret_cast<uint8_t*>(result.getPixels()), result.r
owBytes() * result.height(), result.rowBytes()); | |
88 | |
89 return result; | |
90 } | |
91 | |
92 void NativeImageSkia::drawPattern( | |
93 GraphicsContext* context, | |
94 const FloatRect& floatSrcRect, | |
95 const FloatSize& scale, | |
96 const FloatPoint& phase, | |
97 SkXfermode::Mode compositeOp, | |
98 const FloatRect& destRect, | |
99 const IntSize& repeatSpacing) const | |
100 { | |
101 FloatRect normSrcRect = floatSrcRect; | |
102 normSrcRect.intersect(FloatRect(0, 0, bitmap().width(), bitmap().height())); | |
103 if (destRect.isEmpty() || normSrcRect.isEmpty()) | |
104 return; // nothing to draw | |
105 | |
106 SkMatrix localMatrix; | |
107 // We also need to translate it such that the origin of the pattern is the | |
108 // origin of the destination rect, which is what WebKit expects. Skia uses | |
109 // the coordinate system origin as the base for the pattern. If WebKit wants | |
110 // a shifted image, it will shift it from there using the localMatrix. | |
111 const float adjustedX = phase.x() + normSrcRect.x() * scale.width(); | |
112 const float adjustedY = phase.y() + normSrcRect.y() * scale.height(); | |
113 localMatrix.setTranslate(SkFloatToScalar(adjustedX), SkFloatToScalar(adjuste
dY)); | |
114 | |
115 // Because no resizing occurred, the shader transform should be | |
116 // set to the pattern's transform, which just includes scale. | |
117 localMatrix.preScale(scale.width(), scale.height()); | |
118 | |
119 SkBitmap bitmapToPaint; | |
120 bitmap().extractSubset(&bitmapToPaint, enclosingIntRect(normSrcRect)); | |
121 if (!repeatSpacing.isZero()) { | |
122 SkScalar ctmScaleX = 1.0; | |
123 SkScalar ctmScaleY = 1.0; | |
124 | |
125 if (!RuntimeEnabledFeatures::slimmingPaintEnabled()) { | |
126 AffineTransform ctm = context->getCTM(); | |
127 ctmScaleX = ctm.xScale(); | |
128 ctmScaleY = ctm.yScale(); | |
129 } | |
130 | |
131 bitmapToPaint = createBitmapWithSpace( | |
132 bitmapToPaint, | |
133 repeatSpacing.width() * ctmScaleX / scale.width(), | |
134 repeatSpacing.height() * ctmScaleY / scale.height()); | |
135 } | |
136 RefPtr<SkShader> shader = adoptRef(SkShader::CreateBitmapShader(bitmapToPain
t, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix)); | |
137 | |
138 bool isLazyDecoded = DeferredImageDecoder::isLazyDecoded(bitmap()); | |
139 { | |
140 SkPaint paint; | |
141 int initialSaveCount = context->preparePaintForDrawRectToRect(&paint, fl
oatSrcRect, | |
142 destRect, compositeOp, !bitmap().isOpaque(), isLazyDecoded, isDataCo
mplete()); | |
143 paint.setShader(shader.get()); | |
144 context->drawRect(destRect, paint); | |
145 context->canvas()->restoreToCount(initialSaveCount); | |
146 } | |
147 | |
148 if (isLazyDecoded) | |
149 PlatformInstrumentation::didDrawLazyPixelRef(bitmap().getGenerationID())
; | |
150 } | |
151 | |
152 } // namespace blink | |
OLD | NEW |