Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add support for clearing window color before GV does the first compos…
Browse files Browse the repository at this point in the history
…ition
  • Loading branch information
MortimerGoro committed Aug 28, 2019
1 parent b926cae commit 945e1db
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ public boolean isDialog() {

@Override
public void setFirstDraw(final boolean aIsFirstDraw) {
mWidgetPlacement.firstDraw = aIsFirstDraw;
mWidgetPlacement.composited = aIsFirstDraw;
}

@Override
public boolean getFirstDraw() {
return mWidgetPlacement.firstDraw;
return mWidgetPlacement.composited;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public WidgetPlacement(Context aContext) {
public boolean visible = true;
public boolean opaque = false;
public boolean showPointer = true;
public boolean firstDraw = false;
public boolean composited = false;
public boolean layer = true;
public boolean proxifyLayer = false;
public float textureScale = 0.7f;
// Widget will be curved if enabled.
public boolean cylinder = true;
public int borderColor = 0;
// Color used to render the widget before the it's composited
public int clearColor = 0;
/*
* Flat surface placements are automatically mapped to curved coordinates.
* If a radius is set it's used for the automatic mapping of the yaw & angle when the
Expand Down Expand Up @@ -85,7 +87,7 @@ public void copyFrom(WidgetPlacement w) {
this.visible = w.visible;
this.opaque = w.opaque;
this.showPointer = w.showPointer;
this.firstDraw = w.firstDraw;
this.composited = w.composited;
this.layer = w.layer;
this.textureScale = w.textureScale;
this.cylinder = w.cylinder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public WindowWidget(Context aContext, int windowId, boolean privateMode) {
mHandle = ((WidgetManagerDelegate)aContext).newWidgetHandle();
mWidgetPlacement = new WidgetPlacement(aContext);
initializeWidgetPlacement(mWidgetPlacement);
if (mSessionStack.isPrivateMode()) {
mWidgetPlacement.clearColor = ViewUtils.ARGBtoRGBA(getContext().getColor(R.color.window_private_clear_color));
} else {
mWidgetPlacement.clearColor = ViewUtils.ARGBtoRGBA(getContext().getColor(R.color.window_blank_clear_color));
}

mTopBar = new TopBarWidget(aContext);
mTopBar.attachToWindow(this);
Expand Down Expand Up @@ -767,12 +772,12 @@ public void releaseWidget() {

@Override
public void setFirstDraw(final boolean aIsFirstDraw) {
mWidgetPlacement.firstDraw = aIsFirstDraw;
mWidgetPlacement.composited = aIsFirstDraw;
}

@Override
public boolean getFirstDraw() {
return mWidgetPlacement.firstDraw;
return mWidgetPlacement.composited;
}

@Override
Expand Down Expand Up @@ -1280,7 +1285,10 @@ public void onFirstComposite(GeckoSession session) {
if (mFirstDrawCallback != null) {
// Post this call because running it synchronously can cause a deadlock if the runnable
// resizes the widget and calls surfaceChanged. See https://github.com/MozillaReality/FirefoxReality/issues/1459.
ThreadUtils.postToUiThread(mFirstDrawCallback);
// 1s delay has been added because firstComposite callback is received before a Gecko layer is actually rendered
// and that makes the window transparent for a while. Remove the 1s delay when GV is updated with a better callback.
final int delay = 1000;
ThreadUtils.postDelayedToUiThread(mFirstDrawCallback, delay);
mFirstDrawCallback = null;
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) {
if (aPlacement->cylinder && m.cylinderDensity > 0) {
VRLayerCylinderPtr layer = m.device->CreateLayerCylinder(textureWidth, textureHeight, VRLayerQuad::SurfaceType::AndroidSurface);
CylinderPtr cylinder = Cylinder::Create(m.create, layer);
widget = Widget::Create(m.context, aHandle, textureWidth, textureHeight, (int32_t)worldWidth, (int32_t)worldHeight, cylinder);
widget = Widget::Create(m.context, aHandle, aPlacement, textureWidth, textureHeight, (int32_t)worldWidth, (int32_t)worldHeight, cylinder);
}

if (!widget) {
Expand All @@ -864,7 +864,7 @@ BrowserWorld::AddWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement) {
}

QuadPtr quad = Quad::Create(m.create, worldWidth, worldHeight, layer);
widget = Widget::Create(m.context, aHandle, textureWidth, textureHeight, quad);
widget = Widget::Create(m.context, aHandle, aPlacement, textureWidth, textureHeight, quad);
}

if (aPlacement->opaque) {
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/cpp/Quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ Quad::GetWorldHeight() const {
return m.GetWorldHeight();
}

vrb::RenderStatePtr
Quad::GetRenderState() const {
if (m.geometry) {
return m.geometry->GetRenderState();
}
return nullptr;
}

void
Quad::GetWorldSize(float& aWidth, float& aHeight) const {
aWidth = m.worldMax.x() - m.worldMin.x();
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Quad {
const vrb::Vector& GetWorldMax() const;
float GetWorldWidth() const;
float GetWorldHeight() const;
vrb::RenderStatePtr GetRenderState() const;
void GetWorldSize(float& aWidth, float& aHeight) const;
void SetWorldSize(const float aWidth, const float aHeight) const;
void SetWorldSize(const vrb::Vector& aMin, const vrb::Vector& aMax) const;
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/cpp/VRLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct VRLayer::State {
vrb::Matrix modelTransform[2];
vrb::Matrix modelView[2];
device::Eye currentEye;
vrb::Color clearColor;
vrb::Color tintColor;
device::EyeRect textureRect[2];
SurfaceChangedDelegate surfaceChangedDelegate;
Expand All @@ -33,6 +34,7 @@ struct VRLayer::State {
drawRequested(false),
drawInFront(false),
currentEye(device::Eye::Left),
clearColor(0),
tintColor(1.0f, 1.0f, 1.0f, 1.0f)
{
for (int i = 0; i < 2; ++i) {
Expand Down Expand Up @@ -79,6 +81,12 @@ VRLayer::GetPriority() const {
return m.priority;
}


const vrb::Color&
VRLayer::GetClearColor() const {
return m.clearColor;
}

const vrb::Color&
VRLayer::GetTintColor() const {
return m.tintColor;
Expand Down Expand Up @@ -153,8 +161,14 @@ VRLayer::VRLayer(State& aState, LayerType aLayerType): m(aState) {
m.layerType = aLayerType;
}


void
VRLayer::SetClearColor(const vrb::Color& aClearColor) {
m.clearColor = aClearColor;
}

void
VRLayer::SetTintColor(const vrb::Color &aTintColor) {
VRLayer::SetTintColor(const vrb::Color& aTintColor) {
m.tintColor = aTintColor;
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/cpp/VRLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class VRLayer {
const vrb::Matrix& GetView(device::Eye aEye) const;
device::Eye GetCurrentEye() const;
int32_t GetPriority() const;
const vrb::Color& GetClearColor() const;
const vrb::Color& GetTintColor() const;
const device::EyeRect& GetTextureRect(device::Eye aEye) const;
bool GetDrawInFront() const;
Expand All @@ -56,6 +57,7 @@ class VRLayer {
void SetView(device::Eye aEye, const vrb::Matrix& aModelView);
void SetCurrentEye(device::Eye aEye);
void SetPriority(int32_t aPriority);
void SetClearColor(const vrb::Color& aClearColor);
void SetTintColor(const vrb::Color& aTintColor);
void SetTextureRect(device::Eye aEye, const device::EyeRect& aTextureRect);
void SetSurfaceChangedDelegate(const SurfaceChangedDelegate& aDelegate);
Expand Down
63 changes: 40 additions & 23 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
#include "vrb/TextureSurface.h"
#include "vrb/TextureCache.h"
#include "vrb/Toggle.h"
#include "vrb/Transform.h"
#include "vrb/Vector.h"
Expand Down Expand Up @@ -66,15 +67,15 @@ struct Widget::State {
, cylinderDensity(4680.0f)
{}

void Initialize(const int aHandle, const int32_t aTextureWidth, const int32_t aTextureHeight,
void Initialize(const int aHandle, const WidgetPlacementPtr& aPlacement, const int32_t aTextureWidth, const int32_t aTextureHeight,
const QuadPtr& aQuad, const CylinderPtr& aCylinder) {
handle = (uint32_t)aHandle;
name = "crow::Widget-" + std::to_string(handle);
vrb::RenderContextPtr render = context.lock();
if (!render) {
return;
}

placement = aPlacement;
quad = aQuad;
cylinder = aCylinder;

Expand All @@ -92,12 +93,8 @@ struct Widget::State {
transform->AddNode(cylinder->GetRoot());
}

if (GetLayer()) {
toggleState = true;
root->ToggleAll(true);
} else {
root->ToggleAll(false);
}
toggleState = IsReadyForComposition();
root->ToggleAll(toggleState);
}

void UpdateSurface(const int32_t aTextureWidth, const int32_t aTextureHeight) {
Expand All @@ -112,21 +109,32 @@ struct Widget::State {
vrb::RenderContextPtr render = context.lock();
surface = vrb::TextureSurface::Create(render, name);
}

vrb::Color tintColor(1.0f, 1.0f, 1.0f, 1.0f);
std::string customFragment;
if (!placement->composited && placement->GetClearColor().Alpha() > 0.0f) {
customFragment =
#include "shaders/clear_color.fs"
;
tintColor = placement->GetClearColor();
}

if (quad) {
quad->SetTexture(surface, aTextureWidth, aTextureHeight);
quad->SetMaterial(vrb::Color(0.4f, 0.4f, 0.4f), vrb::Color(1.0f, 1.0f, 1.0f), vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
quad->GetRenderState()->SetCustomFragmentShader(customFragment);
quad->GetRenderState()->SetTintColor(tintColor);
} else if (cylinder) {
cylinder->SetTexture(surface, aTextureWidth, aTextureHeight);
cylinder->SetMaterial(vrb::Color(0.4f, 0.4f, 0.4f), vrb::Color(1.0f, 1.0f, 1.0f), vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
cylinder->GetRenderState()->SetCustomFragmentShader(customFragment);
cylinder->GetRenderState()->SetTintColor(tintColor);
}
}
}

bool FirstDraw() {
if (!placement) {
return false;
}
return placement->firstDraw;
bool IsReadyForComposition() {
return GetLayer() || placement->composited || placement->GetClearColor().Alpha() > 0;
}

const VRLayerSurfacePtr GetLayer() {
Expand Down Expand Up @@ -198,21 +206,21 @@ struct Widget::State {
};

WidgetPtr
Widget::Create(vrb::RenderContextPtr& aContext, const int aHandle,
Widget::Create(vrb::RenderContextPtr& aContext, const int aHandle, const WidgetPlacementPtr& aPlacement,
const int32_t aTextureWidth, const int32_t aTextureHeight,const QuadPtr& aQuad) {
WidgetPtr result = std::make_shared<vrb::ConcreteClass<Widget, Widget::State> >(aContext);
aQuad->GetWorldMinAndMax(result->m.min, result->m.max);
result->m.Initialize(aHandle, aTextureWidth, aTextureHeight, aQuad, nullptr);
result->m.Initialize(aHandle, aPlacement, aTextureWidth, aTextureHeight, aQuad, nullptr);
return result;
}

WidgetPtr
Widget::Create(vrb::RenderContextPtr& aContext, const int aHandle, const float aWorldWidth, const float aWorldHeight,
Widget::Create(vrb::RenderContextPtr& aContext, const int aHandle, const WidgetPlacementPtr& aPlacement, const float aWorldWidth, const float aWorldHeight,
const int32_t aTextureWidth, const int32_t aTextureHeight, const CylinderPtr& aCylinder) {
WidgetPtr result = std::make_shared<vrb::ConcreteClass<Widget, Widget::State> >(aContext);
result->m.min = vrb::Vector(-aWorldWidth * 0.5f, -aWorldHeight * 0.5f, 0.0f);
result->m.max = vrb::Vector(aWorldWidth * 0.5f, aWorldHeight * 0.5f, 0.0f);
result->m.Initialize(aHandle, aTextureWidth, aTextureHeight, nullptr, aCylinder);
result->m.max = vrb::Vector(aWorldWidth *0.5f, aWorldHeight * 0.5f, 0.0f);
result->m.Initialize(aHandle, aPlacement, aTextureWidth, aTextureHeight, nullptr, aCylinder);
return result;
}

Expand All @@ -224,7 +232,7 @@ Widget::GetHandle() const {
void
Widget::ResetFirstDraw() {
if (m.placement) {
m.placement->firstDraw = false;
m.placement->composited = false;
}
if (m.root) {
m.root->ToggleAll(false);
Expand Down Expand Up @@ -352,7 +360,7 @@ Widget::SetTransform(const vrb::Matrix& aTransform) {
void
Widget::ToggleWidget(const bool aEnabled) {
m.toggleState = aEnabled;
m.root->ToggleAll(aEnabled && m.FirstDraw());
m.root->ToggleAll(aEnabled && m.IsReadyForComposition());
}

bool
Expand Down Expand Up @@ -434,13 +442,22 @@ Widget::GetPlacement() const {

void
Widget::SetPlacement(const WidgetPlacementPtr& aPlacement) {
if (!m.FirstDraw() && aPlacement && aPlacement->firstDraw && m.root) {
m.root->ToggleAll(m.toggleState);
}
bool wasComposited = m.placement->composited;
m.placement = aPlacement;
if (!wasComposited && aPlacement->composited && m.root) {
m.root->ToggleAll(m.toggleState);
int32_t textureWidth, textureHeight;
GetSurfaceTextureSize(textureWidth, textureHeight);
m.UpdateSurface(textureWidth, textureHeight);
}
if (m.cylinder) {
m.UpdateCylinderMatrix();
}

VRLayerSurfacePtr layer = GetLayer();
if (layer) {
layer->SetClearColor(aPlacement->clearColor);
}
}

void
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ typedef std::shared_ptr<WidgetPlacement> WidgetPlacementPtr;

class Widget {
public:
static WidgetPtr Create(vrb::RenderContextPtr& aContext, const int aHandle,
static WidgetPtr Create(vrb::RenderContextPtr& aContext, const int aHandle, const WidgetPlacementPtr& aPlacement,
const int32_t aTextureWidth, const int32_t aTextureHeight, const QuadPtr& aQuad);
static WidgetPtr Create(vrb::RenderContextPtr& aContext, const int aHandle, const float aWorldWidth, const float aWorldHeight,
static WidgetPtr Create(vrb::RenderContextPtr& aContext, const int aHandle, const WidgetPlacementPtr& aPlacement, const float aWorldWidth, const float aWorldHeight,
const int32_t aTextureWidth, const int32_t aTextureHeight, const CylinderPtr& aCylinder);
uint32_t GetHandle() const;
void ResetFirstDraw();
Expand Down
22 changes: 4 additions & 18 deletions app/src/main/cpp/WidgetBorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,6 @@

namespace crow {

static const char* sCylinderFragmentShader = R"SHADER(
precision highp float;
uniform sampler2D u_texture0;
varying vec4 v_color;
varying vec2 v_uv;
void main() {
vec4 color = vec4(1.0f, 1.0f, 1.0f, 1.0f);
if ((v_uv.x < 0.0f) || (v_uv.x > 1.0f)) {
color.a = 0.0f;
}
gl_FragColor = color * v_color;
}
)SHADER";

struct WidgetBorder::State {
CylinderPtr cylinder;
vrb::GeometryPtr geometry;
Expand Down Expand Up @@ -156,7 +139,10 @@ WidgetBorderPtr WidgetBorder::Create(vrb::CreationContextPtr& aContext, const vr
// Sometimes there is no handle to hide it (e.g. bottom bar and anchor points != 0.5f)
vrb::TextureGLPtr defaultTexture = aContext->GetDefaultTexture();
result->m.cylinder->SetTexture(defaultTexture, defaultTexture->GetWidth(), defaultTexture->GetHeight());
result->m.cylinder->GetRenderState()->SetCustomFragmentShader(sCylinderFragmentShader);
const std::string customFragment =
#include "shaders/clear_color.fs"
;
result->m.cylinder->GetRenderState()->SetCustomFragmentShader(customFragment);
result->m.transform->AddNode(result->m.cylinder->GetRoot());
} else {
result->m.geometry = result->m.CreateGeometry(aContext, -max, max, aBorderRect);
Expand Down
Loading

0 comments on commit 945e1db

Please sign in to comment.