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

Side by Side Diff: Source/core/html/canvas/CanvasRenderingContext2D.cpp

Issue 300223009: Implement basic parts of hit regions on canvas2d. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: take clipRegion into account Created 6 years, 6 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. All rights reserved. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies) 3 * Copyright (C) 2008, 2010 Nokia Corporation and/or its subsidiary(-ies)
4 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 4 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org> 5 * Copyright (C) 2008 Eric Seidel <eric@webkit.org>
6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org> 6 * Copyright (C) 2008 Dirk Schulze <krit@webkit.org>
7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved. 7 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved. 8 * Copyright (C) 2012, 2013 Intel Corporation. All rights reserved.
9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved. 9 * Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
10 * 10 *
(...skipping 19 matching lines...) Expand all
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */ 31 */
32 32
33 #include "config.h" 33 #include "config.h"
34 #include "core/html/canvas/CanvasRenderingContext2D.h" 34 #include "core/html/canvas/CanvasRenderingContext2D.h"
35 35
36 #include "CSSPropertyNames.h" 36 #include "CSSPropertyNames.h"
37 #include "bindings/v8/ExceptionMessages.h" 37 #include "bindings/v8/ExceptionMessages.h"
38 #include "bindings/v8/ExceptionState.h" 38 #include "bindings/v8/ExceptionState.h"
39 #include "bindings/v8/ExceptionStatePlaceholder.h" 39 #include "bindings/v8/ExceptionStatePlaceholder.h"
40 #include "core/accessibility/AXObjectCache.h"
41 #include "core/css/CSSFontSelector.h" 40 #include "core/css/CSSFontSelector.h"
42 #include "core/css/parser/BisonCSSParser.h" 41 #include "core/css/parser/BisonCSSParser.h"
43 #include "core/css/StylePropertySet.h" 42 #include "core/css/StylePropertySet.h"
44 #include "core/css/resolver/StyleResolver.h" 43 #include "core/css/resolver/StyleResolver.h"
45 #include "core/dom/ExceptionCode.h" 44 #include "core/dom/ExceptionCode.h"
46 #include "core/fetch/ImageResource.h" 45 #include "core/fetch/ImageResource.h"
47 #include "core/frame/ImageBitmap.h" 46 #include "core/frame/ImageBitmap.h"
48 #include "core/html/HTMLCanvasElement.h" 47 #include "core/html/HTMLCanvasElement.h"
49 #include "core/html/HTMLImageElement.h" 48 #include "core/html/HTMLImageElement.h"
50 #include "core/html/HTMLMediaElement.h" 49 #include "core/html/HTMLMediaElement.h"
(...skipping 28 matching lines...) Expand all
79 static const double TryRestoreContextInterval = 0.5; 78 static const double TryRestoreContextInterval = 0.5;
80 static const unsigned MaxTryRestoreContextAttempts = 4; 79 static const unsigned MaxTryRestoreContextAttempts = 4;
81 80
82 static bool contextLostRestoredEventsEnabled() 81 static bool contextLostRestoredEventsEnabled()
83 { 82 {
84 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled(); 83 return RuntimeEnabledFeatures::experimentalCanvasFeaturesEnabled();
85 } 84 }
86 85
87 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode) 86 CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement* canvas, co nst Canvas2DContextAttributes* attrs, bool usesCSSCompatibilityParseMode)
88 : CanvasRenderingContext(canvas) 87 : CanvasRenderingContext(canvas)
88 , m_hitRegionManager(HitRegionManager::create())
89 , m_usesCSSCompatibilityParseMode(usesCSSCompatibilityParseMode) 89 , m_usesCSSCompatibilityParseMode(usesCSSCompatibilityParseMode)
90 , m_hasAlpha(!attrs || attrs->alpha()) 90 , m_hasAlpha(!attrs || attrs->alpha())
91 , m_isContextLost(false) 91 , m_isContextLost(false)
92 , m_contextRestorable(true) 92 , m_contextRestorable(true)
93 , m_storageMode(!attrs ? PersistentStorage : attrs->parsedStorage()) 93 , m_storageMode(!attrs ? PersistentStorage : attrs->parsedStorage())
94 , m_tryRestoreContextAttemptCount(0) 94 , m_tryRestoreContextAttemptCount(0)
95 , m_dispatchContextLostEventTimer(this, &CanvasRenderingContext2D::dispatchC ontextLostEvent) 95 , m_dispatchContextLostEventTimer(this, &CanvasRenderingContext2D::dispatchC ontextLostEvent)
96 , m_dispatchContextRestoredEventTimer(this, &CanvasRenderingContext2D::dispa tchContextRestoredEvent) 96 , m_dispatchContextRestoredEventTimer(this, &CanvasRenderingContext2D::dispa tchContextRestoredEvent)
97 , m_tryRestoreContextEventTimer(this, &CanvasRenderingContext2D::tryRestoreC ontextEvent) 97 , m_tryRestoreContextEventTimer(this, &CanvasRenderingContext2D::tryRestoreC ontextEvent)
98 { 98 {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 m_isContextLost = false; 161 m_isContextLost = false;
162 } 162 }
163 } 163 }
164 } 164 }
165 165
166 void CanvasRenderingContext2D::trace(Visitor* visitor) 166 void CanvasRenderingContext2D::trace(Visitor* visitor)
167 { 167 {
168 #if ENABLE(OILPAN) 168 #if ENABLE(OILPAN)
169 visitor->trace(m_stateStack); 169 visitor->trace(m_stateStack);
170 visitor->trace(m_fetchedFonts); 170 visitor->trace(m_fetchedFonts);
171 visitor->trace(m_hitRegionManager);
171 #endif 172 #endif
172 CanvasRenderingContext::trace(visitor); 173 CanvasRenderingContext::trace(visitor);
173 } 174 }
174 175
175 void CanvasRenderingContext2D::dispatchContextLostEvent(Timer<CanvasRenderingCon text2D>*) 176 void CanvasRenderingContext2D::dispatchContextLostEvent(Timer<CanvasRenderingCon text2D>*)
176 { 177 {
177 if (contextLostRestoredEventsEnabled()) { 178 if (contextLostRestoredEventsEnabled()) {
178 RefPtrWillBeRawPtr<Event> event = Event::createCancelable(EventTypeNames ::contextlost); 179 RefPtrWillBeRawPtr<Event> event = Event::createCancelable(EventTypeNames ::contextlost);
179 canvas()->dispatchEvent(event); 180 canvas()->dispatchEvent(event);
180 if (event->defaultPrevented()) { 181 if (event->defaultPrevented()) {
(...skipping 1056 matching lines...) Expand 10 before | Expand all | Expand 10 after
1237 context->setAlphaAsFloat(1); 1238 context->setAlphaAsFloat(1);
1238 } 1239 }
1239 if (state().m_globalComposite != CompositeSourceOver) { 1240 if (state().m_globalComposite != CompositeSourceOver) {
1240 if (!saved) { 1241 if (!saved) {
1241 context->save(); 1242 context->save();
1242 saved = true; 1243 saved = true;
1243 } 1244 }
1244 context->setCompositeOperation(CompositeSourceOver); 1245 context->setCompositeOperation(CompositeSourceOver);
1245 } 1246 }
1246 context->clearRect(rect); 1247 context->clearRect(rect);
1248 m_hitRegionManager->removeHitRegionsInRect(rect, state().m_transform);
1247 if (saved) 1249 if (saved)
1248 context->restore(); 1250 context->restore();
1249 1251
1250 validateStateStack(); 1252 validateStateStack();
1251 didDraw(dirtyRect); 1253 didDraw(dirtyRect);
1252 } 1254 }
1253 1255
1254 void CanvasRenderingContext2D::fillRect(float x, float y, float width, float hei ght) 1256 void CanvasRenderingContext2D::fillRect(float x, float y, float width, float hei ght)
1255 { 1257 {
1256 if (!validateRectForCanvas(x, y, width, height)) 1258 if (!validateRectForCanvas(x, y, width, height))
(...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after
2349 c->save(); 2351 c->save();
2350 c->setAlphaAsFloat(1.0); 2352 c->setAlphaAsFloat(1.0);
2351 c->clearShadow(); 2353 c->clearShadow();
2352 c->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal); 2354 c->setCompositeOperation(CompositeSourceOver, blink::WebBlendModeNormal);
2353 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor); 2355 c->drawFocusRing(path, focusRingWidth, focusRingOutline, focusRingColor);
2354 c->restore(); 2356 c->restore();
2355 validateStateStack(); 2357 validateStateStack();
2356 didDraw(dirtyRect); 2358 didDraw(dirtyRect);
2357 } 2359 }
2358 2360
2361 void CanvasRenderingContext2D::addHitRegion(ExceptionState& exceptionState)
2362 {
2363 addHitRegion(Dictionary(), exceptionState);
2364 }
2365
2366 void CanvasRenderingContext2D::addHitRegion(const Dictionary& options, Exception State& exceptionState)
2367 {
2368 HitRegionOptions passOptions;
2369
2370 bool hasId = options.getWithUndefinedOrNullCheck("id", passOptions.id) && !p assOptions.id.isEmpty();
2371 bool hasControl = options.getWithUndefinedOrNullCheck("control", passOptions .control) && passOptions.control;
2372
2373 if (!(hasId || hasControl)) {
2374 exceptionState.throwDOMException(NotSupportedError, "Both id and control are null.");
2375 return;
2376 }
2377
2378 FloatRect clipBounds;
2379 GraphicsContext* context = drawingContext();
2380
2381 if (m_path.isEmpty() || !context || !state().m_invertibleCTM
2382 || !context->getTransformedClipBounds(&clipBounds)) {
2383 exceptionState.throwDOMException(NotSupportedError, "The specified path has no pixels.");
2384 return;
2385 }
2386
2387 // This clipPath isn't always empty.
2388 Path clipPath;
2389 clipPath.addRect(clipBounds);
fs 2014/06/04 08:13:40 clipBounds vs. CRC2D.clip() (i.e. what if it is a
zino 2014/06/06 06:35:17 Done. But I didn't add some tests yet. I'll upload
2390
2391 Path specifiedPath = m_path;
2392 specifiedPath.transform(state().m_transform);
2393 specifiedPath.intersectPath(clipPath);
2394
2395 passOptions.path = specifiedPath;
2396
2397 addHitRegionInternal(passOptions, exceptionState);
2398 }
2399
2400 void CanvasRenderingContext2D::addHitRegionInternal(const HitRegionOptions& opti ons, ExceptionState& exceptionState)
2401 {
2402 // Remove previous region (with id or control)
2403 m_hitRegionManager->removeHitRegionById(options.id);
2404 m_hitRegionManager->removeHitRegionByControl(options.control.get());
2405
2406 RefPtrWillBeRawPtr<HitRegion> hitRegion = HitRegion::create(options);
2407 hitRegion->updateAccessibility(canvas());
2408 m_hitRegionManager->addHitRegion(hitRegion.release());
2409 }
2410
2411 void CanvasRenderingContext2D::removeHitRegion(const String& id)
2412 {
2413 m_hitRegionManager->removeHitRegionById(id);
2414 }
2415
2416 void CanvasRenderingContext2D::clearHitRegions()
2417 {
2418 m_hitRegionManager->removeAllHitRegions();
2419 }
2420
2421 HitRegion* CanvasRenderingContext2D::hitRegionAtPoint(const LayoutPoint& point)
2422 {
2423 return m_hitRegionManager->getHitRegionAtPoint(point);
2424 }
2425
2359 } // namespace WebCore 2426 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698