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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/android/in_process/synchronous_compositor_output_surface.h"
#include "base/auto_reset.h"
#include "base/logging.h"
#include "cc/output/compositor_frame.h"
#include "cc/output/context_provider.h"
#include "cc/output/output_surface_client.h"
#include "cc/output/software_output_device.h"
#include "content/browser/android/in_process/synchronous_compositor_external_begin_frame_source.h"
#include "content/browser/android/in_process/synchronous_compositor_impl.h"
#include "content/browser/android/in_process/synchronous_compositor_registry.h"
#include "content/browser/gpu/compositor_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/renderer/gpu/frame_swap_message_queue.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "gpu/command_buffer/common/gpu_memory_allocation.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/transform.h"
namespace content {
namespace {
// Do not limit number of resources, so use an unrealistically high value.
const size_t kNumResourcesLimit = 10 * 1000 * 1000;
} // namespace
class SynchronousCompositorOutputSurface::SoftwareDevice
: public cc::SoftwareOutputDevice {
public:
SoftwareDevice(SynchronousCompositorOutputSurface* surface)
: surface_(surface) {
}
virtual void Resize(const gfx::Size& pixel_size,
float scale_factor) override {
// Intentional no-op: canvas size is controlled by the embedder.
}
virtual SkCanvas* BeginPaint(const gfx::Rect& damage_rect) override {
if (!surface_->current_sw_canvas_) {
NOTREACHED() << "BeginPaint with no canvas set";
return &null_canvas_;
}
LOG_IF(WARNING, surface_->frame_holder_.get())
<< "Mutliple calls to BeginPaint per frame";
return surface_->current_sw_canvas_;
}
virtual void EndPaint(cc::SoftwareFrameData* frame_data) override {
}
virtual void CopyToPixels(const gfx::Rect& rect, void* pixels) override {
NOTIMPLEMENTED();
}
private:
SynchronousCompositorOutputSurface* surface_;
SkCanvas null_canvas_;
DISALLOW_COPY_AND_ASSIGN(SoftwareDevice);
};
SynchronousCompositorOutputSurface::SynchronousCompositorOutputSurface(
int routing_id,
scoped_refptr<FrameSwapMessageQueue> frame_swap_message_queue)
: cc::OutputSurface(
scoped_ptr<cc::SoftwareOutputDevice>(new SoftwareDevice(this))),
routing_id_(routing_id),
registered_(false),
current_sw_canvas_(nullptr),
memory_policy_(0),
output_surface_client_(nullptr),
frame_swap_message_queue_(frame_swap_message_queue),
begin_frame_source_(nullptr) {
capabilities_.deferred_gl_initialization = true;
capabilities_.draw_and_swap_full_viewport_every_frame = true;
capabilities_.adjust_deadline_for_parent = false;
capabilities_.delegated_rendering = true;
capabilities_.max_frames_pending = 1;
memory_policy_.priority_cutoff_when_visible =
gpu::MemoryAllocation::CUTOFF_ALLOW_NICE_TO_HAVE;
}
SynchronousCompositorOutputSurface::~SynchronousCompositorOutputSurface() {
DCHECK(CalledOnValidThread());
if (registered_) {
SynchronousCompositorRegistry::GetInstance()->UnregisterOutputSurface(
routing_id_, this);
}
DCHECK(!begin_frame_source_);
}
bool SynchronousCompositorOutputSurface::BindToClient(
cc::OutputSurfaceClient* surface_client) {
DCHECK(CalledOnValidThread());
if (!cc::OutputSurface::BindToClient(surface_client))
return false;
output_surface_client_ = surface_client;
output_surface_client_->SetMemoryPolicy(memory_policy_);
SynchronousCompositorRegistry::GetInstance()->RegisterOutputSurface(
routing_id_, this);
registered_ = true;
return true;
}
void SynchronousCompositorOutputSurface::Reshape(
const gfx::Size& size, float scale_factor) {
// Intentional no-op: surface size is controlled by the embedder.
}
void SynchronousCompositorOutputSurface::SwapBuffers(
cc::CompositorFrame* frame) {
DCHECK(CalledOnValidThread());
frame_holder_.reset(new cc::CompositorFrame);
frame->AssignTo(frame_holder_.get());
client_->DidSwapBuffers();
}
void SynchronousCompositorOutputSurface::SetBeginFrameSource(
SynchronousCompositorExternalBeginFrameSource* begin_frame_source) {
begin_frame_source_ = begin_frame_source;
}
namespace {
void AdjustTransform(gfx::Transform* transform, gfx::Rect viewport) {
// CC's draw origin starts at the viewport.
transform->matrix().postTranslate(-viewport.x(), -viewport.y(), 0);
}
} // namespace
bool SynchronousCompositorOutputSurface::InitializeHwDraw(
scoped_refptr<cc::ContextProvider> onscreen_context_provider) {
DCHECK(CalledOnValidThread());
DCHECK(HasClient());
DCHECK(!context_provider_.get());
return InitializeAndSetContext3d(onscreen_context_provider);
}
void SynchronousCompositorOutputSurface::ReleaseHwDraw() {
DCHECK(CalledOnValidThread());
cc::OutputSurface::ReleaseGL();
}
scoped_ptr<cc::CompositorFrame>
SynchronousCompositorOutputSurface::DemandDrawHw(
gfx::Size surface_size,
const gfx::Transform& transform,
gfx::Rect viewport,
gfx::Rect clip,
gfx::Rect viewport_rect_for_tile_priority,
const gfx::Transform& transform_for_tile_priority) {
DCHECK(CalledOnValidThread());
DCHECK(HasClient());
DCHECK(context_provider_.get());
surface_size_ = surface_size;
InvokeComposite(transform,
viewport,
clip,
viewport_rect_for_tile_priority,
transform_for_tile_priority,
true);
return frame_holder_.Pass();
}
scoped_ptr<cc::CompositorFrame>
SynchronousCompositorOutputSurface::DemandDrawSw(SkCanvas* canvas) {
DCHECK(CalledOnValidThread());
DCHECK(canvas);
DCHECK(!current_sw_canvas_);
base::AutoReset<SkCanvas*> canvas_resetter(¤t_sw_canvas_, canvas);
SkIRect canvas_clip;
canvas->getClipDeviceBounds(&canvas_clip);
gfx::Rect clip = gfx::SkIRectToRect(canvas_clip);
gfx::Transform transform(gfx::Transform::kSkipInitialization);
transform.matrix() = canvas->getTotalMatrix(); // Converts 3x3 matrix to 4x4.
surface_size_ = gfx::Size(canvas->getDeviceSize().width(),
canvas->getDeviceSize().height());
// Pass in the cached hw viewport and transform for tile priority to avoid
// tile thrashing when the WebView is alternating between hardware and
// software draws.
InvokeComposite(transform,
clip,
clip,
cached_hw_viewport_rect_for_tile_priority_,
cached_hw_transform_for_tile_priority_,
false);
return frame_holder_.Pass();
}
void SynchronousCompositorOutputSurface::InvokeComposite(
const gfx::Transform& transform,
gfx::Rect viewport,
gfx::Rect clip,
gfx::Rect viewport_rect_for_tile_priority,
gfx::Transform transform_for_tile_priority,
bool hardware_draw) {
DCHECK(!frame_holder_.get());
DCHECK(begin_frame_source_);
gfx::Transform adjusted_transform = transform;
AdjustTransform(&adjusted_transform, viewport);
SetExternalDrawConstraints(adjusted_transform,
viewport,
clip,
viewport_rect_for_tile_priority,
transform_for_tile_priority,
!hardware_draw);
SetNeedsRedrawRect(gfx::Rect(viewport.size()));
begin_frame_source_->BeginFrame();
// After software draws (which might move the viewport arbitrarily), restore
// the previous hardware viewport to allow CC's tile manager to prioritize
// properly.
if (hardware_draw) {
cached_hw_transform_ = adjusted_transform;
cached_hw_viewport_ = viewport;
cached_hw_clip_ = clip;
cached_hw_viewport_rect_for_tile_priority_ =
viewport_rect_for_tile_priority;
cached_hw_transform_for_tile_priority_ = transform_for_tile_priority;
} else {
bool resourceless_software_draw = false;
SetExternalDrawConstraints(cached_hw_transform_,
cached_hw_viewport_,
cached_hw_clip_,
cached_hw_viewport_rect_for_tile_priority_,
cached_hw_transform_for_tile_priority_,
resourceless_software_draw);
}
if (frame_holder_.get())
client_->DidSwapBuffersComplete();
}
void SynchronousCompositorOutputSurface::ReturnResources(
const cc::CompositorFrameAck& frame_ack) {
ReclaimResources(&frame_ack);
}
void SynchronousCompositorOutputSurface::SetMemoryPolicy(size_t bytes_limit) {
DCHECK(CalledOnValidThread());
memory_policy_.bytes_limit_when_visible = bytes_limit;
memory_policy_.num_resources_limit = kNumResourcesLimit;
if (output_surface_client_)
output_surface_client_->SetMemoryPolicy(memory_policy_);
}
void SynchronousCompositorOutputSurface::SetTreeActivationCallback(
const base::Closure& callback) {
DCHECK(client_);
client_->SetTreeActivationCallback(callback);
}
void SynchronousCompositorOutputSurface::GetMessagesToDeliver(
ScopedVector<IPC::Message>* messages) {
DCHECK(CalledOnValidThread());
scoped_ptr<FrameSwapMessageQueue::SendMessageScope> send_message_scope =
frame_swap_message_queue_->AcquireSendMessageScope();
frame_swap_message_queue_->DrainMessages(messages);
}
// Not using base::NonThreadSafe as we want to enforce a more exacting threading
// requirement: SynchronousCompositorOutputSurface() must only be used on the UI
// thread.
bool SynchronousCompositorOutputSurface::CalledOnValidThread() const {
return BrowserThread::CurrentlyOn(BrowserThread::UI);
}
} // namespace content
|