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

Side by Side Diff: third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.cpp

Issue 2849463005: Refactor ImageBuffer to make OffscreenCanvas match HTMLCanvasElement (Closed)
Patch Set: x Created 3 years, 7 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
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc.
3 * All rights reserved. 3 * All rights reserved.
4 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 4 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
5 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 5 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 6 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
7 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 7 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
8 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 8 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
9 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 9 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
10 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. 10 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 DCHECK_EQ(static_cast<size_t>(sk_canvas->getSaveCount()), 160 DCHECK_EQ(static_cast<size_t>(sk_canvas->getSaveCount()),
161 state_stack_.size() + 1); 161 state_stack_.size() + 1);
162 } 162 }
163 } 163 }
164 #endif 164 #endif
165 CHECK(state_stack_.front() 165 CHECK(state_stack_.front()
166 .Get()); // Temporary for investigating crbug.com/648510 166 .Get()); // Temporary for investigating crbug.com/648510
167 } 167 }
168 168
169 bool CanvasRenderingContext2D::IsAccelerated() const { 169 bool CanvasRenderingContext2D::IsAccelerated() const {
170 if (!canvas()->HasImageBuffer()) 170 if (!host()->GetImageBuffer())
171 return false; 171 return false;
172 return canvas()->Buffer()->IsAccelerated(); 172 return host()->GetImageBuffer()->IsAccelerated();
173 } 173 }
174 174
175 bool CanvasRenderingContext2D::IsComposited() const { 175 bool CanvasRenderingContext2D::IsComposited() const {
176 return IsAccelerated(); 176 return IsAccelerated();
177 } 177 }
178 178
179 void CanvasRenderingContext2D::Stop() { 179 void CanvasRenderingContext2D::Stop() {
180 if (!isContextLost()) { 180 if (!isContextLost()) {
181 // Never attempt to restore the context because the page is being torn down. 181 // Never attempt to restore the context because the page is being torn down.
182 LoseContext(kSyntheticLostContext); 182 LoseContext(kSyntheticLostContext);
183 } 183 }
184 } 184 }
185 185
186 bool CanvasRenderingContext2D::isContextLost() const { 186 bool CanvasRenderingContext2D::isContextLost() const {
187 return context_lost_mode_ != kNotLostContext; 187 return context_lost_mode_ != kNotLostContext;
188 } 188 }
189 189
190 void CanvasRenderingContext2D::LoseContext(LostContextMode lost_mode) { 190 void CanvasRenderingContext2D::LoseContext(LostContextMode lost_mode) {
191 if (context_lost_mode_ != kNotLostContext) 191 if (context_lost_mode_ != kNotLostContext)
192 return; 192 return;
193 context_lost_mode_ = lost_mode; 193 context_lost_mode_ = lost_mode;
194 if (context_lost_mode_ == kSyntheticLostContext && canvas()) { 194 if (context_lost_mode_ == kSyntheticLostContext && canvas()) {
195 canvas()->DiscardImageBuffer(); 195 host()->DiscardImageBuffer();
196 } 196 }
197 dispatch_context_lost_event_timer_.StartOneShot(0, BLINK_FROM_HERE); 197 dispatch_context_lost_event_timer_.StartOneShot(0, BLINK_FROM_HERE);
198 } 198 }
199 199
200 void CanvasRenderingContext2D::DidSetSurfaceSize() { 200 void CanvasRenderingContext2D::DidSetSurfaceSize() {
201 if (!context_restorable_) 201 if (!context_restorable_)
202 return; 202 return;
203 // This code path is for restoring from an eviction 203 // This code path is for restoring from an eviction
204 // Restoring from surface failure is handled internally 204 // Restoring from surface failure is handled internally
205 DCHECK(context_lost_mode_ != kNotLostContext && !canvas()->HasImageBuffer()); 205 DCHECK(context_lost_mode_ != kNotLostContext && !host()->GetImageBuffer());
206 206
207 if (canvas()->Buffer()) { 207 if (host()->GetOrCreateImageBuffer()) {
208 if (ContextLostRestoredEventsEnabled()) { 208 if (ContextLostRestoredEventsEnabled()) {
209 dispatch_context_restored_event_timer_.StartOneShot(0, BLINK_FROM_HERE); 209 dispatch_context_restored_event_timer_.StartOneShot(0, BLINK_FROM_HERE);
210 } else { 210 } else {
211 // legacy synchronous context restoration. 211 // legacy synchronous context restoration.
212 Reset(); 212 Reset();
213 context_lost_mode_ = kNotLostContext; 213 context_lost_mode_ = kNotLostContext;
214 } 214 }
215 } 215 }
216 } 216 }
217 217
(...skipping 25 matching lines...) Expand all
243 243
244 void CanvasRenderingContext2D::TryRestoreContextEvent(TimerBase* timer) { 244 void CanvasRenderingContext2D::TryRestoreContextEvent(TimerBase* timer) {
245 if (context_lost_mode_ == kNotLostContext) { 245 if (context_lost_mode_ == kNotLostContext) {
246 // Canvas was already restored (possibly thanks to a resize), so stop 246 // Canvas was already restored (possibly thanks to a resize), so stop
247 // trying. 247 // trying.
248 try_restore_context_event_timer_.Stop(); 248 try_restore_context_event_timer_.Stop();
249 return; 249 return;
250 } 250 }
251 251
252 DCHECK(context_lost_mode_ == kRealLostContext); 252 DCHECK(context_lost_mode_ == kRealLostContext);
253 if (canvas()->HasImageBuffer() && canvas()->Buffer()->RestoreSurface()) { 253 if (host()->GetImageBuffer() && host()->GetImageBuffer()->RestoreSurface()) {
254 try_restore_context_event_timer_.Stop(); 254 try_restore_context_event_timer_.Stop();
255 DispatchContextRestoredEvent(nullptr); 255 DispatchContextRestoredEvent(nullptr);
256 } 256 }
257 257
258 if (++try_restore_context_attempt_count_ > kMaxTryRestoreContextAttempts) { 258 if (++try_restore_context_attempt_count_ > kMaxTryRestoreContextAttempts) {
259 // final attempt: allocate a brand new image buffer instead of restoring 259 // final attempt: allocate a brand new image buffer instead of restoring
260 canvas()->DiscardImageBuffer(); 260 host()->DiscardImageBuffer();
261 try_restore_context_event_timer_.Stop(); 261 try_restore_context_event_timer_.Stop();
262 if (canvas()->Buffer()) 262 if (host()->GetOrCreateImageBuffer())
263 DispatchContextRestoredEvent(nullptr); 263 DispatchContextRestoredEvent(nullptr);
264 } 264 }
265 } 265 }
266 266
267 void CanvasRenderingContext2D::DispatchContextRestoredEvent(TimerBase*) { 267 void CanvasRenderingContext2D::DispatchContextRestoredEvent(TimerBase*) {
268 if (context_lost_mode_ == kNotLostContext) 268 if (context_lost_mode_ == kNotLostContext)
269 return; 269 return;
270 Reset(); 270 Reset();
271 context_lost_mode_ = kNotLostContext; 271 context_lost_mode_ = kNotLostContext;
272 if (ContextLostRestoredEventsEnabled()) { 272 if (ContextLostRestoredEventsEnabled()) {
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 hit_region_manager_->RemoveHitRegionsInRect(rect, GetState().Transform()); 361 hit_region_manager_->RemoveHitRegionsInRect(rect, GetState().Transform());
362 } 362 }
363 } 363 }
364 364
365 void CanvasRenderingContext2D::DidDraw(const SkIRect& dirty_rect) { 365 void CanvasRenderingContext2D::DidDraw(const SkIRect& dirty_rect) {
366 if (dirty_rect.isEmpty()) 366 if (dirty_rect.isEmpty())
367 return; 367 return;
368 368
369 if (ExpensiveCanvasHeuristicParameters::kBlurredShadowsAreExpensive && 369 if (ExpensiveCanvasHeuristicParameters::kBlurredShadowsAreExpensive &&
370 GetState().ShouldDrawShadows() && GetState().ShadowBlur() > 0) { 370 GetState().ShouldDrawShadows() && GetState().ShadowBlur() > 0) {
371 ImageBuffer* buffer = canvas()->Buffer(); 371 ImageBuffer* buffer = host()->GetOrCreateImageBuffer();
372 if (buffer) 372 if (buffer)
373 buffer->SetHasExpensiveOp(); 373 buffer->SetHasExpensiveOp();
374 } 374 }
375 375
376 CanvasRenderingContext::DidDraw(dirty_rect); 376 CanvasRenderingContext::DidDraw(dirty_rect);
377 } 377 }
378 378
379 bool CanvasRenderingContext2D::StateHasFilter() { 379 bool CanvasRenderingContext2D::StateHasFilter() {
380 return GetState().HasFilter(canvas(), canvas()->Size(), this); 380 return GetState().HasFilter(canvas(), canvas()->Size(), this);
381 } 381 }
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 595
596 int CanvasRenderingContext2D::Width() const { 596 int CanvasRenderingContext2D::Width() const {
597 return canvas()->width(); 597 return canvas()->width();
598 } 598 }
599 599
600 int CanvasRenderingContext2D::Height() const { 600 int CanvasRenderingContext2D::Height() const {
601 return canvas()->height(); 601 return canvas()->height();
602 } 602 }
603 603
604 bool CanvasRenderingContext2D::HasImageBuffer() const { 604 bool CanvasRenderingContext2D::HasImageBuffer() const {
605 return canvas()->HasImageBuffer(); 605 return host()->GetImageBuffer();
606 } 606 }
607 607
608 ImageBuffer* CanvasRenderingContext2D::GetImageBuffer() const { 608 ImageBuffer* CanvasRenderingContext2D::GetImageBuffer() const {
609 return canvas()->Buffer(); 609 return const_cast<CanvasRenderingContextHost*>(host())
610 ->GetOrCreateImageBuffer();
610 } 611 }
611 612
612 PassRefPtr<Image> blink::CanvasRenderingContext2D::GetImage( 613 PassRefPtr<Image> blink::CanvasRenderingContext2D::GetImage(
613 AccelerationHint hint, 614 AccelerationHint hint,
614 SnapshotReason reason) const { 615 SnapshotReason reason) const {
615 if (!HasImageBuffer()) 616 if (!HasImageBuffer())
616 return nullptr; 617 return nullptr;
617 return canvas()->Buffer()->NewImageSnapshot(hint, reason); 618 return host()->GetOrCreateImageBuffer()->NewImageSnapshot(hint, reason);
618 } 619 }
619 620
620 bool CanvasRenderingContext2D::ParseColorOrCurrentColor( 621 bool CanvasRenderingContext2D::ParseColorOrCurrentColor(
621 Color& color, 622 Color& color,
622 const String& color_string) const { 623 const String& color_string) const {
623 return ::blink::ParseColorOrCurrentColor(color, color_string, canvas()); 624 return ::blink::ParseColorOrCurrentColor(color, color_string, canvas());
624 } 625 }
625 626
626 HitTestCanvasResult* CanvasRenderingContext2D::GetControlAndIdIfHitRegionExists( 627 HitTestCanvasResult* CanvasRenderingContext2D::GetControlAndIdIfHitRegionExists(
627 const LayoutPoint& location) { 628 const LayoutPoint& location) {
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 : -font_metrics.Descent() + font_metrics.Height() / 2; 973 : -font_metrics.Descent() + font_metrics.Height() / 2;
973 case kAlphabeticTextBaseline: 974 case kAlphabeticTextBaseline:
974 default: 975 default:
975 // Do nothing. 976 // Do nothing.
976 break; 977 break;
977 } 978 }
978 return 0; 979 return 0;
979 } 980 }
980 981
981 void CanvasRenderingContext2D::SetIsHidden(bool hidden) { 982 void CanvasRenderingContext2D::SetIsHidden(bool hidden) {
982 if (canvas()->HasImageBuffer()) 983 if (host()->GetImageBuffer())
983 canvas()->Buffer()->SetIsHidden(hidden); 984 host()->GetImageBuffer()->SetIsHidden(hidden);
984 if (hidden) { 985 if (hidden) {
985 PruneLocalFontCache(0); 986 PruneLocalFontCache(0);
986 } 987 }
987 } 988 }
988 989
989 bool CanvasRenderingContext2D::IsTransformInvertible() const { 990 bool CanvasRenderingContext2D::IsTransformInvertible() const {
990 return GetState().IsTransformInvertible(); 991 return GetState().IsTransformInvertible();
991 } 992 }
992 993
993 WebLayer* CanvasRenderingContext2D::PlatformLayer() const { 994 WebLayer* CanvasRenderingContext2D::PlatformLayer() const {
994 return canvas()->Buffer() ? canvas()->Buffer()->PlatformLayer() : 0; 995 return host()->GetOrCreateImageBuffer()
996 ? host()->GetImageBuffer()->PlatformLayer()
997 : 0;
995 } 998 }
996 999
997 void CanvasRenderingContext2D::getContextAttributes( 1000 void CanvasRenderingContext2D::getContextAttributes(
998 CanvasRenderingContext2DSettings& settings) const { 1001 CanvasRenderingContext2DSettings& settings) const {
999 settings.setAlpha(CreationAttributes().alpha()); 1002 settings.setAlpha(CreationAttributes().alpha());
1000 settings.setColorSpace(ColorSpaceAsString()); 1003 settings.setColorSpace(ColorSpaceAsString());
1001 settings.setPixelFormat(PixelFormatAsString()); 1004 settings.setPixelFormat(PixelFormatAsString());
1002 settings.setLinearPixelMath(color_params().LinearPixelMath()); 1005 settings.setLinearPixelMath(color_params().LinearPixelMath());
1003 } 1006 }
1004 1007
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1189 } 1192 }
1190 return true; 1193 return true;
1191 } 1194 }
1192 1195
1193 void CanvasRenderingContext2D::ResetUsageTracking() { 1196 void CanvasRenderingContext2D::ResetUsageTracking() {
1194 UsageCounters new_counters; 1197 UsageCounters new_counters;
1195 usage_counters_ = new_counters; 1198 usage_counters_ = new_counters;
1196 } 1199 }
1197 1200
1198 } // namespace blink 1201 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698