1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include "PixelBuffer.h"
20
21#include "Debug.h"
22#include "Extensions.h"
23#include "Properties.h"
24#include "renderstate/RenderState.h"
25
26#include <utils/Log.h>
27
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// CPU pixel buffer
33///////////////////////////////////////////////////////////////////////////////
34
35class CpuPixelBuffer: public PixelBuffer {
36public:
37    CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
38
39    uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
40    void unmap() override;
41
42    uint8_t* getMappedPointer() const override;
43
44    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
45
46private:
47    std::unique_ptr<uint8_t[]> mBuffer;
48};
49
50CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
51        : PixelBuffer(format, width, height)
52        , mBuffer(new uint8_t[width * height * formatSize(format)]) {
53}
54
55uint8_t* CpuPixelBuffer::map(AccessMode mode) {
56    if (mAccessMode == kAccessMode_None) {
57        mAccessMode = mode;
58    }
59    return mBuffer.get();
60}
61
62void CpuPixelBuffer::unmap() {
63    mAccessMode = kAccessMode_None;
64}
65
66uint8_t* CpuPixelBuffer::getMappedPointer() const {
67    return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
68}
69
70void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
71    glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
72            mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
73}
74
75///////////////////////////////////////////////////////////////////////////////
76// GPU pixel buffer
77///////////////////////////////////////////////////////////////////////////////
78
79class GpuPixelBuffer: public PixelBuffer {
80public:
81    GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
82    ~GpuPixelBuffer();
83
84    uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
85    void unmap() override;
86
87    uint8_t* getMappedPointer() const override;
88
89    void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
90
91private:
92    GLuint mBuffer;
93    uint8_t* mMappedPointer;
94    Caches& mCaches;
95};
96
97GpuPixelBuffer::GpuPixelBuffer(GLenum format,
98        uint32_t width, uint32_t height)
99        : PixelBuffer(format, width, height)
100        , mMappedPointer(nullptr)
101        , mCaches(Caches::getInstance()){
102    glGenBuffers(1, &mBuffer);
103
104    mCaches.pixelBufferState().bind(mBuffer);
105    glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
106    mCaches.pixelBufferState().unbind();
107}
108
109GpuPixelBuffer::~GpuPixelBuffer() {
110    glDeleteBuffers(1, &mBuffer);
111}
112
113uint8_t* GpuPixelBuffer::map(AccessMode mode) {
114    if (mAccessMode == kAccessMode_None) {
115        mCaches.pixelBufferState().bind(mBuffer);
116        mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
117#if DEBUG_OPENGL
118        if (!mMappedPointer) {
119            GLenum status = GL_NO_ERROR;
120            while ((status = glGetError()) != GL_NO_ERROR) {
121                ALOGE("Could not map GPU pixel buffer: 0x%x", status);
122            }
123        }
124#endif
125        mAccessMode = mode;
126    }
127
128    return mMappedPointer;
129}
130
131void GpuPixelBuffer::unmap() {
132    if (mAccessMode != kAccessMode_None) {
133        if (mMappedPointer) {
134            mCaches.pixelBufferState().bind(mBuffer);
135            GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
136            if (status == GL_FALSE) {
137                ALOGE("Corrupted GPU pixel buffer");
138            }
139        }
140        mAccessMode = kAccessMode_None;
141        mMappedPointer = nullptr;
142    }
143}
144
145uint8_t* GpuPixelBuffer::getMappedPointer() const {
146    return mMappedPointer;
147}
148
149void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
150    // If the buffer is not mapped, unmap() will not bind it
151    mCaches.pixelBufferState().bind(mBuffer);
152    unmap();
153    glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
154            GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
155}
156
157///////////////////////////////////////////////////////////////////////////////
158// Factory
159///////////////////////////////////////////////////////////////////////////////
160
161PixelBuffer* PixelBuffer::create(GLenum format,
162        uint32_t width, uint32_t height, BufferType type) {
163    if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
164        return new GpuPixelBuffer(format, width, height);
165    }
166    return new CpuPixelBuffer(format, width, height);
167}
168
169}; // namespace uirenderer
170}; // namespace android
171