Implement placeholder benchmark space UI.

This commit is contained in:
2018-08-02 19:14:34 +02:00
parent 7f42fdf5e8
commit e96efb840a
2 changed files with 230 additions and 0 deletions

230
benchmark/space/__init__.py Normal file
View File

@@ -0,0 +1,230 @@
import blf
import bpy
from bpy.types import Panel
import os
# Global state
result_platform = None
result_stats = None
images = {}
progress = 0.0
# Draw Utilities
font_id = 0
def viewport_size():
import bgl
viewport = bgl.Buffer(bgl.GL_INT, 4)
bgl.glGetIntegerv(bgl.GL_VIEWPORT, viewport)
return viewport[2], viewport[3]
def draw_text_center(text, x, y):
dim = blf.dimensions(font_id, text)
cx = x - dim[0]/2
cy = y - dim[1]/2
blf.position(font_id, cx, cy, 0)
blf.draw(font_id, text)
def draw_text_multiline(text, x, y):
space = 8
for line in text.split('\n'):
dim = blf.dimensions(font_id, line)
y -= dim[1]
blf.position(font_id, x, y, 0)
blf.draw(font_id, line)
y -= space
def draw_rect(x, y, w, h, color):
import gpu
gpu.draw.rect(x, y, x + w, y + h, color[0], color[1], color[2], color[3])
def draw_image(filepath, x, y, w, h):
global images
if filepath not in images:
ima = bpy.data.images.load(filepath)
images[filepath] = ima
import gpu
gpu.draw.image(images[filepath], x, y, x + w, y + h)
# Draw
def benchmark_draw_post_pixel(arg1, arg2):
ui_scale = bpy.context.user_preferences.system.ui_scale
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
window_width, window_height = viewport_size()
# Image
image_h = 370 * ui_scale
image_y = window_height - image_h
splash_dir = os.path.dirname(os.path.abspath(__file__))
splash_filepath = os.path.join(splash_dir, 'splash.png')
draw_image(splash_filepath, 0, image_y, window_width, image_h)
if result_stats:
blf.size(font_id, int(12 * ui_scale), 72)
x = 50.0 * ui_scale
y = image_y - 20 * ui_scale
# Stats
draw_text_multiline(result_platform, x, y)
draw_text_multiline(result_stats, 0.5 * window_width + x, y)
progress_x = 0.0
progress_y = image_y + 1
progress_w = window_width * progress
progress_h = 15.0 * ui_scale
progress_color = [0.8, 1.0, 1.0, 0.2]
draw_rect(progress_x, progress_y, progress_w, progress_h, progress_color)
else:
# Title
x = 0.5 * window_width
y = 0.70 * window_height
blf.size(font_id, int(32 * ui_scale), 72)
draw_text_center("Blender Benchmark 1.0", x, y)
y -= 32 * ui_scale
blf.size(font_id, int(12 * ui_scale), 72)
draw_text_center("Explore the results on opendata.blender.org", x, y)
# Bottom bar
bottom_x = 0
bottom_y = 0
bottom_w = window_width
bottom_h = 52 * ui_scale
bottom_color = [0.2, 0.2, 0.2, 1.0]
draw_rect(bottom_x, bottom_y, bottom_w, bottom_h, bottom_color)
handle_draw = bpy.types.SpaceBenchmark.draw_handler_add(
benchmark_draw_post_pixel, (None, None), 'WINDOW', 'POST_PIXEL')
# Panels
class BENCHMARK_PT_main(Panel):
bl_label = "Benchmark"
bl_options = {'HIDE_HEADER'}
bl_space_type = 'BENCHMARK'
bl_region_type = 'WINDOW'
def draw(self, context):
if result_stats:
return
layout = self.layout
split = layout.split(0.65)
split.label()
split = split.split(0.97)
col = split.column()
sub = col.row()
sub.scale_y = 64.0
sub.separator()
sub = col.row()
sub.scale_y = 2.25
sub.operator("benchmark.run", text="QUICK BENCHMARK")
col.separator()
sub = col.row()
sub.emboss = 'NONE'
sub.scale_y = 1.5
sub.operator("benchmark.run", text="COMPLETE BENCHMARK (approx. 1.h)")
sub = col.row()
sub.emboss = 'NONE'
sub.scale_y = 1.5
sub.operator("benchmark.save", text="SAVE")
split.label()
# Operator
class BENCHMARK_OT_run(bpy.types.Operator):
"Run Blender benchmark"
bl_label = "Run Benchmark"
bl_idname = "benchmark.run"
def invoke(self, context, event):
global result_platform, result_stats, progress
result_platform = "CPU: Intel Core i11\nGPU: NVIDIA GTX 1180\nOS: Minix"
result_stats = "Render Time: infinite\nMemory Usage: 16KB"
progress = 0.01
self._timer = context.window_manager.event_timer_add(0.01, context.window)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def modal(self, context, event):
if event.type == 'TIMER':
global progress
progress = min(progress + 0.001, 1.0)
context.area.tag_redraw()
if progress == 1.0:
context.window_manager.event_timer_remove(self._timer)
return {'FINISHED'}
return {'RUNNING_MODAL'}
def cancel(self, context):
# When users closes window
return
class BENCHMARK_OT_save(bpy.types.Operator):
bl_idname = "benchmark.save"
bl_label = "Save Benchmark Result"
filepath: bpy.props.StringProperty(
subtype='FILE_PATH',
options={'SKIP_SAVE'},
)
def execute(self, context):
print("Saving to " + self.filepath)
return {'FINISHED'}
def invoke(self, context, event):
import os
if not self.filepath:
self.filepath = os.path.join(
os.path.expanduser("~"), "benchmark-result.txt")
wm = context.window_manager
wm.fileselect_add(self)
return {'RUNNING_MODAL'}
# Tweak User Preferences
userpref = bpy.context.user_preferences
theme = userpref.themes[0]
theme.user_interface.wcol_tool.inner = [0.408, 0.690, 0.129, 1.0]
theme.user_interface.wcol_tool.inner_sel = [0.308, 0.590, 0.029, 1.0]
theme.user_interface.wcol_tool.outline = [0.408, 0.690, 0.129]
theme.benchmark.space.back = [0.26, 0.26, 0.26]
style = userpref.ui_styles[0]
style.widget.points = 12
# Registration
classes = (
BENCHMARK_PT_main,
BENCHMARK_OT_run,
BENCHMARK_OT_save,
)

BIN
benchmark/space/splash.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB