Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: Source/platform/graphics/BitmapPattern.cpp

Issue 358893002: Use newImageSnapshot() to get an image from a Canvas (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Correcting bugs and use new cache mechanism from SkImage Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
Justin Novosad 2014/07/29 14:56:51 Use the new abbreviated header (see http://www.chr
Rémi Piotaix 2014/07/29 17:18:22 Done.
2 //
3 // The Chromium Authors can be found at
4 // http://src.chromium.org/svn/trunk/src/AUTHORS
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #include "config.h"
33 #include "platform/graphics/BitmapPattern.h"
34
35
36 #include "SkCanvas.h"
37 #include "SkColorShader.h"
38 #include "platform/graphics/skia/SkiaUtils.h"
39 #include <v8.h>
40
41 namespace WebCore {
Justin Novosad 2014/07/29 14:56:51 WebCore -> blink
Rémi Piotaix 2014/07/29 17:18:22 Done.
42
43 BitmapPattern::BitmapPattern(PassRefPtr<Image> image, bool repeatX, bool repeatY )
44 : Pattern(repeatX, repeatY)
45 {
46 if (image) {
47 m_tileImage = image->nativeImageForCurrentFrame();
48 }
49 }
50
51 PassRefPtr<SkShader> BitmapPattern::createShader()
52 {
53 SkMatrix localMatrix = affineTransformToSkMatrix(m_patternSpaceTransformatio n);
54
55 RefPtr<SkShader> shader;
56
57 // If we don't have a bitmap, return a transparent shader.
58 if (!m_tileImage) {
59 shader = adoptRef(new SkColorShader(SK_ColorTRANSPARENT));
Justin Novosad 2014/07/29 14:56:51 Why not just return nullptr to make calling code a
Rémi Piotaix 2014/07/29 17:18:22 Done.
60 } else if (m_repeatX && m_repeatY) {
61 shader = adoptRef(SkShader::CreateBitmapShader(m_tileImage->bitmap(), Sk Shader::kRepeat_TileMode, SkShader::kRepeat_TileMode, &localMatrix));
62 } else {
63 // Skia does not have a "draw the tile only once" option. Clamp_TileMode
64 // repeats the last line of the image after drawing one tile. To avoid
65 // filling the space with arbitrary pixels, this workaround forces the
66 // image to have a line of transparent pixels on the "repeated" edge(s),
67 // thus causing extra space to be transparent filled.
68 SkShader::TileMode tileModeX = m_repeatX ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
69 SkShader::TileMode tileModeY = m_repeatY ? SkShader::kRepeat_TileMode : SkShader::kClamp_TileMode;
70 int expandW = m_repeatX ? 0 : 1;
71 int expandH = m_repeatY ? 0 : 1;
72
73 // Create a transparent bitmap 1 pixel wider and/or taller than the
74 // original, then copy the orignal into it.
75 // FIXME: Is there a better way to pad (not scale) an image in skia?
76 SkImageInfo info = m_tileImage->bitmap().info();
77 info.fWidth += expandW;
78 info.fHeight += expandH;
79 // we explicitly require non-opaquness, since we are going to add a tran sparent strip.
80 info.fAlphaType = kPremul_SkAlphaType;
81
82 SkBitmap bm2;
83 bm2.allocPixels(info);
84 bm2.eraseARGB(0x00, 0x00, 0x00, 0x00);
Justin Novosad 2014/07/29 14:56:51 Would be more efficient to clear just the border a
Rémi Piotaix 2014/07/30 18:59:12 Done.
85 SkCanvas canvas(bm2);
86 canvas.drawBitmap(m_tileImage->bitmap(), 0, 0);
Justin Novosad 2014/07/29 14:56:51 You should set-up an SkPaint that uses the Src xfe
Rémi Piotaix 2014/07/30 18:59:12 Done.
87 bm2.setImmutable();
88 shader = adoptRef(SkShader::CreateBitmapShader(bm2, tileModeX, tileModeY , &localMatrix));
89
90 // Clamp to int, since that's what the adjust function takes.
91 m_externalMemoryAllocated = static_cast<int> (std::min(static_cast<size_ t> (INT_MAX), bm2.getSafeSize()));
92 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(m_exter nalMemoryAllocated);
Justin Novosad 2014/07/29 14:56:51 The adjustment needs to be a delta between the old
Rémi Piotaix 2014/07/30 18:59:12 Done.
93 }
94 return shader;
Justin Novosad 2014/07/29 14:56:51 return shader.release();
Rémi Piotaix 2014/07/29 17:18:22 Done.
95 }
96
97 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698