1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/vr/elements/textured_element.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/vr/elements/ui_texture.h"
#include "chrome/browser/vr/skia_surface_provider.h"
#include "chrome/browser/vr/ui_element_renderer.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/gfx/geometry/rect_f.h"
namespace vr {
TexturedElement::TexturedElement() = default;
TexturedElement::~TexturedElement() = default;
void TexturedElement::Initialize(SkiaSurfaceProvider* provider) {
TRACE_EVENT0("gpu", "TexturedElement::Initialize");
DCHECK(provider);
provider_ = provider;
DCHECK(GetTexture());
GetTexture()->OnInitialized();
initialized_ = true;
}
bool TexturedElement::PrepareToDraw() {
if (!GetTexture()->dirty() || !IsVisible())
return false;
texture_size_ = MeasureTextureSize();
if (TextureDependsOnMeasurement())
DCHECK(GetTexture()->measured());
return true;
}
bool TexturedElement::HasDirtyTexture() const {
DCHECK(IsVisible()); // We shouldn't be querying non visible elements.
if (!GetTexture()->dirty())
return false;
// Elements can be dirtied by user input. If the texture draw depends on
// measurement, defer until the next frame.
return !TextureDependsOnMeasurement() || GetTexture()->measured();
}
void TexturedElement::UpdateTexture() {
if (!HasDirtyTexture())
return;
// If GL isn't ready yet, or we're in unit tests, pretend we drew a texture to
// accurate articulate that we would have. When GL is initialized, all
// textures are dirtied again.
if (!initialized_) {
GetTexture()->DrawEmptyTexture();
return;
}
if (!texture_size_.IsEmpty()) {
skia_texture_ = provider_->CreateTextureWithSkia(
texture_size_, [this](SkCanvas* canvas) {
GetTexture()->DrawTexture(canvas, texture_size_);
});
DCHECK(skia_texture_);
} else {
skia_texture_.reset();
GetTexture()->DrawEmptyTexture();
}
}
void TexturedElement::SetForegroundColor(SkColor color) {
GetTexture()->SetForegroundColor(color);
}
void TexturedElement::SetBackgroundColor(SkColor color) {
GetTexture()->SetBackgroundColor(color);
}
void TexturedElement::Render(UiElementRenderer* renderer,
const CameraModel& model) const {
DCHECK(initialized_);
// Zero-size elements, such as empty text, don't allocate textures.
if (!skia_texture_) {
return;
}
renderer->DrawTexturedQuad(skia_texture_->texture_id(), 0,
model.view_proj_matrix * world_space_transform(),
GetClipRect(), computed_opacity(), size(),
corner_radius(), true /* blend */);
}
} // namespace vr
|