Bug 1685225 - Use state bits to determine the color for non-native theme meter chunks...
[gecko.git] / gfx / webrender_bindings / RenderAndroidSurfaceTextureHost.cpp
blobc8ba07415a1093503fb68b6ef0c175bbc9321065
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "RenderAndroidSurfaceTextureHost.h"
9 #include "mozilla/gfx/Logging.h"
10 #include "mozilla/webrender/RenderThread.h"
11 #include "GLContext.h"
13 namespace mozilla {
14 namespace wr {
16 RenderAndroidSurfaceTextureHost::RenderAndroidSurfaceTextureHost(
17 const java::GeckoSurfaceTexture::GlobalRef& aSurfTex, gfx::IntSize aSize,
18 gfx::SurfaceFormat aFormat, bool aContinuousUpdate)
19 : mSurfTex(aSurfTex),
20 mSize(aSize),
21 mFormat(aFormat),
22 mContinuousUpdate(aContinuousUpdate),
23 mPrepareStatus(STATUS_NONE),
24 mAttachedToGLContext(false) {
25 MOZ_COUNT_CTOR_INHERITED(RenderAndroidSurfaceTextureHost, RenderTextureHost);
27 if (mSurfTex) {
28 mSurfTex->IncrementUse();
32 RenderAndroidSurfaceTextureHost::~RenderAndroidSurfaceTextureHost() {
33 MOZ_ASSERT(RenderThread::IsInRenderThread());
34 MOZ_COUNT_DTOR_INHERITED(RenderAndroidSurfaceTextureHost, RenderTextureHost);
35 // The SurfaceTexture gets destroyed when its use count reaches zero.
36 if (mSurfTex) {
37 mSurfTex->DecrementUse();
41 wr::WrExternalImage RenderAndroidSurfaceTextureHost::Lock(
42 uint8_t aChannelIndex, gl::GLContext* aGL, wr::ImageRendering aRendering) {
43 MOZ_ASSERT(aChannelIndex == 0);
44 MOZ_ASSERT((mPrepareStatus == STATUS_PREPARED) ||
45 (!mSurfTex->IsSingleBuffer() &&
46 mPrepareStatus == STATUS_UPDATE_TEX_IMAGE_NEEDED));
48 if (mGL.get() != aGL) {
49 // This should not happen. On android, SharedGL is used.
50 MOZ_ASSERT_UNREACHABLE("Unexpected GL context");
51 return InvalidToWrExternalImage();
54 if (!mSurfTex || !mGL || !mGL->MakeCurrent()) {
55 return InvalidToWrExternalImage();
58 MOZ_ASSERT(mAttachedToGLContext);
59 if (!mAttachedToGLContext) {
60 return InvalidToWrExternalImage();
63 if (IsFilterUpdateNecessary(aRendering)) {
64 // Cache new rendering filter.
65 mCachedRendering = aRendering;
66 ActivateBindAndTexParameteri(mGL, LOCAL_GL_TEXTURE0,
67 LOCAL_GL_TEXTURE_EXTERNAL_OES,
68 mSurfTex->GetTexName(), aRendering);
71 if (mContinuousUpdate) {
72 MOZ_ASSERT(!mSurfTex->IsSingleBuffer());
73 mSurfTex->UpdateTexImage();
74 } else if (mPrepareStatus == STATUS_UPDATE_TEX_IMAGE_NEEDED) {
75 MOZ_ASSERT(!mSurfTex->IsSingleBuffer());
76 // When SurfaceTexture is not single buffer mode, call UpdateTexImage() once
77 // just before rendering. During playing video, one SurfaceTexture is used
78 // for all RenderAndroidSurfaceTextureHosts of video.
79 mSurfTex->UpdateTexImage();
80 mPrepareStatus = STATUS_PREPARED;
83 return NativeTextureToWrExternalImage(mSurfTex->GetTexName(), 0, 0,
84 mSize.width, mSize.height);
87 void RenderAndroidSurfaceTextureHost::Unlock() {}
89 bool RenderAndroidSurfaceTextureHost::EnsureAttachedToGLContext() {
90 // During handling WebRenderError, GeckoSurfaceTexture should not be attached
91 // to GLContext.
92 if (RenderThread::Get()->IsHandlingWebRenderError()) {
93 return false;
96 if (mAttachedToGLContext) {
97 return true;
100 if (!mGL) {
101 mGL = RenderThread::Get()->SharedGL();
104 if (!mSurfTex || !mGL || !mGL->MakeCurrent()) {
105 return false;
108 if (!mSurfTex->IsAttachedToGLContext((int64_t)mGL.get())) {
109 GLuint texName;
110 mGL->fGenTextures(1, &texName);
111 ActivateBindAndTexParameteri(mGL, LOCAL_GL_TEXTURE0,
112 LOCAL_GL_TEXTURE_EXTERNAL_OES, texName,
113 mCachedRendering);
115 if (NS_FAILED(mSurfTex->AttachToGLContext((int64_t)mGL.get(), texName))) {
116 MOZ_ASSERT(0);
117 mGL->fDeleteTextures(1, &texName);
118 return false;
122 mAttachedToGLContext = true;
123 return true;
126 void RenderAndroidSurfaceTextureHost::PrepareForUse() {
127 // When SurfaceTexture is single buffer mode, UpdateTexImage needs to be
128 // called only once for each publish. If UpdateTexImage is called more
129 // than once, it causes hang on puglish side. And UpdateTexImage needs to
130 // be called on render thread, since the SurfaceTexture is consumed on render
131 // thread.
132 MOZ_ASSERT(RenderThread::IsInRenderThread());
133 MOZ_ASSERT(mPrepareStatus == STATUS_NONE);
135 if (mContinuousUpdate || !mSurfTex) {
136 return;
139 mPrepareStatus = STATUS_MIGHT_BE_USED_BY_WR;
141 if (mSurfTex->IsSingleBuffer()) {
142 EnsureAttachedToGLContext();
143 // When SurfaceTexture is single buffer mode, it is OK to call
144 // UpdateTexImage() here.
145 mSurfTex->UpdateTexImage();
146 mPrepareStatus = STATUS_PREPARED;
150 void RenderAndroidSurfaceTextureHost::NotifyForUse() {
151 MOZ_ASSERT(RenderThread::IsInRenderThread());
153 if (mPrepareStatus == STATUS_MIGHT_BE_USED_BY_WR) {
154 // This happens when SurfaceTexture of video is rendered on WebRender.
155 // There is a case that SurfaceTexture is not rendered on WebRender, instead
156 // it is rendered to WebGL and the SurfaceTexture should not be attached to
157 // gl context of WebRender. It is ugly. But it is same as Compositor
158 // rendering.
159 MOZ_ASSERT(!mSurfTex->IsSingleBuffer());
160 if (!EnsureAttachedToGLContext()) {
161 return;
163 mPrepareStatus = STATUS_UPDATE_TEX_IMAGE_NEEDED;
167 void RenderAndroidSurfaceTextureHost::NotifyNotUsed() {
168 MOZ_ASSERT(RenderThread::IsInRenderThread());
170 if (!mSurfTex) {
171 MOZ_ASSERT(mPrepareStatus == STATUS_NONE);
172 return;
175 if (mSurfTex->IsSingleBuffer()) {
176 MOZ_ASSERT(mPrepareStatus == STATUS_PREPARED);
177 MOZ_ASSERT(mAttachedToGLContext);
178 // Release SurfaceTexture's buffer to client side.
179 mGL->MakeCurrent();
180 mSurfTex->ReleaseTexImage();
181 } else if (mPrepareStatus == STATUS_UPDATE_TEX_IMAGE_NEEDED) {
182 MOZ_ASSERT(mAttachedToGLContext);
183 // This could happen when video frame was skipped. UpdateTexImage() neeeds
184 // to be called for adjusting SurfaceTexture's buffer status.
185 mSurfTex->UpdateTexImage();
188 mPrepareStatus = STATUS_NONE;
191 } // namespace wr
192 } // namespace mozilla