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

Side by Side Diff: Source/core/page/ImageBitmap.cpp

Issue 19393004: Allow eviction of ImageBitmaps that are created from ImageElements. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix drawImage out of bounds src rect. Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "core/page/ImageBitmap.h" 6 #include "core/page/ImageBitmap.h"
7 7
8 #include "core/html/HTMLCanvasElement.h" 8 #include "core/html/HTMLCanvasElement.h"
9 #include "core/html/HTMLImageElement.h" 9 #include "core/html/HTMLImageElement.h"
10 #include "core/html/HTMLVideoElement.h" 10 #include "core/html/HTMLVideoElement.h"
11 #include "core/html/ImageData.h" 11 #include "core/html/ImageData.h"
12 #include "core/page/ImageBitmapCallback.h" 12 #include "core/platform/graphics/BitmapImage.h"
13 #include "core/platform/graphics/GraphicsContext.h" 13 #include "core/platform/graphics/GraphicsContext.h"
14 #include "wtf/RefPtr.h" 14 #include "wtf/RefPtr.h"
15 15
16 using namespace std; 16 using namespace std;
17 17
18 namespace WebCore { 18 namespace WebCore {
19 19
20 static inline IntRect normalizeRect(const IntRect rect) 20 static inline IntRect normalizeRect(const IntRect& rect)
21 { 21 {
22 return IntRect(min(rect.x(), rect.maxX()), 22 return IntRect(min(rect.x(), rect.maxX()),
23 min(rect.y(), rect.maxY()), 23 min(rect.y(), rect.maxY()),
24 max(rect.width(), -rect.width()), 24 max(rect.width(), -rect.width()),
25 max(rect.height(), -rect.height())); 25 max(rect.height(), -rect.height()));
26 } 26 }
27 27
28 static inline PassRefPtr<BitmapImage> cropImage(Image* image, IntRect cropRect) 28 static inline PassRefPtr<Image> cropImage(Image* image, const IntRect& cropRect)
29 { 29 {
30 SkBitmap cropped; 30 SkBitmap cropped;
31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct); 31 image->nativeImageForCurrentFrame()->bitmap().extractSubset(&cropped, cropRe ct);
32 return BitmapImage::create(NativeImageSkia::create(cropped)); 32 return BitmapImage::create(NativeImageSkia::create(cropped));
33 } 33 }
34 34
35 ImageBitmap::ImageBitmap(HTMLImageElement* image, IntRect cropRect) 35 ImageBitmap::ImageBitmap(HTMLImageElement* image, const IntRect& cropRect)
36 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 36 : m_cropRect(cropRect)
37 , m_size(cropRect.size()) 37 , m_imageElement(image)
38 { 38 {
39 Image* bitmapImage = image->cachedImage()->image(); 39 m_imageElement->addClient(this);
40 m_bitmap = cropImage(bitmapImage, cropRect).get(); 40 m_imageElement->cachedImage()->setCachePriority(CachedResource::CachePriorit yHigh);
41
42 IntSize bitmapSize = intersection(cropRect, IntRect(0, 0, image->width(), im age->height())).size();
43 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), bitmapSize);
41 44
42 ScriptWrappable::init(this); 45 ScriptWrappable::init(this);
43 } 46 }
44 47
45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) 48 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 49 : m_cropRect(cropRect)
47 , m_size(cropRect.size()) 50 , m_imageElement(0)
48 { 51 {
49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); 52 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize());
50 IntRect srcRect = intersection(cropRect, videoRect); 53 IntRect srcRect = intersection(cropRect, videoRect);
51 IntRect dstRect(IntPoint(), srcRect.size()); 54 IntRect dstRect(IntPoint(), srcRect.size());
52 55
53 m_buffer = ImageBuffer::create(videoRect.size()); 56 m_buffer = ImageBuffer::create(videoRect.size());
54 GraphicsContext* c = m_buffer->context(); 57 GraphicsContext* c = m_buffer->context();
55 c->clip(dstRect); 58 c->clip(dstRect);
56 c->translate(-srcRect.x(), -srcRect.y()); 59 c->translate(-srcRect.x(), -srcRect.y());
57 video->paintCurrentFrameInContext(c, videoRect); 60 video->paintCurrentFrameInContext(c, videoRect);
58 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 61 m_bitmap = m_buffer->copyImage(DontCopyBackingStore);
62 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.size());
59 63
60 ScriptWrappable::init(this); 64 ScriptWrappable::init(this);
61 } 65 }
62 66
63 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) 67 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
64 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 68 : m_cropRect(cropRect)
65 , m_size(cropRect.size()) 69 , m_imageElement(0)
66 { 70 {
67 IntSize canvasSize = canvas->size(); 71 IntSize canvasSize = canvas->size();
68 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); 72 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize));
69 IntRect dstRect(IntPoint(), srcRect.size()); 73 IntRect dstRect(IntPoint(), srcRect.size());
70 74
71 m_buffer = ImageBuffer::create(canvasSize); 75 m_buffer = ImageBuffer::create(canvasSize);
72 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); 76 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect);
73 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 77 m_bitmap = m_buffer->copyImage(DontCopyBackingStore);
78 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.size());
74 79
75 ScriptWrappable::init(this); 80 ScriptWrappable::init(this);
76 } 81 }
77 82
78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) 83 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 84 : m_cropRect(cropRect)
80 , m_size(cropRect.size()) 85 , m_imageElement(0)
81 { 86 {
82 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 87 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
83 88
84 m_buffer = ImageBuffer::create(data->size()); 89 m_buffer = ImageBuffer::create(data->size());
85 if (srcRect.width() > 0 && srcRect.height() > 0) 90 if (srcRect.width() > 0 && srcRect.height() > 0)
86 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y()))); 91 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y())));
87 92
88 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 93 m_bitmap = m_buffer->copyImage(DontCopyBackingStore);
94 m_bitmapRect = IntRect(IntPoint(max(0, -cropRect.x()), max(0, -cropRect.y()) ), srcRect.size());
89 95
90 ScriptWrappable::init(this); 96 ScriptWrappable::init(this);
91 } 97 }
92 98
93 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) 99 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, const IntRect& cropRect)
94 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y())) 100 : m_cropRect(cropRect)
95 , m_size(cropRect.size())
96 { 101 {
97 Image* bitmapImage = bitmap->bitmapImage(); 102 IntRect oldBitmapRect = bitmap->bitmapRect();
98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); 103 IntSize bitmapSize = intersection(cropRect, oldBitmapRect).size();
99 m_bitmap = cropImage(bitmapImage, cropRect).get(); 104 IntPoint bitmapOffset(max(0, oldBitmapRect.x() - cropRect.x()), max(0, oldBi tmapRect.y() - cropRect.y()));
100 105 m_bitmapRect = IntRect(bitmapOffset, bitmapSize);
106 if (m_imageElement = bitmap->imageElement()) {
107 m_imageElement->addClient(this);
108 m_bitmap = 0;
109 } else {
110 IntRect adjustedCropRect(IntPoint(cropRect.x() -oldBitmapRect.x(), cropR ect.y() - oldBitmapRect.y()), cropRect.size());
111 m_bitmap = cropImage(bitmap->bitmapImage().get(), adjustedCropRect);
112 }
101 ScriptWrappable::init(this); 113 ScriptWrappable::init(this);
102 } 114 }
103 115
104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) 116 ImageBitmap::~ImageBitmap()
117 {
118 if (m_imageElement)
119 m_imageElement->removeClient(this);
120 }
121
122 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe ct& cropRect)
105 { 123 {
106 IntRect normalizedCropRect = normalizeRect(cropRect); 124 IntRect normalizedCropRect = normalizeRect(cropRect);
107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); 125 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect)));
108 return imageBitmap.release(); 126 return imageBitmap.release();
109 } 127 }
110 128
111 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) 129 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRe ct& cropRect)
112 { 130 {
113 IntRect normalizedCropRect = normalizeRect(cropRect); 131 IntRect normalizedCropRect = normalizeRect(cropRect);
114 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); 132 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect)));
115 return imageBitmap.release(); 133 return imageBitmap.release();
116 } 134 }
117 135
118 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) 136 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const Int Rect& cropRect)
119 { 137 {
120 IntRect normalizedCropRect = normalizeRect(cropRect); 138 IntRect normalizedCropRect = normalizeRect(cropRect);
121 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); 139 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect)));
122 return imageBitmap.release(); 140 return imageBitmap.release();
123 } 141 }
124 142
125 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) 143 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& crop Rect)
126 { 144 {
127 IntRect normalizedCropRect = normalizeRect(cropRect); 145 IntRect normalizedCropRect = normalizeRect(cropRect);
128 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); 146 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect)));
129 return imageBitmap.release(); 147 return imageBitmap.release();
130 } 148 }
131 149
132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) 150 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
133 { 151 {
134 IntRect normalizedCropRect = normalizeRect(cropRect); 152 IntRect normalizedCropRect = normalizeRect(cropRect);
135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); 153 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect)));
136 return imageBitmap.release(); 154 return imageBitmap.release();
137 } 155 }
138 156
157 void ImageBitmap::notifyImageSourceChanged()
158 {
159 m_bitmap = cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
160 m_imageElement = 0;
139 } 161 }
162
163 PassRefPtr<Image> ImageBitmap::bitmapImage() const
164 {
165 ASSERT((m_imageElement || m_bitmap) && (!m_imageElement || !m_bitmap));
166 if (m_imageElement)
167 return cropImage(m_imageElement->cachedImage()->image(), m_cropRect);
168 return m_bitmap;
169 }
170
171 }
OLDNEW
« Source/core/loader/cache/MemoryCacheTest.cpp ('K') | « Source/core/page/ImageBitmap.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698