Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / graphics / BitmapImage.cpp
1 /*
2  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3  * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "platform/graphics/BitmapImage.h"
29
30 #include "platform/Timer.h"
31 #include "platform/geometry/FloatRect.h"
32 #include "platform/graphics/GraphicsContextStateSaver.h"
33 #include "platform/graphics/ImageObserver.h"
34 #include "platform/graphics/skia/NativeImageSkia.h"
35 #include "platform/graphics/skia/SkiaUtils.h"
36 #include "wtf/CurrentTime.h"
37 #include "wtf/PassRefPtr.h"
38 #include "wtf/Vector.h"
39 #include "wtf/text/WTFString.h"
40
41 namespace WebCore {
42
43 BitmapImage::BitmapImage(ImageObserver* observer)
44     : Image(observer)
45     , m_currentFrame(0)
46     , m_frames(0)
47     , m_frameTimer(0)
48     , m_repetitionCount(cAnimationNone)
49     , m_repetitionCountStatus(Unknown)
50     , m_repetitionsComplete(0)
51     , m_desiredFrameStartTime(0)
52     , m_frameCount(0)
53     , m_isSolidColor(false)
54     , m_checkedForSolidColor(false)
55     , m_animationFinished(false)
56     , m_allDataReceived(false)
57     , m_haveSize(false)
58     , m_sizeAvailable(false)
59     , m_hasUniformFrameSize(true)
60     , m_haveFrameCount(false)
61 {
62 }
63
64 BitmapImage::BitmapImage(PassRefPtr<NativeImageSkia> nativeImage, ImageObserver* observer)
65     : Image(observer)
66     , m_size(nativeImage->bitmap().width(), nativeImage->bitmap().height())
67     , m_currentFrame(0)
68     , m_frames(0)
69     , m_frameTimer(0)
70     , m_repetitionCount(cAnimationNone)
71     , m_repetitionCountStatus(Unknown)
72     , m_repetitionsComplete(0)
73     , m_frameCount(1)
74     , m_isSolidColor(false)
75     , m_checkedForSolidColor(false)
76     , m_animationFinished(true)
77     , m_allDataReceived(true)
78     , m_haveSize(true)
79     , m_sizeAvailable(true)
80     , m_haveFrameCount(true)
81 {
82     // Since we don't have a decoder, we can't figure out the image orientation.
83     // Set m_sizeRespectingOrientation to be the same as m_size so it's not 0x0.
84     m_sizeRespectingOrientation = m_size;
85
86     m_frames.grow(1);
87     m_frames[0].m_hasAlpha = !nativeImage->bitmap().isOpaque();
88     m_frames[0].m_frame = nativeImage;
89     m_frames[0].m_haveMetadata = true;
90
91     checkForSolidColor();
92 }
93
94 BitmapImage::~BitmapImage()
95 {
96     stopAnimation();
97 }
98
99 bool BitmapImage::isBitmapImage() const
100 {
101     return true;
102 }
103
104 void BitmapImage::destroyDecodedData(bool destroyAll)
105 {
106     for (size_t i = 0; i < m_frames.size(); ++i) {
107         // The underlying frame isn't actually changing (we're just trying to
108         // save the memory for the framebuffer data), so we don't need to clear
109         // the metadata.
110         m_frames[i].clear(false);
111     }
112
113     destroyMetadataAndNotify(m_source.clearCacheExceptFrame(destroyAll ? kNotFound : m_currentFrame));
114 }
115
116 void BitmapImage::destroyDecodedDataIfNecessary()
117 {
118     // Animated images >5MB are considered large enough that we'll only hang on
119     // to one frame at a time.
120     static const size_t cLargeAnimationCutoff = 5242880;
121     size_t allFrameBytes = 0;
122     for (size_t i = 0; i < m_frames.size(); ++i)
123         allFrameBytes += m_frames[i].m_frameBytes;
124
125     if (allFrameBytes > cLargeAnimationCutoff)
126         destroyDecodedData(false);
127 }
128
129 void BitmapImage::destroyMetadataAndNotify(size_t frameBytesCleared)
130 {
131     m_isSolidColor = false;
132     m_checkedForSolidColor = false;
133
134     if (frameBytesCleared && imageObserver())
135         imageObserver()->decodedSizeChanged(this, -safeCast<int>(frameBytesCleared));
136 }
137
138 void BitmapImage::cacheFrame(size_t index)
139 {
140     size_t numFrames = frameCount();
141     if (m_frames.size() < numFrames)
142         m_frames.grow(numFrames);
143
144     m_frames[index].m_frame = m_source.createFrameAtIndex(index);
145     if (numFrames == 1 && m_frames[index].m_frame)
146         checkForSolidColor();
147
148     m_frames[index].m_orientation = m_source.orientationAtIndex(index);
149     m_frames[index].m_haveMetadata = true;
150     m_frames[index].m_isComplete = m_source.frameIsCompleteAtIndex(index);
151     if (repetitionCount(false) != cAnimationNone)
152         m_frames[index].m_duration = m_source.frameDurationAtIndex(index);
153     m_frames[index].m_hasAlpha = m_source.frameHasAlphaAtIndex(index);
154     m_frames[index].m_frameBytes = m_source.frameBytesAtIndex(index);
155
156     const IntSize frameSize(index ? m_source.frameSizeAtIndex(index) : m_size);
157     if (frameSize != m_size)
158         m_hasUniformFrameSize = false;
159     if (m_frames[index].m_frame) {
160         int deltaBytes = safeCast<int>(m_frames[index].m_frameBytes);
161         // The fully-decoded frame will subsume the partially decoded data used
162         // to determine image properties.
163         if (imageObserver())
164             imageObserver()->decodedSizeChanged(this, deltaBytes);
165     }
166 }
167
168 void BitmapImage::updateSize() const
169 {
170     if (!m_sizeAvailable || m_haveSize)
171         return;
172
173     m_size = m_source.size();
174     m_sizeRespectingOrientation = m_source.size(RespectImageOrientation);
175     m_haveSize = true;
176 }
177
178 IntSize BitmapImage::size() const
179 {
180     updateSize();
181     return m_size;
182 }
183
184 IntSize BitmapImage::sizeRespectingOrientation() const
185 {
186     updateSize();
187     return m_sizeRespectingOrientation;
188 }
189
190 IntSize BitmapImage::currentFrameSize() const
191 {
192     if (!m_currentFrame || m_hasUniformFrameSize)
193         return size();
194     IntSize frameSize = m_source.frameSizeAtIndex(m_currentFrame);
195     return frameSize;
196 }
197
198 bool BitmapImage::getHotSpot(IntPoint& hotSpot) const
199 {
200     bool result = m_source.getHotSpot(hotSpot);
201     return result;
202 }
203
204 bool BitmapImage::dataChanged(bool allDataReceived)
205 {
206     TRACE_EVENT0("webkit", "BitmapImage::dataChanged");
207
208     // Clear all partially-decoded frames. For most image formats, there is only
209     // one frame, but at least GIF and ICO can have more. With GIFs, the frames
210     // come in order and we ask to decode them in order, waiting to request a
211     // subsequent frame until the prior one is complete. Given that we clear
212     // incomplete frames here, this means there is at most one incomplete frame
213     // (even if we use destroyDecodedData() -- since it doesn't reset the
214     // metadata), and it is after all the complete frames.
215     //
216     // With ICOs, on the other hand, we may ask for arbitrary frames at
217     // different times (e.g. because we're displaying a higher-resolution image
218     // in the content area and using a lower-resolution one for the favicon),
219     // and the frames aren't even guaranteed to appear in the file in the same
220     // order as in the directory, so an arbitrary number of the frames might be
221     // incomplete (if we ask for frames for which we've not yet reached the
222     // start of the frame data), and any or none of them might be the particular
223     // frame affected by appending new data here. Thus we have to clear all the
224     // incomplete frames to be safe.
225     unsigned frameBytesCleared = 0;
226     for (size_t i = 0; i < m_frames.size(); ++i) {
227         // NOTE: Don't call frameIsCompleteAtIndex() here, that will try to
228         // decode any uncached (i.e. never-decoded or
229         // cleared-on-a-previous-pass) frames!
230         unsigned frameBytes = m_frames[i].m_frameBytes;
231         if (m_frames[i].m_haveMetadata && !m_frames[i].m_isComplete)
232             frameBytesCleared += (m_frames[i].clear(true) ? frameBytes : 0);
233     }
234     destroyMetadataAndNotify(frameBytesCleared);
235
236     // Feed all the data we've seen so far to the image decoder.
237     m_allDataReceived = allDataReceived;
238     ASSERT(data());
239     m_source.setData(*data(), allDataReceived);
240
241     m_haveFrameCount = false;
242     m_hasUniformFrameSize = true;
243     return isSizeAvailable();
244 }
245
246 bool BitmapImage::isAllDataReceived() const
247 {
248     return m_allDataReceived;
249 }
250
251 bool BitmapImage::hasColorProfile() const
252 {
253     return m_source.hasColorProfile();
254 }
255
256 String BitmapImage::filenameExtension() const
257 {
258     return m_source.filenameExtension();
259 }
260
261 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode)
262 {
263     draw(ctxt, dstRect, srcRect, compositeOp, blendMode, DoNotRespectImageOrientation);
264 }
265
266 void BitmapImage::draw(GraphicsContext* ctxt, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator compositeOp, blink::WebBlendMode blendMode, RespectImageOrientationEnum shouldRespectImageOrientation)
267 {
268     // Spin the animation to the correct frame before we try to draw it, so we
269     // don't draw an old frame and then immediately need to draw a newer one,
270     // causing flicker and wasting CPU.
271     startAnimation();
272
273     RefPtr<NativeImageSkia> bm = nativeImageForCurrentFrame();
274     if (!bm)
275         return; // It's too early and we don't have an image yet.
276
277     FloatRect normDstRect = adjustForNegativeSize(dstRect);
278     FloatRect normSrcRect = adjustForNegativeSize(srcRect);
279     normSrcRect.intersect(FloatRect(0, 0, bm->bitmap().width(), bm->bitmap().height()));
280
281     if (normSrcRect.isEmpty() || normDstRect.isEmpty())
282         return; // Nothing to draw.
283
284     ImageOrientation orientation = DefaultImageOrientation;
285     if (shouldRespectImageOrientation == RespectImageOrientation)
286         orientation = frameOrientationAtIndex(m_currentFrame);
287
288     GraphicsContextStateSaver saveContext(*ctxt, false);
289     if (orientation != DefaultImageOrientation) {
290         saveContext.save();
291
292         // ImageOrientation expects the origin to be at (0, 0)
293         ctxt->translate(normDstRect.x(), normDstRect.y());
294         normDstRect.setLocation(FloatPoint());
295
296         ctxt->concatCTM(orientation.transformFromDefault(normDstRect.size()));
297
298         if (orientation.usesWidthAsHeight()) {
299             // The destination rect will have it's width and height already reversed for the orientation of
300             // the image, as it was needed for page layout, so we need to reverse it back here.
301             normDstRect = FloatRect(normDstRect.x(), normDstRect.y(), normDstRect.height(), normDstRect.width());
302         }
303     }
304
305     bm->draw(ctxt, normSrcRect, normDstRect, WebCoreCompositeToSkiaComposite(compositeOp, blendMode));
306
307     if (ImageObserver* observer = imageObserver())
308         observer->didDraw(this);
309 }
310
311 size_t BitmapImage::frameCount()
312 {
313     if (!m_haveFrameCount) {
314         m_frameCount = m_source.frameCount();
315         // If decoder is not initialized yet, m_source.frameCount() returns 0.
316         if (m_frameCount) {
317             m_haveFrameCount = true;
318         }
319     }
320     return m_frameCount;
321 }
322
323 bool BitmapImage::isSizeAvailable()
324 {
325     if (m_sizeAvailable)
326         return true;
327
328     m_sizeAvailable = m_source.isSizeAvailable();
329
330     return m_sizeAvailable;
331 }
332
333 bool BitmapImage::ensureFrameIsCached(size_t index)
334 {
335     if (index >= frameCount())
336         return false;
337
338     if (index >= m_frames.size() || !m_frames[index].m_frame)
339         cacheFrame(index);
340     return true;
341 }
342
343 PassRefPtr<NativeImageSkia> BitmapImage::frameAtIndex(size_t index)
344 {
345     if (!ensureFrameIsCached(index))
346         return nullptr;
347     return m_frames[index].m_frame;
348 }
349
350 bool BitmapImage::frameIsCompleteAtIndex(size_t index)
351 {
352     if (index < m_frames.size() && m_frames[index].m_haveMetadata && m_frames[index].m_isComplete)
353         return true;
354     return m_source.frameIsCompleteAtIndex(index);
355 }
356
357 float BitmapImage::frameDurationAtIndex(size_t index)
358 {
359     if (index < m_frames.size() && m_frames[index].m_haveMetadata)
360         return m_frames[index].m_duration;
361     return m_source.frameDurationAtIndex(index);
362 }
363
364 PassRefPtr<NativeImageSkia> BitmapImage::nativeImageForCurrentFrame()
365 {
366     return frameAtIndex(currentFrame());
367 }
368
369 bool BitmapImage::frameHasAlphaAtIndex(size_t index)
370 {
371     if (m_frames.size() <= index)
372         return true;
373
374     if (m_frames[index].m_haveMetadata)
375         return m_frames[index].m_hasAlpha;
376
377     return m_source.frameHasAlphaAtIndex(index);
378 }
379
380 bool BitmapImage::currentFrameKnownToBeOpaque()
381 {
382     return !frameHasAlphaAtIndex(currentFrame());
383 }
384
385 ImageOrientation BitmapImage::currentFrameOrientation()
386 {
387     return frameOrientationAtIndex(currentFrame());
388 }
389
390 ImageOrientation BitmapImage::frameOrientationAtIndex(size_t index)
391 {
392     if (m_frames.size() <= index)
393         return DefaultImageOrientation;
394
395     if (m_frames[index].m_haveMetadata)
396         return m_frames[index].m_orientation;
397
398     return m_source.orientationAtIndex(index);
399 }
400
401 #if !ASSERT_DISABLED
402 bool BitmapImage::notSolidColor()
403 {
404     return size().width() != 1 || size().height() != 1 || frameCount() > 1;
405 }
406 #endif
407
408
409
410 int BitmapImage::repetitionCount(bool imageKnownToBeComplete)
411 {
412     if ((m_repetitionCountStatus == Unknown) || ((m_repetitionCountStatus == Uncertain) && imageKnownToBeComplete)) {
413         // Snag the repetition count.  If |imageKnownToBeComplete| is false, the
414         // repetition count may not be accurate yet for GIFs; in this case the
415         // decoder will default to cAnimationLoopOnce, and we'll try and read
416         // the count again once the whole image is decoded.
417         m_repetitionCount = m_source.repetitionCount();
418         m_repetitionCountStatus = (imageKnownToBeComplete || m_repetitionCount == cAnimationNone) ? Certain : Uncertain;
419     }
420     return m_repetitionCount;
421 }
422
423 bool BitmapImage::shouldAnimate()
424 {
425     return (repetitionCount(false) != cAnimationNone && !m_animationFinished && imageObserver());
426 }
427
428 void BitmapImage::startAnimation(CatchUpAnimation catchUpIfNecessary)
429 {
430     if (m_frameTimer || !shouldAnimate() || frameCount() <= 1)
431         return;
432
433     // If we aren't already animating, set now as the animation start time.
434     const double time = monotonicallyIncreasingTime();
435     if (!m_desiredFrameStartTime)
436         m_desiredFrameStartTime = time;
437
438     // Don't advance the animation to an incomplete frame.
439     size_t nextFrame = (m_currentFrame + 1) % frameCount();
440     if (!m_allDataReceived && !frameIsCompleteAtIndex(nextFrame))
441         return;
442
443     // Don't advance past the last frame if we haven't decoded the whole image
444     // yet and our repetition count is potentially unset.  The repetition count
445     // in a GIF can potentially come after all the rest of the image data, so
446     // wait on it.
447     if (!m_allDataReceived && repetitionCount(false) == cAnimationLoopOnce && m_currentFrame >= (frameCount() - 1))
448         return;
449
450     // Determine time for next frame to start.  By ignoring paint and timer lag
451     // in this calculation, we make the animation appear to run at its desired
452     // rate regardless of how fast it's being repainted.
453     const double currentDuration = frameDurationAtIndex(m_currentFrame);
454     m_desiredFrameStartTime += currentDuration;
455
456     // When an animated image is more than five minutes out of date, the
457     // user probably doesn't care about resyncing and we could burn a lot of
458     // time looping through frames below.  Just reset the timings.
459     const double cAnimationResyncCutoff = 5 * 60;
460     if ((time - m_desiredFrameStartTime) > cAnimationResyncCutoff)
461         m_desiredFrameStartTime = time + currentDuration;
462
463     // The image may load more slowly than it's supposed to animate, so that by
464     // the time we reach the end of the first repetition, we're well behind.
465     // Clamp the desired frame start time in this case, so that we don't skip
466     // frames (or whole iterations) trying to "catch up".  This is a tradeoff:
467     // It guarantees users see the whole animation the second time through and
468     // don't miss any repetitions, and is closer to what other browsers do; on
469     // the other hand, it makes animations "less accurate" for pages that try to
470     // sync an image and some other resource (e.g. audio), especially if users
471     // switch tabs (and thus stop drawing the animation, which will pause it)
472     // during that initial loop, then switch back later.
473     if (nextFrame == 0 && m_repetitionsComplete == 0 && m_desiredFrameStartTime < time)
474         m_desiredFrameStartTime = time;
475
476     if (catchUpIfNecessary == DoNotCatchUp || time < m_desiredFrameStartTime) {
477         // Haven't yet reached time for next frame to start; delay until then.
478         m_frameTimer = new Timer<BitmapImage>(this, &BitmapImage::advanceAnimation);
479         m_frameTimer->startOneShot(std::max(m_desiredFrameStartTime - time, 0.), FROM_HERE);
480     } else {
481         // We've already reached or passed the time for the next frame to start.
482         // See if we've also passed the time for frames after that to start, in
483         // case we need to skip some frames entirely.  Remember not to advance
484         // to an incomplete frame.
485         for (size_t frameAfterNext = (nextFrame + 1) % frameCount(); frameIsCompleteAtIndex(frameAfterNext); frameAfterNext = (nextFrame + 1) % frameCount()) {
486             // Should we skip the next frame?
487             double frameAfterNextStartTime = m_desiredFrameStartTime + frameDurationAtIndex(nextFrame);
488             if (time < frameAfterNextStartTime)
489                 break;
490
491             // Yes; skip over it without notifying our observers.
492             if (!internalAdvanceAnimation(true))
493                 return;
494             m_desiredFrameStartTime = frameAfterNextStartTime;
495             nextFrame = frameAfterNext;
496         }
497
498         // Draw the next frame immediately.  Note that m_desiredFrameStartTime
499         // may be in the past, meaning the next time through this function we'll
500         // kick off the next advancement sooner than this frame's duration would
501         // suggest.
502         if (internalAdvanceAnimation(false)) {
503             // The image region has been marked dirty, but once we return to our
504             // caller, draw() will clear it, and nothing will cause the
505             // animation to advance again.  We need to start the timer for the
506             // next frame running, or the animation can hang.  (Compare this
507             // with when advanceAnimation() is called, and the region is dirtied
508             // while draw() is not in the callstack, meaning draw() gets called
509             // to update the region and thus startAnimation() is reached again.)
510             // NOTE: For large images with slow or heavily-loaded systems,
511             // throwing away data as we go (see destroyDecodedData()) means we
512             // can spend so much time re-decoding data above that by the time we
513             // reach here we're behind again.  If we let startAnimation() run
514             // the catch-up code again, we can get long delays without painting
515             // as we race the timer, or even infinite recursion.  In this
516             // situation the best we can do is to simply change frames as fast
517             // as possible, so force startAnimation() to set a zero-delay timer
518             // and bail out if we're not caught up.
519             startAnimation(DoNotCatchUp);
520         }
521     }
522 }
523
524 void BitmapImage::stopAnimation()
525 {
526     // This timer is used to animate all occurrences of this image.  Don't invalidate
527     // the timer unless all renderers have stopped drawing.
528     delete m_frameTimer;
529     m_frameTimer = 0;
530 }
531
532 void BitmapImage::resetAnimation()
533 {
534     stopAnimation();
535     m_currentFrame = 0;
536     m_repetitionsComplete = 0;
537     m_desiredFrameStartTime = 0;
538     m_animationFinished = false;
539
540     // For extremely large animations, when the animation is reset, we just throw everything away.
541     destroyDecodedDataIfNecessary();
542 }
543
544 bool BitmapImage::maybeAnimated()
545 {
546     if (m_animationFinished)
547         return false;
548     if (frameCount() > 1)
549         return true;
550     return m_source.repetitionCount() != cAnimationNone;
551 }
552
553 void BitmapImage::advanceAnimation(Timer<BitmapImage>*)
554 {
555     internalAdvanceAnimation(false);
556     // At this point the image region has been marked dirty, and if it's
557     // onscreen, we'll soon make a call to draw(), which will call
558     // startAnimation() again to keep the animation moving.
559 }
560
561 bool BitmapImage::internalAdvanceAnimation(bool skippingFrames)
562 {
563     // Stop the animation.
564     stopAnimation();
565
566     // See if anyone is still paying attention to this animation.  If not, we don't
567     // advance and will remain suspended at the current frame until the animation is resumed.
568     if (!skippingFrames && imageObserver()->shouldPauseAnimation(this))
569         return false;
570
571     ++m_currentFrame;
572     bool advancedAnimation = true;
573     if (m_currentFrame >= frameCount()) {
574         ++m_repetitionsComplete;
575
576         // Get the repetition count again.  If we weren't able to get a
577         // repetition count before, we should have decoded the whole image by
578         // now, so it should now be available.
579         // Note that we don't need to special-case cAnimationLoopOnce here
580         // because it is 0 (see comments on its declaration in ImageSource.h).
581         if (repetitionCount(true) != cAnimationLoopInfinite && m_repetitionsComplete > m_repetitionCount) {
582             m_animationFinished = true;
583             m_desiredFrameStartTime = 0;
584             --m_currentFrame;
585             advancedAnimation = false;
586         } else
587             m_currentFrame = 0;
588     }
589     destroyDecodedDataIfNecessary();
590
591     // We need to draw this frame if we advanced to it while not skipping, or if
592     // while trying to skip frames we hit the last frame and thus had to stop.
593     if (skippingFrames != advancedAnimation)
594         imageObserver()->animationAdvanced(this);
595     return advancedAnimation;
596 }
597
598 void BitmapImage::checkForSolidColor()
599 {
600     m_isSolidColor = false;
601     m_checkedForSolidColor = true;
602
603     if (frameCount() > 1)
604         return;
605
606     RefPtr<NativeImageSkia> frame = frameAtIndex(0);
607
608     if (frame && size().width() == 1 && size().height() == 1) {
609         SkAutoLockPixels lock(frame->bitmap());
610         if (!frame->bitmap().getPixels())
611             return;
612
613         m_isSolidColor = true;
614         m_solidColor = Color(frame->bitmap().getColor(0, 0));
615     }
616 }
617
618 bool BitmapImage::mayFillWithSolidColor()
619 {
620     if (!m_checkedForSolidColor && frameCount() > 0) {
621         checkForSolidColor();
622         ASSERT(m_checkedForSolidColor);
623     }
624     return m_isSolidColor && !m_currentFrame;
625 }
626
627 Color BitmapImage::solidColor() const
628 {
629     return m_solidColor;
630 }
631
632 }