Commit 5729f982 authored by danakj's avatar danakj Committed by Commit bot

tabstrip: Use a PaintRecorder to access the Canvas for painting.

Don't use the canvas() from the PaintContext so that we can have the
PaintContext be backed by a DisplayItemList instead in the future.
Access to the Canvas should be done through a PaintRecorder.

For compositing effects (opacity) we use a CompositingRecorder to
provide a scoped opacity change.

R=pkasting
BUG=466426

Review URL: https://codereview.chromium.org/1062293003

Cr-Commit-Position: refs/heads/master@{#324173}
parent 5cc4fb59
...@@ -463,10 +463,8 @@ CardUnmaskPromptViews::FadeOutView::~FadeOutView() { ...@@ -463,10 +463,8 @@ CardUnmaskPromptViews::FadeOutView::~FadeOutView() {
void CardUnmaskPromptViews::FadeOutView::PaintChildren( void CardUnmaskPromptViews::FadeOutView::PaintChildren(
const ui::PaintContext& context) { const ui::PaintContext& context) {
if (opacity_ > 0.99) uint8_t alpha = static_cast<uint8_t>(255 * opacity_);
return views::View::PaintChildren(context); ui::CompositingRecorder recorder(context, alpha);
ui::CompositingRecorder recorder(context, opacity_);
views::View::PaintChildren(context); views::View::PaintChildren(context);
} }
......
...@@ -38,7 +38,8 @@ ...@@ -38,7 +38,8 @@
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/list_selection_model.h" #include "ui/base/models/list_selection_model.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/paint_context.h" #include "ui/compositor/compositing_recorder.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/gfx/animation/animation_container.h" #include "ui/gfx/animation/animation_container.h"
#include "ui/gfx/animation/throb_animation.h" #include "ui/gfx/animation/throb_animation.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
...@@ -74,14 +75,14 @@ namespace { ...@@ -74,14 +75,14 @@ namespace {
static const int kTabStripAnimationVSlop = 40; static const int kTabStripAnimationVSlop = 40;
// Inactive tabs in a native frame are slightly transparent. // Inactive tabs in a native frame are slightly transparent.
static const int kGlassFrameInactiveTabAlpha = 200; static const uint8_t kGlassFrameInactiveTabAlpha = 200;
// If there are multiple tabs selected then make non-selected inactive tabs // If there are multiple tabs selected then make non-selected inactive tabs
// even more transparent. // even more transparent.
static const int kGlassFrameInactiveTabAlphaMultiSelection = 150; static const int kGlassFrameInactiveTabAlphaMultiSelection = 150;
// Alpha applied to all elements save the selected tabs. // Alpha applied to all elements save the selected tabs.
static const int kInactiveTabAndNewTabButtonAlphaAsh = 230; static const uint8_t kInactiveTabAndNewTabButtonAlphaAsh = 230;
static const int kInactiveTabAndNewTabButtonAlpha = 255; static const uint8_t kInactiveTabAndNewTabButtonAlpha = 255;
// Inverse ratio of the width of a tab edge to the width of the tab. When // Inverse ratio of the width of a tab edge to the width of the tab. When
// hovering over the left or right edge of a tab, the drop indicator will // hovering over the left or right edge of a tab, the drop indicator will
...@@ -458,10 +459,11 @@ gfx::ImageSkia NewTabButton::GetImageForState( ...@@ -458,10 +459,11 @@ gfx::ImageSkia NewTabButton::GetImageForState(
canvas.DrawImageInt(GetBackgroundImage(state, scale), 0, 0); canvas.DrawImageInt(GetBackgroundImage(state, scale), 0, 0);
// Draw the button border with a slight alpha. // Draw the button border with a slight alpha.
const int kGlassFrameOverlayAlpha = 178; const uint8_t kGlassFrameOverlayAlpha = 178;
const int kOpaqueFrameOverlayAlpha = 230; const uint8_t kOpaqueFrameOverlayAlpha = 230;
uint8 alpha = ShouldWindowContentsBeTransparent() ? uint8_t alpha = ShouldWindowContentsBeTransparent()
kGlassFrameOverlayAlpha : kOpaqueFrameOverlayAlpha; ? kGlassFrameOverlayAlpha
: kOpaqueFrameOverlayAlpha;
canvas.DrawImageInt(*overlay, 0, 0, alpha); canvas.DrawImageInt(*overlay, 0, 0, alpha);
return gfx::ImageSkia(canvas.ExtractImageRep()); return gfx::ImageSkia(canvas.ExtractImageRep());
...@@ -1242,7 +1244,6 @@ void TabStrip::Layout() { ...@@ -1242,7 +1244,6 @@ void TabStrip::Layout() {
} }
void TabStrip::PaintChildren(const ui::PaintContext& context) { void TabStrip::PaintChildren(const ui::PaintContext& context) {
gfx::Canvas* canvas = context.canvas();
// The view order doesn't match the paint order (tabs_ contains the tab // The view order doesn't match the paint order (tabs_ contains the tab
// ordering). Additionally we need to paint the tabs that are closing in // ordering). Additionally we need to paint the tabs that are closing in
// |tabs_closing_map_|. // |tabs_closing_map_|.
...@@ -1255,12 +1256,13 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) { ...@@ -1255,12 +1256,13 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
const chrome::HostDesktopType host_desktop_type = const chrome::HostDesktopType host_desktop_type =
chrome::GetHostDesktopTypeForNativeView(GetWidget()->GetNativeView()); chrome::GetHostDesktopTypeForNativeView(GetWidget()->GetNativeView());
const int inactive_tab_alpha = const uint8_t inactive_tab_alpha =
(host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) ? (host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH)
kInactiveTabAndNewTabButtonAlphaAsh : kInactiveTabAndNewTabButtonAlpha; ? kInactiveTabAndNewTabButtonAlphaAsh
: kInactiveTabAndNewTabButtonAlpha;
if (inactive_tab_alpha < 255) {
canvas->SaveLayerAlpha(inactive_tab_alpha); ui::CompositingRecorder opacity_recorder(context, inactive_tab_alpha);
PaintClosingTabs(tab_count(), context); PaintClosingTabs(tab_count(), context);
...@@ -1302,17 +1304,17 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) { ...@@ -1302,17 +1304,17 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
tab->Paint(context); tab->Paint(context);
} }
} }
if (inactive_tab_alpha < 255) }
canvas->Restore();
if (GetWidget()->ShouldWindowContentsBeTransparent()) { if (GetWidget()->ShouldWindowContentsBeTransparent()) {
ui::PaintRecorder recorder(context);
// Make sure non-active tabs are somewhat transparent. // Make sure non-active tabs are somewhat transparent.
SkPaint paint; SkPaint paint;
// If there are multiple tabs selected, fade non-selected tabs more to make // If there are multiple tabs selected, fade non-selected tabs more to make
// the selected tabs more noticable. // the selected tabs more noticable.
int alpha = selected_tab_count > 1 ? uint8_t alpha = selected_tab_count > 1
kGlassFrameInactiveTabAlphaMultiSelection : ? kGlassFrameInactiveTabAlphaMultiSelection
kGlassFrameInactiveTabAlpha; : kGlassFrameInactiveTabAlpha;
paint.setColor(SkColorSetARGB(alpha, 255, 255, 255)); paint.setColor(SkColorSetARGB(alpha, 255, 255, 255));
paint.setXfermodeMode(SkXfermode::kDstIn_Mode); paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
paint.setStyle(SkPaint::kFill_Style); paint.setStyle(SkPaint::kFill_Style);
...@@ -1324,7 +1326,7 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) { ...@@ -1324,7 +1326,7 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
// overlap the avatar button, leading to visual artifacts. // overlap the avatar button, leading to visual artifacts.
const int kTopOffset = 4; const int kTopOffset = 4;
// The tabstrip area overlaps the toolbar area by 2 px. // The tabstrip area overlaps the toolbar area by 2 px.
canvas->DrawRect( recorder.canvas()->DrawRect(
gfx::Rect(0, kTopOffset, width(), height() - kTopOffset - 2), paint); gfx::Rect(0, kTopOffset, width(), height() - kTopOffset - 2), paint);
} }
...@@ -1338,11 +1340,10 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) { ...@@ -1338,11 +1340,10 @@ void TabStrip::PaintChildren(const ui::PaintContext& context) {
active_tab->Paint(context); active_tab->Paint(context);
// Paint the New Tab button. // Paint the New Tab button.
if (inactive_tab_alpha < 255) {
canvas->SaveLayerAlpha(inactive_tab_alpha); ui::CompositingRecorder opacity_recorder(context, inactive_tab_alpha);
newtab_button_->Paint(context); newtab_button_->Paint(context);
if (inactive_tab_alpha < 255) }
canvas->Restore();
// And the dragged tabs. // And the dragged tabs.
for (size_t i = 0; i < tabs_dragging.size(); ++i) for (size_t i = 0; i < tabs_dragging.size(); ++i)
......
...@@ -10,12 +10,14 @@ ...@@ -10,12 +10,14 @@
namespace ui { namespace ui {
CompositingRecorder::CompositingRecorder(const PaintContext& context, CompositingRecorder::CompositingRecorder(const PaintContext& context,
float opacity) uint8_t alpha)
: canvas_(context.canvas()) { : canvas_(context.canvas()), saved_(alpha < 255) {
canvas_->SaveLayerAlpha(0xff * opacity); if (saved_)
canvas_->SaveLayerAlpha(alpha);
} }
CompositingRecorder::~CompositingRecorder() { CompositingRecorder::~CompositingRecorder() {
if (saved_)
canvas_->Restore(); canvas_->Restore();
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef UI_COMPOSITOR_COMPOSITING_RECORDER_H_ #ifndef UI_COMPOSITOR_COMPOSITING_RECORDER_H_
#define UI_COMPOSITOR_COMPOSITING_RECORDER_H_ #define UI_COMPOSITOR_COMPOSITING_RECORDER_H_
#include "base/basictypes.h"
#include "base/macros.h" #include "base/macros.h"
#include "ui/compositor/compositor_export.h" #include "ui/compositor/compositor_export.h"
...@@ -22,11 +23,14 @@ class PaintContext; ...@@ -22,11 +23,14 @@ class PaintContext;
// be filtered by the effect. // be filtered by the effect.
class COMPOSITOR_EXPORT CompositingRecorder { class COMPOSITOR_EXPORT CompositingRecorder {
public: public:
CompositingRecorder(const PaintContext& context, float opacity); // |alpha| is a value between 0 and 255, where 0 is transparent and 255 is
// opaque.
CompositingRecorder(const PaintContext& context, uint8_t alpha);
~CompositingRecorder(); ~CompositingRecorder();
private: private:
gfx::Canvas* canvas_; gfx::Canvas* canvas_;
bool saved_;
DISALLOW_COPY_AND_ASSIGN(CompositingRecorder); DISALLOW_COPY_AND_ASSIGN(CompositingRecorder);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment