Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚮 🐛 [amp story 360] Remove GL context handling #38144

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 1 addition & 54 deletions extensions/amp-story-360/0.1/amp-story-360.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ const HAVE_CURRENT_DATA = 2;
*/
const CENTER_OFFSET = 90;

/**
* Minimum distance from active page to activate WebGL context.
* @const {number}
*/
const MIN_WEBGL_DISTANCE = 2;

/**
* Renders the template for the permission button.
* @return {!Element}
Expand Down Expand Up @@ -274,9 +268,6 @@ export class AmpStory360 extends AMP.BaseElement {
/** @private {boolean} */
this.isOnActivePage_ = false;

/** @private {?number} */
this.distance_ = null;

/** @private {number} */
this.sceneHeading_ = 0;

Expand All @@ -295,9 +286,6 @@ export class AmpStory360 extends AMP.BaseElement {
/** @private {number} */
this.headingOffset_ = 0;

/** @private WebGL extension for lost context. */
this.lostGlContext_ = null;

/** @private {!Array<number>} */
this.rot_ = null;
}
Expand Down Expand Up @@ -349,18 +337,6 @@ export class AmpStory360 extends AMP.BaseElement {
container.appendChild(this.canvas_);
applyFillContent(container, /* replacedContent */ true);

// Mutation observer for distance attribute
const config = {attributes: true, attributeFilter: ['distance']};
const callback = (mutationsList) => {
this.distance_ = parseInt(
mutationsList[0].target.getAttribute('distance'),
10
);
this.restoreOrLoseGlContext_();
};
const observer = new MutationObserver(callback);
this.getPage_() && observer.observe(this.getPage_(), config);

// Initialize all services before proceeding
return Promise.all([
Services.storyStoreServiceForOrNull(this.win).then((storeService) => {
Expand Down Expand Up @@ -431,20 +407,6 @@ export class AmpStory360 extends AMP.BaseElement {
}
}

/** @private */
restoreOrLoseGlContext_() {
if (!this.renderer_) {
return;
}
if (this.distance_ < MIN_WEBGL_DISTANCE) {
if (this.renderer_.gl.isContextLost()) {
this.lostGlContext_.restoreContext();
}
} else if (!this.renderer_.gl.isContextLost()) {
this.lostGlContext_.loseContext();
}
}

/**
* @param {string} permissionState
* @private
Expand Down Expand Up @@ -510,7 +472,7 @@ export class AmpStory360 extends AMP.BaseElement {
// Debounce onDeviceOrientation_ to rAF.
let rafTimeout;
this.win.addEventListener('deviceorientation', (e) => {
if (this.isReady_ && this.distance_ < MIN_WEBGL_DISTANCE) {
if (this.isReady_) {
rafTimeout && this.win.cancelAnimationFrame(rafTimeout);
rafTimeout = this.win.requestAnimationFrame(() => {
if (!this.isOnActivePage_) {
Expand Down Expand Up @@ -711,7 +673,6 @@ export class AmpStory360 extends AMP.BaseElement {
.then(
() => {
this.renderer_ = new Renderer(this.canvas_);
this.setupGlContextListeners_();
this.image_ = this.checkImageReSize_(
dev().assertElement(this.element.querySelector('img'))
);
Expand Down Expand Up @@ -742,26 +703,12 @@ export class AmpStory360 extends AMP.BaseElement {
.then(
() => {
this.renderer_ = new Renderer(this.canvas_);
this.setupGlContextListeners_();
this.initRenderer_();
},
() => user().error(TAG, 'Failed to load the amp-video.')
);
}

/** @private */
setupGlContextListeners_() {
this.lostGlContext_ = this.renderer_.gl.getExtension('WEBGL_lose_context');
this.renderer_.canvas.addEventListener('webglcontextlost', (e) => {
// Calling preventDefault is necessary for restoring context.
e.preventDefault();
this.isReady_ = false;
});
this.renderer_.canvas.addEventListener('webglcontextrestored', () =>
this.initRenderer_()
);
}

/** @private */
initRenderer_() {
this.renderer_.init();
Expand Down