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
|
// Copyright (c) 2012 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 "ui/surface/transport_dib.h"
#include <windows.h>
#include <limits>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/sys_info.h"
#include "skia/ext/platform_canvas.h"
TransportDIB::TransportDIB()
: size_(0) {
}
TransportDIB::~TransportDIB() {
}
TransportDIB::TransportDIB(HANDLE handle)
: shared_memory_(handle, false /* read write */),
size_(0) {
}
// static
TransportDIB* TransportDIB::Create(size_t size, uint32 sequence_num) {
TransportDIB* dib = new TransportDIB;
if (!dib->shared_memory_.CreateAnonymous(size)) {
delete dib;
return NULL;
}
dib->size_ = size;
dib->sequence_num_ = sequence_num;
return dib;
}
// static
TransportDIB* TransportDIB::Map(Handle handle) {
scoped_ptr<TransportDIB> dib(CreateWithHandle(handle));
if (!dib->Map())
return NULL;
return dib.release();
}
// static
TransportDIB* TransportDIB::CreateWithHandle(Handle handle) {
return new TransportDIB(handle);
}
// static
bool TransportDIB::is_valid_handle(Handle dib) {
return dib != NULL;
}
// static
bool TransportDIB::is_valid_id(TransportDIB::Id id) {
return is_valid_handle(id.handle);
}
skia::PlatformCanvas* TransportDIB::GetPlatformCanvas(int w, int h) {
// This DIB already mapped the file into this process, but PlatformCanvas
// will map it again.
DCHECK(!memory()) << "Mapped file twice in the same process.";
// We can't check the canvas size before mapping, but it's safe because
// Windows will fail to map the section if the dimensions of the canvas
// are too large.
skia::PlatformCanvas* canvas =
skia::CreatePlatformCanvas(w, h, true, handle(),
skia::RETURN_NULL_ON_FAILURE);
// Calculate the size for the memory region backing the canvas.
if (canvas)
size_ = skia::PlatformCanvasStrideForWidth(w) * h;
return canvas;
}
bool TransportDIB::Map() {
if (!is_valid_handle(handle()))
return false;
if (memory())
return true;
if (!shared_memory_.Map(0 /* map whole shared memory segment */)) {
LOG(ERROR) << "Failed to map transport DIB"
<< " handle:" << shared_memory_.handle()
<< " error:" << ::GetLastError();
return false;
}
size_ = shared_memory_.mapped_size();
return true;
}
void* TransportDIB::memory() const {
return shared_memory_.memory();
}
TransportDIB::Handle TransportDIB::handle() const {
return shared_memory_.handle();
}
TransportDIB::Id TransportDIB::id() const {
return Id(handle(), sequence_num_);
}
|