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

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: Use CachedImage as the backing store for ImageBitmap. Implement O(1) decode cache. 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_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y()))
37 , m_size(cropRect.size()) 37 , m_cropRect(cropRect)
38 { 38 {
39 Image* bitmapImage = image->cachedImage()->image(); 39 m_bitmap = new CachedImage(cropImage(image->cachedImage()->image(), cropRect ).get());
Justin Novosad 2013/07/23 16:55:12 Why a new cached image? can we not take a ref on t
40 m_bitmap = cropImage(bitmapImage, cropRect).get(); 40 m_bitmap->setDecodePriority(CachedResource::DecodePriorityHigh);
41 41
42 ScriptWrappable::init(this); 42 ScriptWrappable::init(this);
43 } 43 }
44 44
45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, IntRect cropRect) 45 ImageBitmap::ImageBitmap(HTMLVideoElement* video, const IntRect& cropRect)
46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 46 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y()))
47 , m_size(cropRect.size()) 47 , m_cropRect(cropRect)
48 { 48 {
49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize()); 49 IntRect videoRect = IntRect(IntPoint(), video->player()->naturalSize());
50 IntRect srcRect = intersection(cropRect, videoRect); 50 IntRect srcRect = intersection(cropRect, videoRect);
51 IntRect dstRect(IntPoint(), srcRect.size()); 51 IntRect dstRect(IntPoint(), srcRect.size());
52 52
53 m_buffer = ImageBuffer::create(videoRect.size()); 53 m_buffer = ImageBuffer::create(videoRect.size());
54 GraphicsContext* c = m_buffer->context(); 54 GraphicsContext* c = m_buffer->context();
55 c->clip(dstRect); 55 c->clip(dstRect);
56 c->translate(-srcRect.x(), -srcRect.y()); 56 c->translate(-srcRect.x(), -srcRect.y());
57 video->paintCurrentFrameInContext(c, videoRect); 57 video->paintCurrentFrameInContext(c, videoRect);
58 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 58 m_bitmap = new CachedImage(m_buffer->copyImage(DontCopyBackingStore).get());
Justin Novosad 2013/07/23 16:55:12 When creating from a video, we have no way to rege
59 m_bitmap->setDecodePriority(CachedResource::DecodePriorityHigh);
59 60
60 ScriptWrappable::init(this); 61 ScriptWrappable::init(this);
61 } 62 }
62 63
63 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, IntRect cropRect) 64 ImageBitmap::ImageBitmap(HTMLCanvasElement* canvas, const IntRect& cropRect)
64 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 65 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y()))
65 , m_size(cropRect.size()) 66 , m_cropRect(cropRect)
66 { 67 {
67 IntSize canvasSize = canvas->size(); 68 IntSize canvasSize = canvas->size();
68 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize)); 69 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), canvasSize));
69 IntRect dstRect(IntPoint(), srcRect.size()); 70 IntRect dstRect(IntPoint(), srcRect.size());
70 71
71 m_buffer = ImageBuffer::create(canvasSize); 72 m_buffer = ImageBuffer::create(canvasSize);
72 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect); 73 m_buffer->context()->drawImageBuffer(canvas->buffer(), dstRect, srcRect);
73 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 74 m_bitmap = new CachedImage(m_buffer->copyImage(DontCopyBackingStore).get());
Justin Novosad 2013/07/23 16:55:12 Same here. This should not ba a cached image.
75 m_bitmap->setDecodePriority(CachedResource::DecodePriorityHigh);
74 76
75 ScriptWrappable::init(this); 77 ScriptWrappable::init(this);
76 } 78 }
77 79
78 ImageBitmap::ImageBitmap(ImageData* data, IntRect cropRect) 80 ImageBitmap::ImageBitmap(ImageData* data, const IntRect& cropRect)
79 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y())) 81 : m_bitmapOffset(max(0, -cropRect.x()), max(0, -cropRect.y()))
80 , m_size(cropRect.size()) 82 , m_cropRect(cropRect)
81 { 83 {
82 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size())); 84 IntRect srcRect = intersection(cropRect, IntRect(IntPoint(), data->size()));
83 85
84 m_buffer = ImageBuffer::create(data->size()); 86 m_buffer = ImageBuffer::create(data->size());
85 if (srcRect.width() > 0 && srcRect.height() > 0) 87 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()))); 88 m_buffer->putByteArray(Unmultiplied, data->data(), data->size(), srcRect , IntPoint(min(0, -cropRect.x()), min(0, -cropRect.y())));
87 89
88 m_bitmap = static_cast<BitmapImage*>(m_buffer->copyImage(DontCopyBackingStor e).get()); 90 m_bitmap = new CachedImage(m_buffer->copyImage(DontCopyBackingStore).get());
Justin Novosad 2013/07/23 16:55:12 Should not be a cached image
91 m_bitmap->setDecodePriority(CachedResource::DecodePriorityHigh);
89 92
90 ScriptWrappable::init(this); 93 ScriptWrappable::init(this);
91 } 94 }
92 95
93 ImageBitmap::ImageBitmap(ImageBitmap* bitmap, IntRect cropRect) 96 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())) 97 : m_bitmapOffset(max(0, bitmap->bitmapOffset().x() - cropRect.x()), max(0, b itmap->bitmapOffset().y() - cropRect.y()))
95 , m_size(cropRect.size()) 98 , m_cropRect(cropRect)
96 { 99 {
97 Image* bitmapImage = bitmap->bitmapImage(); 100 IntRect adjustedCropRect(IntPoint(cropRect.x() - bitmap->bitmapOffset().x(), cropRect.y() - bitmap->bitmapOffset().y()), cropRect.size());
98 cropRect.moveBy(IntPoint(-bitmap->bitmapOffset().x(), -bitmap->bitmapOffset( ).y())); 101
99 m_bitmap = cropImage(bitmapImage, cropRect).get(); 102 m_bitmap = new CachedImage(cropImage(bitmap->bitmapImage(), adjustedCropRect ).get());
Justin Novosad 2013/07/23 16:55:12 This function needs to be more complex. It should
103 m_bitmap->setDecodePriority(CachedResource::DecodePriorityHigh);
100 104
101 ScriptWrappable::init(this); 105 ScriptWrappable::init(this);
102 } 106 }
103 107
104 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, IntRect cro pRect) 108 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLImageElement* image, const IntRe ct& cropRect)
105 { 109 {
106 IntRect normalizedCropRect = normalizeRect(cropRect); 110 IntRect normalizedCropRect = normalizeRect(cropRect);
107 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(image, normalizedCr opRect))); 111 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap( image, normalizedC ropRect)));
108 return imageBitmap.release(); 112 return imageBitmap.release();
109 } 113 }
110 114
111 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, IntRect cro pRect) 115 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLVideoElement* video, const IntRe ct& cropRect)
112 { 116 {
113 IntRect normalizedCropRect = normalizeRect(cropRect); 117 IntRect normalizedCropRect = normalizeRect(cropRect);
114 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect))); 118 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(video, normalizedCr opRect)));
115 return imageBitmap.release(); 119 return imageBitmap.release();
116 } 120 }
117 121
118 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, IntRect c ropRect) 122 PassRefPtr<ImageBitmap> ImageBitmap::create(HTMLCanvasElement* canvas, const Int Rect& cropRect)
119 { 123 {
120 IntRect normalizedCropRect = normalizeRect(cropRect); 124 IntRect normalizedCropRect = normalizeRect(cropRect);
121 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect))); 125 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(canvas, normalizedC ropRect)));
122 return imageBitmap.release(); 126 return imageBitmap.release();
123 } 127 }
124 128
125 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, IntRect cropRect) 129 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageData* data, const IntRect& crop Rect)
126 { 130 {
127 IntRect normalizedCropRect = normalizeRect(cropRect); 131 IntRect normalizedCropRect = normalizeRect(cropRect);
128 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect))); 132 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(data, normalizedCro pRect)));
129 return imageBitmap.release(); 133 return imageBitmap.release();
130 } 134 }
131 135
132 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, IntRect cropRec t) 136 PassRefPtr<ImageBitmap> ImageBitmap::create(ImageBitmap* bitmap, const IntRect& cropRect)
133 { 137 {
134 IntRect normalizedCropRect = normalizeRect(cropRect); 138 IntRect normalizedCropRect = normalizeRect(cropRect);
135 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect))); 139 RefPtr<ImageBitmap> imageBitmap(adoptRef(new ImageBitmap(bitmap, normalizedC ropRect)));
136 return imageBitmap.release(); 140 return imageBitmap.release();
137 } 141 }
138 142
139 } 143 }
OLDNEW
« Source/core/loader/cache/MemoryCache.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