Skip to content

Commit

Permalink
Draw overlays at the proper time in the frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed May 30, 2017
1 parent 35aefe4 commit 6147448
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
31 changes: 17 additions & 14 deletions UI/NativeApp.cpp
Expand Up @@ -588,6 +588,8 @@ static void UIThemeInit() {
ui_theme.popupStyle = MakeStyle(g_Config.uPopupStyleFg, g_Config.uPopupStyleBg);
}

void RenderOverlays(UIContext *dc, void *userdata);

void NativeInitGraphics(GraphicsContext *graphicsContext) {
ILOG("NativeInitGraphics");

Expand Down Expand Up @@ -652,6 +654,7 @@ void NativeInitGraphics(GraphicsContext *graphicsContext) {

screenManager->setUIContext(uiContext);
screenManager->setDrawContext(g_draw);
screenManager->setPostRenderCallback(&RenderOverlays, nullptr);

UIBackgroundInit(*uiContext);

Expand Down Expand Up @@ -737,7 +740,7 @@ void TakeScreenshot() {
#endif
}

void DrawDownloadsOverlay(UIContext &dc) {
void RenderOverlays(UIContext *dc, void *userdata) {
// Thin bar at the top of the screen like Chrome.
std::vector<float> progress = g_DownloadManager.GetCurrentProgress();
if (progress.empty()) {
Expand All @@ -751,27 +754,34 @@ void DrawDownloadsOverlay(UIContext &dc) {
0xFF777777,
};

dc.Begin();
dc->Begin();
int h = 5;
for (size_t i = 0; i < progress.size(); i++) {
float barWidth = 10 + (dc.GetBounds().w - 10) * progress[i];
float barWidth = 10 + (dc->GetBounds().w - 10) * progress[i];
Bounds bounds(0, h * i, barWidth, h);
UI::Drawable solid(colors[i & 3]);
dc.FillRect(solid, bounds);
dc->FillRect(solid, bounds);
}
dc->End();
dc->Flush();

if (g_TakeScreenshot) {
TakeScreenshot();
}
dc.End();
dc.Flush();
}

void NativeRender(GraphicsContext *graphicsContext) {
g_GameManager.Update();

// If uitexture gets reloaded, make sure we use the latest one.
// Not sure this happens anymore now that we tear down all graphics on app switches...
uiContext->FrameSetup(uiTexture->GetTexture());

float xres = dp_xres;
float yres = dp_yres;

// Apply the UIContext bounds as a 2D transformation matrix.
// TODO: This should be moved into the draw context...
Matrix4x4 ortho;
switch (GetGPUBackend()) {
case GPUBackend::VULKAN:
Expand All @@ -798,19 +808,12 @@ void NativeRender(GraphicsContext *graphicsContext) {
ui_draw2d.PushDrawMatrix(ortho);
ui_draw2d_front.PushDrawMatrix(ortho);

// All actual rendering happen in here.
screenManager->render();
if (screenManager->getUIContext()->Text()) {
screenManager->getUIContext()->Text()->OncePerFrame();
}

// At this point, the vulkan context has been "ended" already, no more drawing can be done in this frame.
// TODO: Integrate the download overlay with the screen system
DrawDownloadsOverlay(*screenManager->getUIContext());

if (g_TakeScreenshot) {
TakeScreenshot();
}

if (resized) {
resized = false;

Expand Down
4 changes: 4 additions & 0 deletions ext/native/ui/screen.cpp
Expand Up @@ -125,12 +125,16 @@ void ScreenManager::render() {
backback.screen->preRender();
backback.screen->render();
stack_.back().screen->render();
if (postRenderCb_)
postRenderCb_(getUIContext(), postRenderUserdata_);
backback.screen->postRender();
break;
}
default:
stack_.back().screen->preRender();
stack_.back().screen->render();
if (postRenderCb_)
postRenderCb_(getUIContext(), postRenderUserdata_);
stack_.back().screen->postRender();
break;
}
Expand Down
10 changes: 10 additions & 0 deletions ext/native/ui/screen.h
Expand Up @@ -92,6 +92,8 @@ enum {
LAYER_TRANSPARENT = 2,
};

typedef void(*PostRenderCallback)(UIContext *ui, void *userdata);

class ScreenManager {
public:
ScreenManager();
Expand All @@ -106,6 +108,11 @@ class ScreenManager {
void setDrawContext(Draw::DrawContext *context) { thin3DContext_ = context; }
Draw::DrawContext *getDrawContext() { return thin3DContext_; }

void setPostRenderCallback(PostRenderCallback cb, void *userdata) {
postRenderCb_ = cb;
postRenderUserdata_ = userdata;
}

void render();
void resized();
void deviceLost();
Expand Down Expand Up @@ -142,6 +149,9 @@ class ScreenManager {
UIContext *uiContext_;
Draw::DrawContext *thin3DContext_;

PostRenderCallback postRenderCb_ = nullptr;
void *postRenderUserdata_ = nullptr;

const Screen *dialogFinished_;
DialogResult dialogResult_;

Expand Down

0 comments on commit 6147448

Please sign in to comment.