blob: 6df994c623f23c574b3c487771ecbddb8333ae53 [file] [log] [blame]
Romain Guycf51a412013-04-08 19:40:31 -07001/*
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
Chris Craik96a5c4c2015-01-27 15:46:35 -080017#include "PixelBuffer.h"
Romain Guycf51a412013-04-08 19:40:31 -070018
Romain Guy9e6f3ac2013-06-20 16:31:35 -070019#include "Debug.h"
Romain Guycf51a412013-04-08 19:40:31 -070020#include "Extensions.h"
Romain Guycf51a412013-04-08 19:40:31 -070021#include "Properties.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080022#include "renderstate/RenderState.h"
John Reck2de77712016-01-20 11:09:53 -080023#include "utils/GLUtils.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
25#include <utils/Log.h>
Romain Guycf51a412013-04-08 19:40:31 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// CPU pixel buffer
32///////////////////////////////////////////////////////////////////////////////
33
34class CpuPixelBuffer: public PixelBuffer {
35public:
36 CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
Romain Guycf51a412013-04-08 19:40:31 -070037
Chris Craikd41c4d82015-01-05 15:51:13 -080038 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
39 void unmap() override;
Romain Guycf51a412013-04-08 19:40:31 -070040
Chris Craikd41c4d82015-01-05 15:51:13 -080041 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070042
Chris Craikd41c4d82015-01-05 15:51:13 -080043 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070044
45private:
Chris Craik51d6a3d2014-12-22 17:16:56 -080046 std::unique_ptr<uint8_t[]> mBuffer;
Romain Guycf51a412013-04-08 19:40:31 -070047};
48
Chris Craik51d6a3d2014-12-22 17:16:56 -080049CpuPixelBuffer::CpuPixelBuffer(GLenum format, uint32_t width, uint32_t height)
50 : PixelBuffer(format, width, height)
51 , mBuffer(new uint8_t[width * height * formatSize(format)]) {
Romain Guycf51a412013-04-08 19:40:31 -070052}
53
54uint8_t* CpuPixelBuffer::map(AccessMode mode) {
55 if (mAccessMode == kAccessMode_None) {
56 mAccessMode = mode;
57 }
Chris Craik51d6a3d2014-12-22 17:16:56 -080058 return mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070059}
60
61void CpuPixelBuffer::unmap() {
62 mAccessMode = kAccessMode_None;
63}
64
65uint8_t* CpuPixelBuffer::getMappedPointer() const {
Chris Craik51d6a3d2014-12-22 17:16:56 -080066 return mAccessMode == kAccessMode_None ? nullptr : mBuffer.get();
Romain Guycf51a412013-04-08 19:40:31 -070067}
68
69void CpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
70 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height,
Chris Craik51d6a3d2014-12-22 17:16:56 -080071 mFormat, GL_UNSIGNED_BYTE, &mBuffer[offset]);
Romain Guycf51a412013-04-08 19:40:31 -070072}
73
74///////////////////////////////////////////////////////////////////////////////
75// GPU pixel buffer
76///////////////////////////////////////////////////////////////////////////////
77
78class GpuPixelBuffer: public PixelBuffer {
79public:
80 GpuPixelBuffer(GLenum format, uint32_t width, uint32_t height);
81 ~GpuPixelBuffer();
82
Chris Craikd41c4d82015-01-05 15:51:13 -080083 uint8_t* map(AccessMode mode = kAccessMode_ReadWrite) override;
84 void unmap() override;
Romain Guycf51a412013-04-08 19:40:31 -070085
Chris Craikd41c4d82015-01-05 15:51:13 -080086 uint8_t* getMappedPointer() const override;
Romain Guycf51a412013-04-08 19:40:31 -070087
Chris Craikd41c4d82015-01-05 15:51:13 -080088 void upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) override;
Romain Guycf51a412013-04-08 19:40:31 -070089
90private:
91 GLuint mBuffer;
92 uint8_t* mMappedPointer;
93 Caches& mCaches;
94};
95
Chris Craik96a5c4c2015-01-27 15:46:35 -080096GpuPixelBuffer::GpuPixelBuffer(GLenum format,
97 uint32_t width, uint32_t height)
Chris Craikd41c4d82015-01-05 15:51:13 -080098 : PixelBuffer(format, width, height)
99 , mMappedPointer(nullptr)
Chris Craik96a5c4c2015-01-27 15:46:35 -0800100 , mCaches(Caches::getInstance()){
Romain Guycf51a412013-04-08 19:40:31 -0700101 glGenBuffers(1, &mBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800102
Chris Craik44eb2c02015-01-29 09:45:09 -0800103 mCaches.pixelBufferState().bind(mBuffer);
Chris Craikd41c4d82015-01-05 15:51:13 -0800104 glBufferData(GL_PIXEL_UNPACK_BUFFER, getSize(), nullptr, GL_DYNAMIC_DRAW);
Chris Craik44eb2c02015-01-29 09:45:09 -0800105 mCaches.pixelBufferState().unbind();
Romain Guycf51a412013-04-08 19:40:31 -0700106}
107
108GpuPixelBuffer::~GpuPixelBuffer() {
109 glDeleteBuffers(1, &mBuffer);
110}
111
112uint8_t* GpuPixelBuffer::map(AccessMode mode) {
113 if (mAccessMode == kAccessMode_None) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800114 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700115 mMappedPointer = (uint8_t*) glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, getSize(), mode);
John Reck2de77712016-01-20 11:09:53 -0800116 if (CC_UNLIKELY(!mMappedPointer)) {
117 GLUtils::dumpGLErrors();
118 LOG_ALWAYS_FATAL("Failed to map PBO");
Romain Guy9e6f3ac2013-06-20 16:31:35 -0700119 }
Romain Guycf51a412013-04-08 19:40:31 -0700120 mAccessMode = mode;
121 }
122
123 return mMappedPointer;
124}
125
126void GpuPixelBuffer::unmap() {
127 if (mAccessMode != kAccessMode_None) {
128 if (mMappedPointer) {
Chris Craik44eb2c02015-01-29 09:45:09 -0800129 mCaches.pixelBufferState().bind(mBuffer);
Romain Guy03c00b52013-06-20 18:30:28 -0700130 GLboolean status = glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
131 if (status == GL_FALSE) {
132 ALOGE("Corrupted GPU pixel buffer");
133 }
Romain Guycf51a412013-04-08 19:40:31 -0700134 }
135 mAccessMode = kAccessMode_None;
Chris Craikd41c4d82015-01-05 15:51:13 -0800136 mMappedPointer = nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700137 }
138}
139
140uint8_t* GpuPixelBuffer::getMappedPointer() const {
141 return mMappedPointer;
142}
143
144void GpuPixelBuffer::upload(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int offset) {
145 // If the buffer is not mapped, unmap() will not bind it
Chris Craik44eb2c02015-01-29 09:45:09 -0800146 mCaches.pixelBufferState().bind(mBuffer);
Romain Guycf51a412013-04-08 19:40:31 -0700147 unmap();
148 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, mFormat,
Kévin PETIT73fc5582014-02-13 11:03:40 +0000149 GL_UNSIGNED_BYTE, reinterpret_cast<void*>(offset));
Romain Guycf51a412013-04-08 19:40:31 -0700150}
151
152///////////////////////////////////////////////////////////////////////////////
153// Factory
154///////////////////////////////////////////////////////////////////////////////
155
Chris Craik96a5c4c2015-01-27 15:46:35 -0800156PixelBuffer* PixelBuffer::create(GLenum format,
157 uint32_t width, uint32_t height, BufferType type) {
Romain Guyf9f00162013-05-09 11:50:12 -0700158 if (type == kBufferType_Auto && Caches::getInstance().gpuPixelBuffersEnabled) {
159 return new GpuPixelBuffer(format, width, height);
Romain Guycf51a412013-04-08 19:40:31 -0700160 }
161 return new CpuPixelBuffer(format, width, height);
162}
163
164}; // namespace uirenderer
165}; // namespace android