Skip to content

Commit

Permalink
Change out-parameter types in PDFiumEngine::GetRegion()
Browse files Browse the repository at this point in the history
- Change `region` from a void* to a base::span<uint8_t>.
- Change `stride` from int to size_t.

Change-Id: Iebbb079915550111c357efdd4c0b892df4bd6443
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4991109
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1217916}
  • Loading branch information
leizleiz authored and Chromium LUCI CQ committed Oct 31, 2023
1 parent 1f286ad commit f82d1d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
41 changes: 22 additions & 19 deletions pdf/pdfium/pdfium_engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3337,8 +3337,8 @@ void PDFiumEngine::DrawSelections(int progressive_index,
int page_index = progressive_paints_[progressive_index].page_index();
gfx::Rect dirty_in_screen = progressive_paints_[progressive_index].rect();

void* region = nullptr;
int stride;
base::span<uint8_t> region;
size_t stride;
GetRegion(dirty_in_screen.origin(), image_data, region, stride);

std::vector<gfx::Rect> highlighted_rects;
Expand Down Expand Up @@ -3402,14 +3402,16 @@ int PDFiumEngine::GetProgressiveIndex(int page_index) const {
ScopedFPDFBitmap PDFiumEngine::CreateBitmap(const gfx::Rect& rect,
bool has_alpha,
SkBitmap& image_data) const {
void* region;
int stride;
base::span<uint8_t> region;
size_t stride;
GetRegion(rect.origin(), image_data, region, stride);
if (!region)
if (region.empty()) {
return nullptr;
}

int format = has_alpha ? FPDFBitmap_BGRA : FPDFBitmap_BGRx;
return ScopedFPDFBitmap(
FPDFBitmap_CreateEx(rect.width(), rect.height(), format, region, stride));
return ScopedFPDFBitmap(FPDFBitmap_CreateEx(rect.width(), rect.height(),
format, region.data(), stride));
}

void PDFiumEngine::GetPDFiumRect(int page_index,
Expand Down Expand Up @@ -3483,15 +3485,16 @@ gfx::RectF PDFiumEngine::GetPageBoundingBox(int page_index) {
return page->GetBoundingBox();
}

void PDFiumEngine::Highlight(void* buffer,
int stride,
void PDFiumEngine::Highlight(base::span<uint8_t> buffer,
size_t stride,
const gfx::Rect& rect,
int color_red,
int color_green,
int color_blue,
std::vector<gfx::Rect>& highlighted_rects) const {
if (!buffer)
if (buffer.empty()) {
return;
}

gfx::Rect new_rect = rect;
for (const auto& highlighted : highlighted_rects)
Expand All @@ -3512,6 +3515,7 @@ void PDFiumEngine::Highlight(void* buffer,
int h = new_rect.height();

for (int y = t; y < t + h; ++y) {
base::span<uint8_t> row = buffer.subspan(y * stride, stride);
for (int x = l; x < l + w; ++x) {
bool overlaps = false;
for (size_t i : overlapping_rect_indices) {
Expand All @@ -3524,7 +3528,7 @@ void PDFiumEngine::Highlight(void* buffer,
if (overlaps)
continue;

uint8_t* pixel = static_cast<uint8_t*>(buffer) + y * stride + x * 4;
uint8_t* pixel = row.data() + x * 4;
pixel[0] = static_cast<uint8_t>(pixel[0] * (color_blue / 255.0));
pixel[1] = static_cast<uint8_t>(pixel[1] * (color_green / 255.0));
pixel[2] = static_cast<uint8_t>(pixel[2] * (color_red / 255.0));
Expand Down Expand Up @@ -3714,29 +3718,28 @@ void PDFiumEngine::DrawPageShadow(const gfx::Rect& page_rc,

void PDFiumEngine::GetRegion(const gfx::Point& location,
SkBitmap& image_data,
void*& region,
int& stride) const {
base::span<uint8_t>& region,
size_t& stride) const {
if (image_data.isNull()) {
DCHECK(plugin_size().IsEmpty());
stride = 0;
region = nullptr;
return;
}
char* buffer = static_cast<char*>(image_data.getPixels());
uint8_t* buffer = static_cast<uint8_t*>(image_data.getPixels());
stride = image_data.rowBytes();

gfx::Point offset_location = location + page_offset_;
// TODO: update this when we support BIDI and scrollbars can be on the left.
if (!buffer ||
!gfx::Rect(gfx::PointAtOffsetFromOrigin(page_offset_), plugin_size())
.Contains(offset_location)) {
region = nullptr;
return;
}

buffer += location.y() * stride;
buffer += (location.x() + page_offset_.x()) * 4;
region = buffer;
base::span<uint8_t> buffer_span(buffer, image_data.height() * stride);
size_t x_offset = location.x() + page_offset_.x();
size_t offset = location.y() * stride + x_offset * 4;
region = buffer_span.subspan(offset);
}

void PDFiumEngine::OnSelectionTextChanged() {
Expand Down
9 changes: 5 additions & 4 deletions pdf/pdfium/pdfium_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "base/check.h"
#include "base/containers/flat_map.h"
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
Expand Down Expand Up @@ -519,8 +520,8 @@ class PDFiumEngine : public PDFEngine,
// Given an image `buffer` with `stride`, highlights `rect`.
// `highlighted_rects` contains the already highlighted rectangles and will be
// updated to include `rect` if `rect` has not already been highlighted.
void Highlight(void* buffer,
int stride,
void Highlight(base::span<uint8_t> buffer,
size_t stride,
const gfx::Rect& rect,
int color_red,
int color_green,
Expand Down Expand Up @@ -549,8 +550,8 @@ class PDFiumEngine : public PDFEngine,

void GetRegion(const gfx::Point& location,
SkBitmap& image_data,
void*& region,
int& stride) const;
base::span<uint8_t>& region,
size_t& stride) const;

// Called when the selection changes.
void OnSelectionTextChanged();
Expand Down

0 comments on commit f82d1d1

Please sign in to comment.