Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c295bab authored by Tao Bao's avatar Tao Bao Committed by android-build-merger
Browse files

Merge "minui: GRSurface manages data with std::unique_ptr." am: 481613b3 am: 6f42a59e

am: c194bbf6

Change-Id: I593df7f70e74ef872c725675bc8569858ff6b6f0
parents cf2a142c c194bbf6
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#pragma once

#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>

#include <functional>
@@ -32,7 +33,7 @@

class GRSurface {
 public:
  virtual ~GRSurface();
  virtual ~GRSurface() = default;

  // Creates and returns a GRSurface instance that's sufficient for storing an image of the given
  // size. The starting address of the surface data is aligned to SURFACE_DATA_ALIGNMENT. Returns
@@ -44,7 +45,7 @@ class GRSurface {
  std::unique_ptr<GRSurface> Clone() const;

  virtual uint8_t* data() {
    return data_;
    return data_.get();
  }

  const uint8_t* data() const {
@@ -61,7 +62,14 @@ class GRSurface {
      : width(width), height(height), row_bytes(row_bytes), pixel_bytes(pixel_bytes) {}

 private:
  uint8_t* data_{ nullptr };
  // The deleter for data_, whose data is allocated via aligned_alloc(3).
  struct DataDeleter {
    void operator()(uint8_t* data) {
      free(data);
    }
  };

  std::unique_ptr<uint8_t, DataDeleter> data_;
  size_t data_size_;

  DISALLOW_COPY_AND_ASSIGN(GRSurface);
+5 −10
Original line number Diff line number Diff line
@@ -46,24 +46,19 @@ std::unique_ptr<GRSurface> GRSurface::Create(int width, int height, int row_byte
  auto result = std::unique_ptr<GRSurface>(new GRSurface(width, height, row_bytes, pixel_bytes));
  result->data_size_ =
      (data_size + kSurfaceDataAlignment - 1) / kSurfaceDataAlignment * kSurfaceDataAlignment;
  result->data_ = static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_));
  if (result->data_ == nullptr) return nullptr;
  result->data_.reset(
      static_cast<uint8_t*>(aligned_alloc(kSurfaceDataAlignment, result->data_size_)));
  if (!result->data_) return nullptr;
  return result;
}

std::unique_ptr<GRSurface> GRSurface::Clone() const {
  auto result = GRSurface::Create(width, height, row_bytes, pixel_bytes, data_size_);
  memcpy(result->data_, data_, data_size_);
  if (!result) return nullptr;
  memcpy(result->data(), data(), data_size_);
  return result;
}

GRSurface::~GRSurface() {
  if (data_ != nullptr) {
    free(data_);
    data_ = nullptr;
  }
}

PngHandler::PngHandler(const std::string& name) {
  std::string res_path = g_resource_dir + "/" + name + ".png";
  png_fp_.reset(fopen(res_path.c_str(), "rbe"));